Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_identifiers.py
# Copyright (C) 2015-2018 The Software Heritage developers | # Copyright (C) 2015-2018 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import binascii | import binascii | ||||
import datetime | import datetime | ||||
from typing import Dict | from typing import Dict | ||||
import unittest | import unittest | ||||
import pytest | import pytest | ||||
from swh.model import hashutil, identifiers | from swh.model import hashutil, identifiers | ||||
from swh.model.exceptions import ValidationError | from swh.model.exceptions import ValidationError | ||||
from swh.model.hashutil import hash_to_bytes as _x | from swh.model.hashutil import hash_to_bytes as _x | ||||
from swh.model.identifiers import ( | from swh.model.identifiers import normalize_timestamp | ||||
CONTENT, | from swh.model.swhid import SWHID, SWHIDObjectType | ||||
DIRECTORY, | |||||
RELEASE, | |||||
REVISION, | |||||
SNAPSHOT, | |||||
SWHID, | |||||
normalize_timestamp, | |||||
) | |||||
def remove_id(d: Dict) -> Dict: | def remove_id(d: Dict) -> Dict: | ||||
"""Returns a (shallow) copy of a dict with the 'id' key removed.""" | """Returns a (shallow) copy of a dict with the 'id' key removed.""" | ||||
d = d.copy() | d = d.copy() | ||||
if "id" in d: | if "id" in d: | ||||
del d["id"] | del d["id"] | ||||
return d | return d | ||||
▲ Show 20 Lines • Show All 859 Lines • ▼ Show 20 Lines | def test_swhid(self): | ||||
_snapshot = {"id": _snapshot_id} | _snapshot = {"id": _snapshot_id} | ||||
_release = {"id": _release_id} | _release = {"id": _release_id} | ||||
_revision = {"id": _revision_id} | _revision = {"id": _revision_id} | ||||
_directory = {"id": _directory_id} | _directory = {"id": _directory_id} | ||||
_content = {"sha1_git": _content_id} | _content = {"sha1_git": _content_id} | ||||
for full_type, _hash, expected_swhid, version, _meta in [ | for full_type, _hash, expected_swhid, version, _meta in [ | ||||
( | ( | ||||
SNAPSHOT, | SWHIDObjectType.SNAPSHOT, | ||||
_snapshot_id, | _snapshot_id, | ||||
"swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453", | "swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453", | ||||
None, | None, | ||||
{}, | {}, | ||||
), | ), | ||||
( | ( | ||||
RELEASE, | SWHIDObjectType.RELEASE, | ||||
_release_id, | _release_id, | ||||
"swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f", | "swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f", | ||||
1, | 1, | ||||
{}, | {}, | ||||
), | ), | ||||
( | ( | ||||
REVISION, | SWHIDObjectType.REVISION, | ||||
_revision_id, | _revision_id, | ||||
"swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d", | "swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d", | ||||
None, | None, | ||||
{}, | {}, | ||||
), | ), | ||||
( | ( | ||||
DIRECTORY, | SWHIDObjectType.DIRECTORY, | ||||
_directory_id, | _directory_id, | ||||
"swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505", | "swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505", | ||||
None, | None, | ||||
{}, | {}, | ||||
), | ), | ||||
( | ( | ||||
CONTENT, | SWHIDObjectType.CONTENT, | ||||
_content_id, | _content_id, | ||||
"swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | ||||
1, | 1, | ||||
{}, | {}, | ||||
), | ), | ||||
( | ( | ||||
SNAPSHOT, | SWHIDObjectType.SNAPSHOT, | ||||
_snapshot, | _snapshot, | ||||
"swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453", | "swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453", | ||||
None, | None, | ||||
{}, | {}, | ||||
), | ), | ||||
( | ( | ||||
RELEASE, | SWHIDObjectType.RELEASE, | ||||
_release, | _release, | ||||
"swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f", | "swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f", | ||||
1, | 1, | ||||
{}, | {}, | ||||
), | ), | ||||
( | ( | ||||
REVISION, | SWHIDObjectType.REVISION, | ||||
_revision, | _revision, | ||||
"swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d", | "swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d", | ||||
None, | None, | ||||
{}, | {}, | ||||
), | ), | ||||
( | ( | ||||
DIRECTORY, | SWHIDObjectType.DIRECTORY, | ||||
_directory, | _directory, | ||||
"swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505", | "swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505", | ||||
None, | None, | ||||
{}, | {}, | ||||
), | ), | ||||
( | ( | ||||
CONTENT, | SWHIDObjectType.CONTENT, | ||||
_content, | _content, | ||||
"swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | ||||
1, | 1, | ||||
{}, | {}, | ||||
), | ), | ||||
( | ( | ||||
CONTENT, | SWHIDObjectType.CONTENT, | ||||
_content, | _content, | ||||
"swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2;origin=1", | "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2;origin=1", | ||||
1, | 1, | ||||
{"origin": "1"}, | {"origin": "1"}, | ||||
), | ), | ||||
]: | ]: | ||||
if version: | if version: | ||||
actual_value = identifiers.swhid( | actual_value = identifiers.swhid( | ||||
full_type, _hash, version, metadata=_meta | full_type, _hash, version, metadata=_meta | ||||
) | ) | ||||
else: | else: | ||||
actual_value = identifiers.swhid(full_type, _hash, metadata=_meta) | actual_value = identifiers.swhid(full_type, _hash, metadata=_meta) | ||||
self.assertEqual(actual_value, expected_swhid) | self.assertEqual(actual_value, expected_swhid) | ||||
def test_swhid_wrong_input(self): | def test_swhid_wrong_input(self): | ||||
_snapshot_id = "notahash4bc0bf3d81436bf980b46e98bd338453" | _snapshot_id = "notahash4bc0bf3d81436bf980b46e98bd338453" | ||||
_snapshot = {"id": _snapshot_id} | _snapshot = {"id": _snapshot_id} | ||||
for _type, _hash in [ | for _type, _hash in [ | ||||
(SNAPSHOT, _snapshot_id), | (SWHIDObjectType.SNAPSHOT, _snapshot_id), | ||||
(SNAPSHOT, _snapshot), | (SWHIDObjectType.SNAPSHOT, _snapshot), | ||||
("lines", "42"), | ("snapshot", "42"), | ||||
("snp", "42"), | |||||
]: | ]: | ||||
with self.assertRaises(ValidationError): | with self.assertRaises(ValidationError): | ||||
identifiers.swhid(_type, _hash) | identifiers.swhid(_type, _hash) | ||||
def test_parse_swhid(self): | def test_parse_swhid(self): | ||||
for swhid, _type, _version, _hash in [ | for swhid, _type, _version, _hash in [ | ||||
( | ( | ||||
"swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2", | ||||
CONTENT, | SWHIDObjectType.CONTENT, | ||||
1, | 1, | ||||
"94a9ed024d3859793618152ea559a168bbcbb5e2", | "94a9ed024d3859793618152ea559a168bbcbb5e2", | ||||
), | ), | ||||
( | ( | ||||
"swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505", | "swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505", | ||||
DIRECTORY, | SWHIDObjectType.DIRECTORY, | ||||
1, | 1, | ||||
"d198bc9d7a6bcf6db04f476d29314f157507d505", | "d198bc9d7a6bcf6db04f476d29314f157507d505", | ||||
), | ), | ||||
( | ( | ||||
"swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d", | "swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d", | ||||
REVISION, | SWHIDObjectType.REVISION, | ||||
1, | 1, | ||||
"309cf2674ee7a0749978cf8265ab91a60aea0f7d", | "309cf2674ee7a0749978cf8265ab91a60aea0f7d", | ||||
), | ), | ||||
( | ( | ||||
"swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f", | "swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f", | ||||
RELEASE, | SWHIDObjectType.RELEASE, | ||||
1, | 1, | ||||
"22ece559cc7cc2364edc5e5593d63ae8bd229f9f", | "22ece559cc7cc2364edc5e5593d63ae8bd229f9f", | ||||
), | ), | ||||
( | ( | ||||
"swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453", | "swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453", | ||||
SNAPSHOT, | SWHIDObjectType.SNAPSHOT, | ||||
1, | 1, | ||||
"c7c108084bc0bf3d81436bf980b46e98bd338453", | "c7c108084bc0bf3d81436bf980b46e98bd338453", | ||||
), | ), | ||||
]: | ]: | ||||
expected_result = SWHID( | expected_result = SWHID( | ||||
namespace="swh", | namespace="swh", | ||||
scheme_version=_version, | scheme_version=_version, | ||||
object_type=_type, | object_type=_type, | ||||
object_id=_hash, | object_id=_hash, | ||||
metadata={}, | metadata={}, | ||||
) | ) | ||||
actual_result = identifiers.parse_swhid(swhid) | actual_result = identifiers.parse_swhid(swhid) | ||||
self.assertEqual(actual_result, expected_result) | self.assertEqual(actual_result, expected_result) | ||||
for swhid, _type, _version, _hash, _metadata in [ | for swhid, _type, _version, _hash, _metadata in [ | ||||
( | ( | ||||
"swh:1:cnt:9c95815d9e9d91b8dae8e05d8bbc696fe19f796b;lines=1-18;origin=https://github.com/python/cpython", # noqa | "swh:1:cnt:9c95815d9e9d91b8dae8e05d8bbc696fe19f796b;lines=1-18;origin=https://github.com/python/cpython", # noqa | ||||
CONTENT, | SWHIDObjectType.CONTENT, | ||||
1, | 1, | ||||
"9c95815d9e9d91b8dae8e05d8bbc696fe19f796b", | "9c95815d9e9d91b8dae8e05d8bbc696fe19f796b", | ||||
{"lines": "1-18", "origin": "https://github.com/python/cpython"}, | {"lines": "1-18", "origin": "https://github.com/python/cpython"}, | ||||
), | ), | ||||
( | ( | ||||
"swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;origin=deb://Debian/packages/linuxdoc-tools", # noqa | "swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;origin=deb://Debian/packages/linuxdoc-tools", # noqa | ||||
DIRECTORY, | SWHIDObjectType.DIRECTORY, | ||||
1, | 1, | ||||
"0b6959356d30f1a4e9b7f6bca59b9a336464c03d", | "0b6959356d30f1a4e9b7f6bca59b9a336464c03d", | ||||
{"origin": "deb://Debian/packages/linuxdoc-tools"}, | {"origin": "deb://Debian/packages/linuxdoc-tools"}, | ||||
), | ), | ||||
]: | ]: | ||||
expected_result = SWHID( | expected_result = SWHID( | ||||
namespace="swh", | namespace="swh", | ||||
scheme_version=_version, | scheme_version=_version, | ||||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | |||||
def test_parse_swhid_parsing_error(invalid_swhid): | def test_parse_swhid_parsing_error(invalid_swhid): | ||||
with pytest.raises(ValidationError): | with pytest.raises(ValidationError): | ||||
identifiers.parse_swhid(invalid_swhid) | identifiers.parse_swhid(invalid_swhid) | ||||
@pytest.mark.parametrize( | @pytest.mark.parametrize( | ||||
"ns,version,type,id", | "ns,version,type,id", | ||||
[ | [ | ||||
("foo", 1, CONTENT, "abc8bc9d7a6bcf6db04f476d29314f157507d505",), | ( | ||||
("swh", 2, DIRECTORY, "def8bc9d7a6bcf6db04f476d29314f157507d505",), | "foo", | ||||
("swh", 1, "foo", "fed8bc9d7a6bcf6db04f476d29314f157507d505",), | 1, | ||||
("swh", 1, SNAPSHOT, "gh6959356d30f1a4e9b7f6bca59b9a336464c03d",), | SWHIDObjectType.CONTENT, | ||||
"abc8bc9d7a6bcf6db04f476d29314f157507d505", | |||||
), | |||||
( | |||||
"swh", | |||||
2, | |||||
SWHIDObjectType.DIRECTORY, | |||||
"def8bc9d7a6bcf6db04f476d29314f157507d505", | |||||
), | |||||
( | |||||
"swh", | |||||
1, | |||||
SWHIDObjectType.SNAPSHOT, | |||||
"gh6959356d30f1a4e9b7f6bca59b9a336464c03d", | |||||
), | |||||
], | ], | ||||
) | ) | ||||
def test_SWHID_class_validation_error(ns, version, type, id): | def test_SWHID_class_validation_error(ns, version, type, id): | ||||
with pytest.raises(ValidationError): | with pytest.raises(ValidationError): | ||||
SWHID( | SWHID( | ||||
namespace=ns, scheme_version=version, object_type=type, object_id=id, | namespace=ns, scheme_version=version, object_type=type, object_id=id, | ||||
) | ) | ||||
def test_swhid_hash(): | def test_swhid_hash(): | ||||
object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | ||||
assert hash(SWHID(object_type="directory", object_id=object_id)) == hash( | assert hash( | ||||
SWHID(object_type="directory", object_id=object_id) | SWHID(object_type=SWHIDObjectType.DIRECTORY, object_id=object_id) | ||||
) | ) == hash(SWHID(object_type=SWHIDObjectType.DIRECTORY, object_id=object_id)) | ||||
assert hash( | assert hash( | ||||
SWHID(object_type="directory", object_id=object_id, metadata=dummy_qualifiers,) | SWHID( | ||||
object_type=SWHIDObjectType.DIRECTORY, | |||||
object_id=object_id, | |||||
metadata=dummy_qualifiers, | |||||
) | |||||
) == hash( | ) == hash( | ||||
SWHID(object_type="directory", object_id=object_id, metadata=dummy_qualifiers,) | SWHID( | ||||
object_type=SWHIDObjectType.DIRECTORY, | |||||
object_id=object_id, | |||||
metadata=dummy_qualifiers, | |||||
) | |||||
) | ) | ||||
# Different order of the dictionary, so the underlying order of the tuple in | # Different order of the dictionary, so the underlying order of the tuple in | ||||
# ImmutableDict is different. | # ImmutableDict is different. | ||||
assert hash( | assert hash( | ||||
SWHID( | SWHID( | ||||
object_type="directory", | object_type=SWHIDObjectType.DIRECTORY, | ||||
object_id=object_id, | object_id=object_id, | ||||
metadata={"origin": "https://example.com", "lines": "42"}, | metadata={"origin": "https://example.com", "lines": "42"}, | ||||
) | ) | ||||
) == hash( | ) == hash( | ||||
SWHID( | SWHID( | ||||
object_type="directory", | object_type=SWHIDObjectType.DIRECTORY, | ||||
object_id=object_id, | object_id=object_id, | ||||
metadata={"lines": "42", "origin": "https://example.com"}, | metadata={"lines": "42", "origin": "https://example.com"}, | ||||
) | ) | ||||
) | ) | ||||
def test_swhid_eq(): | def test_swhid_eq(): | ||||
object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | object_id = "94a9ed024d3859793618152ea559a168bbcbb5e2" | ||||
assert SWHID(object_type="directory", object_id=object_id) == SWHID( | assert SWHID(object_type=SWHIDObjectType.DIRECTORY, object_id=object_id) == SWHID( | ||||
object_type="directory", object_id=object_id | object_type=SWHIDObjectType.DIRECTORY.DIRECTORY, object_id=object_id | ||||
) | ) | ||||
assert SWHID( | assert SWHID( | ||||
object_type="directory", object_id=object_id, metadata=dummy_qualifiers, | object_type=SWHIDObjectType.DIRECTORY, | ||||
) == SWHID(object_type="directory", object_id=object_id, metadata=dummy_qualifiers,) | object_id=object_id, | ||||
metadata=dummy_qualifiers, | |||||
) == SWHID( | |||||
object_type=SWHIDObjectType.DIRECTORY, | |||||
object_id=object_id, | |||||
metadata=dummy_qualifiers, | |||||
) | |||||
assert SWHID( | assert SWHID( | ||||
object_type="directory", object_id=object_id, metadata=dummy_qualifiers, | object_type=SWHIDObjectType.DIRECTORY, | ||||
) == SWHID(object_type="directory", object_id=object_id, metadata=dummy_qualifiers,) | object_id=object_id, | ||||
metadata=dummy_qualifiers, | |||||
) == SWHID( | |||||
object_type=SWHIDObjectType.DIRECTORY, | |||||
object_id=object_id, | |||||
metadata=dummy_qualifiers, | |||||
) |