Skip to content

Commit 9b2a7dd

Browse files
committed
Add doctest and tests for clearcode coordinate helpers
Signed-off-by: drawlin <itshoussameddine@gmail.com>
1 parent 469c506 commit 9b2a7dd

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

clearcode/cdutils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,14 @@ def str2coord(s):
524524
URL: "cd:/gem/rubygems/-/mocha/1.7.0"
525525
URN: "urn:gem:rubygems:-:mocha:revision:1.7.0:tool:scancode:3.1.0"
526526
plain: /gem/rubygems/foo/mocha/1.7.0"
527+
528+
>>> str2coord("cd:/gem/rubygems/-/mocha/1.7.0")
529+
{'type': 'gem', 'provider': 'rubygems', 'namespace': '-', 'name': 'mocha', 'revision': '1.7.0'}
530+
>>> str2coord("urn:gem:rubygems:-:mocha:revision:1.7.0:tool:scancode:3.1.0")
531+
{'type': 'gem', 'provider': 'rubygems', 'namespace': '-', 'name': 'mocha', 'revision': '1.7.0'}
532+
>>> str2coord("/gem/rubygems/foo/mocha/1.7.0")
533+
{'type': 'gem', 'provider': 'rubygems', 'namespace': 'foo', 'name': 'mocha', 'revision': '1.7.0'}
527534
"""
528-
# TODO: Add doctest
529535
is_urn = s.startswith("urn")
530536
is_url = s.startswith("cd:")
531537
splitter = ":" if is_urn else "/"

clearcode/tests/test_sync.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
from django.test import TestCase
2323

24+
from clearcode.cdutils import coord2str
25+
from clearcode.cdutils import str2coord
2426
from clearcode.models import CDitem
2527
from clearcode.sync import db_saver
2628

@@ -42,3 +44,34 @@ def test_db_saver_identical_path(self):
4244
def test_db_saver_different_path(self):
4345
db_saver(content=self.test_content, blob_path="new/blob/path.json")
4446
self.assertEqual(2, len(CDitem.objects.all()))
47+
48+
49+
class CDUtilsTestCase(TestCase):
50+
def test_str2coord_from_cd_url(self):
51+
assert str2coord("cd:/gem/rubygems/-/mocha/1.7.0") == {
52+
"type": "gem",
53+
"provider": "rubygems",
54+
"namespace": "-",
55+
"name": "mocha",
56+
"revision": "1.7.0",
57+
}
58+
59+
def test_str2coord_from_urn_ignores_extra_segments(self):
60+
assert str2coord("urn:gem:rubygems:-:mocha:revision:1.7.0:tool:scancode:3.1.0") == {
61+
"type": "gem",
62+
"provider": "rubygems",
63+
"namespace": "-",
64+
"name": "mocha",
65+
"revision": "1.7.0",
66+
}
67+
68+
def test_coord2str_preserves_missing_namespace_as_dash(self):
69+
assert coord2str(
70+
{
71+
"type": "git",
72+
"provider": "github",
73+
"namespace": None,
74+
"name": "license-expression",
75+
"revision": "70277cdfc186466667cb58ec9f9c7281e68a221b",
76+
}
77+
) == "git/github/-/license-expression/70277cdfc186466667cb58ec9f9c7281e68a221b"

0 commit comments

Comments
 (0)