Exemplo n.º 1
0
    def test_render(self,mock_graph):
        @render
        def testfunc(repo,doc):
            pass

        mockdoc = Mock()
        mockrepo = Mock()
        mockrepo.store.parsed_path.return_value = "parsed_path.xhtml"
        with open("parsed_path.xhtml", "w") as fp:
            fp.write("""<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:dct="http://purl.org/dc/terms/">
  <head about="http://example.org/doc">
     <title property="dct:title">Document title</title>
  </head>
  <body>
     <h1>Hello!</h1>
  </body>
</html>""")

        mockrepo.store.distilled_path.return_value = "distilled_path.xhtml"
        mockrepo.get_globals.return_value = {'symbol table':'fake'}
        mockdoc.meta = MagicMock() # need Magicmock which supports magic funcs like __iter__
        bodypart = MagicMock()
        bodypart.meta  = MagicMock()
        mockdoc.body = [bodypart]
        mockdoc.meta.__iter__.return_value = []
        mockdoc.uri = "http://example.org/doc"
        with patch('ferenda.util.ensure_dir', return_value=True):
            testfunc(mockrepo, mockdoc)
        
        # 1 ensure that DocumentRepository.render_xhtml is called with
        # four arguments
        mockrepo.render_xhtml.assert_called_with(mockdoc, "parsed_path.xhtml")

        # 2 ensure that DocumentRepository.create_external_resources
        # is called with 1 argument
        mockrepo.create_external_resources.assert_called_with(mockdoc)
        
        # 3 ensure that a Graph object is created, its parse and
        # serialize methods called

        # FIXME: Why doesn't the patching work?!
        # self.assertTrue(mock_graph().parse.called)
        # self.assertTrue(mock_graph().serialize.called)
        
        # (4. ensure that a warning gets printed if doc.meta and
        # distilled_graph do not agree)
        mock_graph().__iter__.return_value = ['a','b']
        mockdoc.meta.__iter__.return_value = ['a','b','c']
        mockdoc.meta.serialize.return_value = b"<c>"

        with patch('ferenda.util.ensure_dir', return_value=True):
            testfunc(mockrepo, mockdoc)
        self.assertTrue(mockrepo.log.warning.called)
        os.remove("parsed_path.xhtml")
        os.remove("distilled_path.xhtml")
Exemplo n.º 2
0
    def test_render(self, mock_graph):
        @render
        def testfunc(repo, doc):
            pass

        mockdoc = Mock()
        mockrepo = MagicMock()
        mockrepo.store.parsed_path.return_value = "parsed_path.xhtml"
        with open("parsed_path.xhtml", "w") as fp:
            fp.write("""<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:dcterms="http://purl.org/dc/terms/">
  <head about="http://example.org/doc">
     <title property="dcterms:title">Document title</title>
  </head>
  <body>
     <h1>Hello!</h1>
  </body>
</html>""")
        with open("entry_path.json", "w") as fp:
            fp.write("""{
  "id": "https://lagen.nu/concept/St\\u00e5ende_anbud", 
  "title": "St\\u00e5ende anbud", 
}""")
        mockrepo.store.distilled_path.return_value = "distilled_path.xhtml"
        mockrepo.get_globals.return_value = {'symbol table': 'fake'}
        mockrepo.required_predicates = []
        mockrepo.config = MagicMock()
        mockdoc.meta = MagicMock(
        )  # need Magicmock which supports magic funcs like __iter__
        bodypart = MagicMock()
        bodypart.meta = MagicMock()
        mockdoc.body = [bodypart]
        mockdoc.meta.__iter__.return_value = []
        mockdoc.uri = "http://example.org/doc"
        mockrepo.store.documententry_path.return_value = None
        with patch('ferenda.util.ensure_dir', return_value=True):
            testfunc(mockrepo, mockdoc)

        # 1 ensure that DocumentRepository.render_xhtml is called with
        # four arguments
        mockrepo.render_xhtml.assert_called_with(mockdoc, "parsed_path.xhtml")

        # 2 ensure that DocumentRepository.create_external_resources
        # is called with 1 argument
        mockrepo.create_external_resources.assert_called_with(mockdoc)

        # 3 ensure that a Graph object is created, its parse and
        # serialize methods called

        # FIXME: Why doesn't the patching work?!
        # self.assertTrue(mock_graph().parse.called)
        # self.assertTrue(mock_graph().serialize.called)

        # (4. ensure that a warning gets printed if doc.meta and
        # distilled_graph do not agree)
        mock_graph().__iter__.return_value = ['a', 'b']
        mockdoc.meta.__iter__.return_value = ['a', 'b', 'c']
        mockdoc.meta.serialize.return_value = b"<c>"

        with patch('ferenda.util.ensure_dir', return_value=True):
            testfunc(mockrepo, mockdoc)
        self.assertTrue(mockrepo.log.warning.called)
        os.remove("parsed_path.xhtml")
        os.remove("distilled_path.xhtml")
        os.remove("entry_path.json")