-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtypes_test.go
162 lines (135 loc) · 3.49 KB
/
types_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package tmeta
import (
"fmt"
"math/rand"
"github.com/gocraft/dbr"
_ "github.com/mattn/go-sqlite3"
)
type Author struct {
AuthorID string `db:"author_id" tmeta:"pk"`
NomDePlume string `db:"nom_de_plume"`
BookList []Book `db:"-" tmeta:"has_many"`
}
type Publisher struct {
PublisherID string `db:"publisher_id" tmeta:"pk"`
CompanyName string `db:"company_name"`
Version int64 `db:"version" tmeta:"version"`
BookList []Book `db:"-" tmeta:"has_many,relation_name=book_list"`
}
type Book struct {
BookID string `db:"book_id" tmeta:"pk"`
AuthorID string `db:"author_id"`
Author *Author `db:"-" tmeta:"belongs_to,sql_id_field=author_id"`
PublisherID string `db:"publisher_id"`
Publisher *Publisher `db:"-" tmeta:"belongs_to"`
Title string `db:"title"`
CategoryList []Category `db:"-" tmeta:"belongs_to_many,join_name=book_category"`
CategoryIDList []string `db:"-" tmeta:"belongs_to_many_ids,join_name=book_category"`
}
type BookCategory struct {
BookID string `db:"book_id" tmeta:"pk"`
CategoryID string `db:"category_id" tmeta:"pk"`
}
type Category struct {
CategoryID string `db:"category_id" tmeta:"pk"`
Name string `db:"name"`
BookList []Book `db:"-" tmeta:"belongs_to_many,join_name=book_category"`
CategoryInfo *CategoryInfo `db:"-" tmeta:"has_one"`
}
type CategoryInfo struct {
CategoryInfoID int64 `db:"category_info_id" tmeta:"pk,auto_incr"`
CategoryID string `db:"category_id"`
InfoStuff string `db:"info_stuff"`
Category *Category `db:"-" tmeta:"belongs_to"`
}
func doSetup() (*dbr.Session, *Meta, error) {
conn, err := dbr.Open("sqlite3", fmt.Sprintf(`file:tmeta_test%d?mode=memory&cache=shared`, rand.Int31()), nil)
if err != nil {
return nil, nil, err
}
sess := conn.NewSession(newPrintEventReceiver(nil))
_, err = sess.Exec(`
CREATE TABLE test_author (
author_id VARCHAR(64),
nom_de_plume VARCHAR(255),
PRIMARY KEY(author_id)
)`)
if err != nil {
return nil, nil, err
}
_, err = sess.Exec(`
CREATE TABLE test_publisher (
publisher_id VARCHAR(64),
company_name VARCHAR(255),
version INTEGER NOT NULL,
PRIMARY KEY(publisher_id)
)`)
if err != nil {
return nil, nil, err
}
_, err = sess.Exec(`
CREATE TABLE test_book (
book_id VARCHAR(64),
author_id VARCHAR(64),
publisher_id VARCHAR(64),
title VARCHAR(255),
PRIMARY KEY(book_id)
)`)
if err != nil {
return nil, nil, err
}
_, err = sess.Exec(`
CREATE TABLE test_book_category (
book_id VARCHAR(64),
category_id VARCHAR(64),
PRIMARY KEY(book_id, category_id)
)`)
if err != nil {
return nil, nil, err
}
_, err = sess.Exec(`
CREATE TABLE test_category (
category_id VARCHAR(64),
name VARCHAR(255),
PRIMARY KEY(category_id)
)`)
if err != nil {
return nil, nil, err
}
_, err = sess.Exec(`
CREATE TABLE test_category_info (
category_info_id INTEGER PRIMARY KEY AUTOINCREMENT,
category_id VARCHAR(64),
info_stuff VARCHAR(255)
)`)
if err != nil {
return nil, nil, err
}
meta := NewMeta()
err = meta.Parse(&Author{})
if err != nil {
return nil, nil, err
}
err = meta.Parse(&Publisher{})
if err != nil {
return nil, nil, err
}
err = meta.Parse(&Book{})
if err != nil {
return nil, nil, err
}
err = meta.Parse(&BookCategory{})
if err != nil {
return nil, nil, err
}
err = meta.Parse(&Category{})
if err != nil {
return nil, nil, err
}
err = meta.Parse(&CategoryInfo{})
if err != nil {
return nil, nil, err
}
meta.ReplaceSQLNames(func(name string) string { return "test_" + name })
return sess, meta, nil
}