CODE_CONVENTIONS.md sets out the conventions for Go code in this repository: naming, layout, comments, errors, logging, configuration and tests. .golangci.json enables the linters that check the parts that can be checked (each limited to the rules the conventions use); the rest is applied by hand. Applied across the codebase: - Naming: full-word names in application code, initialisms in one case (BuyerID, PrintJSON, PrintYAML, ocdsGetBuyerID), no names shadowing built-ins (min/max are minGap/maxGap), mustRaw renamed rawJSON as it returns nil rather than panicking, and config.ConfigLevelToLogLevel renamed LevelToLogLevel. - Layout: lines kept to 80 characters where possible, assignments taken out of if and for statements that don't fit, wrapped arguments all on one line or one per line, steps separated by blank lines, no else after a return, function and struct literals wrapped. - Comments: doc comments on every exported name (including each section's Map, Flatten, TypeOf, ValueOf and SetValue), section banners, British spelling. - Errors: a created error starts with its package name, a wrapping error adds only what was being done, sentinels in one commented var block, errors.Is and errors.As in place of == and type assertions, and no err := shadowing of a named result. That fixes the mock server losing its access log's close error. - Logging: the fetcher passes its workers an untagged logger, so findatender's lines are tagged with their own component rather than with fetcher's as well; cmd/mock-fts logs "stopped" with slog. - Configuration: util.WithTimeout (moved from the websocket package) applies a timeout of 0 or less as no limit, used for the websocket write and ping, TCP close and tcpserver shutdown timeouts; every setting documents what 0 or empty means, including the rate limit rule's keys in internal/config/README.md. - Tests: related tests merged into table-driven ones with named cases. Separately from CODE_CONVENTIONS.md, internal/store/README.md gains a soft SQL style convention for the queries in internal/store/queries.go, and the queries are reformatted to follow it.
150 lines
4.3 KiB
Go
150 lines
4.3 KiB
Go
package mock
|
|
|
|
import (
|
|
"math/rand"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// loadTestCatalog parses the small fixture catalog used by the tests.
|
|
func loadTestCatalog(t *testing.T) *Catalog {
|
|
t.Helper()
|
|
b, err := os.ReadFile("testdata/catalog.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cat, err := parseCatalog(b, "testdata/catalog.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return cat
|
|
}
|
|
|
|
// The catalog compiled into the build must load: a bad data/catalog.json
|
|
// fails here rather than when the mock starts.
|
|
func TestEmbeddedCatalogIsValid(t *testing.T) {
|
|
if len(embeddedCatalog) == 0 {
|
|
t.Fatal("data/catalog.json was not embedded")
|
|
}
|
|
cat, err := LoadCatalog()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(cat.Buyers) == 0 || len(cat.Suppliers) == 0 ||
|
|
len(cat.Subjects) == 0 || len(cat.Methods) == 0 {
|
|
t.Fatalf(
|
|
"embedded catalog has an empty pool: %d buyers, %d suppliers, %d subjects, %d methods",
|
|
len(cat.Buyers),
|
|
len(cat.Suppliers),
|
|
len(cat.Subjects),
|
|
len(cat.Methods),
|
|
)
|
|
}
|
|
}
|
|
|
|
func TestParseCatalog(t *testing.T) {
|
|
cat := loadTestCatalog(t)
|
|
if len(cat.Buyers) != 2 || len(cat.Suppliers) != 1 ||
|
|
len(cat.Subjects) != 2 || len(cat.Methods) != 2 {
|
|
t.Fatalf(
|
|
"got %d buyers, %d suppliers, %d subjects, %d methods",
|
|
len(cat.Buyers),
|
|
len(cat.Suppliers),
|
|
len(cat.Subjects),
|
|
len(cat.Methods),
|
|
)
|
|
}
|
|
|
|
if d := cat.Buyers[0].Details; d == nil || len(d.Classifications) != 1 ||
|
|
d.Classifications[0].Scheme != "UK_CA_TYPE" {
|
|
t.Fatalf("buyer details.classifications not read: %+v", d)
|
|
}
|
|
|
|
s := cat.Subjects[0]
|
|
if s.Classification != "CPV-77314000" || len(s.Items) != 1 {
|
|
t.Fatalf("subject classification key/items not read: %+v", s)
|
|
}
|
|
if ac := s.Items[0].AdditionalClassifications; len(ac) != 1 ||
|
|
ac[0] != "CPV-77310000" {
|
|
t.Fatalf("items.additionalClassifications keys not read: %+v", ac)
|
|
}
|
|
if v := s.Lots[0].Value; v == nil || v.AmountGross == nil ||
|
|
*v.AmountGross != 120000 {
|
|
t.Fatalf("lot value not read: %+v", v)
|
|
}
|
|
}
|
|
|
|
// Picking a subject resolves its classification keys to full classifications.
|
|
func TestSubjectResolvesClassifications(t *testing.T) {
|
|
cat := loadTestCatalog(t)
|
|
//nolint:gosec // G404: seeded math/rand keeps the test deterministic
|
|
r := rand.New(rand.NewSource(1))
|
|
for i := 0; i < 20; i++ {
|
|
tender := cat.subject(r)
|
|
if tender.Title != "Grounds maintenance services" {
|
|
continue
|
|
}
|
|
if cl := tender.Classification; cl == nil || cl.ID != "77314000" ||
|
|
cl.Description != "Grounds maintenance services" {
|
|
t.Fatalf("main classification = %+v", cl)
|
|
}
|
|
ac := tender.Items[0].AdditionalClassifications
|
|
if len(ac) != 1 ||
|
|
ac[0].Description != "Planting and maintenance services of green areas" {
|
|
t.Fatalf("item classifications = %+v", ac)
|
|
}
|
|
// The catalog itself still holds keys, untouched by picking.
|
|
if cat.Subjects[0].Classification != "CPV-77314000" {
|
|
t.Fatal("picking modified the catalog")
|
|
}
|
|
return
|
|
}
|
|
t.Fatal("never picked the grounds maintenance subject")
|
|
}
|
|
|
|
func TestParseCatalogRejectsInvalidCatalogs(t *testing.T) {
|
|
cases := map[string]struct {
|
|
doc string
|
|
want []string
|
|
}{
|
|
"empty pools": {
|
|
doc: `{"buyers": [], "subjects": [{"title": "T"}], "methods": [], "classifications": {}}`,
|
|
want: []string{"no buyers", "no suppliers", "no methods"},
|
|
},
|
|
"unknown classification keys": {
|
|
doc: `{"buyers": [{"name": "B"}], "suppliers": [{"name": "S"}], "methods": [{"procurementMethod": "open"}],
|
|
"subjects": [{"title": "T", "classification": "CPV-1", "items": [{"id": "1", "additionalClassifications": ["CPV-2"]}]}],
|
|
"classifications": {}}`,
|
|
want: []string{"2 classification key(s) not found", "CPV-1", "CPV-2"},
|
|
},
|
|
"not JSON": {doc: `{`, want: []string{"decoding"}},
|
|
}
|
|
for name, tc := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
_, err := parseCatalog([]byte(tc.doc), "test catalog")
|
|
if err == nil {
|
|
t.Fatal("expected an error")
|
|
}
|
|
for _, w := range tc.want {
|
|
if !strings.Contains(err.Error(), w) {
|
|
t.Errorf("error %q does not mention %q", err, w)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestResolveOneUnknownKey(t *testing.T) {
|
|
cat := loadTestCatalog(t)
|
|
if cl := cat.resolveOne("CPV-00000000"); cl != nil {
|
|
t.Errorf("unknown key resolved to %+v", cl)
|
|
}
|
|
if cl := cat.resolveOne(""); cl != nil {
|
|
t.Errorf("empty key resolved to %+v", cl)
|
|
}
|
|
if got := cat.resolve([]string{"CPV-00000000"}); got != nil {
|
|
t.Errorf("resolve of unknown keys = %+v", got)
|
|
}
|
|
}
|