def test_find_by_name_with_expected_name(self):
     with self.app_context():
         repository = PolygonRepository(db.session)
         polygon = repository.find_by_name('Polygon')
         self.assertIsNotNone(polygon, "Expected to find an area.")
         self.assertEqual(polygon.name, "Polygon",
                          "Expected to find an area with name Polygon.")
    def test_find_intersected_area(self):
        with self.app_context():
            repository = PolygonRepository(db.session)
            polygons = repository.find_intersected_area(
                'POLYGON((5 2, 7 6, 9 5, 5 2))')

            self.assertEqual(len(list(polygons)), 1)
 def test_find_by_properties(self):
     with self.app_context():
         repository = PolygonRepository(db.session)
         polygons = repository.find_by_properties(
             '{"property2":"value21","property5":"value5"}')
         expected = ['testPolygon2']
         polygons = list(polygons)
         self.assertEqual(len(polygons), len(expected))
 def test_find_like_name_returns_empty(self):
     with self.app_context():
         repository = PolygonRepository(db.session)
         polygons = repository.find_like_name('alejandro')
         expected = []
         self.assertListEqual(
             list(polygons), expected,
             "Expected list should be equal to the retrieved list of names."
         )
 def test_is_valid_simple_polygon(self):
     with self.app_context():
         repository = PolygonRepository(db.session)
         geometry = {
             "type": "Polygon",
             "coordinates": [[[0, 4], [4, 4], [7, 2], [0, 4]]]
         }
         valid = repository.is_closed_polygon(geometry)
         self.assertTrue(valid, "It was expected to be valid.")
 def test_find_by_area_intersects_areas(self):
     with self.app_context():
         repository = PolygonRepository(db.session)
         polygons = repository.find_by_area('POLYGON((5 2, 7 6, 9 5, 5 2))')
         expected = ['PolygonWithHole', 'testPolygon3', 'testPolygon2']
         polygon_names = [polygon.name for polygon in polygons]
         self.assertListEqual(
             polygon_names, expected,
             "Expected list should be equal to the retrieved list of names."
         )
 def test_find_by_area_no_intersect(self):
     with self.app_context():
         repository = PolygonRepository(db.session)
         polygons = repository.find_by_area(
             'POLYGON((25 22, 27 26, 29 25, 25 22))')
         expected = []
         polygon_names = [polygon.name for polygon in polygons]
         self.assertListEqual(
             polygon_names, expected,
             "Expected list should be equal to the retrieved list of names."
         )
 def test_is_valid_complex_polygon(self):
     with self.app_context():
         repository = PolygonRepository(db.session)
         geometry = {
             "type":
             "Polygon",
             "coordinates": [[[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]],
                             [[2, 2], [2, 4], [4, 4], [4, 2], [2, 2]],
                             [[5.5, 5.5], [5, 7], [7, 5], [5.5, 5.5]]]
         }
         valid = repository.is_closed_polygon(geometry)
         self.assertTrue(valid, "It was expected to be valid.")
    def test_find_like_name(self):
        with self.app_context():
            repository = PolygonRepository(db.session)
            polygons = repository.find_like_name('olygon')
            expected = [
                'Polygon', 'PolygonWithHole', 'testPolygon2', 'testPolygon3'
            ]

            polygon_names = [polygon.name for polygon in polygons]
            self.assertListEqual(
                polygon_names, expected,
                "Expected list should be equal to the retrieved list of names."
            )
 def test_serializer(self):
     with self.app_context():
         repository = PolygonRepository(db.session)
         polygon = repository.find_by_name('Polygon')
         serializer = PolygonSerializer(repository)
         expected = {
             'name': 'Polygon',
             'date': '2020-06-16T15:36:38',
             'properties': {
                 'property1': 'value11',
                 'property4': 'value4'
             },
             'geom': {
                 'type': 'Polygon',
                 'coordinates': [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]
             }
         }
         self.assertDictEqual(serializer.serialize(polygon), expected)
    def test_crud(self):
        with self.app_context():
            repository = PolygonRepository(db.session)
            geom = repository.geojson_to_geo(GEOMETRY)
            polygon = Polygon('testpolygon3', "2020-08-21T18:25:43", geom, {
                "prop1": "value1",
                "prop2": "value2"
            })
            saved = repository.save(polygon)
            self.assertIsNotNone(saved, "Expected to save the polygon.")
            self.assertEqual(saved, polygon,
                             "saved polygon to be equal to the original.")

            retrieved = repository.find_by_name(polygon.name)
            self.assertEqual(retrieved, saved,
                             "retrieved polygon to be equal to the saved.")

            repository.delete(polygon.name)
            retrieved = repository.find_by_name(polygon.name)
            self.assertIsNone(retrieved, "Polygon not expected to be found.")
 def test_geom_from_json(self):
     with self.app_context():
         repository = PolygonRepository(db.session)
         with raises(InternalError):
             repository.geojson_to_geo(INVALID_GEOMETRY)
 def test_is_valid_open_polygon(self):
     with self.app_context():
         repository = PolygonRepository(db.session)
         valid = repository.is_closed_polygon(INVALID_GEOMETRY)
         self.assertFalse(valid, "It was expected to be invalid.")
 def test_find_by_name_with_not_expected_name(self):
     with self.app_context():
         repository = PolygonRepository(db.session)
         polygon = repository.find_by_name('alejandro')
         self.assertIsNone(polygon, "It was not expected to find an area.")
Exemplo n.º 15
0
from app.repository.polygon_repository import PolygonRepository
from app.resources.polygon_resource import PolygonResource
from app.services.polygon_services import PolygonSerializer, PolygonRegistrator, PolygonQuerySolver, PolygonEraser
from app.services.validator import SavePolygonValidator
from app.utilities.db import db

logging.basicConfig(level=logging.INFO)

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get(
    'DATABASE_URL',
    'postgresql://*****:*****@polygon-api_postgis_1:5432/postgres'
)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
api = Api(app)
repository = PolygonRepository(db.session)
serializer = PolygonSerializer(repository)
registrator = PolygonRegistrator(repository)
query_solver = PolygonQuerySolver(repository, serializer)
eraser = PolygonEraser(repository)
validator = SavePolygonValidator(repository)

api.add_resource(PolygonResource,
                 "/area",
                 resource_class_kwargs={
                     'serializer': serializer,
                     'registrator': registrator,
                     'query_solver': query_solver,
                     'eraser': eraser,
                     'validator': validator
                 })