diff options
author | Julien Dessaux | 2021-09-15 01:19:24 +0200 |
---|---|---|
committer | Julien Dessaux | 2021-09-15 01:22:47 +0200 |
commit | dc874c52b8faeb714f8fe9a7c7abf3d79792f96e (patch) | |
tree | c05d47bb44a7168f32e5f5c45f583b7940979f88 /pkg/database | |
parent | Updated dependencies (diff) | |
download | trains-dc874c52b8faeb714f8fe9a7c7abf3d79792f96e.tar.gz trains-dc874c52b8faeb714f8fe9a7c7abf3d79792f96e.tar.bz2 trains-dc874c52b8faeb714f8fe9a7c7abf3d79792f96e.zip |
Finished rewriting database package tests for consistency
Diffstat (limited to 'pkg/database')
-rw-r--r-- | pkg/database/database_test.go | 14 | ||||
-rw-r--r-- | pkg/database/error.go | 10 | ||||
-rw-r--r-- | pkg/database/sessions_test.go | 8 | ||||
-rw-r--r-- | pkg/database/stop_test.go | 16 | ||||
-rw-r--r-- | pkg/database/users_test.go | 29 |
5 files changed, 38 insertions, 39 deletions
diff --git a/pkg/database/database_test.go b/pkg/database/database_test.go index 69f0012..e195191 100644 --- a/pkg/database/database_test.go +++ b/pkg/database/database_test.go @@ -17,8 +17,8 @@ func TestInitDB(t *testing.T) { dsn string expectedError error }{ - {"Invalid dbType", "non-existant", "test", &InitError{}}, - {"Non existant path", "sqlite3", "/non-existant/non-existant", &InitError{}}, + {"Invalid dbType", "non-existant", "test", InitError{}}, + {"Non existant path", "sqlite3", "/non-existant/non-existant", InitError{}}, {"Working DB", "sqlite3", "file::memory:?_foreign_keys=on", nil}, } for _, tc := range testCases { @@ -50,7 +50,7 @@ func TestMigrate(t *testing.T) { onlyFirstMigration := []func(tx *sql.Tx) error{migrations[0]} notFromScratchMigration := []func(tx *sql.Tx) error{ func(tx *sql.Tx) (err error) { - return &MigrationError{version: 1, err: nil} + return MigrationError{version: 1, err: nil} }, func(tx *sql.Tx) (err error) { return nil @@ -71,8 +71,8 @@ func TestMigrate(t *testing.T) { migrs []func(tx *sql.Tx) error expectedError error }{ - {"bad migration", "file::memory:?_foreign_keys=on", badMigration, &MigrationError{}}, - {"no schema_version migration", "file::memory:?_foreign_keys=on", noSchemaVersionMigration, &MigrationError{}}, + {"bad migration", "file::memory:?_foreign_keys=on", badMigration, MigrationError{}}, + {"no schema_version migration", "file::memory:?_foreign_keys=on", noSchemaVersionMigration, MigrationError{}}, {"not from scratch", "file:testfile_notFromScratch.db?_foreign_keys=on", notFromScratchMigration, nil}, {"from scratch", "file::memory:?_foreign_keys=on", allMigrations, nil}, } @@ -115,8 +115,8 @@ func TestMigrateWithSQLMock(t *testing.T) { migrs []func(tx *sql.Tx) error expectedError error }{ - {"begin transaction error", &DBEnv{db: dbBeginError}, fakeMigration, &TransactionError{}}, - {"commit transaction error", &DBEnv{db: dbCommitError}, fakeMigration, &TransactionError{}}, + {"begin transaction error", &DBEnv{db: dbBeginError}, fakeMigration, TransactionError{}}, + {"commit transaction error", &DBEnv{db: dbCommitError}, fakeMigration, TransactionError{}}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { diff --git a/pkg/database/error.go b/pkg/database/error.go index bbf4d69..93f1d32 100644 --- a/pkg/database/error.go +++ b/pkg/database/error.go @@ -14,7 +14,7 @@ func (e InitError) Error() string { func (e InitError) Unwrap() error { return e.err } func newInitError(path string, err error) error { - return &InitError{ + return InitError{ path: path, err: err, } @@ -32,7 +32,7 @@ func (e MigrationError) Error() string { func (e MigrationError) Unwrap() error { return e.err } func newMigrationError(version int, err error) error { - return &MigrationError{ + return MigrationError{ version: version, err: err, } @@ -49,7 +49,7 @@ func (e PasswordError) Error() string { func (e PasswordError) Unwrap() error { return e.err } func newPasswordError(err error) error { - return &PasswordError{ + return PasswordError{ err: err, } } @@ -66,7 +66,7 @@ func (e QueryError) Error() string { func (e QueryError) Unwrap() error { return e.err } func newQueryError(msg string, err error) error { - return &QueryError{ + return QueryError{ msg: msg, err: err, } @@ -84,7 +84,7 @@ func (e TransactionError) Error() string { func (e TransactionError) Unwrap() error { return e.err } func newTransactionError(msg string, err error) error { - return &TransactionError{ + return TransactionError{ msg: msg, err: err, } diff --git a/pkg/database/sessions_test.go b/pkg/database/sessions_test.go index b6c65e2..4373462 100644 --- a/pkg/database/sessions_test.go +++ b/pkg/database/sessions_test.go @@ -31,7 +31,7 @@ func TestCreateSession(t *testing.T) { }{ {"Normal user", user1, nil}, {"A normal user can request multiple tokens", user1, nil}, - {"a non existant user id triggers an error", &user2, &QueryError{}}, + {"a non existant user id triggers an error", &user2, QueryError{}}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { @@ -65,8 +65,8 @@ func TestCreateSessionWithSQLMock(t *testing.T) { db *DBEnv expectedError error }{ - {"begin transaction error", &DBEnv{db: dbBeginError}, &TransactionError{}}, - {"commit transaction error", &DBEnv{db: dbCommitError}, &TransactionError{}}, + {"begin transaction error", &DBEnv{db: dbBeginError}, TransactionError{}}, + {"commit transaction error", &DBEnv{db: dbCommitError}, TransactionError{}}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { @@ -125,7 +125,7 @@ func TestResumeSession(t *testing.T) { {"Normal user resume", *token1, user1, nil}, {"Normal user resume 1bis", *token1bis, user1, nil}, {"Normal user resume 2", *token2, user2, nil}, - {"a non existant user token triggers an error", "XXX", nil, &QueryError{}}, + {"a non existant user token triggers an error", "XXX", nil, QueryError{}}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { diff --git a/pkg/database/stop_test.go b/pkg/database/stop_test.go index 1779930..4a72af5 100644 --- a/pkg/database/stop_test.go +++ b/pkg/database/stop_test.go @@ -20,7 +20,7 @@ func TestCountStops(t *testing.T) { // check sql error i, err := db.CountStops() require.Error(t, err) - requireErrorTypeMatch(t, err, &QueryError{}) + requireErrorTypeMatch(t, err, QueryError{}) // normal check err = db.Migrate() require.NoError(t, err) @@ -48,7 +48,7 @@ func TestGetStop(t *testing.T) { // error check stop, err = db.GetStop("non_existent") require.Error(t, err) - requireErrorTypeMatch(t, err, &QueryError{}) + requireErrorTypeMatch(t, err, QueryError{}) } func TestGetStops(t *testing.T) { @@ -62,7 +62,7 @@ func TestGetStops(t *testing.T) { // error check res, err := db.GetStops() require.Error(t, err) - requireErrorTypeMatch(t, err, &QueryError{}) + requireErrorTypeMatch(t, err, QueryError{}) // normal check err = db.Migrate() require.NoError(t, err) @@ -79,7 +79,7 @@ func TestGetStopsWithSQLMock(t *testing.T) { mock.ExpectQuery(`SELECT id, name FROM stops;`).WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(nil, "b").RowError(1, fmt.Errorf("row error"))) _, err = (&DBEnv{db: db}).GetStops() require.Error(t, err) - requireErrorTypeMatch(t, err, &QueryError{}) + requireErrorTypeMatch(t, err, QueryError{}) } func TestReplaceAndImportStops(t *testing.T) { @@ -154,10 +154,10 @@ func TestReplaceAndImportStopsWithSQLMock(t *testing.T) { db *DBEnv expectedError error }{ - {"begin transaction error", &DBEnv{db: dbBeginError}, &TransactionError{}}, - {"query error cannot delete from", &DBEnv{db: dbCannotDeleteFrom}, &QueryError{}}, - {"query error cannot insert into", &DBEnv{db: dbCannotInsertError}, &QueryError{}}, - {"commit transaction error", &DBEnv{db: dbCommitError}, &TransactionError{}}, + {"begin transaction error", &DBEnv{db: dbBeginError}, TransactionError{}}, + {"query error cannot delete from", &DBEnv{db: dbCannotDeleteFrom}, QueryError{}}, + {"query error cannot insert into", &DBEnv{db: dbCannotInsertError}, QueryError{}}, + {"commit transaction error", &DBEnv{db: dbCommitError}, TransactionError{}}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { diff --git a/pkg/database/users_test.go b/pkg/database/users_test.go index f34f7be..c70c4fd 100644 --- a/pkg/database/users_test.go +++ b/pkg/database/users_test.go @@ -1,7 +1,6 @@ package database import ( - "reflect" "testing" "git.adyxax.org/adyxax/trains/pkg/model" @@ -33,10 +32,10 @@ func TestCreateUser(t *testing.T) { name string input *model.UserRegistration expected int - expectedError interface{} + expectedError error }{ {"Normal user", &normalUser, 1, nil}, - {"Duplicate user", &normalUser, 0, &QueryError{}}, + {"Duplicate user", &normalUser, 0, QueryError{}}, {"Normal user 2", &normalUser2, 2, nil}, } for _, tc := range testCases { @@ -44,7 +43,7 @@ func TestCreateUser(t *testing.T) { valid, err := db.CreateUser(tc.input) if tc.expectedError != nil { require.Error(t, err) - require.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(tc.expectedError), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(tc.expectedError)) + requireErrorTypeMatch(t, err, tc.expectedError) require.Nil(t, valid) } else { require.NoError(t, err) @@ -58,7 +57,7 @@ func TestCreateUser(t *testing.T) { passwordFunction = bcrypt.GenerateFromPassword require.Error(t, err) require.Nil(t, valid) - require.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(&PasswordError{}), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(&PasswordError{})) + requireErrorTypeMatch(t, err, PasswordError{}) } func TestCreateUserWithSQLMock(t *testing.T) { @@ -71,7 +70,7 @@ func TestCreateUserWithSQLMock(t *testing.T) { require.NoError(t, err, "an error '%s' was not expected when opening a stub database connection", err) defer dbLastInsertIdError.Close() mockLastInsertIdError.ExpectBegin() - mockLastInsertIdError.ExpectExec(`INSERT INTO`).WillReturnResult(sqlmock.NewErrorResult(&TransactionError{"test", nil})) + mockLastInsertIdError.ExpectExec(`INSERT INTO`).WillReturnResult(sqlmock.NewErrorResult(TransactionError{"test", nil})) // Transaction commit error dbCommitError, mockCommitError, err := sqlmock.New() require.NoError(t, err, "an error '%s' was not expected when opening a stub database connection", err) @@ -82,18 +81,18 @@ func TestCreateUserWithSQLMock(t *testing.T) { testCases := []struct { name string db *DBEnv - expectedError interface{} + expectedError error }{ - {"begin transaction error", &DBEnv{db: dbBeginError}, &TransactionError{}}, - {"last insert id transaction error", &DBEnv{db: dbLastInsertIdError}, &TransactionError{}}, - {"commit transaction error", &DBEnv{db: dbCommitError}, &TransactionError{}}, + {"begin transaction error", &DBEnv{db: dbBeginError}, TransactionError{}}, + {"last insert id transaction error", &DBEnv{db: dbLastInsertIdError}, TransactionError{}}, + {"commit transaction error", &DBEnv{db: dbCommitError}, TransactionError{}}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { valid, err := tc.db.CreateUser(&model.UserRegistration{}) if tc.expectedError != nil { require.Error(t, err) - require.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(tc.expectedError), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(tc.expectedError)) + requireErrorTypeMatch(t, err, tc.expectedError) require.Nil(t, valid) } else { require.NoError(t, err) @@ -144,19 +143,19 @@ func TestLogin(t *testing.T) { testCases := []struct { name string input *model.UserLogin - expectedError interface{} + expectedError error }{ {"login user1", &loginUser1, nil}, {"login user2", &loginUser2, nil}, - {"failed login", &failedUser1, &PasswordError{}}, - {"query error", &queryError, &QueryError{}}, + {"failed login", &failedUser1, PasswordError{}}, + {"query error", &queryError, QueryError{}}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { valid, err := db.Login(tc.input) if tc.expectedError != nil { require.Error(t, err) - require.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(tc.expectedError), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(tc.expectedError)) + requireErrorTypeMatch(t, err, tc.expectedError) require.Nil(t, valid) } else { require.NoError(t, err) |