Skip to content

Commit 9eb198b

Browse files
committed
Added tests for models in different schemas
Some tests refactoring More code coverage
1 parent 6d21b5d commit 9eb198b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+2315
-19
lines changed

index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ module.exports = sails => {
133133
for (modelName in models) {
134134
modelDef = models[modelName];
135135
this.setAssociation(modelDef);
136-
this.setDefaultScope(modelDef);
136+
this.setDefaultScope(modelDef, sails.models[modelDef.globalId.toLowerCase()]);
137137
}
138138
},
139139

@@ -146,10 +146,9 @@ module.exports = sails => {
146146
}
147147
},
148148

149-
setDefaultScope (modelDef) {
149+
setDefaultScope (modelDef, model) {
150150
if (modelDef.defaultScope !== null) {
151151
sails.log.verbose('Loading default scope for \'' + modelDef.globalId + '\'');
152-
const model = global[modelDef.globalId];
153152
if (typeof modelDef.defaultScope === 'function') {
154153
const defaultScope = modelDef.defaultScope() || {};
155154
model.addScope('defaultScope', defaultScope, { override: true });
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
describe('Sails.js v0.12 Sequelize hook tests with db schemes (psql)', () => {
2+
3+
let Sails, rc, sails;
4+
5+
// Before running any tests, attempt to lift Sails
6+
before(function (done) {
7+
8+
// Hook will timeout in 10 seconds
9+
this.timeout(11000);
10+
11+
const Sequelize = require('sequelize'),
12+
connInfo = require('./fixtures/v0.12-create-db-schemes-app/config/connections').connections.somePostgresqlServer;
13+
14+
let connection = new Sequelize(connInfo.url, connInfo.options);
15+
16+
// Drop schemas if exists
17+
connection.query('DROP SCHEMA IF EXISTS sails, sails_img CASCADE;').then(() => {
18+
Sails = require('./fixtures/v0.12-create-db-schemes-app/app').sails;
19+
rc = require('rc');
20+
21+
const config = rc('sails');
22+
config.hooks.sequelize = require('../index');
23+
24+
// Attempt to lift sails
25+
Sails().lift(config, (err, _sails) => {
26+
if (err) { return done(err); }
27+
sails = _sails;
28+
return done(err, sails);
29+
});
30+
});
31+
32+
});
33+
34+
// Test that Sails can lift with the hook in place
35+
it('sails does not crash', () => true);
36+
37+
require('./unit/create.test');
38+
require('./unit/associations.test');
39+
require('./unit/scope.test');
40+
41+
after(done => {
42+
sails.lower(err => {
43+
if (err) {
44+
return console.log('Error occurred lowering Sails app: ', err);
45+
}
46+
console.log('Sails app lowered successfully!');
47+
done();
48+
});
49+
});
50+
});

test/bootstrap.v0.12-db-schemes.test.js renamed to test/bootstrap.v0.12-many-schemes.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ describe('Sails.js v0.12 Sequelize hook tests with db schemes (psql)', () => {
99
this.timeout(11000);
1010

1111
const Sequelize = require('sequelize'),
12-
connInfo = require('./fixtures/v0.12-db-schemes-app/config/connections').connections.somePostgresqlServer;
12+
connInfo = require('./fixtures/v0.12-many-schemes-app/config/connections').connections.somePostgresqlServer;
1313

1414
let connection = new Sequelize(connInfo.url, connInfo.options);
1515

1616
// Drop schemas if exists
17-
connection.query('DROP SCHEMA IF EXISTS sails CASCADE;').then(() => {
18-
Sails = require('./fixtures/v0.12-db-schemes-app/app').sails;
17+
connection.query('DROP SCHEMA IF EXISTS sails, sails_img CASCADE;').then(() => {
18+
Sails = require('./fixtures/v0.12-many-schemes-app/app').sails;
1919
rc = require('rc');
2020

2121
const config = rc('sails');
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* User.js
3+
*
4+
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
5+
* @docs :: http://sailsjs.org/#!documentation/models
6+
*/
7+
8+
module.exports = {
9+
attributes : {
10+
name: {
11+
type: Sequelize.STRING
12+
},
13+
age : {
14+
type: Sequelize.INTEGER
15+
}
16+
},
17+
associations: function () {
18+
User.hasMany(Image, { as: 'images', foreignKey: 'userId' });
19+
},
20+
defaultScope: function () {
21+
return {
22+
include: [
23+
{ model: Image, as: 'images' }
24+
]
25+
};
26+
},
27+
options : {
28+
freezeTableName : false,
29+
tableName : 'user',
30+
schema : 'sails',
31+
classMethods : {
32+
oneUniqueClassMethod: function () {
33+
return 'User class method';
34+
}
35+
},
36+
instanceMethods : {
37+
toJSON: function () {
38+
let obj = this.get();
39+
obj.ageString = '' + obj.age + ' years';
40+
return obj;
41+
}
42+
},
43+
hooks : {}
44+
}
45+
};
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
################################################
2+
############### .gitignore ##################
3+
################################################
4+
#
5+
# This file is only relevant if you are using git.
6+
#
7+
# Files which match the splat patterns below will
8+
# be ignored by git. This keeps random crap and
9+
# sensitive credentials from being uploaded to
10+
# your repository. It allows you to configure your
11+
# app for your machine without accidentally
12+
# committing settings which will smash the local
13+
# settings of other developers on your team.
14+
#
15+
# Some reasonable defaults are included below,
16+
# but, of course, you should modify/extend/prune
17+
# to fit your needs!
18+
################################################
19+
20+
21+
22+
23+
################################################
24+
# Local Configuration
25+
#
26+
# Explicitly ignore files which contain:
27+
#
28+
# 1. Sensitive information you'd rather not push to
29+
# your git repository.
30+
# e.g., your personal API keys or passwords.
31+
#
32+
# 2. Environment-specific configuration
33+
# Basically, anything that would be annoying
34+
# to have to change every time you do a
35+
# `git pull`
36+
# e.g., your local development database, or
37+
# the S3 bucket you're using for file uploads
38+
# development.
39+
#
40+
################################################
41+
42+
config/local.js
43+
44+
45+
46+
47+
48+
################################################
49+
# Dependencies
50+
#
51+
# When releasing a production app, you may
52+
# consider including your node_modules and
53+
# bower_components directory in your git repo,
54+
# but during development, its best to exclude it,
55+
# since different developers may be working on
56+
# different kernels, where dependencies would
57+
# need to be recompiled anyway.
58+
#
59+
# More on that here about node_modules dir:
60+
# http://www.futurealoof.com/posts/nodemodules-in-git.html
61+
# (credit Mikeal Rogers, @mikeal)
62+
#
63+
# About bower_components dir, you can see this:
64+
# http://addyosmani.com/blog/checking-in-front-end-dependencies/
65+
# (credit Addy Osmani, @addyosmani)
66+
#
67+
################################################
68+
69+
node_modules
70+
bower_components
71+
72+
73+
74+
75+
################################################
76+
# Sails.js / Waterline / Grunt
77+
#
78+
# Files generated by Sails and Grunt, or related
79+
# tasks and adapters.
80+
################################################
81+
.tmp
82+
dump.rdb
83+
84+
85+
86+
87+
88+
################################################
89+
# Node.js / NPM
90+
#
91+
# Common files generated by Node, NPM, and the
92+
# related ecosystem.
93+
################################################
94+
lib-cov
95+
*.seed
96+
*.log
97+
*.out
98+
*.pid
99+
npm-debug.log
100+
101+
102+
103+
104+
105+
################################################
106+
# Miscellaneous
107+
#
108+
# Common files generated by text editors,
109+
# operating systems, file systems, etc.
110+
################################################
111+
112+
*~
113+
*#
114+
.DS_STORE
115+
.netbeans
116+
nbproject
117+
.idea
118+
.node_history
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"generators": {
3+
"modules": {}
4+
},
5+
"hooks": {
6+
"blueprints": false,
7+
"grunt": false,
8+
"sockets": false,
9+
"pubsub": false,
10+
"orm": false,
11+
"i18n": false
12+
},
13+
"log": {
14+
"level": "verbose"
15+
}
16+
}

test/fixtures/v0.12-many-schemes-app/api/controllers/.gitkeep

Whitespace-only changes.

test/fixtures/v0.12-many-schemes-app/api/models/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* User.js
2+
* Group.js
33
*
44
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
55
* @docs :: http://sailsjs.org/#!documentation/models
@@ -10,26 +10,27 @@ module.exports = {
1010
name: {
1111
type: Sequelize.STRING
1212
},
13-
age : {
14-
type: Sequelize.INTEGER
13+
role: {
14+
type: Sequelize.ENUM('USER', 'ADMIN')
1515
}
1616
},
1717
associations: function () {
18-
User.hasMany(Image, { as: 'images', foreignKey: 'userId' });
18+
Group.hasMany(User, { as: 'users', foreignKey: 'groupId' });
1919
},
2020
defaultScope: function () {
2121
return {
2222
include: [
23-
{ model: Image, as: 'images' }
23+
{ model: User, as: 'users' }
2424
]
2525
};
2626
},
2727
options : {
2828
freezeTableName : false,
29-
tableName : 'user',
30-
schema : 'sails',
29+
tableName : 'group',
30+
schema : '',
3131
classMethods : {},
3232
instanceMethods : {},
3333
hooks : {}
3434
}
3535
};
36+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Image.js
3+
*
4+
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
5+
* @docs :: http://sailsjs.org/#!documentation/models
6+
*/
7+
8+
module.exports = {
9+
attributes : {
10+
url: {
11+
type: Sequelize.STRING
12+
}
13+
},
14+
associations: function () {
15+
Image.belongsTo(User, { foreignKey: 'userId' });
16+
},
17+
options : {
18+
freezeTableName : false,
19+
tableName : 'image',
20+
schema : 'sails_img',
21+
classMethods : {},
22+
instanceMethods : {},
23+
hooks : {}
24+
}
25+
};
26+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* User.js
3+
*
4+
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
5+
* @docs :: http://sailsjs.org/#!documentation/models
6+
*/
7+
8+
module.exports = {
9+
attributes : {
10+
name: {
11+
type: Sequelize.STRING
12+
},
13+
age : {
14+
type: Sequelize.INTEGER
15+
}
16+
},
17+
associations: function () {
18+
User.hasMany(Image, { as: 'images', foreignKey: 'userId' });
19+
},
20+
defaultScope: function () {
21+
return {
22+
include: [
23+
{ model: Image, as: 'images' }
24+
]
25+
};
26+
},
27+
options : {
28+
freezeTableName : false,
29+
tableName : 'user',
30+
schema : 'sails',
31+
classMethods : {
32+
oneUniqueClassMethod: function () {
33+
return 'User class method';
34+
}
35+
},
36+
instanceMethods : {
37+
toJSON: function () {
38+
let obj = this.get();
39+
obj.ageString = '' + obj.age + ' years';
40+
return obj;
41+
}
42+
},
43+
hooks : {}
44+
}
45+
};

0 commit comments

Comments
 (0)