Skip to content

Commit 56f82fb

Browse files
add tests for queryable interface machines
1 parent fc694ff commit 56f82fb

File tree

4 files changed

+418
-0
lines changed

4 files changed

+418
-0
lines changed

test/queryable/compile-statement.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var assert = require('assert');
2+
var Pack = require('../../');
3+
4+
describe('Queryable ::', function() {
5+
describe('Compile Statement', function() {
6+
it('should generate a SQL Statement from a WLQL query', function(done) {
7+
Pack.compileStatement({
8+
statement: {
9+
select: ['title', 'author', 'year'],
10+
from: 'books'
11+
}
12+
})
13+
.exec(function(err, report) {
14+
if (err) {
15+
return done(err);
16+
}
17+
18+
assert.equal(report.nativeQuery.sql, 'select `title`, `author`, `year` from `books`');
19+
return done();
20+
});
21+
});
22+
23+
// TODO: Add lots of checking to the statement compiler
24+
it.skip('should return the malformed exit for bad WLQL', function(done) {
25+
Pack.compileStatement({
26+
statement: {
27+
foo: 'bar',
28+
from: 'books'
29+
}
30+
})
31+
.exec(function(err) {
32+
assert(err);
33+
assert.equal(err.exit, 'malformed');
34+
return done();
35+
});
36+
});
37+
});
38+
});
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
var assert = require('assert');
2+
var _ = require('lodash');
3+
var Pack = require('../../');
4+
5+
describe('Queryable ::', function() {
6+
describe('Parse Native Query Error', function() {
7+
var manager;
8+
var connection;
9+
10+
// Create a manager and connection
11+
before(function(done) {
12+
// Needed to dynamically get the host using the docker container
13+
var host = process.env.MYSQL_PORT_3306_TCP_ADDR || 'localhost';
14+
15+
Pack.createManager({
16+
connectionString: 'mysql://mp:mp@' + host + ':3306/mppg'
17+
})
18+
.exec(function(err, report) {
19+
if (err) {
20+
return done(err);
21+
}
22+
23+
// Store the manager
24+
manager = report.manager;
25+
26+
Pack.getConnection({
27+
manager: manager
28+
})
29+
.exec(function(err, report) {
30+
if (err) {
31+
return done(err);
32+
}
33+
34+
// Store the connection
35+
connection = report.connection;
36+
37+
// Create a table to use for testing
38+
Pack.sendNativeQuery({
39+
connection: connection,
40+
nativeQuery: 'CREATE TABLE IF NOT EXISTS people(name varchar(255) UNIQUE);'
41+
})
42+
.exec(function(err) {
43+
if (err) {
44+
return done(err);
45+
}
46+
47+
return done();
48+
});
49+
});
50+
});
51+
});
52+
53+
// Afterwards destroy the test table and release the connection
54+
after(function(done) {
55+
Pack.sendNativeQuery({
56+
connection: connection,
57+
nativeQuery: 'DROP TABLE people;'
58+
})
59+
.exec(function(err) {
60+
if (err) {
61+
return done(err);
62+
}
63+
64+
Pack.releaseConnection({
65+
connection: connection
66+
}).exec(done);
67+
});
68+
});
69+
70+
it('should normalize UNIQUE constraint errors', function(done) {
71+
// Insert two records with identical names
72+
Pack.sendNativeQuery({
73+
connection: connection,
74+
nativeQuery: 'INSERT INTO people VALUES (\'Batman\'), (\'Batman\');'
75+
})
76+
.exec(function(err) {
77+
assert(err);
78+
assert.equal(err.exit, 'queryFailed');
79+
80+
Pack.parseNativeQueryError({
81+
nativeQueryError: err.output.error
82+
})
83+
.exec(function(err, report) {
84+
if (err) {
85+
return done(err);
86+
}
87+
88+
assert(report.footprint);
89+
assert(report.footprint.identity);
90+
assert.equal(report.footprint.identity, 'notUnique');
91+
assert(_.isArray(report.footprint.keys));
92+
assert.equal(report.footprint.keys.length, 1);
93+
assert.equal(_.first(report.footprint.keys), 'name');
94+
95+
return done();
96+
});
97+
});
98+
});
99+
});
100+
});
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
var assert = require('assert');
2+
var _ = require('lodash');
3+
var Pack = require('../../');
4+
5+
describe('Queryable ::', function() {
6+
describe('Parse Native Query Result', function() {
7+
var manager;
8+
var connection;
9+
10+
// Create a manager and connection
11+
before(function(done) {
12+
// Needed to dynamically get the host using the docker container
13+
var host = process.env.MYSQL_PORT_3306_TCP_ADDR || 'localhost';
14+
15+
Pack.createManager({
16+
connectionString: 'mysql://mp:mp@' + host + ':3306/mppg'
17+
})
18+
.exec(function(err, report) {
19+
if (err) {
20+
return done(err);
21+
}
22+
23+
// Store the manager
24+
manager = report.manager;
25+
26+
Pack.getConnection({
27+
manager: manager
28+
})
29+
.exec(function(err, report) {
30+
if (err) {
31+
return done(err);
32+
}
33+
34+
// Store the connection
35+
connection = report.connection;
36+
37+
// Create a table to use for testing
38+
Pack.sendNativeQuery({
39+
connection: connection,
40+
nativeQuery: 'CREATE TABLE IF NOT EXISTS people(id serial primary key, name varchar(255));'
41+
})
42+
.exec(function(err) {
43+
if (err) {
44+
return done(err);
45+
}
46+
47+
return done();
48+
});
49+
});
50+
});
51+
});
52+
53+
// Afterwards destroy the test table and release the connection
54+
after(function(done) {
55+
Pack.sendNativeQuery({
56+
connection: connection,
57+
nativeQuery: 'DROP TABLE people;'
58+
})
59+
.exec(function(err) {
60+
if (err) {
61+
return done(err);
62+
}
63+
64+
Pack.releaseConnection({
65+
connection: connection
66+
}).exec(done);
67+
});
68+
});
69+
70+
it('should normalize SELECT query results from a native query', function(done) {
71+
Pack.sendNativeQuery({
72+
connection: connection,
73+
nativeQuery: 'SELECT * from people;'
74+
})
75+
.exec(function(err, report) {
76+
if (err) {
77+
return done(err);
78+
}
79+
80+
var result = report.result;
81+
82+
Pack.parseNativeQueryResult({
83+
queryType: 'select',
84+
nativeQueryResult: result
85+
})
86+
.exec(function(err, report) {
87+
if (err) {
88+
return done(err);
89+
}
90+
91+
assert(report.result);
92+
assert(_.isArray(report.result));
93+
assert.equal(report.result.length, 0);
94+
95+
return done();
96+
});
97+
});
98+
});
99+
100+
it('should normalize INSERT query results from a native query', function(done) {
101+
Pack.sendNativeQuery({
102+
connection: connection,
103+
nativeQuery: 'INSERT INTO people (name) VALUES (\'hugo\');'
104+
})
105+
.exec(function(err, report) {
106+
if (err) {
107+
return done(err);
108+
}
109+
110+
var result = report.result;
111+
112+
Pack.parseNativeQueryResult({
113+
queryType: 'insert',
114+
nativeQueryResult: result
115+
})
116+
.exec(function(err, report) {
117+
if (err) {
118+
return done(err);
119+
}
120+
121+
assert(report.result);
122+
assert(report.result.inserted);
123+
124+
// We don't know what the ID will be so just check it's a number
125+
assert(_.isNumber(report.result.inserted));
126+
127+
return done();
128+
});
129+
});
130+
});
131+
132+
it('should normalize UPDATE query results from a native query', function(done) {
133+
Pack.sendNativeQuery({
134+
connection: connection,
135+
nativeQuery: 'INSERT INTO people (name) VALUES (\'hugo\');'
136+
})
137+
.exec(function(err, report) {
138+
if (err) {
139+
return done(err);
140+
}
141+
142+
Pack.sendNativeQuery({
143+
connection: connection,
144+
nativeQuery: 'UPDATE people SET name = \'George\' where id = ' + report.result.insertId + ';'
145+
})
146+
.exec(function(err, report) {
147+
if (err) {
148+
return done(err);
149+
}
150+
151+
var result = report.result;
152+
153+
Pack.parseNativeQueryResult({
154+
queryType: 'update',
155+
nativeQueryResult: result
156+
})
157+
.exec(function(err, report) {
158+
if (err) {
159+
return done(err);
160+
}
161+
162+
assert(report.result);
163+
assert(report.result.numRecordsUpdated);
164+
assert.equal(report.result.numRecordsUpdated, 1);
165+
166+
return done();
167+
});
168+
});
169+
});
170+
});
171+
172+
it('should normalize DELETE query results from a native query', function(done) {
173+
Pack.sendNativeQuery({
174+
connection: connection,
175+
nativeQuery: 'INSERT INTO people (name) VALUES (\'Sally\');'
176+
})
177+
.exec(function(err, report) {
178+
if (err) {
179+
return done(err);
180+
}
181+
182+
// Ensure that the record inserted ok
183+
assert.equal(report.result.affectedRows, 1);
184+
185+
Pack.sendNativeQuery({
186+
connection: connection,
187+
nativeQuery: 'DELETE FROM people WHERE name = \'Sally\';'
188+
})
189+
.exec(function(err, report) {
190+
if (err) {
191+
return done(err);
192+
}
193+
194+
var result = report.result;
195+
196+
Pack.parseNativeQueryResult({
197+
queryType: 'delete',
198+
nativeQueryResult: result
199+
})
200+
.exec(function(err, report) {
201+
if (err) {
202+
return done(err);
203+
}
204+
205+
assert(report.result);
206+
assert(report.result.numRecordsDeleted);
207+
assert.equal(report.result.numRecordsDeleted, 1);
208+
209+
return done();
210+
});
211+
});
212+
});
213+
});
214+
});
215+
});

0 commit comments

Comments
 (0)