-
Notifications
You must be signed in to change notification settings - Fork 1
/
save_test.go
126 lines (103 loc) · 3.19 KB
/
save_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
package makroud_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/ulule/loukoum/v3"
"github.com/ulule/makroud"
)
func TestSave_Owl(t *testing.T) {
Setup(t)(func(driver makroud.Driver) {
ctx := context.Background()
is := require.New(t)
name := "Kika"
featherColor := "white"
favoriteFood := "Tomato"
owl := &Owl{
Name: name,
FeatherColor: featherColor,
FavoriteFood: favoriteFood,
}
err := makroud.Save(ctx, driver, owl)
is.NoError(err)
is.NotEmpty(owl.ID)
id := owl.ID
query := loukoum.Select("*").From("ztp_owl").Where(loukoum.Condition("id").Equal(id))
last := &Owl{}
err = makroud.Exec(ctx, driver, query, last)
is.NoError(err)
is.Equal(name, last.Name)
is.Equal(featherColor, last.FeatherColor)
is.Equal(favoriteFood, last.FavoriteFood)
favoriteFood = "Chocolate Cake"
owl.FavoriteFood = favoriteFood
err = makroud.Save(ctx, driver, owl)
is.NoError(err)
query = loukoum.Select("*").From("ztp_owl").Where(loukoum.Condition("id").Equal(id))
last = &Owl{}
err = makroud.Exec(ctx, driver, query, last)
is.NoError(err)
is.Equal(name, last.Name)
is.Equal(featherColor, last.FeatherColor)
is.Equal(favoriteFood, last.FavoriteFood)
})
}
func TestSave_Meow(t *testing.T) {
Setup(t)(func(driver makroud.Driver) {
ctx := context.Background()
is := require.New(t)
cat := &Cat{
Name: "Hemlock",
}
err := makroud.Save(ctx, driver, cat)
is.NoError(err)
is.NotEmpty(cat.ID)
t0 := time.Now()
body := "meow"
meow := &Meow{
Body: body,
CatID: cat.ID,
}
err = makroud.Save(ctx, driver, meow)
is.NoError(err)
is.NotEmpty(meow.Hash)
is.True(t0.Before(meow.CreatedAt))
is.True(t0.Before(meow.UpdatedAt))
is.True(time.Now().After(meow.CreatedAt))
is.True(time.Now().After(meow.UpdatedAt))
is.False(meow.DeletedAt.Valid)
hash := meow.Hash
query := loukoum.Select("*").From("ztp_meow").Where(loukoum.Condition("hash").Equal(hash))
last := &Meow{}
err = makroud.Exec(ctx, driver, query, last)
is.NoError(err)
is.Equal(body, last.Body)
is.Equal(cat.ID, last.CatID)
is.Equal(meow.CreatedAt.UnixNano(), last.CreatedAt.UnixNano())
is.Equal(meow.UpdatedAt.UnixNano(), last.UpdatedAt.UnixNano())
is.Equal(meow.DeletedAt.Valid, last.DeletedAt.Valid)
is.Equal(meow.DeletedAt.Time.UnixNano(), last.DeletedAt.Time.UnixNano())
t1 := time.Now()
body = "meow meow!"
meow.Body = body
err = makroud.Save(ctx, driver, meow)
is.NoError(err)
is.True(t0.Before(meow.CreatedAt))
is.True(t0.Before(meow.UpdatedAt))
is.True(t1.After(meow.CreatedAt))
is.True(t1.Before(meow.UpdatedAt))
is.True(time.Now().After(meow.CreatedAt))
is.True(time.Now().After(meow.UpdatedAt))
query = loukoum.Select("*").From("ztp_meow").Where(loukoum.Condition("hash").Equal(hash))
last = &Meow{}
err = makroud.Exec(ctx, driver, query, last)
is.NoError(err)
is.Equal(body, last.Body)
is.Equal(cat.ID, last.CatID)
is.Equal(meow.CreatedAt.UnixNano(), last.CreatedAt.UnixNano())
is.Equal(meow.UpdatedAt.UnixNano(), last.UpdatedAt.UnixNano())
is.Equal(meow.DeletedAt.Valid, last.DeletedAt.Valid)
is.Equal(meow.DeletedAt.Time.UnixNano(), last.DeletedAt.Time.UnixNano())
})
}