async def test_get_geography(app, access_token_builder, dummy_zmq_server): """ Test that JSON is returned when getting a query. """ client, db, log_dir, app = app aggregation_unit = "DUMMY_AGGREGATION" # Set the rows returned by iterating over the rows from the db # This is a long chain of mocks corresponding to getting a connection using # the pool's context manager, getting the cursor on that, and then looping # over the values in cursor db.acquire.return_value.__aenter__.return_value.cursor.return_value.__aiter__.return_value = [ { "some": "valid" }, { "json": "bits" }, ] token = access_token_builder({ "geography": { "permissions": { "get_result": True }, "spatial_aggregation": [aggregation_unit], } }) zmq_reply = ZMQReply(status="success", payload={ "query_state": "completed", "sql": "SELECT 1;" }) dummy_zmq_server.side_effect = (zmq_reply.as_json(), ) response = await client.get( f"/api/0/geography/{aggregation_unit}", headers={"Authorization": f"Bearer {token}"}, ) gjs = loads(await response.get_data()) assert 200 == response.status_code assert "FeatureCollection" == gjs["type"] assert [{"some": "valid"}, {"json": "bits"}] == gjs["features"] assert "application/geo+json" == response.headers["content-type"] assert (f"attachment;filename={aggregation_unit}.geojson" == response.headers["content-disposition"])
async def test_get_geography_status(status, http_code, app, dummy_zmq_server, access_token_builder): """ Test that correct status code is returned when server returns an error. """ client, db, log_dir, app = app token = access_token_builder({ "geography": { "permissions": { "get_result": True }, "spatial_aggregation": ["DUMMY_AGGREGATION"], } }) zmq_reply = ZMQReply(status="error", msg="Some error") dummy_zmq_server.side_effect = (zmq_reply.as_json(), ) response = await client.get( f"/api/0/geography/DUMMY_AGGREGATION", headers={"Authorization": f"Bearer {token}"}, ) assert http_code == response.status_code