diff --git a/PKG-INFO b/PKG-INFO index 77a79614a9a217c9fcf906644e6302a3fccafd96..6ab0f2d3c453ffbec97c1559c1ee60819c39f8d9 100644 --- a/PKG-INFO +++ b/PKG-INFO @@ -1,6 +1,6 @@ Metadata-Version: 2.1 Name: swh.model -Version: 0.6.7 +Version: 0.7.0 Summary: Software Heritage data model Home-page: https://forge.softwareheritage.org/diffusion/DMOD/ Author: Software Heritage developers diff --git a/swh.model.egg-info/PKG-INFO b/swh.model.egg-info/PKG-INFO index 77a79614a9a217c9fcf906644e6302a3fccafd96..6ab0f2d3c453ffbec97c1559c1ee60819c39f8d9 100644 --- a/swh.model.egg-info/PKG-INFO +++ b/swh.model.egg-info/PKG-INFO @@ -1,6 +1,6 @@ Metadata-Version: 2.1 Name: swh.model -Version: 0.6.7 +Version: 0.7.0 Summary: Software Heritage data model Home-page: https://forge.softwareheritage.org/diffusion/DMOD/ Author: Software Heritage developers diff --git a/swh.model.egg-info/SOURCES.txt b/swh.model.egg-info/SOURCES.txt index 16bd75b4b07767e4deecfd43e4d26d7aac7aa411..41e33ce05d1343687198b11af955dfbf13c1b07d 100644 --- a/swh.model.egg-info/SOURCES.txt +++ b/swh.model.egg-info/SOURCES.txt @@ -52,7 +52,6 @@ swh/model/identifiers.py swh/model/merkle.py swh/model/model.py swh/model/py.typed -swh/model/test_identifiers.py swh/model/toposort.py swh/model/validators.py swh/model/fields/__init__.py diff --git a/swh/model/cli.py b/swh/model/cli.py index 4c8b7c17c4a4ddd49987d4e00db3938d05f57feb..d14d6f983bae18a88b6797edf9892c663aac99d4 100644 --- a/swh/model/cli.py +++ b/swh/model/cli.py @@ -9,8 +9,9 @@ import sys # WARNING: do not import unnecessary things here to keep cli startup time under # control import click -from swh.core.cli import swh as swh_cli_group +from swh.core.cli import swh as swh_cli_group +from swh.model.identifiers import SWHID CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) @@ -25,17 +26,19 @@ _DULWICH_TYPES = { class SWHIDParamType(click.ParamType): - name = "persistent identifier" + """Click argument that accepts SWHID and return them as + :class:`swh.model.identifiers.SWHID` instances """ + + name = "SWHID" - def convert(self, value, param, ctx): + def convert(self, value, param, ctx) -> SWHID: from swh.model.exceptions import ValidationError from swh.model.identifiers import parse_swhid try: - parse_swhid(value) - return value # return as string, as we need just that + return parse_swhid(value) except ValidationError as e: - self.fail("%s is not a valid SWHID. %s." % (value, e), param, ctx) + self.fail(f'"{value}" is not a valid SWHID: {e}', param, ctx) def swhid_of_file(path): @@ -209,7 +212,7 @@ def identify(obj_type, verify, show_filename, follow_symlinks, objects): if verify: swhid = next(results)[1] - if verify == swhid: + if str(verify) == swhid: click.echo("SWHID match: %s" % swhid) sys.exit(0) else: diff --git a/swh/model/test_identifiers.py b/swh/model/test_identifiers.py deleted file mode 100644 index 412e00a0a6b77d9de1a8f73acb6bc31181f3ad3a..0000000000000000000000000000000000000000 --- a/swh/model/test_identifiers.py +++ /dev/null @@ -1,72 +0,0 @@ -# Copyright (C) 2020 The Software Heritage developers -# See the AUTHORS file at the top-level directory of this distribution -# License: GNU General Public License version 3, or any later version -# See top-level LICENSE file for more information - -from swh.model.identifiers import SWHID - - -def test_swhid_hash(): - object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" - - assert hash(SWHID(object_type="directory", object_id=object_id)) == hash( - SWHID(object_type="directory", object_id=object_id) - ) - - assert hash( - SWHID( - object_type="directory", - object_id=object_id, - metadata={"foo": "bar", "baz": "qux"}, - ) - ) == hash( - SWHID( - object_type="directory", - object_id=object_id, - metadata={"foo": "bar", "baz": "qux"}, - ) - ) - - # Different order of the dictionary, so the underlying order of the tuple in - # ImmutableDict is different. - assert hash( - SWHID( - object_type="directory", - object_id=object_id, - metadata={"foo": "bar", "baz": "qux"}, - ) - ) == hash( - SWHID( - object_type="directory", - object_id=object_id, - metadata={"baz": "qux", "foo": "bar"}, - ) - ) - - -def test_swhid_eq(): - object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" - - assert SWHID(object_type="directory", object_id=object_id) == SWHID( - object_type="directory", object_id=object_id - ) - - assert SWHID( - object_type="directory", - object_id=object_id, - metadata={"foo": "bar", "baz": "qux"}, - ) == SWHID( - object_type="directory", - object_id=object_id, - metadata={"foo": "bar", "baz": "qux"}, - ) - - assert SWHID( - object_type="directory", - object_id=object_id, - metadata={"foo": "bar", "baz": "qux"}, - ) == SWHID( - object_type="directory", - object_id=object_id, - metadata={"baz": "qux", "foo": "bar"}, - ) diff --git a/swh/model/tests/test_identifiers.py b/swh/model/tests/test_identifiers.py index 3741b70af906cff0f4b8c6af25112ccb126356c9..ed34d23dd8fcc5e34c51c9123d54f344ff87ae79 100644 --- a/swh/model/tests/test_identifiers.py +++ b/swh/model/tests/test_identifiers.py @@ -1075,3 +1075,69 @@ TS_DICTS_INVALID_TIMESTAMP = [ def test_normalize_timestamp_dict_invalid_timestamp(dict_input): with pytest.raises(ValueError, match="non-integer timestamp"): normalize_timestamp(dict_input) + + +def test_swhid_hash(): + object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" + + assert hash(SWHID(object_type="directory", object_id=object_id)) == hash( + SWHID(object_type="directory", object_id=object_id) + ) + + assert hash( + SWHID( + object_type="directory", + object_id=object_id, + metadata={"foo": "bar", "baz": "qux"}, + ) + ) == hash( + SWHID( + object_type="directory", + object_id=object_id, + metadata={"foo": "bar", "baz": "qux"}, + ) + ) + + # Different order of the dictionary, so the underlying order of the tuple in + # ImmutableDict is different. + assert hash( + SWHID( + object_type="directory", + object_id=object_id, + metadata={"foo": "bar", "baz": "qux"}, + ) + ) == hash( + SWHID( + object_type="directory", + object_id=object_id, + metadata={"baz": "qux", "foo": "bar"}, + ) + ) + + +def test_swhid_eq(): + object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" + + assert SWHID(object_type="directory", object_id=object_id) == SWHID( + object_type="directory", object_id=object_id + ) + + assert SWHID( + object_type="directory", + object_id=object_id, + metadata={"foo": "bar", "baz": "qux"}, + ) == SWHID( + object_type="directory", + object_id=object_id, + metadata={"foo": "bar", "baz": "qux"}, + ) + + assert SWHID( + object_type="directory", + object_id=object_id, + metadata={"foo": "bar", "baz": "qux"}, + ) == SWHID( + object_type="directory", + object_id=object_id, + metadata={"baz": "qux", "foo": "bar"}, + ) diff --git a/tox.ini b/tox.ini index f0adbaf78eca987fa2f912b951bc2b58a36af673..930a49232a0322752c7d82c40b5b786e1a5c8ce1 100644 --- a/tox.ini +++ b/tox.ini @@ -14,7 +14,7 @@ commands = [testenv:black] skip_install = true deps = - black + black==19.10b0 commands = {envpython} -m black --check swh