class NamerTests(unittest.TestCase): def setUp(self): self.namer = Namer() def test_class(self): self.assertEqual("NamerTests", self.namer.get_class_name()) def test_method(self): n = Namer() self.assertEqual("test_method", n.get_method_name()) def test_file(self): self.assertTrue( os.path.exists(self.namer.get_directory() + "/test_namer.py")) def test_basename(self): n = Namer() self.assertTrue(n.get_basename().endswith("NamerTests.test_basename"), n.get_basename()) def test_received_name(self): filename = self.namer.get_received_filename('./stuff') self.assertEqual(filename, './stuff.received.txt') def test_approved_name(self): filename = self.namer.get_approved_filename('./stuff') self.assertEqual(filename, './stuff.approved.txt') def test_alternative_extension(self): n = Namer(extension='.html') filename = n.get_approved_filename('./stuff') self.assertEqual(filename, './stuff.approved.html')
def test_constructs_valid_diff_command(self): reporter = self.factory.get("BeyondCompare4") namer = Namer() received = namer.get_received_filename() approved = namer.get_approved_filename() command = reporter.get_command(received, approved) expected_command = [reporter.path, received, approved] self.assertEqual(command, expected_command)
def test_empty_approved_file_created_when_one_does_not_exist(self): namer = Namer() received = namer.get_received_filename() approved = namer.get_approved_filename() if os.path.isfile(approved): os.remove(approved) self.assertFalse(os.path.isfile(approved)) reporter = self.factory.get("BeyondCompare4") reporter.run_command = lambda command_array: None reporter.report(received, approved) self.assertEqual(0, os.stat(approved).st_size)
def test_approved_file_not_changed_when_one_exists_already(self): namer = Namer() approved_contents = "Approved" approved = namer.get_approved_filename() os.remove(approved) with open(approved, 'w') as approved_file: approved_file.write(approved_contents) reporter = self.factory.get("BeyondCompare4") reporter.run_command = lambda command_array: None reporter.report(namer.get_received_filename(), approved) with open(approved, 'r') as approved_file: actual_contents = approved_file.read() self.assertEqual(actual_contents, approved_contents)
def test_returns_none_when_files_are_same_files(self): namer = Namer() writer = StringWriter("b") reporter = GenericDiffReporterFactory().get_first_working() approver = FileApprover() error = approver.verify(namer, writer, reporter) self.assertEqual(None, error)
def verify_xml(xml_string, reporter=None): try: dom = xml.dom.minidom.parseString(xml_string) pretty_xml = dom.toprettyxml() except Exception: pretty_xml = xml_string verify_with_namer(pretty_xml, Namer(extension='.xml'), reporter)
def test_returns_error_when_files_are_different(self): namer = Namer() writer = StringWriter("b") reporter = TestingReporter() approver = FileApprover() error = approver.verify(namer, writer, reporter) self.assertEqual("Approval Mismatch", error)
def test_serialization(self): n = Namer() saved_reporters_file = os.path.join(n.get_directory(), 'saved-reporters.json') self.factory.save(saved_reporters_file) try: with open(saved_reporters_file, 'r') as f: file_contents = f.read() # remove the absolute path to the python_native_reporter.py file since it is different on every machine regex = re.compile(r'.*"([^"]*)python_native_reporter.py') match = regex.findall(file_contents) if match: file_contents = file_contents.replace(match[0], "") verify(file_contents, self.reporter) finally: os.remove(saved_reporters_file)
def test_full(self): namer = Namer() writer = StringWriter("b") reporter = TestingReporter() approver = FileApprover() approver.verify(namer, writer, reporter) self.assertTrue(reporter.called)
def verify(self, namer: Namer, writer: Writer, reporter: Reporter, comparator: Comparator) -> Optional[str]: base = namer.get_basename() approved = namer.get_approved_filename(base) received = namer.get_received_filename(base) # The writer has the ability to change the name of the received file received = writer.write_received_file(received) ok = self.verify_files(approved, received, reporter, comparator) if not ok: return (f"Approval Mismatch, received != approved\n" f"\tApproved: {approved}\n" f"\tReceived: {received} ") return None
def test_deserialization(self): namer = Namer() full_name = os.path.join(namer.get_directory(), 'custom-reporters.json') reporters = self.factory.load(full_name) verify(json.dumps(reporters, sort_keys=True, indent=4, separators=(',', ': ')), self.reporter)
def test_method(self): n = Namer() self.assertEqual("test_method", n.get_method_name())
def test_basename(self): n = Namer() self.assertTrue(n.get_basename().endswith("NamerTests.test_basename"), n.get_basename())
def setUp(self): self.namer = Namer()
def test_deserialization(self): namer = Namer() full_name = os.path.join(namer.get_directory(), 'custom-reporters.json') reporters = self.factory.load(full_name) verify(to_json(reporters), self.reporter)
def get_default_namer(): return Namer()
def test_serialization(self): n = Namer() path = os.path.join(n.get_directory(), 'saved-reporters.json') self.factory.save(path) with open(path, 'r') as f: verify(f.read(), self.reporter)
def test_alternative_extension(self): n = Namer(extension='.html') filename = n.get_approved_filename('./stuff') self.assertEqual(filename, './stuff.approved.html')
def test_alternative_extension(self): n = Namer(extension=".html") filename = n.get_approved_filename("./stuff") self.assertEqual(filename, "./stuff.approved.html")