class TestCAPParser_1_1(unittest.TestCase): def setUp(self): with open('resources/weather.cap', 'br') as f: data = f.read() encoding = chardet.detect(data)['encoding'] self.cap_object = CAPParser(data.decode(encoding)) def test_determine_cap_type(self): self.cap_object.determine_cap_type() self.assertEqual("CAP1_1", self.cap_object.cap_xml_type) def test_get_objectified_xml(self): self.cap_object.determine_cap_type() objectified_xml = self.cap_object.get_objectified_xml() children = objectified_xml.info.getchildren() self.assertIsNotNone(children) def test_parse_alert(self): self.cap_object.determine_cap_type() objectified_xml = self.cap_object.get_objectified_xml() alert_dict = self.cap_object.parse_alert(objectified_xml) self.assertEqual("2014-05-10T22:00:00-06:00", alert_dict['cap_sent']) def test_load(self): self.cap_object.load() result = self.cap_object.alert_list self.assertEqual("*****@*****.**", result[0]["cap_sender"])
class TestClass(unittest.TestCase): @parameterized.expand(CAP_DATA_FILES) def test_valid(self, filename, cap_xml_type, cap_alert_count, cap_sent, cap_sender): with open(filename, 'br') as f: data = f.read() encoding = chardet.detect(data)['encoding'] # Can we even load the cap alert? self.cap_object = CAPParser(data.decode(encoding)) self.cap_object.determine_cap_type() self.assertEqual(cap_xml_type, self.cap_object.cap_xml_type) # Did the CAP alert have a sender/sent? result = self.cap_object.alert_list self.assertEqual(cap_sender, result[0]["cap_sender"]) self.assertEqual(cap_sent, result[0]['cap_sent']) self.assertEqual(cap_alert_count, len(result)) # Now, can we parse the alerts? alert_list = self.cap_object.get_alert_list() alert = alert_list[0] alert_dict = self.cap_object.parse_alert(alert) self.assertEqual(cap_sent, alert_dict['cap_sent']) info_list = alert['info'] self.assertIsNotNone(info_list)
class TestCAPParser_1_2(unittest.TestCase): def setUp(self): with open('resources/taiwan.cap', 'br') as f: data = f.read() encoding = chardet.detect(data)['encoding'] self.cap_object = CAPParser(data.decode(encoding)) def test_determine_cap_type(self): self.cap_object.determine_cap_type() self.assertEqual("CAP1_2", self.cap_object.cap_xml_type) def test_get_objectified_xml(self): self.cap_object.determine_cap_type() objectified_xml = self.cap_object.get_objectified_xml() children = objectified_xml.info.getchildren() self.assertIsNotNone(children) def test_parse_alert(self): self.cap_object.determine_cap_type() objectified_xml = self.cap_object.get_objectified_xml() alert_dict = self.cap_object.parse_alert(objectified_xml) self.assertEqual("2014-05-14T20:10:00+08:00", alert_dict['cap_sent']) info = alert_dict.get('cap_info')[0] self.assertEqual("Infra", info.get('cap_category')) self.assertEqual("Observed", info.get('cap_certainty')) self.assertEqual(u"水利署訊:明德水庫:洩洪中.影響範圍:後龍溪流域,鄉鎮:頭屋鄉,請及早應變.", info.get('cap_description')) self.assertEqual("2014-05-14T20:10:00+08:00", info.get('cap_effective')) self.assertEqual(u"水庫洩洪", info.get('cap_event')) self.assertEqual("2014-05-14T21:10:00+08:00", info.get('cap_expires')) self.assertEqual(u"明德水庫:洩洪中", info.get('cap_headline')) self.assertEqual("zh-tw", info.get('cap_language')) self.assertEqual("Monitor", info.get('cap_response_type')) self.assertEqual("Moderate", info.get('cap_severity')) self.assertEqual("Future", info.get('cap_urgency')) self.assertEqual( "http://fhy2.wra.gov.tw/Pub_Web_2011/Page/Reservoir.aspx", info.get('cap_link')) self.assertEqual("profile:CAP-TWP:Event:1.0", info.get('cap_event_code')[0]['valueName']) self.assertEqual("ReservoirDis", info.get('cap_event_code')[0]['value']) self.assertEqual(u"水庫洩洪警戒", info.get('cap_parameter')[0]['value']) area = info.get('cap_area')[0] self.assertEqual(u"苗栗縣頭屋鄉", area['area_description']) self.assertEqual(1000512, area['geocodes'][0]['value']) def test_load(self): self.cap_object.load() result = self.cap_object.alert_list self.assertEqual("*****@*****.**", result[0]["cap_sender"])
class TestCAPParser_1_2(unittest.TestCase): def setUp(self): with open('resources/taiwan.cap', 'br') as f: data = f.read() encoding = chardet.detect(data)['encoding'] self.cap_object = CAPParser(data.decode(encoding)) def test_determine_cap_type(self): self.cap_object.determine_cap_type() self.assertEqual("CAP1_2", self.cap_object.cap_xml_type) def test_get_objectified_xml(self): self.cap_object.determine_cap_type() objectified_xml = self.cap_object.get_objectified_xml() children = objectified_xml.info.getchildren() self.assertIsNotNone(children) def test_parse_alert(self): self.cap_object.determine_cap_type() objectified_xml = self.cap_object.get_objectified_xml() alert_dict = self.cap_object.parse_alert(objectified_xml) self.assertEqual("2014-05-14T20:10:00+08:00", alert_dict['cap_sent']) info = alert_dict.get('cap_info')[0] self.assertEqual("Infra", info.get('cap_category')) self.assertEqual("Observed", info.get('cap_certainty')) self.assertEqual(u"水利署訊:明德水庫:洩洪中.影響範圍:後龍溪流域,鄉鎮:頭屋鄉,請及早應變.", info.get('cap_description')) self.assertEqual("2014-05-14T20:10:00+08:00", info.get('cap_effective')) self.assertEqual(u"水庫洩洪", info.get('cap_event')) self.assertEqual("2014-05-14T21:10:00+08:00", info.get('cap_expires')) self.assertEqual(u"明德水庫:洩洪中", info.get('cap_headline')) self.assertEqual("zh-tw", info.get('cap_language')) self.assertEqual("Monitor", info.get('cap_response_type')) self.assertEqual("Moderate", info.get('cap_severity')) self.assertEqual("Future", info.get('cap_urgency')) self.assertEqual("http://fhy2.wra.gov.tw/Pub_Web_2011/Page/Reservoir.aspx", info.get('cap_link')) self.assertEqual("profile:CAP-TWP:Event:1.0", info.get('cap_event_code')[0]['valueName']) self.assertEqual("ReservoirDis", info.get('cap_event_code')[0]['value']) self.assertEqual(u"水庫洩洪警戒", info.get('cap_parameter')[0]['value']) area = info.get('cap_area')[0] self.assertEqual(u"苗栗縣頭屋鄉", area['area_description']) self.assertEqual(1000512, area['geocodes'][0]['value']) def test_load(self): self.cap_object.load() result = self.cap_object.alert_list self.assertEqual("*****@*****.**", result[0]["cap_sender"])