Naming, layout and comments: descriptive full-word names, one item per line in wrapped calls and literals, and doc comments on exported names
Short and single-letter names in application code become full words (tests keep theirs). Wrapped calls, signatures and composite literals put one item per line, and one-line function bodies are expanded. Exported names and groups of constants get doc comments, and comments refer to the renamed identifiers.
This commit is contained in:
1 parent
043e307663
commit
465358609a
78 files changed
+2374
-1402
No files matched your search
+155
-46
@@ -20,7 +20,9 @@ func openStore(t *testing.T) *store.Store {
|
||||
if err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { s.Close() })
|
||||
t.Cleanup(func() {
|
||||
s.Close()
|
||||
})
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -51,7 +53,10 @@ func opportunity(uuid, title string, releaseDay int, releaseIDs ...string) *type
|
||||
{IsCPV: true, Code: "03000000", Classification: "Agricultural products"},
|
||||
},
|
||||
// No UUID: SaveOpportunity must fill it in.
|
||||
Marks: types.OpportunityMarks{Interested: true, BidSubmitted: true},
|
||||
Marks: types.OpportunityMarks{
|
||||
Interested: true,
|
||||
BidSubmitted: true,
|
||||
},
|
||||
}
|
||||
for i, id := range releaseIDs {
|
||||
t.Releases = append(t.Releases, types.OpportunityRelease{
|
||||
@@ -131,7 +136,11 @@ func TestSaveAndGetRoundTrip(t *testing.T) {
|
||||
t.Errorf("classifications: got %+v (leading zero must survive)", got.Classifications)
|
||||
}
|
||||
|
||||
wantMarks := types.OpportunityMarks{UUID: "ocds-1", Interested: true, BidSubmitted: true}
|
||||
wantMarks := types.OpportunityMarks{
|
||||
UUID: "ocds-1",
|
||||
Interested: true,
|
||||
BidSubmitted: true,
|
||||
}
|
||||
if got.Marks != wantMarks {
|
||||
t.Errorf("marks: got %+v, want %+v", got.Marks, wantMarks)
|
||||
}
|
||||
@@ -230,7 +239,9 @@ func TestNewerReleaseUpdatesOpportunityAndSearchIndex(t *testing.T) {
|
||||
}
|
||||
|
||||
for title, want := range map[string]int{"cloud": 0, "redesign": 1} {
|
||||
ts, err := s.ListOpportunities(ctx, store.OpportunityFilter{Title: title})
|
||||
ts, err := s.ListOpportunities(ctx, store.OpportunityFilter{
|
||||
Title: title,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -247,8 +258,11 @@ func TestClassificationRowsAreShared(t *testing.T) {
|
||||
mustSave(t, s, a)
|
||||
mustSave(t, s, b)
|
||||
if a.Classifications[0].ID == 0 || a.Classifications[0].ID != b.Classifications[0].ID {
|
||||
t.Errorf("identical classifications should share an ID: %d vs %d",
|
||||
a.Classifications[0].ID, b.Classifications[0].ID)
|
||||
t.Errorf(
|
||||
"identical classifications should share an ID: %d vs %d",
|
||||
a.Classifications[0].ID,
|
||||
b.Classifications[0].ID,
|
||||
)
|
||||
}
|
||||
|
||||
c := opportunity("ocds-c", "C", 1, "r1")
|
||||
@@ -299,31 +313,59 @@ func TestListOpportunitiesFilters(t *testing.T) {
|
||||
want []string
|
||||
}{
|
||||
{"all, newest first", store.OpportunityFilter{}, []string{"ocds-3", "ocds-2", "ocds-1"}},
|
||||
{"status", store.OpportunityFilter{Status: "complete"}, []string{"ocds-2"}},
|
||||
{"status", store.OpportunityFilter{
|
||||
Status: "complete",
|
||||
}, []string{"ocds-2"}},
|
||||
{
|
||||
"classification",
|
||||
store.OpportunityFilter{ClassificationCode: "72000000"},
|
||||
store.OpportunityFilter{
|
||||
ClassificationCode: "72000000",
|
||||
},
|
||||
[]string{"ocds-3", "ocds-1"},
|
||||
},
|
||||
{"title word", store.OpportunityFilter{Title: "cloud"}, []string{"ocds-3", "ocds-1"}},
|
||||
{"title prefixes", store.OpportunityFilter{Title: "clo migr"}, []string{"ocds-1"}},
|
||||
{"title stemming", store.OpportunityFilter{Title: "migrating"}, []string{"ocds-1"}},
|
||||
{"title case", store.OpportunityFilter{Title: "ROAD"}, []string{"ocds-2"}},
|
||||
{"title word", store.OpportunityFilter{
|
||||
Title: "cloud",
|
||||
}, []string{"ocds-3", "ocds-1"}},
|
||||
{"title prefixes", store.OpportunityFilter{
|
||||
Title: "clo migr",
|
||||
}, []string{"ocds-1"}},
|
||||
{"title stemming", store.OpportunityFilter{
|
||||
Title: "migrating",
|
||||
}, []string{"ocds-1"}},
|
||||
{"title case", store.OpportunityFilter{
|
||||
Title: "ROAD",
|
||||
}, []string{"ocds-2"}},
|
||||
{
|
||||
"title syntax is not FTS syntax",
|
||||
store.OpportunityFilter{Title: `cloud" OR title:road*`},
|
||||
store.OpportunityFilter{
|
||||
Title: `cloud" OR title:road*`,
|
||||
},
|
||||
[]string{},
|
||||
},
|
||||
{
|
||||
"title punctuation only",
|
||||
store.OpportunityFilter{Title: `"*:()`},
|
||||
store.OpportunityFilter{
|
||||
Title: `"*:()`,
|
||||
},
|
||||
[]string{"ocds-3", "ocds-2", "ocds-1"},
|
||||
},
|
||||
{"released from", store.OpportunityFilter{ReleasedFrom: day(2)}, []string{"ocds-3", "ocds-2"}},
|
||||
{"released before", store.OpportunityFilter{ReleasedBefore: day(2)}, []string{"ocds-1"}},
|
||||
{"combined", store.OpportunityFilter{Title: "cloud", ReleasedFrom: day(2)}, []string{"ocds-3"}},
|
||||
{"limit", store.OpportunityFilter{Limit: 2}, []string{"ocds-3", "ocds-2"}},
|
||||
{"offset", store.OpportunityFilter{Limit: 2, Offset: 2}, []string{"ocds-1"}},
|
||||
{"released from", store.OpportunityFilter{
|
||||
ReleasedFrom: day(2),
|
||||
}, []string{"ocds-3", "ocds-2"}},
|
||||
{"released before", store.OpportunityFilter{
|
||||
ReleasedBefore: day(2),
|
||||
}, []string{"ocds-1"}},
|
||||
{"combined", store.OpportunityFilter{
|
||||
Title: "cloud",
|
||||
ReleasedFrom: day(2),
|
||||
}, []string{"ocds-3"}},
|
||||
{"limit", store.OpportunityFilter{
|
||||
Limit: 2,
|
||||
}, []string{"ocds-3", "ocds-2"}},
|
||||
{"offset", store.OpportunityFilter{
|
||||
Limit: 2,
|
||||
Offset: 2,
|
||||
}, []string{"ocds-1"}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
@@ -337,7 +379,9 @@ func TestListOpportunitiesFilters(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
got, err := s.ListOpportunities(ctx, store.OpportunityFilter{Status: "complete"})
|
||||
got, err := s.ListOpportunities(ctx, store.OpportunityFilter{
|
||||
Status: "complete",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -413,7 +457,9 @@ func TestReopenKeepsDataAndSkipsAppliedMigrations(t *testing.T) {
|
||||
t.Fatalf("reopen: %v", err)
|
||||
}
|
||||
defer s.Close()
|
||||
got, err := s.ListOpportunities(ctx, store.OpportunityFilter{Title: "cloud"})
|
||||
got, err := s.ListOpportunities(ctx, store.OpportunityFilter{
|
||||
Title: "cloud",
|
||||
})
|
||||
if err != nil || len(got) != 1 {
|
||||
t.Errorf("after reopen: %v, %v", uuids(got), err)
|
||||
}
|
||||
@@ -440,8 +486,12 @@ func TestResaveLoadedOpportunity(t *testing.T) {
|
||||
t.Errorf("resave not applied: %+v", got)
|
||||
}
|
||||
if got.Classifications[0].ID != ids[0] || got.Classifications[1].ID != ids[1] {
|
||||
t.Errorf("classification IDs changed on resave: %v -> %d, %d",
|
||||
ids, got.Classifications[0].ID, got.Classifications[1].ID)
|
||||
t.Errorf(
|
||||
"classification IDs changed on resave: %v -> %d, %d",
|
||||
ids,
|
||||
got.Classifications[0].ID,
|
||||
got.Classifications[1].ID,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -473,14 +523,19 @@ func TestSetMarksErrors(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
if err := s.SetMarks(
|
||||
ctx,
|
||||
types.OpportunityMarks{UUID: "missing", Hidden: true},
|
||||
types.OpportunityMarks{
|
||||
UUID: "missing",
|
||||
Hidden: true,
|
||||
},
|
||||
); !errors.Is(
|
||||
err,
|
||||
store.ErrNotFound,
|
||||
) {
|
||||
t.Errorf("unknown opportunity: err = %v, want ErrNotFound", err)
|
||||
}
|
||||
if err := s.SetMarks(ctx, types.OpportunityMarks{Hidden: true}); err == nil {
|
||||
if err := s.SetMarks(ctx, types.OpportunityMarks{
|
||||
Hidden: true,
|
||||
}); err == nil {
|
||||
t.Errorf("no UUID: expected an error")
|
||||
}
|
||||
}
|
||||
@@ -492,7 +547,11 @@ func TestSaveKeepsExistingMarks(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
mustSave(t, s, opportunity("ocds-1", "T", 1, "r1"))
|
||||
mustSave(t, s, opportunity("ocds-2", "T", 1, "r1"))
|
||||
want := types.OpportunityMarks{UUID: "ocds-1", Hidden: true, Lost: true}
|
||||
want := types.OpportunityMarks{
|
||||
UUID: "ocds-1",
|
||||
Hidden: true,
|
||||
Lost: true,
|
||||
}
|
||||
mustSetMarks(t, s, want)
|
||||
|
||||
newer := opportunity("ocds-1", "Amended", 2, "r2")
|
||||
@@ -518,7 +577,9 @@ func TestSaveOpportunitiesStoresMarks(t *testing.T) {
|
||||
s := openStore(t)
|
||||
a := opportunity("ocds-a", "A", 1, "r1")
|
||||
b := opportunity("ocds-b", "B", 1, "r1")
|
||||
b.Marks = types.OpportunityMarks{Skipped: true}
|
||||
b.Marks = types.OpportunityMarks{
|
||||
Skipped: true,
|
||||
}
|
||||
if _, err := s.SaveOpportunities(context.Background(), []*types.Opportunity{a, b}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -539,8 +600,15 @@ func TestListOpportunitiesMarkFilters(t *testing.T) {
|
||||
mustSave(t, s, opportunity(uuid, "Cloud hosting", i+1, "r1"))
|
||||
}
|
||||
// ocds-1 keeps the marks it was saved with: Interested, BidSubmitted.
|
||||
mustSetMarks(t, s, types.OpportunityMarks{UUID: "ocds-2", Hidden: true})
|
||||
mustSetMarks(t, s, types.OpportunityMarks{UUID: "ocds-3", Skipped: true, Lost: true})
|
||||
mustSetMarks(t, s, types.OpportunityMarks{
|
||||
UUID: "ocds-2",
|
||||
Hidden: true,
|
||||
})
|
||||
mustSetMarks(t, s, types.OpportunityMarks{
|
||||
UUID: "ocds-3",
|
||||
Skipped: true,
|
||||
Lost: true,
|
||||
})
|
||||
mustSetMarks(t, s, types.OpportunityMarks{
|
||||
UUID: "ocds-4", Interested: true, BidSubmitted: true, Won: true,
|
||||
})
|
||||
@@ -552,31 +620,65 @@ func TestListOpportunitiesMarkFilters(t *testing.T) {
|
||||
want []string
|
||||
}{
|
||||
{"any", store.OpportunityFilter{}, []string{"ocds-4", "ocds-3", "ocds-2", "ocds-1"}},
|
||||
{"hidden", store.OpportunityFilter{Hidden: set}, []string{"ocds-2"}},
|
||||
{"not hidden", store.OpportunityFilter{Hidden: unset}, []string{"ocds-4", "ocds-3", "ocds-1"}},
|
||||
{"skipped", store.OpportunityFilter{Skipped: set}, []string{"ocds-3"}},
|
||||
{"hidden", store.OpportunityFilter{
|
||||
Hidden: set,
|
||||
}, []string{"ocds-2"}},
|
||||
{"not hidden", store.OpportunityFilter{
|
||||
Hidden: unset,
|
||||
}, []string{"ocds-4", "ocds-3", "ocds-1"}},
|
||||
{"skipped", store.OpportunityFilter{
|
||||
Skipped: set,
|
||||
}, []string{"ocds-3"}},
|
||||
{
|
||||
"not skipped",
|
||||
store.OpportunityFilter{Skipped: unset},
|
||||
store.OpportunityFilter{
|
||||
Skipped: unset,
|
||||
},
|
||||
[]string{"ocds-4", "ocds-2", "ocds-1"},
|
||||
},
|
||||
{"interested", store.OpportunityFilter{Interested: set}, []string{"ocds-4", "ocds-1"}},
|
||||
{"not interested", store.OpportunityFilter{Interested: unset}, []string{"ocds-3", "ocds-2"}},
|
||||
{"bid submitted", store.OpportunityFilter{BidSubmitted: set}, []string{"ocds-4", "ocds-1"}},
|
||||
{"interested", store.OpportunityFilter{
|
||||
Interested: set,
|
||||
}, []string{"ocds-4", "ocds-1"}},
|
||||
{"not interested", store.OpportunityFilter{
|
||||
Interested: unset,
|
||||
}, []string{"ocds-3", "ocds-2"}},
|
||||
{"bid submitted", store.OpportunityFilter{
|
||||
BidSubmitted: set,
|
||||
}, []string{"ocds-4", "ocds-1"}},
|
||||
{
|
||||
"bid not submitted",
|
||||
store.OpportunityFilter{BidSubmitted: unset},
|
||||
store.OpportunityFilter{
|
||||
BidSubmitted: unset,
|
||||
},
|
||||
[]string{"ocds-3", "ocds-2"},
|
||||
},
|
||||
{"won", store.OpportunityFilter{Won: set}, []string{"ocds-4"}},
|
||||
{"not won", store.OpportunityFilter{Won: unset}, []string{"ocds-3", "ocds-2", "ocds-1"}},
|
||||
{"lost", store.OpportunityFilter{Lost: set}, []string{"ocds-3"}},
|
||||
{"not lost", store.OpportunityFilter{Lost: unset}, []string{"ocds-4", "ocds-2", "ocds-1"}},
|
||||
{"two marks", store.OpportunityFilter{BidSubmitted: set, Won: unset}, []string{"ocds-1"}},
|
||||
{"no match", store.OpportunityFilter{Won: set, Lost: set}, []string{}},
|
||||
{"won", store.OpportunityFilter{
|
||||
Won: set,
|
||||
}, []string{"ocds-4"}},
|
||||
{"not won", store.OpportunityFilter{
|
||||
Won: unset,
|
||||
}, []string{"ocds-3", "ocds-2", "ocds-1"}},
|
||||
{"lost", store.OpportunityFilter{
|
||||
Lost: set,
|
||||
}, []string{"ocds-3"}},
|
||||
{"not lost", store.OpportunityFilter{
|
||||
Lost: unset,
|
||||
}, []string{"ocds-4", "ocds-2", "ocds-1"}},
|
||||
{"two marks", store.OpportunityFilter{
|
||||
BidSubmitted: set,
|
||||
Won: unset,
|
||||
}, []string{"ocds-1"}},
|
||||
{"no match", store.OpportunityFilter{
|
||||
Won: set,
|
||||
Lost: set,
|
||||
}, []string{}},
|
||||
{
|
||||
"with other filters",
|
||||
store.OpportunityFilter{Hidden: unset, Title: "cloud", ReleasedFrom: day(3)},
|
||||
store.OpportunityFilter{
|
||||
Hidden: unset,
|
||||
Title: "cloud",
|
||||
ReleasedFrom: day(3),
|
||||
},
|
||||
[]string{"ocds-4", "ocds-3"},
|
||||
},
|
||||
}
|
||||
@@ -608,7 +710,11 @@ func TestWonAndLostAreExclusive(t *testing.T) {
|
||||
mustSave(t, s, opportunity("ocds-1", "T", 1, "r1"))
|
||||
before := mustGet(t, s, "ocds-1").Marks
|
||||
|
||||
both := types.OpportunityMarks{UUID: "ocds-1", Won: true, Lost: true}
|
||||
both := types.OpportunityMarks{
|
||||
UUID: "ocds-1",
|
||||
Won: true,
|
||||
Lost: true,
|
||||
}
|
||||
if err := s.SetMarks(ctx, both); !errors.Is(err, types.ErrWonAndLost) {
|
||||
t.Errorf("SetMarks(won and lost): err = %v, want ErrWonAndLost", err)
|
||||
}
|
||||
@@ -617,7 +723,10 @@ func TestWonAndLostAreExclusive(t *testing.T) {
|
||||
}
|
||||
|
||||
in := opportunity("ocds-2", "T", 1, "r1")
|
||||
in.Marks = types.OpportunityMarks{Won: true, Lost: true}
|
||||
in.Marks = types.OpportunityMarks{
|
||||
Won: true,
|
||||
Lost: true,
|
||||
}
|
||||
if _, err := s.SaveOpportunity(ctx, in); !errors.Is(err, types.ErrWonAndLost) {
|
||||
t.Errorf("SaveOpportunity(won and lost): err = %v, want ErrWonAndLost", err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user