def _sdmj_to_facts(self, sdmj_string): """ Transform Simple Data Model JSON to Indivo Facts. Takes a string of valid SDMJ and returns a list of ``indivo.model.Fact`` subclasses. """ parser = SDMJData(sdmj_string) return [instance for instance in parser.get_output()]
def cause_exception(doc): parser = SDMJData(doc) output = [obj for obj in parser.get_output()]
class SDMJDataUnitTests(TransactionInternalTests): def setUp(self): super(SDMJDataUnitTests, self).setUp() self.instance = SDMJData(TEST_SDMJ_DOCS[0]) self.required_classes = [] # Load test Classes self.required_classes = self.load_models_from_sdml(TEST_SDML_DOCS[0]) def tearDown(self): self.instance = None # Unregister the classes, reset the DB self.unload_models(self.required_classes) self.required_classes = [] super(SDMJDataUnitTests, self).tearDown() def test_get_output(self): output_objects = [obj for obj in self.instance.get_output()] self.assertEqual(len(output_objects), 4) # Three models in the definition med_obj = scrip_obj = None fill_objs = [] for obj in output_objects: klass_name = obj.__class__.__name__ if klass_name == 'TestMedication2': med_obj = obj elif klass_name == 'TestPrescription2': scrip_obj = obj elif klass_name == 'TestFill2': fill_objs.append(obj) else: self.fail('SDMJ Document parsing produced an instance of an invalid class %s'%klass_name) if not med_obj: self.fail('SDMJ Document parsing did not produce an instance of TestMedication2') if not scrip_obj: self.fail('SDMJ Document parsing did not produce an instance of TestPrescription2') if not fill_objs or len(fill_objs) != 2: self.fail('SDMJ Document parsing did not produce two instances of TestFill2') # Make sure the testmedication2 object parsed as expected med_expected_fields = { 'name': 'ibuprofen', 'date_started': iso8601.parse_utc_date('2010-10-01T00:00:00Z'), 'date_stopped': iso8601.parse_utc_date('2010-10-31T00:00:00Z'), 'brand_name': 'Advil', 'route': 'Oral', } self.check_object_fields(med_obj, med_expected_fields) # The 'prescription' field should be a OneToOne field, pointing at the prescription object self.assertEqual(med_obj.prescription, scrip_obj) # The 'fills' field should be a manager for fills objects # We can't test whether they match up because we aren't saving them to the database # So currently 'med_obj.fills' will raise a DoesNotExist exception # Make sure the testprescription2 class parsed as expected scrip_expected_fields = { 'prescribed_by_name': 'Kenneth D. Mandl', 'prescribed_by_institution': 'Children\'s Hospital Boston', 'prescribed_on': iso8601.parse_utc_date('2010-09-30T00:00:00Z'), 'prescribed_stop_on': iso8601.parse_utc_date('2010-10-31T00:00:00Z'), } self.check_object_fields(scrip_obj, scrip_expected_fields) # The TestPrescription2 object should have a 'testmedication2' field pointing to the Medication class # (the reverse link of the OneToOne from the TestMedication2) # We can't test this because we aren't saving object to the database. # If we were, we should test this with: self.assertEqual(scrip_obj.testmedication2, med_obj) # Make sure the testfill2 class parsed as expected fill_expected_fields = { 'supply_days': 15, 'filled_at_name': 'CVS', } fill_dates = set([iso8601.parse_utc_date('2010-10-01T00:00:00Z'), iso8601.parse_utc_date('2010-10-16T00:00:00Z')]) for fill_obj in fill_objs: self.check_object_fields(fill_obj, fill_expected_fields) self.assertEqual(fill_obj.testmedication2, med_obj) self.assertEqual(set([o.date_filled for o in fill_objs]), fill_dates) def test_invalid_schemas(self): def cause_exception(doc): parser = SDMJData(doc) output = [obj for obj in parser.get_output()] for doc in INVALID_TEST_SDMJ_DOCS: self.assertRaises(SDMException, cause_exception, doc) def check_object_fields(self, obj, expected_fields): for field_name, expected_val in expected_fields.iteritems(): actual_val = getattr(obj, field_name, None) self.assertEqual(actual_val, expected_val)
class SDMJDataUnitTests(TransactionInternalTests): def setUp(self): super(SDMJDataUnitTests, self).setUp() self.instance = SDMJData(TEST_SDMJ_DOCS[0]) self.required_classes = [] # Load test Classes self.required_classes = self.load_models_from_sdml(TEST_SDML_DOCS[0]) def tearDown(self): self.instance = None # Unregister the classes, reset the DB self.unload_models(self.required_classes) self.required_classes = [] super(SDMJDataUnitTests, self).tearDown() def test_get_output(self): output_objects = [obj for obj in self.instance.get_output()] self.assertEqual(len(output_objects), 4) # Three models in the definition med_obj = scrip_obj = None fill_objs = [] for obj in output_objects: klass_name = obj.__class__.__name__ if klass_name == 'TestMedication2': med_obj = obj elif klass_name == 'TestPrescription2': scrip_obj = obj elif klass_name == 'TestFill2': fill_objs.append(obj) else: self.fail( 'SDMJ Document parsing produced an instance of an invalid class %s' % klass_name) if not med_obj: self.fail( 'SDMJ Document parsing did not produce an instance of TestMedication2' ) if not scrip_obj: self.fail( 'SDMJ Document parsing did not produce an instance of TestPrescription2' ) if not fill_objs or len(fill_objs) != 2: self.fail( 'SDMJ Document parsing did not produce two instances of TestFill2' ) # Make sure the testmedication2 object parsed as expected med_expected_fields = { 'name': 'ibuprofen', 'date_started': iso8601.parse_utc_date('2010-10-01T00:00:00Z'), 'date_stopped': iso8601.parse_utc_date('2010-10-31T00:00:00Z'), 'brand_name': 'Advil', 'route': 'Oral', } self.check_object_fields(med_obj, med_expected_fields) # The 'prescription' field should be a OneToOne field, pointing at the prescription object self.assertEqual(med_obj.prescription, scrip_obj) # The 'fills' field should be a manager for fills objects # We can't test whether they match up because we aren't saving them to the database # So currently 'med_obj.fills' will raise a DoesNotExist exception # Make sure the testprescription2 class parsed as expected scrip_expected_fields = { 'prescribed_by_name': 'Kenneth D. Mandl', 'prescribed_by_institution': 'Children\'s Hospital Boston', 'prescribed_on': iso8601.parse_utc_date('2010-09-30T00:00:00Z'), 'prescribed_stop_on': iso8601.parse_utc_date('2010-10-31T00:00:00Z'), } self.check_object_fields(scrip_obj, scrip_expected_fields) # The TestPrescription2 object should have a 'testmedication2' field pointing to the Medication class # (the reverse link of the OneToOne from the TestMedication2) # We can't test this because we aren't saving object to the database. # If we were, we should test this with: self.assertEqual(scrip_obj.testmedication2, med_obj) # Make sure the testfill2 class parsed as expected fill_expected_fields = { 'supply_days': 15, 'filled_at_name': 'CVS', } fill_dates = set([ iso8601.parse_utc_date('2010-10-01T00:00:00Z'), iso8601.parse_utc_date('2010-10-16T00:00:00Z') ]) for fill_obj in fill_objs: self.check_object_fields(fill_obj, fill_expected_fields) self.assertEqual(fill_obj.testmedication2, med_obj) self.assertEqual(set([o.date_filled for o in fill_objs]), fill_dates) def test_invalid_schemas(self): def cause_exception(doc): parser = SDMJData(doc) output = [obj for obj in parser.get_output()] for doc in INVALID_TEST_SDMJ_DOCS: self.assertRaises(SDMException, cause_exception, doc) def check_object_fields(self, obj, expected_fields): for field_name, expected_val in expected_fields.iteritems(): actual_val = getattr(obj, field_name, None) self.assertEqual(actual_val, expected_val)