@@ -86,6 +86,144 @@ func (suite *OnPremTestSuite) TestNamespacesOp() {
86
86
suite .doDDLTest ("drop namespace NSABC" , nosqlerr .NoError )
87
87
}
88
88
89
+ // TestNamespacesOp tests create, drop and show namespaces operations.
90
+ func (suite * OnPremTestSuite ) TestDefaultNamespaces () {
91
+
92
+ // this only works with versions of KV with default namespace support
93
+ if suite .Config .Version <= "22.3.31" {
94
+ return
95
+ }
96
+
97
+ suite .Client .RequestConfig .Namespace = "mydns"
98
+ suite .doDDLTest ("create namespace mydns" , nosqlerr .NoError )
99
+
100
+ // parent in mydns
101
+ stmt := "create table parent(sid integer, id integer, name string, " +
102
+ "salary long, primary key(SHARD(sid), id))"
103
+ suite .CreateTable (stmt , nil )
104
+
105
+ // child in mydns
106
+ stmt = "create table parent.child(cid integer, cname string, " +
107
+ "primary key(cid))"
108
+ suite .CreateTable (stmt , nil )
109
+
110
+ // test ListTables with default namespace: should return just myns
111
+ req := & nosqldb.ListTablesRequest {}
112
+ res , err := suite .Client .ListTables (req )
113
+ if suite .NoErrorf (err , "ListTables failed: %v" , err ) {
114
+ suite .Equalf (2 , len (res .Tables ), "unexpected number of tables returned" )
115
+ }
116
+
117
+ // test ListTables with explicit namespace
118
+ req = & nosqldb.ListTablesRequest {Namespace : "mydns" }
119
+ res , err = suite .Client .ListTables (req )
120
+ if suite .NoErrorf (err , "ListTables failed: %v" , err ) {
121
+ suite .Equalf (2 , len (res .Tables ), "unexpected number of tables returned" )
122
+ }
123
+
124
+ // test ListTables with explicit invalid
125
+ req = & nosqldb.ListTablesRequest {Namespace : "invalid" }
126
+ res , err = suite .Client .ListTables (req )
127
+ if suite .NoErrorf (err , "ListTables failed: %v" , err ) {
128
+ suite .Equalf (0 , len (res .Tables ), "expected zero tables returned for invalid namespace" )
129
+ }
130
+
131
+ suite .Client .RequestConfig .Namespace = ""
132
+
133
+ // test ListTables with no namespace: should return all
134
+ req = & nosqldb.ListTablesRequest {}
135
+ res , err = suite .Client .ListTables (req )
136
+ if suite .NoErrorf (err , "ListTables failed: %v" , err ) {
137
+ // should have more than 2 tables listed
138
+ if len (res .Tables ) <= 2 {
139
+ suite .Fail ("Expected more than 2 tables, got %d" , len (res .Tables ))
140
+ }
141
+ }
142
+
143
+ suite .Client .RequestConfig .Namespace = "mydns"
144
+
145
+ // put data in both tables
146
+ for i := 0 ; i < 10 ; i ++ {
147
+ value := & types.MapValue {}
148
+ value .Put ("id" , i ).Put ("name" , "pname" )
149
+ value .Put ("sid" , i ).Put ("salary" , i * 1000 )
150
+ putReq := & nosqldb.PutRequest {
151
+ TableName : "parent" ,
152
+ Value : value ,
153
+ }
154
+ //putRes, err := suite.Client.Put(putReq)
155
+ _ , err := suite .Client .Put (putReq )
156
+ suite .NoErrorf (err , "Parent put failed: %v" , err )
157
+ for j := 0 ; j < 10 ; j ++ {
158
+ value .Put ("cid" , j ).Put ("cname" , fmt .Sprintf ("cname%d" , j ))
159
+ putReq .TableName = "parent.child"
160
+ putReq .Value = value
161
+ //putRes, err = suite.Client.Put(putReq)
162
+ _ , err = suite .Client .Put (putReq )
163
+ suite .NoErrorf (err , "Child put failed: %v" , err )
164
+ }
165
+ }
166
+
167
+ // get parent
168
+ key := types .ToMapValue ("id" , 1 ).Put ("sid" , 1 )
169
+ getReq := & nosqldb.GetRequest {TableName : "parent" , Key : key }
170
+ _ , err = suite .Client .Get (getReq )
171
+ suite .NoErrorf (err , "Error trying to get record from parent: %v" , err )
172
+
173
+ // get child
174
+ key = types .ToMapValue ("id" , 1 ).Put ("sid" , 1 ).Put ("cid" , 1 )
175
+ getReq = & nosqldb.GetRequest {TableName : "parent.child" , Key : key }
176
+ _ , err = suite .Client .Get (getReq )
177
+ suite .NoErrorf (err , "Error trying to get record from child: %v" , err )
178
+
179
+ // same ops should fail with no default namespace
180
+ suite .Client .RequestConfig .Namespace = ""
181
+ // get parent
182
+ key = types .ToMapValue ("id" , 1 ).Put ("sid" , 1 )
183
+ getReq = & nosqldb.GetRequest {TableName : "parent" , Key : key }
184
+ _ , err = suite .Client .Get (getReq )
185
+ suite .Errorf (err , "Expected error trying to get record from parent, got none" )
186
+
187
+ // get child
188
+ key = types .ToMapValue ("id" , 1 ).Put ("sid" , 1 ).Put ("cid" , 1 )
189
+ getReq = & nosqldb.GetRequest {TableName : "parent.child" , Key : key }
190
+ _ , err = suite .Client .Get (getReq )
191
+ suite .Errorf (err , "Expected error trying to get record from child, got none" )
192
+
193
+ // verify namespace in tablename overrides default
194
+ key = types .ToMapValue ("id" , 1 ).Put ("sid" , 1 ).Put ("cid" , 1 )
195
+ getReq = & nosqldb.GetRequest {TableName : "mydns:parent.child" , Key : key , Namespace : "invalid" }
196
+ _ , err = suite .Client .Get (getReq )
197
+ suite .NoErrorf (err , "Error trying to get record from child: %v" , err )
198
+
199
+
200
+ // query parent
201
+ stmt = "select * from parent"
202
+ suite .Client .RequestConfig .Namespace = "mydns"
203
+ suite .DoQueryWithNamespace (stmt , "" , 10 )
204
+ suite .Client .RequestConfig .Namespace = "invalid"
205
+ suite .DoQueryWithNamespace (stmt , "mydns" , 10 )
206
+
207
+ // query child
208
+ stmt = "select * from parent.child"
209
+ suite .Client .RequestConfig .Namespace = "mydns"
210
+ suite .DoQueryWithNamespace (stmt , "" , 100 )
211
+ suite .Client .RequestConfig .Namespace = "invalid"
212
+ suite .DoQueryWithNamespace (stmt , "mydns" , 100 )
213
+
214
+ // test complex query (exercises internal request copying)
215
+ suite .Client .RequestConfig .Namespace = ""
216
+ stmt = "select sid, count(*) as cnt, sum(salary) as sum " +
217
+ "from parent group by sid" ;
218
+ suite .DoQueryWithNamespace (stmt , "mydns" , 10 )
219
+
220
+ // drop table with namespace in request
221
+ suite .DropTableWithNamespace ("parent.child" , false , "mydns" )
222
+
223
+ // drop namespace - use cascade to remove tables
224
+ suite .doDDLTest ("drop namespace mydns cascade" , nosqlerr .NoError )
225
+ }
226
+
89
227
// TestUserRolesOp tests create/drop/show/list users, create/drop/show/list roles
90
228
// and grant/revoke roles operations.
91
229
//
0 commit comments