-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitems_test.go
162 lines (132 loc) · 4.52 KB
/
items_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 directus
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
type Regime struct {
ID string `json:"id,omitempty"`
Code string `json:"code,omitempty"`
Status string `json:"status"`
}
type RegimeWithTranslations struct {
Code string `json:"code,omitempty"`
Status string `json:"status,omitempty"`
Translations []Relation[RegimeTranslation] `json:"translations,omitempty"`
}
type RegimeTranslation struct {
Lang string `json:"languages_code"`
DisplayName string `json:"display_name"`
}
func TestItemsList(t *testing.T) {
items := NewItemsClient[Regime](initClient(t), "regimes")
regimes, err := items.List(context.Background())
require.NoError(t, err)
require.NotEmpty(t, regimes)
for _, regime := range regimes {
fmt.Printf("%#v\n", regime)
}
}
func TestItemsListFields(t *testing.T) {
items := NewItemsClient[Regime](initClient(t), "regimes", WithFields("id", "code"))
regimes, err := items.List(context.Background())
require.NoError(t, err)
require.NotEmpty(t, regimes)
for _, regime := range regimes {
require.Empty(t, regime.Status)
fmt.Printf("%#v\n", regime)
}
}
func TestItemsGet(t *testing.T) {
items := NewItemsClient[Regime](initClient(t), "regimes")
regime, err := items.Get(context.Background(), "1158c0ee-bd5d-4e7b-a640-8bceca82b5db")
require.NoError(t, err)
require.NotEmpty(t, regime)
fmt.Printf("%#v\n", regime)
}
func TestItemsGetFields(t *testing.T) {
items := NewItemsClient[Regime](initClient(t), "regimes", WithFields("id", "code"))
regime, err := items.Get(context.Background(), "1158c0ee-bd5d-4e7b-a640-8bceca82b5db")
require.NoError(t, err)
require.NotEmpty(t, regime)
require.Empty(t, regime.Status)
fmt.Printf("%#v\n", regime)
}
func TestItemsGetNotFound(t *testing.T) {
items := NewItemsClient[Regime](initClient(t), "regimes")
regime, err := items.Get(context.Background(), "foo")
require.Nil(t, regime)
require.EqualError(t, err, "directus: item not found: foo")
}
func TestItemsCreate(t *testing.T) {
items := NewItemsClient[Regime](initClient(t), "regimes")
regime, err := items.Create(context.Background(), &Regime{
Code: "test",
Status: "published",
})
require.NoError(t, err)
require.NotEmpty(t, regime)
fmt.Printf("%#v\n", regime)
}
func TestItemsCreateRelationship(t *testing.T) {
items := NewItemsClient[RegimeWithTranslations](initClient(t), "regimes")
regime, err := items.Create(context.Background(), &RegimeWithTranslations{
Code: "test",
Translations: []Relation[RegimeTranslation]{
NewRelation(&RegimeTranslation{Lang: "en-GB", DisplayName: "Test"}),
NewRelation(&RegimeTranslation{Lang: "es-ES", DisplayName: "Test"}),
},
})
require.NoError(t, err)
require.NotEmpty(t, regime)
fmt.Printf("%#v\n", regime)
}
func TestItemsUpdate(t *testing.T) {
items := NewItemsClient[Regime](initClient(t), "regimes")
regime, err := items.Update(context.Background(), "1158c0ee-bd5d-4e7b-a640-8bceca82b5db", &Regime{
Status: "draft",
})
require.NoError(t, err)
require.NotEmpty(t, regime)
fmt.Printf("%#v\n", regime)
}
func TestItemsUpdateFields(t *testing.T) {
items := NewItemsClient[RegimeWithTranslations](initClient(t), "regimes", WithFields("*", "translations.*"))
req := &RegimeWithTranslations{
Status: "draft",
}
regime, err := items.Update(context.Background(), "1918e55e-8ad0-42bf-bc38-26304f0a6b1b", req)
require.NoError(t, err)
require.NotEmpty(t, regime)
fmt.Printf("%+v\n", regime)
fmt.Printf("%+v\n", regime.Translations[0].Value())
}
func TestItemsRelationNumeric(t *testing.T) {
items := NewItemsClient[RegimeWithTranslations](initClient(t), "regimes", WithFields("*"))
regime, err := items.Get(context.Background(), "1158c0ee-bd5d-4e7b-a640-8bceca82b5db")
require.NoError(t, err)
require.NotEmpty(t, regime)
fmt.Printf("%+v\n", regime)
}
func TestItemsRelationValue(t *testing.T) {
items := NewItemsClient[RegimeWithTranslations](initClient(t), "regimes", WithFields("*", "translations.*"))
regime, err := items.Get(context.Background(), "1158c0ee-bd5d-4e7b-a640-8bceca82b5db")
require.NoError(t, err)
require.NotEmpty(t, regime)
fmt.Printf("%+v\n", regime)
fmt.Printf("%+v\n", regime.Translations[0].Value())
}
type PMSRoom struct {
ID string `json:"id"`
Code string `json:"code"`
DisplayName string `json:"display_name"`
}
func TestItemsFilter(t *testing.T) {
items := NewItemsClient[PMSRoom](initClient(t), "pms_rooms")
rooms, err := items.Filter(context.Background(), Gt("max_people", 3))
require.NoError(t, err)
for _, room := range rooms {
fmt.Printf("%#v\n", room)
}
}