From 05e94de287e70528864027c91a4ec61f54b9dad1 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 14 Apr 2020 17:49:33 +0300 Subject: [PATCH] fix(index.js): add test and fix for query with totals --- README.md | 2 ++ index.js | 29 +++++++++++++++-------------- package.json | 2 +- test/test.js | 29 ++++++++++++++++------------- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index bea0723..efd1c42 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,8 @@ const result = await rs.pipe(tf).pipe(ws).exec(); *** **Changelogs**: +* 2020-04-17 (v2.1.0) + - Fix query with totals. For json formats work perfect, but for another - doesn't * 2019-02-13 - Add compatibility with user and username options * 2019-02-07 diff --git a/index.js b/index.js index 90d8c3e..d916bd2 100644 --- a/index.js +++ b/index.js @@ -542,27 +542,27 @@ class QueryCursor { } try { - let data = me.getBodyParser()(res.body); + const data = me.getBodyParser()(res.body); if (me.format === 'json') { - data = data.data; + if (me.useTotals) { + return cb(null, data); + } + + return cb(null, data.data); } if (me.useTotals) { - const totals = JSON.parse(res.headers['x-clickhouse-summary']); - return cb( - null, - { - meta: {}, - data: data, - totals, - rows: data.length, - statistics: {}, - } - ); + return cb(null, { + meta: {}, + data, + totals: {}, + rows: {}, + statistics: {}, + }); } - cb(null, data); + return cb(null, data); } catch (err) { cb(err); } @@ -603,6 +603,7 @@ class QueryCursor { withTotals() { this.useTotals = true; + return this; } diff --git a/package.json b/package.json index d9f4432..dd0594c 100644 --- a/package.json +++ b/package.json @@ -92,5 +92,5 @@ "test": "mocha --bail --timeout 60000 --slow 5000" }, "types": "index.d.ts", - "version": "2.0.2" + "version": "2.1.0" } diff --git a/test/test.js b/test/test.js index 0335e20..c371acf 100644 --- a/test/test.js +++ b/test/test.js @@ -665,17 +665,16 @@ describe('Exec system queries', () => { describe('Select and WITH TOTALS statement', () => { [false, true].forEach(withTotals => { it(`is ${withTotals}`, async () => { - const query = clickhouse.query( - `SELECT - number % 3 AS i, - groupArray(number) as kList - FROM ( - SELECT number FROM system.numbers LIMIT 14 - ) - GROUP BY i ${withTotals ? '' : 'WITH TOTALS'} - FORMAT TabSeparatedWithNames - ` - ); + const query = clickhouse.query(` + SELECT + number % 3 AS i, + groupArray(number) as kList + FROM ( + SELECT number FROM system.numbers LIMIT 14 + ) + GROUP BY i ${withTotals ? '' : 'WITH TOTALS'} + FORMAT TabSeparatedWithNames + `); if (withTotals) { query.withTotals(); @@ -692,6 +691,8 @@ describe('Select and WITH TOTALS statement', () => { }); it('WITH TOTALS #2', async () => { + const LIMIT_COUNT = 10; + const result = await clickhouse.query(` SELECT rowNumberInAllBlocks() AS i, @@ -703,13 +704,15 @@ describe('Select and WITH TOTALS statement', () => { system.numbers LIMIT 1000 ) - GROUP BY i WITH TOTALS LIMIT 10 + GROUP BY i WITH TOTALS + LIMIT ${LIMIT_COUNT} `).toPromise(); expect(result).to.have.key('meta'); expect(result).to.have.key('data'); expect(result).to.have.key('totals'); expect(result).to.have.key('rows'); + expect(result.rows).to.be(LIMIT_COUNT); expect(result).to.have.key('statistics'); }) }); @@ -738,7 +741,7 @@ describe('Abort query', () => { setTimeout(() => cb(), 4 * 1000); }) .on('end', () => { - cb(new Error('no way!')); + cb(new Error('no way! May be stream very quick!')); }); setTimeout(() => $q.destroy(), 10 * 1000);