From dc04336f5d1c9581b7e5bcc9bde2cc1e1c750051 Mon Sep 17 00:00:00 2001 From: Martin Czygan Date: Wed, 12 Aug 2020 11:56:01 +0200 Subject: add tests --- tests/test_utils.py | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 tests/test_utils.py (limited to 'tests') diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..ed206dd --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,115 @@ +# coding: utf-8 + +from typing import List, NamedTuple +import pytest + +from fuzzycat.utils import * + + +def test_extract_issns(): + Case = NamedTuple("Case", [("s", str), ("result", List[str])]) + cases = ( + Case("", []), + Case("Hello 1234", []), + Case("Hello 1084-5100 World", ["1084-5100"]), + Case("Hello 10845100 World", []), + Case("Hello 1084-5100 1084-5100 World", ["1084-5100", "1084-5100"]), + Case("2323-573X 2169-1886 Journal", ["2323-573X", "2169-1886"]), + ) + for c in cases: + result = extract_issns(c.s) + assert result == c.result + + +def test_longest_common_prefix(): + Case = NamedTuple("Case", [("a", str), ("b", str), ("result", str)]) + cases = ( + Case("", "", ""), + Case("a", "", ""), + Case("ab", "a", "a"), + Case("123", "123", "123"), + ) + for c in cases: + result = longest_common_prefix(c.a, c.b) + assert result == c.result + + +def test_common_prefix_length_ratio(): + Case = NamedTuple("Case", [("a", str), ("b", str), ("result", float)]) + cases = ( + Case("", "", 0.0), + Case("a", "", 0.0), + Case("Hello World!", "ello", 0.0), + Case("ab", "a", 0.5), + Case("123", "123", 1.0), + Case("1234", "123", 0.75), + ) + for c in cases: + result = common_prefix_length_ratio(c.a, c.b) + assert result == c.result + + +def test_hamming_distance(): + Case = NamedTuple("Case", [("a", str), ("b", str), ("result", int)]) + cases = ( + Case("", "", 0), + Case("a", "a", 0), + Case("a", "ab", 1), + Case("abc", "cba", 2), + Case("1234", "", 4), + ) + for c in cases: + result = hamming_distance(c.a, c.b) + assert result == c.result + + +def test_is_valid_issn(): + cases = { + "value_error": ("", "1234", "123456", "111122223333", "XXXXXXXX"), + "valid": ( + "0710-4081", + "0011-7625", + "2268-5901", + "1809-0710", + "1533-7561", + "07104081", + "00117625", + "22685901", + "18090710", + "15337561", + ), + "invalid": ( + "0710-4080", + "0011-7626", + "2268-5902", + "1809-0709", + "1533-7560", + "07104080", + "00117626", + "22685902", + "18090709", + "15337560", + ), + } + for ve in cases["value_error"]: + with pytest.raises(ValueError): + is_valid_issn(ve) + for v in cases["valid"]: + assert is_valid_issn(v) == True + for v in cases["invalid"]: + assert is_valid_issn(v) == False + + +def test_keys_with_values(): + Case = NamedTuple("Case", [("d", Dict), ("result", List[Any])]) + cases = ( + Case({}, []), + Case({"a": "v"}, ["a"]), + Case({"a": "", "b": "v"}, ["b"]), + Case({"a": None, "b": "v"}, ["b"]), + Case({"a": [], "b": "v"}, ["b"]), + Case({"a": 0, "b": "v"}, ["b"]), + ) + for case in cases: + result = keys_with_values(case.d) + assert result == case.result -- cgit v1.2.3