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 | ||||
import unittest | import unittest | ||||
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 | |||||
from swh.model.identifiers import (CONTENT, DIRECTORY, | from swh.model.identifiers import (CONTENT, DIRECTORY, | ||||
RELEASE, REVISION, | RELEASE, REVISION, | ||||
SNAPSHOT, PersistentId) | SNAPSHOT, PersistentId) | ||||
class UtilityFunctionsIdentifier(unittest.TestCase): | class UtilityFunctionsIdentifier(unittest.TestCase): | ||||
def setUp(self): | def setUp(self): | ||||
self.str_id = 'c2e41aae41ac17bd4a650770d6ee77f62e52235b' | self.str_id = 'c2e41aae41ac17bd4a650770d6ee77f62e52235b' | ||||
▲ Show 20 Lines • Show All 86 Lines • ▼ Show 20 Lines | def setUp(self): | ||||
self.content_id = hashutil.MultiHash.from_data( | self.content_id = hashutil.MultiHash.from_data( | ||||
self.content['data']).digest() | self.content['data']).digest() | ||||
def test_content_identifier(self): | def test_content_identifier(self): | ||||
self.assertEqual(identifiers.content_identifier(self.content), | self.assertEqual(identifiers.content_identifier(self.content), | ||||
self.content_id) | self.content_id) | ||||
class DirectoryIdentifier(unittest.TestCase): | directory_example = { | ||||
def setUp(self): | |||||
self.directory = { | |||||
'id': 'c2e41aae41ac17bd4a650770d6ee77f62e52235b', | 'id': 'c2e41aae41ac17bd4a650770d6ee77f62e52235b', | ||||
'entries': [ | 'entries': [ | ||||
{ | { | ||||
'type': 'file', | 'type': 'file', | ||||
'perms': 33188, | 'perms': 33188, | ||||
'name': b'README', | 'name': b'README', | ||||
'target': '37ec8ea2110c0b7a32fbb0e872f6e7debbf95e21' | 'target': '37ec8ea2110c0b7a32fbb0e872f6e7debbf95e21' | ||||
}, | }, | ||||
{ | { | ||||
'type': 'file', | 'type': 'file', | ||||
'perms': 33188, | 'perms': 33188, | ||||
'name': b'Rakefile', | 'name': b'Rakefile', | ||||
'target': '3bb0e8592a41ae3185ee32266c860714980dbed7' | 'target': '3bb0e8592a41ae3185ee32266c860714980dbed7' | ||||
}, | }, | ||||
{ | { | ||||
'type': 'dir', | 'type': 'dir', | ||||
'perms': 16384, | 'perms': 16384, | ||||
'name': b'app', | 'name': b'app', | ||||
'target': '61e6e867f5d7ba3b40540869bc050b0c4fed9e95' | 'target': '61e6e867f5d7ba3b40540869bc050b0c4fed9e95' | ||||
}, | }, | ||||
{ | { | ||||
'type': 'file', | 'type': 'file', | ||||
'perms': 33188, | 'perms': 33188, | ||||
'name': b'1.megabyte', | 'name': b'1.megabyte', | ||||
'target': '7c2b2fbdd57d6765cdc9d84c2d7d333f11be7fb3' | 'target': '7c2b2fbdd57d6765cdc9d84c2d7d333f11be7fb3' | ||||
}, | }, | ||||
{ | { | ||||
'type': 'dir', | 'type': 'dir', | ||||
'perms': 16384, | 'perms': 16384, | ||||
'name': b'config', | 'name': b'config', | ||||
'target': '591dfe784a2e9ccc63aaba1cb68a765734310d98' | 'target': '591dfe784a2e9ccc63aaba1cb68a765734310d98' | ||||
}, | }, | ||||
{ | { | ||||
'type': 'dir', | 'type': 'dir', | ||||
'perms': 16384, | 'perms': 16384, | ||||
'name': b'public', | 'name': b'public', | ||||
'target': '9588bf4522c2b4648bfd1c61d175d1f88c1ad4a5' | 'target': '9588bf4522c2b4648bfd1c61d175d1f88c1ad4a5' | ||||
}, | }, | ||||
{ | { | ||||
'type': 'file', | 'type': 'file', | ||||
'perms': 33188, | 'perms': 33188, | ||||
'name': b'development.sqlite3', | 'name': b'development.sqlite3', | ||||
'target': 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391' | 'target': 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391' | ||||
}, | }, | ||||
{ | { | ||||
'type': 'dir', | 'type': 'dir', | ||||
'perms': 16384, | 'perms': 16384, | ||||
'name': b'doc', | 'name': b'doc', | ||||
'target': '154705c6aa1c8ead8c99c7915373e3c44012057f' | 'target': '154705c6aa1c8ead8c99c7915373e3c44012057f' | ||||
}, | }, | ||||
{ | { | ||||
'type': 'dir', | 'type': 'dir', | ||||
'perms': 16384, | 'perms': 16384, | ||||
'name': b'db', | 'name': b'db', | ||||
'target': '85f157bdc39356b7bc7de9d0099b4ced8b3b382c' | 'target': '85f157bdc39356b7bc7de9d0099b4ced8b3b382c' | ||||
}, | }, | ||||
{ | { | ||||
'type': 'dir', | 'type': 'dir', | ||||
'perms': 16384, | 'perms': 16384, | ||||
'name': b'log', | 'name': b'log', | ||||
'target': '5e3d3941c51cce73352dff89c805a304ba96fffe' | 'target': '5e3d3941c51cce73352dff89c805a304ba96fffe' | ||||
}, | }, | ||||
{ | { | ||||
'type': 'dir', | 'type': 'dir', | ||||
'perms': 16384, | 'perms': 16384, | ||||
'name': b'script', | 'name': b'script', | ||||
'target': '1b278423caf176da3f3533592012502aa10f566c' | 'target': '1b278423caf176da3f3533592012502aa10f566c' | ||||
}, | }, | ||||
{ | { | ||||
'type': 'dir', | 'type': 'dir', | ||||
'perms': 16384, | 'perms': 16384, | ||||
'name': b'test', | 'name': b'test', | ||||
'target': '035f0437c080bfd8711670b3e8677e686c69c763' | 'target': '035f0437c080bfd8711670b3e8677e686c69c763' | ||||
}, | }, | ||||
{ | { | ||||
'type': 'dir', | 'type': 'dir', | ||||
'perms': 16384, | 'perms': 16384, | ||||
'name': b'vendor', | 'name': b'vendor', | ||||
'target': '7c0dc9ad978c1af3f9a4ce061e50f5918bd27138' | 'target': '7c0dc9ad978c1af3f9a4ce061e50f5918bd27138' | ||||
}, | }, | ||||
{ | { | ||||
'type': 'rev', | 'type': 'rev', | ||||
'perms': 57344, | 'perms': 57344, | ||||
'name': b'will_paginate', | 'name': b'will_paginate', | ||||
'target': '3d531e169db92a16a9a8974f0ae6edf52e52659e' | 'target': '3d531e169db92a16a9a8974f0ae6edf52e52659e' | ||||
} | } | ||||
], | ], | ||||
} | } | ||||
class DirectoryIdentifier(unittest.TestCase): | |||||
def setUp(self): | |||||
self.directory = directory_example | |||||
self.empty_directory = { | self.empty_directory = { | ||||
'id': '4b825dc642cb6eb9a060e54bf8d69288fbee4904', | 'id': '4b825dc642cb6eb9a060e54bf8d69288fbee4904', | ||||
'entries': [], | 'entries': [], | ||||
} | } | ||||
def test_dir_identifier(self): | def test_dir_identifier(self): | ||||
self.assertEqual( | self.assertEqual( | ||||
identifiers.directory_identifier(self.directory), | identifiers.directory_identifier(self.directory), | ||||
self.directory['id']) | self.directory['id']) | ||||
def test_dir_identifier_empty_directory(self): | def test_dir_identifier_empty_directory(self): | ||||
self.assertEqual( | self.assertEqual( | ||||
identifiers.directory_identifier(self.empty_directory), | identifiers.directory_identifier(self.empty_directory), | ||||
self.empty_directory['id']) | self.empty_directory['id']) | ||||
class RevisionIdentifier(unittest.TestCase): | |||||
def setUp(self): | |||||
linus_tz = datetime.timezone(datetime.timedelta(minutes=-420)) | linus_tz = datetime.timezone(datetime.timedelta(minutes=-420)) | ||||
revision_example = { | |||||
'id': 'bc0195aad0daa2ad5b0d76cce22b167bc3435590', | |||||
'directory': '85a74718d377195e1efd0843ba4f3260bad4fe07', | |||||
'parents': ['01e2d0627a9a6edb24c37db45db5ecb31e9de808'], | |||||
'author': { | |||||
'name': b'Linus Torvalds', | |||||
'email': b'torvalds@linux-foundation.org', | |||||
'fullname': b'Linus Torvalds <torvalds@linux-foundation.org>' | |||||
}, | |||||
'date': datetime.datetime(2015, 7, 12, 15, 10, 30, | |||||
tzinfo=linus_tz), | |||||
'committer': { | |||||
'name': b'Linus Torvalds', | |||||
'email': b'torvalds@linux-foundation.org', | |||||
'fullname': b'Linus Torvalds <torvalds@linux-foundation.org>' | |||||
}, | |||||
'committer_date': datetime.datetime(2015, 7, 12, 15, 10, 30, | |||||
tzinfo=linus_tz), | |||||
'message': b'Linux 4.2-rc2\n', | |||||
'type': 'git', | |||||
'synthetic': False | |||||
} | |||||
class RevisionIdentifier(unittest.TestCase): | |||||
def setUp(self): | |||||
gpgsig = b'''\ | gpgsig = b'''\ | ||||
-----BEGIN PGP SIGNATURE----- | -----BEGIN PGP SIGNATURE----- | ||||
Version: GnuPG v1.4.13 (Darwin) | Version: GnuPG v1.4.13 (Darwin) | ||||
iQIcBAABAgAGBQJVJcYsAAoJEBiY3kIkQRNJVAUQAJ8/XQIfMqqC5oYeEFfHOPYZ | iQIcBAABAgAGBQJVJcYsAAoJEBiY3kIkQRNJVAUQAJ8/XQIfMqqC5oYeEFfHOPYZ | ||||
L7qy46bXHVBa9Qd8zAJ2Dou3IbI2ZoF6/Et89K/UggOycMlt5FKV/9toWyuZv4Po | L7qy46bXHVBa9Qd8zAJ2Dou3IbI2ZoF6/Et89K/UggOycMlt5FKV/9toWyuZv4Po | ||||
L682wonoxX99qvVTHo6+wtnmYO7+G0f82h+qHMErxjP+I6gzRNBvRr+SfY7VlGdK | L682wonoxX99qvVTHo6+wtnmYO7+G0f82h+qHMErxjP+I6gzRNBvRr+SfY7VlGdK | ||||
wikMKOMWC5smrScSHITnOq1Ews5pe3N7qDYMzK0XVZmgDoaem4RSWMJs4My/qVLN | wikMKOMWC5smrScSHITnOq1Ews5pe3N7qDYMzK0XVZmgDoaem4RSWMJs4My/qVLN | ||||
e0CqYWq2A22GX7sXl6pjneJYQvcAXUX+CAzp24QnPSb+Q22Guj91TcxLFcHCTDdn | e0CqYWq2A22GX7sXl6pjneJYQvcAXUX+CAzp24QnPSb+Q22Guj91TcxLFcHCTDdn | ||||
qgqMsEyMiisoglwrCbO+D+1xq9mjN9tNFWP66SQ48mrrHYTBV5sz9eJyDfroJaLP | qgqMsEyMiisoglwrCbO+D+1xq9mjN9tNFWP66SQ48mrrHYTBV5sz9eJyDfroJaLP | ||||
CWgbDTgq6GzRMehHT3hXfYS5NNatjnhkNISXR7pnVP/obIi/vpWh5ll6Gd8q26z+ | CWgbDTgq6GzRMehHT3hXfYS5NNatjnhkNISXR7pnVP/obIi/vpWh5ll6Gd8q26z+ | ||||
a/O41UzOaLTeNI365MWT4/cnXohVLRG7iVJbAbCxoQmEgsYMRc/pBAzWJtLfcB2G | a/O41UzOaLTeNI365MWT4/cnXohVLRG7iVJbAbCxoQmEgsYMRc/pBAzWJtLfcB2G | ||||
jdTswYL6+MUdL8sB9pZ82D+BP/YAdHe69CyTu1lk9RT2pYtI/kkfjHubXBCYEJSG | jdTswYL6+MUdL8sB9pZ82D+BP/YAdHe69CyTu1lk9RT2pYtI/kkfjHubXBCYEJSG | ||||
+VGllBbYG6idQJpyrOYNRJyrDi9yvDJ2W+S0iQrlZrxzGBVGTB/y65S8C+2WTBcE | +VGllBbYG6idQJpyrOYNRJyrDi9yvDJ2W+S0iQrlZrxzGBVGTB/y65S8C+2WTBcE | ||||
lf1Qb5GDsQrZWgD+jtWTywOYHtCBwyCKSAXxSARMbNPeak9WPlcW/Jmu+fUcMe2x | lf1Qb5GDsQrZWgD+jtWTywOYHtCBwyCKSAXxSARMbNPeak9WPlcW/Jmu+fUcMe2x | ||||
dg1KdHOa34shrKDaOVzW | dg1KdHOa34shrKDaOVzW | ||||
=od6m | =od6m | ||||
-----END PGP SIGNATURE-----''' | -----END PGP SIGNATURE-----''' | ||||
self.revision = { | self.revision = revision_example | ||||
'id': 'bc0195aad0daa2ad5b0d76cce22b167bc3435590', | |||||
'directory': '85a74718d377195e1efd0843ba4f3260bad4fe07', | |||||
'parents': ['01e2d0627a9a6edb24c37db45db5ecb31e9de808'], | |||||
'author': { | |||||
'name': b'Linus Torvalds', | |||||
'email': b'torvalds@linux-foundation.org', | |||||
}, | |||||
'date': datetime.datetime(2015, 7, 12, 15, 10, 30, | |||||
tzinfo=linus_tz), | |||||
'committer': { | |||||
'name': b'Linus Torvalds', | |||||
'email': b'torvalds@linux-foundation.org', | |||||
}, | |||||
'committer_date': datetime.datetime(2015, 7, 12, 15, 10, 30, | |||||
tzinfo=linus_tz), | |||||
'message': b'Linux 4.2-rc2\n', | |||||
} | |||||
self.revision_none_metadata = { | self.revision_none_metadata = { | ||||
'id': 'bc0195aad0daa2ad5b0d76cce22b167bc3435590', | 'id': 'bc0195aad0daa2ad5b0d76cce22b167bc3435590', | ||||
'directory': '85a74718d377195e1efd0843ba4f3260bad4fe07', | 'directory': '85a74718d377195e1efd0843ba4f3260bad4fe07', | ||||
'parents': ['01e2d0627a9a6edb24c37db45db5ecb31e9de808'], | 'parents': ['01e2d0627a9a6edb24c37db45db5ecb31e9de808'], | ||||
'author': { | 'author': { | ||||
'name': b'Linus Torvalds', | 'name': b'Linus Torvalds', | ||||
'email': b'torvalds@linux-foundation.org', | 'email': b'torvalds@linux-foundation.org', | ||||
▲ Show 20 Lines • Show All 232 Lines • ▼ Show 20 Lines | def test_revision_identifier_only_fullname(self): | ||||
self.assertEqual( | self.assertEqual( | ||||
identifiers.revision_identifier( | identifiers.revision_identifier( | ||||
self.revision_only_fullname), | self.revision_only_fullname), | ||||
identifiers.identifier_to_str( | identifiers.identifier_to_str( | ||||
self.revision_only_fullname['id']), | self.revision_only_fullname['id']), | ||||
) | ) | ||||
class ReleaseIdentifier(unittest.TestCase): | release_example = { | ||||
def setUp(self): | |||||
linus_tz = datetime.timezone(datetime.timedelta(minutes=-420)) | |||||
self.release = { | |||||
'id': '2b10839e32c4c476e9d94492756bb1a3e1ec4aa8', | 'id': '2b10839e32c4c476e9d94492756bb1a3e1ec4aa8', | ||||
'target': b't\x1b"R\xa5\xe1Ml`\xa9\x13\xc7z`\x99\xab\xe7:\x85J', | 'target': b't\x1b"R\xa5\xe1Ml`\xa9\x13\xc7z`\x99\xab\xe7:\x85J', | ||||
'target_type': 'revision', | 'target_type': 'revision', | ||||
'name': b'v2.6.14', | 'name': b'v2.6.14', | ||||
'author': { | 'author': { | ||||
'name': b'Linus Torvalds', | 'name': b'Linus Torvalds', | ||||
'email': b'torvalds@g5.osdl.org', | 'email': b'torvalds@g5.osdl.org', | ||||
'fullname': b'Linus Torvalds <torvalds@g5.osdl.org>' | |||||
}, | }, | ||||
'date': datetime.datetime(2005, 10, 27, 17, 2, 33, | 'date': datetime.datetime(2005, 10, 27, 17, 2, 33, | ||||
tzinfo=linus_tz), | tzinfo=linus_tz), | ||||
'message': b'''\ | 'message': b'''\ | ||||
Linux 2.6.14 release | Linux 2.6.14 release | ||||
-----BEGIN PGP SIGNATURE----- | -----BEGIN PGP SIGNATURE----- | ||||
Version: GnuPG v1.4.1 (GNU/Linux) | Version: GnuPG v1.4.1 (GNU/Linux) | ||||
iD8DBQBDYWq6F3YsRnbiHLsRAmaeAJ9RCez0y8rOBbhSv344h86l/VVcugCeIhO1 | iD8DBQBDYWq6F3YsRnbiHLsRAmaeAJ9RCez0y8rOBbhSv344h86l/VVcugCeIhO1 | ||||
wdLOnvj91G4wxYqrvThthbE= | wdLOnvj91G4wxYqrvThthbE= | ||||
=7VeT | =7VeT | ||||
-----END PGP SIGNATURE----- | -----END PGP SIGNATURE----- | ||||
''', | ''', | ||||
'synthetic': False, | 'synthetic': False, | ||||
} | } | ||||
class ReleaseIdentifier(unittest.TestCase): | |||||
def setUp(self): | |||||
linus_tz = datetime.timezone(datetime.timedelta(minutes=-420)) | |||||
self.release = release_example | |||||
self.release_no_author = { | self.release_no_author = { | ||||
'id': b'&y\x1a\x8b\xcf\x0em3\xf4:\xefv\x82\xbd\xb5U#mV\xde', | 'id': b'&y\x1a\x8b\xcf\x0em3\xf4:\xefv\x82\xbd\xb5U#mV\xde', | ||||
'target': '9ee1c939d1cb936b1f98e8d81aeffab57bae46ab', | 'target': '9ee1c939d1cb936b1f98e8d81aeffab57bae46ab', | ||||
'target_type': 'revision', | 'target_type': 'revision', | ||||
'name': b'v2.6.12', | 'name': b'v2.6.12', | ||||
'message': b'''\ | 'message': b'''\ | ||||
This is the final 2.6.12 release | This is the final 2.6.12 release | ||||
-----BEGIN PGP SIGNATURE----- | -----BEGIN PGP SIGNATURE----- | ||||
▲ Show 20 Lines • Show All 120 Lines • ▼ Show 20 Lines | ''', | ||||
def test_release_identifier_snapshot_target(self): | def test_release_identifier_snapshot_target(self): | ||||
self.assertEqual( | self.assertEqual( | ||||
identifiers.release_identifier(self.release_snapshot_target), | identifiers.release_identifier(self.release_snapshot_target), | ||||
identifiers.identifier_to_str(self.release_snapshot_target['id']) | identifiers.identifier_to_str(self.release_snapshot_target['id']) | ||||
) | ) | ||||
snapshot_example = { | |||||
'id': hash_to_bytes('6e65b86363953b780d92b0a928f3e8fcdd10db36'), | |||||
'branches': { | |||||
b'directory': { | |||||
'target': hash_to_bytes( | |||||
'1bd0e65f7d2ff14ae994de17a1e7fe65111dcad8'), | |||||
'target_type': 'directory', | |||||
}, | |||||
b'content': { | |||||
'target': hash_to_bytes( | |||||
'fe95a46679d128ff167b7c55df5d02356c5a1ae1'), | |||||
'target_type': 'content', | |||||
}, | |||||
b'alias': { | |||||
'target': b'revision', | |||||
'target_type': 'alias', | |||||
}, | |||||
b'revision': { | |||||
'target': hash_to_bytes( | |||||
'aafb16d69fd30ff58afdd69036a26047f3aebdc6'), | |||||
'target_type': 'revision', | |||||
}, | |||||
b'release': { | |||||
'target': hash_to_bytes( | |||||
'7045404f3d1c54e6473c71bbb716529fbad4be24'), | |||||
'target_type': 'release', | |||||
}, | |||||
b'snapshot': { | |||||
'target': hash_to_bytes( | |||||
'1a8893e6a86f444e8be8e7bda6cb34fb1735a00e' | |||||
), | |||||
'target_type': 'snapshot', | |||||
}, | |||||
b'dangling': None, | |||||
} | |||||
} | |||||
class SnapshotIdentifier(unittest.TestCase): | class SnapshotIdentifier(unittest.TestCase): | ||||
def setUp(self): | def setUp(self): | ||||
super().setUp() | super().setUp() | ||||
self.empty = { | self.empty = { | ||||
'id': '1a8893e6a86f444e8be8e7bda6cb34fb1735a00e', | 'id': '1a8893e6a86f444e8be8e7bda6cb34fb1735a00e', | ||||
'branches': {}, | 'branches': {}, | ||||
} | } | ||||
Show All 10 Lines | def setUp(self): | ||||
'branches': { | 'branches': { | ||||
b'foo': { | b'foo': { | ||||
'target': b'bar', | 'target': b'bar', | ||||
'target_type': 'alias', | 'target_type': 'alias', | ||||
}, | }, | ||||
}, | }, | ||||
} | } | ||||
self.all_types = { | self.all_types = snapshot_example | ||||
'id': '6e65b86363953b780d92b0a928f3e8fcdd10db36', | |||||
'branches': { | |||||
b'directory': { | |||||
'target': '1bd0e65f7d2ff14ae994de17a1e7fe65111dcad8', | |||||
'target_type': 'directory', | |||||
}, | |||||
b'content': { | |||||
'target': 'fe95a46679d128ff167b7c55df5d02356c5a1ae1', | |||||
'target_type': 'content', | |||||
}, | |||||
b'alias': { | |||||
'target': b'revision', | |||||
'target_type': 'alias', | |||||
}, | |||||
b'revision': { | |||||
'target': 'aafb16d69fd30ff58afdd69036a26047f3aebdc6', | |||||
'target_type': 'revision', | |||||
}, | |||||
b'release': { | |||||
'target': '7045404f3d1c54e6473c71bbb716529fbad4be24', | |||||
'target_type': 'release', | |||||
}, | |||||
b'snapshot': { | |||||
'target': '1a8893e6a86f444e8be8e7bda6cb34fb1735a00e', | |||||
'target_type': 'snapshot', | |||||
}, | |||||
b'dangling': None, | |||||
} | |||||
} | |||||
def test_empty_snapshot(self): | def test_empty_snapshot(self): | ||||
self.assertEqual( | self.assertEqual( | ||||
identifiers.snapshot_identifier(self.empty), | identifiers.snapshot_identifier(self.empty), | ||||
identifiers.identifier_to_str(self.empty['id']), | identifiers.identifier_to_str(self.empty['id']), | ||||
) | ) | ||||
def test_dangling_branch(self): | def test_dangling_branch(self): | ||||
▲ Show 20 Lines • Show All 181 Lines • Show Last 20 Lines |