Commons and EDRPOU basic functionality (parser (but complete only), checksum with test coverage)

This commit is contained in:
2024-12-11 03:02:07 -05:00
parent 9db28e7277
commit a7692124e9
16 changed files with 477 additions and 19 deletions

View File

@@ -0,0 +1,92 @@
defmodule UkraineTaxidEx.CommonsTest do
use ExUnit.Case
alias UkraineTaxidEx.Commons
doctest UkraineTaxidEx.Commons
describe "digits/2" do
test "converts string to list of digits" do
assert Commons.digits("123") == [1, 2, 3]
assert Commons.digits("456") == [4, 5, 6]
assert Commons.digits("789") == [7, 8, 9]
end
test "converts integer to list of digits" do
assert Commons.digits(123) == [1, 2, 3]
assert Commons.digits(456) == [4, 5, 6]
assert Commons.digits(789) == [7, 8, 9]
end
test "pads with zeros when length is specified" do
assert Commons.digits("123", 5) == [0, 0, 1, 2, 3]
assert Commons.digits(123, 5) == [0, 0, 1, 2, 3]
assert Commons.digits("45", 4) == [0, 0, 4, 5]
end
test "handles strings with non-digit characters" do
assert Commons.digits("1-2-3") == [1, 2, 3]
assert Commons.digits("A1B2C3") == [1, 2, 3]
assert Commons.digits("12.34") == [1, 2, 3, 4]
end
test "handles empty string" do
assert Commons.digits("") == []
assert Commons.digits("", 3) == [0, 0, 0]
end
end
describe "check_digit/1" do
test "returns the last digit from the list" do
assert Commons.check_digit([1, 2, 3, 4, 5]) == 5
assert Commons.check_digit([9, 8, 7]) == 7
end
test "returns nil for empty list" do
assert Commons.check_digit([]) == nil
end
end
describe "value_digits/1" do
test "returns all digits except the last one" do
assert Commons.value_digits([1, 2, 3, 4, 5]) == [1, 2, 3, 4]
assert Commons.value_digits([9, 8, 7]) == [9, 8]
end
test "returns empty list for empty input" do
assert Commons.value_digits([]) == []
end
test "returns empty list for single digit input" do
assert Commons.value_digits([1]) == []
end
end
describe "value_and_check_digits/1" do
test "returns tuple with value digits and check digit" do
assert Commons.value_and_check_digits([1, 2, 3, 4, 5]) == {[1, 2, 3, 4], 5}
assert Commons.value_and_check_digits([9, 8, 7]) == {[9, 8], 7}
end
test "handles empty list" do
assert Commons.value_and_check_digits([]) == {[], nil}
end
test "handles single digit" do
assert Commons.value_and_check_digits([1]) == {[], 1}
end
end
describe "ok/1" do
test "wraps data in ok tuple" do
assert Commons.ok(123) == {:ok, 123}
assert Commons.ok([1, 2, 3]) == {:ok, [1, 2, 3]}
assert Commons.ok("test") == {:ok, "test"}
end
end
describe "error/1" do
test "wraps error in error tuple" do
assert Commons.error("invalid") == {:error, "invalid"}
assert Commons.error(:invalid_format) == {:error, :invalid_format}
end
end
end

View File

@@ -0,0 +1,66 @@
defmodule UkraineTaxidEx.Edrpou.CheckSumTest do
use ExUnit.Case
alias UkraineTaxidEx.Edrpou.CheckSum
describe "check_sum/1" do
test "calculates correct checksum for EDRPOU with base weights (< 30M) short EDRPOU with leading zeros" do
# Example EDRPOU: 0003212[9] (where 2 is the check digit)
digits = [0, 0, 0, 3, 2, 1, 2, 9]
assert CheckSum.check_sum(digits) == 9
end
test "calculates correct checksum for EDRPOU with base weights (< 30M)" do
# Example EDRPOU: 1436050[6] (where 2 is the check digit)
digits = [1, 4, 3, 6, 0, 5, 0, 6]
assert CheckSum.check_sum(digits) == 6
end
test "calculates correct checksum for EDRPOU with base weights (> 60M)" do
# Example EDRPOU: 6543217[6]
digits = [6, 5, 4, 3, 2, 1, 7, 6]
assert CheckSum.check_sum(digits) == 6
end
test "calculates correct checksum for EDRPOU with alternative weights (30M-60M)" do
# Example EDRPOU: 3145193[2]
digits = [3, 1, 4, 5, 1, 9, 3, 2]
assert CheckSum.check_sum(digits) == 2
end
test "calculates correct checksum for EDRPOU with alternative weights (30M-60M) when first calculation >= 10" do
# Example EDRPOU: 3702668[4]
digits = [3, 7, 0, 2, 6, 6, 8, 4]
assert CheckSum.check_sum(digits) == 4
end
test "calculates correct checksum for EDRPOU with base weights when first calculation >= 10" do
# Example EDRPOU: 2113335[2]
digits = [2, 1, 1, 3, 3, 3, 5, 2]
assert CheckSum.check_sum(digits) == 2
end
test "handles edge case at 30M boundary" do
# Just below 30M
digits = [2, 9, 9, 9, 9, 9, 9]
result_below = CheckSum.check_sum(digits)
# Just at 30M
digits = [3, 0, 0, 0, 0, 0, 0]
result_at = CheckSum.check_sum(digits)
assert result_below != result_at
end
test "handles edge case at 60M boundary" do
# Just below 60M
digits = [5, 9, 9, 9, 9, 9, 9]
result_below = CheckSum.check_sum(digits)
# Just at 60M
digits = [6, 0, 0, 0, 0, 0, 0]
result_at = CheckSum.check_sum(digits)
assert result_below != result_at
end
end
end

View File

@@ -0,0 +1,26 @@
defmodule UkraineTaxidEx.EdrpouTest do
use ExUnit.Case
# alias UkraineTaxidEx.Edrpou
# describe "weights/2" do
# test "returns base weights by default" do
# assert Edrpou.weights() == [1, 2, 3, 4, 5, 6, 7]
# end
# test "returns base weights when not doubled" do
# assert Edrpou.weights(:base, false) == [1, 2, 3, 4, 5, 6, 7]
# end
# test "returns alternative weights when not doubled" do
# assert Edrpou.weights(:alternative, false) == [7, 1, 2, 3, 4, 5, 6]
# end
# test "returns doubled base weights" do
# assert Edrpou.weights(:base, true) == [2, 4, 6, 8, 10, 12, 14]
# end
# test "returns doubled alternative weights" do
# assert Edrpou.weights(:alternative, true) == [14, 2, 4, 6, 8, 10, 12]
# end
# end
end