-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfields_test.go
More file actions
129 lines (107 loc) · 4.42 KB
/
fields_test.go
File metadata and controls
129 lines (107 loc) · 4.42 KB
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
package foon
import (
"testing"
"github.com/stretchr/testify/assert"
"time"
)
type embedded struct {
ID string `foon:"id"`
CreatedAt time.Time `foon:"createdAt"`
UpdatedAt time.Time `foon:"updatedAt"`
}
type testNormal struct {
__kind string `foon:"collection,KindTest"`
ID string `foon:"id"`
Name string `firestore:"name"`
CreatedAt time.Time `foon:"createdAt"`
UpdatedAt time.Time `foon:"updatedAt"`
}
type testEmbedded struct {
__kind string `foon:"collection,KindEmbedded"`
embedded
Name string `firestore:"name"`
}
func TestCollection_ノーマルのテスト(t *testing.T) {
normal := &testNormal{ID: "test", Name: "name"}
col , err := newFields(normal)
assert.NoError(t, err, "failed to create fields")
assert.Equal(t, "KindTest", col.CollectionName())
}
func TestCollection_Embeddedがされてる場合のテスト(t *testing.T) {
normal := &testEmbedded{embedded: embedded{ID: "test"}, Name: "name"}
col , err := newFields(normal)
assert.NoError(t, err, "failed to create fields")
assert.Equal(t, "KindEmbedded", col.CollectionName())
}
func TestIdField_ノーマルのテスト(t *testing.T) {
test := &testNormal{ID: "test"}
fld , err := newIDField(test)
assert.NoError(t, err , "Failed to get Fields")
assert.Equal(t, "ID", fld.FieldName)
assert.Equal(t , "test", fld.ID)
assert.Equal(t , "test", test.ID)
fld.SetID("bcde")
assert.Equal(t , "bcde", fld.ID)
assert.Equal(t , "bcde", test.ID)
}
func TestIdField_Embeddedがされてる場合のテスト(t *testing.T) {
test := &testEmbedded{embedded: embedded{ID: "test"}, Name: "name"}
fld , err := newIDField(test)
assert.NoError(t, err , "Failed to get Fields")
assert.Equal(t , "test", test.ID, "data failed")
if !assert.Equal(t , "test", fld.ID, "field is not reflected.") {
t.Fatal("field is not reflected")
}
fld.SetID("bcde")
print("bbb")
assert.Equal(t , "bcde", test.ID, "data is not changed.")
assert.Equal(t , "bcde", fld.ID, "field is not changed.")
}
func TestDateField_正常に変更されるかどうか(t *testing.T) {
test := &testNormal{ID: "test"}
create := newCreateField(test)
update := newUpdatedField(test)
assert.True(t, create.has(), "createdAt is not initialized")
assert.True(t, update.has(), "updatedAt is not initialized")
assert.True(t, test.CreatedAt.IsZero(), "data is invalid")
assert.True(t, test.UpdatedAt.IsZero(), "data is invalid")
assert.True(t, create.get().IsZero(), "creaetdAt is not zero")
assert.True(t, update.get().IsZero(), "updatedAt is not zero")
now := time.Now()
create.UpdateTime(now)
update.UpdateTime(now)
assert.Equal(t, create.get(), now, "createdAt is not synced.")
assert.Equal(t, update.get(), now, "updatedAt is not synced.")
assert.Equal(t, test.CreatedAt, now, "createdAt(data) is not synced.")
assert.Equal(t, test.UpdatedAt , now, "updatedAt(data) is not synced.")
now2 := now.Add(300)
create.UpdateTime(now2)
update.UpdateTime(now2)
assert.Equal(t, create.get(), now, "createdAt is not synced.")
assert.Equal(t, update.get(), now2, "updatedAt is not synced.")
assert.Equal(t, test.CreatedAt, now, "createdAt(data) is not synced.")
assert.Equal(t, test.UpdatedAt , now2, "updatedAt(data) is not synced.")
}
func TestDateField_Embeddedも正常に変更されるかどうか(t *testing.T) {
test := &testEmbedded{embedded: embedded{ID: "test"}, Name: "name"}
info , err := newFields(test)
assert.NoError(t, err , "failed to create fields")
assert.True(t, info.createdAt.has(), "createdAt is not initialized")
assert.True(t, info.updaetdAt.has(), "updatedAt is not initialized")
assert.True(t, test.CreatedAt.IsZero(), "data is invalid")
assert.True(t, test.UpdatedAt.IsZero(), "data is invalid")
assert.True(t, info.createdAt.get().IsZero(), "creaetdAt is not zero")
assert.True(t, info.updaetdAt.get().IsZero(), "updatedAt is not zero")
now := time.Now()
info.UpdateTime(now)
assert.Equal(t, info.createdAt.get(), now, "createdAt is not synced.")
assert.Equal(t, info.updaetdAt.get(), now, "updatedAt is not synced.")
assert.Equal(t, test.CreatedAt, now, "createdAt(data) is not synced.")
assert.Equal(t, test.UpdatedAt , now, "updatedAt(data) is not synced.")
now2 := now.Add(300)
info.UpdateTime(now2)
assert.Equal(t, info.createdAt.get(), now, "createdAt is not synced.")
assert.Equal(t, info.updaetdAt.get(), now2, "updatedAt is not synced.")
assert.Equal(t, test.CreatedAt, now, "createdAt(data) is not synced.")
assert.Equal(t, test.UpdatedAt , now2, "updatedAt(data) is not synced.")
}