diff --git a/digest/test/test_digest_tools.py b/digest/test/test_digest_tools.py new file mode 100644 index 000000000..b04f64c6f --- /dev/null +++ b/digest/test/test_digest_tools.py @@ -0,0 +1,43 @@ +import pytest + +from digest.digest_tools import Digest, content_path, InvalidDigestException + +@pytest.mark.parametrize('digest, output_args', [ + ('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')), +]) +def test_parse_good(digest, output_args): + assert Digest.parse_digest(digest) == Digest(*output_args) + assert str(Digest.parse_digest(digest)) == digest + + +@pytest.mark.parametrize('bad_digest', [ + 'tarsum.v+md5:abc:', + 'sha1:123deadbeefzxczxv', + 'sha256123123', + 'tarsum.v1+', + 'tarsum.v1123+sha1:', +]) +def test_parse_fail(bad_digest): + with pytest.raises(InvalidDigestException): + Digest.parse_digest(bad_digest) + + +@pytest.mark.parametrize('digest, path', [ + ('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'), + ('md5.....+++:1', 'md5/01/1'), + ('.md5.:1', 'md5/01/1'), +]) +def test_paths(digest, path): + assert content_path(digest) == path diff --git a/test/test_digest_tools.py b/test/test_digest_tools.py deleted file mode 100644 index fd873cefa..000000000 --- a/test/test_digest_tools.py +++ /dev/null @@ -1,57 +0,0 @@ -import unittest - -from digest.digest_tools import Digest, content_path, InvalidDigestException - -class TestParseDigest(unittest.TestCase): - def test_parse_good(self): - examples = [ - ('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')), - ] - - 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) - - def test_parse_fail(self): - examples = [ - 'tarsum.v+md5:abc:', - 'sha1:123deadbeefzxczxv', - 'sha256123123', - 'tarsum.v1+', - 'tarsum.v1123+sha1:', - ] - - for bad_digest in examples: - with self.assertRaises(InvalidDigestException): - Digest.parse_digest(bad_digest) - - -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'), - ('md5.....+++:1', 'md5/01/1'), - ('.md5.:1', 'md5/01/1'), - ] - - for digest, path in examples: - self.assertEquals(content_path(digest), path) - - -if __name__ == '__main__': - unittest.main() -