query tests
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/3afd3b55 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/3afd3b55 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/3afd3b55 Branch: refs/heads/ignite-7777 Commit: 3afd3b559b485edcd26123f08c5bfec755ae521d Parents: 289a83b Author: ekaterina-nbl <[email protected]> Authored: Tue May 8 21:07:55 2018 +0300 Committer: ekaterina-nbl <[email protected]> Committed: Tue May 8 21:07:55 2018 +0300 ---------------------------------------------------------------------- .../nodejs/examples/SqlQueryExample.js | 1 - modules/platforms/nodejs/lib/Cursor.js | 50 ++-- modules/platforms/nodejs/lib/Query.js | 8 +- .../nodejs/lib/internal/BinaryUtils.js | 5 +- .../spec/cache/CachePutGetDiffTypes.spec.js | 8 +- .../nodejs/spec/query/ScanQuery.spec.js | 207 ++++++++++++++++ .../nodejs/spec/query/SqlFieldsQuery.spec.js | 247 +++++++++++++++++++ .../nodejs/spec/query/SqlQuery.spec.js | 247 +++++++++++++++++++ 8 files changed, 736 insertions(+), 37 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/3afd3b55/modules/platforms/nodejs/examples/SqlQueryExample.js ---------------------------------------------------------------------- diff --git a/modules/platforms/nodejs/examples/SqlQueryExample.js b/modules/platforms/nodejs/examples/SqlQueryExample.js index 5f8d40d..a3ae12a 100644 --- a/modules/platforms/nodejs/examples/SqlQueryExample.js +++ b/modules/platforms/nodejs/examples/SqlQueryExample.js @@ -66,7 +66,6 @@ class SqlQueryExample { const cacheCfg = new CacheConfiguration(). setQueryEntities( new QueryEntity(). - setKeyTypeName('INT'). setValueTypeName('Person'). setFields([ new QueryField('id', 'java.lang.Integer'), http://git-wip-us.apache.org/repos/asf/ignite/blob/3afd3b55/modules/platforms/nodejs/lib/Cursor.js ---------------------------------------------------------------------- diff --git a/modules/platforms/nodejs/lib/Cursor.js b/modules/platforms/nodejs/lib/Cursor.js index dfdb4ed..b97a82f 100644 --- a/modules/platforms/nodejs/lib/Cursor.js +++ b/modules/platforms/nodejs/lib/Cursor.js @@ -49,7 +49,7 @@ class Cursor { await this._getValues(); this._valueIndex = 0; } - if (this._values) { + if (this._values && this._values.length > 0) { const value = this._values[this._valueIndex]; this._valueIndex++; return value; @@ -162,27 +162,31 @@ class Cursor { buffer.writeLong(this._id); } + /** + * @ignore + */ + _readId(buffer) { + this._id = buffer.readLong(); + } /** * @ignore */ - async _read(buffer) { - const id = buffer.readLong(); - if (this._id) { - if (!this._id.equals(id)) { - throw Errors.IgniteClientError.internalError(); - } - } - else { - this._id = id; - } + async _readRow(buffer) { const CacheEntry = require('./CacheClient').CacheEntry; + return new CacheEntry( + await BinaryReader.readObject(buffer, this._keyType), + await BinaryReader.readObject(buffer, this._valueType)); + } + + /** + * @ignore + */ + async _read(buffer) { const rowCount = buffer.readInteger(); this._values = new Array(rowCount); for (let i = 0; i < rowCount; i++) { - this._values[i] = new CacheEntry( - await BinaryReader.readObject(buffer, this._keyType), - await BinaryReader.readObject(buffer, this._valueType)); + this._values[i] = await this._readRow(buffer); } this._hasNext = buffer.readBoolean(); } @@ -289,20 +293,14 @@ class SqlFieldsCursor extends Cursor { /** * @ignore */ - async _read(buffer) { - const rowCount = buffer.readInteger(); - this._values = new Array(rowCount); - let values; + async _readRow(buffer) { + let values = new Array(this._fieldCount); let fieldType; - for (let i = 0; i < rowCount; i++) { - values = new Array(this._fieldCount); - for (let j = 0; j < this._fieldCount; j++) { - fieldType = this._fieldTypes && j < this._fieldTypes.length ? this._fieldTypes[j] : null; - values[j] = await BinaryReader.readObject(buffer); - } - this._values[i] = values; + for (let i = 0; i < this._fieldCount; i++) { + fieldType = this._fieldTypes && i < this._fieldTypes.length ? this._fieldTypes[i] : null; + values[i] = await BinaryReader.readObject(buffer); } - this._hasNext = buffer.readBoolean(); + return values; } } http://git-wip-us.apache.org/repos/asf/ignite/blob/3afd3b55/modules/platforms/nodejs/lib/Query.js ---------------------------------------------------------------------- diff --git a/modules/platforms/nodejs/lib/Query.js b/modules/platforms/nodejs/lib/Query.js index e17e0c3..3458899 100644 --- a/modules/platforms/nodejs/lib/Query.js +++ b/modules/platforms/nodejs/lib/Query.js @@ -249,7 +249,9 @@ class SqlQuery extends Query { * @ignore */ async _getCursor(socket, payload, keyType = null, valueType = null) { - return new Cursor(socket, BinaryUtils.OPERATION.QUERY_SQL_CURSOR_GET_PAGE, payload, keyType, valueType); + const cursor = new Cursor(socket, BinaryUtils.OPERATION.QUERY_SQL_CURSOR_GET_PAGE, payload, keyType, valueType); + cursor._readId(payload); + return cursor; } } @@ -492,7 +494,9 @@ class ScanQuery extends Query { * @ignore */ async _getCursor(socket, payload, keyType = null, valueType = null) { - return new Cursor(socket, BinaryUtils.OPERATION.QUERY_SCAN_CURSOR_GET_PAGE, payload, keyType, valueType); + const cursor = new Cursor(socket, BinaryUtils.OPERATION.QUERY_SCAN_CURSOR_GET_PAGE, payload, keyType, valueType); + cursor._readId(payload); + return cursor; } } http://git-wip-us.apache.org/repos/asf/ignite/blob/3afd3b55/modules/platforms/nodejs/lib/internal/BinaryUtils.js ---------------------------------------------------------------------- diff --git a/modules/platforms/nodejs/lib/internal/BinaryUtils.js b/modules/platforms/nodejs/lib/internal/BinaryUtils.js index b602cbe..ab2d40c 100644 --- a/modules/platforms/nodejs/lib/internal/BinaryUtils.js +++ b/modules/platforms/nodejs/lib/internal/BinaryUtils.js @@ -365,10 +365,7 @@ class BinaryUtils { return; } const valueTypeCode = BinaryUtils.getTypeCode(BinaryUtils.calcObjectType(value)); - if (typeCode === BinaryUtils.TYPE_CODE.BINARY_OBJECT && - valueTypeCode === BinaryUtils.TYPE_CODE.COMPLEX_OBJECT || - typeCode !== BinaryUtils.TYPE_CODE.BINARY_OBJECT && - valueTypeCode === BinaryUtils.TYPE_CODE.BINARY_OBJECT) { + if (typeCode !== valueTypeCode) { throw Errors.IgniteClientError.typeCastError(valueTypeCode, typeCode); } } http://git-wip-us.apache.org/repos/asf/ignite/blob/3afd3b55/modules/platforms/nodejs/spec/cache/CachePutGetDiffTypes.spec.js ---------------------------------------------------------------------- diff --git a/modules/platforms/nodejs/spec/cache/CachePutGetDiffTypes.spec.js b/modules/platforms/nodejs/spec/cache/CachePutGetDiffTypes.spec.js index 6e75078..eb0f2be 100644 --- a/modules/platforms/nodejs/spec/cache/CachePutGetDiffTypes.spec.js +++ b/modules/platforms/nodejs/spec/cache/CachePutGetDiffTypes.spec.js @@ -441,7 +441,7 @@ describe('cache put get test suite >', () => { } let array = new Array(); - for (let i = 0; i < 10; i++) { + for (let i = 0; i < 5; i++) { for (let field in object) { const type = parseInt(field.substring(5)); object[field] = TestingHelper.primitiveValues[type].modificator(object[field]); @@ -468,7 +468,7 @@ describe('cache put get test suite >', () => { } const objectType = new ComplexObjectType(object, 'tstComplObjectWithDefaultFieldTypes'); let array = new Array(); - for (let i = 0; i < 10; i++) { + for (let i = 0; i < 5; i++) { for (let field in object) { const type = parseInt(field.substring(5)); object[field] = TestingHelper.primitiveValues[type].modificator(object[field]); @@ -492,7 +492,7 @@ describe('cache put get test suite >', () => { binObject.setField('field' + type, typeInfo.values[0], type); } let array = new Array(); - for (let i = 0; i < 10; i++) { + for (let i = 0; i < 5; i++) { for (let field of binObject.getFieldNames()) { const type = parseInt(field.substring(5)); binObject.setField( @@ -525,7 +525,7 @@ describe('cache put get test suite >', () => { } let array = new Array(); - for (let i = 0; i < 4; i++) { + for (let i = 0; i < 2; i++) { let innerArray = new Array(); for (let j = 0; j < 2; j++) { for (let field in object) { http://git-wip-us.apache.org/repos/asf/ignite/blob/3afd3b55/modules/platforms/nodejs/spec/query/ScanQuery.spec.js ---------------------------------------------------------------------- diff --git a/modules/platforms/nodejs/spec/query/ScanQuery.spec.js b/modules/platforms/nodejs/spec/query/ScanQuery.spec.js new file mode 100644 index 0000000..ab28973 --- /dev/null +++ b/modules/platforms/nodejs/spec/query/ScanQuery.spec.js @@ -0,0 +1,207 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +require('jasmine-expect'); + +const config = require('../config'); +const TestingHelper = require('../TestingHelper'); +const IgniteClient = require('apache-ignite-client'); +const Errors = IgniteClient.Errors; +const ScanQuery = IgniteClient.ScanQuery; +const ObjectType = IgniteClient.ObjectType; + +const CACHE_NAME = '__test_cache'; +const ELEMENTS_NUMBER = 10; + +describe('scan query test suite >', () => { + let igniteClient = null; + + beforeAll((done) => { + Promise.resolve(). + then(async () => { + await TestingHelper.init(); + igniteClient = TestingHelper.igniteClient; + await testSuiteCleanup(done); + await igniteClient.getOrCreateCache(CACHE_NAME); + await generateData(done); + }). + then(done). + catch(error => done.fail(error)); + }, TestingHelper.TIMEOUT); + + afterAll((done) => { + Promise.resolve(). + then(async () => { + await testSuiteCleanup(done); + await TestingHelper.cleanUp(); + }). + then(done). + catch(error => done()); + }, TestingHelper.TIMEOUT); + + it('get all', (done) => { + Promise.resolve(). + then(async () => { + let cache = igniteClient.getCache(CACHE_NAME); + const cursor = await cache.query(new ScanQuery()); + const set = new Set(); + for (let cacheEntry of await cursor.getAll()) { + expect(generateValue(cacheEntry.getKey()) === cacheEntry.getValue()).toBe(true); + set.add(cacheEntry.getKey()); + expect(cacheEntry.getKey() >= 0 && cacheEntry.getKey() < ELEMENTS_NUMBER).toBe(true); + } + expect(set.size).toBe(ELEMENTS_NUMBER); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('get all with page size', (done) => { + Promise.resolve(). + then(async () => { + let cache = igniteClient.getCache(CACHE_NAME); + const cursor = await cache.query(new ScanQuery().setPageSize(1)); + const set = new Set(); + for (let cacheEntry of await cursor.getAll()) { + expect(generateValue(cacheEntry.getKey()) === cacheEntry.getValue()).toBe(true); + set.add(cacheEntry.getKey()); + expect(cacheEntry.getKey() >= 0 && cacheEntry.getKey() < ELEMENTS_NUMBER).toBe(true); + } + expect(set.size).toBe(ELEMENTS_NUMBER); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('get value', (done) => { + Promise.resolve(). + then(async () => { + let cache = igniteClient.getCache(CACHE_NAME); + const cursor = await cache.query(new ScanQuery()); + const set = new Set(); + do { + let cacheEntry = await cursor.getValue(); + expect(generateValue(cacheEntry.getKey()) === cacheEntry.getValue()).toBe(true); + set.add(cacheEntry.getKey()); + expect(cacheEntry.getKey() >= 0 && cacheEntry.getKey() < ELEMENTS_NUMBER).toBe(true); + } while (cursor.hasMore()); + expect(set.size).toBe(ELEMENTS_NUMBER); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('get value with page size', (done) => { + Promise.resolve(). + then(async () => { + let cache = igniteClient.getCache(CACHE_NAME); + const cursor = await cache.query(new ScanQuery().setPageSize(2)); + const set = new Set(); + do { + let cacheEntry = await cursor.getValue(); + expect(generateValue(cacheEntry.getKey()) === cacheEntry.getValue()).toBe(true); + set.add(cacheEntry.getKey()); + expect(cacheEntry.getKey() >= 0 && cacheEntry.getKey() < ELEMENTS_NUMBER).toBe(true); + } while (cursor.hasMore()); + expect(set.size).toBe(ELEMENTS_NUMBER); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('close cursor', (done) => { + Promise.resolve(). + then(async () => { + let cache = igniteClient.getCache(CACHE_NAME); + const cursor = await cache.query(new ScanQuery().setPageSize(1)); + await cursor.getValue(); + await cursor.close(); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('close cursor after get all', (done) => { + Promise.resolve(). + then(async () => { + let cache = igniteClient.getCache(CACHE_NAME); + const cursor = await cache.query(new ScanQuery()); + await cursor.getAll(); + await cursor.close(); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('scan query settings', (done) => { + Promise.resolve(). + then(async () => { + let cache = igniteClient.getCache(CACHE_NAME); + const cursor = await cache.query(new ScanQuery(). + setPartitionNumber(0). + setPageSize(2). + setLocal(true)); + await cursor.getAll(); + await cursor.close(); + }). + then(done). + catch(error => done.fail(error)); + }); + + + it('scan empty cache', (done) => { + Promise.resolve(). + then(async () => { + let cache = igniteClient.getCache(CACHE_NAME); + await cache.removeAll(); + let cursor = await cache.query(new ScanQuery()); + const cacheEntries = await cursor.getAll(); + expect(cacheEntries.length).toBe(0); + await cursor.close(); + + cursor = await cache.query(new ScanQuery()); + expect(await cursor.getValue()).toBe(null); + expect(cursor.hasMore()).toBe(false); + await cursor.close(); + }). + then(done). + catch(error => done.fail(error)); + }); + + async function testSuiteCleanup(done) { + await TestingHelper.destroyCache(CACHE_NAME, done); + } + + async function generateData(done) { + try { + let cache = igniteClient.getCache(CACHE_NAME). + setKeyType(ObjectType.PRIMITIVE_TYPE.INTEGER); + for (let i = 0; i < ELEMENTS_NUMBER; i++) { + await cache.put(i, generateValue(i)); + } + } + catch (err) { + done.fail('unexpected error: ' + err); + } + } + + function generateValue(key) { + return 'value' + key; + } +}); http://git-wip-us.apache.org/repos/asf/ignite/blob/3afd3b55/modules/platforms/nodejs/spec/query/SqlFieldsQuery.spec.js ---------------------------------------------------------------------- diff --git a/modules/platforms/nodejs/spec/query/SqlFieldsQuery.spec.js b/modules/platforms/nodejs/spec/query/SqlFieldsQuery.spec.js new file mode 100644 index 0000000..c81838a --- /dev/null +++ b/modules/platforms/nodejs/spec/query/SqlFieldsQuery.spec.js @@ -0,0 +1,247 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +require('jasmine-expect'); + +const config = require('../config'); +const TestingHelper = require('../TestingHelper'); +const IgniteClient = require('apache-ignite-client'); +const Errors = IgniteClient.Errors; +const SqlFieldsQuery = IgniteClient.SqlFieldsQuery; +const ObjectType = IgniteClient.ObjectType; +const CacheConfiguration = IgniteClient.CacheConfiguration; + +const CACHE_NAME = '__test_cache'; +const TABLE_NAME = '__test_SqlFieldsQuery_table'; +const ELEMENTS_NUMBER = 10; + +describe('sql fields query test suite >', () => { + let igniteClient = null; + + beforeAll((done) => { + Promise.resolve(). + then(async () => { + await TestingHelper.init(); + igniteClient = TestingHelper.igniteClient; + await testSuiteCleanup(done); + await igniteClient.getOrCreateCache(CACHE_NAME, new CacheConfiguration().setSqlSchema('PUBLIC')); + await generateData(done); + }). + then(done). + catch(error => done.fail(error)); + }, TestingHelper.TIMEOUT); + + afterAll((done) => { + Promise.resolve(). + then(async () => { + await dropTables(done); + await testSuiteCleanup(done); + await TestingHelper.cleanUp(); + }). + then(done). + catch(error => done()); + }, TestingHelper.TIMEOUT); + + it('get all', (done) => { + Promise.resolve(). + then(async () => { + let cache = igniteClient.getCache(CACHE_NAME); + const cursor = await cache.query( + new SqlFieldsQuery(`SELECT * FROM ${TABLE_NAME}`)); + const set = new Set(); + for (let fields of await cursor.getAll()) { + expect(fields.length).toBe(2); + expect(generateValue(fields[0]) === fields[1]).toBe(true); + set.add(fields[0]); + expect(fields[0] >= 0 && fields[0] < ELEMENTS_NUMBER).toBe(true); + } + expect(set.size).toBe(ELEMENTS_NUMBER); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('get all with page size', (done) => { + Promise.resolve(). + then(async () => { + let cache = igniteClient.getCache(CACHE_NAME); + const cursor = await cache.query( + new SqlFieldsQuery(`SELECT * FROM ${TABLE_NAME}`).setPageSize(1)); + const set = new Set(); + for (let fields of await cursor.getAll()) { + expect(fields.length).toBe(2); + expect(generateValue(fields[0]) === fields[1]).toBe(true); + set.add(fields[0]); + expect(fields[0] >= 0 && fields[0] < ELEMENTS_NUMBER).toBe(true); + } + expect(set.size).toBe(ELEMENTS_NUMBER); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('get value', (done) => { + Promise.resolve(). + then(async () => { + let cache = igniteClient.getCache(CACHE_NAME); + const cursor = await cache.query( + new SqlFieldsQuery(`SELECT * FROM ${TABLE_NAME}`)); + const set = new Set(); + do { + let fields = await cursor.getValue(); + expect(fields.length).toBe(2); + expect(generateValue(fields[0]) === fields[1]).toBe(true); + set.add(fields[0]); + expect(fields[0] >= 0 && fields[0] < ELEMENTS_NUMBER).toBe(true); + } while (cursor.hasMore()); + expect(set.size).toBe(ELEMENTS_NUMBER); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('get value with page size', (done) => { + Promise.resolve(). + then(async () => { + let cache = igniteClient.getCache(CACHE_NAME); + const cursor = await cache.query( + new SqlFieldsQuery(`SELECT * FROM ${TABLE_NAME}`).setPageSize(2)); + const set = new Set(); + do { + let fields = await cursor.getValue(); + expect(fields.length).toBe(2); + expect(generateValue(fields[0]) === fields[1]).toBe(true); + set.add(fields[0]); + expect(fields[0] >= 0 && fields[0] < ELEMENTS_NUMBER).toBe(true); + } while (cursor.hasMore()); + expect(set.size).toBe(ELEMENTS_NUMBER); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('close cursor', (done) => { + Promise.resolve(). + then(async () => { + let cache = igniteClient.getCache(CACHE_NAME); + const cursor = await cache.query( + new SqlFieldsQuery(`SELECT * FROM ${TABLE_NAME}`).setPageSize(1)); + await cursor.getValue(); + await cursor.close(); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('close cursor after get all', (done) => { + Promise.resolve(). + then(async () => { + let cache = igniteClient.getCache(CACHE_NAME); + const cursor = await cache.query(new SqlFieldsQuery(`SELECT * FROM ${TABLE_NAME}`)); + await cursor.getAll(); + await cursor.close(); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('sql fields query settings', (done) => { + Promise.resolve(). + then(async () => { + let cache = igniteClient.getCache(CACHE_NAME); + const cursor = await cache.query(new SqlFieldsQuery(`SELECT * FROM ${TABLE_NAME}`). + setPageSize(2). + setLocal(false). + setSql(`INSERT INTO ${TABLE_NAME} (field1, field2) VALUES (?, ?)`). + setArgTypes(ObjectType.PRIMITIVE_TYPE.INTEGER, ObjectType.PRIMITIVE_TYPE.STRING). + setArgs(50, 'test'). + setDistributedJoins(true). + setReplicatedOnly(false). + setTimeout(10000). + setSchema('PUBLIC'). + setMaxRows(20). + setStatementType(SqlFieldsQuery.STATEMENT_TYPE.ANY). + setEnforceJoinOrder(true). + setCollocated(false). + setLazy(true). + setIncludeFieldNames(true)); + await cursor.getAll(); + await cursor.close(); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('get empty results', (done) => { + Promise.resolve(). + then(async () => { + let cache = igniteClient.getCache(CACHE_NAME); + await cache.removeAll(); + let cursor = await cache.query( + new SqlFieldsQuery(`SELECT field1 FROM ${TABLE_NAME} WHERE field1 > 100`)); + const cacheEntries = await cursor.getAll(); + expect(cacheEntries.length).toBe(0); + await cursor.close(); + + cursor = await cache.query( + new SqlFieldsQuery(`SELECT field1 FROM ${TABLE_NAME} WHERE field1 > 100`)); + expect(await cursor.getValue()).toBe(null); + expect(cursor.hasMore()).toBe(false); + await cursor.close(); + }). + then(done). + catch(error => done.fail(error)); + }); + + async function dropTables(done) { + try { + let cache = igniteClient.getCache(CACHE_NAME); + (await cache.query(new SqlFieldsQuery(`DROP TABLE ${TABLE_NAME}`))).getAll(); + } + catch (err) { + done.fail('unexpected error: ' + err); + } + } + + async function testSuiteCleanup(done) { + await TestingHelper.destroyCache(CACHE_NAME, done); + } + + async function generateData(done) { + try { + let cache = igniteClient.getCache(CACHE_NAME); + (await cache.query(new SqlFieldsQuery( + `CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (field1 INT, field2 VARCHAR, PRIMARY KEY (field1))`))).getAll(); + + const insertQuery = new SqlFieldsQuery(`INSERT INTO ${TABLE_NAME} (field1, field2) VALUES (?, ?)`). + setArgTypes(ObjectType.PRIMITIVE_TYPE.INTEGER); + + for (let i = 0; i < ELEMENTS_NUMBER; i++) { + (await cache.query(insertQuery.setArgs(i, generateValue(i)))).getAll(); + } + } + catch (err) { + done.fail('unexpected error: ' + err); + } + } + + function generateValue(key) { + return 'value' + key; + } +}); http://git-wip-us.apache.org/repos/asf/ignite/blob/3afd3b55/modules/platforms/nodejs/spec/query/SqlQuery.spec.js ---------------------------------------------------------------------- diff --git a/modules/platforms/nodejs/spec/query/SqlQuery.spec.js b/modules/platforms/nodejs/spec/query/SqlQuery.spec.js new file mode 100644 index 0000000..2bbd0e5 --- /dev/null +++ b/modules/platforms/nodejs/spec/query/SqlQuery.spec.js @@ -0,0 +1,247 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +require('jasmine-expect'); + +const config = require('../config'); +const TestingHelper = require('../TestingHelper'); +const IgniteClient = require('apache-ignite-client'); +const Errors = IgniteClient.Errors; +const SqlQuery = IgniteClient.SqlQuery; +const SqlFieldsQuery = IgniteClient.SqlFieldsQuery; +const ObjectType = IgniteClient.ObjectType; +const CacheConfiguration = IgniteClient.CacheConfiguration; +const QueryEntity = IgniteClient.QueryEntity; +const QueryField = IgniteClient.QueryField; +const ComplexObjectType = IgniteClient.ComplexObjectType; + +const CACHE_NAME = '__test_cache'; +const TABLE_NAME = '__test_SqlQuery'; +const ELEMENTS_NUMBER = 10; + +describe('sql query test suite >', () => { + let igniteClient = null; + + beforeAll((done) => { + Promise.resolve(). + then(async () => { + await TestingHelper.init(); + igniteClient = TestingHelper.igniteClient; + await testSuiteCleanup(done); + await igniteClient.getOrCreateCache( + CACHE_NAME, + new CacheConfiguration(). + setQueryEntities( + new QueryEntity(). + setKeyTypeName('java.lang.Integer'). + setValueTypeName(TABLE_NAME). + setFields([ + new QueryField('field1', 'java.lang.Integer'), + new QueryField('field2', 'java.lang.String') + ]))); + await generateData(done); + }). + then(done). + catch(error => done.fail(error)); + }, TestingHelper.TIMEOUT); + + afterAll((done) => { + Promise.resolve(). + then(async () => { + await testSuiteCleanup(done); + await TestingHelper.cleanUp(); + }). + then(done). + catch(error => done()); + }, TestingHelper.TIMEOUT); + + it('get all', (done) => { + Promise.resolve(). + then(async () => { + let cache = getCache(); + const cursor = await cache.query( + new SqlQuery(TABLE_NAME, `SELECT * FROM ${TABLE_NAME}`)); + const set = new Set(); + for (let cacheEntry of await cursor.getAll()) { + expect(generateValue(cacheEntry.getKey()) === cacheEntry.getValue().field2).toBe(true); + set.add(cacheEntry.getKey()); + expect(cacheEntry.getKey() >= 0 && cacheEntry.getKey() < ELEMENTS_NUMBER).toBe(true); + } + expect(set.size).toBe(ELEMENTS_NUMBER); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('get all with page size', (done) => { + Promise.resolve(). + then(async () => { + let cache = getCache(); + const cursor = await cache.query( + new SqlQuery(TABLE_NAME, `SELECT * FROM ${TABLE_NAME}`).setPageSize(1)); + const set = new Set(); + for (let cacheEntry of await cursor.getAll()) { + expect(generateValue(cacheEntry.getKey()) === cacheEntry.getValue().field2).toBe(true); + set.add(cacheEntry.getKey()); + expect(cacheEntry.getKey() >= 0 && cacheEntry.getKey() < ELEMENTS_NUMBER).toBe(true); + } + expect(set.size).toBe(ELEMENTS_NUMBER); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('get value', (done) => { + Promise.resolve(). + then(async () => { + let cache = getCache(); + const cursor = await cache.query( + new SqlQuery(TABLE_NAME, `SELECT * FROM ${TABLE_NAME}`)); + const set = new Set(); + do { + let cacheEntry = await cursor.getValue(); + expect(generateValue(cacheEntry.getKey()) === cacheEntry.getValue().field2).toBe(true); + set.add(cacheEntry.getKey()); + expect(cacheEntry.getKey() >= 0 && cacheEntry.getKey() < ELEMENTS_NUMBER).toBe(true); + } while (cursor.hasMore()); + expect(set.size).toBe(ELEMENTS_NUMBER); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('get value with page size', (done) => { + Promise.resolve(). + then(async () => { + let cache = getCache(); + const cursor = await cache.query( + new SqlQuery(TABLE_NAME, `SELECT * FROM ${TABLE_NAME}`).setPageSize(2)); + const set = new Set(); + do { + let cacheEntry = await cursor.getValue(); + expect(generateValue(cacheEntry.getKey()) === cacheEntry.getValue().field2).toBe(true); + set.add(cacheEntry.getKey()); + expect(cacheEntry.getKey() >= 0 && cacheEntry.getKey() < ELEMENTS_NUMBER).toBe(true); + } while (cursor.hasMore()); + expect(set.size).toBe(ELEMENTS_NUMBER); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('close cursor', (done) => { + Promise.resolve(). + then(async () => { + let cache = getCache(); + const cursor = await cache.query( + new SqlQuery(TABLE_NAME, `SELECT * FROM ${TABLE_NAME}`).setPageSize(1)); + await cursor.getValue(); + await cursor.close(); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('close cursor after get all', (done) => { + Promise.resolve(). + then(async () => { + let cache = getCache(); + const cursor = await cache.query( + new SqlQuery(TABLE_NAME, `SELECT * FROM ${TABLE_NAME}`)); + await cursor.getAll(); + await cursor.close(); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('sql query settings', (done) => { + Promise.resolve(). + then(async () => { + let cache = getCache(); + const cursor = await cache.query(new SqlQuery(TABLE_NAME, `SELECT * FROM ${TABLE_NAME}`). + setType(TABLE_NAME). + setPageSize(2). + setLocal(false). + setSql('field1 > ? and field1 <= ?'). + setArgTypes(ObjectType.PRIMITIVE_TYPE.INTEGER, ObjectType.PRIMITIVE_TYPE.INTEGER). + setArgs(3, 7). + setDistributedJoins(true). + setReplicatedOnly(false). + setTimeout(10000)); + await cursor.getAll(); + await cursor.close(); + }). + then(done). + catch(error => done.fail(error)); + }); + + it('get values from empty cache', (done) => { + Promise.resolve(). + then(async () => { + let cache = getCache(); + await cache.removeAll(); + let cursor = await cache.query(new SqlQuery(TABLE_NAME, `SELECT * FROM ${TABLE_NAME}`).setPageSize(1)); + const cacheEntries = await cursor.getAll(); + expect(cacheEntries.length).toBe(0); + await cursor.close(); + + cursor = await cache.query(new SqlQuery(TABLE_NAME, `SELECT * FROM ${TABLE_NAME}`).setPageSize(1)); + expect(await cursor.getValue()).toBe(null); + expect(cursor.hasMore()).toBe(false); + await cursor.close(); + }). + then(done). + catch(error => done.fail(error)); + }); + + async function testSuiteCleanup(done) { + await TestingHelper.destroyCache(CACHE_NAME, done); + } + + function getCache() { + return igniteClient.getCache(CACHE_NAME). + setKeyType(ObjectType.PRIMITIVE_TYPE.INTEGER). + setValueType(new ComplexObjectType({ + 'field1' : 1, + 'field2' : 'a' + }, TABLE_NAME). + setFieldType('field1', ObjectType.PRIMITIVE_TYPE.INTEGER)); + } + + async function generateData(done) { + try { + let cache = igniteClient.getCache(CACHE_NAME); + + const insertQuery = new SqlFieldsQuery(`INSERT INTO ${TABLE_NAME} (_key, field1, field2) VALUES (?, ?, ?)`). + setArgTypes(ObjectType.PRIMITIVE_TYPE.INTEGER, ObjectType.PRIMITIVE_TYPE.INTEGER); + + for (let i = 0; i < ELEMENTS_NUMBER; i++) { + (await cache.query(insertQuery.setArgs(i, i, generateValue(i)))).getAll(); + } + } + catch (err) { + done.fail('unexpected error: ' + err); + } + } + + function generateValue(key) { + return 'value' + key; + } +});
