class TestSearches(unittest.TestCase): def setUp(self): from fhirbug.server.requesthandlers import GetRequestHandler self.handler = GetRequestHandler() def test_lt_datetime(self): url = "Patient?birthDate=lt2000-02-02T12:34" res, status = self.handler.handle(url) self.assertEqual(status, 200) self.assertEqual(res["total"], 13) def test_lt_date(self): url = "Patient?birthDate=gt1990-02-02" res, status = self.handler.handle(url) self.assertEqual(status, 200) self.assertEqual(res["total"], 5) def test_ne_date(self): url = "Patient?birthDate=ne1982-01" res, status = self.handler.handle(url) self.assertEqual(status, 200) self.assertEqual(res["total"], 21) def test_combined(self): url = "Patient?birthDate=gt1990-02-02&birthDate=lt2000-02-02T12:34" res, status = self.handler.handle(url) self.assertEqual(status, 200) self.assertEqual(res["total"], 1) def test_year_only(self): url = "Patient?deceasedDateTime=2015-02" res, status = self.handler.handle(url) self.assertEqual(status, 200) self.assertEqual(res["total"], 1)
def test_handle_failure(self): handler = GetRequestHandler() handler.parse_url = Mock(side_effect=OperationError) handler.log_request = Mock() urlMock = Mock() contextMock = Mock() ret, status = handler.handle(urlMock, contextMock) handler.parse_url.assert_called_with(urlMock, contextMock) handler.log_request.assert_called_once() self.assertEqual( ret, { "issue": [{ "code": "exception", "diagnostics": "", "severity": "error" }], "resourceType": "OperationOutcome", }, ) self.assertEqual(status, 500)
def test_fetch_items_success(self): handler = GetRequestHandler() mockQuery = Mock() handler.query = mockQuery modelMock = Mock() res = handler.fetch_items(modelMock) modelMock.get.assert_called_with(query=mockQuery) self.assertEqual(res, modelMock.get())
def test_fetch_items_exceptions(self): handler = GetRequestHandler() mockQuery = Mock() handler.query = mockQuery modelMock = Mock() modelMock.get = Mock(side_effect=MappingValidationError) with self.assertRaises(OperationError) as e: res = handler.fetch_items(modelMock) self.assertEqual(e.exception.status_code, 404) modelMock = Mock() modelMock.get = Mock(side_effect=AuthorizationError(Mock())) with self.assertRaises(OperationError) as e: res = handler.fetch_items(modelMock) self.assertEqual(e.exception.status_code, 403) modelMock = Mock() modelMock.get = Mock(side_effect=Exception) with self.assertRaises(OperationError) as e: res = handler.fetch_items(modelMock) self.assertEqual(e.exception.status_code, 500) with patch("fhirbug.server.requesthandlers.settings") as settingsMock: modelMock = Mock() modelMock.get = Mock(side_effect=Exception) with self.assertRaises(OperationError) as e: res = handler.fetch_items(modelMock) self.assertEqual(e.exception.status_code, 500) self.assertTrue("Traceback" in e.exception.diagnostics)
def test_handle_success(self): handler = GetRequestHandler() handler.parse_url = Mock() handler.query = Mock() handler._audit_request = Mock() handler.import_models = Mock() handler.get_resource = Mock() handler.fetch_items = Mock() handler.log_request = Mock() urlMock = Mock() ret, status = handler.handle(urlMock) handler.parse_url.assert_called_with(urlMock, None) handler._audit_request.assert_called_with(handler.query) handler.import_models.assert_called_once() handler.get_resource.assert_called_with(handler.import_models()) handler.fetch_items.assert_called_with(handler.get_resource()) handler.log_request.assert_called_once() self.assertEqual(ret, handler.fetch_items()) self.assertEqual(status, 200)
def setUp(self): from fhirbug.server.requesthandlers import GetRequestHandler self.handler = GetRequestHandler()
class TestBundles(unittest.TestCase): def setUp(self): from fhirbug.server.requesthandlers import GetRequestHandler self.handler = GetRequestHandler() def test_404(self): """ 404 """ url = "does-not-exist" res, status = self.handler.handle(url) self.assertEqual(status, 404) self.assertEqual(res["resourceType"], "OperationOutcome") def test_patients_bundle(self): """ GET /Patient/ """ url = "Patient" res, status = self.handler.handle(url) self.assertEqual(status, 200) self.assertEqual(len(examples["Patient"]), res["total"]) self.assertEqual( res["link"], [{ "relation": "next", "url": "Patient/?_count=20&search-offset=21" }], ) def test_query_limits(self): """ Bundle limits """ url = "Procedure?_count=7&search-offset=12" res, status = self.handler.handle(url) self.assertEqual(status, 200) self.assertEqual(len(res["entry"]), 7) self.assertEqual( res["link"], [ { "relation": "next", "url": "Procedure/?_count=7&search-offset=19" }, { "relation": "previous", "url": "Procedure/?_count=7&search-offset=5" }, ], ) def test_get_one(self): """ GET a single resource """ id = examples["Practitioner"][0]["id"] url = f"Practitioner/{id}" res, status = self.handler.handle(url) self.assertEqual(status, 200) self.assertEqual(res["id"], id)