dl
Version:
DreamLab Libs
182 lines (170 loc) • 8.01 kB
JavaScript
/*jslint node: true */
;
var Long = require('long');
var Nosqldb = require('../../lib/nosqldb/Nosqldb.js').Nosqldb;
var Errors = require('../../lib/nosqldb/Errors.js');
describe('Nosqldb', function() {
beforeEach(function () {
jasmine.Clock.useMock();
this.nosqldb = new Nosqldb('tests.cassandra.nosqldb.onetapi.pl');
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)}
});
});
});
});