Skip to content

Commit

Permalink
Merge pull request #1721 from strongloop/fix/failing-tests
Browse files Browse the repository at this point in the history
test: define models properly
  • Loading branch information
Biniam Admikew committed Apr 30, 2019
2 parents 0bba44e + eff6458 commit 6677c58
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 32 deletions.
44 changes: 33 additions & 11 deletions test/basic-querying.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1056,37 +1056,59 @@ describe('basic-querying', function() {
});

describe('updateAll', function() {
it('coerces primitive datatypes on update', async () => {
const numAndDateModel = db.define('numAndDateModel', {
let numAndDateModel, numAndDateArrayModel;

before(function() {
numAndDateModel = db.define('numAndDateModel', {
dateProp: Date,
dateArray: [Date],
numProp: Number,
});
numAndDateArrayModel = db.define('numAndDateArrayModel', {
dateArray: [Date],
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 createData = {
dateProp: createDate,
dateArray: [createDate, createDate],
numProp: '1',
numArray: ['1', '2'],
};
const updateDate = new Date('2019-04-15T12:00:00').toISOString();
const updateData = {
dateProp: updateDate,
dateArray: [updateDate, updateDate],
numProp: '3',
numArray: ['3', '4'],
};
const created = await numAndDateModel.create(createData);
const updated = await numAndDateModel.updateAll({id: created.id}, updateData);
const found = await numAndDateModel.findById(created.id);
found.dateProp.should.deepEqual(new Date(updateDate));
found.dateArray[0].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[1].should.equal(4);
});

// 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[1].should.deepEqual(new Date(updateDate));
found.numArray[0].should.equal(3);
found.numArray[1].should.equal(4);
});
});

context('regexp operator', function() {
Expand Down
43 changes: 22 additions & 21 deletions test/datatype.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/* global getSchema:false */
const should = require('./init.js');

let db, Model;
let db, Model, modelWithDecimalArray, dateArrayModel, numArrayModel;

class NestedClass {
constructor(roleName) {
Expand All @@ -32,7 +32,24 @@ describe('datatypes', function() {
nestedClass: NestedClass,
};
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() {
Expand All @@ -53,25 +70,12 @@ describe('datatypes', function() {
Account.definition.properties.item.type.should.not.equal(String);
});
it('should resolve array prop with connector specific metadata', function() {
const model = db.define('test', {
randomReview: {
type: [String],
mongodb: {
dataType: 'Decimal128',
},
},
});
model.definition.properties.randomReview.type.should.deepEqual(Array(String));
model.definition.properties.randomReview.mongodb.should.deepEqual({dataType: 'Decimal128'});
const props = modelWithDecimalArray.definition.properties;
props.randomReview.type.should.deepEqual(Array(String));
props.randomReview.mongodb.should.deepEqual({dataType: 'Decimal128'});
});

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 created = await dateArrayModel.create({
bunchOfDates: [dateVal,
Expand All @@ -88,9 +92,6 @@ describe('datatypes', function() {
});

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 created = await numArrayModel.create({
bunchOfNums: ['1',
Expand Down

0 comments on commit 6677c58

Please sign in to comment.