Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_identifiers.py
Show First 20 Lines • Show All 801 Lines • ▼ Show 20 Lines | def setUp(self): | ||||
"release": CoreSWHID.from_string("swh:1:rel:" + "01" * 20), | "release": CoreSWHID.from_string("swh:1:rel:" + "01" * 20), | ||||
"revision": CoreSWHID.from_string("swh:1:rev:" + "02" * 20), | "revision": CoreSWHID.from_string("swh:1:rev:" + "02" * 20), | ||||
"path": b"/abc/def", | "path": b"/abc/def", | ||||
"directory": CoreSWHID.from_string("swh:1:dir:" + "03" * 20), | "directory": CoreSWHID.from_string("swh:1:dir:" + "03" * 20), | ||||
} | } | ||||
def test_minimal(self): | def test_minimal(self): | ||||
manifest = ( | manifest = ( | ||||
b"raw_extrinsic_metadata 225\0" | b"raw_extrinsic_metadata 210\0" | ||||
b"target swh:1:cnt:568aaf43d83b2c3df8067f3bedbb97d83260be6d\n" | b"target swh:1:cnt:568aaf43d83b2c3df8067f3bedbb97d83260be6d\n" | ||||
b"discovery_date 2021-01-25T11:27:51+00:00\n" | b"discovery_date 1611574071\n" | ||||
b"authority forge https://forge.softwareheritage.org/\n" | b"authority forge https://forge.softwareheritage.org/\n" | ||||
b"fetcher swh-phabricator-metadata-fetcher 0.0.1\n" | b"fetcher swh-phabricator-metadata-fetcher 0.0.1\n" | ||||
b"format json\n" | b"format json\n" | ||||
b"\n" | b"\n" | ||||
b'{"foo": "bar"}' | b'{"foo": "bar"}' | ||||
) | ) | ||||
self.assertEqual( | self.assertEqual( | ||||
identifiers.raw_extrinsic_metadata_identifier(self.minimal), | identifiers.raw_extrinsic_metadata_identifier(self.minimal), | ||||
hashlib.sha1(manifest).hexdigest(), | hashlib.sha1(manifest).hexdigest(), | ||||
) | ) | ||||
self.assertEqual( | self.assertEqual( | ||||
identifiers.raw_extrinsic_metadata_identifier(self.minimal), | identifiers.raw_extrinsic_metadata_identifier(self.minimal), | ||||
"df16b5ea35b12f530fb7ecd0eb10b87a8b1fc3d2", | "5c13f20ba336e44549baf3d7b9305b027ec9f43d", | ||||
) | ) | ||||
def test_maximal(self): | def test_maximal(self): | ||||
manifest = ( | manifest = ( | ||||
b"raw_extrinsic_metadata 548\0" | b"raw_extrinsic_metadata 533\0" | ||||
b"target swh:1:cnt:568aaf43d83b2c3df8067f3bedbb97d83260be6d\n" | b"target swh:1:cnt:568aaf43d83b2c3df8067f3bedbb97d83260be6d\n" | ||||
b"discovery_date 2021-01-25T11:27:51+00:00\n" | b"discovery_date 1611574071\n" | ||||
b"authority forge https://forge.softwareheritage.org/\n" | b"authority forge https://forge.softwareheritage.org/\n" | ||||
b"fetcher swh-phabricator-metadata-fetcher 0.0.1\n" | b"fetcher swh-phabricator-metadata-fetcher 0.0.1\n" | ||||
b"format json\n" | b"format json\n" | ||||
b"origin https://forge.softwareheritage.org/source/swh-model/\n" | b"origin https://forge.softwareheritage.org/source/swh-model/\n" | ||||
b"visit 42\n" | b"visit 42\n" | ||||
b"snapshot swh:1:snp:0000000000000000000000000000000000000000\n" | b"snapshot swh:1:snp:0000000000000000000000000000000000000000\n" | ||||
b"release swh:1:rel:0101010101010101010101010101010101010101\n" | b"release swh:1:rel:0101010101010101010101010101010101010101\n" | ||||
b"revision swh:1:rev:0202020202020202020202020202020202020202\n" | b"revision swh:1:rev:0202020202020202020202020202020202020202\n" | ||||
b"path /abc/def\n" | b"path /abc/def\n" | ||||
b"directory swh:1:dir:0303030303030303030303030303030303030303\n" | b"directory swh:1:dir:0303030303030303030303030303030303030303\n" | ||||
b"\n" | b"\n" | ||||
b'{"foo": "bar"}' | b'{"foo": "bar"}' | ||||
) | ) | ||||
self.assertEqual( | self.assertEqual( | ||||
identifiers.raw_extrinsic_metadata_identifier(self.maximal), | identifiers.raw_extrinsic_metadata_identifier(self.maximal), | ||||
hashlib.sha1(manifest).hexdigest(), | hashlib.sha1(manifest).hexdigest(), | ||||
) | ) | ||||
self.assertEqual( | self.assertEqual( | ||||
identifiers.raw_extrinsic_metadata_identifier(self.maximal), | identifiers.raw_extrinsic_metadata_identifier(self.maximal), | ||||
"55563d91a3f9cb41aa36c60c2b518433bf318ae4", | "f96966e1093d15236a31fde07e47d5b1c9428049", | ||||
) | ) | ||||
def test_nonascii_path(self): | def test_nonascii_path(self): | ||||
metadata = { | metadata = { | ||||
**self.minimal, | **self.minimal, | ||||
"path": b"/ab\nc/d\xf0\x9f\xa4\xb7e\x00f", | "path": b"/ab\nc/d\xf0\x9f\xa4\xb7e\x00f", | ||||
} | } | ||||
manifest = ( | manifest = ( | ||||
b"raw_extrinsic_metadata 246\0" | b"raw_extrinsic_metadata 231\0" | ||||
b"target swh:1:cnt:568aaf43d83b2c3df8067f3bedbb97d83260be6d\n" | b"target swh:1:cnt:568aaf43d83b2c3df8067f3bedbb97d83260be6d\n" | ||||
b"discovery_date 2021-01-25T11:27:51+00:00\n" | b"discovery_date 1611574071\n" | ||||
b"authority forge https://forge.softwareheritage.org/\n" | b"authority forge https://forge.softwareheritage.org/\n" | ||||
b"fetcher swh-phabricator-metadata-fetcher 0.0.1\n" | b"fetcher swh-phabricator-metadata-fetcher 0.0.1\n" | ||||
b"format json\n" | b"format json\n" | ||||
b"path /ab\n" | b"path /ab\n" | ||||
b" c/d\xf0\x9f\xa4\xb7e\x00f\n" | b" c/d\xf0\x9f\xa4\xb7e\x00f\n" | ||||
b"\n" | b"\n" | ||||
b'{"foo": "bar"}' | b'{"foo": "bar"}' | ||||
) | ) | ||||
self.assertEqual( | self.assertEqual( | ||||
identifiers.raw_extrinsic_metadata_identifier(metadata), | identifiers.raw_extrinsic_metadata_identifier(metadata), | ||||
hashlib.sha1(manifest).hexdigest(), | hashlib.sha1(manifest).hexdigest(), | ||||
) | ) | ||||
self.assertEqual( | self.assertEqual( | ||||
identifiers.raw_extrinsic_metadata_identifier(metadata), | identifiers.raw_extrinsic_metadata_identifier(metadata), | ||||
"d8e5856601cdae96dfdfb5147235895949c9322d", | "7cc83fd1912176510c083f5df43f01b09af4b333", | ||||
) | |||||
ardumont: To decrease the repetition on those 2 tests (timezone and microsecond insensitive), you could… | |||||
def test_timezone_insensitive(self): | |||||
"""Checks the timezone of the datetime.datetime does not affect the | |||||
hashed manifest.""" | |||||
utc_plus_one = datetime.timezone(datetime.timedelta(hours=1)) | |||||
metadata = { | |||||
**self.minimal, | |||||
"discovery_date": datetime.datetime( | |||||
2021, 1, 25, 12, 27, 51, tzinfo=utc_plus_one, | |||||
), | |||||
} | |||||
self.assertEqual( | |||||
identifiers.raw_extrinsic_metadata_identifier(self.minimal), | |||||
identifiers.raw_extrinsic_metadata_identifier(metadata), | |||||
) | |||||
self.assertEqual( | |||||
identifiers.raw_extrinsic_metadata_identifier(metadata), | |||||
"5c13f20ba336e44549baf3d7b9305b027ec9f43d", | |||||
) | |||||
def test_microsecond_insensitive(self): | |||||
"""Checks the microseconds of the datetime.datetime does not affect the | |||||
hashed manifest.""" | |||||
metadata = { | |||||
**self.minimal, | |||||
"discovery_date": datetime.datetime( | |||||
2021, 1, 25, 11, 27, 51, 123456, tzinfo=datetime.timezone.utc, | |||||
), | |||||
} | |||||
self.assertEqual( | |||||
identifiers.raw_extrinsic_metadata_identifier(self.minimal), | |||||
identifiers.raw_extrinsic_metadata_identifier(metadata), | |||||
) | |||||
self.assertEqual( | |||||
identifiers.raw_extrinsic_metadata_identifier(metadata), | |||||
"5c13f20ba336e44549baf3d7b9305b027ec9f43d", | |||||
) | |||||
def test_negative_timestamp(self): | |||||
metadata = { | |||||
**self.minimal, | |||||
"discovery_date": datetime.datetime( | |||||
1960, 1, 25, 11, 27, 51, tzinfo=datetime.timezone.utc, | |||||
), | |||||
} | |||||
manifest = ( | |||||
b"raw_extrinsic_metadata 210\0" | |||||
b"target swh:1:cnt:568aaf43d83b2c3df8067f3bedbb97d83260be6d\n" | |||||
b"discovery_date -313504329\n" | |||||
b"authority forge https://forge.softwareheritage.org/\n" | |||||
b"fetcher swh-phabricator-metadata-fetcher 0.0.1\n" | |||||
b"format json\n" | |||||
b"\n" | |||||
b'{"foo": "bar"}' | |||||
) | |||||
self.assertEqual( | |||||
identifiers.raw_extrinsic_metadata_identifier(metadata), | |||||
hashlib.sha1(manifest).hexdigest(), | |||||
) | |||||
self.assertEqual( | |||||
identifiers.raw_extrinsic_metadata_identifier(metadata), | |||||
"895d0821a2991dd376ddc303424aceb7c68280f9", | |||||
) | ) | ||||
origin_example = { | origin_example = { | ||||
"url": "https://github.com/torvalds/linux", | "url": "https://github.com/torvalds/linux", | ||||
} | } | ||||
▲ Show 20 Lines • Show All 705 Lines • Show Last 20 Lines |
To decrease the repetition on those 2 tests (timezone and microsecond insensitive), you could use make the "equivalent" datetime parametric (one naive, one with timezone, one with microseconds, one with both timezone and microseconds, etc... (using pytest.mark.parametrize).
Expectations being the same hash in the end.