Skip to content

chore: generate and order dbfake funcs automatically #7986

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

Merged
merged 11 commits into from
Jun 12, 2023
Prev Previous commit
Next Next commit
Remove unused method
  • Loading branch information
kylecarbs committed Jun 12, 2023
commit 81c6f280a23cf7b2a38d44e1330c61d387f3f3cc
8 changes: 0 additions & 8 deletions coderd/database/dbfake/dbfake.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ import (

var validProxyByHostnameRegex = regexp.MustCompile(`^[a-zA-Z0-9._-]+$`)

// FakeDatabase is helpful for knowing if the underlying db is an in memory fake
// database. This is only in the databasefake package, so will only be used
// by unit tests.
type FakeDatabase interface {
IsFakeDB()
}

var errDuplicateKey = &pq.Error{
Code: "23505",
Message: "duplicate key value violates unique constraint",
Expand Down Expand Up @@ -218,7 +211,6 @@ func validateDatabaseType(args interface{}) error {
return nil
}

func (fakeQuerier) IsFakeDB() {}
func (*fakeQuerier) Ping(_ context.Context) (time.Duration, error) {
return 0, nil
}
Expand Down
53 changes: 0 additions & 53 deletions coderd/database/dbfake/dbfake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package dbfake_test
import (
"context"
"database/sql"
"fmt"
"reflect"
"sort"
"testing"
"time"
Expand Down Expand Up @@ -64,49 +62,6 @@ func TestInTx(t *testing.T) {
}
}

// TestExactMethods will ensure the fake database does not hold onto excessive
// functions. The fake database is a manual implementation, so it is possible
// we forget to delete functions that we remove. This unit test just ensures
// we remove the extra methods.
func TestExactMethods(t *testing.T) {
t.Parallel()

// extraFakeMethods contains the extra allowed methods that are not a part
// of the database.Store interface.
extraFakeMethods := map[string]string{
// Example
// "SortFakeLists": "Helper function used",
"IsFakeDB": "Helper function used for unit testing",
}

fake := reflect.TypeOf(dbfake.New())
fakeMethods := methods(fake)

store := reflect.TypeOf((*database.Store)(nil)).Elem()
storeMethods := methods(store)

// Store should be a subset
for k := range storeMethods {
_, ok := fakeMethods[k]
if !ok {
panic(fmt.Sprintf("This should never happen. FakeDB missing method %s, so doesn't fit the interface", k))
}
delete(storeMethods, k)
delete(fakeMethods, k)
}

for k := range fakeMethods {
_, ok := extraFakeMethods[k]
if ok {
continue
}
// If you are seeing this error, you have an extra function not required
// for the database.Store. If you still want to keep it, add it to
// 'extraFakeMethods' to allow it.
t.Errorf("Fake method '%s()' is excessive and not needed to fit interface, delete it", k)
}
}

// TestUserOrder ensures that the fake database returns users sorted by username.
func TestUserOrder(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -252,11 +207,3 @@ func TestProxyByHostname(t *testing.T) {
})
}
}

func methods(rt reflect.Type) map[string]bool {
methods := make(map[string]bool)
for i := 0; i < rt.NumMethod(); i++ {
methods[rt.Method(i).Name] = true
}
return methods
}