forked from sohamkamani/blog_example__go_web_db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_test.go
112 lines (97 loc) · 2.83 KB
/
store_test.go
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
package main
import (
"database/sql"
"testing"
// The "testify/suite" package is used to make the test suite
"github.com/stretchr/testify/suite"
)
type StoreSuite struct {
suite.Suite
/*
The suite is defined as a struct, with the store and db as its
attributes. Any variables that are to be shared between tests in a
suite should be stored as attributes of the suite instance
*/
store *dbStore
db *sql.DB
}
func (s *StoreSuite) SetupSuite() {
/*
The database connection is opened in the setup, and
stored as an instance variable,
as is the higher level `store`, that wraps the `db`
*/
connString := "dbname=temp sslmode=disable"
db, err := sql.Open("postgres", connString)
if err != nil {
s.T().Fatal(err)
}
s.db = db
s.store = &dbStore{db: db}
}
func (s *StoreSuite) SetupTest() {
/*
We delete all entries from the table before each test runs, to ensure a
consistent state before our tests run. In more complex applications, this
is sometimes achieved in the form of migrations
*/
_, err := s.db.Query("DELETE FROM birds")
if err != nil {
s.T().Fatal(err)
}
}
func (s *StoreSuite) TearDownSuite() {
// Close the connection after all tests in the suite finish
s.db.Close()
}
// This is the actual "test" as seen by Go, which runs the tests defined below
func TestStoreSuite(t *testing.T) {
s := new(StoreSuite)
suite.Run(t, s)
}
func (s *StoreSuite) TestCreateBird() {
// Create a bird through the store `CreateBird` method
s.store.CreateBird(&Bird{
Description: "test description",
Species: "test species",
})
// Query the database for the entry we just created
res, err := s.db.Query(`SELECT COUNT(*) FROM birds WHERE description='test description' AND SPECIES='test species'`)
if err != nil {
s.T().Fatal(err)
}
// Get the count result
var count int
for res.Next() {
err := res.Scan(&count)
if err != nil {
s.T().Error(err)
}
}
// Assert that there must be one entry with the properties of the bird that we just inserted (since the database was empty before this)
if count != 1 {
s.T().Errorf("incorrect count, wanted 1, got %d", count)
}
}
func (s *StoreSuite) TestGetBird() {
// Insert a sample bird into the `birds` table
_, err := s.db.Query(`INSERT INTO birds (species, description) VALUES('bird','description')`)
if err != nil {
s.T().Fatal(err)
}
// Get the list of birds through the stores `GetBirds` method
birds, err := s.store.GetBirds()
if err != nil {
s.T().Fatal(err)
}
// Assert that the count of birds received must be 1
nBirds := len(birds)
if nBirds != 1 {
s.T().Errorf("incorrect count, wanted 1, got %d", nBirds)
}
// Assert that the details of the bird is the same as the one we inserted
expectedBird := Bird{"bird", "description"}
if *birds[0] != expectedBird {
s.T().Errorf("incorrect details, expected %v, got %v", expectedBird, *birds[0])
}
}