From 335d56720fcd81eb2e7a6dab21d18386d6d68d65 Mon Sep 17 00:00:00 2001 From: jwierzbo Date: Tue, 27 Sep 2022 13:34:12 +0200 Subject: [PATCH] GT-201 Add Rename View support --- CHANGELOG.md | 1 + test/view_test.go | 49 +++++++++++++++++++++++--------------- view.go | 3 +++ view_arangosearch.go | 2 +- view_arangosearch_alias.go | 2 +- view_impl.go | 28 ++++++++++++++++++++++ 6 files changed, 64 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 542aedea..f1ab5b72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - Add support for Pregel API - Add tests to check support for Enterprise Graphs - Search View v2 (`search-alias`) +- Add Rename View support ## [1.3.3](https://github.com/arangodb/go-driver/tree/v1.3.3) (2022-07-27) - Fix `lastValue` field type diff --git a/test/view_test.go b/test/view_test.go index 1118f030..b7d463e4 100644 --- a/test/view_test.go +++ b/test/view_test.go @@ -394,33 +394,44 @@ func TestGetArangoSearchViews(t *testing.T) { } } -// TestRemoveArangoSearchView creates an arangosearch view and then removes it. -func TestRemoveArangoSearchView(t *testing.T) { +// TestRenameAndRemoveArangoSearchView creates an arangosearch view and then removes it. +func TestRenameAndRemoveArangoSearchView(t *testing.T) { ctx := context.Background() c := createClientFromEnv(t, true) skipBelowVersion(c, "3.4", t) db := ensureDatabase(ctx, c, "view_test", nil, t) - name := "test_remove_asview" + name := "test_rename_view" v, err := db.CreateArangoSearchView(ctx, name, nil) - if err != nil { - t.Fatalf("Failed to create collection '%s': %s", name, describe(err)) - } + require.NoError(t, err) + // View must exist now - if found, err := db.ViewExists(ctx, name); err != nil { - t.Errorf("ViewExists('%s') failed: %s", name, describe(err)) - } else if !found { - t.Errorf("ViewExists('%s') return false, expected true", name) - } + found, err := db.ViewExists(ctx, name) + require.NoError(t, err) + require.True(t, found) + + // Rename view + newName := "test_rename_view_new" + err = v.Rename(ctx, newName) + require.NoError(t, err) + require.Equal(t, newName, v.Name()) + + // Renamed View must exist + found, err = db.ViewExists(ctx, newName) + require.NoError(t, err) + require.True(t, found) + // Now remove it - if err := v.Remove(ctx); err != nil { - t.Fatalf("Failed to remove view '%s': %s", name, describe(err)) - } + err = v.Remove(ctx) + require.NoError(t, err) + // View must not exist now - if found, err := db.ViewExists(ctx, name); err != nil { - t.Errorf("ViewExists('%s') failed: %s", name, describe(err)) - } else if found { - t.Errorf("ViewExists('%s') return true, expected false", name) - } + found, err = db.ViewExists(ctx, name) + require.NoError(t, err) + require.False(t, found) + + found, err = db.ViewExists(ctx, newName) + require.NoError(t, err) + require.False(t, found) } // TestUseArangoSearchView tries to create a view and actually use it in diff --git a/view.go b/view.go index b7aca1ea..82193078 100644 --- a/view.go +++ b/view.go @@ -46,6 +46,9 @@ type View interface { // Database returns the database containing the view. Database() Database + // Rename renames the view. + Rename(ctx context.Context, newName string) error + // Remove removes the entire view. // If the view does not exist, a NotFoundError is returned. Remove(ctx context.Context) error diff --git a/view_arangosearch.go b/view_arangosearch.go index 87bd258c..92a79e7d 100644 --- a/view_arangosearch.go +++ b/view_arangosearch.go @@ -29,7 +29,7 @@ import ( // ArangoSearchView provides access to the information of a view. // Views are only available in ArangoDB 3.4 and higher. type ArangoSearchView interface { - // Include generic View functions + // View Includes generic View functions View // Properties fetches extended information about the view. diff --git a/view_arangosearch_alias.go b/view_arangosearch_alias.go index 71995871..0e701196 100644 --- a/view_arangosearch_alias.go +++ b/view_arangosearch_alias.go @@ -29,7 +29,7 @@ import ( // ArangoSearchViewAlias provides access to the information of a view alias // Views aliases are only available in ArangoDB 3.10 and higher. type ArangoSearchViewAlias interface { - // View Include generic View functions + // View Includes generic View functions View // Properties fetches extended information about the view. diff --git a/view_impl.go b/view_impl.go index 7c39d033..153e6926 100644 --- a/view_impl.go +++ b/view_impl.go @@ -92,6 +92,34 @@ func (v *view) Database() Database { return v.db } +func (v *view) Rename(ctx context.Context, newName string) error { + if newName == "" { + return WithStack(InvalidArgumentError{Message: "newName is empty"}) + } + req, err := v.conn.NewRequest("PUT", path.Join(v.relPath(), "rename")) + if err != nil { + return WithStack(err) + } + input := struct { + Name string `json:"name"` + }{ + Name: newName, + } + if _, err := req.SetBody(input); err != nil { + return WithStack(err) + } + applyContextSettings(ctx, req) + resp, err := v.conn.Do(ctx, req) + if err != nil { + return WithStack(err) + } + if err := resp.CheckStatus(200); err != nil { + return WithStack(err) + } + v.name = newName + return nil +} + // Remove removes the entire view. // If the view does not exist, a NotFoundError is returned. func (v *view) Remove(ctx context.Context) error {