import pytest

from imecilabt_utils.urn_util import URN


TEST_URN_1 = 'urn:publicid:IDN+example.com+ttype1+tname1'
TEST_URN_2 = 'urn:publicid:IDN+example.com:sub1:sub2+ttype2+tname2'


def test_get_authority_parts1():
    actual = URN(urn=TEST_URN_1).authority_parts
    expected = ['example.com']
    assert actual == expected, 'actual({}) != expected({})'.format(actual, expected)


def test_get_authority_parts2():
    actual = URN(urn=TEST_URN_2).authority_parts
    expected = ['example.com', 'sub1', 'sub2']
    assert actual == expected, 'actual({}) != expected({})'.format(actual, expected)


def test_authority1():
    actual = URN(urn=TEST_URN_1).authority
    expected = 'example.com'
    assert actual == expected, 'actual({}) != expected({})'.format(actual, expected)


def test_authority2():
    actual = URN(urn=TEST_URN_2).authority
    expected = 'example.com//sub1//sub2'
    assert actual == expected, 'actual({}) != expected({})'.format(actual, expected)


def test_type1():
    actual = URN(urn=TEST_URN_1).type
    expected = 'ttype1'
    assert actual == expected, 'actual({}) != expected({})'.format(actual, expected)


def test_type2():
    actual = URN(urn=TEST_URN_2).type
    expected = 'ttype2'
    assert actual == expected, 'actual({}) != expected({})'.format(actual, expected)


def test_name1():
    actual = URN(urn=TEST_URN_1).name
    expected = 'tname1'
    assert actual == expected, 'actual({}) != expected({})'.format(actual, expected)


def test_name2():
    actual = URN(urn=TEST_URN_2).name
    expected = 'tname2'
    assert actual == expected, 'actual({}) != expected({})'.format(actual, expected)


def test_urn1a():
    actual = URN(urn=TEST_URN_1).urn
    expected = TEST_URN_1
    assert actual == expected, 'actual({}) != expected({})'.format(actual, expected)


def test_urn2a():
    actual = URN(urn=TEST_URN_2).urn
    expected = TEST_URN_2
    assert actual == expected, 'actual({}) != expected({})'.format(actual, expected)


def test_urn1b():
    actual = URN(authority='example.com', type='ttype1', name='tname1').urn
    expected = TEST_URN_1
    assert actual == expected, 'actual({}) != expected({})'.format(actual, expected)


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_5b():
    urn_a = URN('urn:publicid:IDN+example.com+ttype1+tname1')
    urn_b = URN('urn:publicid:IDN+EXAMPLE.COM+TTYPE1+TNAME1')
    assert urn_a.__hash__() == urn_b.__hash__()


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)
