-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathschema_test.go
69 lines (58 loc) · 2.15 KB
/
schema_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
package orm
import (
"database/sql"
"testing"
"github.com/stretchr/testify/assert"
)
func setup(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
err = SetupConnections(ConnectionConfig{
Name: "default",
DB: db,
Dialect: Dialects.SQLite3,
})
// orm.Schematic()
_, err = GetConnection("default").DB.Exec(`CREATE TABLE IF NOT EXISTS posts (id INTEGER PRIMARY KEY, body text, created_at TIMESTAMP, updated_at TIMESTAMP, deleted_at TIMESTAMP)`)
_, err = GetConnection("default").DB.Exec(`CREATE TABLE IF NOT EXISTS emails (id INTEGER PRIMARY KEY, post_id INTEGER, email text)`)
_, err = GetConnection("default").DB.Exec(`CREATE TABLE IF NOT EXISTS header_pictures (id INTEGER PRIMARY KEY, post_id INTEGER, link text)`)
_, err = GetConnection("default").DB.Exec(`CREATE TABLE IF NOT EXISTS comments (id INTEGER PRIMARY KEY, post_id INTEGER, body text)`)
_, err = GetConnection("default").DB.Exec(`CREATE TABLE IF NOT EXISTS categories (id INTEGER PRIMARY KEY, title text)`)
_, err = GetConnection("default").DB.Exec(`CREATE TABLE IF NOT EXISTS post_categories (post_id INTEGER, category_id INTEGER, PRIMARY KEY(post_id, category_id))`)
assert.NoError(t, err)
}
type Object struct {
ID int64
Name string
Timestamps
}
func (o Object) ConfigureEntity(e *EntityConfigurator) {
e.Table("objects").Connection("default")
}
func TestGenericFieldsOf(t *testing.T) {
t.Run("fields of with id and timestamps embedded", func(t *testing.T) {
fs := genericFieldsOf(&Object{})
assert.Len(t, fs, 5)
assert.Equal(t, "id", fs[0].Name)
assert.True(t, fs[0].IsPK)
assert.Equal(t, "name", fs[1].Name)
assert.Equal(t, "created_at", fs[2].Name)
assert.Equal(t, "updated_at", fs[3].Name)
assert.Equal(t, "deleted_at", fs[4].Name)
})
}
func TestGenericValuesOf(t *testing.T) {
t.Run("values of", func(t *testing.T) {
setup(t)
vs := genericValuesOf(Object{}, true)
assert.Len(t, vs, 5)
})
}
func TestEntityConfigurator(t *testing.T) {
t.Run("test has many with user provided values", func(t *testing.T) {
setup(t)
var ec EntityConfigurator
ec.Table("users").Connection("default").HasMany(Object{}, HasManyConfig{
"objects", "user_id",
})
})
}