def test_dumpstore(self): d = Devel() d.config = Mock() # only test that Triplestore is called correctly, mock any # calls to any real database config = {"connect.return_value": Mock(**{"get_serialized.return_value": b"[fake store content]"})} printmock = MagicMock() with patch("ferenda.devel.TripleStore", **config): with patch(builtins + ".print", printmock): d.dumpstore(format="trix") want = "[fake store content]" printmock.assert_has_calls([call(want)])
def test_dumpstore(self): d = Devel() d.config = Mock() # only test that Triplestore is called correctly, mock any # calls to any real database config = { 'connect.return_value': Mock(**{'get_serialized.return_value': b'[fake store content]'}) } printmock = MagicMock() with patch('ferenda.devel.TripleStore', **config): with patch('builtins.print', printmock): d.dumpstore(format="trix") want = "[fake store content]" printmock.assert_has_calls([call(want)])
def test_dumprdf(self): fileno, tmpfile = mkstemp() fp = os.fdopen(fileno, "w") fp.write("""<html xmlns="http://www.w3.org/1999/xhtml"> <head about="http://example.org/doc"> <title property="http://purl.org/dc/terms/">Doc title</title> </head> <body>...</body> </html>""") fp.close() d = Devel() mock = MagicMock() with patch('builtins.print', mock): d.dumprdf(tmpfile, format="nt") os.unlink(tmpfile) self.assertTrue(mock.called) want = '<http://example.org/doc> <http://purl.org/dc/terms/> "Doc title" .\n\n' mock.assert_has_calls([call(want)])
def test_fsmparse(self): try: # 1. write a new python module containing a class with a staticmethod with open("testparser.py", "w") as fp: fp.write(""" from six import text_type as str from ferenda.elements import Body, Paragraph class Testobject(object): @staticmethod def get_parser(): return Parser() class Parser(object): def parse(self, source): res = Body() for chunk in source: res.append(Paragraph([str(len(chunk.strip()))])) return res """) import imp fp, pathname, desc = imp.find_module("testparser") imp.load_module("testparser", fp, pathname, desc) # 2. write a textfile with two paragraphs with open("testparseinput.txt", "w") as fp: fp.write("""This is one paragraph. And another. """) # 3. patch print and call fsmparse d = Devel() printmock = MagicMock() with patch('builtins.print', printmock): # 3.1 fsmparse dynamically imports the module and call the method # with every chunk from the text file # 3.2 fsmparse asserts that the method returned a callable # 3.3 fsmparse calls it with a iterable of text chunks from the # textfile # 3.4 fsmparse recieves a Element structure and prints a # serialized version d.fsmparse("testparser.Testobject.get_parser", "testparseinput.txt") self.assertTrue(printmock.called) # 4. check that the expected thing was printed want = """ <Body> <Paragraph> <str>22</str> </Paragraph> <Paragraph> <str>12</str> </Paragraph> </Body> """.strip() + "\n" printmock.assert_has_calls([call(want)]) finally: util.robust_remove("testparser.py") util.robust_remove("testparser.pyc") util.robust_remove("testparseinput.txt") if os.path.exists("__pycache__") and os.path.isdir("__pycache__"): shutil.rmtree("__pycache__")
def test_fsmparse(self): try: # 1. write a new python module containing a class with a staticmethod with open("testparser.py", "w") as fp: fp.write(""" from six import text_type as str from ferenda.elements import Body, Paragraph class Testobject(object): @staticmethod def get_parser(): return Parser() class Parser(object): def parse(self, source): res = Body() for chunk in source: res.append(Paragraph([str(len(chunk.strip()))])) return res """) import imp fp, pathname, desc = imp.find_module("testparser") imp.load_module("testparser", fp, pathname, desc) # 2. write a textfile with two paragraphs with open("testparseinput.txt", "w") as fp: fp.write("""This is one paragraph. And another. """) # 3. patch print and call fsmparse d = Devel() printmock = MagicMock() with patch('builtins.print', printmock): # 3.1 fsmparse dynamically imports the module and call the method # with every chunk from the text file # 3.2 fsmparse asserts that the method returned a callable # 3.3 fsmparse calls it with a iterable of text chunks from the # textfile # 3.4 fsmparse recieves a Element structure and prints a # serialized version d.fsmparse("testparser.Testobject.get_parser", "testparseinput.txt") self.assertTrue(printmock.called) # 4. check that the expected thing was printed want = """ <Body> <Paragraph> <str>22</str> </Paragraph> <Paragraph> <str>12</str> </Paragraph> </Body> """.strip()+"\n" printmock.assert_has_calls([call(want)]) finally: util.robust_remove("testparser.py") util.robust_remove("testparser.pyc") util.robust_remove("testparseinput.txt") if os.path.exists("__pycache__") and os.path.isdir("__pycache__"): shutil.rmtree("__pycache__")