forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pop_test.go
145 lines (119 loc) · 3.28 KB
/
pop_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package pop_test
import (
"log"
"os"
"testing"
"time"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"github.com/markbates/pop"
"github.com/markbates/pop/nulls"
"github.com/markbates/validate"
"github.com/markbates/validate/validators"
_ "github.com/mattn/go-sqlite3"
"github.com/satori/go.uuid"
"github.com/stretchr/testify/suite"
)
var PDB *pop.Connection
type PostgreSQLSuite struct {
suite.Suite
}
type MySQLSuite struct {
suite.Suite
}
type SQLiteSuite struct {
suite.Suite
}
func TestSpecificSuites(t *testing.T) {
switch os.Getenv("SODA_DIALECT") {
case "postgres":
suite.Run(t, &PostgreSQLSuite{})
case "mysql":
suite.Run(t, &MySQLSuite{})
case "sqlite":
suite.Run(t, &SQLiteSuite{})
}
}
func init() {
pop.Debug = false
pop.AddLookupPaths("./")
dialect := os.Getenv("SODA_DIALECT")
var err error
PDB, err = pop.Connect(dialect)
if err != nil {
log.Panic(err)
}
pop.MapTableName("Friend", "good_friends")
pop.MapTableName("Friends", "good_friends")
}
func transaction(fn func(tx *pop.Connection)) {
err := PDB.Rollback(func(tx *pop.Connection) {
fn(tx)
})
if err != nil {
log.Fatal(err)
}
}
func ts(s string) string {
return PDB.Dialect.TranslateSQL(s)
}
type User struct {
ID int `db:"id"`
Email string `db:"email"`
Name nulls.String `db:"name"`
Alive nulls.Bool `db:"alive"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
BirthDate nulls.Time `db:"birth_date"`
Bio nulls.String `db:"bio"`
Price nulls.Float64 `db:"price"`
FullName nulls.String `db:"full_name" select:"name as full_name"`
}
type Users []User
type Friend struct {
ID int `db:"id"`
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
type Friends []Friend
type Enemy struct {
A string
}
type Song struct {
ID uuid.UUID `db:"id"`
Title string `db:"title"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
type ValidatableCar struct {
ID int `db:"id"`
Name string `db:"name"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
var validationLogs = []string{}
func (v *ValidatableCar) Validate(tx *pop.Connection) (*validate.Errors, error) {
validationLogs = append(validationLogs, "Validate")
verrs := validate.Validate(&validators.StringIsPresent{Field: v.Name, Name: "Name"})
return verrs, nil
}
func (v *ValidatableCar) ValidateSave(tx *pop.Connection) (*validate.Errors, error) {
validationLogs = append(validationLogs, "ValidateSave")
return nil, nil
}
func (v *ValidatableCar) ValidateUpdate(tx *pop.Connection) (*validate.Errors, error) {
validationLogs = append(validationLogs, "ValidateUpdate")
return nil, nil
}
func (v *ValidatableCar) ValidateCreate(tx *pop.Connection) (*validate.Errors, error) {
validationLogs = append(validationLogs, "ValidateCreate")
return nil, nil
}
type NotValidatableCar struct {
ID int `db:"id"`
Name string `db:"name"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}