github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/core/ledger/kvledger/txmgmt/statedb/statecouchdb/query_wrapper_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package statecouchdb 18 19 import ( 20 "strings" 21 "testing" 22 23 "github.com/hyperledger/fabric/common/ledger/testutil" 24 ) 25 26 // TestSimpleQuery tests a simple query 27 func TestSimpleQuery(t *testing.T) { 28 29 rawQuery := []byte(`{"selector":{"owner":{"$eq":"jerry"}},"limit": 10,"skip": 0}`) 30 31 wrappedQuery, err := ApplyQueryWrapper("ns1", string(rawQuery), 10000, 0) 32 33 //Make sure the query did not throw an exception 34 testutil.AssertNoError(t, err, "Unexpected error thrown when for query JSON") 35 36 //There should be one wrapped field 37 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.owner\""), 1) 38 39 //$eg operator should be unchanged 40 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$eq\""), 1) 41 42 //value should be unchanged 43 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"jerry\""), 1) 44 45 } 46 47 // TestSimpleQuery tests a query with a leading operator 48 func TestQueryWithOperator(t *testing.T) { 49 50 rawQuery := []byte(`{"selector":{"$or":[{"owner":{"$eq":"jerry"}},{"owner": {"$eq": "frank"}}]},"limit": 10,"skip": 0}`) 51 52 wrappedQuery, err := ApplyQueryWrapper("ns1", string(rawQuery), 10000, 0) 53 54 //Make sure the query did not throw an exception 55 testutil.AssertNoError(t, err, "Unexpected error thrown when for query JSON") 56 57 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.owner\""), 2) 58 59 //$or operator should be unchanged 60 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$or\""), 1) 61 62 //$eq operator should be unchanged 63 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$eq\""), 2) 64 65 //value should be unchanged 66 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"jerry\""), 1) 67 68 } 69 70 // TestQueryWithImplicitOperatorAndExplicitOperator tests an implicit operator and an explicit operator 71 func TestQueryWithImplicitOperatorAndExplicitOperator(t *testing.T) { 72 73 rawQuery := []byte(`{"selector":{"color":"green","$or":[{"owner":"fred"},{"owner":"mary"}]}}`) 74 75 wrappedQuery, err := ApplyQueryWrapper("ns1", string(rawQuery), 10000, 0) 76 77 //Make sure the query did not throw an exception 78 testutil.AssertNoError(t, err, "Unexpected error thrown when for query JSON") 79 80 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.owner\""), 2) 81 82 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.color\""), 1) 83 84 //$or operator should be unchanged 85 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$or\""), 1) 86 87 //value should be unchanged 88 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"fred\""), 1) 89 90 //value should be unchanged 91 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"mary\""), 1) 92 93 } 94 95 // TestQueryWithFields tests a query with specified fields 96 func TestQueryWithFields(t *testing.T) { 97 98 rawQuery := []byte(`{"selector":{"owner": {"$eq": "tom"}},"fields": ["owner", "asset_name", "color", "size"], "limit": 10, "skip": 0}`) 99 100 wrappedQuery, err := ApplyQueryWrapper("ns1", string(rawQuery), 10000, 0) 101 102 //Make sure the query did not throw an exception 103 testutil.AssertNoError(t, err, "Unexpected error thrown when for query JSON") 104 105 //$eq operator should be unchanged 106 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$eq\""), 1) 107 108 //field value should be wrapped 109 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.owner\""), 2) 110 111 //field value should be wrapped 112 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.asset_name\""), 1) 113 114 //field value should be wrapped 115 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.color\""), 1) 116 117 //field value should be wrapped 118 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.size\""), 1) 119 120 } 121 122 //TestQueryWithSortFields tests sorting fields 123 func TestQueryWithSortFields(t *testing.T) { 124 125 rawQuery := []byte(`{"selector":{"owner": {"$eq": "tom"}},"fields": ["owner", "asset_name", "color", "size"], "sort": ["size", "color"], "limit": 10, "skip": 0}`) 126 127 wrappedQuery, err := ApplyQueryWrapper("ns1", string(rawQuery), 10000, 0) 128 129 //Make sure the query did not throw an exception 130 testutil.AssertNoError(t, err, "Unexpected error thrown when for query JSON") 131 132 //$eq operator should be unchanged 133 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$eq\""), 1) 134 135 //field value should be wrapped 136 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.owner\""), 2) 137 138 //field value should be wrapped 139 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.asset_name\""), 1) 140 141 //field value should be wrapped 142 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.color\""), 2) 143 144 //field value should be wrapped 145 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.size\""), 2) 146 147 } 148 149 //TestQueryWithSortObjects tests a sort objects 150 func TestQueryWithSortObjects(t *testing.T) { 151 152 rawQuery := []byte(`{"selector":{"owner": {"$eq": "tom"}},"fields": ["owner", "asset_name", "color", "size"], "sort": [{"size": "desc"}, {"color": "desc"}], "limit": 10, "skip": 0}`) 153 154 wrappedQuery, err := ApplyQueryWrapper("ns1", string(rawQuery), 10000, 0) 155 156 //Make sure the query did not throw an exception 157 testutil.AssertNoError(t, err, "Unexpected error thrown when for query JSON") 158 159 //$eq operator should be unchanged 160 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$eq\""), 1) 161 162 //field value should be wrapped 163 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.owner\""), 2) 164 165 //field value should be wrapped 166 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.asset_name\""), 1) 167 168 //field value should be wrapped 169 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.color\""), 2) 170 171 //field value should be wrapped 172 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.size\""), 2) 173 174 //sort descriptor should be unchanged 175 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"desc\""), 2) 176 177 } 178 179 //TestQueryLeadingOperator tests a leading operator 180 func TestQueryLeadingOperator(t *testing.T) { 181 182 rawQuery := []byte(`{"selector": 183 {"$and":[ 184 {"size": { 185 "$gte": 2 186 }}, 187 {"size": { 188 "$lte": 5 189 }} 190 ] 191 } 192 }`) 193 194 wrappedQuery, err := ApplyQueryWrapper("ns1", string(rawQuery), 10000, 0) 195 196 //Make sure the query did not throw an exception 197 testutil.AssertNoError(t, err, "Unexpected error thrown when for query JSON") 198 199 //$and operator should be unchanged 200 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$and\""), 2) 201 202 //$gte operator should be unchanged 203 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$gte\""), 1) 204 205 //$lte operator should be unchanged 206 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$lte\""), 1) 207 208 //field value should be wrapped 209 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.size\""), 2) 210 211 } 212 213 //TestQueryLeadingOperator tests a leading operator and embedded operator 214 func TestQueryLeadingAndEmbeddedOperator(t *testing.T) { 215 216 rawQuery := []byte(`{"selector":{ 217 "$and": [ 218 { "size": {"$gte": 10}}, 219 { "size": {"$lte": 20}}, 220 { "$not": {"size": 15}} 221 ] 222 }}`) 223 224 wrappedQuery, err := ApplyQueryWrapper("ns1", string(rawQuery), 10000, 0) 225 226 //Make sure the query did not throw an exception 227 testutil.AssertNoError(t, err, "Unexpected error thrown when for query JSON") 228 229 //$and operator should be unchanged 230 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$and\""), 2) 231 232 //$gte operator should be unchanged 233 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$gte\""), 1) 234 235 //$lte operator should be unchanged 236 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$lte\""), 1) 237 238 //$not operator should be unchanged 239 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$not\""), 1) 240 241 //field value should be wrapped 242 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.size\""), 3) 243 244 } 245 246 //TestQueryEmbeddedOperatorAndArrayOfObjects an embedded operator and object array 247 func TestQueryEmbeddedOperatorAndArrayOfObjects(t *testing.T) { 248 249 rawQuery := []byte(`{ 250 "selector": { 251 "$and": [ 252 { 253 "size": {"$gte": 10} 254 }, 255 { 256 "size": {"$lte": 30} 257 }, 258 { 259 "$nor": [ 260 {"size": 15}, 261 {"size": 18}, 262 {"size": 22} 263 ] 264 } 265 ] 266 } 267 }`) 268 269 wrappedQuery, err := ApplyQueryWrapper("ns1", string(rawQuery), 10000, 0) 270 271 //Make sure the query did not throw an exception 272 testutil.AssertNoError(t, err, "Unexpected error thrown when for query JSON") 273 274 //$and operator should be unchanged 275 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$and\""), 2) 276 277 //$gte operator should be unchanged 278 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$gte\""), 1) 279 280 //$lte operator should be unchanged 281 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$lte\""), 1) 282 283 //field value should be wrapped 284 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.size\""), 5) 285 286 } 287 288 //TestQueryEmbeddedOperatorAndArrayOfValues tests an array of values 289 func TestQueryEmbeddedOperatorAndArrayOfValues(t *testing.T) { 290 291 rawQuery := []byte(`{ 292 "selector": { 293 "size": {"$gt": 10}, 294 "owner": { 295 "$all": ["bob","fred"] 296 } 297 } 298 }`) 299 300 wrappedQuery, err := ApplyQueryWrapper("ns1", string(rawQuery), 10000, 0) 301 302 //Make sure the query did not throw an exception 303 testutil.AssertNoError(t, err, "Unexpected error thrown when for query JSON") 304 305 //$gt operator should be unchanged 306 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$gt\""), 1) 307 308 //$all operator should be unchanged 309 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"$all\""), 1) 310 311 //field value should be wrapped 312 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.size\""), 1) 313 314 //field value should be wrapped 315 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"data.owner\""), 1) 316 317 //value should be unchanged 318 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"bob\""), 1) 319 320 //value should be unchanged 321 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"fred\""), 1) 322 323 } 324 325 //TestQueryNoSelector with no selector specified 326 func TestQueryNoSelector(t *testing.T) { 327 328 rawQuery := []byte(`{"fields": ["owner", "asset_name", "color", "size"]}`) 329 330 wrappedQuery, err := ApplyQueryWrapper("ns1", string(rawQuery), 10000, 0) 331 332 //Make sure the query did not throw an exception 333 testutil.AssertNoError(t, err, "Unexpected error thrown when for query JSON") 334 335 //check to make sure the default selector is added 336 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"selector\":{\"chaincodeid\":\"ns1\"}"), 1) 337 338 } 339 340 //TestQueryWithUseDesignDoc tests query with index design doc specified 341 func TestQueryWithUseDesignDoc(t *testing.T) { 342 343 rawQuery := []byte(`{"selector":{"owner":{"$eq":"jerry"}},"use_index":"_design/testDoc","limit": 10,"skip": 0}`) 344 345 wrappedQuery, err := ApplyQueryWrapper("ns1", string(rawQuery), 10000, 0) 346 347 //Make sure the query did not throw an exception 348 testutil.AssertNoError(t, err, "Unexpected error thrown when for query JSON") 349 350 //check to make sure the default selector is added 351 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"use_index\":\"_design/testDoc\""), 1) 352 353 } 354 355 //TestQueryWithUseDesignDocAndIndexName tests query with index design doc and index name specified 356 func TestQueryWithUseDesignDocAndIndexName(t *testing.T) { 357 358 rawQuery := []byte(`{"selector":{"owner":{"$eq":"jerry"}},"use_index":["_design/testDoc","testIndexName"],"limit": 10,"skip": 0}`) 359 360 wrappedQuery, err := ApplyQueryWrapper("ns1", string(rawQuery), 10000, 0) 361 362 //Make sure the query did not throw an exception 363 testutil.AssertNoError(t, err, "Unexpected error thrown when for query JSON") 364 365 //check to make sure the default selector is added 366 testutil.AssertEquals(t, strings.Count(wrappedQuery, "\"use_index\":[\"_design/testDoc\",\"testIndexName\"]"), 1) 367 368 } 369 370 //TestQueryWithLargeInteger tests query with large integer 371 func TestQueryWithLargeInteger(t *testing.T) { 372 373 rawQuery := []byte(`{"selector":{"$and":[{"size":{"$eq": 1000007}}]}}`) 374 375 wrappedQuery, err := ApplyQueryWrapper("ns1", string(rawQuery), 10000, 0) 376 377 //Make sure the query did not throw an exception 378 testutil.AssertNoError(t, err, "Unexpected error thrown when for query JSON") 379 380 //check to make sure the default selector is added 381 testutil.AssertEquals(t, strings.Count(wrappedQuery, "{\"$eq\":1000007}"), 1) 382 383 }