frostfs-node/pkg/innerring/processors/netmap/nodevalidation/locode/calls_test.go
George Bartolomey 9c2c76ca32
All checks were successful
Vulncheck / Vulncheck (pull_request) Successful in 4m47s
DCO action / DCO (pull_request) Successful in 4m40s
Build / Build Components (1.21) (pull_request) Successful in 7m27s
Tests and linters / Staticcheck (pull_request) Successful in 8m22s
Tests and linters / gopls check (pull_request) Successful in 10m56s
Build / Build Components (1.22) (pull_request) Successful in 11m48s
Tests and linters / Tests (1.22) (pull_request) Successful in 16m11s
Tests and linters / Lint (pull_request) Successful in 16m27s
Pre-commit hooks / Pre-commit (pull_request) Successful in 19m12s
Tests and linters / Tests (1.21) (pull_request) Successful in 20m31s
Tests and linters / Tests with -race (pull_request) Successful in 8m56s
[#1259] Move pkg/util/locode to frostfs-locode-db
Removed pkg/util/locode package, added
git.frostfs.info/TrueCloudLab/frostfs-locode-db/pkg/locode dependency.

Signed-off-by: George Bartolomey <george@bh4.ru>
2024-07-23 14:59:14 +03:00

132 lines
2.9 KiB
Go

package locode_test
import (
"errors"
"fmt"
"testing"
locodestd "git.frostfs.info/TrueCloudLab/frostfs-locode-db/pkg/locode"
locodedb "git.frostfs.info/TrueCloudLab/frostfs-locode-db/pkg/locode/db"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/processors/netmap/nodevalidation/locode"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
"github.com/stretchr/testify/require"
)
type record struct {
*locodedb.Key
*locodedb.Record
}
type db struct {
items map[locodestd.LOCODE]locode.Record
}
func (x db) add(lc locodestd.LOCODE, rec locode.Record) {
x.items[lc] = rec
}
func (x db) Get(lc *locodestd.LOCODE) (locode.Record, error) {
r, ok := x.items[*lc]
if !ok {
return nil, errors.New("record not found")
}
return r, nil
}
func addLocodeAttrValue(n *netmap.NodeInfo, val string) {
n.SetLOCODE(val)
}
func addLocodeAttr(n *netmap.NodeInfo, lc locodestd.LOCODE) {
n.SetLOCODE(fmt.Sprintf("%s %s", lc[0], lc[1]))
}
func nodeInfoWithSomeAttrs() *netmap.NodeInfo {
var n netmap.NodeInfo
n.SetAttribute("key1", "val1")
n.SetAttribute("key2", "val2")
return &n
}
func TestValidator_VerifyAndUpdate(t *testing.T) {
db := &db{
items: make(map[locodestd.LOCODE]locode.Record),
}
// test record with valid but random values
r := locodestd.Record{
LOCODE: locodestd.LOCODE{"RU", "MOW"},
NameWoDiacritics: "Moskva",
SubDiv: "MSK",
}
k, err := locodedb.NewKey(r.LOCODE)
require.NoError(t, err)
rdb, err := locodedb.NewRecord(r)
require.NoError(t, err)
rdb.SetCountryName("Russia")
rdb.SetSubDivName("Moskva oblast")
var cont locodedb.Continent = locodedb.ContinentEurope
rdb.SetContinent(&cont)
rec := record{
Key: k,
Record: rdb,
}
db.add(r.LOCODE, rec)
var p locode.Prm
p.DB = db
validator := locode.New(p)
t.Run("w/o locode", func(t *testing.T) {
n := nodeInfoWithSomeAttrs()
err := validator.VerifyAndUpdate(n)
require.NoError(t, err)
})
t.Run("w/ locode", func(t *testing.T) {
t.Run("invalid locode", func(t *testing.T) {
n := nodeInfoWithSomeAttrs()
addLocodeAttrValue(n, "WRONG LOCODE")
err := validator.VerifyAndUpdate(n)
require.Error(t, err)
})
t.Run("missing DB record", func(t *testing.T) {
n := nodeInfoWithSomeAttrs()
addLocodeAttr(n, locodestd.LOCODE{"RU", "SPB"})
err := validator.VerifyAndUpdate(n)
require.Error(t, err)
})
n := nodeInfoWithSomeAttrs()
addLocodeAttr(n, r.LOCODE)
err := validator.VerifyAndUpdate(n)
require.NoError(t, err)
require.Equal(t, rec.CountryCode().String(), n.Attribute("CountryCode"))
require.Equal(t, rec.CountryName(), n.Attribute("Country"))
require.Equal(t, rec.LocationName(), n.Attribute("Location"))
require.Equal(t, rec.SubDivCode(), n.Attribute("SubDivCode"))
require.Equal(t, rec.SubDivName(), n.Attribute("SubDiv"))
require.Equal(t, rec.Continent().String(), n.Attribute("Continent"))
})
}