Skip to content
Snippets Groups Projects
test_metadata.py 42.4 KiB
Newer Older
David Douard's avatar
David Douard committed
                "type": "SoftwareSourceCode",
                "author": [{"type": "Person", "name": "Ruby Coder"}],
                "name": "example",
                "license": "https://spdx.org/licenses/MIT",
                "codeRepository": "https://rubygems.org/gems/example",
                "email": "rubycoder@example.com",
                "version": "0.1.0",
            },
        )
vlorentz's avatar
vlorentz committed

    def test_gemspec_two_author_fields(self):
        raw_content = b"""
Gem::Specification.new do |s|
  s.authors     = ["Ruby Coder1"]
  s.author      = "Ruby Coder2"
end"""
        result = self.gemspec_mapping.translate(raw_content)
David Douard's avatar
David Douard committed
        self.assertCountEqual(
            result.pop("author"),
            [
                {"type": "Person", "name": "Ruby Coder1"},
                {"type": "Person", "name": "Ruby Coder2"},
            ],
        )
        self.assertEqual(
            result,
David Douard's avatar
David Douard committed
                "@context": "https://doi.org/10.5063/schema/codemeta-2.0",
                "type": "SoftwareSourceCode",
David Douard's avatar
David Douard committed
        )
vlorentz's avatar
vlorentz committed

    def test_gemspec_invalid_author(self):
        raw_content = b"""
Gem::Specification.new do |s|
  s.author      = ["Ruby Coder"]
end"""
        result = self.gemspec_mapping.translate(raw_content)
David Douard's avatar
David Douard committed
        self.assertEqual(
            result,
            {
                "@context": "https://doi.org/10.5063/schema/codemeta-2.0",
                "type": "SoftwareSourceCode",
            },
        )
        raw_content = b"""
vlorentz's avatar
vlorentz committed
Gem::Specification.new do |s|
  s.author      = "Ruby Coder1",
end"""
        result = self.gemspec_mapping.translate(raw_content)
David Douard's avatar
David Douard committed
        self.assertEqual(
            result,
            {
                "@context": "https://doi.org/10.5063/schema/codemeta-2.0",
                "type": "SoftwareSourceCode",
            },
        )
        raw_content = b"""
Gem::Specification.new do |s|
  s.authors     = ["Ruby Coder1", ["Ruby Coder2"]]
end"""
        result = self.gemspec_mapping.translate(raw_content)
David Douard's avatar
David Douard committed
        self.assertEqual(
            result,
            {
                "@context": "https://doi.org/10.5063/schema/codemeta-2.0",
                "type": "SoftwareSourceCode",
                "author": [{"type": "Person", "name": "Ruby Coder1"}],
            },
        )
vlorentz's avatar
vlorentz committed

    def test_gemspec_alternative_header(self):
        raw_content = b"""
require './lib/version'

Gem::Specification.new { |s|
  s.name = 'rb-system-with-aliases'
  s.summary = 'execute system commands with aliases'
}
"""
        result = self.gemspec_mapping.translate(raw_content)
David Douard's avatar
David Douard committed
        self.assertEqual(
            result,
            {
                "@context": "https://doi.org/10.5063/schema/codemeta-2.0",
                "type": "SoftwareSourceCode",
                "name": "rb-system-with-aliases",
                "description": "execute system commands with aliases",
            },
        )
    @settings(suppress_health_check=[HealthCheck.too_slow])
    @given(json_document_strategy(keys=list(NpmMapping.mapping)))
    def test_npm_adversarial(self, doc):
        raw = json.dumps(doc).encode()
        self.npm_mapping.translate(raw)

    @settings(suppress_health_check=[HealthCheck.too_slow])
    @given(json_document_strategy(keys=CODEMETA_TERMS))
    def test_codemeta_adversarial(self, doc):
        raw = json.dumps(doc).encode()
        self.codemeta_mapping.translate(raw)

    @settings(suppress_health_check=[HealthCheck.too_slow])
David Douard's avatar
David Douard committed
    @given(
        xml_document_strategy(
            keys=list(MavenMapping.mapping),
            root="project",
            xmlns="http://maven.apache.org/POM/4.0.0",
        )
    )
    def test_maven_adversarial(self, doc):
    @settings(suppress_health_check=[HealthCheck.too_slow])
David Douard's avatar
David Douard committed
    @given(
        strategies.dictionaries(
            # keys
            strategies.one_of(
                strategies.text(), *map(strategies.just, GemspecMapping.mapping)
            ),
            # values
            strategies.recursive(
                strategies.characters(),
                lambda children: strategies.lists(children, min_size=1),
            ),
David Douard's avatar
David Douard committed
    )
    def test_gemspec_adversarial(self, doc):
David Douard's avatar
David Douard committed
        parts = [b"Gem::Specification.new do |s|\n"]
        for (k, v) in doc.items():
David Douard's avatar
David Douard committed
            parts.append("  s.{} = {}\n".format(k, repr(v)).encode())
        parts.append(b"end\n")
        self.gemspec_mapping.translate(b"".join(parts))
    def test_revision_metadata_indexer(self):
David Douard's avatar
David Douard committed
        metadata_indexer = RevisionMetadataIndexer(config=REVISION_METADATA_CONFIG)
        fill_obj_storage(metadata_indexer.objstorage)
        fill_storage(metadata_indexer.storage)

        tool = metadata_indexer.idx_storage.indexer_configuration_get(
            {f"tool_{k}": v for (k, v) in TRANSLATOR_TOOL.items()}
David Douard's avatar
David Douard committed
        )
        rev = REVISION
        assert rev.directory == DIRECTORY2.id
David Douard's avatar
David Douard committed
        metadata_indexer.idx_storage.content_metadata_add(
            [
                ContentMetadataRow(
                    id=DIRECTORY2.entries[0].target,
                    indexer_configuration_id=tool["id"],
                    metadata=YARN_PARSER_METADATA,
                )
David Douard's avatar
David Douard committed
            ]
        )
        metadata_indexer.run([rev.id])
            metadata_indexer.idx_storage.revision_intrinsic_metadata_get([REVISION.id])
David Douard's avatar
David Douard committed
        )
David Douard's avatar
David Douard committed
        expected_results = [
            RevisionIntrinsicMetadataRow(
                id=rev.id,
                tool=TRANSLATOR_TOOL,
                metadata=YARN_PARSER_METADATA,
                mappings=["npm"],
            )
David Douard's avatar
David Douard committed
        ]
        self.assertEqual(results, expected_results)

    def test_revision_metadata_indexer_single_root_dir(self):
David Douard's avatar
David Douard committed
        metadata_indexer = RevisionMetadataIndexer(config=REVISION_METADATA_CONFIG)
        fill_obj_storage(metadata_indexer.objstorage)
        fill_storage(metadata_indexer.storage)

        # Add a parent directory, that is the only directory at the root
        # of the revision
        rev = REVISION
        assert rev.directory == DIRECTORY2.id

        directory = Directory(
            entries=(
                DirectoryEntry(
                    name=b"foobar-1.0.0", type="dir", target=rev.directory, perms=16384,
                ),
            ),
David Douard's avatar
David Douard committed
        )
        assert directory.id is not None
        metadata_indexer.storage.directory_add([directory])

        new_rev_dict = {**rev.to_dict(), "directory": directory.id}
        new_rev_dict.pop("id")
        new_rev = Revision.from_dict(new_rev_dict)
        metadata_indexer.storage.revision_add([new_rev])

        tool = metadata_indexer.idx_storage.indexer_configuration_get(
            {f"tool_{k}": v for (k, v) in TRANSLATOR_TOOL.items()}
David Douard's avatar
David Douard committed
        )
        assert tool is not None

David Douard's avatar
David Douard committed
        metadata_indexer.idx_storage.content_metadata_add(
            [
                ContentMetadataRow(
                    id=DIRECTORY2.entries[0].target,
                    indexer_configuration_id=tool["id"],
                    metadata=YARN_PARSER_METADATA,
                )
David Douard's avatar
David Douard committed
            ]
        )
        metadata_indexer.run([new_rev.id])
            metadata_indexer.idx_storage.revision_intrinsic_metadata_get([new_rev.id])
David Douard's avatar
David Douard committed
        )
David Douard's avatar
David Douard committed
        expected_results = [
            RevisionIntrinsicMetadataRow(
                id=new_rev.id,
                tool=TRANSLATOR_TOOL,
                metadata=YARN_PARSER_METADATA,
                mappings=["npm"],
            )
David Douard's avatar
David Douard committed
        ]
vlorentz's avatar
vlorentz committed

        for result in results:
vlorentz's avatar
vlorentz committed

        self.assertEqual(results, expected_results)