def test_by_id(db, capsys): # capsys is a Pytest builtin fixture from dlx.marc import Auth, Bib, BibSet from xmldiff.main import diff_texts control = '<collection><record><datafield tag="035" ind1=" " ind2=" "><subfield code="a">(DHL)1</subfield></datafield><datafield tag="191" ind1=" " ind2=" "><subfield code="a">TEST/1</subfield></datafield><datafield tag="245" ind1=" " ind2=" "><subfield code="a">title_1</subfield></datafield><datafield tag="700" ind1=" " ind2=" "><subfield code="a">name_1</subfield><subfield code="0">(DHLAUTH)1</subfield></datafield><datafield tag="980" ind1=" " ind2=" "><subfield code="a">BIB</subfield></datafield><datafield tag="FFT" ind1=" " ind2=" "><subfield code="a">https://mock_bucket.s3.amazonaws.com/1e50210a0202497fb79bc38b6ade6c34</subfield><subfield code="d">English</subfield><subfield code="n">TEST_1-EN.pdf</subfield></datafield></record></collection>' dlx_dl.run(connect=db, source='test', type='bib', id='1', xml='STDOUT') assert diff_texts(capsys.readouterr().out, control) == [] control = '<collection><record><datafield tag="035" ind1=" " ind2=" "><subfield code="a">(DHLAUTH)1</subfield></datafield><datafield tag="100" ind1=" " ind2=" "><subfield code="a">name_1</subfield></datafield><datafield tag="980" ind1=" " ind2=" "><subfield code="a">AUTHORITY</subfield></datafield><datafield tag="980" ind1=" " ind2=" "><subfield code="a">PERSONAL</subfield></datafield></record></collection>' dlx_dl.run(connect=db, source='test', type='auth', id='1', xml='STDOUT') assert diff_texts(capsys.readouterr().out, control) == [] # --ids dlx_dl.run(connect=db, source='test', type='auth', ids=['1'], xml='STDOUT') assert diff_texts(capsys.readouterr().out, control) == []
def test_modified_since_log(db, capsys): from http.server import HTTPServer from xmldiff.main import diff_texts from dlx import DB from dlx.marc import Bib server = HTTPServer(('127.0.0.1', 9090), None) responses.add(responses.POST, 'http://127.0.0.1:9090', body='test OK') dlx_dl.API_URL = 'http://127.0.0.1:9090' dlx_dl.run(connect=db, source='test', type='bib', modified_within=100, use_api=True, api_key='x') capsys.readouterr().out # clear stdout Bib().set('999', 'a', 'new').commit() dlx_dl.run(connect=db, source='test', type='bib', modified_since_log=True, use_api=True, api_key='x') entry = db['dummy']['dlx_dl_log'].find_one({'record_id': 3}) control = '<record><datafield tag="035" ind1=" " ind2=" "><subfield code="a">(DHL)3</subfield></datafield><datafield tag="980" ind1=" " ind2=" "><subfield code="a">BIB</subfield></datafield><datafield tag="999" ind1=" " ind2=" "><subfield code="a">new</subfield></datafield></record>' assert diff_texts(entry['xml'], control) == []
def test_to_file(db, tmp_path): from xmldiff.main import diff_texts control = '<collection><record><datafield tag="035" ind1=" " ind2=" "><subfield code="a">(DHL)1</subfield></datafield><datafield tag="191" ind1=" " ind2=" "><subfield code="a">TEST/1</subfield></datafield><datafield tag="245" ind1=" " ind2=" "><subfield code="a">title_1</subfield></datafield><datafield tag="700" ind1=" " ind2=" "><subfield code="a">name_1</subfield><subfield code="0">(DHLAUTH)1</subfield></datafield><datafield tag="980" ind1=" " ind2=" "><subfield code="a">BIB</subfield></datafield><datafield tag="FFT" ind1=" " ind2=" "><subfield code="a">https://mock_bucket.s3.amazonaws.com/1e50210a0202497fb79bc38b6ade6c34</subfield><subfield code="d">English</subfield><subfield code="n">TEST_1-EN.pdf</subfield></datafield></record></collection>' out = tmp_path / 'out.xml' dlx_dl.run(connect=db, source='test', type='bib', id='1', xml=out) assert diff_texts(out.read_text(), control) == []
def test_post_and_log(db, excel_export): from http.server import HTTPServer from xmldiff.main import diff_texts server = HTTPServer(('127.0.0.1', 9090), None) responses.add(responses.POST, 'http://127.0.0.1:9090', body='test OK') dlx_dl.API_URL = 'http://127.0.0.1:9090' dlx_dl.run(connect=db, source='test', type='bib', modified_within=100, use_api=True, api_key='x') entry = db['dummy']['dlx_dl_log'].find_one({'record_id': 1}) assert entry['record_id'] == 1 assert entry['response_code'] == 200 assert entry['response_text'] == 'test OK' assert isinstance(entry['export_start'], datetime) assert isinstance(entry['time'], datetime) control = '<record><datafield tag="035" ind1=" " ind2=" "><subfield code="a">(DHL)1</subfield></datafield><datafield tag="191" ind1=" " ind2=" "><subfield code="a">TEST/1</subfield></datafield><datafield tag="245" ind1=" " ind2=" "><subfield code="a">title_1</subfield></datafield><datafield tag="700" ind1=" " ind2=" "><subfield code="a">name_1</subfield><subfield code="0">(DHLAUTH)1</subfield></datafield><datafield tag="980" ind1=" " ind2=" "><subfield code="a">BIB</subfield></datafield><datafield tag="FFT" ind1=" " ind2=" "><subfield code="a">https://mock_bucket.s3.amazonaws.com/1e50210a0202497fb79bc38b6ade6c34</subfield><subfield code="d">English</subfield><subfield code="n">TEST_1-EN.pdf</subfield></datafield></record>' assert diff_texts(entry['xml'], control) == [] entry = db['dummy']['dlx_dl_log'].find_one({'source': 'test'}) assert isinstance(entry['export_start'], datetime) entry = db['dummy']['dlx_dl_log'].find_one({ 'source': 'test', 'export_end': { '$exists': 1 } }) assert entry['record_type'] == 'bib' assert isinstance(entry['export_end'], datetime)
def test_multiple_test_cases_with_different_verdicts(self): self.maxDiff = None tc1 = TestCaseResult('tc1', 'a.b.tc1', times[0][0]) tc1.set_finished(times[0][2], Verdict.PASSED, None, '') tc2 = TestCaseResult('tc2', 'a.b.tc2', times[1][0]) tc2.set_finished(times[1][2], Verdict.FAILED, AssertionError('Message'), 'stacktrace') tc3 = TestCaseResult('tc3', 'a.c.tc3', times[2][0]) tc3.set_finished( times[2][2], Verdict.SKIPPED, AssertionError('test case skipped'), 'stacktrace') test_results = results([tc1, tc2, tc3]) self.assertFalse( main.diff_texts( writer.generate_junit_report(test_results).encode('utf-8'), dedent( f"""\ <testsuites disabled="0" errors="0" failures="1" tests="3" time="54.102"> \t<testsuite disabled="0" errors="0" failures="1" file="None" log="None" name="Name of Suite" skipped="1" tests="3" time="54.102" timestamp="{times[0][1]}" url="None"> \t\t<testcase classname="a.b" name="tc1" status="SUCCESSFUL" time="{times[0][4]}" timestamp="{times[0][1]}"/> \t\t<testcase classname="a.b" name="tc2" status="FAILED" time="{times[1][4]}" timestamp="{times[1][1]}"> \t\t\t<failure message="Message" type="failure">stacktrace</failure> \t\t</testcase> \t\t<testcase classname="a.c" name="tc3" status="SUCCESSFUL" time="{times[2][4]}" timestamp="{times[2][1]}"> \t\t\t<skipped message="test case skipped" type="skipped">stacktrace</skipped> \t\t</testcase> \t</testsuite> </testsuites> """)))
def xml_tree_equal_to_xml_string(et, xml_s: str) -> bool: xml_actual = tostring(et, encoding="unicode") actions = diff_texts(xml_actual, xml_s) if actions: print(actions) # No actions indicates that xmldiff thinks these two XML documents are equal. return len(actions) == 0
def test_xml_encoding(): from dlx.marc import Bib from xmldiff import main control = '<record><datafield ind1=" " ind2=" " tag="245"><subfield code="a">Title with an é</subfield></datafield></record>' bib = Bib().set('245', 'a', 'Title with an é') assert main.diff_texts(bib.to_xml(), control) == []
def test_to_xml(db): from dlx.marc import Bib from xmldiff import main control = '<record><controlfield tag="000">leader</controlfield><controlfield tag="008">controlfield</controlfield><datafield ind1=" " ind2=" " tag="245"><subfield code="a">This</subfield><subfield code="b">is the</subfield><subfield code="c">title</subfield></datafield><datafield ind1=" " ind2=" " tag="520"><subfield code="a">Description</subfield></datafield><datafield ind1=" " ind2=" " tag="520"><subfield code="a">Another description</subfield><subfield code="a">Repeated subfield</subfield></datafield><datafield ind1=" " ind2=" " tag="650"><subfield code="a">Header</subfield><subfield code="0">1</subfield></datafield><datafield ind1=" " ind2=" " tag="710"><subfield code="a">Another header</subfield><subfield code="0">2</subfield></datafield></record>' bib = Bib.find_one({'_id': 1}) assert main.diff_texts(bib.to_xml(), control) == []
def compare_element_at_xpath(config: str, element: str, xpath: str, context: dict) -> bool: """ Grab an xml fragment from the config given at xpath and compare it to this element :param config: XML document string from which to pull the XML element to compare :param element: element to check against :param xpath: xpath to grab an xml fragment from the config for comparison :param context: jinja context used to interpolate any variables that may be present in the template :return: bool true if they match """ # render metadata will combine the xpath with the cherry_pick attribute to give us the full xpath # to the element in question. It will also load the element from our source element or source file into the # element attribute if element == '' or element is None: logger.warning('Element was blank for validate_xml test!') return False config_doc = elementTree.fromstring(config) relative_xpath = xpath.replace('/config/', './') config_element = config_doc.find(relative_xpath) config_element_str = elementTree.tostring(config_element).strip() diffs = xmldiff_main.diff_texts(config_element_str, element) if len(diffs) == 0: return True return False
def test_test_case_with_owner(self): self.maxDiff = None tc = TestCaseResult('tc1', 'a.b.tc1', times[0][0]) tc.set_finished( times[0][2], Verdict.PASSED, exception=None, stacktrace=None, owner=('Owner <*****@*****.**>')) self.assertFalse( main.diff_texts( writer.generate_testng_report(results([tc])), dedent( """\ <?xml version="1.0" encoding="utf-8"?> <testng-results version="1.0"> <reporter-output/> <suite duration-ms="18034" finished-at="2017-05-08T14:15:17Z" name="Name of Suite" started-at="2017-05-08T14:14:59Z"> <groups/> <test duration-ms="18034" finished-at="2017-05-08T14:15:17Z" name="Name of Suite" started-at="2017-05-08T14:14:59Z"> <class name="a.b"> <test-method description="Test case owner: Owner <[email protected]> (Please contact the owner if you need assistance with trouble-shooting.)" duration-ms="18034" finished-at="2017-05-08T14:15:17Z" name="tc1" signature="a.b.tc1" started-at="2017-05-08T14:14:59Z" status="PASS"/> </class> </test> </suite> </testng-results> """).encode('utf-8')))
def assert_xml_diff(actual, expected, msg=None): # Only use xmldiff if it has been imported and both the xml messages # contain data if xml_diff and actual and expected: diff = xml_diff.diff_texts(actual, expected) assert len(diff) == 0, msg else: assert actual == expected, msg
def test_junit_xml_output(run_semgrep_in_tmp, snapshot): actual_output = run_semgrep_in_tmp("rules/eqeq.yaml", output_format="junit-xml") f = open(str(snapshot.snapshot_dir) + "/results.xml", "r") expected_output = f.read() f.close() assert len(main.diff_texts(expected_output, actual_output)) == 0
def test_passed(self): self.maxDiff = None time_index = 1 self.assertFalse( main.diff_texts( writer.generate_testng_report(results([tc(Verdict.PASSED, time_index)])), no_exception(time_index, 'PASS')))
def test_params_appended_to_test_case_name(self): self.maxDiff = None tc = TestCaseResult('tc1', 'a.b.tc1', times[0][0], params=['value1', 'value2']) tc.set_finished(times[0][2], Verdict.PASSED, None, '[value1, value2]') self.assertFalse( main.diff_texts( writer.generate_junit_report(results([tc])).encode('utf-8'), no_exception(0, Verdict.PASSED, name_suffix='[value1, value2]')))
def test_passed(self): self.maxDiff = None time_index = 0 self.assertFalse( main.diff_texts( writer.generate_junit_report(results([tc(Verdict.PASSED, time_index)])).encode('utf-8'), no_exception(time_index, Verdict.PASSED)))
def test_api_diff_texts(self): # diff_text can take bytes with open(LEFT_FILE, "rb") as linfile: with open(RIGHT_FILE, "rb") as rinfile: left = linfile.read() right = rinfile.read() result1 = main.diff_texts(left, right) # And unicode result2 = main.diff_texts(left.decode("utf8"), right.decode("utf8")) self.assertEqual(result1, result2) with open(LEFT_FILE, "rb") as infile: with open(RIGHT_FILE, "rb") as infile: # Give something else, and it fails: with self.assertRaises(ValueError): main.diff_texts(infile, infile)
def test_by_list(db, tmp_path, capsys): from xmldiff.main import diff_texts ids = tmp_path / 'ids.txt' ids.write_text('\n'.join([str(x) for x in (1, 2)])) control = '<collection><record><datafield tag="035" ind1=" " ind2=" "><subfield code="a">(DHL)1</subfield></datafield><datafield tag="191" ind1=" " ind2=" "><subfield code="a">TEST/1</subfield></datafield><datafield tag="245" ind1=" " ind2=" "><subfield code="a">title_1</subfield></datafield><datafield tag="700" ind1=" " ind2=" "><subfield code="a">name_1</subfield><subfield code="0">(DHLAUTH)1</subfield></datafield><datafield tag="980" ind1=" " ind2=" "><subfield code="a">BIB</subfield></datafield><datafield tag="FFT" ind1=" " ind2=" "><subfield code="a">https://mock_bucket.s3.amazonaws.com/1e50210a0202497fb79bc38b6ade6c34</subfield><subfield code="d">English</subfield><subfield code="n">TEST_1-EN.pdf</subfield></datafield></record><record><datafield tag="035" ind1=" " ind2=" "><subfield code="a">(DHL)2</subfield></datafield><datafield tag="245" ind1=" " ind2=" "><subfield code="a">title_2</subfield></datafield><datafield tag="700" ind1=" " ind2=" "><subfield code="a">name_2</subfield><subfield code="0">(DHLAUTH)2</subfield></datafield><datafield tag="980" ind1=" " ind2=" "><subfield code="a">BIB</subfield></datafield></record></collection>' dlx_dl.run(connect=db, source='test', type='bib', list=ids, xml='STDOUT') assert diff_texts(capsys.readouterr().out, control) == []
def test_error_test_case_with_exception(self): self.maxDiff = None time_index = 0 self.assertFalse( main.diff_texts( writer.generate_junit_report( results( [tc(Verdict.ERROR, time_index, AssertionError('Message'), 'stacktrace')])).encode('utf-8'), with_exception(time_index, Verdict.ERROR, 'stacktrace')))
def test_ignored_mapped_to_skip(self): self.maxDiff = None time_index = 0 self.assertFalse( main.diff_texts( writer.generate_junit_report( results( [tc(Verdict.IGNORED, time_index, AssertionError('Message'), 'stacktrace')])).encode('utf-8'), with_exception(time_index, Verdict.IGNORED, 'stacktrace')))
def test_create_default_config(self): '''Make sure default config lines up with actual.''' config = configs.default() path = 'mr_utils/test_data/tests/gadgetron/config/' file = 'default.xml' load_test_data(path, [file], do_return=False) with open('%s/%s' % (path, file), 'r') as f: truth = f.read() res = len(main.diff_texts(truth.encode(), config.tostring().encode())) self.assertTrue(res == 0)
def test_diff_patch(self): here = os.path.split(__file__)[0] lfile = os.path.join(here, "test_data", "all_actions.left.xml") rfile = os.path.join(here, "test_data", "all_actions.right.xml") with open(lfile) as f: left = f.read() with open(rfile) as f: right = f.read() diff = diff_texts(left, right, formatter=DiffFormatter(normalize=WS_NONE)) result = patch_text(diff, left) compare_elements(etree.fromstring(result), etree.fromstring(right))
def test_pending_mapped_to_fail(self): self.maxDiff = None time_index = 2 self.assertFalse( main.diff_texts( writer.generate_testng_report( results( [ tc( Verdict.PENDING, time_index, AssertionError('assert failed'), 'stacktrace') ])), with_exception(time_index, 'FAIL', 'stacktrace')))
def test_ignored_mapped_to_skip(self): self.maxDiff = None time_index = 2 self.assertFalse( main.diff_texts( writer.generate_testng_report( results( [ tc( Verdict.IGNORED, time_index, AssertionError('assert failed'), 'stacktrace') ])), with_exception(time_index, 'SKIP', 'stacktrace')))
def test_failed_test_case_with_exception(self): self.maxDiff = None time_index = 1 self.assertFalse( main.diff_texts( writer.generate_testng_report( results( [ tc( Verdict.FAILED, time_index, AssertionError('assert failed'), 'stacktrace') ])), with_exception(time_index, 'FAIL', 'stacktrace')))
def compare(self, old: str, new: str): old, new = self.preprocess(old, new) old, new = get_xml(old), get_xml(new) diffs = xml.diff_texts(old, new, diff_options={ 'F': 0.5, 'ratio_mode': 'accurate' }) diff = xml.diff_texts( old, new, formatter=formatting.XMLFormatter(normalize=formatting.WS_BOTH), diff_options={ 'F': 0.5, 'ratio_mode': 'accurate' }) diff = normalize_html(diff) self.set_style(diff) return diff.prettify()
def test_module_xml(orb_course): """Ensure the module.xml content is built as expected Test against an existing XML file by reading into memory """ oppia_exporter = orb_course.oppia_exporter() # type: OppiaExport expected_file_path = os.path.join(os.path.dirname(__file__), "expected_module.xml") with open(expected_file_path, "r") as expected_file: # expected_xml = ET.fromstring(expected_file.read()) diff = differ.diff_texts( expected_file.read(), oppia_exporter.module_xml(oppia_exporter.module_context()), ) assert not diff
def test_paragraph_conversion(): paragraph_base = f"{os.path.dirname(os.path.realpath(__file__))}/data/paragraph_" input_path = f"{paragraph_base}input.txt" target_path = f"{paragraph_base}target.xml" with open(input_path, "r") as f: paragraph = f.read() with open(target_path, "rb") as f: target = f.read() actual_out = FrameAnnotatorGenerator.convert(paragraph).encode("UTF-8") actions_to_fix_differences = main.diff_texts(actual_out, target) # Assert that only changes that need to happen are to the timestamp assert (len(actions_to_fix_differences) == len([ action for action in actions_to_fix_differences if action.name == "cDate" ]))
def test_module_xml(orb_course): """Ensure the module.xml content is built as expected Test against an existing XML file by reading into memory """ oppia_exporter = orb_course.oppia_exporter() # type: OppiaExport expected_file_path = os.path.join(os.path.dirname(__file__), "expected_module.xml") with open(expected_file_path, "r") as expected_file: # expected_xml = ET.fromstring(expected_file.read()) diff = differ.diff_texts( expected_file.read(), oppia_exporter.module_xml(oppia_exporter.module_context()), ) assert not diff
def test_run_verdict_mapped_to_test_case(self): self.maxDiff = None self.assertFalse( main.diff_texts( writer.generate_junit_report( results([tc(Verdict.PASSED, 0)], run_verdict=Verdict.FAILED)).encode('utf-8'), dedent( f"""\ <testsuites disabled="0" errors="0" failures="1" tests="2" time="18.034"> \t<testsuite disabled="0" errors="0" failures="1" file="None" log="None" name="Name of Suite" skipped="0" tests="2" time="18.034" timestamp="{times[0][1]}" url="None"> \t\t<testcase classname="a.b" name="tc1" status="SUCCESSFUL" time="{times[0][4]}" timestamp="{times[0][1]}"/> \t\t<testcase classname="Name of Suite" name="Name of Suite" status="FAILED" timestamp="{times[0][1]}"> \t\t\t<failure message="Message" type="failure"/> \t\t</testcase> \t</testsuite> </testsuites> """)))
def compare(esdls): with self.flask_app.app_context(): esh = get_handler() es_id1 = esdls['esdl1'] es_id2 = esdls['esdl2'] es1_str = esh.to_string( es_id1).encode() # convert string to bytes es2_str = esh.to_string( es_id2).encode() # convert string to bytes logger.info("Comparing EnergySystems with id {} and {}".format( es_id1, es_id2)) results = main.diff_texts(es1_str, es2_str) emit('esdl_compare_window', results, namespace='/esdl')
def test_run_verdict_mapped_to_test_case(self): self.maxDiff = None self.assertFalse( main.diff_texts( writer.generate_testng_report( results([tc(Verdict.PASSED, 0)], run_verdict=Verdict.FAILED)), dedent( """\ <?xml version="1.0" encoding="utf-8"?> <testng-results version="1.0"> <reporter-output/> <suite duration-ms="18034" finished-at="2017-05-08T14:15:17Z" name="Name of Suite" started-at="2017-05-08T14:14:59Z"> <groups/> <test duration-ms="18034" finished-at="2017-05-08T14:15:17Z" name="Name of Suite" started-at="2017-05-08T14:14:59Z"> <class name="a.b"> <test-method duration-ms="18034" finished-at="2017-05-08T14:15:17Z" name="tc1" signature="a.b.tc1" started-at="2017-05-08T14:14:59Z" status="PASS"/> </class> <class name="Name of Suite"> <test-method duration-ms="18034" finished-at="2017-05-08T14:15:17Z" name="Name of Suite" signature="Name of Suite" started-at="2017-05-08T14:14:59Z" status="FAIL"> <exception class="str"> <message> <![CDATA[A multiline error message .]]> \n\ \n\ </message> <full-stacktrace> <![CDATA[str: A multiline error message . ]]> \n\ \n\ </full-stacktrace> </exception> <reporter-output/> </test-method> </class> </test> </suite> </testng-results> """).encode('utf-8')))