Commit 4225f62d authored by Wim Van de Meerssche's avatar Wim Van de Meerssche
Browse files

urn compare is now lowercase

parent 1bb9dce0
......@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
setup(
name="wvdemeer-utils",
version="1.3.0",
version="1.3.1",
description="A collection of useful functions",
long_description=long_description,
......
......@@ -77,3 +77,88 @@ def test_urn2b():
actual = URN(authority='example.com//sub1//sub2', type='ttype2', name='tname2').urn
expected = TEST_URN_2
assert actual == expected, 'actual({}) != expected({})'.format(actual, expected)
def test_urn_equality_1():
urn_a = URN('urn:publicid:IDN+example.com+ttype1+tname1')
urn_b = URN('urn:publicid:IDN+example.com+ttype1+tname1')
assert urn_a == urn_b
def test_urn_equality_2():
urn_a = URN('urn:publicid:IDN+example.com+ttype1+tname1')
urn_b = URN('urn:publicid:IDN+example.com+ttype1+Tname1')
assert urn_a == urn_b
def test_urn_equality_3():
urn_a = URN('urn:publicid:IDN+example.com+ttype1+tname1')
urn_b = URN('urn:publicid:IDN+example.com+Ttype1+tname1')
assert urn_a == urn_b
def test_urn_equality_4():
urn_a = URN('urn:publicid:IDN+example.com+ttype1+tname1')
urn_b = URN('urn:publicid:IDN+Example.Com+ttype1+tname1')
assert urn_a == urn_b
def test_urn_equality_5():
urn_a = URN('urn:publicid:IDN+example.com+ttype1+tname1')
urn_b = URN('urn:publicid:IDN+EXAMPLE.COM+TTYPE1+TNAME1')
assert urn_a == urn_b
def test_urn_equality_6():
urn_a = URN('urn:publicid:IDN+example.com+ttype1+tname1')
urn_b = URN('urn:publicid:IDN+example.com+ttype1+tname2')
assert urn_a != urn_b
def test_urn_hash_1():
urn_a = URN('urn:publicid:IDN+example.com+ttype1+tname1')
urn_b = URN('urn:publicid:IDN+example.com+ttype1+tname1')
d = {urn_a: 'test'}
assert urn_b in d
def test_urn_hash_2():
urn_a = URN('urn:publicid:IDN+example.com+ttype1+tname1')
urn_b = URN('urn:publicid:IDN+example.com+ttype1+Tname1')
d = {urn_a: 'test'}
assert urn_b in d
def test_urn_hash_3():
urn_a = URN('urn:publicid:IDN+example.com+ttype1+tname1')
urn_b = URN('urn:publicid:IDN+example.com+Ttype1+tname1')
d = {urn_a: 'test'}
assert urn_b in d
def test_urn_hash_4():
urn_a = URN('urn:publicid:IDN+example.com+ttype1+tname1')
urn_b = URN('urn:publicid:IDN+Example.Com+ttype1+tname1')
d = {urn_a: 'test'}
assert urn_b in d
def test_urn_hash_5():
urn_a = URN('urn:publicid:IDN+example.com+ttype1+tname1')
urn_b = URN('urn:publicid:IDN+EXAMPLE.COM+TTYPE1+TNAME1')
d = {urn_a: 'test'}
assert urn_b in d
def test_urn_hash_6():
urn_a = URN('urn:publicid:IDN+example.com+ttype1+tname1')
urn_b = URN('urn:publicid:IDN+example.com+ttype1+tname2')
d = {urn_a: 'test'}
assert urn_b not in d
def test_lowercase_urn_string_1():
inurn = URN('urn:publicid:IDN+EXAMPLE.COM+TTYPE1+TNAME1')
expected = 'urn:publicid:IDN+example.com+ttype1+tname1'
actual = inurn.lowercase_urn_string
assert actual == expected, 'actual({!r}) != expected({!r})'.format(actual, expected)
......@@ -93,6 +93,11 @@ class URN(object):
raise ValueError(
"Failed to create valid URN from args %s, %s, %s" % (self.authority, self.type, self.name))
lower_authority = string_to_urn_format(self.authority).lower()
lower_type = string_to_urn_format(self.type).lower()
lower_name = string_to_urn_format(self.name).lower()
self.lowercase_urn_string = '%s+%s+%s+%s' % (URN_PREFIX, lower_authority, lower_type, lower_name)
def __str__(self):
return self.urn_string()
......@@ -102,17 +107,17 @@ class URN(object):
def __eq__(self, other):
"""Overrides the default implementation"""
if isinstance(other, URN):
return self.urn_string() == other.urn_string()
return self.lowercase_urn_string == other.lowercase_urn_string
return NotImplemented
def __lt__(self, other):
if not isinstance(other, URN):
return NotImplemented
return self.urn_string() < other.urn_string()
return self.lowercase_urn_string < other.lowercase_urn_string
def __hash__(self):
"""Overrides the default implementation"""
return hash(self.urn_string())
return hash(self.lowercase_urn_string)
def urn_string(self):
return self.urn
......@@ -324,4 +329,4 @@ def urn_to_string_format(urnstr):
# Validate it is reasonable URN string?
for a, b in reversed(publicid_xforms):
publicid = publicid.replace(b, a)
return publicid
\ No newline at end of file
return publicid
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment