Skip to content

Commit 93e1f1e

Browse files
author
Andrea Coronese
committed
package automated tests
1 parent 1101104 commit 93e1f1e

30 files changed

+1560
-13
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ ssl
77
.idea
88
nbproject
99
.waterline
10-
npm-debug.log
10+
npm-debug.log
11+
.c9

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
MOCHA_OPTS= --check-leaks
22
REPORTER = dot
33

4-
test: test-integration
4+
test: test-unit test-integration
55

66
test-integration:
77
@NODE_ENV=test node test/integration/runner.js
@@ -11,3 +11,9 @@ test-load:
1111
--reporter $(REPORTER) \
1212
$(MOCHA_OPTS) \
1313
test/load/**
14+
15+
test-unit:
16+
@NODE_ENV=test ./node_modules/.bin/mocha \
17+
--reporter $(REPORTER) \
18+
$(MOCHA_OPTS) \
19+
test/unit/**

lib/sql.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,13 @@ var sql = module.exports = {
350350

351351
// Cast waterline types into SQL data types
352352
function sqlTypeCast(attr) {
353-
var type = attr.type;
353+
var type;
354+
if(_.isObject(attr) && _.has(attr, 'type')) {
355+
type = attr.type;
356+
} else {
357+
type = attr;
358+
}
359+
354360
type = type && type.toLowerCase();
355361

356362
switch (type) {
@@ -406,6 +412,9 @@ function sqlTypeCast(attr) {
406412
case 'float':
407413
case 'double':
408414
return 'FLOAT';
415+
416+
case 'decimal':
417+
return 'DECIMAL';
409418

410419
case 'date':
411420
return 'DATE';

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"waterline-cursor": "~0.0.5"
2929
},
3030
"devDependencies": {
31+
"should": "*",
3132
"mocha": "~1.13.0",
3233
"waterline-adapter-tests": "~0.10.7",
3334
"captains-log": "~0.11.5"

test/integration/runner.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ var Adapter = require('../../lib/adapter');
2222

2323

2424
// Grab targeted interfaces from this adapter's `package.json` file:
25-
var package = {},
25+
var packageDefinition = {},
2626
interfaces = [];
2727
try {
28-
package = require('../../package.json');
29-
interfaces = package.waterlineAdapter.interfaces;
28+
packageDefinition = require('../../package.json');
29+
interfaces = packageDefinition.waterlineAdapter.interfaces;
3030
} catch (e) {
3131
throw new Error(
3232
'\n' +
@@ -38,7 +38,7 @@ try {
3838

3939

4040

41-
log.info('Testing `' + package.name + '`, a Sails/Waterline adapter.');
41+
log.info('Testing `' + packageDefinition.name + '`, a Sails/Waterline adapter.');
4242
log.info('Running `waterline-adapter-tests` against ' + interfaces.length + ' interfaces...');
4343
log.info('( ' + interfaces.join(', ') + ' )');
4444
console.log();
@@ -69,9 +69,9 @@ new TestRunner({
6969
config: {
7070
host: process.env.WATERLINE_ADAPTER_TESTS_HOST || 'localhost',
7171
port: process.env.WATERLINE_ADAPTER_TESTS_PORT || 3306,
72-
user: process.env.WATERLINE_ADAPTER_TESTS_USER || 'root',
72+
user: process.env.WATERLINE_ADAPTER_TESTS_USER || 'sixpounder',
7373
password: process.env.WATERLINE_ADAPTER_TESTS_PASSWORD || '',
74-
database: process.env.WATERLINE_ADAPTER_TESTS_DATABASE || 'sails_mysql',
74+
database: process.env.WATERLINE_ADAPTER_TESTS_DATABASE || 'sails_loadTest',
7575
pool: true,
7676
connectionLimit: 10,
7777
queueLimit: 0,

test/load/loadTest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('Load Testing', function() {
1313
var Schema;
1414

1515
// Register The Collection
16-
Adapter.registerCollection({ identity: 'loadTest', config: Config }, function(err) {
16+
Adapter.registerConnection({ identity: 'loadTest', config: Config }, {'Person': Fixture}, function(err) {
1717
if(err) done(err);
1818

1919
// Define The Collection

test/load/support/bootstrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ after(function(done) {
1717
});
1818

1919
function dropTable(cb) {
20-
Adapter.registerCollection({ identity: 'loadTest', config: config }, function(err) {
20+
Adapter.registerConnection({ identity: 'loadTest', config: config }, function(err) {
2121
if(err) cb(err);
2222
Adapter.drop('loadTest', cb);
2323
});

test/load/support/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module.exports = {
22
host: 'localhost',
3-
user: 'root',
3+
user: 'sixpounder',
44
password: '',
5-
database: 'sails-loadTest',
5+
database: 'sails_loadTest',
66
pool: true,
77
connectionLimit: 10,
88
waitForConnections: true

test/load/support/fixture.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = {
99
id:{
1010
type: 'integer',
1111
autoIncrement: true,
12+
size: 64,
1213
defaultsTo: 'AUTO_INCREMENT',
1314
primaryKey: true
1415
},

test/unit/adapter.addAttribute.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var adapter = require('../../lib/adapter'),
2+
_ = require('lodash'),
3+
should = require('should'),
4+
support = require('./support/bootstrap');
5+
6+
describe('adapter', function() {
7+
8+
/**
9+
* Setup and Teardown
10+
*/
11+
12+
before(function(done) {
13+
support.Setup('test_addAttribute', done);
14+
});
15+
16+
after(function(done) {
17+
support.Teardown('test_addAttribute', done);
18+
});
19+
20+
/**
21+
* ADD ATTRIBUTE
22+
*
23+
* Adds a column to a Table
24+
*/
25+
26+
describe('.addAttribute()', function() {
27+
28+
// Add a column to a table
29+
it('should add column color to the table', function(done) {
30+
31+
adapter.addAttribute('test', 'test_addAttribute', 'color', 'string', function(err, result) {
32+
adapter.describe('test', 'test_addAttribute', function(err, result) {
33+
34+
// Test Row length
35+
Object.keys(result).length.should.eql(4);
36+
37+
// Test the name of the last column
38+
should.exist(result.color);
39+
40+
done();
41+
});
42+
});
43+
44+
});
45+
});
46+
});

test/unit/adapter.avg.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var Sequel = require('waterline-sequel'),
2+
should = require('should'),
3+
Support = require('./support/bootstrap');
4+
5+
describe('query', function() {
6+
7+
/**
8+
* AVG
9+
*
10+
* Adds a AVG select parameter to a sql statement
11+
*/
12+
13+
describe('.avg()', function() {
14+
15+
describe('with array', function() {
16+
17+
// Lookup criteria
18+
var criteria = {
19+
where: {
20+
name: 'foo'
21+
},
22+
average: ['age']
23+
};
24+
25+
var schema = {'test': Support.Schema('test', { name: { type: 'text' }, age: { type: 'integer'} })};
26+
27+
it('should use the AVG aggregate option in the select statement', function() {
28+
var query = new Sequel(schema, Support.SqlOptions).find('test', criteria);
29+
var sql = 'SELECT CAST( AVG("test"."age") AS float) AS age FROM "test" AS "test" WHERE ' +
30+
'LOWER("test"."name") = $1 ';
31+
32+
query.query[0].should.eql(sql);
33+
});
34+
});
35+
36+
describe('with string', function() {
37+
38+
// Lookup criteria
39+
var criteria = {
40+
where: {
41+
name: 'foo'
42+
},
43+
average: 'age'
44+
};
45+
46+
var schema = {'test': Support.Schema('test', { name: { type: 'text' }, age: { type: 'integer'} })};
47+
48+
it('should use the AVG aggregate option in the select statement', function() {
49+
var query = new Sequel(schema, Support.SqlOptions).find('test', criteria);
50+
var sql = 'SELECT CAST( AVG("test"."age") AS float) AS age FROM "test" AS "test" WHERE ' +
51+
'LOWER("test"."name") = $1 ';
52+
53+
query.query[0].should.eql(sql);
54+
});
55+
});
56+
57+
});
58+
});

test/unit/adapter.create.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
var adapter = require('../../lib/adapter'),
2+
should = require('should'),
3+
support = require('./support/bootstrap');
4+
5+
describe('adapter', function() {
6+
7+
/**
8+
* Setup and Teardown
9+
*/
10+
11+
before(function(done) {
12+
support.Setup('test_create', done);
13+
});
14+
15+
after(function(done) {
16+
support.Teardown('test_create', done);
17+
});
18+
19+
// Attributes for the test table
20+
var attributes = {
21+
field_1: 'foo',
22+
field_2: 'bar'
23+
};
24+
25+
/**
26+
* CREATE
27+
*
28+
* Insert a row into a table
29+
*/
30+
31+
describe('.create()', function() {
32+
33+
// Insert a record
34+
it('should insert a single record', function(done) {
35+
adapter.create('test', 'test_create', attributes, function(err, result) {
36+
37+
// Check record was actually inserted
38+
support.Client(function(err, client, close) {
39+
client.query('SELECT * FROM test_create', function(err, rows) {
40+
if (err) {
41+
return done(err);
42+
}
43+
// Test 1 row is returned
44+
rows.length.should.eql(1);
45+
46+
// close client
47+
client.end();
48+
49+
done();
50+
});
51+
});
52+
});
53+
});
54+
55+
// Create Auto-Incremented ID
56+
it('should create an auto-incremented ID field', function(done) {
57+
adapter.create('test', 'test_create', attributes, function(err, result) {
58+
59+
// Should have an ID of 2
60+
result.id.should.eql(2);
61+
62+
done();
63+
});
64+
});
65+
66+
it('should keep case', function(done) {
67+
var attributes = {
68+
field_1: 'Foo',
69+
field_2: 'bAr'
70+
};
71+
72+
adapter.create('test', 'test_create', attributes, function(err, result) {
73+
74+
result.field_1.should.eql('Foo');
75+
result.field_2.should.eql('bAr');
76+
77+
done();
78+
});
79+
});
80+
81+
});
82+
});

test/unit/adapter.createEach.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var adapter = require('../../lib/adapter'),
2+
should = require('should'),
3+
support = require('./support/bootstrap');
4+
5+
describe('adapter', function() {
6+
7+
/**
8+
* Setup and Teardown
9+
*/
10+
11+
before(function(done) {
12+
support.Setup('test_createEach', done);
13+
});
14+
15+
after(function(done) {
16+
support.Teardown('test_createEach', done);
17+
});
18+
19+
// Attributes for the test table
20+
var attributes = {
21+
field_1: 'foo',
22+
field_2: 'bar'
23+
};
24+
25+
/**
26+
* CREATE EACH
27+
*
28+
* Insert an array of rows into a table
29+
*/
30+
31+
describe('.createEach()', function() {
32+
33+
// Insert multiple records
34+
it('should insert multiple records', function(done) {
35+
adapter.createEach('test', 'test_createEach', [attributes, attributes], function(err, result) {
36+
37+
// Check records were actually inserted
38+
support.Client(function(err, client) {
39+
client.query('SELECT * FROM test_createEach', function(err, rows) {
40+
41+
// Test 2 rows are returned
42+
rows.length.should.eql(2);
43+
44+
// close client
45+
client.end();
46+
47+
done();
48+
});
49+
});
50+
});
51+
});
52+
53+
});
54+
});

0 commit comments

Comments
 (0)