Beispiel #1
0
    def test_syntax_analyzer_raises_missing_property_error_correctly(
        self,
        _,
        rwpm_manifest_content,
        expected_class_with_missing_property,
        expected_missing_property,
    ):
        # Arrange
        syntax_analyzer = RWPMSyntaxAnalyzer()
        input_steam = six.StringIO(rwpm_manifest_content)
        manifest_json = DocumentParser.get_manifest_json(input_steam)

        # Act
        with self.assertRaises(MissingPropertyError) as assert_raises_context:
            syntax_analyzer.analyze(manifest_json)

        # Assert
        self.assertEqual(
            expected_class_with_missing_property,
            assert_raises_context.exception.cls,
        )
        self.assertEqual(
            expected_missing_property,
            assert_raises_context.exception.object_property.key,
        )
    def test_syntax_analyzer_raises_missing_property_error_correctly(
        self,
        _,
        rwpm_manifest_content,
        expected_class_with_missing_property,
        expected_missing_property,
    ):
        # Arrange
        syntax_analyzer = RWPMSyntaxAnalyzer()
        input_steam = six.StringIO(rwpm_manifest_content)
        manifest_json = ManifestParser.get_manifest_json(input_steam)

        # Act
        syntax_analyzer.analyze(manifest_json)

        # Assert
        error = first_or_default(syntax_analyzer.context.errors)

        self.assertIsNotNone(error)
        self.assertEqual(
            expected_class_with_missing_property,
            error.node.__class__,
        )
        self.assertEqual(
            expected_missing_property,
            error.node_property.key,
        )
Beispiel #3
0
    def test_syntax_analyzer_correctly_parses_contributor_metadata_as_contributor_objects(
            self, _, rwpm_manifest_content, expected_authors):
        # Arrange
        syntax_analyzer = RWPMSyntaxAnalyzer()
        input_steam = six.StringIO(rwpm_manifest_content)
        manifest_json = DocumentParser.get_manifest_json(input_steam)

        # Act
        manifest = syntax_analyzer.analyze(manifest_json)

        # Assert
        self.assertEqual(expected_authors, manifest.metadata.authors)
    def test_syntax_analyzer_returns_ast(self):
        # Arrange
        syntax_analyzer = RWPMSyntaxAnalyzer()
        input_steam = six.StringIO(RWPM_MANIFEST)
        manifest_json = ManifestParser.get_manifest_json(input_steam)

        # Act
        manifest = syntax_analyzer.analyze(manifest_json)

        # Assert
        self.assertIsInstance(manifest, RWPMManifest)

        self.assertIsInstance(manifest.context, list)
        self.assertEqual(1, len(manifest.context))
        [context] = manifest.context
        self.assertEqual(context,
                         "https://readium.org/webpub-manifest/context.jsonld")

        self.assertIsInstance(manifest.metadata, Metadata)
        self.assertEqual("http://schema.org/Book", manifest.metadata.type)
        self.assertEqual("Moby-Dick", manifest.metadata.title)
        self.assertEqual(
            [Contributor(name="Herman Melville", roles=[], links=LinkList())],
            manifest.metadata.authors,
        )
        self.assertEqual("urn:isbn:978031600000X",
                         manifest.metadata.identifier)
        self.assertEqual(["en"], manifest.metadata.languages)
        self.assertEqual(
            datetime.datetime(2015, 9, 29, 17, 0, 0, tzinfo=tzutc()),
            manifest.metadata.modified,
        )

        self.assertIsInstance(manifest.links, list)
        self.assertEqual(1, len(manifest.links))
        [link] = manifest.links

        self.assertIsInstance(link.rels, list)
        self.assertEqual(1, len(link.rels))
        self.assertEqual(RWPMLinkRelationsRegistry.SELF.key, link.rels[0])
        self.assertEqual("https://example.com/manifest.json", link.href)
        self.assertEqual(RWPMMediaTypesRegistry.MANIFEST.key, link.type)

        self.assertIsInstance(manifest.reading_order, CompactCollection)
        self.assertIsInstance(manifest.reading_order.links, list)
        self.assertEqual(1, len(manifest.reading_order.links))
        [reading_order_link] = manifest.reading_order.links
        self.assertEqual("https://example.com/c001.html",
                         reading_order_link.href)
        self.assertEqual(RWPMMediaTypesRegistry.HTML.key,
                         reading_order_link.type)
        self.assertEqual("Chapter 1", reading_order_link.title)
Beispiel #5
0
    def test_syntax_analyzer_raises_value_parsing_error_when_property_has_incorrect_value(
            self, _, rwpm_manifest_content, expected_error_message):
        # Arrange
        syntax_analyzer = RWPMSyntaxAnalyzer()
        input_steam = six.StringIO(rwpm_manifest_content)
        manifest_json = DocumentParser.get_manifest_json(input_steam)

        # Act
        with self.assertRaises(ValueParsingError) as assert_raises_context:
            syntax_analyzer.analyze(manifest_json)

        # Assert
        self.assertEqual(
            expected_error_message,
            six.text_type(assert_raises_context.exception).strip("u"),
        )
    def test_syntax_analyzer_raises_value_parsing_error_when_property_has_incorrect_value(
            self, _, rwpm_manifest_content, expected_error_message):
        # Arrange
        syntax_analyzer = RWPMSyntaxAnalyzer()
        input_steam = six.StringIO(rwpm_manifest_content)
        manifest_json = ManifestParser.get_manifest_json(input_steam)

        # Act
        syntax_analyzer.analyze(manifest_json)

        # Assert
        error = first_or_default(syntax_analyzer.context.errors)

        self.assertIsNotNone(error)
        self.assertEqual(
            expected_error_message,
            six.text_type(error).strip("u"),
        )
    def create(self):
        """Create a new RWPMParser.

        :return: RWPM parser instance
        :rtype: Parser
        """
        media_types_registry = RWPMMediaTypesRegistry()
        link_relations_registry = RWPMLinkRelationsRegistry()
        collection_roles_registry = RWPMCollectionRolesRegistry()
        syntax_analyzer = RWPMSyntaxAnalyzer()
        semantic_analyzer = RWPMSemanticAnalyzer(media_types_registry,
                                                 link_relations_registry,
                                                 collection_roles_registry)
        parser = DocumentParser(syntax_analyzer, semantic_analyzer)

        return parser