aboutsummaryrefslogtreecommitdiff
path: root/pkg/database/database_test.go
blob: e1951916a1d9a43f726426b7ca84327c50c92be2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package database

import (
	"database/sql"
	"os"
	"testing"

	"github.com/DATA-DOG/go-sqlmock"
	"github.com/stretchr/testify/require"
)

func TestInitDB(t *testing.T) {
	// Test cases
	testCases := []struct {
		name          string
		dbType        string
		dsn           string
		expectedError error
	}{
		{"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 {
		t.Run(tc.name, func(t *testing.T) {
			db, err := InitDB(tc.dbType, tc.dsn)
			if tc.expectedError != nil {
				require.Error(t, err)
				requireErrorTypeMatch(t, err, tc.expectedError)
				require.Nil(t, db)
			} else {
				require.NoError(t, err)
				require.NotNil(t, db)
			}
		})
	}
}

func TestMigrate(t *testing.T) {
	badMigration := []func(tx *sql.Tx) error{
		func(tx *sql.Tx) (err error) {
			return newMigrationError(0, nil)
		},
	}
	noSchemaVersionMigration := []func(tx *sql.Tx) error{
		func(tx *sql.Tx) (err error) {
			return nil
		},
	}
	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}
		},
		func(tx *sql.Tx) (err error) {
			return nil
		},
	}
	os.Remove("testfile_notFromScratch.db")
	notFromScratchDB, err := InitDB("sqlite3", "file:testfile_notFromScratch.db?_foreign_keys=on")
	require.NoError(t, err, "Failed to init testfile.db : %+v", err)
	defer os.Remove("testfile_notFromScratch.db")
	migrations = onlyFirstMigration
	err = notFromScratchDB.Migrate()
	require.NoError(t, err, "Failed to migrate testfile.db to first schema version : %+v", err)

	// Test cases
	testCases := []struct {
		name          string
		dsn           string
		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{}},
		{"not from scratch", "file:testfile_notFromScratch.db?_foreign_keys=on", notFromScratchMigration, nil},
		{"from scratch", "file::memory:?_foreign_keys=on", allMigrations, nil},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			db, err := InitDB("sqlite3", tc.dsn)
			require.NoError(t, err)
			migrations = tc.migrs
			err = db.Migrate()
			if tc.expectedError != nil {
				require.Error(t, err)
				requireErrorTypeMatch(t, err, tc.expectedError)
			} else {
				require.NoError(t, err)
			}
		})
	}
}

func TestMigrateWithSQLMock(t *testing.T) {
	fakeMigration := []func(tx *sql.Tx) error{
		func(tx *sql.Tx) (err error) {
			return nil
		},
	}
	// Transaction begin error
	dbBeginError, _, err := sqlmock.New()
	require.NoError(t, err, "an error '%s' was not expected when opening a stub database connection", err)
	defer dbBeginError.Close()
	// 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)
	defer dbCommitError.Close()
	mockCommitError.ExpectBegin()
	mockCommitError.ExpectExec(`DELETE FROM schema_version`).WillReturnResult(sqlmock.NewResult(1, 1))
	// Test cases
	testCases := []struct {
		name          string
		db            *DBEnv
		migrs         []func(tx *sql.Tx) error
		expectedError error
	}{
		{"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) {
			migrations = tc.migrs
			err = tc.db.Migrate()
			migrations = allMigrations
			if tc.expectedError != nil {
				require.Error(t, err)
				requireErrorTypeMatch(t, err, tc.expectedError)
			} else {
				require.NoError(t, err)
			}
		})
	}
}