-
Notifications
You must be signed in to change notification settings - Fork 3
/
fixtures_test.go
114 lines (93 loc) · 2.05 KB
/
fixtures_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
package yago_test
import (
"fmt"
"time"
"github.com/m4rw3r/uuid"
"github.com/orus-io/yago"
)
//go:generate yago --fmt --package yago_test --output fixtures_yago_test.go
type FixtureModel struct {
meta *yago.Metadata
PersonStruct PersonStructModel
SimpleStruct SimpleStructModel
}
func NewFixtureModel(meta *yago.Metadata) FixtureModel {
return FixtureModel{
meta: meta,
PersonStruct: NewPersonStructModel(meta),
SimpleStruct: NewSimpleStructModel(meta),
}
}
//yago:
type SimpleStruct struct {
ID int64 `yago:"primary_key,auto_increment"`
Name string `yago:"unique_index"`
}
//yago:notable,autoattrs
type BaseStruct struct {
ID uuid.UUID `yago:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
}
// Person is a person gender
type PersonGender int
const (
NoGender PersonGender = iota
Male
Female
)
func (g PersonGender) MarshalText() ([]byte, error) {
switch g {
case NoGender:
return []byte("none"), nil
case Male:
return []byte("male"), nil
case Female:
return []byte("female"), nil
default:
return nil, fmt.Errorf("Invalid PersonGender: %d", g)
}
}
func (g *PersonGender) UnmarshalText(text []byte) error {
switch string(text) {
case "none":
*g = NoGender
case "male":
*g = Male
case "female":
*g = Female
default:
return fmt.Errorf("Invalid gender: %s", text)
}
return nil
}
//yago:autoattrs
type PersonStruct struct {
BaseStruct
Active bool
FirstName string `yago:"unique"`
LastName string `yago:"null"`
Gender PersonGender `yago:"textmarshaled"`
}
//yago:notable
type AutoIncBase struct {
ID int64 `yago:"primary_key,auto_increment"`
}
//yago:autoattrs
type AutoIncChild struct {
AutoIncBase
Name string
Person uuid.UUID `yago:"fk=PersonStruct ONDELETE SET NULL ONUPDATE CASCADE"`
}
func (s *BaseStruct) BeforeInsert(db *yago.DB) {
var err error
s.ID, err = uuid.V4()
if err != nil {
panic(fmt.Sprintf("Cannot generate a UUID. Got err %s", err))
}
s.CreatedAt = time.Now()
s.UpdatedAt = time.Now()
}
func (s *BaseStruct) BeforeUpdate(db *yago.DB) {
s.UpdatedAt = time.Now()
}