#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  Generated from FHIR 0.5.0.5149 on 2015-07-06.
#  2015, SMART Health IT.


import os
import io
import unittest
import json
from . import dataelement
from .fhirdate import FHIRDate


class DataElementTests(unittest.TestCase):
    def instantiate_from(self, filename):
        datadir = os.environ.get('FHIR_UNITTEST_DATADIR') or ''
        with io.open(os.path.join(datadir, filename), 'r', encoding='utf-8') as handle:
            js = json.load(handle)
            self.assertEqual("DataElement", js["resourceType"])
        return dataelement.DataElement(js)
    
    def testDataElement1(self):
        inst = self.instantiate_from("dataelement-example.json")
        self.assertIsNotNone(inst, "Must have instantiated a DataElement instance")
        self.implDataElement1(inst)
        
        js = inst.as_json()
        self.assertEqual("DataElement", js["resourceType"])
        inst2 = dataelement.DataElement(js)
        self.implDataElement1(inst2)
    
    def implDataElement1(self, inst):
        self.assertEqual(inst.element[0].binding.name, "Gender")
        self.assertEqual(inst.element[0].binding.strength, "required")
        self.assertEqual(inst.element[0].binding.valueSetReference.extension[0].url, "http://hl7.org/fhir/StructureDefinition/11179-permitted-value-valueset")
        self.assertEqual(inst.element[0].binding.valueSetReference.extension[1].url, "http://hl7.org/fhir/StructureDefinition/11179-permitted-value-conceptmap")
        self.assertEqual(inst.element[0].definition, "The code representing the gender of a person.")
        self.assertEqual(inst.element[0].extension[0].url, "http://hl7.org/fhir/StructureDefinition/minLength")
        self.assertEqual(inst.element[0].extension[0].valueInteger, 1)
        self.assertEqual(inst.element[0].extension[1].url, "http://hl7.org/fhir/StructureDefinition/elementdefinition-question")
        self.assertEqual(inst.element[0].extension[1].valueString, "Gender")
        self.assertEqual(inst.element[0].maxLength, 13)
        self.assertEqual(inst.element[0].path, "Gender")
        self.assertEqual(inst.element[0].type[0].code, "CodeableConcept")
        self.assertEqual(inst.id, "gender")
        self.assertEqual(inst.identifier.value, "2179650")
        self.assertEqual(inst.name, "Gender Code")
        self.assertEqual(inst.publisher, "DCP")
        self.assertEqual(inst.status, "active")
        self.assertEqual(inst.text.status, "generated")
        self.assertEqual(inst.useContext[0].coding[0].display, "FBPP Pooled Database")
        self.assertEqual(inst.useContext[0].coding[0].system, "http://example.org/FBPP")
        self.assertEqual(inst.useContext[0].coding[1].display, "Demographics")
        self.assertEqual(inst.useContext[0].coding[1].system, "http://example.org/PhenX")
        self.assertEqual(inst.useContext[0].coding[2].display, "Pt. Administrative")
        self.assertEqual(inst.useContext[0].coding[2].system, "http://example.org/EligibilityCriteria")
        self.assertEqual(inst.useContext[0].coding[3].display, "UAMS New CDEs")
        self.assertEqual(inst.useContext[0].coding[3].system, "http://example.org/UAMSClinicalResearch")
        self.assertEqual(inst.useContext[0].coding[4].display, "Substance Abuse and ")
        self.assertEqual(inst.useContext[0].coding[4].system, "http://example.org/PhenX")
        self.assertEqual(inst.useContext[0].coding[5].display, "CSAERS Adverse Event")
        self.assertEqual(inst.useContext[0].coding[5].system, "http://example.org/Category")
        self.assertEqual(inst.useContext[0].coding[6].display, "Core: Tier 1")
        self.assertEqual(inst.useContext[0].coding[6].system, "http://example.org/PhenX")
        self.assertEqual(inst.useContext[0].coding[7].display, "Case Report Forms")
        self.assertEqual(inst.useContext[0].coding[7].system, "http://example.org/Category")
        self.assertEqual(inst.useContext[0].coding[8].display, "CSAERS Review Set")
        self.assertEqual(inst.useContext[0].coding[8].system, "http://example.org/Category")
        self.assertEqual(inst.useContext[0].coding[9].display, "CIAF")
        self.assertEqual(inst.useContext[0].coding[9].system, "http://example.org/Demonstration%20Applications")
        self.assertEqual(inst.version, "1.0")
    
    def testDataElement2(self):
        inst = self.instantiate_from("dataelement-labtestmaster-example.json")
        self.assertIsNotNone(inst, "Must have instantiated a DataElement instance")
        self.implDataElement2(inst)
        
        js = inst.as_json()
        self.assertEqual("DataElement", js["resourceType"])
        inst2 = dataelement.DataElement(js)
        self.implDataElement2(inst2)
    
    def implDataElement2(self, inst):
        self.assertEqual(inst.element[0].alias[0], "Protime, PT")
        self.assertEqual(inst.element[0].comments, "Used to screen the integrity of the extrinsic and common pathways of coagulation and to monitor warfarin anticoagulation. ")
        self.assertEqual(inst.element[0].definition, "The PT test evaluates the extrinsic and common pathways of the coagulation cascade.")
        self.assertEqual(inst.element[0].exampleDecimal, 10.0)
        self.assertEqual(inst.element[0].extension[0].url, "http://hl7.org/fhir/StructureDefinition/elementdefinition-allowedUnits")
        self.assertEqual(inst.element[0].extension[0].valueCodeableConcept.coding[0].code, "s")
        self.assertEqual(inst.element[0].extension[0].valueCodeableConcept.coding[0].display, "second")
        self.assertTrue(inst.element[0].extension[0].valueCodeableConcept.coding[0].primary)
        self.assertEqual(inst.element[0].extension[0].valueCodeableConcept.coding[0].system, "http://unitsofmeasure.org")
        self.assertEqual(inst.element[0].extension[0].valueCodeableConcept.coding[0].version, "1.9")
        self.assertEqual(inst.element[0].extension[0].valueCodeableConcept.text, "second")
        self.assertEqual(inst.element[0].mapping[0].identity, "loinc")
        self.assertEqual(inst.element[0].mapping[0].map, "5964-2")
        self.assertEqual(inst.element[0].path, "prothrombin")
        self.assertEqual(inst.element[0].requirements, "This test is orderable. A plasma specimen in a 3.2% sodium citrate blue top tube is required.")
        self.assertEqual(inst.element[0].type[0].code, "Decimal")
        self.assertEqual(inst.id, "prothrombin")
        self.assertEqual(inst.identifier.period.start.date, FHIRDate("2011-05-19").date)
        self.assertEqual(inst.identifier.period.start.as_json(), "2011-05-19")
        self.assertEqual(inst.identifier.system, "http://www.CenturyHospital/Laboratory/DirectoryofServices")
        self.assertEqual(inst.identifier.type.text, "Prothrombin Time, PT")
        self.assertEqual(inst.identifier.value, "11")
        self.assertEqual(inst.mapping[0].comments, "Version 2.48 or later")
        self.assertEqual(inst.mapping[0].identity, "loinc")
        self.assertEqual(inst.mapping[0].name, "LOINC")
        self.assertEqual(inst.mapping[0].uri, "http://loinc.org/")
        self.assertEqual(inst.name, "Prothrombin Time")
        self.assertEqual(inst.status, "active")
        self.assertEqual(inst.text.status, "generated")
    
    def testDataElement3(self):
        inst = self.instantiate_from("dataelement-sdc-profile-example-de.json")
        self.assertIsNotNone(inst, "Must have instantiated a DataElement instance")
        self.implDataElement3(inst)
        
        js = inst.as_json()
        self.assertEqual("DataElement", js["resourceType"])
        inst2 = dataelement.DataElement(js)
        self.implDataElement3(inst2)
    
    def implDataElement3(self, inst):
        self.assertEqual(inst.contact[0].extension[0].url, "http://hl7.org/fhir/StructureDefinition/11179-de-contact-address")
        self.assertEqual(inst.contact[0].extension[0].valueAddress.text, "540 Gaither Rd Rockville MD 20850")
        self.assertEqual(inst.contact[0].name, "Bill Munier")
        self.assertEqual(inst.contact[0].telecom[0].system, "email")
        self.assertEqual(inst.contact[0].telecom[0].value, "william.munier@ahrq.hhs.gov")
        self.assertEqual(inst.date.date, FHIRDate("2010-03-31").date)
        self.assertEqual(inst.date.as_json(), "2010-03-31")
        self.assertEqual(inst.element[0].binding.name, "Gender")
        self.assertEqual(inst.element[0].binding.strength, "required")
        self.assertEqual(inst.element[0].binding.valueSetReference.extension[0].url, "http://hl7.org/fhir/StructureDefinition/11179-permitted-value-valueset")
        self.assertEqual(inst.element[0].binding.valueSetReference.extension[1].url, "http://hl7.org/fhir/StructureDefinition/11179-permitted-value-valueset")
        self.assertEqual(inst.element[0].code[0].code, "74698-2")
        self.assertEqual(inst.element[0].code[0].extension[0].url, "http://hl7.org/fhir/StructureDefinition/11179-de-is-data-element-concept")
        self.assertTrue(inst.element[0].code[0].extension[0].valueBoolean)
        self.assertEqual(inst.element[0].code[0].system, "http://loinc.org")
        self.assertEqual(inst.element[0].definition, "The patient's documented gender on arrival at the hospital.")
        self.assertEqual(inst.element[0].extension[0].url, "http://hl7.org/fhir/StructureDefinition/elementdefinition-question")
        self.assertEqual(inst.element[0].extension[0].valueString, "Patient's Gender")
        self.assertEqual(inst.element[0].extension[1].url, "http://hl7.org/fhir/StructureDefinition/elementdefinition-question")
        self.assertEqual(inst.element[0].extension[1].valueString, "Patient-Gender,CD")
        self.assertEqual(inst.element[0].mapping[0].extension[0].url, "http://hl7.org/fhir/StructureDefinition/11179-objectClass")
        self.assertEqual(inst.element[0].mapping[0].extension[0].valueCoding.code, "116154003")
        self.assertEqual(inst.element[0].mapping[0].extension[0].valueCoding.display, "Patient")
        self.assertEqual(inst.element[0].mapping[0].extension[0].valueCoding.system, "http://snomed.info/sct")
        self.assertEqual(inst.element[0].mapping[0].extension[1].url, "http://hl7.org/fhir/StructureDefinition/11179-objectClassProperty")
        self.assertEqual(inst.element[0].mapping[0].extension[1].valueCoding.code, "263495000")
        self.assertEqual(inst.element[0].mapping[0].extension[1].valueCoding.display, "Gender")
        self.assertEqual(inst.element[0].mapping[0].extension[1].valueCoding.system, "http://snomed.info/sct")
        self.assertEqual(inst.element[0].mapping[0].identity, "objclass")
        self.assertEqual(inst.element[0].mapping[0].map, "http://snomed.info/sct#116154003 | http://snomed.info/sct | 263495000")
        self.assertEqual(inst.element[0].path, "gender")
        self.assertEqual(inst.element[0].type[0].code, "CodeableConcept")
        self.assertEqual(inst.extension[0].url, "http://hl7.org/fhir/StructureDefinition/11179-de-document-reference")
        self.assertEqual(inst.extension[1].url, "http://hl7.org/fhir/StructureDefinition/11179-de-effective-period")
        self.assertEqual(inst.extension[1].valuePeriod.start.date, FHIRDate("2010-03-31").date)
        self.assertEqual(inst.extension[1].valuePeriod.start.as_json(), "2010-03-31")
        self.assertEqual(inst.extension[2].url, "http://hl7.org/fhir/StructureDefinition/11179-de-submitter-org")
        self.assertEqual(inst.id, "sdc-gender-de")
        self.assertEqual(inst.identifier.system, "http://nlm.nih.gov/some_other_text/data_element_identifier")
        self.assertEqual(inst.identifier.value, "DE42AHRQ")
        self.assertEqual(inst.mapping[0].identity, "objclass")
        self.assertEqual(inst.mapping[0].name, "Structured Data Capture ObjectClass/Property")
        self.assertEqual(inst.mapping[0].uri, "http://healthit.gov/sdc/objectclassproperty")
        self.assertEqual(inst.name, "Patient Gender")
        self.assertEqual(inst.publisher, "Agency for Healthcare Research and Quality (AHRQ) Center for Patient Safety and Quality")
        self.assertEqual(inst.status, "draft")
        self.assertEqual(inst.text.div, "<div>TODO</div>")
        self.assertEqual(inst.text.status, "generated")
        self.assertEqual(inst.useContext[0].coding[0].code, "Healthcare Event Reporting Form")
        self.assertEqual(inst.useContext[0].coding[0].display, "Patient safety event report - hospital: healthcare event reporting form")
        self.assertEqual(inst.version, "1.2")
    
    def testDataElement4(self):
        inst = self.instantiate_from("dataelement-sdc-profile-example.json")
        self.assertIsNotNone(inst, "Must have instantiated a DataElement instance")
        self.implDataElement4(inst)
        
        js = inst.as_json()
        self.assertEqual("DataElement", js["resourceType"])
        inst2 = dataelement.DataElement(js)
        self.implDataElement4(inst2)
    
    def implDataElement4(self, inst):
        self.assertEqual(inst.contact[0].telecom[0].system, "url")
        self.assertEqual(inst.contact[0].telecom[0].value, "http://hl7.org")
        self.assertEqual(inst.date.date, FHIRDate("2015-02-04").date)
        self.assertEqual(inst.date.as_json(), "2015-02-04")
        self.assertEqual(inst.element[0].binding.name, "AdministrativeGender")
        self.assertEqual(inst.element[0].binding.strength, "required")
        self.assertEqual(inst.element[0].definition, "The gender (i.e., the behavioral, cultural, or psychological traits typically associated with one sex) of a living subject as defined for administrative purposes.")
        self.assertEqual(inst.element[0].mapping[0].identity, "ccda")
        self.assertEqual(inst.element[0].mapping[0].language, "xpath")
        self.assertEqual(inst.element[0].mapping[0].map, "/ClinicalDocument/recordTarget/patientRole/patient/administrativeGender")
        self.assertEqual(inst.element[0].path, "administrativeGender")
        self.assertEqual(inst.element[0].type[0].code, "CodeableConcept")
        self.assertEqual(inst.id, "sdc-gender")
        self.assertEqual(inst.identifier.system, "http://nlm.nih.gov/some_other_text/data_element_identifier")
        self.assertEqual(inst.identifier.value, "DE42AHRQ")
        self.assertEqual(inst.mapping[0].identity, "ccda")
        self.assertEqual(inst.mapping[0].name, "Consolidated CDA R2")
        self.assertEqual(inst.mapping[0].uri, "http://hl7.org/CCDA-not-a-real-url")
        self.assertEqual(inst.name, "Patient Gender")
        self.assertEqual(inst.publisher, "Health Level Seven, International")
        self.assertEqual(inst.status, "draft")
        self.assertEqual(inst.text.div, "<div>TODO</div>")
        self.assertEqual(inst.text.status, "generated")

