Skip to content

Commit

Permalink
#3 Added queues for multi queries support
Browse files Browse the repository at this point in the history
  • Loading branch information
SiroDiaz committed Aug 17, 2019
1 parent ff1da0f commit b2bd53b
Show file tree
Hide file tree
Showing 7 changed files with 801 additions and 666 deletions.
96 changes: 96 additions & 0 deletions __tests__/queue.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import Sphinxql from '../src/Sphinxql';
import Queue from '../src/Queue';
import BaseStatement from '../src/Statements/BaseStatement';

describe('Tests for Queue class', () => {

const sphql = Sphinxql.createConnection({
host: 'localhost',
port: 9307,
multipleStatements: true,
});

afterAll(() => {
sphql.getConnection().close();
})

test('create an empty queue', () => {
const queue = new Queue(sphql.getConnection());

expect(queue.empty()).toBeTruthy();
});

test('push multiple statements', () => {
const queue = new Queue(sphql.getConnection());

queue.push(sphql.getQueryBuilder().select('*').from('rt'));
queue.push(sphql.getQueryBuilder().select('*').from('rt'));

expect(queue.empty()).toBeFalsy();
expect(queue.size()).toBe(2);
});

test('shift statements', () => {
const queue = new Queue(sphql.getConnection());

queue.push(sphql.getQueryBuilder().select('*').from('rt'));
queue.push(sphql.getQueryBuilder().select('*').from('rt'));

expect(queue.empty()).toBeFalsy();
expect(queue.size()).toBe(2);

expect(queue.shift()).toBeInstanceOf(BaseStatement);
expect(queue.empty()).toBeFalsy();
expect(queue.size()).toBe(1);
expect(queue.shift()).toBeInstanceOf(BaseStatement);
expect(queue.empty()).toBeTruthy();
expect(queue.size()).toBe(0);
});

test('join query statements with a semicolon separator', () => {
const queue = new Queue(sphql.getConnection());

queue.push(sphql.getQueryBuilder().select('*').from('rt'));
queue.push(sphql.getQueryBuilder().select('*').from('rt'));

const result = queue.joinQueries();
const expected = "SELECT * FROM rt;SELECT * FROM rt";
expect(result).toBe(expected);
});

test('process multi query statements', (done) => {
const queue = new Queue(sphql.getConnection());

queue
.push(sphql.getQueryBuilder().select('*').from('rt'))
.push(sphql.getQueryBuilder().select('*').from('rt'))
.push(sphql.getQueryBuilder().select('*').from('rt'));

queue.process()
.then((results) => {
expect(results.results).toHaveLength(3);

done();
})
.catch((error) => {
console.log(error);
done();
});
});

test('process empty queue with rejected promise with an Error', (done) => {
const queue = new Queue(sphql.getConnection());

queue.process()
.then((results) => {
expect(results.results).toHaveLength(3);

done();
})
.catch((error) => {
expect(error).toBeInstanceOf(Error);
expect(error.message).not.toHaveLength(0);
done();
});
});
});
Loading

0 comments on commit b2bd53b

Please sign in to comment.