-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb_test.go
66 lines (56 loc) · 1.43 KB
/
db_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
package npidb_test
import (
"database/sql"
"errors"
"time"
. "github.com/cwarden/npidb"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type queryResult int
const (
noRows queryResult = iota
queryError
validDate
)
type stubDbUpdater struct {
result queryResult
}
func (u stubDbUpdater) GetLastUpdate() (lastDate time.Time, err error) {
switch u.result {
case noRows:
err = sql.ErrNoRows
case queryError:
err = errors.New("Query Failed")
case validDate:
lastDate = time.Date(2010, time.January, 1, 0, 0, 0, 0, time.UTC)
}
return
}
func (u stubDbUpdater) RecordUpdate(t time.Time) error {
return nil
}
var _ = Describe("Test", func() {
var (
stub stubDbUpdater
)
Describe("LastUpdate", func() {
It("should return GetLastUpdate", func() {
stub = stubDbUpdater{result: validDate}
timeStamp, err := LastUpdate(stub)
Expect(err).ToNot(HaveOccurred())
Expect(timeStamp).To(Equal(time.Date(2010, time.January, 1, 0, 0, 0, 0, time.UTC)))
})
It("should return error if GetLastUpdate fails", func() {
stub = stubDbUpdater{result: queryError}
_, err := LastUpdate(stub)
Expect(err).To(HaveOccurred())
})
It("should return default date if GetLastUpdate returns sql.ErrNoRows", func() {
stub = stubDbUpdater{result: noRows}
timeStamp, err := LastUpdate(stub)
Expect(err).ToNot(HaveOccurred())
Expect(timeStamp).To(Equal(time.Date(2017, time.June, 11, 0, 0, 0, 0, time.UTC)))
})
})
})