From eff6458a2a02bc5da8b58fd182535a2131297137 Mon Sep 17 00:00:00 2001 From: biniam Date: Wed, 17 Apr 2019 10:42:28 -0400 Subject: [PATCH] test: define models in before hooks Re-work tests from #1702 based on the failures observed in #1719 and cherry-pick those commits back to master using ES6 syntax. --- test/basic-querying.test.js | 44 +++++++++++++++++++++++++++---------- test/datatype.test.js | 43 ++++++++++++++++++------------------ 2 files changed, 55 insertions(+), 32 deletions(-) diff --git a/test/basic-querying.test.js b/test/basic-querying.test.js index 0fb605d2..a6e6b529 100644 --- a/test/basic-querying.test.js +++ b/test/basic-querying.test.js @@ -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() { diff --git a/test/datatype.test.js b/test/datatype.test.js index 45def863..2aae02b4 100644 --- a/test/datatype.test.js +++ b/test/datatype.test.js @@ -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) { @@ -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() { @@ -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, @@ -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',