Skip to content

Commit 4dd0e70

Browse files
authored
GT-149 Optional computeOn field in computedValues (#418)
1 parent 761827c commit 4dd0e70

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [master](https://github.com/arangodb/go-driver/tree/master) (N/A)
44
- Add `hex` property to analyzer's properties
55
- Add support for `computedValues`
6+
- Optional `computeOn` field in `computedValues`
67

78
## [1.3.3](https://github.com/arangodb/go-driver/tree/v1.3.3) (2022-07-27)
89
- Fix `lastValue` field type

database_collections.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ type ComputedValue struct {
154154
// An array of strings to define on which write operations the value shall be
155155
// computed. The possible values are `"insert"`, `"update"`, and `"replace"`.
156156
// The default is `["insert", "update", "replace"]`.
157-
ComputeOn []ComputeOn `json:"computeOn"`
157+
ComputeOn []ComputeOn `json:"computeOn,omitempty"`
158158
// Whether the computed value shall take precedence over a user-provided or existing attribute.
159159
Overwrite bool `json:"overwrite"`
160160
// Whether to let the write operation fail if the expression produces a warning. The default is false.

test/collection_test.go

+55-1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ func TestCollection_ComputedValues(t *testing.T) {
200200
// Check if the computed value is in the list of computed values
201201
require.Len(t, prop.ComputedValues, 1)
202202
require.Equal(t, computedValue.Name, prop.ComputedValues[0].Name)
203+
require.Len(t, prop.ComputedValues[0].ComputeOn, 1)
204+
require.Equal(t, computedValue.ComputeOn[0], prop.ComputedValues[0].ComputeOn[0])
203205
require.Equal(t, computedValue.Expression, prop.ComputedValues[0].Expression)
204206

205207
// Create a document
@@ -222,7 +224,20 @@ func TestCollection_ComputedValues(t *testing.T) {
222224
require.True(t, createdAtIsPresent)
223225

224226
// Verify that the computed value is a valid date
225-
tm := time.Unix(int64(createdAtValue.(float64)), 0)
227+
var createdAtValueInt64 int64
228+
229+
switch cav := createdAtValue.(type) {
230+
case int64:
231+
createdAtValueInt64 = cav
232+
case float64:
233+
createdAtValueInt64 = int64(cav)
234+
case uint64:
235+
createdAtValueInt64 = int64(cav)
236+
default:
237+
t.Fatalf("Unexpected type of createdAt value: %T", createdAtValue)
238+
}
239+
240+
tm := time.Unix(createdAtValueInt64, 0)
226241
require.True(t, tm.After(time.Now().Add(-time.Second)))
227242
})
228243

@@ -263,6 +278,45 @@ func TestCollection_ComputedValues(t *testing.T) {
263278

264279
require.Len(t, prop.ComputedValues, 1)
265280
})
281+
282+
t.Run("Use default ComputeOn values in ComputedValues", func(t *testing.T) {
283+
name := "test_default_computeon_computed_values"
284+
285+
// Add an attribute with the creation timestamp to new documents
286+
computedValue := driver.ComputedValue{
287+
Name: "createdAt",
288+
Expression: "RETURN DATE_NOW()",
289+
Overwrite: true,
290+
}
291+
292+
_, err := db.CreateCollection(nil, name, nil)
293+
require.NoError(t, err)
294+
295+
// Collection must exist now
296+
col, err := db.Collection(nil, name)
297+
require.NoError(t, err)
298+
299+
prop, err := col.Properties(nil)
300+
require.NoError(t, err)
301+
302+
require.Len(t, prop.ComputedValues, 0)
303+
304+
err = col.SetProperties(nil, driver.SetCollectionPropertiesOptions{
305+
ComputedValues: []driver.ComputedValue{computedValue},
306+
})
307+
require.NoError(t, err)
308+
309+
// Check if the computed value is in the list of computed values
310+
col, err = db.Collection(nil, name)
311+
require.NoError(t, err)
312+
313+
prop, err = col.Properties(nil)
314+
require.NoError(t, err)
315+
316+
require.Len(t, prop.ComputedValues, 1)
317+
// we should get the default value for ComputeOn - ["insert", "update", "replace"]
318+
require.Len(t, prop.ComputedValues[0].ComputeOn, 3)
319+
})
266320
}
267321

268322
// TestCreateSatelliteCollection create a satellite collection

0 commit comments

Comments
 (0)