Esempio n. 1
0
    def test_generate_pid(self, req, db_session, check_csrf_token):
        """
        It should generate a PID for new patients
        """
        import mock
        from occams_studies import models

        site_la = models.Site(name=u'la', title=u'LA')
        reftype = models.ReferenceType(name=u'foo', title=u'FOO')
        db_session.add_all([site_la, reftype])
        db_session.flush()

        req.json_body = {
            'site':
            site_la.id,
            'references': [{
                'reference_type': reftype.id,
                'reference_number': u'ABC'
            }]
        }

        # Fake generate a PID, the roster should unit test this
        with mock.patch('occams_studies.views.patient.generate') as generate:
            generate.return_value = u'12345'
            res = self._call_fut(models.PatientFactory(req), req)

        assert generate.called
        assert check_csrf_token.called
        assert u'12345' == res['pid']
        assert site_la.id == res['site']['id']
        assert [(reftype.id, u'ABC')] == \
            [(r['reference_type']['id'], r['reference_number'])
             for r in res['references']]
Esempio n. 2
0
    def test_reference_unique(self, req, db_session, check_csrf_token):
        """
        It should enforce unique reference_types
        """
        from pyramid.httpexceptions import HTTPBadRequest
        from occams_studies import models

        site_la = models.Site(name=u'la', title=u'LA')
        reftype = models.ReferenceType(name=u'foo', title=u'Foo')
        other = models.Patient(site=site_la,
                               pid=u'ABCDE',
                               references=[
                                   models.PatientReference(
                                       reference_type=reftype,
                                       reference_number=u'XYZ')
                               ])
        patient = models.Patient(site=site_la, pid=u'12345')
        db_session.add_all([patient, other])
        db_session.flush()

        req.json_body = {
            'site':
            site_la.id,
            'references': [{
                'reference_type': reftype.id,
                'reference_number': u'XYZ'
            }]
        }

        with pytest.raises(HTTPBadRequest) as excinfo:
            self._call_fut(patient, req)

        assert check_csrf_token.called
        assert 'Already assigned' in \
            excinfo.value.json['errors']['references-0-reference_number']
Esempio n. 3
0
    def test_reference_valid_number(self, req, db_session, check_csrf_token):
        """
        It should check reference patterns if they are supported by the type
        """
        from pyramid.httpexceptions import HTTPBadRequest
        from occams_studies import models

        site_la = models.Site(name=u'la', title=u'LA')
        reftype = models.ReferenceType(name=u'foo',
                                       title=u'Foo',
                                       reference_pattern=u'^[0-9]+$')
        patient = models.Patient(site=site_la, pid=u'12345')
        db_session.add_all([patient, reftype])
        db_session.flush()

        req.json_body = {
            'site':
            site_la.id,
            'references': [{
                'reference_type': reftype.id,
                'reference_number': u'XYZ'
            }]
        }
        with pytest.raises(HTTPBadRequest) as excinfo:
            self._call_fut(patient, req)
        assert check_csrf_token.called
        assert 'Invalid format' in \
            excinfo.value.json['errors']['references-0-reference_number']
Esempio n. 4
0
    def test_data_with_refs(self, db_session):
        """
        It should generate a basic listing of all the PIDs in the database
        """
        from occams_studies import models

        plan = self._create_one(db_session)

        reference_type = models.ReferenceType(name=u'med_num',
                                              title=u'Medical Number')

        patient = models.Patient(pid=u'xxx-xxx',
                                 references=[
                                     models.PatientReference(
                                         reference_type=reference_type,
                                         reference_number=u'999')
                                 ],
                                 site=models.Site(name=u'someplace',
                                                  title=u'Some Place'))

        db_session.add(patient)

        codebook = list(plan.codebook())
        query = plan.data()

        codebook_columns = [c['field'] for c in codebook]
        data_columns = [c['name'] for c in query.column_descriptions]
        assert sorted(codebook_columns) == sorted(data_columns)

        data = query.one()._asdict()
        assert data['med_num'] == '999'
Esempio n. 5
0
    def test_references(self, req, db_session, check_csrf_token):
        """
        It should update references
        """
        from occams_studies import models

        reftype1 = models.ReferenceType(name=u'foo', title=u'Foo')
        reftype2 = models.ReferenceType(name=u'bar', title=u'Bar')
        site_la = models.Site(name=u'la', title=u'LA')
        patient = models.Patient(site=site_la, pid=u'12345')
        patient.references = [
            models.PatientReference(reference_type=reftype1,
                                    reference_number=u'XYZ'),
            models.PatientReference(reference_type=reftype2,
                                    reference_number=u'ABC')
        ]
        db_session.add_all([site_la, patient])
        db_session.flush()

        req.json_body = {
            'site':
            patient.site.id,
            'references': [{
                'reference_type': reftype1.id,
                'reference_number': u'XYZ'
            }, {
                'reference_type': reftype1.id,
                'reference_number': u'RST'
            }]
        }

        self._call_fut(patient, req)

        assert check_csrf_token.called
        assert sorted([(reftype1.id, u'XYZ'), (reftype1.id, u'RST')]) == \
            sorted([(r.reference_type.id, r.reference_number)
                    for r in patient.references])
Esempio n. 6
0
    def test_no_match(self, req, db_session):

        from occams_studies import models

        reftype = models.ReferenceType(name=u'medical_number',
                                       title=u'Medical Number')

        db_session.add(reftype)

        req.GET = {'term': 'other'}
        factory = models.ReferenceTypeFactory(req)

        res = self._call_fut(factory, req)

        assert len(res['reference_types']) == 0, \
            'Reference types were found when none were expected'
Esempio n. 7
0
    def test_match(self, req, db_session):

        from occams_studies import models

        reftype = models.ReferenceType(name=u'medical_number',
                                       title=u'Medical Number')

        db_session.add(reftype)

        req.GET = {'term': 'med'}
        factory = models.ReferenceTypeFactory(req)

        res = self._call_fut(factory, req)

        assert len(res['reference_types']) == 1, \
            'Incorrect number of results received'
Esempio n. 8
0
    def test_by_reference_number(self, req, db_session):
        """
        It should be able to search by external ID
        """
        from occams_studies import models
        from webob.multidict import MultiDict

        site_la = models.Site(name=u'la', title=u'LA')
        patient = models.Patient(
            site=site_la,
            pid=u'12345',
            references=[
                models.PatientReference(reference_type=models.ReferenceType(
                    name=u'ext', title=u'External ID'),
                                        reference_number=u'05-01-0000-5')
            ])
        db_session.add_all([site_la, patient])
        db_session.flush()

        req.GET = MultiDict([('query', u'05-01')])
        res = self._call_fut(models.PatientFactory(req), req)
        assert patient.pid == res['patients'][0]['pid']