@@ -168,6 +168,103 @@ func TestCollection_CacheEnabled(t *testing.T) {
168
168
})
169
169
}
170
170
171
+ // TestCollection_ComputedValues
172
+ func TestCollection_ComputedValues (t * testing.T ) {
173
+ c := createClientFromEnv (t , true )
174
+ skipBelowVersion (c , "3.10" , t )
175
+ db := ensureDatabase (nil , c , "collection_test_computed_values" , nil , t )
176
+
177
+ t .Run ("Create with ComputedValues" , func (t * testing.T ) {
178
+ name := "test_users_computed_values"
179
+
180
+ // Add an attribute with the creation timestamp to new documents
181
+ computedValue := driver.ComputedValue {
182
+ Name : "createdAt" ,
183
+ Expression : "RETURN DATE_NOW()" ,
184
+ Overwrite : true ,
185
+ ComputeOn : []driver.ComputeOn {driver .ComputeOnInsert },
186
+ }
187
+
188
+ _ , err := db .CreateCollection (nil , name , & driver.CreateCollectionOptions {
189
+ ComputedValues : []driver.ComputedValue {computedValue },
190
+ })
191
+ require .NoError (t , err )
192
+
193
+ // Collection must exist now
194
+ col , err := db .Collection (nil , name )
195
+ require .NoError (t , err )
196
+
197
+ prop , err := col .Properties (nil )
198
+ require .NoError (t , err )
199
+
200
+ // Check if the computed value is in the list of computed values
201
+ require .Len (t , prop .ComputedValues , 1 )
202
+ require .Equal (t , computedValue .Name , prop .ComputedValues [0 ].Name )
203
+ require .Equal (t , computedValue .Expression , prop .ComputedValues [0 ].Expression )
204
+
205
+ // Create a document
206
+ doc := UserDoc {Name : fmt .Sprintf ("Jakub" )}
207
+ meta , err := col .CreateDocument (nil , doc )
208
+ if err != nil {
209
+ t .Fatalf ("Failed to create document: %s" , describe (err ))
210
+ }
211
+
212
+ // Read document
213
+ var readDoc map [string ]interface {}
214
+ if _ , err := col .ReadDocument (nil , meta .Key , & readDoc ); err != nil {
215
+ t .Fatalf ("Failed to read document '%s': %s" , meta .Key , describe (err ))
216
+ }
217
+
218
+ require .Equal (t , doc .Name , readDoc ["name" ])
219
+
220
+ // Verify that the computed value is set
221
+ createdAtValue , createdAtIsPresent := readDoc ["createdAt" ]
222
+ require .True (t , createdAtIsPresent )
223
+
224
+ // Verify that the computed value is a valid date
225
+ tm := time .Unix (int64 (createdAtValue .(float64 )), 0 )
226
+ require .True (t , tm .After (time .Now ().Add (- time .Second )))
227
+ })
228
+
229
+ t .Run ("Update to ComputedValues" , func (t * testing.T ) {
230
+ name := "test_update_computed_values"
231
+
232
+ // Add an attribute with the creation timestamp to new documents
233
+ computedValue := driver.ComputedValue {
234
+ Name : "createdAt" ,
235
+ Expression : "RETURN DATE_NOW()" ,
236
+ Overwrite : true ,
237
+ ComputeOn : []driver.ComputeOn {driver .ComputeOnInsert },
238
+ }
239
+
240
+ _ , err := db .CreateCollection (nil , name , nil )
241
+ require .NoError (t , err )
242
+
243
+ // Collection must exist now
244
+ col , err := db .Collection (nil , name )
245
+ require .NoError (t , err )
246
+
247
+ prop , err := col .Properties (nil )
248
+ require .NoError (t , err )
249
+
250
+ require .Len (t , prop .ComputedValues , 0 )
251
+
252
+ err = col .SetProperties (nil , driver.SetCollectionPropertiesOptions {
253
+ ComputedValues : []driver.ComputedValue {computedValue },
254
+ })
255
+ require .NoError (t , err )
256
+
257
+ // Check if the computed value is in the list of computed values
258
+ col , err = db .Collection (nil , name )
259
+ require .NoError (t , err )
260
+
261
+ prop , err = col .Properties (nil )
262
+ require .NoError (t , err )
263
+
264
+ require .Len (t , prop .ComputedValues , 1 )
265
+ })
266
+ }
267
+
171
268
// TestCreateSatelliteCollection create a satellite collection
172
269
func TestCreateSatelliteCollection (t * testing.T ) {
173
270
skipNoEnterprise (t )
0 commit comments