From 5e3d27c8512a6f8441283bdf3afa5ab84d0b8e16 Mon Sep 17 00:00:00 2001 From: Jakob Helgesson Date: Sat, 1 Jan 2022 16:31:29 +0100 Subject: [PATCH] Improve tests format and add prepare option (#16) Co-authored-by: Luc Co-authored-by: Luc --- __tests__/__GLOBAL_SETUP__.ts | 27 +++++++++++++++++++ __tests__/__GLOBAL_TEARDOWN__.ts | 24 +++++++++++++++++ __tests__/{ => live}/insertInto.spec.ts | 4 +-- __tests__/{ => live}/selectFrom.spec.ts | 4 +-- __tests__/{ => live}/selectOneFrom.spec.ts | 4 +-- __tests__/{ => live}/update.spec.ts | 4 +-- __tests__/{ => raw}/createIndexRaw.spec.ts | 2 +- __tests__/{ => raw}/createTableRaw.spec.ts | 2 +- __tests__/{ => raw}/deleteFromRaw.spec.ts | 2 +- __tests__/{ => raw}/insertIntoRaw.spec.ts | 2 +- __tests__/{ => raw}/updateRaw.spec.ts | 2 +- jest.config.js | 4 ++- package.json | 2 +- src/ScylloClient.ts | 30 ++++++++++++++++++---- src/ScylloTranslator.ts | 6 +---- 15 files changed, 94 insertions(+), 25 deletions(-) create mode 100644 __tests__/__GLOBAL_SETUP__.ts create mode 100644 __tests__/__GLOBAL_TEARDOWN__.ts rename __tests__/{ => live}/insertInto.spec.ts (90%) rename __tests__/{ => live}/selectFrom.spec.ts (93%) rename __tests__/{ => live}/selectOneFrom.spec.ts (91%) rename __tests__/{ => live}/update.spec.ts (87%) rename __tests__/{ => raw}/createIndexRaw.spec.ts (92%) rename __tests__/{ => raw}/createTableRaw.spec.ts (92%) rename __tests__/{ => raw}/deleteFromRaw.spec.ts (87%) rename __tests__/{ => raw}/insertIntoRaw.spec.ts (87%) rename __tests__/{ => raw}/updateRaw.spec.ts (89%) diff --git a/__tests__/__GLOBAL_SETUP__.ts b/__tests__/__GLOBAL_SETUP__.ts new file mode 100644 index 0000000..433b57e --- /dev/null +++ b/__tests__/__GLOBAL_SETUP__.ts @@ -0,0 +1,27 @@ +import { ScylloClient } from '../lib'; + +type User = { + username: string, + uid: number +} + +export default async () => { + console.log('Preparing Database for Tests'); + + const DB = new ScylloClient<{ 'users': User }>({ + client: { + contactPoints: [ + 'localhost:9042' + ], + localDataCenter: 'datacenter1', + keyspace: 'system' + } + }); + await DB.awaitConnection(); + await DB.useKeyspace('scyllojestsuite', true); + await DB.createTable('users', true, { + username: { type: 'text' }, + uid: { type: 'bigint' }, + }, 'uid'); + await DB.shutdown(); +}; \ No newline at end of file diff --git a/__tests__/__GLOBAL_TEARDOWN__.ts b/__tests__/__GLOBAL_TEARDOWN__.ts new file mode 100644 index 0000000..db6bc13 --- /dev/null +++ b/__tests__/__GLOBAL_TEARDOWN__.ts @@ -0,0 +1,24 @@ +import { ScylloClient } from '../lib'; + +type User = { + username: string, + uid: number +} + +export default async () => { + console.log('Cleaning Database after Tests'); + + const DB = new ScylloClient<{ 'users': User }>({ + client: { + contactPoints: [ + 'localhost:9042' + ], + localDataCenter: 'datacenter1', + keyspace: 'system' + }, + prepare: false + }); + await DB.awaitConnection(); + await DB.raw('DROP KEYSPACE IF EXISTS scyllojestsuite'); + await DB.shutdown(); +}; \ No newline at end of file diff --git a/__tests__/insertInto.spec.ts b/__tests__/live/insertInto.spec.ts similarity index 90% rename from __tests__/insertInto.spec.ts rename to __tests__/live/insertInto.spec.ts index f6b5c8b..1db0a22 100644 --- a/__tests__/insertInto.spec.ts +++ b/__tests__/live/insertInto.spec.ts @@ -1,4 +1,4 @@ -import { ScylloClient } from "../lib"; +import { ScylloClient } from "../../lib"; type User = { username: string, @@ -14,7 +14,7 @@ beforeAll(async () => { 'localhost:9042' ], localDataCenter: 'datacenter1', - keyspace: 'scyllo' + keyspace: 'scyllojestsuite' } }); await DB.awaitConnection(); diff --git a/__tests__/selectFrom.spec.ts b/__tests__/live/selectFrom.spec.ts similarity index 93% rename from __tests__/selectFrom.spec.ts rename to __tests__/live/selectFrom.spec.ts index 33bf84a..596afb8 100644 --- a/__tests__/selectFrom.spec.ts +++ b/__tests__/live/selectFrom.spec.ts @@ -1,4 +1,4 @@ -import { ScylloClient } from "../lib"; +import { ScylloClient } from "../../lib"; type User = { username: string, @@ -14,7 +14,7 @@ beforeAll(async () => { 'localhost:9042' ], localDataCenter: 'datacenter1', - keyspace: 'scyllo' + keyspace: 'scyllojestsuite' } }); await DB.awaitConnection(); diff --git a/__tests__/selectOneFrom.spec.ts b/__tests__/live/selectOneFrom.spec.ts similarity index 91% rename from __tests__/selectOneFrom.spec.ts rename to __tests__/live/selectOneFrom.spec.ts index 898b547..a99eb0f 100644 --- a/__tests__/selectOneFrom.spec.ts +++ b/__tests__/live/selectOneFrom.spec.ts @@ -1,5 +1,5 @@ import Long from "long"; -import { ScylloClient, selectOneFromRaw } from "../lib"; +import { ScylloClient, selectOneFromRaw } from "../../lib"; type User = { username: string, @@ -15,7 +15,7 @@ beforeAll(async () => { 'localhost:9042' ], localDataCenter: 'datacenter1', - keyspace: 'scyllo' + keyspace: 'scyllojestsuite' } }); await DB.awaitConnection(); diff --git a/__tests__/update.spec.ts b/__tests__/live/update.spec.ts similarity index 87% rename from __tests__/update.spec.ts rename to __tests__/live/update.spec.ts index 1b85cbd..50f5401 100644 --- a/__tests__/update.spec.ts +++ b/__tests__/live/update.spec.ts @@ -1,5 +1,5 @@ import Long from "long"; -import { ScylloClient } from "../lib"; +import { ScylloClient } from "../../lib"; type User = { username: string, @@ -15,7 +15,7 @@ beforeAll(async () => { 'localhost:9042' ], localDataCenter: 'datacenter1', - keyspace: 'scyllo' + keyspace: 'scyllojestsuite' } }); await DB.awaitConnection(); diff --git a/__tests__/createIndexRaw.spec.ts b/__tests__/raw/createIndexRaw.spec.ts similarity index 92% rename from __tests__/createIndexRaw.spec.ts rename to __tests__/raw/createIndexRaw.spec.ts index 5848cbd..dcdbd97 100644 --- a/__tests__/createIndexRaw.spec.ts +++ b/__tests__/raw/createIndexRaw.spec.ts @@ -1,4 +1,4 @@ -import { createIndexRaw, createLocalIndexRaw } from '../lib'; +import { createIndexRaw, createLocalIndexRaw } from '../../lib'; it('Can create a basic table', async () => { expect( diff --git a/__tests__/createTableRaw.spec.ts b/__tests__/raw/createTableRaw.spec.ts similarity index 92% rename from __tests__/createTableRaw.spec.ts rename to __tests__/raw/createTableRaw.spec.ts index 5fde08d..1aca7ad 100644 --- a/__tests__/createTableRaw.spec.ts +++ b/__tests__/raw/createTableRaw.spec.ts @@ -1,4 +1,4 @@ -import { createTableRaw } from "../lib"; +import { createTableRaw } from "../../lib"; it('Can create a basic table', async () => { expect(createTableRaw<{'atable': {a:string}}, 'atable'>('scyllo', 'atable', false, {a: {type: 'bigint'}}, 'a')).toEqual({"args": [], "query": "CREATE TABLE scyllo.atable (a bigint, PRIMARY KEY (a))"}); diff --git a/__tests__/deleteFromRaw.spec.ts b/__tests__/raw/deleteFromRaw.spec.ts similarity index 87% rename from __tests__/deleteFromRaw.spec.ts rename to __tests__/raw/deleteFromRaw.spec.ts index fea5586..1881c8f 100644 --- a/__tests__/deleteFromRaw.spec.ts +++ b/__tests__/raw/deleteFromRaw.spec.ts @@ -1,4 +1,4 @@ -import { selectOneFromRaw } from "../lib"; +import { selectOneFromRaw } from "../../lib"; // fix it('Can insert a user into the database', async () => { diff --git a/__tests__/insertIntoRaw.spec.ts b/__tests__/raw/insertIntoRaw.spec.ts similarity index 87% rename from __tests__/insertIntoRaw.spec.ts rename to __tests__/raw/insertIntoRaw.spec.ts index fea5586..1881c8f 100644 --- a/__tests__/insertIntoRaw.spec.ts +++ b/__tests__/raw/insertIntoRaw.spec.ts @@ -1,4 +1,4 @@ -import { selectOneFromRaw } from "../lib"; +import { selectOneFromRaw } from "../../lib"; // fix it('Can insert a user into the database', async () => { diff --git a/__tests__/updateRaw.spec.ts b/__tests__/raw/updateRaw.spec.ts similarity index 89% rename from __tests__/updateRaw.spec.ts rename to __tests__/raw/updateRaw.spec.ts index 4309ea6..64dcdaf 100644 --- a/__tests__/updateRaw.spec.ts +++ b/__tests__/raw/updateRaw.spec.ts @@ -1,4 +1,4 @@ -import { updateRaw } from "../lib" +import { updateRaw } from "../../lib" it('Can update a specific user in the database', async () => { expect(updateRaw<{'users': {uid: number, username: string}}, 'users', 'uid' | 'username'>('scyllo', 'users', { username: "Jest" }, { uid: 1234567890 })).toEqual({query: 'UPDATE scyllo.users SET username=? WHERE uid=?', args: ['Jest', 1234567890]}); diff --git a/jest.config.js b/jest.config.js index ddce99d..257a709 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,8 +2,10 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', - testPathIgnorePatterns: ['/.history/', '/node_modules/'], + testPathIgnorePatterns: ['/.history/', '/node_modules/', '/__tests__/__.*.ts'], /*reporters: [ 'jest-nyancat-reporter' ]*/ + globalSetup: './__tests__/__GLOBAL_SETUP__.ts', + globalTeardown: './__tests__/__GLOBAL_TEARDOWN__.ts', }; \ No newline at end of file diff --git a/package.json b/package.json index 879c770..ddea08f 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "typescript-eslint": "^0.0.1-alpha.0" }, "scripts": { - "test": "yarn build && jest", + "test": "yarn build && jest --verbose", "build": "tsc", "lint": "eslint -c .eslintrc.json --ext .ts ./src", "pub": "yarn build && yarn publish --access public" diff --git a/src/ScylloClient.ts b/src/ScylloClient.ts index 313c109..f01e922 100644 --- a/src/ScylloClient.ts +++ b/src/ScylloClient.ts @@ -24,7 +24,25 @@ export type ScylloClientOptions = { * Cassandra-Driver config * Empty Object = `localhost:9042` */ - client: CassandraConfig; + client: CassandraConfig & { + /** + * Name of the keyspace you would like to use + * -scyllo was here + */ + keyspace: string, + /** + * Name of the datacenter you would like to use, + * This can be completely arbitrary and is used for routing. + * -scyllo was here + */ + localDataCenter: string + }; + /** + * Whether to prepare inputed args for query + * @default true + * @example false + */ + prepare?: boolean; /** * Custom log method * @default `console.log` @@ -45,16 +63,18 @@ const fromObjScyllo = (row: types.Row) => .map((item: any) => ({ [item]: fromScyllo(row.get(item)) })) ); export class ScylloClient { - keyspace: string = 'scyllo'; + keyspace: string; client: Client; debug: boolean; log: LogMethod; + prepare: boolean; constructor(options: ScylloClientOptions) { this.client = new Client(options.client); - this.keyspace = options.client.keyspace ?? ''; + this.keyspace = options.client.keyspace; this.debug = options.debug || false; this.log = options.log || console.log; + this.prepare = options.prepare ?? true; } /** * Await the connection @@ -81,7 +101,7 @@ export class ScylloClient { if (this.debug) this.log(`[Scyllo][Debug]\t${query}\n${args.join(' ')}`); - return await this.client.execute(query, args); + return await this.client.execute(query, args, { prepare: this.prepare }); } async query(query: QueryBuild): Promise { @@ -206,7 +226,7 @@ export class ScylloClient { async dropTable( table: Table ): Promise { - return await this.rawWithParams('DROP TABLE ?', [table]); + return await this.raw('DROP TABLE ' + table); } /** * Create diff --git a/src/ScylloTranslator.ts b/src/ScylloTranslator.ts index e5fa30b..af15e01 100644 --- a/src/ScylloTranslator.ts +++ b/src/ScylloTranslator.ts @@ -1,6 +1,6 @@ import { types } from 'cassandra-driver'; -export type ScylloSafeType = string | number | types.Long | boolean; +export type ScylloSafeType = string | number | types.Long | boolean | object; export type ValidDataType = | string | number @@ -12,10 +12,6 @@ export type ValidDataType = export const toScyllo: (a: ValidDataType) => ScylloSafeType = (a) => { if (a instanceof types.Long) return a; - if (Array.isArray(a) || a instanceof Object) { - return JSON.stringify(a); - } - return a; };