This repository was archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_test.go
More file actions
executable file
·58 lines (49 loc) · 1.49 KB
/
database_test.go
File metadata and controls
executable file
·58 lines (49 loc) · 1.49 KB
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
package main
import (
"database/sql"
"github.com/stretchr/testify/assert"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
"testing"
)
func TestSetupDatabase(t *testing.T) {
_, ctx := testSetup(t)
res, _ := setupDatabase(ctx)
actual := ctx.db
expected := &sql.DB{}
assert.IsType(t, actual, expected)
assert.Contains(t, res, "@tcp(")
}
func TestCreateTable(t *testing.T) {
mock, ctx := testSetup(t)
mock.ExpectExec("CREATE TABLE IF NOT EXISTS").WillReturnResult(testResult)
dbCreateTable(ctx)
assert.Nil(t, mock.ExpectationsWereMet())
}
func FakeSetupDatabase(ctx *context) (string, error) {
db, mock, _ := sqlmock.New()
mock.ExpectClose()
ctx.db = db
return "", nil
}
func TestLastVersionNumDb(t *testing.T) {
mock, ctx := testSetup(t)
mock.ExpectQuery("select VersionNum from entries").WithArgs("strawberry").WillReturnRows(testRows)
res := dbLastVersionNum(ctx, "strawberry", false)
assert.Equal(t, -1, res)
mock.ExpectQuery("select VersionNum from entries").WithArgs("strawberry").WillReturnRows(testRows)
res = dbLastVersionNum(ctx, "strawberry", true)
assert.Equal(t, -1, res)
assert.Nil(t, mock.ExpectationsWereMet())
}
func TestMoveOldFileDb(t *testing.T) {
mock, ctx := testSetup(t)
result := sqlmock.NewResult(0, 0)
mock.ExpectExec("update entries").WithArgs("banana", "apple", 2).WillReturnResult(result)
err := dbArchiveFile(ctx, "apple", "banana", 2)
if err != nil {
t.Fatal(err)
}
if err = mock.ExpectationsWereMet(); err != nil {
t.Fatal("Unfulfilled expections: ", err)
}
}