def test_what_we_want(self): clear(I2B2Core) I2B2Core.download_date = lambda: I2B2Core.update_date I2B2Core.import_date = lambda: I2B2Core.update_date I2B2Core.sourcesystem_cd = "Biggie" self.assertEqual( OrderedDict([('update_date', None), ('download_date', None), ('import_date', None), ('sourcesystem_cd', 'Biggie')]), I2B2Core()._freeze()) I2B2Core.update_date = datetime(2017, 9, 27) self.assertEqual( OrderedDict([('update_date', datetime(2017, 9, 27, 0, 0)), ('download_date', datetime(2017, 9, 27, 0, 0)), ('import_date', datetime(2017, 9, 27, 0, 0)), ('sourcesystem_cd', 'Biggie')]), I2B2Core()._freeze()) # Clear doesn't affect class functions clear(I2B2Core) self.assertEqual( OrderedDict([('update_date', None), ('download_date', None), ('import_date', None), ('sourcesystem_cd', "Unspecified")]), I2B2Core()._freeze()) I2B2Core.update_date = datetime(2003, 1, 4) self.assertEqual( OrderedDict([('update_date', datetime(2003, 1, 4)), ('download_date', None), ('import_date', None), ('sourcesystem_cd', "Unspecified")]), I2B2Core()._freeze())
def test_extension(self): class C1(DynProps): """ Ordered list of properties in the i2b2 core model""" sourcesystem_cd: Global[Optional[str]] x: Global[str] self.assertEqual("\t", row(C1())) with self.assertRaises(AttributeError): class Extension0(C1): sourcesystem_cd = "SS2" foo: Local[str] _ = Extension0 # Prevent lint check class Extension(C1): foo: Local[str] = "Apples" _ = Extension() # Prevent lint check C1.sourcesystem_cd = "SS3" self.assertEqual("SS3\t", row(C1())) clear(C1) self.assertEqual("\t", row(C1()))
def setUp(self): self.assertIsNotNone(self.test_prefix, "CRCTestCase.test_prefix must be set") self.assertIsNotNone( self.test_conf_file, "CRCTestCase.test_conf_file location must be set") clear(I2B2Core) clear(I2B2CoreWithUploadId)
def test_settings(self): clear(I2B2Core) I2B2Core.sourcesystem_cd = "abcd" I2B2Core.update_date = datetime(2014, 7, 31) I2B2Core.download_date = datetime.now rtn = I2B2Core() rtnf = as_dict(rtn) self.assertEqual(str(rtnf['update_date']), '2014-07-31 00:00:00') self.assertAlmostNow(rtn.download_date) self.assertEqual(rtn.update_date, rtn.import_date) self.assertEqual(rtnf['sourcesystem_cd'], 'abcd') self.assertEqual( 'update_date\tdownload_date\timport_date\tsourcesystem_cd', heading(rtn))
def test_str(self): clear(I2B2CoreUploadIdFirst) x = I2B2Core() y = I2B2CoreUploadIdFirst() I2B2Core.sourcesystem_cd = "SSCODE" I2B2Core.import_date = datetime(2017, 5, 31) I2B2Core.download_date = datetime(2017, 5, 30, 11, 33, 20) I2B2Core.update_date = datetime(2017, 5, 29, 8, 33, 20) self.assertEqual( "I2B2Core(update_date:'2017-05-29 08:33:20', download_date:'2017-05-30 " "11:33:20', import_date:'2017-05-31 00:00:00', sourcesystem_cd:'SSCODE')", str(x)) self.assertEqual( "I2B2CoreUploadIdFirst(upload_id:'None', update_date:'2017-05-29 08:33:20', " "download_date:'2017-05-30 11:33:20', import_date:'2017-05-31 00:00:00', " "sourcesystem_cd:'SSCODE')", str(y))
def test_freeze(self): x = I2B2Core() y = I2B2CoreUploadIdFirst() I2B2Core.sourcesystem_cd = "SSCODE" I2B2Core.import_date = datetime(2017, 5, 31) I2B2Core.download_date = datetime(2017, 5, 30, 11, 33, 20) I2B2Core.update_date = datetime(2017, 5, 29, 8, 33, 20) self.assertEqual( OrderedDict([('update_date', datetime(2017, 5, 29, 8, 33, 20)), ('download_date', datetime(2017, 5, 30, 11, 33, 20)), ('import_date', datetime(2017, 5, 31, 0, 0)), ('sourcesystem_cd', 'SSCODE')]), x._freeze()) self.assertEqual( OrderedDict([('upload_id', None), ('update_date', datetime(2017, 5, 29, 8, 33, 20)), ('download_date', datetime(2017, 5, 30, 11, 33, 20)), ('import_date', datetime(2017, 5, 31, 0, 0)), ('sourcesystem_cd', 'SSCODE')]), y._freeze()) I2B2CoreUploadIdFirst.upload_id = 12345 self.assertEqual( OrderedDict([('update_date', datetime(2017, 5, 29, 8, 33, 20)), ('download_date', datetime(2017, 5, 30, 11, 33, 20)), ('import_date', datetime(2017, 5, 31, 0, 0)), ('sourcesystem_cd', 'SSCODE')]), x._freeze()) self.assertEqual( OrderedDict([('upload_id', 12345), ('update_date', datetime(2017, 5, 29, 8, 33, 20)), ('download_date', datetime(2017, 5, 30, 11, 33, 20)), ('import_date', datetime(2017, 5, 31, 0, 0)), ('sourcesystem_cd', 'SSCODE')]), y._freeze()) clear(I2B2Core) self.assertEqual( OrderedDict([('update_date', None), ('download_date', None), ('import_date', None), ('sourcesystem_cd', '' "Unspecified" '')]), x._freeze()) self.assertEqual( OrderedDict([('upload_id', 12345), ('update_date', None), ('download_date', None), ('import_date', None), ('sourcesystem_cd', '' "Unspecified" '')]), y._freeze()) clear(I2B2CoreUploadIdFirst)
def test_defaults(self): rtn = I2B2Core() rtnf = as_dict(rtn) self.assertAlmostNow(rtn.update_date) self.assertDatesAlmostEqual(rtn.update_date, str(rtnf['update_date'])) self.assertAlmostNow(rtn.download_date) self.assertDatesAlmostEqual(rtn.download_date, str(rtnf['download_date'])) self.assertAlmostNow(rtn.import_date) self.assertDatesAlmostEqual(rtn.import_date, str(rtnf['import_date'])) self.assertEqual('Unspecified', rtn.sourcesystem_cd) self.assertEqual(rtn.sourcesystem_cd, rtnf['sourcesystem_cd']) self.assertEqual( ['update_date', 'download_date', 'import_date', 'sourcesystem_cd'], list(rtnf.keys())) rtn = I2B2Core() I2B2Core.download_date = datetime(2009, 1, 1, 12, 0) I2B2Core.sourcesystem_cd = "MASTER" I2B2Core.import_date = datetime(2011, 1, 1, 12, 0) I2B2Core.update_date = datetime.now() + timedelta(hours=2) self.assertEqual('2009-01-01 12:00:00', str(rtn.download_date)) self.assertEqual('2011-01-01 12:00:00', str(rtn.import_date)) self.assertEqual('MASTER', rtn.sourcesystem_cd) self.assertDatesAlmostEqual(rtn.update_date, str(datetime.now() + timedelta(hours=2))) clear(I2B2Core) I2B2CoreWithUploadId.upload_id = 1777439 rtn = I2B2CoreWithUploadId() if rtn.sourcesystem_cd != 'Unspecified': print("Caught it") self.assertEqual('Unspecified', rtn.sourcesystem_cd) self.assertEqual(1777439, rtn.upload_id) self.assertEqual( 'update_date\tdownload_date\timport_date\tsourcesystem_cd\tupload_id', heading(rtn)) rtn = I2B2Core() with self.assertRaises(AttributeError): _ = rtn.upload_id
def test_repr(self): clear(I2B2CoreUploadIdFirst) x = I2B2Core() y = I2B2CoreUploadIdFirst() I2B2Core.sourcesystem_cd = "SSCODE" I2B2Core.import_date = datetime(2017, 5, 31) I2B2Core.download_date = datetime(2017, 5, 30, 11, 33, 20) I2B2Core.update_date = datetime(2017, 5, 29, 8, 33, 20) self.assertEqual( '2017-05-29 08:33:20\t2017-05-30 11:33:20\t2017-05-31 00:00:00\tSSCODE', row(x)) self.assertEqual( '\t2017-05-29 08:33:20\t2017-05-30 11:33:20\t2017-05-31 00:00:00\tSSCODE', row(y)) I2B2CoreUploadIdFirst.upload_id = 12345 self.assertEqual( '2017-05-29 08:33:20\t2017-05-30 11:33:20\t2017-05-31 00:00:00\tSSCODE', row(x)) self.assertEqual( '12345\t2017-05-29 08:33:20\t2017-05-30 11:33:20\t2017-05-31 00:00:00\tSSCODE', row(y)) I2B2Core._separator = ',' self.assertEqual( '2017-05-29 08:33:20,2017-05-30 11:33:20,2017-05-31 00:00:00,SSCODE', row(x)) self.assertEqual( '12345,2017-05-29 08:33:20,2017-05-30 11:33:20,2017-05-31 00:00:00,SSCODE', row(y)) clear(I2B2Core) self.assertEqual(',,,Unspecified', row(x)) self.assertEqual('12345,,,,Unspecified', row(y)) clear(I2B2CoreUploadIdFirst) I2B2Core._separator = '\t'
def test_common_elements(self): """ Test the i2b2 core elements and demonstrate that they can't be overridden on an individual basis. These elements are: sourcesystem_cd = None update_date = None download_date = None import_date = None """ from i2b2model.data.i2b2patientdimension import PatientDimension PatientDimension._clear() clear(I2B2CoreWithUploadId) pd = PatientDimension(111, VitalStatusCd('U', 'D')) I2B2Core.sourcesystem_cd = "abc" self.assertEqual("abc", pd.sourcesystem_cd) I2B2Core.update_date = datetime(2014, 7, 31) self.assertEqual(datetime(2014, 7, 31), pd.update_date) self.assertEqual(datetime(2014, 7, 31), pd.download_date) self.assertEqual(datetime(2014, 7, 31), pd.import_date) I2B2Core.download_date = datetime(2014, 7, 30) I2B2Core.import_date = datetime(2014, 7, 29) I2B2CoreWithUploadId.upload_id = 17 self.assertEqual(datetime(2014, 7, 31), pd.update_date) self.assertEqual(datetime(2014, 7, 30), pd.download_date) self.assertEqual(datetime(2014, 7, 29), pd.import_date) self.assertEqual(17, pd.upload_id) PatientDimension._clear() self.assertEqual("abc", pd.sourcesystem_cd) clear(I2B2Core) self.assertEqual("Unspecified", pd.sourcesystem_cd)
def test_basics(self): from i2b2model.metadata.i2b2conceptdimension import ConceptDimension clear(ConceptDimension) I2B2Core.download_date = datetime(2017, 5, 25) I2B2Core.sourcesystem_cd = "TEST_SS" I2B2Core.import_date = datetime(2017, 5, 25) cd = ConceptDimension('TEST', 'root', 'Root test concept', ['L1', 'L2', 'root'], '\\TEST\\') self.assertAlmostNow(cd.update_date) I2B2Core.update_date = datetime(2001, 12, 1) expected = OrderedDict([('concept_path', '\\TEST\\L1\\L2\\root\\'), ('concept_cd', 'TEST:root'), ('name_char', 'TEST Root test concept'), ('concept_blob', ''), ('update_date', datetime(2001, 12, 1, 0, 0)), ('download_date', datetime(2017, 5, 25, 0, 0)), ('import_date', datetime(2017, 5, 25, 0, 0)), ('sourcesystem_cd', 'TEST_SS'), ('upload_id', None)]) self.assertEqual(expected, as_dict(cd)) # Note - balance is actually a modifier. This is strictly an example cd = ConceptDimension('TEST', 'root', 'Root balance test concept', ['L1', 'L2', 'balance'], '\\TEST\\') expected = OrderedDict([ ('concept_path', '\\TEST\\L1\\L2\\balance\\root\\'), ('concept_cd', 'TEST:root'), ('name_char', 'TEST Root balance test concept'), ('concept_blob', ''), ('update_date', datetime(2001, 12, 1, 0, 0)), ('download_date', datetime(2017, 5, 25, 0, 0)), ('import_date', datetime(2017, 5, 25, 0, 0)), ('sourcesystem_cd', 'TEST_SS'), ('upload_id', None) ]) self.assertEqual(expected, as_dict(cd))
def tearDown(self): clear(I2B2Core)
def setUp(self): clear(I2B2Core)
def tearDown(self): # if getattr(self, "_sourcesystem_cd", None): # remove_facts(f"--conf {self.test_conf_file} -ss {self._sourcesystem_cd}".split()) clear(I2B2Core) clear(I2B2CoreWithUploadId)
def tearDown(self): clear(I2B2Core) clear(I2B2CoreWithUploadId)
def setUp(self): clear(I2B2Core) clear(I2B2CoreWithUploadId)
def test_attributes(self): cwi_instance = I2B2CoreUploadIdFirst() core_instance = I2B2Core() # Cannot set property on the instance level with self.assertRaises(ValueError): core_instance.sourcesystem_cd = "test" # Cannot access a properties' internal form on instance level with self.assertRaises(ValueError): core_instance._sourcesystem_cd = "test" # Or the class level with self.assertRaises(ValueError): I2B2Core._sourcesystem_cd = "test" # Properties can be set on the class level I2B2Core.sourcesystem_cd = "test" # Properties are visible at the instance level self.assertEqual("test", core_instance.sourcesystem_cd) # Properties are visible at the class level self.assertEqual(core_instance.sourcesystem_cd, I2B2Core.sourcesystem_cd) # Properties are visible to subclasses self.assertEqual(core_instance.sourcesystem_cd, I2B2CoreUploadIdFirst.sourcesystem_cd) self.assertEqual(core_instance.sourcesystem_cd, cwi_instance.sourcesystem_cd) # Global Properties CANNOT be set at the inherited class level with self.assertRaises(ValueError): I2B2CoreUploadIdFirst.sourcesystem_cd = "test2" I2B2Core.sourcesystem_cd = "test3" # And settings are visible on the instance self.assertEqual("test3", core_instance.sourcesystem_cd) # ... inherited instance self.assertEqual(core_instance.sourcesystem_cd, cwi_instance.sourcesystem_cd) # ... class ... self.assertEqual(core_instance.sourcesystem_cd, I2B2CoreUploadIdFirst.sourcesystem_cd) # ... and superclass level self.assertEqual(core_instance.sourcesystem_cd, I2B2Core.sourcesystem_cd) # Subclass properties cannot be set at the instance level with self.assertRaises(ValueError): cwi_instance.upload_id = 117 # Only the class level I2B2CoreUploadIdFirst.upload_id = 118 # Subclass properties are not visible to superclass instances with self.assertRaises(AttributeError): _ = core_instance.upload_id # ... or the superclasses with self.assertRaises(AttributeError): _ = I2B2Core.upload_id # But are visible at the subclass instance level self.assertEqual(118, cwi_instance.upload_id) # And class level self.assertEqual(cwi_instance.upload_id, I2B2CoreUploadIdFirst.upload_id) # Clear covers base class but not inherited elements clear(I2B2Core) self.assertEqual("Unspecified", core_instance.sourcesystem_cd) self.assertEqual("Unspecified", cwi_instance.sourcesystem_cd) self.assertEqual(118, cwi_instance.upload_id) # Clear of subclass covers superclass as well I2B2Core.sourcesystem_cd = "test3" clear(I2B2Core) self.assertEqual("Unspecified", core_instance.sourcesystem_cd) self.assertEqual("Unspecified", cwi_instance.sourcesystem_cd) self.assertEqual(118, cwi_instance.upload_id) clear(I2B2CoreUploadIdFirst) self.assertIsNone(cwi_instance.upload_id)