def test_invalid_end_date(self): root = etree.Element("people") person = etree.SubElement(root, "person", attrib={'full_name': 'user'}) start = etree.SubElement(person, "start") start.text = '12-12-2007 00:00:00' end = etree.SubElement(person, "end") end.text = 'asd' file = open('asdasdasd.xml', 'wt') file.write( etree.tostring(root, xml_declaration=True, pretty_print=True, encoding='utf-8').decode()) file.close() log_message = """WARNING:src.main:--- Skipped! time data 'asd' does not match format '%d-%m-%Y %H:%M:%S' <person full_name="user"> <start>12-12-2007 00:00:00</start> <end>asd</end> </person> ---""" with self.assertLogs(main.logger, level='DEBUG') as cm: main.calculate(file.name) self.assertEqual(cm.output[0], log_message) os.remove(file.name)
def test_more_than_24_hours_field(self): root = etree.Element("people") person = etree.SubElement(root, "person", attrib={'full_name': 'user'}) start = etree.SubElement(person, "start") start.text = '12-12-2007 00:00:00' end = etree.SubElement(person, "end") end.text = '14-12-2007 00:00:00' file = open('asdasdasd.xml', 'wt') file.write( etree.tostring(root, xml_declaration=True, pretty_print=True, encoding='utf-8').decode()) file.close() log_message = """WARNING:src.main:--- Added more than 24 hours a day <person full_name="user"> <start>12-12-2007 00:00:00</start> <end>14-12-2007 00:00:00</end> </person> ---""" with self.assertLogs(main.logger, level='DEBUG') as cm: main.calculate(file.name) self.assertEqual(cm.output[0], log_message) os.remove(file.name)
def test_no_end_field(self): root = etree.Element("people") person = etree.SubElement(root, "person", attrib={'full_name': 'user'}) start = etree.SubElement(person, "start") start.text = '12-12-2007 00:00:00' random_field = etree.SubElement(person, "random_field") random_field.text = '12-12-2007 00:00:00' file = open('asdasdasd.xml', 'wt') file.write( etree.tostring(root, xml_declaration=True, pretty_print=True, encoding='utf-8').decode()) file.close() log_message = """WARNING:src.main:--- Skipped! not found "start" or "end" field in element <person full_name="user"> <start>12-12-2007 00:00:00</start> <random_field>12-12-2007 00:00:00</random_field> </person> ---""" with self.assertLogs(main.logger, level='DEBUG') as cm: main.calculate(file.name) self.assertEqual(cm.output[0], log_message) os.remove(file.name)
def test_no_username_field(self): root = etree.Element("people") person = etree.SubElement(root, "person") start = etree.SubElement(person, "start") start.text = 'asd' end = etree.SubElement(person, "end") end.text = 'asdasd' file = open('asdasdasd.xml', 'wt') file.write( etree.tostring(root, xml_declaration=True, pretty_print=True, encoding='utf-8').decode()) file.close() log_message = """WARNING:src.main:--- Skipped! field "full_name" is not specified <person> <start>asd</start> <end>asdasd</end> </person> ---""" with self.assertLogs(main.logger, level='DEBUG') as cm: main.calculate(file.name) self.assertEqual(cm.output[0], log_message) os.remove(file.name)
def test_invalid_xml_syntax(self): file = open('asdasdasd.xml', 'wt') file.write("qwdqwd") file.close() with self.assertRaises(main.AppException) as exc: main.calculate(file.name) self.assertIsInstance(exc.exception, main.AppException) self.assertEqual( exc.exception.args[0], 'Invalid syntax: Document is empty, line 1, column 1', )
def test_success_with_users_filter(self): expected = { 'user0': { '2007-12-12': 3600, '2007-12-13': 3600, } } result = main.calculate(self.test_file, users_filter=['user0']) self.assertDictEqual(result, expected)
def test_success_with_end_filter(self): expected = { 'user0': { '2007-12-12': 3600, }, 'user1': { '2007-12-12': 3600, }, } result = main.calculate(self.test_file, end_filter=datetime(2007, 12, 13)) self.assertDictEqual(result, expected)
def test_success(self): expected = { 'user0': { '2007-12-12': 3600, '2007-12-13': 3600, }, 'user1': { '2007-12-12': 3600, '2007-12-13': 3600, }, } self.assertDictEqual(main.calculate(self.test_file), expected)