1// Copyright 2013-2020 Aerospike, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package aerospike_test
16
17import (
18	as "github.com/aerospike/aerospike-client-go"
19
20	. "github.com/onsi/ginkgo"
21	. "github.com/onsi/gomega"
22)
23
24// ALL tests are isolated by SetName and Key, which are 50 random characters
25var _ = Describe("Complex Index operations test", func() {
26
27	Describe("Complex Index Creation", func() {
28		// connection data
29		var err error
30		var ns = *namespace
31		var set = randString(50)
32		var key *as.Key
33		var wpolicy = as.NewWritePolicy(0, 0)
34
35		const keyCount = 1000
36
37		valueList := []interface{}{1, 2, 3, "a", "ab", "abc"}
38		valueMap := map[interface{}]interface{}{"a": "b", 0: 1, 1: "a", "b": 2}
39
40		bin1 := as.NewBin("Aerospike1", valueList)
41		bin2 := as.NewBin("Aerospike2", valueMap)
42
43		BeforeEach(func() {
44			for i := 0; i < keyCount; i++ {
45				key, err = as.NewKey(ns, set, randString(50))
46				Expect(err).ToNot(HaveOccurred())
47
48				err = client.PutBins(wpolicy, key, bin1, bin2)
49				Expect(err).ToNot(HaveOccurred())
50			}
51		})
52
53		Context("Create non-existing complex index", func() {
54
55			It("must create a complex Index for Lists", func() {
56				idxTask, err := client.CreateComplexIndex(wpolicy, ns, set, set+bin1.Name, bin1.Name, as.STRING, as.ICT_LIST)
57				Expect(err).ToNot(HaveOccurred())
58				defer client.DropIndex(wpolicy, ns, set, set+bin1.Name)
59
60				// wait until index is created
61				<-idxTask.OnComplete()
62
63				// no duplicate index is allowed
64				_, err = client.CreateIndex(wpolicy, ns, set, set+bin1.Name, bin1.Name, as.STRING)
65				Expect(err).To(HaveOccurred())
66			})
67
68			It("must create a complex Index for Map Keys", func() {
69				idxTask, err := client.CreateComplexIndex(wpolicy, ns, set, set+bin2.Name+"keys", bin2.Name, as.STRING, as.ICT_MAPKEYS)
70				Expect(err).ToNot(HaveOccurred())
71				defer client.DropIndex(wpolicy, ns, set, set+bin2.Name+"keys")
72
73				// wait until index is created
74				<-idxTask.OnComplete()
75
76				// no duplicate index is allowed
77				_, err = client.CreateIndex(wpolicy, ns, set, set+bin2.Name+"keys", bin1.Name, as.STRING)
78				Expect(err).To(HaveOccurred())
79			})
80
81			It("must create a complex Index for Map Values", func() {
82				idxTask, err := client.CreateComplexIndex(wpolicy, ns, set, set+bin2.Name+"values", bin2.Name, as.STRING, as.ICT_MAPVALUES)
83				Expect(err).ToNot(HaveOccurred())
84				defer client.DropIndex(wpolicy, ns, set, set+bin2.Name+"values")
85
86				// wait until index is created
87				<-idxTask.OnComplete()
88
89				// no duplicate index is allowed
90				_, err = client.CreateIndex(wpolicy, ns, set, set+bin2.Name+"values", bin1.Name, as.STRING)
91				Expect(err).To(HaveOccurred())
92			})
93
94		})
95
96	})
97})
98