Skip to content

Commit 244c906

Browse files
committed
add: parse okato code
1 parent 1ab5915 commit 244c906

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

okato/models.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func ParseOKATO(okato string) (*OKATOStruct, error) {
7373
okatoArr = append(okatoArr, val)
7474
}
7575

76-
if len(okato) != codeLength {
76+
if len(okatoArr) != codeLength {
7777
return nil, &models.CommonError{
7878
Method: packageName,
7979
Err: models.ErrInvalidLength,

okato/okato_test.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,53 @@
11
package okato
22

33
import (
4+
"fmt"
45
"testing"
56

7+
"github.com/stretchr/testify/assert"
68
"github.com/stretchr/testify/require"
9+
10+
"github.com/sshaplygin/docs-code/models"
711
)
812

913
func Test_Validete(t *testing.T) {
10-
require.Panics(t, func() {
11-
_, err := Validate("")
12-
require.NoError(t, err)
13-
})
14+
type testCase struct {
15+
Code string
16+
IsValid bool
17+
Error error
18+
}
19+
20+
testCases := []testCase{
21+
{
22+
Code: "1234567888776",
23+
Error: models.ErrInvalidLength,
24+
},
25+
{
26+
Code: "044525",
27+
Error: models.ErrInvalidLength,
28+
},
29+
{
30+
Code: "17 205 000 000",
31+
IsValid: true,
32+
},
33+
{
34+
Code: "01 201 802 003",
35+
IsValid: true,
36+
},
37+
}
38+
39+
for i, tc := range testCases {
40+
tc := tc
41+
42+
isValid, err := Validate(tc.Code)
43+
if err != nil {
44+
require.ErrorAs(t, err, &tc.Error, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
45+
} else {
46+
require.NoError(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
47+
}
48+
49+
assert.Equal(t, tc.IsValid, isValid, tc.Code)
50+
}
1451
}
1552

1653
func Test_Generate(t *testing.T) {

0 commit comments

Comments
 (0)