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
    def test_rp_alteration(self):
        # orgs with old rp should migrate to multiple w/ updated retired
        from portal.system_uri import SHORTCUT_ALIAS, TRUENTH_CR_NAME
        from portal.models.identifier import Identifier

        rp1 = ResearchProtocol(name='initial')
        rp2 = ResearchProtocol(name='replacement')
        org = Organization(name='testy')
        org.research_protocols.append(rp1)
        with SessionScope(db):
            db.session.add(rp2)
            db.session.add(org)
            db.session.commit()
        mp = ModelPersistence(Organization,
                              lookup_field='id',
                              sequence_name='organizations_id_seq',
                              target_dir=self.tmpdir)
        mp.export()

        # Add second rp, mark old as retired
        with open(os.path.join(self.tmpdir, 'Organization.json'),
                  'r') as pfile:
            data = json.load(pfile)

        now = datetime.utcnow().replace(microsecond=0)
        updated = {
            'url':
            ResearchProtocolExtension.extension_url,
            'research_protocols': [{
                "name": 'replacement'
            }, {
                "name": 'initial',
                "retired_as_of": FHIR_datetime.as_fhir(now)
            }]
        }
        for i, entry in enumerate(data['entry']):
            if entry['name'] != 'testy':
                continue
            extensions = entry['extension']
            keepers = [
                ext for ext in extensions
                if ext['url'] != ResearchProtocolExtension.extension_url
            ]
            keepers.append(updated)
            data['entry'][i]['extension'] = keepers

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

        mp.import_(keep_unmentioned=False)
        org = Organization.query.filter(Organization.name == 'testy').one()
        assert len(org.research_protocols) == 2

        # Make sure retired_as_of was set properly on old
        rp1, rp2 = map(db.session.merge, (rp1, rp2))
        expected = [(rp2, None), (rp1, now)]
        results = [(rp, retired) for rp, retired in org.rps_w_retired()]
        assert results == expected
    def test_rp_alteration(self):
        # orgs with old rp should migrate to multiple w/ updated retired
        from portal.system_uri import SHORTCUT_ALIAS, TRUENTH_CR_NAME
        from portal.models.identifier import Identifier

        rp1 = ResearchProtocol(name='initial')
        rp2 = ResearchProtocol(name='replacement')
        org = Organization(name='testy')
        org.research_protocols.append(rp1)
        with SessionScope(db):
            db.session.add(rp2)
            db.session.add(org)
            db.session.commit()
        mp = ModelPersistence(
            Organization, lookup_field='id',
            sequence_name='organizations_id_seq',
            target_dir=self.tmpdir)
        mp.export()

        # Add second rp, mark old as retired
        with open(
                os.path.join(self.tmpdir, 'Organization.json'), 'r') as pfile:
            data = json.load(pfile)

        now = datetime.utcnow().replace(microsecond=0)
        updated = {
            'url': ResearchProtocolExtension.extension_url,
            'research_protocols': [
                {"name": 'replacement'},
                {"name": 'initial', "retired_as_of": FHIR_datetime.as_fhir(
                    now)}]}
        for i, entry in enumerate(data['entry']):
            if entry['name'] != 'testy':
                continue
            extensions = entry['extension']
            keepers = [
                ext for ext in extensions
                if ext['url'] != ResearchProtocolExtension.extension_url]
            keepers.append(updated)
            data['entry'][i]['extension'] = keepers

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

        mp.import_(keep_unmentioned=False)
        org = Organization.query.filter(Organization.name == 'testy').one()
        assert len(org.research_protocols) == 2

        # Make sure retired_as_of was set properly on old
        rp1, rp2 = map(db.session.merge, (rp1, rp2))
        expected = [(rp2, None), (rp1, now)]
        results = [(rp, retired) for rp, retired in org.rps_w_retired()]
        assert results == expected
Пример #4
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