def test_post_data_fields_present(self, mock_get, session, TestProfiledQuantity): mock_get.return_value.json.return_value = { "count": 2, "data": [{ "code": "kg" }, { "code": "<" }], } post = TestProfiledQuantity( id=1, quantity={ "code": "code", "system": "http://unitsofmeasure.org", "unit": "kg", "value": 2.400023, "comparator": "<", }, ) session.execute(""" CREATE TABLE test_quantity ( id INTEGER, quantity fhir_quantity);""") session.add(post) session.commit() register_composites(session.connection()) get = session.query(TestProfiledQuantity).first() assert get.id == 1 assert get.quantity.code == "code"
def test_warning_if_system_is_not_unitsofmeasure_org( self, mock_get, session, TestSimpleQuantityModel): mock_get.return_value.json.return_value = { "count": 2, "data": [{ "code": "kg" }] } post = TestSimpleQuantityModel( id=1, simplequantity={ "code": "code", "system": "http://someotheruom.org", "unit": "kg", "value": 2.400023, }, ) session.execute(""" CREATE TABLE test_simplequantity ( id INTEGER, simplequantity fhir_simplequantity);""") session.add(post) register_composites(session.connection()) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") session.commit() assert len(w) == 1 assert "Your are using a different unit of measure system." in str( w[-1].message) get = session.query(TestSimpleQuantityModel).first() assert get.id == 1 assert get.simplequantity.system == "http://someotheruom.org"
def test_post_data(self, mock_get, session, TestSimpleQuantityModel): mock_get.return_value.json.return_value = { "count": 2, "data": [{ "code": "kg" }] } post = TestSimpleQuantityModel( id=1, simplequantity={ "code": "code", "system": "http://unitsofmeasure.org", "unit": "kg", "value": 2.400023, }, ) session.execute(""" CREATE TABLE test_simplequantity ( id INTEGER, simplequantity fhir_simplequantity);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestSimpleQuantityModel).first() assert get.id == 1 assert get.simplequantity.value == Decimal("2.400023")
def test_save_practitioner(self, mock_get, session): mock_get.return_value.json.return_value = { "count": len(self.valuesets_data), "data": self.valuesets_data, } data = Practitioner( id=self.id, implicitRules=self.implicitRules, language=self.language, identifier=self.identifier, active=self.active, name=self.name, telecom=self.telecom, address=self.address, gender=self.gender, birthDate=self.birthDate, photo=self.photo, role=self.role, qualification=self.qualification, communication=self.communication, ) session.add(data) session.commit() register_composites(session.connection()) get = session.query(Practitioner).first() assert get.id == "1" assert get.language == "en"
def test_post_data(self, session, TestAttachmentModel): data = "data to be encoded" byte_data = b"data to be encoded" d_hash = hashlib.sha1(byte_data).digest() post = TestAttachmentModel( id=1, attachment={ "contentType": "application/pdf", "creation": "2011-05-24", "data": data, "hash": d_hash, "language": "en", "size": 21, "title": "title", "url": "url", }, ) session.execute(""" CREATE TABLE test_attachment ( id INTEGER, attachment fhir_attachment);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestAttachmentModel).first() assert get.id == 1 assert get.attachment.contentType == "application/pdf"
def test_post_data_with_null_sampleddata_field(self, session, TestSampledDataModel): post = TestSampledDataModel( id=1, sampleddata={ "data": "2.3U", "dimensions": 3, "period": 0.1, "origin": { "code": "code", "system": "system", "unit": "kg", "value": 2.400023, }, }, ) session.execute(""" CREATE TABLE test_sampleddata ( id INTEGER, sampleddata fhir_sampleddata);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestSampledDataModel).first() assert get.id == 1 assert get.sampleddata.factor is None
def session(self, request, engine, connection, Base, Account): sa.orm.configure_mappers() Session = sessionmaker(bind=connection) session = Session() session.execute( "CREATE TYPE money_type AS (currency VARCHAR, amount INTEGER)" ) session.execute( """CREATE TABLE account ( id SERIAL, balance MONEY_TYPE, PRIMARY KEY(id) )""" ) def teardown(): session.execute('DROP TABLE account') session.execute('DROP TYPE money_type') session.commit() session.close_all() connection.close() remove_composite_listeners() engine.dispose() register_composites(connection) request.addfinalizer(teardown) return session
def test_post_data_fields_present(self, mock_get, session, TestProfiledHumanName): mock_get.return_value.json.return_value = { "count": 1, "data": [{"code": "official"}], } post = TestProfiledHumanName( id=1, humanname={ "family": ["family", "family2"], "given": ["given", "given2"], "prefix": ["prefix", "prefix2"], "suffix": ["suffix", "suffix2"], "text": "family given", "use": "official", "period": {"start": "2011-05-24", "end": "2011-06-24"}, }, ) session.execute( """ CREATE TABLE test_humanname ( id INTEGER, humanname fhir_humanname);""" ) session.add(post) session.commit() register_composites(session.connection()) get = session.query(TestProfiledHumanName).first() assert get.id == 1 assert get.humanname.use == "official"
def test_save_organization(self, mock_get, mock_ref, session): mock_ref.return_value = True mock_get.return_value.json.return_value = { "count": len(self.valuesets_data), "data": self.valuesets_data, } data = Organization( id=self.id, implicitRules=self.implicitRules, language=self.language, active=self.active, name=self.name, identifier=self.identifier, type=self.org_type, telecom=self.telecom, address=None, partOf=self.partOf, contact=self.contact, meta=self.meta, ) session.add(data) session.commit() register_composites(session.connection()) get = session.query(Organization).first() assert get.name == "Test Organization"
def setup_method(self, method): self.engine = create_engine(self.dns) self.engine.echo = True self.connection = self.engine.connect() self.Base = declarative_base() pg_composite.registered_composites = {} self.type = CompositeType( 'money_type', [ sa.Column('currency', sa.String), sa.Column('amount', sa.Integer) ] ) self.create_models() sa.orm.configure_mappers() Session = sessionmaker(bind=self.connection) self.session = Session() self.session.execute( "CREATE TYPE money_type AS (currency VARCHAR, amount INTEGER)" ) self.session.execute( """CREATE TABLE account ( id SERIAL, balance MONEY_TYPE, PRIMARY KEY(id) )""" ) register_composites(self.connection)
def test_post_data_field_city_present(self, session, TestProfiledAnnotation): post = TestProfiledAnnotation( id=1, annotation={ "authorString": "author string", "text": "text", "time": "2011-05-24", "authorReference": { "reference": "reference url", "display": "Patient X", }, }, ) session.execute(""" CREATE TABLE test_annotation ( id INTEGER, annotation fhir_annotation);""") session.add(post) session.commit() register_composites(session.connection()) get = session.query(TestProfiledAnnotation).first() assert get.id == 1 assert get.annotation.authorString == "author string"
def test_post_data_with_external_reference(self, mock_get, session, TestReferenceModel): mock_get.return_value = Response(status=200) post = TestReferenceModel( id=1, reference={ "display": "display", "reference": "http://spark.furore.com/fhir/Patient/89/" "_history/spark45", }, ) session.execute(""" CREATE TABLE test_reference ( id INTEGER, reference fhir_reference);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestReferenceModel).first() assert get.id == 1 assert get.reference.display == "display"
def test_post_data(self, mock_get, session, TestNarrativeModel): mock_get.return_value.json.return_value = { "count": 2, "data": [{ "code": "generated" }], } post = TestNarrativeModel( id=1, narrative={ "div": "<a style=someattribute>someattribute</a><b>strong</b>", "status": "generated", }, ) session.execute(""" CREATE TABLE test_narrative ( id INTEGER, narrative fhir_narrative);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestNarrativeModel).first() assert get.id == 1 assert get.narrative.status == "generated"
def test_post_data(self, mock_get, session, TestContactPointModel): mock_get.return_value.json.return_value = { "count": 2, "data": [{ "code": "phone" }, { "code": "home" }], } post = TestContactPointModel( id=1, contactpoint={ "rank": 2, "system": "phone", "use": "home", "value": "+254712122988", "period": { "start": "2011-05-24", "end": "2011-06-24" }, }, ) session.execute(""" CREATE TABLE test_contactpoint ( id INTEGER, contactpoint fhir_contactpoint);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestContactPointModel).first() assert get.id == 1 assert get.contactpoint.value == "+254712122988"
def test_post_data_with_use_not_defined_in_valueset( self, mock_get, session, TestContactPointModel): mock_get.return_value.json.return_value = { "count": 2, "data": [{ "code": "phone" }], } post = TestContactPointModel( id=1, contactpoint={ "rank": 2, "system": "phone", "use": "nothere", "value": "+254712122988", "period": { "start": "2011-05-24", "end": "2011-06-24" }, }, ) session.execute(""" CREATE TABLE test_contactpoint ( id INTEGER, contactpoint fhir_contactpoint);""") session.add(post) register_composites(session.connection()) with pytest.raises(StatementError) as excinfo: session.commit() assert "The contactpoint use must be defined in" in str(excinfo.value)
def test_post_data(self, mock_get, session, TestAgeModel): mock_get.return_value.json.return_value = { "count": 2, "data": [{ "code": "mo" }, { "code": "<" }], } post = TestAgeModel( id=1, age={ "code": "mo", "system": "http://unitsofmeasure.org", "unit": "mo", "value": 24, "comparator": "<", }, ) session.execute(""" CREATE TABLE test_age ( id INTEGER, age fhir_age);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestAgeModel).first() assert get.id == 1 assert get.age.value == 24
def test_post_data(self, mock_get, session, TestDistanceModel): mock_get.return_value.json.return_value = {"count": 2, "data": [{"code": "<"}]} post = TestDistanceModel( id=1, distance={ "code": "code", "system": "system", "unit": "kg", "value": 2.400023, "comparator": "<", }, ) session.execute( """ CREATE TABLE test_distance ( id INTEGER, distance fhir_distance);""" ) session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestDistanceModel).first() assert get.id == 1 assert get.distance.value == Decimal("2.400023")
def test_post_data(self, session, TestSampledDataModel): post = TestSampledDataModel( id=1, sampleddata={ "data": "2.1E", "dimensions": 3, "factor": 0.2333, "lowerLimit": 1.5, "period": 0.1, "upperLimit": 7.9, "origin": { "code": "code", "system": "system", "unit": "kg", "value": 2.400023, }, }, ) session.execute(""" CREATE TABLE test_sampleddata ( id INTEGER, sampleddata fhir_sampleddata);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestSampledDataModel).first() assert get.id == 1 assert get.sampleddata.upperLimit == Decimal("7.9")
def test_reject_content_containing_empty_string(self, mock_get, session, TestNarrativeModel): mock_get.return_value.json.return_value = { "count": 2, "data": [{ "code": "generated" }], } post = TestNarrativeModel(id=1, narrative={ "div": " ", "status": "generated" }) session.execute(""" CREATE TABLE test_narrative ( id INTEGER, narrative fhir_narrative);""") session.add(post) register_composites(session.connection()) with pytest.raises(StatementError) as excinfo: session.commit() assert "narrative content must not be an empty string" in str( excinfo.value)
def test_post_data(self, session, TestRangeModel): post = TestRangeModel( id=1, range={ "high": { "code": "code", "system": "system", "unit": "kg", "value": 2.400023, }, "low": { "code": "code", "system": "system", "unit": "kg", "value": 0.400023, }, }, ) session.execute(""" CREATE TABLE test_range ( id INTEGER, range fhir_range);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestRangeModel).first() assert get.id == 1 assert get.range.high.unit == "kg"
def test_reject_data_with_invalid_attributes(self, mock_get, session, TestNarrativeModel): mock_get.return_value.json.return_value = { "count": 2, "data": [{ "code": "generated" }], } post = TestNarrativeModel( id=1, narrative={ "div": "<a noattr=someattribute></a><b>strong</b>", "status": "generated", }, ) session.execute(""" CREATE TABLE test_narrative ( id INTEGER, narrative fhir_narrative);""") session.add(post) register_composites(session.connection()) with pytest.raises(StatementError) as excinfo: session.commit() assert "The attribute noattr is not valid in" in str(excinfo.value)
def test_save_location(self, mock_get, mock_ref, session): mock_ref.return_value = True mock_get.return_value.json.return_value = { "count": len(self.valueset_data), "data": self.valueset_data, } data = Location( id=self.id, implicitRules=self.implicitRules, language=self.language, identifier=self.identifier, status=self.status, name=self.name, description=self.description, mode=self.mode, type=self.locationType, telecom=self.telecom, address=self.address, physicalType=self.physical_type, position=self.position, managingOrganization=self.managingOrganization, partOf=self.part_of, ) session.add(data) session.commit() register_composites(session.connection()) get = session.query(Location).first() assert get.id == "1" assert get.language == "EN"
def test_save_operationoutcome(self, mock_get, session): mock_get.return_value.json.return_value = { "count": len(self.valueset_data), "data": self.valueset_data, } data = OperationOutcome(id=self.id, issue=self.issue) session.add(data) session.commit() register_composites(session.connection()) get = session.query(OperationOutcome).first() assert get.id == "1" assert get.issue[0].code == "invalid"
def test_post_data_with_null_duration_field(self, session, TestAgeModel): post = TestAgeModel(id=1, age={}) session.execute(""" CREATE TABLE test_age ( id INTEGER, age fhir_age);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestAgeModel).first() assert get.id == 1 assert get.age.code is None
def test_post_data_with_null_coding_field(self, session, TestCodingModel): post = TestCodingModel(id=1, coding={}) session.execute(""" CREATE TABLE test_coding ( id INTEGER, coding fhir_coding);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestCodingModel).first() assert get.id == 1 assert get.coding.code is None
def test_post_data_with_null_money_field(self, session, TestMoneyModel): post = TestMoneyModel(id=1, money={}) session.execute(""" CREATE TABLE test_money ( id INTEGER, money fhir_money);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestMoneyModel).first() assert get.id == 1 assert get.money.code is None
def test_post_data_with_null_period_field(self, session, TestPeriodModel): post = TestPeriodModel(id=1, period={}) session.execute(""" CREATE TABLE test_period ( id INTEGER, period fhir_period);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestPeriodModel).first() assert get.id == 1 assert get.period.end is None
def create_app(config=None, app_name=None, blueprints=None): """Create a Flask app.""" if app_name is None: app_name = DefaultConfig.PROJECT if blueprints is None: blueprints = DEFAULT_BLUEPRINTS app = FhirAPI(app_name) configure_app(app, config) app.config.setdefault("SQLALCHEMY_TRACK_MODIFICATIONS", True) app.config["CACHE_TYPE"] = "redis" app.config["TRAP_HTTP_EXCEPTIONS"] = True app.config["DEFAULT_RENDERERS"] = [ "flask_api.renderers.JSONRenderer", "flask_api.renderers.BrowsableAPIRenderer", ] app.config["DEFAULT_PARSERS"] = [ "flask_api.parsers.JSONParser", "flask_api.parsers.URLEncodedParser", "flask_api.parsers.MultiPartParser", ] app.config.from_object(os.environ["APP_SETTINGS"]) db.init_app(app) configure_extensions(app) configure_blueprints(app, blueprints) if not app.config["DEBUG"]: # Override default Flask error handlers if DEBUG = False configure_error_handlers(app) db.app = app conn = db.session.connection() register_composites(conn) with app.app_context(): # Extensions like Flask-SQLAlchemy now know what the "current" app # is while within this block. Without this you get an extension not # registered error db.create_all() if app.debug: print("running in debug mode") else: print("NOT running in debug mode") return app
def test_post_data_with_null_simplequantity_field(self, session, TestSimpleQuantityModel): post = TestSimpleQuantityModel(id=1, simplequantity={}) session.execute(""" CREATE TABLE test_simplequantity ( id INTEGER, simplequantity fhir_simplequantity);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestSimpleQuantityModel).first() assert get.id == 1 assert get.simplequantity.code is None
def test_post_data_with_null_attachment_field(self, session, TestAttachmentModel): post = TestAttachmentModel(id=1, attachment={}) session.execute(""" CREATE TABLE test_attachment ( id INTEGER, attachment fhir_attachment);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestAttachmentModel).first() assert get.id == 1 assert get.attachment.data is None
def test_post_null_data_for_address_fields(self, session, TestAddressModel): post = TestAddressModel(id=1, address={}) session.execute(""" CREATE TABLE test_address ( id INTEGER, address fhir_address);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestAddressModel).first() assert get.id == 1 assert get.address.district is None
def test_post_data_with_null_reference_field(self, session, TestReferenceModel): post = TestReferenceModel(id=1, reference={}) session.execute(""" CREATE TABLE test_reference ( id INTEGER, reference fhir_reference);""") session.add(post) register_composites(session.connection()) session.commit() get = session.query(TestReferenceModel).first() assert get.id == 1 assert get.reference.display is None