Skip to content

Commit 8fc9a1c

Browse files
authored
Merge pull request #10 from sshaplygin/ref-bik
ref BIK package
2 parents 0410fcd + 244c906 commit 8fc9a1c

File tree

25 files changed

+2666
-80
lines changed

25 files changed

+2666
-80
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ jobs:
4343
skip-cache: true
4444
skip-pkg-cache: true
4545
skip-build-cache: true
46+
args: --timeout 3m --verbose
4647

4748
- name: Test
4849
run: go test -v ./...

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88
# Test binary, built with `go test -c`
99
*.test
1010
*.xml
11+
*.csv
12+
*.html
13+
14+
.DS_Store
1115

1216
# Output of the go coverage tool, specifically when used with LiteIDE
1317
*.out
18+
output.*
1419

1520
# Dependency directories (remove the comment below to include it)
1621
# vendor/

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ It is not production ready public API! It is API could be change it the future.
1010

1111
## Status
1212

13-
Status of implementation by code package
13+
Status of implementation by code package:
1414

1515
- [ ] BIK
16-
- [x] Generation method
17-
- [ ] Validation method
16+
- [ ] Generation method
17+
- [x] Validation method
1818
- [x] INN
1919
- [x] Generation method
2020
- [x] Validation method
2121
- [ ] KPP
22-
- [x] Generation method
23-
- [ ] Validation method
24-
- [ ] OGRN
22+
- [] Generation method
23+
- [x] Validation method
24+
- [x] OGRN
2525
- [x] Generation method
2626
- [ ] Validation method
2727
- [ ] OGRNIP
@@ -34,10 +34,13 @@ Status of implementation by code package
3434
- [ ] Generation method
3535
- [ ] Validation method
3636
- [ ] SNILS
37-
- [x] Generation method
38-
- [ ] Validation method
37+
- [ ] Generation method
38+
- [x] Validation method
3939
- [ ] Swift
40-
- [x] Generation method
40+
- [ ] Generation method
41+
- [ ] Validation method
42+
- [ ] KS
43+
- [ ] Generation method
4144
- [ ] Validation method
4245

4346
## Usage

bik/bik.go

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,47 @@
11
package bik
22

33
import (
4-
"strconv"
5-
6-
"github.com/sshaplygin/docs-code/models"
7-
"github.com/sshaplygin/docs-code/utils"
4+
"fmt"
85
)
96

10-
// Validate check to valid BIK format
11-
// example valid format is 044525225
7+
// Validate check to valid BIK format.
8+
// Example valid format is 044525225
129
func Validate(bik string) (bool, error) {
13-
if len(bik) != 9 {
14-
return false, &models.CommonError{
15-
Method: packageName,
16-
Err: models.ErrInvalidLength,
17-
}
10+
bikData, err := ParseBIK(bik)
11+
if err != nil {
12+
return false, fmt.Errorf("create %s model: %w", packageName, err)
1813
}
1914

20-
bikArr, err := utils.StrToArr(bik)
21-
if err != nil {
22-
return false, err
15+
return bikData.IsValid()
16+
}
17+
18+
// Exists check to valid BIK format and check to used code.
19+
// Example valid format is 044525677 - АО "Яндекс Банк".
20+
func Exists(bik string) (bool, error) {
21+
_, ok := existsBIKs[bik]
22+
if ok {
23+
return true, nil
2324
}
2425

25-
if bikArr[0] != 0 || bikArr[1] != 4 {
26-
return false, ErrInvalidCountryCode
26+
bikData, err := ParseBIK(bik)
27+
if err != nil {
28+
return false, fmt.Errorf("create %s model: %w", packageName, err)
2729
}
2830

29-
// special code
30-
if bikArr[6] == 0 && bikArr[7] == 1 && bikArr[8] == 2 {
31-
return true, nil
31+
isValid, err := bikData.IsValid()
32+
if err != nil {
33+
return false, fmt.Errorf("check valid %s model: %w", packageName, err)
3234
}
3335

34-
latestTriadStr := bik[6:]
35-
code, _ := strconv.Atoi(latestTriadStr)
36+
if !isValid {
37+
return false, fmt.Errorf("invalid %s model", packageName)
38+
}
3639

37-
return code >= 50 && code < 1000, nil
40+
return bikData.Exists()
3841
}
3942

40-
func Generate() string {
41-
panic("not implemented!")
43+
// Generate method generate a valid BIK code, but possible usaged or not usaged in reality.
44+
// Method guaranteed that code will be valid, but not guaranteed that code will be exists.
45+
func Generate(opts ...GenerateOpt) string {
46+
return NewBIK(opts...).String()
4247
}

bik/bik_test.go

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,19 @@ func TestValidate(t *testing.T) {
2222
t.Run("invalid bik length", func(t *testing.T) {
2323
testCases := []testCase{
2424
{
25-
Code: "1234567888776",
26-
Error: models.ErrInvalidLength,
27-
IsValid: false,
25+
Code: "1234567888776",
26+
Error: models.ErrInvalidLength,
2827
},
2928
{
30-
Code: "044525",
31-
Error: models.ErrInvalidLength,
32-
IsValid: false,
29+
Code: "044525",
30+
Error: models.ErrInvalidLength,
3331
},
3432
{
3533
Code: "044525225",
36-
Error: nil,
3734
IsValid: true,
3835
},
3936
{
4037
Code: "044525012",
41-
Error: nil,
4238
IsValid: true,
4339
},
4440
}
@@ -47,12 +43,13 @@ func TestValidate(t *testing.T) {
4743
tc := tc
4844

4945
isValid, err := Validate(tc.Code)
50-
assert.Equal(t, tc.IsValid, isValid, tc.Code)
5146
if err != nil {
52-
assert.ErrorAs(t, err, &tc.Error, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
47+
require.ErrorAs(t, err, &tc.Error, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
5348
} else {
54-
assert.NoError(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
49+
require.NoError(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
5550
}
51+
52+
assert.Equal(t, tc.IsValid, isValid, tc.Code)
5653
}
5754
})
5855

@@ -65,52 +62,56 @@ func TestValidate(t *testing.T) {
6562

6663
testCases := []testCase{
6764
{
68-
Code: "0445?5226",
69-
Error: models.ErrInvalidValue,
70-
IsValid: false,
65+
Code: "0445?5226",
66+
Error: models.ErrInvalidValue,
7167
},
7268
{
73-
Code: "054525225",
74-
Error: ErrInvalidCountryCode,
75-
IsValid: false,
69+
Code: "054525225",
70+
Error: ErrInvalidCountryCode,
7671
},
7772
{
78-
Code: "104525225",
79-
Error: ErrInvalidCountryCode,
80-
IsValid: false,
73+
Code: "104525225",
74+
Error: ErrInvalidCountryCode,
8175
},
8276
{
83-
Code: "044#55#25",
84-
Error: models.ErrInvalidValue,
85-
IsValid: false,
77+
Code: "044#55#25",
78+
Error: models.ErrInvalidValue,
8679
},
8780
{
8881
Code: "044525225",
89-
Error: nil,
9082
IsValid: true,
9183
},
9284
{
9385
Code: "044525012",
94-
Error: nil,
9586
IsValid: true,
9687
},
9788
}
9889
for i, tc := range testCases {
9990
tc := tc
10091

10192
isValid, err := Validate(tc.Code)
102-
assert.Equal(t, tc.IsValid, isValid, tc.Code, tc.IsValid)
10393
if err != nil {
104-
assert.ErrorAs(t, err, &tc.Error, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
94+
require.ErrorAs(t, err, &tc.Error, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
10595
} else {
106-
assert.Empty(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
96+
require.Empty(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
10797
}
98+
99+
assert.Equal(t, tc.IsValid, isValid, tc.Code, tc.IsValid)
108100
}
109101
})
110102
}
111103

112104
func Test_Generate(t *testing.T) {
113-
require.Panics(t, func() {
114-
Generate()
115-
})
105+
bik := Generate()
106+
isValid, err := Validate(bik)
107+
108+
require.NoError(t, err, fmt.Sprintf("invalid bik value: %s", bik))
109+
require.True(t, isValid)
110+
}
111+
112+
func Test_Exists(t *testing.T) {
113+
is, err := Exists("044525677") // АО "Яндекс Банк".
114+
require.NoError(t, err)
115+
116+
assert.True(t, is)
116117
}

0 commit comments

Comments
 (0)