def test_multiple_rps_in_fhir(self):
        yesterday = datetime.utcnow() - timedelta(days=1)
        lastyear = datetime.utcnow() - timedelta(days=365)
        org = Organization(name='Testy')
        rp1 = ResearchProtocol(name='rp1')
        rp2 = ResearchProtocol(name='yesterday')
        rp3 = ResearchProtocol(name='last year')
        with SessionScope(db):
            map(db.session.add, (org, rp1, rp2, rp3))
            db.session.commit()
        org, rp1, rp2, rp3 = map(db.session.merge, (org, rp1, rp2, rp3))
        o_rp1 = OrganizationResearchProtocol(
            research_protocol=rp1, organization=org)
        o_rp2 = OrganizationResearchProtocol(
            research_protocol=rp2, organization=org, retired_as_of=yesterday)
        o_rp3 = OrganizationResearchProtocol(
            research_protocol=rp3, organization=org, retired_as_of=lastyear)
        with SessionScope(db):
            map(db.session.add, (o_rp1, o_rp2, o_rp3))
            db.session.commit()
        org, rp1, rp2, rp3 = map(db.session.merge, (org, rp1, rp2, rp3))
        data = org.as_fhir()
        assert org.name == data['name']
        rps = [
            extension for extension in data['extension']
            if extension['url'] == ResearchProtocolExtension.extension_url]

        assert len(rps) == 1
        assert len(rps[0]['research_protocols']) == 3

        # confirm the order is descending in the custom accessor method
        results = [(rp, retired) for rp, retired in org.rps_w_retired()]
        assert [(rp1, None), (rp2, yesterday), (rp3, lastyear)] == results
Ejemplo n.º 2
0
    def test_organization_extension_update(self):
        # confirm clearing one of several extensions works
        self.promote_user(role_name=ROLE.ADMIN.value)
        self.login()

        en_AU = LocaleConstants().AustralianEnglish

        # Populate db with complete org, and set many fields
        org = Organization(name='test',
                           phone='800-800-5665',
                           timezone='US/Pacific')
        org.identifiers.append(
            Identifier(value='state:NY', system=PRACTICE_REGION))
        org.locales.append(en_AU)
        org.default_locale = 'en_AU'
        rp = ResearchProtocol(name='rp1')

        with SessionScope(db):
            db.session.add(rp)
            db.session.add(org)
            db.session.commit()
        org, rp = map(db.session.merge, (org, rp))
        org_id, rp_id = org.id, rp.id
        org.research_protocols.append(rp)
        data = org.as_fhir()
        input = {
            k: v
            for k, v in data.items() if k in ('name', 'resourceType')
        }

        # Replace locale extension with null value, copy
        # over others.
        input['extension'] = [
            e for e in data['extension']
            if e['url'] != LocaleExtension.extension_url
        ]
        input['extension'].append({'url': LocaleExtension.extension_url})

        response = self.client.put('/api/organization/{}'.format(org_id),
                                   content_type='application/json',
                                   data=json.dumps(input))
        assert response.status_code == 200

        # Pull the updated db entry
        org = Organization.query.get(org_id)
        en_AU = db.session.merge(en_AU)

        # Confirm all the unmentioned entries survived
        assert org.phone == '800-800-5665'
        assert org.default_locale == 'en_AU'
        assert org.locales.count() == 0
        assert org.timezone == 'US/Pacific'
        assert org.research_protocol(as_of_date=datetime.utcnow()).id == rp_id

        # Confirm empty extension isn't included in result
        results = response.json
        for e in results['extension']:
            assert 'url' in e
            assert len(e.keys()) > 1
Ejemplo n.º 3
0
 def test_as_fhir(self):
     org = Organization(name='Homer\'s Hospital')
     org.use_specific_codings = True
     org.race_codings = False
     data = org.as_fhir()
     assert org.name == data['name']
     assert data['use_specific_codings']
     assert not data['race_codings']
 def test_as_fhir(self):
     org = Organization(name='Homer\'s Hospital')
     org.use_specific_codings = True
     org.race_codings = False
     data = org.as_fhir()
     self.assertEquals(org.name, data['name'])
     self.assertTrue(data['use_specific_codings'])
     self.assertFalse(data['race_codings'])
 def test_as_fhir(self):
     org = Organization(name='Homer\'s Hospital')
     org.use_specific_codings = True
     org.race_codings = False
     data = org.as_fhir()
     assert org.name == data['name']
     assert data['use_specific_codings']
     assert not data['race_codings']
    def test_organization_extension_update(self):
        # confirm clearing one of several extensions works
        self.promote_user(role_name=ROLE.ADMIN.value)
        self.login()

        en_AU = LocaleConstants().AustralianEnglish

        # Populate db with complete org, and set many fields
        org = Organization(
            name='test', phone='800-800-5665', timezone='US/Pacific')
        org.identifiers.append(Identifier(
            value='state:NY', system=PRACTICE_REGION))
        org.locales.append(en_AU)
        org.default_locale = 'en_AU'
        rp = ResearchProtocol(name='rp1')

        with SessionScope(db):
            db.session.add(rp)
            db.session.add(org)
            db.session.commit()
        org, rp = map(db.session.merge, (org, rp))
        org_id, rp_id = org.id, rp.id
        org.research_protocols.append(rp)
        data = org.as_fhir()
        input = {k: v for k, v in data.items() if k in (
            'name', 'resourceType')}

        # Replace locale extension with null value, copy
        # over others.
        input['extension'] = [
            e for e in data['extension']
            if e['url'] != LocaleExtension.extension_url]
        input['extension'].append({'url': LocaleExtension.extension_url})

        response = self.client.put(
            '/api/organization/{}'.format(org_id),
            content_type='application/json',
            data=json.dumps(input))
        assert response.status_code == 200

        # Pull the updated db entry
        org = Organization.query.get(org_id)
        en_AU = db.session.merge(en_AU)

        # Confirm all the unmentioned entries survived
        assert org.phone == '800-800-5665'
        assert org.default_locale == 'en_AU'
        assert org.locales.count() == 0
        assert org.timezone == 'US/Pacific'
        assert org.research_protocol(as_of_date=datetime.utcnow()).id == rp_id

        # Confirm empty extension isn't included in result
        results = response.json
        for e in results['extension']:
            assert 'url' in e
            assert len(e.keys()) > 1
    def test_delete_extension(self):
        org = Organization(name='testy')
        org.timezone = 'Asia/Tokyo'  # stored in an extension
        with SessionScope(db):
            db.session.add(org)
            db.session.commit()
        org = db.session.merge(org)
        mp = ModelPersistence(
            Organization, lookup_field='id',
            sequence_name='organizations_id_seq',
            target_dir=self.tmpdir)
        mp.export()

        # Strip the empty extensions, as expected in the real persistence file
        with open(
                os.path.join(self.tmpdir, 'Organization.json'), 'r') as pfile:
            data = json.load(pfile)
            # Special handling of extensions - empties only have 'url' key

        for i, entry in enumerate(data['entry']):
            extensions = entry['extension']
            keepers = []
            for e in extensions:
                if len(e.keys()) > 1:
                    keepers.append(e)
            data['entry'][i]['extension'] = keepers
            empty_keys = [k for k, v in entry.items() if not v]
            for k in empty_keys:
                del data['entry'][i][k]

        with open(
                os.path.join(self.tmpdir, 'Organization.json'), 'w') as pfile:
            pfile.write(json.dumps(data))

        # Add an additional extension to the org, make sure
        # they are deleted when importing again from
        # persistence that doesn't include them

        org.locales.append(LocaleConstants().AmericanEnglish)
        with SessionScope(db):
            db.session.commit()
        org = db.session.merge(org)
        assert len(org.as_fhir()['extension']) > 1

        mp.import_(keep_unmentioned=False)
        org = Organization.query.filter(Organization.name == 'testy').one()
        assert org.locales.count() == 0
        assert org.timezone == 'Asia/Tokyo'
Ejemplo n.º 8
0
    def test_organization_put_update(self):
        # confirm unmentioned fields persist
        self.promote_user(role_name=ROLE.ADMIN.value)
        self.login()

        en_AU = LocaleConstants().AustralianEnglish

        # Populate db with complet org, and set many fields
        org = Organization(name='test',
                           phone='800-800-5665',
                           timezone='US/Pacific')
        org.identifiers.append(
            Identifier(value='state:NY', system=PRACTICE_REGION))
        org.locales.append(en_AU)
        org.default_locale = 'en_AU'

        with SessionScope(db):
            db.session.add(org)
            db.session.commit()
        org = db.session.merge(org)
        org_id = org.id
        data = org.as_fhir()

        # Now strip down the representation - confirm a post doesn't
        # wipe unmentioned fields
        del data['extension']
        del data['telecom']
        del data['language']

        response = self.client.put('/api/organization/{}'.format(org_id),
                                   content_type='application/json',
                                   data=json.dumps(data))
        assert response.status_code == 200

        # Pull the updated db entry
        org = Organization.query.get(org_id)
        en_AU = db.session.merge(en_AU)

        # Confirm all the unmentioned entries survived
        assert org.phone == '800-800-5665'
        assert org.default_locale == 'en_AU'
        assert org.locales[0] == en_AU
        assert org.timezone == 'US/Pacific'
    def test_organization_put_update(self):
        # confirm unmentioned fields persist
        self.promote_user(role_name=ROLE.ADMIN.value)
        self.login()

        en_AU = LocaleConstants().AustralianEnglish

        # Populate db with complet org, and set many fields
        org = Organization(
            name='test', phone='800-800-5665', timezone='US/Pacific')
        org.identifiers.append(Identifier(
            value='state:NY', system=PRACTICE_REGION))
        org.locales.append(en_AU)
        org.default_locale = 'en_AU'

        with SessionScope(db):
            db.session.add(org)
            db.session.commit()
        org = db.session.merge(org)
        org_id = org.id
        data = org.as_fhir()

        # Now strip down the representation - confirm a post doesn't
        # wipe unmentioned fields
        del data['extension']
        del data['telecom']
        del data['language']

        response = self.client.put(
            '/api/organization/{}'.format(org_id),
            content_type='application/json',
            data=json.dumps(data))
        assert response.status_code == 200

        # Pull the updated db entry
        org = Organization.query.get(org_id)
        en_AU = db.session.merge(en_AU)

        # Confirm all the unmentioned entries survived
        assert org.phone == '800-800-5665'
        assert org.default_locale == 'en_AU'
        assert org.locales[0] == en_AU
        assert org.timezone == 'US/Pacific'
Ejemplo n.º 10
0
    def test_multiple_rps_in_fhir(self):
        yesterday = datetime.utcnow() - timedelta(days=1)
        lastyear = datetime.utcnow() - timedelta(days=365)
        org = Organization(name='Testy')
        rp1 = ResearchProtocol(name='rp1')
        rp2 = ResearchProtocol(name='yesterday')
        rp3 = ResearchProtocol(name='last year')
        with SessionScope(db):
            map(db.session.add, (org, rp1, rp2, rp3))
            db.session.commit()
        org, rp1, rp2, rp3 = map(db.session.merge, (org, rp1, rp2, rp3))
        o_rp1 = OrganizationResearchProtocol(research_protocol=rp1,
                                             organization=org)
        o_rp2 = OrganizationResearchProtocol(research_protocol=rp2,
                                             organization=org,
                                             retired_as_of=yesterday)
        o_rp3 = OrganizationResearchProtocol(research_protocol=rp3,
                                             organization=org,
                                             retired_as_of=lastyear)
        with SessionScope(db):
            map(db.session.add, (o_rp1, o_rp2, o_rp3))
            db.session.commit()
        org, rp1, rp2, rp3 = map(db.session.merge, (org, rp1, rp2, rp3))
        data = org.as_fhir()
        assert org.name == data['name']
        rps = [
            extension for extension in data['extension']
            if extension['url'] == ResearchProtocolExtension.extension_url
        ]

        assert len(rps) == 1
        assert len(rps[0]['research_protocols']) == 3

        # confirm the order is descending in the custom accessor method
        results = [(rp, retired) for rp, retired in org.rps_w_retired()]
        assert [(rp1, None), (rp2, yesterday), (rp3, lastyear)] == results