Merge pull request #1721 from strongloop/fix/failing-tests

test: define models properly
This commit is contained in:
Biniam Admikew 2019-04-30 11:50:10 -04:00 committed by GitHub
commit 6677c58cff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 32 deletions

View File

@ -1056,34 +1056,56 @@ describe('basic-querying', function() {
}); });
describe('updateAll', function() { describe('updateAll', function() {
it('coerces primitive datatypes on update', async () => { let numAndDateModel, numAndDateArrayModel;
const numAndDateModel = db.define('numAndDateModel', {
before(function() {
numAndDateModel = db.define('numAndDateModel', {
dateProp: Date, dateProp: Date,
dateArray: [Date],
numProp: Number, numProp: Number,
});
numAndDateArrayModel = db.define('numAndDateArrayModel', {
dateArray: [Date],
numArray: [Number], numArray: [Number],
}); });
return db.automigrate(['numAndDateModel', 'numAndDateArrayModel']);
});
it('coerces primitive datatypes on update', async () => {
const createDate = new Date('2019-02-21T12:00:00').toISOString(); const createDate = new Date('2019-02-21T12:00:00').toISOString();
const createData = { const createData = {
dateProp: createDate, dateProp: createDate,
dateArray: [createDate, createDate],
numProp: '1', numProp: '1',
numArray: ['1', '2'],
}; };
const updateDate = new Date('2019-04-15T12:00:00').toISOString(); const updateDate = new Date('2019-04-15T12:00:00').toISOString();
const updateData = { const updateData = {
dateProp: updateDate, dateProp: updateDate,
dateArray: [updateDate, updateDate],
numProp: '3', numProp: '3',
numArray: ['3', '4'],
}; };
const created = await numAndDateModel.create(createData); const created = await numAndDateModel.create(createData);
const updated = await numAndDateModel.updateAll({id: created.id}, updateData); const updated = await numAndDateModel.updateAll({id: created.id}, updateData);
const found = await numAndDateModel.findById(created.id); const found = await numAndDateModel.findById(created.id);
found.dateProp.should.deepEqual(new Date(updateDate)); found.dateProp.should.deepEqual(new Date(updateDate));
found.numProp.should.equal(3);
});
// PostgreSQL connector does not support arrays at the moment
bdd.itIf(connectorCapabilities.supportsArrays !== false,
'coerces primitive array datatypes on update', async () => {
const createDate = new Date('2019-02-21T12:00:00').toISOString();
const createData = {
dateArray: [createDate, createDate],
numArray: ['1', '2'],
};
const updateDate = new Date('2019-04-15T12:00:00').toISOString();
const updateData = {
dateArray: [updateDate, updateDate],
numArray: ['3', '4'],
};
const created = await numAndDateArrayModel.create(createData);
const updated = await numAndDateArrayModel.updateAll({id: created.id}, updateData);
const found = await numAndDateArrayModel.findById(created.id);
found.dateArray[0].should.deepEqual(new Date(updateDate)); found.dateArray[0].should.deepEqual(new Date(updateDate));
found.dateArray[1].should.deepEqual(new Date(updateDate)); found.dateArray[1].should.deepEqual(new Date(updateDate));
found.numProp.should.equal(3);
found.numArray[0].should.equal(3); found.numArray[0].should.equal(3);
found.numArray[1].should.equal(4); found.numArray[1].should.equal(4);
}); });

View File

@ -9,7 +9,7 @@
/* global getSchema:false */ /* global getSchema:false */
const should = require('./init.js'); const should = require('./init.js');
let db, Model; let db, Model, modelWithDecimalArray, dateArrayModel, numArrayModel;
class NestedClass { class NestedClass {
constructor(roleName) { constructor(roleName) {
@ -32,7 +32,24 @@ describe('datatypes', function() {
nestedClass: NestedClass, nestedClass: NestedClass,
}; };
Model = db.define('Model', modelTableSchema); Model = db.define('Model', modelTableSchema);
db.automigrate(['Model'], done); modelWithDecimalArray = db.define('modelWithDecimalArray', {
randomReview: {
type: [String],
mongodb: {
dataType: 'Decimal128',
},
},
});
dateArrayModel = db.define('dateArrayModel', {
bunchOfDates: [Date],
bunchOfOtherDates: {
type: [Date],
},
});
numArrayModel = db.define('numArrayModel', {
bunchOfNums: [Number],
});
db.automigrate(['Model', 'modelWithDecimalArray', 'dateArrayModel', 'numArrayModel'], done);
}); });
it('should resolve top-level "type" property correctly', function() { it('should resolve top-level "type" property correctly', function() {
@ -53,25 +70,12 @@ describe('datatypes', function() {
Account.definition.properties.item.type.should.not.equal(String); Account.definition.properties.item.type.should.not.equal(String);
}); });
it('should resolve array prop with connector specific metadata', function() { it('should resolve array prop with connector specific metadata', function() {
const model = db.define('test', { const props = modelWithDecimalArray.definition.properties;
randomReview: { props.randomReview.type.should.deepEqual(Array(String));
type: [String], props.randomReview.mongodb.should.deepEqual({dataType: 'Decimal128'});
mongodb: {
dataType: 'Decimal128',
},
},
});
model.definition.properties.randomReview.type.should.deepEqual(Array(String));
model.definition.properties.randomReview.mongodb.should.deepEqual({dataType: 'Decimal128'});
}); });
it('should coerce array of dates from string', async () => { it('should coerce array of dates from string', async () => {
const dateArrayModel = db.define('dateArrayModel', {
bunchOfDates: [Date],
bunchOfOtherDates: {
type: [Date],
},
});
const dateVal = new Date('2019-02-21T12:00:00').toISOString(); const dateVal = new Date('2019-02-21T12:00:00').toISOString();
const created = await dateArrayModel.create({ const created = await dateArrayModel.create({
bunchOfDates: [dateVal, bunchOfDates: [dateVal,
@ -88,9 +92,6 @@ describe('datatypes', function() {
}); });
it('should coerce array of numbers from string', async () => { it('should coerce array of numbers from string', async () => {
const numArrayModel = db.define('numArrayModel', {
bunchOfNums: [Number],
});
const dateVal = new Date('2019-02-21T12:00:00').toISOString(); const dateVal = new Date('2019-02-21T12:00:00').toISOString();
const created = await numArrayModel.create({ const created = await numArrayModel.create({
bunchOfNums: ['1', bunchOfNums: ['1',