From 308a6536e593910651c27463bc8bb8afcaffa610 Mon Sep 17 00:00:00 2001 From: Tomasz Mielech Date: Sat, 13 Jun 2020 07:48:05 +0200 Subject: [PATCH] Create agency subkeys --- .travis.yml | 2 +- Makefile | 10 ++++---- agency/operation.go | 15 +++++++++++- agency/operation_test.go | 51 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 agency/operation_test.go diff --git a/.travis.yml b/.travis.yml index 30b8d93f..676fa825 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,7 +28,7 @@ services: language: go env: - - TEST_SUITE=run-tests-http + - TEST_SUITE=run-unit-tests - TEST_SUITE=run-tests-single ARANGODB=arangodb:3.5 - TEST_SUITE=run-tests-single ARANGODB=arangodb/arangodb:latest - TEST_SUITE=run-tests-single ARANGODB=arangodb/arangodb-preview:latest diff --git a/Makefile b/Makefile index 61c2ee66..3ba4f6cc 100644 --- a/Makefile +++ b/Makefile @@ -128,17 +128,19 @@ changelog: --no-author \ --unreleased-label "Master" -run-tests: run-tests-http run-tests-single run-tests-resilientsingle run-tests-cluster +run-tests: run-unit-tests run-tests-single run-tests-resilientsingle run-tests-cluster -# Tests of HTTP package -run-tests-http: +# The below rule exists only for backward compatibility. +run-tests-http: run-unit-tests + +run-unit-tests: @docker run \ --rm \ -v "${ROOTDIR}":/usr/code \ -e CGO_ENABLED=0 \ -w /usr/code/ \ golang:$(GOVERSION) \ - go test $(TESTOPTIONS) $(REPOPATH)/http + go test $(TESTOPTIONS) $(REPOPATH)/http $(REPOPATH)/agency # Single server tests run-tests-single: run-tests-single-json run-tests-single-vpack run-tests-single-vst-1.0 $(VST11_SINGLE_TESTS) diff --git a/agency/operation.go b/agency/operation.go index b7dd0127..0d525c64 100644 --- a/agency/operation.go +++ b/agency/operation.go @@ -24,6 +24,8 @@ package agency import "time" +type Key []string + // KeyChanger describes how operation should be performed on a key in the agency type KeyChanger interface { // GetKey returns which key must be changed @@ -41,7 +43,18 @@ type KeyChanger interface { } type keyCommon struct { - key []string + key Key +} + +// CreateSubKey creates new key based on receiver key. +// Returns new key with new allocated memory. +func (k Key) CreateSubKey(elements ...string) Key { + NewKey := make([]string, 0, len(k)+len(elements)) + + NewKey = append(NewKey, k...) + NewKey = append(NewKey, elements...) + + return NewKey } func (k *keyCommon) GetKey() string { diff --git a/agency/operation_test.go b/agency/operation_test.go new file mode 100644 index 00000000..7fe7bc05 --- /dev/null +++ b/agency/operation_test.go @@ -0,0 +1,51 @@ +package agency_test + +import ( + "github.com/arangodb/go-driver/agency" + "github.com/stretchr/testify/require" + "testing" +) + +func TestCreateSubKey(t *testing.T) { + testCases := []struct { + name string + elements []string + key agency.Key + }{ + { + name: "Create a new key based on not empty key with not empty elements", + key: agency.Key{"level1", "level2"}, + elements: []string{"level3"}, + }, + { + name: "Create a new key based on not empty key with empty elements", + key: agency.Key{"level1", "level2"}, + }, + { + name: "Create a new key based on empty key", + elements: []string{"level3"}, + }, + { + name: "Create a new key based on empty key with empty elements", + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + newKey := testCase.key.CreateSubKey(testCase.elements...) + + require.Len(t, newKey, len(testCase.key)+len(testCase.elements)) + if len(testCase.key) > 0 && &testCase.key[0] == &newKey[0] { + require.Fail(t, "New key should have always different address") + } + + for i, s := range testCase.key { + require.Equal(t, s, newKey[i]) + } + for i, s := range testCase.elements { + require.Equal(t, s, newKey[i+len(testCase.key)]) + } + }) + } + +}