Skip to content

Commit c33c04c

Browse files
committed
chore: update package name
1 parent adeeb39 commit c33c04c

File tree

3 files changed

+187
-26
lines changed

3 files changed

+187
-26
lines changed

errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package go_codes_validator
1+
package ru_doc_code
22

33
import "errors"
44

validator.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package go_codes_validator
1+
package ru_doc_code
22

33
import (
44
"strconv"
@@ -18,8 +18,8 @@ func strToArr(str string) ([]int, error) {
1818
return arr, nil
1919
}
2020

21-
// IsINNValid
22-
// example valid format is
21+
// IsINNValid check to valid INN format
22+
// example valid format is `7707083893`
2323
func IsINNValid(inn string) (bool, error) {
2424
if len(inn) != 10 && len(inn) != 12 {
2525
return false, ErrInvalidINNLength
@@ -37,8 +37,8 @@ func IsINNValid(inn string) (bool, error) {
3737
return firstControlNumber == innArr[len(innArr)-2] && secondControlNumber == innArr[len(innArr)-1], nil
3838
}
3939

40-
// IsBIKValid
41-
// example valid format is
40+
// IsBIKValid check to valid BIK format
41+
// example valid format is `044525225`
4242
func IsBIKValid(bik string) (bool, error) {
4343
if len(bik) != 9 {
4444
return false, ErrInvalidBIKLength
@@ -59,7 +59,7 @@ func IsBIKValid(bik string) (bool, error) {
5959
return code >= 50 && code < 1000, nil
6060
}
6161

62-
// IsOGRNValid
62+
// IsOGRNValid check to valid OGRN format
6363
// example valid format is
6464
func IsOGRNValid(ogrn string) (bool, error) {
6565
if len(ogrn) != 13 {
@@ -74,7 +74,7 @@ func IsOGRNValid(ogrn string) (bool, error) {
7474
}
7575

7676
// IsOGRNIPValid check to valid OGRNIP format
77-
// example valid format is
77+
// example valid format is `304500116000157`
7878
func IsOGRNIPValid(ogrnip string) (bool, error) {
7979
if len(ogrnip) != 15 {
8080
return false, ErrInvalidOGRNIPLength
@@ -86,11 +86,11 @@ func IsOGRNIPValid(ogrnip string) (bool, error) {
8686
if ogrnipArr[0] != 3 && ogrnipArr[0] != 4 {
8787
return false, ErrInvalidValue
8888
}
89-
code, _ := strconv.Atoi(ogrnip[:12])
89+
code, _ := strconv.Atoi(ogrnip[:len(ogrnip)-1])
9090
return ogrnipArr[len(ogrnip)-1] == code%13%10, nil
9191
}
9292

93-
// IsSNILSValid check
93+
// IsSNILSValid check to valid SNILS format
9494
// example valid format is `112-233-445 95`
9595
func IsSNILSValid(snils string) (bool, error) {
9696
if len(snils) != 14 {
@@ -106,19 +106,23 @@ func IsSNILSValid(snils string) (bool, error) {
106106
return false, err
107107
}
108108
hashSum := 0
109-
hashLen := len(snilsArr) - 2
110-
code, _ := strconv.Atoi(snils[hashLen:])
109+
hashLen := len(fSnils) - 2
110+
code, _ := strconv.Atoi(fSnils[hashLen:])
111111
for i, v := range snilsArr[:hashLen] {
112112
hashSum += v * (hashLen - i)
113113
}
114114
return hashSum%101 == code, nil
115115
}
116116

117-
// IsKPPValid
118-
// example valid format is
117+
// IsKPPValid check to valid KPP format
118+
// example valid format is `773643301`
119119
func IsKPPValid(kpp string) (bool, error) {
120-
if len(kpp) == 9 {
121-
return true, nil
120+
if len(kpp) != 9 {
121+
return false, ErrInvalidKPPLength
122+
}
123+
_, err := strToArr(kpp)
124+
if err != nil {
125+
return false, err
122126
}
123-
return false, ErrInvalidKPPLength
127+
return true, nil
124128
}

validator_test.go

Lines changed: 166 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package go_codes_validator
1+
package ru_doc_code
22

33
import (
44
"errors"
@@ -41,7 +41,7 @@ func TestIsBIKValid(t *testing.T) {
4141
}
4242
for _, test := range testCases {
4343
isValid, err := IsBIKValid(test.Code)
44-
assert.Equal(t, isValid, test.IsValid, test.Code)
44+
assert.Equal(t, test.IsValid, isValid, test.Code)
4545
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
4646
}
4747
})
@@ -81,7 +81,7 @@ func TestIsBIKValid(t *testing.T) {
8181
}
8282
for _, test := range testCases {
8383
isValid, err := IsBIKValid(test.Code)
84-
assert.Equal(t, isValid, test.IsValid, test.Code, test.IsValid)
84+
assert.Equal(t, test.IsValid, isValid, test.Code, test.IsValid)
8585
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
8686
}
8787
})
@@ -115,7 +115,7 @@ func TestIsINNValid(t *testing.T) {
115115
}
116116
for _, test := range testCases {
117117
isValid, err := IsINNValid(test.Code)
118-
assert.Equal(t, isValid, test.IsValid, test.Code)
118+
assert.Equal(t, test.IsValid, isValid, test.Code)
119119
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
120120
}
121121
})
@@ -150,7 +150,7 @@ func TestIsINNValid(t *testing.T) {
150150
}
151151
for _, test := range testCases {
152152
isValid, err := IsINNValid(test.Code)
153-
assert.Equal(t, isValid, test.IsValid, test.Code)
153+
assert.Equal(t, test.IsValid, isValid, test.Code)
154154
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
155155
}
156156
})
@@ -184,7 +184,7 @@ func TestValidateOGRN(t *testing.T) {
184184
}
185185
for _, test := range testCases {
186186
isValid, err := IsOGRNValid(test.Code)
187-
assert.Equal(t, isValid, test.IsValid, test.Code)
187+
assert.Equal(t, test.IsValid, isValid, test.Code)
188188
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
189189
}
190190
})
@@ -219,7 +219,7 @@ func TestValidateOGRN(t *testing.T) {
219219
}
220220
for _, test := range testCases {
221221
isValid, err := IsOGRNValid(test.Code)
222-
assert.Equal(t, isValid, test.IsValid, test.Code, test.IsValid)
222+
assert.Equal(t, test.IsValid, isValid, test.Code, test.IsValid)
223223
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
224224
}
225225
})
@@ -228,16 +228,143 @@ func TestValidateOGRN(t *testing.T) {
228228
func TestValidateOGRNIP(t *testing.T) {
229229
t.Parallel()
230230

231-
t.Run("", func(t *testing.T) {
231+
t.Run("invalid ogrnip length", func(t *testing.T) {
232+
testCases := []TestCodeCase{
233+
TestCodeCase{
234+
Code: "304500116000157",
235+
Error: nil,
236+
IsValid: true,
237+
},
238+
TestCodeCase{
239+
Code: "312502904600034",
240+
Error: nil,
241+
IsValid: true,
242+
},
243+
TestCodeCase{
244+
Code: "31250290460",
245+
Error: ErrInvalidOGRNIPLength,
246+
IsValid: false,
247+
},
248+
TestCodeCase{
249+
Code: "3045001111236000157",
250+
Error: ErrInvalidOGRNIPLength,
251+
IsValid: false,
252+
},
253+
}
254+
for _, test := range testCases {
255+
isValid, err := IsOGRNIPValid(test.Code)
256+
assert.Equal(t, test.IsValid, isValid, test.Code)
257+
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
258+
}
259+
})
232260

261+
t.Run("invalid ogrnip value", func(t *testing.T) {
262+
testCases := []TestCodeCase{
263+
TestCodeCase{
264+
Code: "312502??4600034",
265+
Error: ErrInvalidValue,
266+
IsValid: false,
267+
},
268+
TestCodeCase{
269+
Code: "304500116000158",
270+
Error: nil,
271+
IsValid: false,
272+
},
273+
TestCodeCase{
274+
Code: "512502904600034",
275+
Error: ErrInvalidValue,
276+
IsValid: false,
277+
},
278+
TestCodeCase{
279+
Code: "304500116000157",
280+
Error: nil,
281+
IsValid: true,
282+
},
283+
TestCodeCase{
284+
Code: "312502904600034",
285+
Error: nil,
286+
IsValid: true,
287+
},
288+
}
289+
for _, test := range testCases {
290+
isValid, err := IsOGRNIPValid(test.Code)
291+
assert.Equal(t, test.IsValid, isValid, test.Code, test.IsValid)
292+
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
293+
}
233294
})
234295
}
235296

236297
func TestValidateSNILS(t *testing.T) {
237298
t.Parallel()
238299

239-
t.Run("", func(t *testing.T) {
300+
t.Run("invalid snils length", func(t *testing.T) {
301+
testCases := []TestCodeCase{
302+
TestCodeCase{
303+
Code: "112-233-445 95",
304+
Error: nil,
305+
IsValid: true,
306+
},
307+
TestCodeCase{
308+
Code: "646-663-083 23",
309+
Error: nil,
310+
IsValid: true,
311+
},
312+
TestCodeCase{
313+
Code: "112-233-445 951213",
314+
Error: ErrInvalidSNILSLength,
315+
IsValid: false,
316+
},
317+
TestCodeCase{
318+
Code: "112-233 95",
319+
Error: ErrInvalidSNILSLength,
320+
IsValid: false,
321+
},
322+
}
323+
for _, test := range testCases {
324+
isValid, err := IsSNILSValid(test.Code)
325+
assert.Equal(t, test.IsValid, isValid, test.Code)
326+
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
327+
}
328+
})
240329

330+
t.Run("invalid snils value", func(t *testing.T) {
331+
testCases := []TestCodeCase{
332+
TestCodeCase{
333+
Code: "112-233?445 95",
334+
Error: ErrInvalidFormattedSNILSLength,
335+
IsValid: false,
336+
},
337+
TestCodeCase{
338+
Code: "1M2-234-445 95",
339+
Error: ErrInvalidValue,
340+
IsValid: false,
341+
},
342+
TestCodeCase{
343+
Code: "112-233-445 98",
344+
Error: nil,
345+
IsValid: false,
346+
},
347+
TestCodeCase{
348+
Code: "112-233-445#95",
349+
Error: ErrInvalidFormattedSNILSLength,
350+
IsValid: false,
351+
},
352+
TestCodeCase{
353+
Code: "112-233-445 95",
354+
Error: nil,
355+
IsValid: true,
356+
},
357+
TestCodeCase{
358+
Code: "646-663-083 23",
359+
Error: nil,
360+
IsValid: true,
361+
},
362+
}
363+
for _, test := range testCases {
364+
isValid, err := IsSNILSValid(test.Code)
365+
assert.Equal(t, test.IsValid, isValid, test.Code, test.IsValid)
366+
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
367+
}
241368
})
242369
}
243370

@@ -273,4 +400,34 @@ func TestValidateKPP(t *testing.T) {
273400
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
274401
}
275402
})
403+
404+
t.Run("invalid kpp value", func(t *testing.T) {
405+
testCases := []TestCodeCase{
406+
TestCodeCase{
407+
Code: "773$N3301",
408+
Error: ErrInvalidValue,
409+
IsValid: false,
410+
},
411+
TestCodeCase{
412+
Code: "7736#3&01",
413+
Error: ErrInvalidValue,
414+
IsValid: false,
415+
},
416+
TestCodeCase{
417+
Code: "773643301",
418+
Error: nil,
419+
IsValid: true,
420+
},
421+
TestCodeCase{
422+
Code: "773643001",
423+
Error: nil,
424+
IsValid: true,
425+
},
426+
}
427+
for _, test := range testCases {
428+
isValid, err := IsKPPValid(test.Code)
429+
assert.Equal(t, isValid, test.IsValid, test.Code)
430+
assert.Equal(t, true, errors.Is(test.Error, err), test.Code)
431+
}
432+
})
276433
}

0 commit comments

Comments
 (0)