-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder_test.go
132 lines (109 loc) · 3.26 KB
/
builder_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
package kin
import (
"database/sql"
"os"
"testing"
"time"
_ "github.com/jmataya/renv/autoload"
)
const (
createBuilderTable = `
create table builders (
id serial primary key,
name text not null,
description text,
attributes jsonb not null default '{}',
tags text[] not null default '{}',
form_ids integer[] not null default '{}',
is_active boolean not null default false,
created_at timestamp without time zone
);
`
sqlInsertBuilder = `
INSERT INTO builders (name, attributes, description, is_active, tags, form_ids, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING *
`
)
type builderModel struct {
ID int `db:"id"`
Name string `db:"name"`
Description *string `db:"description"`
Attributes map[string]interface{} `db:"attributes"`
IsActive bool `db:"is_active"`
Tags []string `db:"tags"`
FormIDs []int `db:"form_ids"`
CreatedAt time.Time `db:"created_at"`
}
func (b builderModel) TableName() string {
return "builders"
}
func TestBuild(t *testing.T) {
migrationPath := "./sql"
cleanupMigrationDir(migrationPath)
if err := setupMigrationDir(migrationPath); err != nil {
t.Errorf("setupMigrationDir = %v", err)
return
}
if err := createFile(migrationPath, "1__create_builders.sql", createBuilderTable); err != nil {
t.Errorf("createFile = %v", err)
cleanupMigrationDir(migrationPath)
return
}
connStr := os.Getenv("POSTGRES_URL")
if connStr == "" {
t.Error("POSTGRES_URL is empty")
return
}
migrator, err := NewMigratorConnection(connStr)
if err != nil {
panic(err)
}
defer migrator.Close()
if err := migrator.Migrate(migrationPath); err != nil {
t.Errorf("migrator.Migrate(%s) = %v, want nil", migrationPath, err)
cleanupMigrationDir(migrationPath)
return
}
name := "Builder Test"
attributes := `{ "lang": "en" }`
description := "Some description"
tags := "{tag1,tag2}"
formIds := "{1,2,3}"
isActive := true
createdAt := time.Now().UTC()
db, _ := sql.Open("postgres", connStr)
defer db.Close()
builder, err := NewQuery[builderModel](db).Raw(sqlInsertBuilder, name, attributes, description, isActive, tags, formIds, createdAt).One()
if err != nil {
t.Errorf("query.Raw(...).One(...) = %v, want <nil>", err)
return
}
if builder.ID == 0 {
t.Error("builder.ID = 0, want > 0")
}
if builder.Name != name {
t.Errorf("builder.Name = %v, want %v", builder.Name, name)
}
if builder.Description == nil {
t.Error("builder.Description = nil, want not nil")
} else if *builder.Description != description {
t.Errorf("builder.Description = %v, want %v", *builder.Description, description)
}
if len(builder.Tags) != 2 {
t.Errorf("len(builder.Tags) = %v, want 2", len(builder.Tags))
}
if len(builder.FormIDs) != 3 {
t.Errorf("len(builder.FormIDs) = %v, want 3", len(builder.FormIDs))
}
if builder.IsActive != isActive {
t.Errorf("builder.IsActive = %v, want %v", builder.IsActive, isActive)
}
if !builder.CreatedAt.Equal(createdAt) {
t.Errorf("builder.CreatedAt = %v, want %v", builder.CreatedAt, createdAt)
}
if builder.Attributes["lang"] != "en" {
t.Error(`"builder.Attributes != { "lang": "en" }`)
}
cleanupMigrationDir(migrationPath)
}