2015-06-22 21:37:13 +00:00
|
|
|
import unittest
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
from digest.digest_tools import Digest, content_path, InvalidDigestException
|
2015-06-22 21:37:13 +00:00
|
|
|
|
|
|
|
class TestParseDigest(unittest.TestCase):
|
|
|
|
def test_parse_good(self):
|
|
|
|
examples = [
|
2015-11-06 22:47:28 +00:00
|
|
|
('tarsum.v123123+sha1:123deadbeef', ('tarsum.v123123+sha1', '123deadbeef')),
|
|
|
|
('tarsum.v1+sha256:123123', ('tarsum.v1+sha256', '123123')),
|
|
|
|
('tarsum.v0+md5:abc', ('tarsum.v0+md5', 'abc')),
|
|
|
|
('tarsum+sha1:abc', ('tarsum+sha1', 'abc')),
|
|
|
|
('sha1:123deadbeef', ('sha1', '123deadbeef')),
|
|
|
|
('sha256:123123', ('sha256', '123123')),
|
|
|
|
('md5:abc', ('md5', 'abc')),
|
2015-06-22 21:37:13 +00:00
|
|
|
]
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
for digest, output_args in examples:
|
|
|
|
self.assertEquals(Digest.parse_digest(digest), Digest(*output_args))
|
|
|
|
|
|
|
|
# Test the string method
|
|
|
|
self.assertEquals(str(Digest.parse_digest(digest)), digest)
|
2015-06-22 21:37:13 +00:00
|
|
|
|
|
|
|
def test_parse_fail(self):
|
|
|
|
examples = [
|
2015-11-06 22:47:28 +00:00
|
|
|
'tarsum.v+md5:abc:',
|
2015-06-22 21:37:13 +00:00
|
|
|
'sha1:123deadbeefzxczxv',
|
|
|
|
'sha256123123',
|
|
|
|
'tarsum.v1+',
|
|
|
|
'tarsum.v1123+sha1:',
|
|
|
|
]
|
|
|
|
|
|
|
|
for bad_digest in examples:
|
|
|
|
with self.assertRaises(InvalidDigestException):
|
2015-08-12 20:39:32 +00:00
|
|
|
Digest.parse_digest(bad_digest)
|
2015-06-22 21:37:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestDigestPath(unittest.TestCase):
|
|
|
|
def test_paths(self):
|
|
|
|
examples = [
|
|
|
|
('tarsum.v123123+sha1:123deadbeef', 'tarsum/v123123/sha1/12/123deadbeef'),
|
|
|
|
('tarsum.v1+sha256:123123', 'tarsum/v1/sha256/12/123123'),
|
|
|
|
('tarsum.v0+md5:abc', 'tarsum/v0/md5/ab/abc'),
|
|
|
|
('sha1:123deadbeef', 'sha1/12/123deadbeef'),
|
|
|
|
('sha256:123123', 'sha256/12/123123'),
|
|
|
|
('md5:abc', 'md5/ab/abc'),
|
|
|
|
('md5:1', 'md5/01/1'),
|
2015-11-06 22:47:28 +00:00
|
|
|
('md5.....+++:1', 'md5/01/1'),
|
|
|
|
('.md5.:1', 'md5/01/1'),
|
2015-06-22 21:37:13 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
for digest, path in examples:
|
|
|
|
self.assertEquals(content_path(digest), path)
|