Skip to content

Commit 832078d

Browse files
Improve test coverage (#136)
* Improve coverage Signed-off-by: Levko Kravets <levko.ne@gmail.com> * Remove unused methods of ITransport (previously were used in TcpTransport/TlsTransport which were also removed earlier) Signed-off-by: Levko Kravets <levko.ne@gmail.com> * Add tests for HttpTransport Signed-off-by: Levko Kravets <levko.ne@gmail.com> * Add more tests for result handlers Signed-off-by: Levko Kravets <levko.ne@gmail.com> * Add tests for NoSaslAuthentication Signed-off-by: Levko Kravets <levko.ne@gmail.com> --------- Signed-off-by: Levko Kravets <levko.ne@gmail.com>
1 parent 75e58f2 commit 832078d

File tree

11 files changed

+290
-49
lines changed

11 files changed

+290
-49
lines changed

lib/connection/auth/NoSaslAuthentication.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import ITransport from '../contracts/ITransport';
44

55
export default class NoSaslAuthentication implements IAuthentication {
66
authenticate(transport: ITransport): Promise<ITransport> {
7-
transport.connect();
87
transport.setOptions('transport', thrift.TBufferedTransport);
98

109
return Promise.resolve(transport);

lib/connection/contracts/ITransport.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,4 @@ export default interface ITransport {
66
setOptions(option: string, value: any): void;
77

88
getOptions(): object;
9-
10-
connect(): any;
11-
12-
addListener(eventName: string, listener: Function): void;
13-
14-
removeListener(eventName: string, listener: Function): void;
15-
16-
emit(eventName: string): void;
17-
18-
write(data: Buffer | String): void;
19-
20-
end(): void;
219
}

lib/connection/transports/HttpTransport.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,4 @@ export default class HttpTransport implements ITransport {
2121
getOptions(): object {
2222
return this.httpOptions;
2323
}
24-
25-
connect() {}
26-
27-
addListener() {}
28-
29-
removeListener() {}
30-
31-
write() {}
32-
33-
end() {}
34-
35-
emit() {}
3624
}

lib/result/JsonResult.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default class JsonResult implements IOperationResult {
4141
);
4242
}
4343

44-
private getSchemaValues(descriptor: TColumnDesc, column: TColumn): Array<any> {
44+
private getSchemaValues(descriptor: TColumnDesc, column?: TColumn): Array<any> {
4545
const typeDescriptor = descriptor.typeDesc.types[0]?.primitiveEntry;
4646
const columnValue = this.getColumnValue(column);
4747

@@ -64,7 +64,10 @@ export default class JsonResult implements IOperationResult {
6464
return (byte & ofs) !== 0;
6565
}
6666

67-
private getColumnValue(column: TColumn) {
67+
private getColumnValue(column?: TColumn) {
68+
if (!column) {
69+
return undefined;
70+
}
6871
return (
6972
column[ColumnCode.binaryVal] ||
7073
column[ColumnCode.boolVal] ||
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { expect } = require('chai');
2+
const thrift = require('thrift');
3+
const NoSaslAuthentication = require('../../../../dist/connection/auth/NoSaslAuthentication').default;
4+
5+
describe('NoSaslAuthentication', () => {
6+
it('auth token must be set to header', () => {
7+
const auth = new NoSaslAuthentication();
8+
const transportMock = {
9+
setOptions(name, value) {
10+
expect(name).to.be.equal('transport');
11+
expect(value).to.be.equal(thrift.TBufferedTransport);
12+
},
13+
};
14+
return auth.authenticate(transportMock).then((transport) => {
15+
expect(transport).to.be.eq(transportMock);
16+
});
17+
});
18+
});

tests/unit/connection/connections/HttpConnection.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const http = require('http');
22
const { expect } = require('chai');
3-
const HttpConnection = require('../../../../').connections.HttpConnection;
3+
const HttpConnection = require('../../../../dist/connection/connections/HttpConnection').default;
44

55
const thriftMock = (connection) => ({
66
createHttpConnection(host, port, options) {
@@ -27,6 +27,8 @@ describe('HttpConnection.connect', () => {
2727
};
2828
connection.thrift = thriftMock(resultConnection);
2929

30+
expect(connection.isConnected()).to.be.false;
31+
3032
return connection
3133
.connect(
3234
{
@@ -45,6 +47,7 @@ describe('HttpConnection.connect', () => {
4547
expect(connection.thrift.port).to.be.eq(10001);
4648
expect(connection.thrift.options.path).to.be.eq('/hive');
4749
expect(connection.getConnection()).to.be.eq(resultConnection);
50+
expect(connection.isConnected()).to.be.true;
4851
});
4952
});
5053

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const http = require('http');
2+
const { expect } = require('chai');
3+
const HttpTransport = require('../../../../dist/connection/transports/HttpTransport').default;
4+
5+
describe('HttpTransport', () => {
6+
it('should initialize with default options', () => {
7+
const transport = new HttpTransport();
8+
expect(transport.getTransport()).to.deep.equal({});
9+
expect(transport.getOptions()).to.deep.equal({});
10+
});
11+
12+
it('should handle options', () => {
13+
const initialOptions = { test: 'Hello, World' };
14+
15+
const transport = new HttpTransport(initialOptions);
16+
expect(transport.getTransport()).to.deep.equal(initialOptions);
17+
expect(transport.getOptions()).to.deep.equal(initialOptions);
18+
19+
const optionName = 'option';
20+
const optionValue = 123;
21+
const updatedOptions = {
22+
...initialOptions,
23+
[optionName]: optionValue,
24+
};
25+
26+
transport.setOptions(optionName, optionValue);
27+
expect(transport.getTransport()).to.deep.equal(updatedOptions);
28+
expect(transport.getOptions()).to.deep.equal(updatedOptions);
29+
});
30+
});

tests/unit/dto/InfoValue.test.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ const { expect } = require('chai');
22
const InfoValue = require('../../../dist/dto/InfoValue').default;
33
const NodeInt64 = require('node-int64');
44

5-
describe('InfoValue', () => {
6-
const infoValue = (value) =>
7-
Object.assign(
8-
{
9-
stringValue: null,
10-
smallIntValue: null,
11-
integerBitmask: null,
12-
integerFlag: null,
13-
lenValue: null,
14-
},
15-
value,
16-
);
5+
const createInfoValueMock = (value) =>
6+
Object.assign(
7+
{
8+
stringValue: null,
9+
smallIntValue: null,
10+
integerBitmask: null,
11+
integerFlag: null,
12+
lenValue: null,
13+
},
14+
value,
15+
);
1716

17+
describe('InfoValue', () => {
1818
it('should return string', () => {
1919
const value = new InfoValue(
20-
infoValue({
20+
createInfoValueMock({
2121
stringValue: 'value',
2222
}),
2323
);
@@ -27,38 +27,44 @@ describe('InfoValue', () => {
2727

2828
it('should return number', () => {
2929
const smallInt = new InfoValue(
30-
infoValue({
30+
createInfoValueMock({
3131
smallIntValue: 1,
3232
}),
3333
);
3434

3535
expect(smallInt.getValue()).to.be.eq(1);
3636

3737
const bitMask = new InfoValue(
38-
infoValue({
38+
createInfoValueMock({
3939
integerBitmask: 0xaa55aa55,
4040
}),
4141
);
4242

4343
expect(bitMask.getValue()).to.be.eq(0xaa55aa55);
4444

4545
const integerFlag = new InfoValue(
46-
infoValue({
46+
createInfoValueMock({
4747
integerFlag: 0x01,
4848
}),
4949
);
5050

51-
expect(integerFlag.getValue()).to.be.eq(01);
51+
expect(integerFlag.getValue()).to.be.eq(0x01);
5252
});
5353

5454
it('should return int64', () => {
5555
const value = new InfoValue(
56-
infoValue({
56+
createInfoValueMock({
5757
lenValue: new NodeInt64(Buffer.from([0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10])),
5858
}),
5959
);
6060

6161
expect(value.getValue()).to.be.instanceOf(NodeInt64);
6262
expect(value.getValue().toNumber()).to.be.eq(4521260802379792);
6363
});
64+
65+
it('should return null for empty info value', () => {
66+
const value = new InfoValue(createInfoValueMock({}));
67+
68+
expect(value.getValue()).to.be.null;
69+
});
6470
});

tests/unit/result/JsonResult.test.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ const JsonResult = require('../../../dist/result/JsonResult').default;
33
const { TCLIService_types } = require('../../../').thrift;
44
const Int64 = require('node-int64');
55

6-
const getColumnSchema = (name, type, position) => {
6+
const getColumnSchema = (columnName, type, position) => {
7+
if (type === undefined) {
8+
return {
9+
columnName,
10+
typeDesc: { types: [] },
11+
position,
12+
};
13+
}
14+
715
return {
8-
columnName: name,
16+
columnName,
917
typeDesc: {
1018
types: [
1119
{
@@ -201,6 +209,7 @@ describe('JsonResult', () => {
201209
},
202210
],
203211
},
212+
{}, // it should also handle empty sets
204213
{
205214
columns: [
206215
{
@@ -383,4 +392,50 @@ describe('JsonResult', () => {
383392

384393
expect(result.getValue(data)).to.be.deep.eq([]);
385394
});
395+
396+
it('should return raw data if types are not specified', () => {
397+
const schema = {
398+
columns: [
399+
getColumnSchema('table.array', undefined, 1),
400+
getColumnSchema('table.map', undefined, 2),
401+
getColumnSchema('table.struct', undefined, 3),
402+
getColumnSchema('table.union', undefined, 4),
403+
],
404+
};
405+
const data = [
406+
{
407+
columns: [
408+
{
409+
stringVal: { values: ['["a", "b"]', '["c", "d"]'] },
410+
},
411+
{
412+
stringVal: { values: ['{ "key": 12 }', '{ "key": 13 }'] },
413+
},
414+
{
415+
stringVal: { values: ['{ "name": "Jon", "surname": "Doe" }', '{ "name": "Jane", "surname": "Doe" }'] },
416+
},
417+
{
418+
stringVal: { values: ['{0:12}', '{1:"foo"}'] },
419+
},
420+
],
421+
},
422+
];
423+
424+
const result = new JsonResult(schema);
425+
426+
expect(result.getValue(data)).to.be.deep.eq([
427+
{
428+
'table.array': '["a", "b"]',
429+
'table.map': '{ "key": 12 }',
430+
'table.struct': '{ "name": "Jon", "surname": "Doe" }',
431+
'table.union': '{0:12}',
432+
},
433+
{
434+
'table.array': '["c", "d"]',
435+
'table.map': '{ "key": 13 }',
436+
'table.struct': '{ "name": "Jane", "surname": "Doe" }',
437+
'table.union': '{1:"foo"}',
438+
},
439+
]);
440+
});
386441
});

0 commit comments

Comments
 (0)