Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref BIK package #10

Merged
merged 19 commits into from
Jan 4, 2024
1 change: 1 addition & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
skip-cache: true
skip-pkg-cache: true
skip-build-cache: true
args: --timeout 3m --verbose

- name: Test
run: go test -v ./...
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
# Test binary, built with `go test -c`
*.test
*.xml
*.csv
*.html

.DS_Store

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
output.*

# Dependency directories (remove the comment below to include it)
# vendor/
Expand Down
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ It is not production ready public API! It is API could be change it the future.

## Status

Status of implementation by code package
Status of implementation by code package:

- [ ] BIK
- [x] Generation method
- [ ] Validation method
- [ ] Generation method
- [x] Validation method
- [x] INN
- [x] Generation method
- [x] Validation method
- [ ] KPP
- [x] Generation method
- [ ] Validation method
- [ ] OGRN
- [] Generation method
- [x] Validation method
- [x] OGRN
- [x] Generation method
- [ ] Validation method
- [ ] OGRNIP
Expand All @@ -34,10 +34,13 @@ Status of implementation by code package
- [ ] Generation method
- [ ] Validation method
- [ ] SNILS
- [x] Generation method
- [ ] Validation method
- [ ] Generation method
- [x] Validation method
- [ ] Swift
- [x] Generation method
- [ ] Generation method
- [ ] Validation method
- [ ] KS
- [ ] Generation method
- [ ] Validation method

## Usage
Expand Down
53 changes: 29 additions & 24 deletions bik/bik.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
package bik

import (
"strconv"

"github.com/sshaplygin/docs-code/models"
"github.com/sshaplygin/docs-code/utils"
"fmt"
)

// Validate check to valid BIK format
// example valid format is 044525225
// Validate check to valid BIK format.
// Example valid format is 044525225
func Validate(bik string) (bool, error) {
if len(bik) != 9 {
return false, &models.CommonError{
Method: packageName,
Err: models.ErrInvalidLength,
}
bikData, err := ParseBIK(bik)
if err != nil {
return false, fmt.Errorf("create %s model: %w", packageName, err)
}

bikArr, err := utils.StrToArr(bik)
if err != nil {
return false, err
return bikData.IsValid()
}

// Exists check to valid BIK format and check to used code.
// Example valid format is 044525677 - АО "Яндекс Банк".
func Exists(bik string) (bool, error) {
_, ok := existsBIKs[bik]
if ok {
return true, nil
}

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

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

latestTriadStr := bik[6:]
code, _ := strconv.Atoi(latestTriadStr)
if !isValid {
return false, fmt.Errorf("invalid %s model", packageName)
}

return code >= 50 && code < 1000, nil
return bikData.Exists()
}

func Generate() string {
panic("not implemented!")
// Generate method generate a valid BIK code, but possible usaged or not usaged in reality.
// Method guaranteed that code will be valid, but not guaranteed that code will be exists.
func Generate(opts ...GenerateOpt) string {
return NewBIK(opts...).String()
}
63 changes: 32 additions & 31 deletions bik/bik_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,19 @@ func TestValidate(t *testing.T) {
t.Run("invalid bik length", func(t *testing.T) {
testCases := []testCase{
{
Code: "1234567888776",
Error: models.ErrInvalidLength,
IsValid: false,
Code: "1234567888776",
Error: models.ErrInvalidLength,
},
{
Code: "044525",
Error: models.ErrInvalidLength,
IsValid: false,
Code: "044525",
Error: models.ErrInvalidLength,
},
{
Code: "044525225",
Error: nil,
IsValid: true,
},
{
Code: "044525012",
Error: nil,
IsValid: true,
},
}
Expand All @@ -47,12 +43,13 @@ func TestValidate(t *testing.T) {
tc := tc

isValid, err := Validate(tc.Code)
assert.Equal(t, tc.IsValid, isValid, tc.Code)
if err != nil {
assert.ErrorAs(t, err, &tc.Error, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
require.ErrorAs(t, err, &tc.Error, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
} else {
assert.NoError(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
require.NoError(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
}

assert.Equal(t, tc.IsValid, isValid, tc.Code)
}
})

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

testCases := []testCase{
{
Code: "0445?5226",
Error: models.ErrInvalidValue,
IsValid: false,
Code: "0445?5226",
Error: models.ErrInvalidValue,
},
{
Code: "054525225",
Error: ErrInvalidCountryCode,
IsValid: false,
Code: "054525225",
Error: ErrInvalidCountryCode,
},
{
Code: "104525225",
Error: ErrInvalidCountryCode,
IsValid: false,
Code: "104525225",
Error: ErrInvalidCountryCode,
},
{
Code: "044#55#25",
Error: models.ErrInvalidValue,
IsValid: false,
Code: "044#55#25",
Error: models.ErrInvalidValue,
},
{
Code: "044525225",
Error: nil,
IsValid: true,
},
{
Code: "044525012",
Error: nil,
IsValid: true,
},
}
for i, tc := range testCases {
tc := tc

isValid, err := Validate(tc.Code)
assert.Equal(t, tc.IsValid, isValid, tc.Code, tc.IsValid)
if err != nil {
assert.ErrorAs(t, err, &tc.Error, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
require.ErrorAs(t, err, &tc.Error, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
} else {
assert.Empty(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
require.Empty(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
}

assert.Equal(t, tc.IsValid, isValid, tc.Code, tc.IsValid)
}
})
}

func Test_Generate(t *testing.T) {
require.Panics(t, func() {
Generate()
})
bik := Generate()
isValid, err := Validate(bik)

require.NoError(t, err, fmt.Sprintf("invalid bik value: %s", bik))
require.True(t, isValid)
}

func Test_Exists(t *testing.T) {
is, err := Exists("044525677") // АО "Яндекс Банк".
require.NoError(t, err)

assert.True(t, is)
}
Loading