EDRPOU parser fixes

This commit is contained in:
Danil Negrienko 2024-12-12 12:05:18 -05:00
parent f9a45fb29d
commit 0edc3c3139
4 changed files with 52 additions and 30 deletions

View File

@ -36,16 +36,39 @@ defmodule UkraineTaxidEx.Edrpou.CheckSum do
defguardp is_base_weights(value) when value < 30_000_000 or value > 60_000_000
@spec weights(type :: weights_type, double_added? :: boolean()) :: C.digits()
defp weights(type \\ :base, double_added \\ false)
defp weights(:base, false), do: Enum.to_list(1..7)
@doc """
Returns a list of weights used for EDRPOU check sum calculation.
When double_added is true, adds 2 to each weight in the list.
defp weights(:alternative, false) do
## Parameters
- type: `:base` or `:alternative` weights pattern
- double_added: when true, adds 2 to each weight
## Examples
```elixir
iex> weights(:base)
[1, 2, 3, 4, 5, 6, 7]
iex> weights(:alternative)
[7, 1, 2, 3, 4, 5, 6]
iex> weights(:base, true)
[3, 4, 5, 6, 7, 8, 9]
iex> weights(:alternative, true)
[9, 3, 4, 5, 6, 7, 8]
```
"""
@spec weights(type :: weights_type, double_added? :: boolean()) :: C.digits()
def weights(type \\ :base, double_added \\ false)
def weights(:base, false), do: Enum.to_list(1..7)
def weights(:alternative, false) do
base = weights()
[List.last(base) | Enum.take(base, length(base) - 1)]
end
defp weights(type, true) do
def weights(type, true) do
type
|> weights(false)
|> Enum.map(&(&1 + 2))

View File

@ -21,13 +21,12 @@ defmodule UkraineTaxidEx.Edrpou.Parser do
| {:error,
:length_too_short
| :length_too_long
| :invalid_length
| :invalid_checksum}
@impl BaseParser
@doc """
Parses an EDRPOU code string into a structured format.
Parses an EDRPOU code string into a structured format (clean and normalize, validate and decompose).
Options:
- normalize?: When true, pads string to full EDRPOU length. Defaults to false.
- clean?: When true, removes non-digit characters before processing. Defaults to false.

View File

@ -1,6 +1,29 @@
defmodule UkraineTaxidEx.Edrpou.CheckSumTest do
use ExUnit.Case
alias UkraineTaxidEx.Edrpou.CheckSum
doctest UkraineTaxidEx.Edrpou.Parser
describe "weights/2" do
test "returns base weights by default" do
assert CheckSum.weights() == [1, 2, 3, 4, 5, 6, 7]
end
test "returns base weights when not added 2" do
assert CheckSum.weights(:base, false) == [1, 2, 3, 4, 5, 6, 7]
end
test "returns alternative weights when added 2" do
assert CheckSum.weights(:alternative, false) == [7, 1, 2, 3, 4, 5, 6]
end
test "returns base weights + 2" do
assert CheckSum.weights(:base, true) == [3, 4, 5, 6, 7, 8, 9]
end
test "returns alternative weights + 2 " do
assert CheckSum.weights(:alternative, true) == [9, 3, 4, 5, 6, 7, 8]
end
end
describe "check_sum/1" do
test "calculates correct checksum for EDRPOU with base weights (< 30M) short EDRPOU with leading zeros" do

View File

@ -1,26 +1,3 @@
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