Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add model factory #28

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add model factory
  • Loading branch information
vincentsarago committed May 6, 2021
commit 9a0954a14ed92bab9ca786358ec945c53b581a56
102 changes: 100 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,103 @@
*.egg-info/
# Byte-compiled / optimized / DLL files
__pycache__/
.tox
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# dotenv
.env

# virtualenv
.venv
venv/
ENV/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

.pytest_cache
15 changes: 15 additions & 0 deletions geojson_pydantic/factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""geojson-pydantic model factory"""

from typing import Type

from pydantic import BaseModel, Field, create_model

from geojson_pydantic.features import Feature
from geojson_pydantic.geometries import Geometry


def model_factory(geom: Geometry) -> Type[BaseModel]:
"""Create Feature Model."""
return create_model(
"Feature", geometry=(geom, Field(title="Geometry")), __base__=Feature,
)
79 changes: 79 additions & 0 deletions test/test_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import pytest

from geojson_pydantic.factory import model_factory
from geojson_pydantic.geometries import (
LineString,
MultiLineString,
MultiPoint,
MultiPolygon,
Point,
Polygon,
)

geom_types = [
(Point, {"type": "Point", "coordinates": [1, 2]}),
(
LineString,
{
"type": "LineString",
"coordinates": [[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]],
},
),
(
Polygon,
{
"type": "Polygon",
"coordinates": [
[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]
],
},
),
(MultiPoint, {"type": "MultiPoint", "coordinates": [[100.0, 0.0], [101.0, 1.0]]}),
(
MultiLineString,
{
"type": "MultiLineString",
"coordinates": [[[100.0, 0.0], [101.0, 1.0]], [[102.0, 2.0], [103.0, 3.0]]],
},
),
(
MultiPolygon,
{
"type": "MultiPolygon",
"coordinates": [
[
[
[102.0, 2.0],
[103.0, 2.0],
[103.0, 3.0],
[102.0, 3.0],
[102.0, 2.0],
]
],
[
[
[100.0, 0.0],
[101.0, 0.0],
[101.0, 1.0],
[100.0, 1.0],
[100.0, 0.0],
],
[
[100.2, 0.2],
[100.8, 0.2],
[100.8, 0.8],
[100.2, 0.8],
[100.2, 0.2],
],
],
],
},
),
]


@pytest.mark.parametrize("geom", geom_types)
def test_factory(geom):
"""test if feature collection is iterable"""
Feature = model_factory(geom[0])
assert Feature(geometry=geom[1])