Skip to content

Commit 21683d2

Browse files
committed
Add unit test to keep fake db clean
1 parent e5ec365 commit 21683d2

File tree

3 files changed

+60
-20
lines changed

3 files changed

+60
-20
lines changed

coderd/database/databasefake/databasefake.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -958,25 +958,6 @@ func (q *fakeQuerier) GetParameterValueByScopeAndName(_ context.Context, arg dat
958958
return database.ParameterValue{}, sql.ErrNoRows
959959
}
960960

961-
func (q *fakeQuerier) GetTemplatesByIDs(_ context.Context, ids []uuid.UUID) ([]database.Template, error) {
962-
q.mutex.RLock()
963-
defer q.mutex.RUnlock()
964-
965-
templates := make([]database.Template, 0)
966-
for _, template := range q.templates {
967-
for _, id := range ids {
968-
if template.ID.String() != id.String() {
969-
continue
970-
}
971-
templates = append(templates, template)
972-
}
973-
}
974-
if len(templates) == 0 {
975-
return nil, sql.ErrNoRows
976-
}
977-
return templates, nil
978-
}
979-
980961
func (q *fakeQuerier) GetOrganizationMemberByUserID(_ context.Context, arg database.GetOrganizationMemberByUserIDParams) (database.OrganizationMember, error) {
981962
q.mutex.RLock()
982963
defer q.mutex.RUnlock()
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package databasefake_test
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"testing"
7+
8+
"github.com/coder/coder/coderd/database"
9+
10+
"github.com/coder/coder/coderd/database/databasefake"
11+
)
12+
13+
// TestExactMethods will ensure the fake database does not hold onto excessive
14+
// functions. The fake database is a manual implementation, so it is possible
15+
// we forget to delete functions that we remove. This unit test just ensures
16+
// we remove the extra methods.
17+
func TestExactMethods(t *testing.T) {
18+
// extraFakeMethods contains the extra allowed methods that are not a part
19+
// of the database.Store interface.
20+
extraFakeMethods := map[string]string{
21+
// Example
22+
// "SortFakeLists": "Helper function used",
23+
}
24+
25+
fake := reflect.TypeOf(databasefake.New())
26+
fakeMethods := methods(fake)
27+
28+
store := reflect.TypeOf((*database.Store)(nil)).Elem()
29+
storeMethods := methods(store)
30+
31+
// Store should be a subset
32+
for k := range storeMethods {
33+
_, ok := fakeMethods[k]
34+
if !ok {
35+
panic(fmt.Sprintf("This should never happen. FakeDB missing method %s, so doesn't fit the interface", k))
36+
}
37+
delete(storeMethods, k)
38+
delete(fakeMethods, k)
39+
}
40+
41+
for k := range fakeMethods {
42+
_, ok := extraFakeMethods[k]
43+
if ok {
44+
continue
45+
}
46+
// If you are seeing this error, you have an extra function not required
47+
// for the database.Store. If you still want to keep it, add it to
48+
// 'extraFakeMethods' to allow it.
49+
t.Errorf("Fake method '%s()' is excessive and not needed to fit interface, delete it", k)
50+
}
51+
}
52+
53+
func methods(rt reflect.Type) map[string]bool {
54+
methods := make(map[string]bool)
55+
for i := 0; i < rt.NumMethod(); i++ {
56+
methods[rt.Method(i).Name] = true
57+
}
58+
return methods
59+
}

coderd/workspaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (api *API) workspaces(rw http.ResponseWriter, r *http.Request) {
151151
if filter.OwnerUsername == "me" {
152152
if !(filter.OwnerID == uuid.Nil || filter.OwnerID == apiKey.UserID) {
153153
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
154-
Message: fmt.Sprintf("Cannot set both \"me\" in \"owner_name\" and use \"owner_id\"."),
154+
Message: "Cannot set both \"me\" in \"owner_name\" and use \"owner_id\".",
155155
})
156156
return
157157
}

0 commit comments

Comments
 (0)