コード例 #1
0
class SearchByArea(object):
    def __init__(self, logger, config):
        self.logger = logger
        self.local_land_charge_service = LocalLandChargeService(config)

    def process(self, bounding_box):
        response = dict()
        search_response = self.process_request(bounding_box)

        response['status'] = search_response.status_code
        if search_response.status_code == 200:
            response['data'] = search_response.json()
        return response

    def process_request(self, bounding_box):
        if bounding_box:
            return self.get_results_for_boundary(bounding_box)

    def get_results_for_boundary(self, bounding_box):
        self.logger.info("Searching area by bounding box")
        return self.local_land_charge_service.get(
            self.prepare_bounding_box(bounding_box))

    @staticmethod
    def prepare_bounding_box(bounding_box):
        return SearchByArea.build_bounding_box_json(bounding_box)

    @staticmethod
    def build_bounding_box_json(bounding_box):
        geo_dict = {
            "type": "Polygon",
            "coordinates": json.loads(bounding_box),
            "crs": {
                "type": "name",
                "properties": {
                    "name": "EPSG:27700"
                }
            }
        }

        return json.dumps(geo_dict)
    def test_get_with_bounding_box(self, mock_current_app):
        with main.app.test_request_context():
            g.requests = MagicMock()
            response = MagicMock()
            response.status_code = 200
            response.json.return_value = {'results': [{"abc": "def"}]}

            g.requests.post.return_value = response
            search_api = 'abc'
            mock_current_app.config = {'SEARCH_API_URL': search_api}

            local_land_charge_service = LocalLandChargeService(mock_current_app.config)

            response = local_land_charge_service.get(BASE64_BOUNDING_BOX)

            self.assertEqual(response.status_code, 200)

            g.requests.post.assert_called_with(
                "http://{}/v2.0/search/local_land_charges".format(search_api),
                data=BASE64_BOUNDING_BOX,
                headers={'Content-Type': 'application/json'}
            )