UNPKG

dl-nosqldb

Version:

DL nosqldb library

432 lines (398 loc) 16.7 kB
/*jslint node: true */ 'use strict'; var Long = require('long'); var Nosqldb = require('../lib/Nosqldb.js').Nosqldb; var Errors = require('../lib/Errors.js'); describe('Nosqldb', function () { beforeEach(function () { jasmine.Clock.useMock(); this.nosqldb = new Nosqldb('tests.cassandra.nosqldb.onetapi.pl'); this.nosqldb._nosqldbRpc = function (method, params, cb) { return cb(null, []); }; this.nosqldb._metadata = { tables: { types_test: { primary_key: [ { name: 'key', typestring: 'text' } ], columns: { key: { name: 'key', typestring: 'text' }, int_value: { name: 'int_value', typestring: 'int' }, text_value: { name: 'text_value', typestring: 'text' }, boolean_value: { name: 'boolean_value', typestring: 'boolean' }, bigint_value: { name: 'bigint_value', typestring: 'bigint' }, decimal_value: { name: 'decimal_value', typestring: 'decimal' }, blob_value: { name: 'blob_value', typestring: 'blob' }, text_set: { name: 'text_set', typestring: 'set<text>' }, text_list: { name: 'text_list', typestring: 'list<text>' }, text_map: { name: 'text_map', typestring: 'map<text, text>' }, bigint_set: { name: 'bigint_set', typestring: 'set<bigint>' }, bigint_list: { name: 'bigint_list', typestring: 'list<bigint>' }, bigint_map: { name: 'bigint_map', typestring: 'map<text, bigint>' } } }, composite_bigint_key_test: { primary_key: [ { name: 'key1', typestring: 'bigint' }, { name: 'key2', typestring: 'bigint' } ], columns: { key1: { name: 'key1', typestring: 'bigint' }, key2: { name: 'key2', typestring: 'bigint' } } } } }; }); describe('encodeKey', function () { it('should encode types_test key', function () { var encodedKey = this.nosqldb._encodeKey('types_test', 'key'); expect(encodedKey).toEqual(['key']); }); it('should encode composite_bigint_key_test key', function () { var key1 = Long.fromString('1234567890123456789', false); var key2 = Long.fromString('123', false); var encodedKey = this.nosqldb._encodeKey( 'composite_bigint_key_test', [key1, key2] ); expect(encodedKey).toEqual(['1234567890123456789', '123']); }); }); describe('encodeColumns', function () { it('should encode types_test columns', function () { var columns = { 'int_value': 123, 'text_value': 'abc', 'boolean_value': true, 'bigint_value': Long.fromString('1234567890123456789', false), 'decimal_value': 123.45, 'blob_value': new Buffer('abc'), 'text_set': ['a', 'b', 'c'], 'text_list': ['a', 'b', 'c'], 'text_map': { 'a': 1, 'b': 2, 'c': 3 }, 'bigint_set': [Long.fromString('1234567890123456789', false)], 'bigint_list': [Long.fromString('1234567890123456789', false)], 'bigint_map': { 'key': Long.fromString('1234567890123456789', false) } }; var encodedColumns = this.nosqldb._encodeColumns( 'types_test', columns ); expect(encodedColumns).toEqual({ 'int_value': 123, 'text_value': 'abc', 'boolean_value': true, 'bigint_value': '1234567890123456789', 'decimal_value': '123.45', 'blob_value': '616263', 'text_set': ['a', 'b', 'c'], 'text_list': ['a', 'b', 'c'], 'text_map': [['a', 1], ['b', 2], ['c', 3]], 'bigint_set': ['1234567890123456789'], 'bigint_list': ['1234567890123456789'], 'bigint_map': [['key', '1234567890123456789']] }); }); }); describe('encodeAssignments', function () { it('should encode types_test assignments', function () { var assignments = { 'int_value': ['=', 123], 'text_value': ['=', 'abc'], 'boolean_value': ['=', true], 'bigint_value': ['=', Long.fromString('1234567890123456789', false)], 'decimal_value': ['=', 123.45], 'blob_value': ['=', new Buffer('abc')], 'text_set': ['=', ['a', 'b', 'c']], 'text_list': [[0], 'a'], 'text_map': [['a'], 1], 'bigint_set': ['+', [new Long.fromString('1234567890123456789', false)]], 'bigint_list': ['+', [new Long.fromString('1234567890123456789', false)]], 'bigint_map': ['+', { 'key': new Long.fromString('1234567890123456789', false) }] }; var encodedAssignments = this.nosqldb._encodeAssignments( 'types_test', assignments ); expect(encodedAssignments).toEqual({ 'int_value': ['=', 123], 'text_value': ['=', 'abc'], 'boolean_value': ['=', true], 'bigint_value': ['=', '1234567890123456789'], 'decimal_value': ['=', '123.45'], 'blob_value': ['=', '616263'], 'text_set': ['=', ['a', 'b', 'c']], 'text_list': [[0], 'a'], 'text_map': [['a'], 1], 'bigint_set': ['+', ['1234567890123456789']], 'bigint_list': ['+', ['1234567890123456789']], 'bigint_map': ['+', [['key', '1234567890123456789']]] }); }); }); describe('handleError', function () { it('should handle InvalidRequest error', function () { var res = this.nosqldb._handleError({ code: Errors.INVALID_REQUEST }); expect(res instanceof Errors.InvalidRequest).toEqual(true); }); }); describe('handleResult', function () { it('should handle null result', function () { var decodedRes = this.nosqldb._handleResult('types_test', null); expect(decodedRes).toEqual(null); }); it('should handle result', function () { var res = { 'int_value': 123, 'text_value': 'abc', 'boolean_value': true, 'bigint_value': '1234567890123456789', 'decimal_value': '123.45', 'text_set': ['a', 'b', 'c'], 'text_list': ['a', 'b', 'c'], 'text_map': [['a', 1], ['b', 2], ['c', 3]], 'bigint_set': ['1234567890123456789'], 'bigint_list': ['1234567890123456789'], 'bigint_map': [['key', '1234567890123456789']] } var decodedRes = this.nosqldb._handleResult('types_test', res); expect(decodedRes).toEqual({ 'int_value': 123, 'text_value': 'abc', 'boolean_value': true, 'bigint_value': Long.fromString('1234567890123456789', false), 'decimal_value': 123.45, 'text_set': ['a', 'b', 'c'], 'text_list': ['a', 'b', 'c'], 'text_map': { 'a': 1, 'b': 2, 'c': 3 }, 'bigint_set': [Long.fromString('1234567890123456789', false)], 'bigint_list': [Long.fromString('1234567890123456789', false)], 'bigint_map': { 'key': Long.fromString('1234567890123456789', false) } }); }); }); describe('getMethod ', function () { it('should handle get data with promise', function (done) { this.nosqldb._nosqldbRpc = function (method, params, cb) { return cb(null, { 'testColumn': 'testValue', 'testColumn2': 'testValue2', 'testColumn3': ['testval3', 'testval4'] }); }; var res = this.nosqldb.get('types_test', 'key') res.then((d) => { expect(d).toEqual({ 'testColumn': 'testValue', 'testColumn2': 'testValue2', 'testColumn3': ['testval3', 'testval4'] }); done() }); }); it('should handle get data with cb', function (done) { this.nosqldb._nosqldbRpc = function (method, params, cb) { return cb(null, { 'testColumn': 'testValue', 'testColumn2': 'testValue2', 'testColumn3': ['testval3', 'testval4'] }); }; this.nosqldb.get('types_test', 'key', {}, function (err, data) { expect(data).toEqual({ 'testColumn': 'testValue', 'testColumn2': 'testValue2', 'testColumn3': ['testval3', 'testval4'] }); expect(err).toEqual(null); done(); }); }); }); describe('multiGet method ', function () { it('should handle multiGet data with promise', function (done) { this.nosqldb._nosqldbRpc = function (method, params, cb) { return cb(null, []); }; var res = this.nosqldb.multiGet([{ "table": "types_test", "key": "test" }, { "table": "types_test", "key": "test2" }]) res.then((d) => { expect(d).toEqual([]); done() }); }); it('should handle multiGet data with cb', function (done) { this.nosqldb._nosqldbRpc = function (method, params, cb) { return cb(null, []); }; this.nosqldb.multiGet( [{ "table": "types_test", "key": "test" }, { "table": "types_test", "key": "test2" }], {}, function (err, data) { expect(data).toEqual([]); expect(err).toEqual(null); done(); }); }); }); describe('getRange method', function () { it('should handle getRange data with promise', function (done) { this.nosqldb._nosqldbRpc = function (method, params, cb) { return cb(null, []); }; var res = this.nosqldb.getRange('types_test', ["test"]); res.then((d) => { expect(d).toEqual([]); done() }); }); it('should handle multiGet data with cb', function (done) { this.nosqldb._nosqldbRpc = function (method, params, cb) { return cb(null, []); }; this.nosqldb.getRange('types_test', ["test"], {}, function (err, data) { expect(data).toEqual([]); expect(err).toEqual(null); done(); }); }); }); describe('insert method', function () { it('should handle multiGet data with promise', function (done) { this.nosqldb._nosqldbRpc = function (method, params, cb) { return cb(null, []); }; var res = this.nosqldb.insert("types_test", "test", { "int_value": "test" }); res.then((data) => { expect(data).toEqual(undefined); done() }); }); it('should handle multiGet data with cb', function (done) { this.nosqldb._nosqldbRpc = function (method, params, cb) { return cb(null, []); }; this.nosqldb.insert("types_test", "test", { "int_value": "test" }, {}, function (err, data) { expect(data).toEqual(undefined); expect(err).toEqual(null); done(); }); }); }); describe('multiInsert method', function () { it('should handle multiGet data with promise', function (done) { this.nosqldb._nosqldbRpc = function (method, params, cb) { return cb(null, []); }; var res = this.nosqldb.multiInsert([{ "table": "types_test", "key": "test", "columns": { "int_value": "test" } }]); res.then((data) => { expect(data).toEqual(undefined); done() }); }); it('should handle multiGet data with cb', function (done) { this.nosqldb._nosqldbRpc = function (method, params, cb) { return cb(null, []); }; this.nosqldb.multiInsert([{ "table": "types_test", "key": "test", "columns": { "int_value": 3 } }], {}, function (err, data) { expect(data).toEqual(undefined); expect(err).toEqual(null); done(); }); }); }); describe('update method', function () { it('should handle multiGet data with promise', function (done) { this.nosqldb._nosqldbRpc = function (method, params, cb) { return cb(null, []); }; var res = this.nosqldb.update("types_test", "test", { "int_value": ["=", 5] }); res.then((data) => { expect(data).toEqual(undefined); done() }); }); it('should handle multiGet data with cb', function (done) { this.nosqldb._nosqldbRpc = function (method, params, cb) { return cb(null, []); }; this.nosqldb.update("types_test", "test", { "int_value": ["=", 5] }, {}, function (err, data) { expect(data).toEqual(undefined); expect(err).toEqual(null); done(); }); }); }); describe('batch method', function () { it('should handle multiGet data with promise', function (done) { this.nosqldb._nosqldbRpc = function (method, params, cb) { return cb(null, []); }; var res = this.nosqldb.batch([ { "name": "insert", "params": { "table": "types_test", "key": "test2", "columns": { "int_value": 1 }, "ttl": 200 } }, { "name": "insert", "params": { "table": "types_test", "key": "test2", "columns": { "int_value": 11 }, } }]); res.then((data) => { expect(data).toEqual(undefined); done() }); }); it('should handle batch data with cb', function (done) { this.nosqldb._nosqldbRpc = function (method, params, cb) { return cb(null, []); }; this.nosqldb.batch([ { "name": "insert", "params": { "table": "types_test", "key": "test2", "columns": { "int_value": 1 }, "ttl": 200 } }, { "name": "insert", "params": { "table": "types_test", "key": "test2", "columns": { "int_value": 11 }, } }], {}, function (err, data) { expect(data).toEqual(undefined); expect(err).toEqual(null); done(); }); }); }); });