Skip to content

Commit

Permalink
Improve tests format and add prepare option (#16)
Browse files Browse the repository at this point in the history
Co-authored-by: Luc <[email protected]>

Co-authored-by: Luc <[email protected]>
  • Loading branch information
svemat01 and lucemans committed Jan 1, 2022
1 parent f1da4f2 commit 5e3d27c
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 25 deletions.
27 changes: 27 additions & 0 deletions __tests__/__GLOBAL_SETUP__.ts
Original file line number Diff line number Diff line change
@@ -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();
};
24 changes: 24 additions & 0 deletions __tests__/__GLOBAL_TEARDOWN__.ts
Original file line number Diff line number Diff line change
@@ -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();
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ScylloClient } from "../lib";
import { ScylloClient } from "../../lib";

type User = {
username: string,
Expand All @@ -14,7 +14,7 @@ beforeAll(async () => {
'localhost:9042'
],
localDataCenter: 'datacenter1',
keyspace: 'scyllo'
keyspace: 'scyllojestsuite'
}
});
await DB.awaitConnection();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ScylloClient } from "../lib";
import { ScylloClient } from "../../lib";

type User = {
username: string,
Expand All @@ -14,7 +14,7 @@ beforeAll(async () => {
'localhost:9042'
],
localDataCenter: 'datacenter1',
keyspace: 'scyllo'
keyspace: 'scyllojestsuite'
}
});
await DB.awaitConnection();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Long from "long";
import { ScylloClient, selectOneFromRaw } from "../lib";
import { ScylloClient, selectOneFromRaw } from "../../lib";

type User = {
username: string,
Expand All @@ -15,7 +15,7 @@ beforeAll(async () => {
'localhost:9042'
],
localDataCenter: 'datacenter1',
keyspace: 'scyllo'
keyspace: 'scyllojestsuite'
}
});
await DB.awaitConnection();
Expand Down
4 changes: 2 additions & 2 deletions __tests__/update.spec.ts → __tests__/live/update.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Long from "long";
import { ScylloClient } from "../lib";
import { ScylloClient } from "../../lib";

type User = {
username: string,
Expand All @@ -15,7 +15,7 @@ beforeAll(async () => {
'localhost:9042'
],
localDataCenter: 'datacenter1',
keyspace: 'scyllo'
keyspace: 'scyllojestsuite'
}
});
await DB.awaitConnection();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createIndexRaw, createLocalIndexRaw } from '../lib';
import { createIndexRaw, createLocalIndexRaw } from '../../lib';

it('Can create a basic table', async () => {
expect(
Expand Down
Original file line number Diff line number Diff line change
@@ -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))"});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { selectOneFromRaw } from "../lib";
import { selectOneFromRaw } from "../../lib";

// fix
it('Can insert a user into the database', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { selectOneFromRaw } from "../lib";
import { selectOneFromRaw } from "../../lib";

// fix
it('Can insert a user into the database', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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]});
Expand Down
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 25 additions & 5 deletions src/ScylloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -45,16 +63,18 @@ const fromObjScyllo = (row: types.Row) =>
.map((item: any) => ({ [item]: fromScyllo(row.get(item)) }))
);
export class ScylloClient<Tables extends TableScheme> {
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
Expand All @@ -81,7 +101,7 @@ export class ScylloClient<Tables extends TableScheme> {
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<types.ResultSet> {
Expand Down Expand Up @@ -206,7 +226,7 @@ export class ScylloClient<Tables extends TableScheme> {
async dropTable<Table extends keyof Tables>(
table: Table
): Promise<types.ResultSet> {
return await this.rawWithParams('DROP TABLE ?', [table]);
return await this.raw('DROP TABLE ' + table);
}
/**
* Create
Expand Down
6 changes: 1 addition & 5 deletions src/ScylloTranslator.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
};

Expand Down

0 comments on commit 5e3d27c

Please sign in to comment.