"""Unit test module for Reference class"""
from tests import TestCase, TEST_USER_ID

from portal.models.intervention import Intervention
from portal.models.reference import Reference
from portal.models.questionnaire import Questionnaire
from portal.models.questionnaire_bank import QuestionnaireBank


class TestDemographics(TestCase):

    def test_patient(self):
        patient = Reference.patient(TEST_USER_ID)
        self.assertEquals(
            patient.as_fhir()['display'], self.test_user.display_name)

    def test_organization(self):
        org = Reference.organization(0)
        self.assertEquals(
            org.as_fhir()['display'], 'none of the above')

    def test_questionnaire(self):
        q = Questionnaire(name='testy')
        q_ref = Reference.questionnaire(q.name)
        self.assertEquals(
            q_ref.as_fhir()['display'], 'testy')

    def test_questionnaire_bank(self):
        q = QuestionnaireBank(name='testy')
        q_ref = Reference.questionnaire_bank(q.name)
        self.assertEquals(
            q_ref.as_fhir()['display'], 'testy')

    def test_intervention(self):
        i = Intervention.query.filter_by(name='self_management').one()
        i_ref = Reference.intervention(i.id)
        self.assertEquals(
            i_ref.as_fhir()['display'], 'self_management')
        self.assertEquals(
            i_ref.as_fhir()['reference'], 'api/intervention/self_management')

    def test_intervention_parse(self):
        ref = {'reference': 'api/intervention/self_management'}
        i = Reference.parse(ref)
        self.assertEquals(i.name, 'self_management')

@@ -36,16 +36,55 @@
 lass%22%22%22%0A
+from flask_webtest import SessionScope%0A
 from tes
@@ -133,47 +133,180 @@
 tal.
-models.intervention import Intervention
+extensions import db%0Afrom portal.models.identifier import Identifier%0Afrom portal.models.intervention import Intervention%0Afrom portal.models.practitioner import Practitioner
 %0Afro
@@ -469,32 +469,65 @@
 ank%0A
-%0A%0Aclass TestDemographics
+from portal.system_uri import US_NPI%0A%0Aclass TestReference
 (Tes
@@ -1794,28 +1794,895 @@
 (i.name, 'self_management')%0A
+%0A    def prep_practitioner(self):%0A        p = Practitioner(first_name='first', last_name='last')%0A        i = Identifier(system=US_NPI, value='12345')%0A        p.identifiers.append(i)%0A        with SessionScope(db):%0A            db.session.add(p)%0A            db.session.commit()%0A        p = db.session.merge(p)%0A        return p%0A%0A    def test_practioner(self):%0A        p = self.prep_practitioner()%0A        p_ref = Reference.practitioner(p.id)%0A        self.assertEquals(%0A            p_ref.as_fhir()%5B'display'%5D, 'first last')%0A        self.assertEquals(%0A            p_ref.as_fhir()%5B'reference'%5D,%0A            'api/practitioner/%7B%7D'.format(p.id))%0A%0A    def test_practitioner_parse(self):%0A        p = self.prep_practitioner()%0A        ref = %7B'reference': 'api/practitioner/12345?system=%7B%7D'.format(US_NPI)%7D%0A        parsed = Reference.parse(ref)%0A        self.assertEquals(p, parsed)%0A

Add missing tests for practitioner reference.