def test_datetime_type(self): attrs = { "FileOID": "ODM.MDV.TEST.001", "Granularity": "Metadata", "AsOfDateTime": "2020-07-13T00:13:51.309617+00:00", "CreationDateTime": "2020-07-13T00:13:51.309617+00:00", "ODMVersion": "1.3.2", "FileType": "Snapshot", "Originator": "RDS", "SourceSystem": "ODMLib" } odm = ODM.ODM(**attrs) self.assertEqual(odm.AsOfDateTime, "2020-07-13T00:13:51.309617+00:00") # derived current datetime in ISO formate from datetime library attrs["AsOfDateTime"] = self.set_datetime() odm = ODM.ODM(**attrs) self.assertIsInstance(odm, ODM.ODM) # date only attrs["AsOfDateTime"] = "2020-07-13" with self.assertRaises(ValueError): odm = ODM.ODM(**attrs) # basic time attrs["AsOfDateTime"] = "2020-07-13T00:13:51" odm = ODM.ODM(**attrs) self.assertEqual(odm.AsOfDateTime, "2020-07-13T00:13:51") # out of range time attrs["AsOfDateTime"] = "2020-07-13T00:13:61" with self.assertRaises(ValueError): odm = ODM.ODM(**attrs)
def test_insert_item_with_none_element(self): attrs = self.set_item_attributes() item = ODM.ItemDef(**attrs) # to support the test a null version of CodeListRef is created, but should be ignored on output if item.CodeListRef: self.assertIsNotNone(item.CodeListRef) if item.SignificantDigits: self.assertIsNotNone(item.SignificantDigits) self.loader.open_odm_document(self.odm_file) mdv = self.loader.MetaDataVersion() mdv.ItemDef.append(item) attrs = self.get_root_attributes() root = ODM.ODM(**attrs) study = self.add_study(mdv) root.Study = [study] root.write_xml(self.odm_file_out) loader = LD.ODMLoader(OL.XMLODMLoader()) loader.open_odm_document(self.odm_file_out) mdv = loader.MetaDataVersion() # is_found_inserted_item = False # for it in mdv.ItemDef: # if it.OID == "ODM.IT.AE.TEST": # is_found_inserted_item = True # self.assertTrue(is_found_inserted_item) itd = mdv.find("ItemDef", "OID", "ODM.IT.AE.TEST") self.assertEqual(itd.OID, "ODM.IT.AE.TEST") # comment was created as None and should not be part of the object self.assertIsNone(itd.Comment) itd_json = itd.to_json() self.assertNotIn("Comment", itd_json) odm_json = root.to_json()
def setUp(self) -> None: current_datetime = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat() root = ODM.ODM(FileOID="ODM.DEMO.001", Granularity="Metadata", AsOfDateTime=current_datetime, CreationDateTime=current_datetime, ODMVersion="1.3.2", FileType="Snapshot", Originator="swhume", SourceSystem="odmlib", SourceSystemVersion="0.1") # create Study and add to ODM root.Study.append(ODM.Study(OID="ODM.GET.STARTED")) # create the global variables root.Study[0].GlobalVariables = ODM.GlobalVariables() root.Study[0].GlobalVariables.StudyName = ODM.StudyName(_content="Get Started with ODM XML") root.Study[0].GlobalVariables.StudyDescription = ODM.StudyDescription( _content="Demo to get started with odmlib") root.Study[0].GlobalVariables.ProtocolName = ODM.ProtocolName(_content="ODM XML Get Started") # create the MetaDataVersion root.Study[0].MetaDataVersion.append(ODM.MetaDataVersion(OID="MDV.DEMO-ODM-01", Name="Get Started MDV", Description="Get Started Demo")) # create Protocol p = ODM.Protocol() p.Description = ODM.Description() p.Description.TranslatedText.append(ODM.TranslatedText(_content="Get Started Protocol", lang="en")) p.StudyEventRef.append(ODM.StudyEventRef(StudyEventOID="BASELINE", OrderNumber=1, Mandatory="Yes")) root.Study[0].MetaDataVersion[0].Protocol = p # create a StudyEventDef sed = ODM.StudyEventDef(OID="BASELINE", Name="Baseline Visit", Repeating="No", Type="Scheduled") sed.FormRef.append(ODM.FormRef(FormOID="ODM.F.DM", Mandatory="Yes", OrderNumber=1)) root.Study[0].MetaDataVersion[0].StudyEventDef.append(sed) # create a FormDef fd = ODM.FormDef(OID="ODM.F.DM", Name="Demographics", Repeating="No") fd.ItemGroupRef.append(ODM.ItemGroupRef(ItemGroupOID="ODM.IG.DM", Mandatory="Yes", OrderNumber=2)) root.Study[0].MetaDataVersion[0].FormDef.append(fd) # create an ItemGroupDef igd = ODM.ItemGroupDef(OID="ODM.IG.DM", Name="Demographics", Repeating="No") igd.ItemRef.append(ODM.ItemRef(ItemOID="ODM.IT.DM.BRTHYR", Mandatory="Yes")) root.Study[0].MetaDataVersion[0].ItemGroupDef.append(igd) # create an ItemDef itd = ODM.ItemDef(OID="ODM.IT.DM.BRTHYR", Name="Birth Year", DataType="integer") itd.Description = ODM.Description() itd.Description.TranslatedText.append(ODM.TranslatedText(_content="Year of the subject's birth", lang="en")) itd.Question = ODM.Question() itd.Question.TranslatedText.append(ODM.TranslatedText(_content="Birth Year", lang="en")) itd.Alias.append(ODM.Alias(Context="CDASH", Name="BRTHYR")) itd.Alias.append(ODM.Alias(Context="SDTM", Name="BRTHDTC")) root.Study[0].MetaDataVersion[0].ItemDef.append(itd) # save the new ODM document to an ODM XML file root.write_xml(ODM_XML_FILE) # save the same ODM document to a JSON file root.write_json(ODM_JSON_FILE)
def create_root(self): """ create the ODM root element object with test data :return: ODM root element object """ root = { "FileOID": "ODM.TEST.CD.001", "Granularity": "Metadata", "AsOfDateTime": self.set_datetime(), "CreationDateTime": self.set_datetime(), "ODMVersion": "1.3.2", "Originator": "ODMLIB", "SourceSystem": "ODMLIB", "SourceSystemVersion": "0.1", "FileType": "Snapshot" } root = ODM.ODM(**root) return root
def add_root(self): attrs = self.get_root_attributes() root = ODM.ODM(**attrs) return root
def test_xml_to_string(self): current_datetime = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat() root = ODM.ODM(FileOID="ODM.DEMO.001", Granularity="Metadata", AsOfDateTime=current_datetime, CreationDateTime=current_datetime, ODMVersion="1.3.2", FileType="Snapshot", Originator="swhume", SourceSystem="odmlib", SourceSystemVersion="0.1") # create Study and add to ODM root.Study.append(ODM.Study(OID="ODM.GET.STARTED")) # create the global variables root.Study[0].GlobalVariables = ODM.GlobalVariables() root.Study[0].GlobalVariables.StudyName = ODM.StudyName(_content="Get Started with ODM XML") root.Study[0].GlobalVariables.StudyDescription = ODM.StudyDescription( _content="Demo to get started with odmlib") root.Study[0].GlobalVariables.ProtocolName = ODM.ProtocolName(_content="ODM XML Get Started") # create the MetaDataVersion root.Study[0].MetaDataVersion.append(ODM.MetaDataVersion(OID="MDV.DEMO-ODM-01", Name="Get Started MDV", Description="Get Started Demo")) # create Protocol p = ODM.Protocol() p.Description = ODM.Description() p.Description.TranslatedText.append(ODM.TranslatedText(_content="Get Started Protocol", lang="en")) p.StudyEventRef.append(ODM.StudyEventRef(StudyEventOID="BASELINE", OrderNumber=1, Mandatory="Yes")) root.Study[0].MetaDataVersion[0].Protocol = p # create a StudyEventDef sed = ODM.StudyEventDef(OID="BASELINE", Name="Baseline Visit", Repeating="No", Type="Scheduled") sed.FormRef.append(ODM.FormRef(FormOID="ODM.F.DM", Mandatory="Yes", OrderNumber=1)) root.Study[0].MetaDataVersion[0].StudyEventDef.append(sed) # create a FormDef fd = ODM.FormDef(OID="ODM.F.DM", Name="Demographics", Repeating="No") fd.ItemGroupRef.append(ODM.ItemGroupRef(ItemGroupOID="ODM.IG.DM", Mandatory="Yes", OrderNumber=2)) root.Study[0].MetaDataVersion[0].FormDef.append(fd) # create an ItemGroupDef igd = ODM.ItemGroupDef(OID="ODM.IG.DM", Name="Demographics", Repeating="No") igd.ItemRef.append(ODM.ItemRef(ItemOID="ODM.IT.DM.BRTHYR", Mandatory="Yes")) root.Study[0].MetaDataVersion[0].ItemGroupDef.append(igd) # create an ItemDef itd = ODM.ItemDef(OID="ODM.IT.DM.BRTHYR", Name="Birth Year", DataType="integer") itd.Description = ODM.Description() itd.Description.TranslatedText.append(ODM.TranslatedText(_content="Year of the subject's birth", lang="en")) itd.Question = ODM.Question() itd.Question.TranslatedText.append(ODM.TranslatedText(_content="Birth Year", lang="en")) itd.Alias.append(ODM.Alias(Context="CDASH", Name="BRTHYR")) itd.Alias.append(ODM.Alias(Context="SDTM", Name="BRTHDTC")) root.Study[0].MetaDataVersion[0].ItemDef.append(itd) odm_xml_string = root.to_xml_string() # add namespaces nsr = NS.NamespaceRegistry() odm_str = nsr.set_odm_namespace_attributes_string(odm_xml_string) with open("./data/simple_create_from_string.xml", "w") as xml_file: xml_file.write(odm_str) loader = LD.ODMLoader(OL.XMLODMLoader(model_package="odm_1_3_2", ns_uri="http://www.cdisc.org/ns/odm/v1.3")) loader.open_odm_document("./data/simple_create_from_string.xml") mdv = loader.MetaDataVersion() item_list = mdv.ItemDef item = item_list[0] self.assertEqual(item.OID, "ODM.IT.DM.BRTHYR")