loopback-connector-mysql/test/mysql.test.js

1036 lines
33 KiB
JavaScript
Raw Normal View History

2020-02-08 22:23:33 +00:00
// Copyright IBM Corp. 2013,2019. All Rights Reserved.
2016-05-03 23:52:03 +00:00
// Node module: loopback-connector-mysql
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2016-08-10 18:41:03 +00:00
'use strict';
const async = require('async');
const should = require('./init.js');
const sinon = require('sinon');
2019-06-28 14:54:45 +00:00
const List = require('loopback-datasource-juggler/lib/list');
2014-05-30 22:15:27 +00:00
let Post, PostWithStringId, PostWithUniqueTitle, PostWithNumId, Student, db;
2014-05-30 22:15:27 +00:00
// Mock up mongodb ObjectID
function ObjectID(id) {
if (!(this instanceof ObjectID)) {
return new ObjectID(id);
}
this.id1 = id.substring(0, 2);
this.id2 = id.substring(2);
}
ObjectID.prototype.toJSON = function() {
return this.id1 + this.id2;
};
2016-08-10 18:41:03 +00:00
describe('mysql', function() {
before(function(done) {
2017-07-24 16:48:26 +00:00
db = global.getDataSource();
2014-05-30 22:15:27 +00:00
Post = db.define('PostWithDefaultId', {
2016-08-10 18:41:03 +00:00
title: {type: String, length: 255, index: true},
content: {type: String},
comments: [String],
history: Object,
stars: Number,
2016-08-10 18:41:03 +00:00
userId: ObjectID,
}, {
2016-09-28 20:32:38 +00:00
forceId: false,
indexes: {
content_fts_index: {
kind: 'FULLTEXT',
columns: 'content',
},
title_fts_index: {
kind: 'FULLTEXT',
columns: 'title',
},
},
allowExtendedOperators: true,
2014-05-30 22:15:27 +00:00
});
PostWithStringId = db.define('PostWithStringId', {
id: {type: String, id: true},
2016-08-10 18:41:03 +00:00
title: {type: String, length: 255, index: true},
content: {type: String},
2014-05-30 22:15:27 +00:00
});
PostWithNumId = db.define('PostWithNumId', {
id: {type: Number, id: true, null: false},
title: {type: String, length: 255, null: false, index: true},
content: {type: String, null: false},
buffProp: {type: Buffer, null: false},
objProp: {type: Object, null: false},
arrProp: {type: [Number], null: false},
dateProp: {type: Date, null: false},
pointProp: {type: 'GeoPoint', null: false},
});
2014-10-16 12:03:12 +00:00
PostWithUniqueTitle = db.define('PostWithUniqueTitle', {
2016-08-10 18:41:03 +00:00
title: {type: String, length: 255, index: {unique: true}},
content: {type: String},
2014-10-16 12:03:12 +00:00
});
Student = db.define('Student', {
name: {type: String, length: 255},
age: {type: Number},
}, {
forceId: false,
2014-05-30 22:15:27 +00:00
});
db.automigrate(
['PostWithDefaultId', 'PostWithStringId',
'PostWithUniqueTitle', 'PostWithNumId', 'Student'],
function(err) {
should.not.exist(err);
done(err);
},
);
});
beforeEach(function() {
return deleteAllModelInstances();
2014-05-30 22:15:27 +00:00
});
2016-08-10 18:41:03 +00:00
it('should allow array or object', function(done) {
Post.create({title: 'a', content: 'AAA', comments: ['1', '2'],
history: {a: 1, b: 'b'}}, function(err, post) {
should.not.exist(err);
Post.findById(post.id, function(err, p) {
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
p.title.should.be.equal('a');
2019-06-28 14:54:45 +00:00
p.comments.should.eql(new List(['1', '2']));
p.history.should.eql({a: 1, b: 'b'});
done();
});
});
});
it('should allow ObjectID', function(done) {
const uid = new ObjectID('123');
Post.create({title: 'a', content: 'AAA', userId: uid},
function(err, post) {
should.not.exist(err);
Post.findById(post.id, function(err, p) {
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
p.title.should.be.equal('a');
p.userId.should.eql(uid);
done();
});
});
});
2016-08-10 18:41:03 +00:00
it('updateOrCreate should update the instance', function(done) {
Post.create({title: 'a', content: 'AAA'}, function(err, post) {
2014-05-30 22:15:27 +00:00
post.title = 'b';
2016-08-10 18:41:03 +00:00
Post.updateOrCreate(post, function(err, p) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
2016-08-10 18:41:03 +00:00
Post.findById(post.id, function(err, p) {
2014-05-30 22:15:27 +00:00
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
p.title.should.be.equal('b');
done();
});
});
});
});
2016-08-10 18:41:03 +00:00
it('updateOrCreate should update the instance without removing existing properties', function(done) {
Post.create({title: 'a', content: 'AAA'}, function(err, post) {
2014-05-30 22:15:27 +00:00
post = post.toObject();
delete post.title;
2016-08-10 18:41:03 +00:00
Post.updateOrCreate(post, function(err, p) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
2016-08-10 18:41:03 +00:00
Post.findById(post.id, function(err, p) {
2014-05-30 22:15:27 +00:00
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
p.title.should.be.equal('a');
done();
});
});
});
});
2016-08-10 18:41:03 +00:00
it('updateOrCreate should create a new instance if it does not exist', function(done) {
const post = {id: 123, title: 'a', content: 'AAA'};
2016-08-10 18:41:03 +00:00
Post.updateOrCreate(post, function(err, p) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
p.title.should.be.equal(post.title);
p.content.should.be.equal(post.content);
p.id.should.be.equal(post.id);
2016-08-10 18:41:03 +00:00
Post.findById(p.id, function(err, p) {
2014-05-30 22:15:27 +00:00
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
p.title.should.be.equal(post.title);
p.id.should.be.equal(post.id);
done();
});
});
});
2015-12-04 19:55:13 +00:00
context('replaceOrCreate', function() {
it('should replace the instance', function(done) {
Post.create({title: 'a', content: 'AAA'}, function(err, post) {
if (err) return done(err);
post = post.toObject();
delete post.content;
Post.replaceOrCreate(post, function(err, p) {
if (err) return done(err);
p.id.should.equal(post.id);
2016-08-10 18:41:03 +00:00
p.title.should.equal('a');
2015-12-04 19:55:13 +00:00
should.not.exist(p.content);
should.not.exist(p._id);
Post.findById(post.id, function(err, p) {
if (err) return done(err);
p.id.should.equal(post.id);
2016-08-10 18:41:03 +00:00
p.title.should.equal('a');
2015-12-04 19:55:13 +00:00
should.not.exist(post.content);
should.not.exist(p._id);
done();
});
});
});
});
it('should replace with new data', function(done) {
Post.create({title: 'a', content: 'AAA', comments: ['Comment1']},
function(err, post) {
if (err) return done(err);
post = post.toObject();
delete post.comments;
delete post.content;
post.title = 'b';
Post.replaceOrCreate(post, function(err, p) {
if (err) return done(err);
p.id.should.equal(post.id);
should.not.exist(p._id);
2016-08-10 18:41:03 +00:00
p.title.should.equal('b');
2015-12-04 19:55:13 +00:00
should.not.exist(p.content);
should.not.exist(p.comments);
Post.findById(post.id, function(err, p) {
if (err) return done(err);
p.id.should.equal(post.id);
should.not.exist(p._id);
p.title.should.equal('b');
should.not.exist(p.content);
should.not.exist(p.comments);
done();
});
});
});
});
it('should create a new instance if it does not exist', function(done) {
const post = {id: 123, title: 'a', content: 'AAA'};
2015-12-04 19:55:13 +00:00
Post.replaceOrCreate(post, function(err, p) {
if (err) return done(err);
p.id.should.equal(post.id);
should.not.exist(p._id);
p.title.should.equal(post.title);
p.content.should.equal(post.content);
Post.findById(p.id, function(err, p) {
if (err) return done(err);
p.id.should.equal(post.id);
should.not.exist(p._id);
p.title.should.equal(post.title);
p.content.should.equal(post.content);
done();
});
});
});
it('isNewInstance should be undefined for after save hook', function(done) {
const student = {name: 'Joe', age: 20};
const newStudent = {};
let isNewInstanceBefore = false;
let isNewInstanceAfter = false;
Student.create(student, function(err, createdStudent) {
if (err) return done(err);
newStudent.id = createdStudent.id;
newStudent.name = 'Hannah';
newStudent.age = 25;
Student.observe('before save', function(ctx, next) {
isNewInstanceBefore = ctx.isNewInstance;
next();
});
Student.observe('after save', function(ctx, next) {
isNewInstanceAfter = ctx.isNewInstance;
next();
});
Student.replaceOrCreate(newStudent, function(err, s) {
if (err) return done(err);
should.not.exist(isNewInstanceBefore);
should.not.exist(isNewInstanceAfter);
done();
});
});
});
2015-12-04 19:55:13 +00:00
});
2016-08-10 18:41:03 +00:00
it('save should update the instance with the same id', function(done) {
Post.create({title: 'a', content: 'AAA'}, function(err, post) {
2014-05-30 22:15:27 +00:00
post.title = 'b';
2016-08-10 18:41:03 +00:00
post.save(function(err, p) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
2016-08-10 18:41:03 +00:00
Post.findById(post.id, function(err, p) {
2014-05-30 22:15:27 +00:00
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
p.title.should.be.equal('b');
done();
});
});
});
});
2017-07-24 16:48:26 +00:00
it('save should update the instance without removing existing properties',
function(done) {
Post.create({title: 'a', content: 'AAA'}, function(err, post) {
delete post.title;
post.save(function(err, p) {
should.not.exist(err);
2014-05-30 22:15:27 +00:00
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
2017-07-24 16:48:26 +00:00
Post.findById(post.id, function(err, p) {
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
p.title.should.be.equal('a');
done();
});
2014-05-30 22:15:27 +00:00
});
});
});
2016-08-10 18:41:03 +00:00
it('save should create a new instance if it does not exist', function(done) {
const post = new Post({id: 123, title: 'a', content: 'AAA'});
2016-08-10 18:41:03 +00:00
post.save(post, function(err, p) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
p.title.should.be.equal(post.title);
p.content.should.be.equal(post.content);
p.id.should.be.equal(post.id);
2016-08-10 18:41:03 +00:00
Post.findById(p.id, function(err, p) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
p.id.should.be.equal(post.id);
p.content.should.be.equal(post.content);
p.title.should.be.equal(post.title);
p.id.should.be.equal(post.id);
done();
});
});
});
2016-08-10 18:41:03 +00:00
it('all return should honor filter.fields', function(done) {
const post = new Post({title: 'b', content: 'BBB'});
2016-08-10 18:41:03 +00:00
post.save(function(err, post) {
Post.all({fields: ['title'], where: {title: 'b'}}, function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.should.have.lengthOf(1);
post = posts[0];
post.should.have.property('title', 'b');
2015-02-21 00:15:15 +00:00
post.should.have.property('content', undefined);
2014-05-30 22:15:27 +00:00
should.not.exist(post.id);
done();
});
});
});
it('find should order by id if the order is not set for the query filter',
2016-08-10 18:41:03 +00:00
function(done) {
PostWithStringId.create({id: '2', title: 'c', content: 'CCC'}, function(err, post) {
PostWithStringId.create({id: '1', title: 'd', content: 'DDD'}, function(err, post) {
PostWithStringId.find(function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.length.should.be.equal(2);
posts[0].id.should.be.equal('1');
2016-08-10 18:41:03 +00:00
PostWithStringId.find({limit: 1, offset: 0}, function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.length.should.be.equal(1);
posts[0].id.should.be.equal('1');
2016-08-10 18:41:03 +00:00
PostWithStringId.find({limit: 1, offset: 1}, function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.length.should.be.equal(1);
posts[0].id.should.be.equal('2');
done();
});
});
});
});
});
});
2016-08-10 18:41:03 +00:00
it('should allow to find using like', function(done) {
Post.create({title: 'My Post', content: 'Hello'}, function(err, post) {
Post.find({where: {title: {like: 'M%st'}}}, function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.should.have.property('length', 1);
done();
});
});
});
2016-08-10 18:41:03 +00:00
it('should support like for no match', function(done) {
Post.create({title: 'My Post', content: 'Hello'}, function(err, post) {
Post.find({where: {title: {like: 'M%XY'}}}, function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.should.have.property('length', 0);
done();
});
});
});
2016-08-10 18:41:03 +00:00
it('should allow to find using nlike', function(done) {
Post.create({title: 'My Post', content: 'Hello'}, function(err, post) {
Post.find({where: {title: {nlike: 'M%st'}}}, function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.should.have.property('length', 0);
done();
});
});
});
2016-08-10 18:41:03 +00:00
it('should support nlike for no match', function(done) {
Post.create({title: 'My Post', content: 'Hello'}, function(err, post) {
Post.find({where: {title: {nlike: 'M%XY'}}}, function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.should.have.property('length', 1);
done();
});
});
});
2016-08-10 18:41:03 +00:00
it('should support "and" operator that is satisfied', function(done) {
Post.create({title: 'My Post', content: 'Hello'}, function(err, post) {
2014-05-30 22:15:27 +00:00
Post.find({where: {and: [
{title: 'My Post'},
2016-08-10 18:41:03 +00:00
{content: 'Hello'},
]}}, function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.should.have.property('length', 1);
done();
});
});
});
2016-08-10 18:41:03 +00:00
it('should support "and" operator that is not satisfied', function(done) {
Post.create({title: 'My Post', content: 'Hello'}, function(err, post) {
2014-05-30 22:15:27 +00:00
Post.find({where: {and: [
{title: 'My Post'},
2016-08-10 18:41:03 +00:00
{content: 'Hello1'},
]}}, function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.should.have.property('length', 0);
done();
});
});
});
2016-08-10 18:41:03 +00:00
it('should support "or" that is satisfied', function(done) {
Post.create({title: 'My Post', content: 'Hello'}, function(err, post) {
2014-05-30 22:15:27 +00:00
Post.find({where: {or: [
{title: 'My Post'},
2016-08-10 18:41:03 +00:00
{content: 'Hello1'},
]}}, function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.should.have.property('length', 1);
done();
});
});
});
2016-08-10 18:41:03 +00:00
it('should support "or" operator that is not satisfied', function(done) {
Post.create({title: 'My Post', content: 'Hello'}, function(err, post) {
2014-05-30 22:15:27 +00:00
Post.find({where: {or: [
{title: 'My Post1'},
2016-08-10 18:41:03 +00:00
{content: 'Hello1'},
]}}, function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.should.have.property('length', 0);
done();
});
});
});
context('null vals in different operators', function() {
const defaultPost = {
id: 3,
title: 'defTitle',
content: 'defContent',
buffProp: new Buffer('defBuffer'),
objProp: {defKey: 'defVal'},
arrProp: [0],
dateProp: new Date('2017-06-14'),
pointProp: {lng: 4.51515, lat: 57.2314},
};
beforeEach(function(done) {
PostWithNumId.destroyAll(done);
});
after(function(done) {
PostWithNumId.destroyAll(done);
});
it('should handle null in inq operator', function(done) {
defaultPost.id = 1;
defaultPost.title = 'Foo';
defaultPost.content = 'Bar';
PostWithNumId.create(defaultPost, function(err, post) {
should.not.exist(err);
post.id.should.equal(defaultPost.id);
PostWithNumId.find({where: {id: {inq: [null, 1]}}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].title.should.equal('Foo');
posts[0].id.should.equal(1);
done();
});
});
});
it('should handle null in nin operator', function(done) {
defaultPost.id = 2;
defaultPost.title = 'Make';
defaultPost.content = 'Toyota';
PostWithNumId.create(defaultPost, function(err, post) {
should.not.exist(err);
post.id.should.equal(defaultPost.id);
PostWithNumId.find({where: {id: {nin: [null, 3]}}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('Toyota');
posts[0].id.should.equal(2);
done();
});
});
});
it('should handle null in neq operator', function(done) {
defaultPost.id = 3;
defaultPost.title = 'Model';
defaultPost.content = 'Corolla';
PostWithNumId.create(defaultPost, function(err, post) {
should.not.exist(err);
post.id.should.equal(defaultPost.id);
PostWithNumId.find({where: {id: {neq: null}}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('Corolla');
posts[0].id.should.equal(3);
done();
});
});
});
it('should handle null in nin op for different datatypes', function(done) {
PostWithNumId.create(defaultPost, function(err, post) {
should.not.exist(err);
post.id.should.equal(defaultPost.id);
PostWithNumId.find({where: {and: [
2017-07-24 16:48:26 +00:00
{id: {nin: [null]}},
{title: {nin: [null]}},
{content: {nin: [null]}},
{buffProp: {nin: [null]}},
{objProp: {nin: [null]}},
{arrProp: {nin: [null]}},
{dateProp: {nin: [null]}},
{pointProp: {nin: [null]}},
]}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].toObject().should.deepEqual(defaultPost);
done();
});
});
});
});
2014-05-30 22:15:27 +00:00
// The where object should be parsed by the connector
2016-08-10 18:41:03 +00:00
it('should support where for count', function(done) {
Post.create({title: 'My Post', content: 'Hello'}, function(err, post) {
2014-05-30 22:15:27 +00:00
Post.count({and: [
{title: 'My Post'},
2016-08-10 18:41:03 +00:00
{content: 'Hello'},
]}, function(err, count) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
count.should.be.equal(1);
Post.count({and: [
{title: 'My Post1'},
2016-08-10 18:41:03 +00:00
{content: 'Hello'},
]}, function(err, count) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
count.should.be.equal(0);
done();
});
});
});
});
// The where object should be parsed by the connector
2016-08-10 18:41:03 +00:00
it('should support where for destroyAll', function(done) {
Post.create({title: 'My Post1', content: 'Hello'}, function(err, post) {
Post.create({title: 'My Post2', content: 'Hello'}, function(err, post) {
2014-05-30 22:15:27 +00:00
Post.destroyAll({and: [
{title: 'My Post1'},
2016-08-10 18:41:03 +00:00
{content: 'Hello'},
]}, function(err) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
2016-08-10 18:41:03 +00:00
Post.count(function(err, count) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
count.should.be.equal(1);
done();
});
});
});
});
});
2016-08-10 18:41:03 +00:00
it('should not allow SQL injection for inq operator', function(done) {
2014-05-30 22:15:27 +00:00
Post.create({title: 'My Post1', content: 'Hello', stars: 5},
2016-08-10 18:41:03 +00:00
function(err, post) {
2014-05-30 22:15:27 +00:00
Post.create({title: 'My Post2', content: 'Hello', stars: 20},
2016-08-10 18:41:03 +00:00
function(err, post) {
Post.find({where: {title: {inq: ['SELECT title from PostWithDefaultId']}}},
2016-08-10 18:41:03 +00:00
function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.should.have.property('length', 0);
done();
});
});
});
});
2016-08-10 18:41:03 +00:00
it('should not allow SQL injection for lt operator', function(done) {
2014-05-30 22:15:27 +00:00
Post.create({title: 'My Post1', content: 'Hello', stars: 5},
2016-08-10 18:41:03 +00:00
function(err, post) {
2014-05-30 22:15:27 +00:00
Post.create({title: 'My Post2', content: 'Hello', stars: 20},
2016-08-10 18:41:03 +00:00
function(err, post) {
2014-05-30 22:15:27 +00:00
Post.find({where: {stars: {lt: 'SELECT title from PostWithDefaultId'}}},
2016-08-10 18:41:03 +00:00
function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.should.have.property('length', 0);
done();
});
});
});
});
2016-08-10 18:41:03 +00:00
it('should not allow SQL injection for nin operator', function(done) {
2014-05-30 22:15:27 +00:00
Post.create({title: 'My Post1', content: 'Hello', stars: 5},
2016-08-10 18:41:03 +00:00
function(err, post) {
2014-05-30 22:15:27 +00:00
Post.create({title: 'My Post2', content: 'Hello', stars: 20},
2016-08-10 18:41:03 +00:00
function(err, post) {
Post.find({where: {title: {nin: ['SELECT title from PostWithDefaultId']}}},
2016-08-10 18:41:03 +00:00
function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.should.have.property('length', 2);
done();
});
});
});
});
2016-08-10 18:41:03 +00:00
it('should not allow SQL injection for inq operator with number column', function(done) {
2014-05-30 22:15:27 +00:00
Post.create({title: 'My Post1', content: 'Hello', stars: 5},
2016-08-10 18:41:03 +00:00
function(err, post) {
2014-05-30 22:15:27 +00:00
Post.create({title: 'My Post2', content: 'Hello', stars: 20},
2016-08-10 18:41:03 +00:00
function(err, post) {
Post.find({where: {stars: {inq: ['SELECT title from PostWithDefaultId']}}},
2016-08-10 18:41:03 +00:00
function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.should.have.property('length', 0);
done();
});
});
});
});
2016-08-10 18:41:03 +00:00
it('should not allow SQL injection for inq operator with array value', function(done) {
2014-05-30 22:15:27 +00:00
Post.create({title: 'My Post1', content: 'Hello', stars: 5},
2016-08-10 18:41:03 +00:00
function(err, post) {
2014-05-30 22:15:27 +00:00
Post.create({title: 'My Post2', content: 'Hello', stars: 20},
2016-08-10 18:41:03 +00:00
function(err, post) {
2014-05-30 22:15:27 +00:00
Post.find({where: {stars: {inq: [5, 'SELECT title from PostWithDefaultId']}}},
2016-08-10 18:41:03 +00:00
function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.should.have.property('length', 1);
done();
});
});
});
});
2016-08-10 18:41:03 +00:00
it('should not allow SQL injection for between operator', function(done) {
2014-05-30 22:15:27 +00:00
Post.create({title: 'My Post1', content: 'Hello', stars: 5},
2016-08-10 18:41:03 +00:00
function(err, post) {
2014-05-30 22:15:27 +00:00
Post.create({title: 'My Post2', content: 'Hello', stars: 20},
2016-08-10 18:41:03 +00:00
function(err, post) {
2014-05-30 22:15:27 +00:00
Post.find({where: {stars: {between: [5, 'SELECT title from PostWithDefaultId']}}},
2016-08-10 18:41:03 +00:00
function(err, posts) {
2014-05-30 22:15:27 +00:00
should.not.exist(err);
posts.should.have.property('length', 0);
done();
});
});
});
});
2016-08-10 18:41:03 +00:00
it('should not allow duplicate titles', function(done) {
const data = {title: 'a', content: 'AAA'};
2016-08-10 18:41:03 +00:00
PostWithUniqueTitle.create(data, function(err, post) {
2014-10-16 12:03:12 +00:00
should.not.exist(err);
2016-08-10 18:41:03 +00:00
PostWithUniqueTitle.create(data, function(err, post) {
2014-10-16 12:03:12 +00:00
should.exist(err);
done();
});
});
});
2015-07-21 04:50:47 +00:00
context('regexp operator', function() {
2015-07-29 00:55:27 +00:00
beforeEach(function deleteExistingTestFixtures(done) {
Post.destroyAll(done);
});
2015-07-21 04:50:47 +00:00
beforeEach(function createTestFixtures(done) {
Post.create([
{title: 'a', content: 'AAA'},
2016-08-10 18:41:03 +00:00
{title: 'b', content: 'BBB'},
2015-07-21 04:50:47 +00:00
], done);
});
2015-07-29 00:55:27 +00:00
after(function deleteTestFixtures(done) {
2015-07-21 04:50:47 +00:00
Post.destroyAll(done);
});
context('with regex strings', function() {
context('using no flags', function() {
it('should work', function(done) {
Post.find({where: {content: {regexp: '^A'}}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('AAA');
done();
});
});
});
context('using flags', function() {
beforeEach(function addSpy() {
sinon.stub(console, 'warn');
});
afterEach(function removeSpy() {
2015-07-21 04:50:47 +00:00
console.warn.restore();
});
2015-07-29 16:31:24 +00:00
it('should work', function(done) {
Post.find({where: {content: {regexp: '^a/i'}}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('AAA');
done();
});
});
2015-07-21 04:50:47 +00:00
it('should print a warning when the ignore flag is set',
2017-07-24 16:48:26 +00:00
function(done) {
Post.find({where: {content: {regexp: '^a/i'}}}, function(err, posts) {
console.warn.calledOnce.should.be.ok;
done();
2016-08-10 18:41:03 +00:00
});
2017-07-24 16:48:26 +00:00
});
2015-07-21 04:50:47 +00:00
it('should print a warning when the global flag is set',
2017-07-24 16:48:26 +00:00
function(done) {
Post.find({where: {content: {regexp: '^a/g'}}}, function(err, posts) {
console.warn.calledOnce.should.be.ok;
done();
2016-08-10 18:41:03 +00:00
});
2017-07-24 16:48:26 +00:00
});
2015-07-21 04:50:47 +00:00
it('should print a warning when the multiline flag is set',
2017-07-24 16:48:26 +00:00
function(done) {
Post.find({where: {content: {regexp: '^a/m'}}}, function(err, posts) {
console.warn.calledOnce.should.be.ok;
done();
2016-08-10 18:41:03 +00:00
});
2017-07-24 16:48:26 +00:00
});
2015-07-21 04:50:47 +00:00
});
2017-06-12 18:18:34 +00:00
it('filter with case sensitive regex string', function(done) {
Post.find({where: {content: {regexp: '^a'}}}, function(err, posts) {
should.not.exist(err);
should.exist(posts);
posts.length.should.equal(0);
done();
});
});
2015-07-21 04:50:47 +00:00
});
context('with regex literals', function() {
context('using no flags', function() {
it('should work', function(done) {
Post.find({where: {content: {regexp: /^A/}}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('AAA');
done();
});
});
});
context('using flags', function() {
beforeEach(function addSpy() {
sinon.stub(console, 'warn');
});
afterEach(function removeSpy() {
2015-07-21 04:50:47 +00:00
console.warn.restore();
});
2015-07-29 16:31:24 +00:00
it('should work', function(done) {
Post.find({where: {content: {regexp: /^a/i}}}, function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('AAA');
done();
});
});
2015-07-21 04:50:47 +00:00
it('should print a warning when the ignore flag is set',
2017-07-24 16:48:26 +00:00
function(done) {
Post.find({where: {content: {regexp: /^a/i}}}, function(err, posts) {
console.warn.calledOnce.should.be.ok;
done();
2016-08-10 18:41:03 +00:00
});
2017-07-24 16:48:26 +00:00
});
2015-07-21 04:50:47 +00:00
it('should print a warning when the global flag is set',
2017-07-24 16:48:26 +00:00
function(done) {
Post.find({where: {content: {regexp: /^a/g}}}, function(err, posts) {
console.warn.calledOnce.should.be.ok;
done();
2016-08-10 18:41:03 +00:00
});
2017-07-24 16:48:26 +00:00
});
2015-07-21 04:50:47 +00:00
it('should print a warning when the multiline flag is set',
2017-07-24 16:48:26 +00:00
function(done) {
Post.find({where: {content: {regexp: /^a/m}}}, function(err, posts) {
console.warn.calledOnce.should.be.ok;
done();
2016-08-10 18:41:03 +00:00
});
2017-07-24 16:48:26 +00:00
});
2015-07-21 04:50:47 +00:00
});
2017-06-12 18:18:34 +00:00
it('filter with case sensitive regex literal', function(done) {
Post.find({where: {content: {regexp: /^B/}}}, function(err, posts) {
should.not.exist(err);
should.exist(posts);
posts.length.should.equal(1);
posts[0].content.should.equal('BBB');
done();
});
});
2015-07-21 04:50:47 +00:00
});
context('with regex objects', function() {
beforeEach(function addSpy() {
sinon.stub(console, 'warn');
});
afterEach(function removeSpy() {
2015-07-21 04:50:47 +00:00
console.warn.restore();
});
context('using no flags', function() {
it('should work', function(done) {
Post.find({where: {content: {regexp: new RegExp(/^A/)}}},
2017-07-24 16:48:26 +00:00
function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('AAA');
done();
});
2015-07-21 04:50:47 +00:00
});
});
context('using flags', function() {
2015-07-29 16:31:24 +00:00
it('should work', function(done) {
Post.find({where: {content: {regexp: new RegExp(/^a/i)}}},
2017-07-24 16:48:26 +00:00
function(err, posts) {
should.not.exist(err);
posts.length.should.equal(1);
posts[0].content.should.equal('AAA');
done();
});
2015-07-29 16:31:24 +00:00
});
2015-07-21 04:50:47 +00:00
it('should print a warning when the ignore flag is set',
2017-07-24 16:48:26 +00:00
function(done) {
Post.find({where: {content: {regexp: new RegExp(/^a/i)}}},
2015-07-29 16:46:51 +00:00
function(err, posts) {
2016-08-10 18:41:03 +00:00
console.warn.calledOnce.should.be.ok;
done();
});
2017-07-24 16:48:26 +00:00
});
2015-07-21 04:50:47 +00:00
it('should print a warning when the global flag is set',
2017-07-24 16:48:26 +00:00
function(done) {
Post.find({where: {content: {regexp: new RegExp(/^a/g)}}},
2015-07-29 16:46:51 +00:00
function(err, posts) {
2016-08-10 18:41:03 +00:00
console.warn.calledOnce.should.be.ok;
done();
});
2017-07-24 16:48:26 +00:00
});
2015-07-21 04:50:47 +00:00
it('should print a warning when the multiline flag is set',
2017-07-24 16:48:26 +00:00
function(done) {
Post.find({where: {content: {regexp: new RegExp(/^a/m)}}},
2015-07-29 16:46:51 +00:00
function(err, posts) {
2016-08-10 18:41:03 +00:00
console.warn.calledOnce.should.be.ok;
done();
});
2017-07-24 16:48:26 +00:00
});
2017-06-12 18:18:34 +00:00
it('filter with case sensitive regex object', function(done) {
Post.find({where: {content: {regexp: new RegExp(/^a/)}}}, function(err, posts) {
should.not.exist(err);
should.exist(posts);
posts.length.should.equal(0);
done();
});
});
2015-07-21 04:50:47 +00:00
});
});
});
context('match operator', function() {
beforeEach(function deleteExistingTestFixtures(done) {
Post.destroyAll(done);
});
beforeEach(function createTestFixtures(done) {
Post.create([
{title: 'About Redis', content: 'Redis is a Database'},
{title: 'Usage', content: 'How To Use MySQL database Well'},
{title: 'About Mysql', content: 'Mysql is a database'},
], done);
});
after(function deleteTestFixtures(done) {
Post.destroyAll(done);
});
context('with one column and string', () => {
it('should work', function(done) {
Post.find({where: {content: {match: '+using MYSQL'}}}, (err, posts) => {
should.not.exist(err);
should.exist(posts);
posts.length.should.equal(2);
done();
});
});
it('should work in boolean mode with empty result expected', function(done) {
Post.find({where: {content: {matchbool: '+using MYSQL'}}}, (err, posts) => {
should.not.exist(err);
should.exist(posts);
posts.length.should.equal(0);
done();
});
});
it('should work in boolean mode with one result expected', function(done) {
Post.find({where: {content: {matchbool: '+use MYSQL'}}}, (err, posts) => {
should.not.exist(err);
should.exist(posts);
posts.length.should.equal(1);
done();
});
});
it('should work with matchqe operator with expected result in first and second pass', function(done) {
Post.find({where: {content: {match: 'redis'}}}, (err, posts) => {
should.not.exist(err);
should.exist(posts);
posts.length.should.equal(1);
Post.find({where: {content: {matchqe: 'redis'}}}, (err, expandedPosts) => {
should.not.exist(err);
should.exist(expandedPosts);
expandedPosts.length.should.equal(3);
done();
});
});
});
it('should work with matchnlqe operator with expected result in first and second pass', function(done) {
Post.find({where: {content: {match: 'redis'}}}, (err, posts) => {
should.not.exist(err);
should.exist(posts);
posts.length.should.equal(1);
Post.find({where: {content: {matchnlqe: 'redis'}}}, (err, expandedPosts) => {
should.not.exist(err);
should.exist(expandedPosts);
expandedPosts.length.should.equal(3);
done();
});
});
});
});
context('with multiple column and one string', () => {
it('should work', function(done) {
const against = 'using MYSQL';
Post.find(
{
where: {
or: [
{
content: {
match: against,
},
},
{
title: {
match: against,
},
},
],
},
},
(err, posts) => {
should.not.exist(err);
should.exist(posts);
posts.length.should.equal(2);
done();
},
);
});
});
});
function deleteAllModelInstances() {
const models = [
Post, PostWithStringId, PostWithUniqueTitle, PostWithNumId, Student,
];
return Promise.all(models.map(m => m.destroyAll()));
}
after(function() {
return deleteAllModelInstances();
2014-05-30 22:15:27 +00:00
});
});