Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_identifiers.py
Show First 20 Lines • Show All 797 Lines • ▼ Show 20 Lines | def setUp(self): | ||||
"release": parse_swhid("swh:1:rel:" + "01" * 20), | "release": parse_swhid("swh:1:rel:" + "01" * 20), | ||||
"revision": parse_swhid("swh:1:rev:" + "02" * 20), | "revision": parse_swhid("swh:1:rev:" + "02" * 20), | ||||
"path": b"/abc/def", | "path": b"/abc/def", | ||||
"directory": parse_swhid("swh:1:dir:" + "03" * 20), | "directory": parse_swhid("swh:1:dir:" + "03" * 20), | ||||
} | } | ||||
def test_minimal(self): | def test_minimal(self): | ||||
manifest = ( | manifest = ( | ||||
b"raw_extrinsic_metadata 245\0" | b"raw_extrinsic_metadata 230\0" | ||||
b"target_type content\n" | b"target_type content\n" | ||||
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), | ||||
"da734f1531f830b7282ee01c5e0c0dfe7ecc99e9", | "e35827936a4ae7c351a92eda0eeb36da07da315f", | ||||
) | ) | ||||
def test_maximal(self): | def test_maximal(self): | ||||
manifest = ( | manifest = ( | ||||
b"raw_extrinsic_metadata 568\0" | b"raw_extrinsic_metadata 553\0" | ||||
b"target_type content\n" | b"target_type content\n" | ||||
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), | ||||
"0d9e3bb9a72850e32bfb575f612cfad1a7e6b66a", | "7523fa6cef72ced2935242a45def7c5a36d0f609", | ||||
) | ) | ||||
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 266\0" | b"raw_extrinsic_metadata 251\0" | ||||
b"target_type content\n" | b"target_type content\n" | ||||
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), | ||||
"63f4cb28396e00926ab7ebfd96e96b60227fc11a", | "caacc32905ebf4659a633a2dc856ccd6412991dc", | ||||
) | |||||
def test_timezone_insensitive(self): | |||||
ardumont: To decrease the repetition on those 2 tests (timezone and microsecond insensitive), you could… | |||||
"""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), | |||||
"e35827936a4ae7c351a92eda0eeb36da07da315f", | |||||
) | |||||
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), | |||||
"e35827936a4ae7c351a92eda0eeb36da07da315f", | |||||
) | |||||
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 230\0" | |||||
b"target_type content\n" | |||||
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), | |||||
"02e58aa3e7d476f6fc174669f9f4b88d56f534fa", | |||||
) | ) | ||||
class OriginIdentifier(unittest.TestCase): | class OriginIdentifier(unittest.TestCase): | ||||
def setUp(self): | def setUp(self): | ||||
self.origin = { | self.origin = { | ||||
"url": "https://github.com/torvalds/linux", | "url": "https://github.com/torvalds/linux", | ||||
} | } | ||||
▲ Show 20 Lines • Show All 413 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.