Skip to content

Commit

Permalink
Add tests for columnName in criteria and sort
Browse files Browse the repository at this point in the history
  • Loading branch information
jelhan committed Sep 15, 2016
1 parent 44c53c8 commit eb29183
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 6 deletions.
60 changes: 58 additions & 2 deletions interfaces/associations/hasMany/find.populate.where.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ describe('Association Interface', function() {
var payments = [];

for(var i=0; i<8; i++) {
if(i < 4) payments.push({ amount: i, a_customer: customers[0].id });
if(i >= 4) payments.push({ amount: i, a_customer: customers[1].id });
payments.push({
amount: i,
a_customer: i < 4 ? customers[0].id : customers[1].id,
type: i % 2 === 0 ? 'cash' : 'prepayment'
});
}

Associations.Payment.createEach(payments, function(err, payments) {
Expand Down Expand Up @@ -139,6 +142,59 @@ describe('Association Interface', function() {

});

it('should support sort', function(done) {
Associations.Customer.find({ name: 'hasMany find where' })
.populate('payments', { sort: { amount: 0 }})
.exec(function(err, customers) {
assert(!err,err);

assert(Array.isArray(customers));
assert.strictEqual(customers.length, 2);

assert(Array.isArray(customers[0].payments));
assert(Array.isArray(customers[1].payments));

assert.strictEqual(customers[0].payments.length, 4);
assert.strictEqual(customers[1].payments.length, 4);
assert.deepEqual(
customers[0].payments.map(function(payment) { return payment.amount; }),
[ 3, 2, 1, 0 ]
);
assert.deepEqual(
customers[1].payments.map(function(payment) { return payment.amount; }),
[ 7, 6, 5, 4 ]
);

done();
});
});

it('should support sort by a column with custom columnName', function(done) {
Associations.Customer.find({ name: 'hasMany find where' })
.populate('payments', { sort: { type: 1 }})
.exec(function(err, customers) {
assert(!err,err);

assert(Array.isArray(customers));
assert.strictEqual(customers.length, 2);

assert(Array.isArray(customers[0].payments));
assert(Array.isArray(customers[1].payments));

assert.strictEqual(customers[0].payments.length, 4);
assert.strictEqual(customers[1].payments.length, 4);
assert.deepEqual(
customers[0].payments.map(function(payment) { return payment.type; }),
[ 'cash', 'cash', 'prepayment', 'prepayment' ]
);
assert.deepEqual(
customers[1].payments.map(function(payment) { return payment.type; }),
[ 'cash', 'cash', 'prepayment', 'prepayment' ]
);

done();
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ module.exports = Waterline.Collection.extend({

attributes: {
amount: 'integer',
type: 'string',
type: {
type: 'string',
columnName: 'payment_type'
},
apartment: {
model: 'apartment',
columnName: 'apartment_id'
Expand Down
23 changes: 22 additions & 1 deletion interfaces/semantic/find.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Semantic Interface', function() {
var users = [];

for(var i=0; i<10; i++) {
users.push({first_name: 'find_user' + i, type: 'find test', age: i*10 }); // include an integer field
users.push({first_name: 'find_user' + i, type: 'find test', age: i*10, email: '[email protected]', percent: i/10 }); // include an integer field
}

Semantic.User.createEach(users, function(err, users) {
Expand Down Expand Up @@ -78,5 +78,26 @@ describe('Semantic Interface', function() {
});
});

it('should support columnName in criteria', function(done) {
Semantic.User.find({ email: '[email protected]' }, function(err, users) {
assert.ifError(err);
assert(Array.isArray(users));
assert.strictEqual(users.length, 10);
done();
});
});

it('should support columnName in sort', function(done) {
Semantic.User.find({ type: 'find test', sort: 'percent DESC' }, function(err, users) {
assert.ifError(err);
assert(Array.isArray(users));
assert.strictEqual(users.length, 10);
assert.deepEqual(
users.map(function(user) { return user.percent; }),
[0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0]
);
done();
});
});
});
});
5 changes: 4 additions & 1 deletion interfaces/semantic/support/fixtures/crud.fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ module.exports = Waterline.Collection.extend({
type: 'boolean',
defaultsTo: false
},
percent: 'float',
percent: {
type: 'float',
columnName: 'percent_value'
},
list: {
type: 'array',
columnName: 'arrList'
Expand Down
2 changes: 1 addition & 1 deletion test/integration/runnerDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var jpath = require('jpath');
// Config

// The adapters being tested
var adapters = ['sails-postgresql', 'sails-memory', 'sails-disk', 'sails-mongo', 'sails-mysql'];
var adapters = ['sails-memory', 'sails-disk', 'sails-mysql'];

// Core modules npm Dependencies path
var coreModulesPaths = {
Expand Down

0 comments on commit eb29183

Please sign in to comment.