-
Notifications
You must be signed in to change notification settings - Fork 3
/
assert.go
68 lines (59 loc) · 1.31 KB
/
assert.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package dbassert
import (
"database/sql"
"errors"
"reflect"
"github.com/stretchr/testify/assert"
)
var ErrNilTestingT = errors.New("TestingT is nil")
// DbAsserts provides database assertion methods around the TestingT
// interface.
type DbAsserts struct {
T TestingT
Db *sql.DB
Dialect string
}
// New creates a new DbAsserts.
func New(t TestingT, db *sql.DB, dialect string) *DbAsserts {
if isNil(t) {
panic(ErrNilTestingT)
}
if !assert.NotNil(t, db, "db is nill") {
return nil
}
if !assert.NotEmpty(t, dialect, "dialect is not set") {
return nil
}
switch dialect {
case "postgres":
default:
assert.FailNowf(t, "%s not a supported dialect", dialect)
return nil
}
return &DbAsserts{
T: t,
Db: db,
Dialect: dialect,
}
}
// TestingT is the testing interface used by the dbassert package.
type TestingT interface {
Errorf(format string, args ...interface{})
FailNow()
}
// THelper is the helper interface used by the dbassert package.
type THelper interface {
Helper()
}
func isNil(i interface{}) bool {
if i == nil {
return true
}
switch reflect.TypeOf(i).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
return reflect.ValueOf(i).IsNil()
}
return false
}