From 6552fc1c72a5d3fbba34832416c0dbd94dc3c4f3 Mon Sep 17 00:00:00 2001 From: Sciator <39964450+Sciator@users.noreply.github.com> Date: Mon, 21 Aug 2023 11:09:25 +0200 Subject: [PATCH] test: InfluxDBClient argument options --- packages/client/test/unit/Influxdb.test.ts | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/packages/client/test/unit/Influxdb.test.ts b/packages/client/test/unit/Influxdb.test.ts index 779d4bfa..83d7b6be 100644 --- a/packages/client/test/unit/Influxdb.test.ts +++ b/packages/client/test/unit/Influxdb.test.ts @@ -1,7 +1,58 @@ import {expect} from 'chai' +import sinon from 'sinon' import {InfluxDBClient, ClientOptions, Transport} from '../../src' +import type WriteApi from '../../src/WriteApi' +import type QueryApi from '../../src/QueryApi' +import {rejects} from 'assert' describe('InfluxDB', () => { + afterEach(() => { + sinon.restore() + }) + + it('uses options database', () => { + const database = 'my-db' + const client = new InfluxDBClient({ + host: 'http://localhost:8086', + database, + }) + const writeApi: WriteApi = (client as any)._writeApi + const queryApi: QueryApi = (client as any)._queryApi + const writeStub = sinon.stub(writeApi, 'doWrite') + const queryStub = sinon.stub(queryApi, 'query') + + const lines = ['lpdata'] + + client.write(lines) + + expect(writeStub.calledWith(lines, database)).to.be.true + writeStub.resetHistory() + + client.write(lines, 'another') + expect(writeStub.calledOnceWith(lines, 'another')).to.be.true + + const query = 'select *' + client.query(query) + + expect(queryStub.calledOnceWith(query, database, 'sql')).to.be.true + queryStub.resetHistory() + + client.query(query, 'another') + expect(queryStub.calledOnceWith(query, 'another', 'sql')).to.be.true + }) + + it('throws when no database provided', async () => { + const client = new InfluxDBClient({ + host: 'http://localhost:8086', + }) + + expect(() => client.query('query')).to.throw(`\ +Please specify the 'database' as a method parameter or use default configuration \ +at 'ClientOptions.database' +`) + await rejects(client.write('data')) + }) + describe('constructor', () => { it('is created from configuration with host', () => { expect(