-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfactory_test.go
183 lines (166 loc) · 3.61 KB
/
factory_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package testcraft
import (
"errors"
"reflect"
"testing"
)
func TestNewFactory(t *testing.T) {
type User struct {
Name string
}
obj := User{}
factory := NewFactory(obj)
// Assert that the object is set correctly
if factory.object != obj {
t.Errorf("NewFactory returned factory with object %v, expected %v", factory.object, obj)
}
// Assert that the sequence map is initialized
if factory.sequence == nil {
t.Errorf("NewFactory returned factory with nil sequence map")
}
}
func TestFactoryAttr(t *testing.T) {
type User struct {
Name string
}
obj := User{}
factory := NewFactory(obj)
// Test adding a single attribute generator
attrGen1 := func(instance *User) error {
return nil
}
factory.Attr(attrGen1)
// Assert that the attribute generator is added
if len(factory.attrsGen) != 1 {
t.Errorf("Factory.Attr did not add attribute generator correctly")
}
factory.Attr(attrGen1)
// Assert that both attribute generators are added in the correct order
if len(factory.attrsGen) != 2 {
t.Errorf("Factory.Attr did not add attribute generators correctly")
}
}
func TestFactoryBuild(t *testing.T) {
type Person struct {
Name string
Age int
}
tests := []struct {
obj Person
attrsGens []AttrGenerator[Person]
expected Person
expectFail bool
}{
{
obj: Person{
Name: "Alice",
Age: 25,
},
attrsGens: []AttrGenerator[Person]{
func(p *Person) error {
p.Name = "Bob"
return nil
},
func(p *Person) error {
p.Age = 25
return nil
},
},
expected: Person{
Name: "Bob",
Age: 25,
},
expectFail: false,
},
{
obj: Person{
Name: "Charlie",
Age: 30,
},
attrsGens: []AttrGenerator[Person]{
func(p *Person) error {
return errors.New("failed to generate attributes")
},
},
expected: Person{},
expectFail: true,
},
}
for i, test := range tests {
factory := NewFactory(test.obj).Attr(test.attrsGens...)
result, err := factory.build()
if test.expectFail {
if err == nil {
t.Errorf("Test case %d: expected error, but got nil", i)
}
} else {
if err != nil {
t.Errorf("Test case %d: unexpected error: %v", i, err)
}
if !reflect.DeepEqual(result, test.expected) {
t.Errorf("Test case %d: unexpected result: got %v, expected %v", i, result, test.expected)
}
}
}
}
func TestFactory_Randomize(t *testing.T) {
type Person struct {
Name string
Age int
}
tests := []struct {
obj Person
expectFail bool
}{
{
obj: Person{},
expectFail: false,
},
}
for i, test := range tests {
_, err := NewFactory(test.obj).Randomize()
if test.expectFail {
if err == nil {
t.Errorf("Test case %d: expected error, but got nil", i)
}
} else {
if err != nil {
t.Errorf("Test case %d: unexpected error: %v", i, err)
}
}
}
}
func TestFactory_RandomizeWithAttrs(t *testing.T) {
type Person struct {
ID int
Name string
Age int
}
seq := NewSequencer(1)
pFactory := NewFactory(Person{}).Attr(func(p *Person) error {
p.ID = seq.Next()
return nil
})
p1 := pFactory.MustRandomizeWithAttrs()
p2 := pFactory.MustRandomizeWithAttrs()
if p1.ID != 1 {
t.Errorf("Expected p1.ID to be 1, got %d", p1.ID)
}
if p2.ID != 2 {
t.Errorf("Expected p2.ID to be 2, got %d", p2.ID)
}
p3, err := pFactory.RandomizeWithAttrs()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
p4, err := pFactory.RandomizeWithAttrs()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if p3.ID != 3 {
t.Errorf("Expected p3.ID to be 3, got %d", p3.ID)
}
if p4.ID != 4 {
t.Errorf("Expected p4.ID to be 4, got %d", p4.ID)
}
}