Exemple #1
0
    def test_generate_pid(self, req, dbsession, check_csrf_token):
        """
        It should generate a PID for new patients
        """
        import mock
        from occams import models

        site_la = models.Site(name=u'la', title=u'LA')
        reftype = models.ReferenceType(name=u'foo', title=u'FOO')
        dbsession.add_all([site_la, reftype])
        dbsession.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.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']]
Exemple #2
0
    def test_reference_unique(self, req, dbsession, check_csrf_token):
        """
        It should enforce unique reference_types
        """
        from pyramid.httpexceptions import HTTPBadRequest
        from occams 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')
        dbsession.add_all([patient, other])
        dbsession.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']
Exemple #3
0
    def test_reference_valid_number(self, req, dbsession, check_csrf_token):
        """
        It should check reference patterns if they are supported by the type
        """
        from pyramid.httpexceptions import HTTPBadRequest
        from occams 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')
        dbsession.add_all([patient, reftype])
        dbsession.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']
Exemple #4
0
    def test_data_with_refs(self, dbsession):
        """
        It should generate a basic listing of all the PIDs in the database
        """
        from occams import models

        plan = self._create_one(dbsession)

        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')
        )

        dbsession.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'
Exemple #5
0
    def test_references(self, req, dbsession, check_csrf_token):
        """
        It should update references
        """
        from occams 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')
        ]
        dbsession.add_all([site_la, patient])
        dbsession.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])
Exemple #6
0
    def test_no_match(self, req, dbsession):

        from occams import models

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

        dbsession.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'
Exemple #7
0
    def test_match(self, req, dbsession):

        from occams import models

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

        dbsession.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'
Exemple #8
0
    def test_by_reference_number(self, req, dbsession):
        """
        It should be able to search by external ID
        """
        from occams 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')
            ])
        dbsession.add_all([site_la, patient])
        dbsession.flush()

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