|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/arangodb/go-driver" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +// ensureArangoSearchView is a helper to check if an arangosearch view exists and create it if needed. |
| 13 | +// It will fail the test when an error occurs. |
| 14 | +func ensureArangoSearchAliasView(ctx context.Context, db driver.Database, name string, options *driver.ArangoSearchAliasViewProperties, t testEnv) driver.ArangoSearchViewAlias { |
| 15 | + v, err := db.View(ctx, name) |
| 16 | + if driver.IsNotFound(err) { |
| 17 | + v, err = db.CreateArangoSearchAliasView(ctx, name, options) |
| 18 | + if err != nil { |
| 19 | + t.Fatalf("Failed to create arangosearch view '%s': %s", name, describe(err)) |
| 20 | + } |
| 21 | + } else if err != nil { |
| 22 | + t.Fatalf("Failed to open view '%s': %s", name, describe(err)) |
| 23 | + } |
| 24 | + result, err := v.ArangoSearchViewAlias() |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("Failed to open view '%s' as arangosearch view: %s", name, describe(err)) |
| 27 | + } |
| 28 | + return result |
| 29 | +} |
| 30 | + |
| 31 | +// TestSearchViewsAlias tests the arangosearch view alias methods |
| 32 | +func TestSearchViewsAlias(t *testing.T) { |
| 33 | + ctx := context.Background() |
| 34 | + c := createClientFromEnv(t, true) |
| 35 | + skipBelowVersion(c, "3.10", t) |
| 36 | + skipBelowVersion(c, "3.10", t) |
| 37 | + db := ensureDatabase(ctx, c, "search_view_test_basic", nil, t) |
| 38 | + |
| 39 | + nameAlias := "test_add_collection_view_alias" |
| 40 | + nameCol := "col_in_alias_view" |
| 41 | + nameInvInd := "inv_index_alias_view" |
| 42 | + |
| 43 | + col := ensureCollection(ctx, db, nameCol, nil, t) |
| 44 | + v := ensureArangoSearchAliasView(ctx, db, nameAlias, nil, t) |
| 45 | + |
| 46 | + p, err := v.Properties(ctx) |
| 47 | + require.NoError(t, err) |
| 48 | + require.Equal(t, p.Type, driver.ViewTypeArangoSearchAlias) |
| 49 | + require.Equal(t, p.Name, nameAlias) |
| 50 | + require.Len(t, p.Indexes, 0) |
| 51 | + |
| 52 | + _, err = v.ArangoSearchView() |
| 53 | + require.Error(t, err) |
| 54 | + |
| 55 | + indexOpt := driver.InvertedIndexOptions{ |
| 56 | + Name: nameInvInd, |
| 57 | + PrimarySort: driver.InvertedIndexPrimarySort{ |
| 58 | + Fields: []driver.ArangoSearchPrimarySortEntry{ |
| 59 | + {Field: "test1", Ascending: newBool(true)}, |
| 60 | + {Field: "test2", Ascending: newBool(false)}, |
| 61 | + }, |
| 62 | + Compression: driver.PrimarySortCompressionLz4, |
| 63 | + }, |
| 64 | + Fields: []driver.InvertedIndexField{ |
| 65 | + {Name: "field1", Features: []driver.ArangoSearchAnalyzerFeature{driver.ArangoSearchAnalyzerFeatureFrequency}, Nested: nil}, |
| 66 | + {Name: "field2", Features: []driver.ArangoSearchAnalyzerFeature{driver.ArangoSearchAnalyzerFeaturePosition}, TrackListPositions: false, Nested: nil}, |
| 67 | + }, |
| 68 | + } |
| 69 | + idx, created, err := col.EnsureInvertedIndex(ctx, &indexOpt) |
| 70 | + require.NoError(t, err) |
| 71 | + require.True(t, created) |
| 72 | + require.Equal(t, nameInvInd, idx.UserName()) |
| 73 | + |
| 74 | + opt := driver.ArangoSearchAliasViewProperties{ |
| 75 | + Indexes: []driver.ArangoSearchAliasIndex{ |
| 76 | + { |
| 77 | + Collection: nameCol, |
| 78 | + Index: nameInvInd, |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + p, err = v.SetProperties(ctx, opt) |
| 83 | + require.NoError(t, err) |
| 84 | + require.Equal(t, p.Type, driver.ViewTypeArangoSearchAlias) |
| 85 | + require.Equal(t, p.Name, nameAlias) |
| 86 | + require.Len(t, p.Indexes, 1) |
| 87 | + require.Equal(t, p.Indexes[0].Collection, nameCol) |
| 88 | + require.Equal(t, p.Indexes[0].Index, nameInvInd) |
| 89 | + |
| 90 | + p, err = v.Properties(ctx) |
| 91 | + require.NoError(t, err) |
| 92 | + require.Equal(t, p.Type, driver.ViewTypeArangoSearchAlias) |
| 93 | + require.Equal(t, p.Name, nameAlias) |
| 94 | + require.Len(t, p.Indexes, 1) |
| 95 | + |
| 96 | + views, err := db.Views(ctx) |
| 97 | + require.NoError(t, err) |
| 98 | + require.Len(t, views, 1) |
| 99 | + |
| 100 | + exist, err := db.ViewExists(ctx, nameAlias) |
| 101 | + require.NoError(t, err) |
| 102 | + require.True(t, exist) |
| 103 | + |
| 104 | + vv, err := db.View(ctx, nameAlias) |
| 105 | + require.NoError(t, err) |
| 106 | + require.Equal(t, vv.Name(), nameAlias) |
| 107 | + |
| 108 | + err = v.Remove(ctx) |
| 109 | + require.NoError(t, err) |
| 110 | + |
| 111 | + views, err = db.Views(ctx) |
| 112 | + require.NoError(t, err) |
| 113 | + require.Len(t, views, 0) |
| 114 | +} |
0 commit comments