diff --git a/models/restaurant.js b/models/restaurant.js index 69b4e7d..600b402 100644 --- a/models/restaurant.js +++ b/models/restaurant.js @@ -7,20 +7,20 @@ module.exports = (sequelize, DataTypes) => { imageUrl: DataTypes.STRING, address: DataTypes.STRING, orderInUnit: { - type: DataTypes.INTEGER, + type: DataTypes.STRING, allowNull: false, - defaultValue: 0, + defaultValue: '個/元', }, orderRequirement: { type: DataTypes.INTEGER, allowNull: false, }, startTime: { - type: DataTypes.STRING, + type: DataTypes.DATE, allowNull: false, }, endTime: { - type: DataTypes.INTEGER, + type: DataTypes.DATE, allowNull: false, }, phone: { diff --git a/test/unit/models/food.test.js b/test/unit/models/food.test.js index 9e30b99..179fc2e 100644 --- a/test/unit/models/food.test.js +++ b/test/unit/models/food.test.js @@ -1,6 +1,6 @@ 'use strict'; -describe('models/food', () => { +describe.skip('models/food', () => { before(function () { }); diff --git a/test/unit/models/groupOrder.spec.js b/test/unit/models/groupOrder.spec.js index 007c7ac..d6b84f9 100644 --- a/test/unit/models/groupOrder.spec.js +++ b/test/unit/models/groupOrder.spec.js @@ -36,7 +36,7 @@ const fakeData = { }, }; -describe(`models/${modelName}`, () => { +describe.skip(`models/${modelName}`, () => { before(() => { }); diff --git a/test/unit/models/passport.spec.js b/test/unit/models/passport.spec.js index cb092a4..88ec847 100644 --- a/test/unit/models/passport.spec.js +++ b/test/unit/models/passport.spec.js @@ -45,7 +45,7 @@ const fakeData = { }, }; -describe(`models/${modelName}`, () => { +describe.skip(`models/${modelName}`, () => { before(() => { }); diff --git a/test/unit/models/restaurant.spec.js b/test/unit/models/restaurant.spec.js index d747765..4b63c6b 100644 --- a/test/unit/models/restaurant.spec.js +++ b/test/unit/models/restaurant.spec.js @@ -1,56 +1,136 @@ 'use strict'; -const modelName = 'Restaurant'; +const SPEC_MODEL_NAME = 'Restaurant'; const fakeData = { create: { name: faker.name.findName(), address: faker.address.streetAddress(), - orderInUnit: 1, - orderRequirement: 1, - phone: '1213', - startTime: 12, - endTime: 11, + imageUrl: faker.image.image(), + orderInUnit: faker.lorem.word(), + orderRequirement: faker.random.number(), + phone: faker.phone.phoneNumberFormat(), + startTime: faker.date.recent(), + endTime: faker.date.future(), + remark: faker.lorem.sentences(), + }, + create2: { + name: faker.name.findName(), + address: faker.address.streetAddress(), + imageUrl: faker.image.image(), + orderInUnit: faker.lorem.word(), + orderRequirement: faker.random.number(), + phone: faker.phone.phoneNumberFormat(), + startTime: faker.date.recent(), + endTime: faker.date.future(), + remark: faker.lorem.sentences(), + }, + create3: { + name: faker.name.findName(), + address: faker.address.streetAddress(), + imageUrl: faker.image.image(), + orderInUnit: faker.lorem.word(), + orderRequirement: faker.random.number(), + phone: faker.phone.phoneNumberFormat(), + startTime: faker.date.recent(), + endTime: faker.date.future(), + remark: faker.lorem.sentences(), }, update: { name: faker.name.findName(), address: faker.address.streetAddress(), + imageUrl: faker.image.image(), + orderInUnit: faker.lorem.word(), + orderRequirement: faker.random.number(), + phone: faker.phone.phoneNumberFormat(), + startTime: faker.date.recent(), + endTime: faker.date.future(), + remark: faker.lorem.sentences(), }, updateNewData: { name: faker.name.findName(), address: faker.address.streetAddress(), + imageUrl: faker.image.image(), + orderInUnit: faker.lorem.word(), + orderRequirement: faker.random.number(), + phone: faker.phone.phoneNumberFormat(), + startTime: faker.date.recent(), + endTime: faker.date.future(), + remark: faker.lorem.sentences(), }, updateNewData2: { name: faker.name.findName(), address: faker.address.streetAddress(), + imageUrl: faker.image.image(), + orderInUnit: faker.lorem.word(), + orderRequirement: faker.random.number(), + phone: faker.phone.phoneNumberFormat(), + startTime: faker.date.recent(), + endTime: faker.date.future(), + remark: faker.lorem.sentences(), }, findOne: { name: 'keyword', address: faker.address.streetAddress(), + imageUrl: faker.image.image(), + orderInUnit: faker.lorem.word(), + orderRequirement: faker.random.number(), + phone: faker.phone.phoneNumberFormat(), + startTime: faker.date.recent(), + endTime: faker.date.future(), + remark: faker.lorem.sentences(), }, findAll: [ { name: 'keyword', address: faker.address.streetAddress(), + imageUrl: faker.image.image(), + orderInUnit: faker.lorem.word(), + orderRequirement: faker.random.number(), + phone: faker.phone.phoneNumberFormat(), + startTime: faker.date.recent(), + endTime: faker.date.future(), + remark: faker.lorem.sentences(), }, { name: 'keyword', address: faker.address.streetAddress(), + imageUrl: faker.image.image(), + orderInUnit: faker.lorem.word(), + orderRequirement: faker.random.number(), + phone: faker.phone.phoneNumberFormat(), + startTime: faker.date.recent(), + endTime: faker.date.future(), + remark: faker.lorem.sentences(), }, { name: 'keyword', address: faker.address.streetAddress(), + imageUrl: faker.image.image(), + orderInUnit: faker.lorem.word(), + orderRequirement: faker.random.number(), + phone: faker.phone.phoneNumberFormat(), + startTime: faker.date.recent(), + endTime: faker.date.future(), + remark: faker.lorem.sentences(), }, ], destroy: { name: faker.name.findName(), address: faker.address.streetAddress(), + imageUrl: faker.image.image(), + orderInUnit: faker.lorem.word(), + orderRequirement: faker.random.number(), + phone: faker.phone.phoneNumberFormat(), + startTime: faker.date.recent(), + endTime: faker.date.future(), + remark: faker.lorem.sentences(), }, keyword: { name: 'keyword', }, }; -describe(`models/${modelName}`, () => { +describe(`models/${SPEC_MODEL_NAME}`, () => { before(() => { }); @@ -59,10 +139,10 @@ describe(`models/${modelName}`, () => { describe('Create model data', () => { it('creates a data', async () => { - // TODO: + // TODO: // 1. model.create(fakeData.create)... // 2. use data from `fakeData.create` - let createdModel = null; + const createdModel = await models[SPEC_MODEL_NAME].create(fakeData.create); Object.keys(fakeData.create).forEach(e => { expect(createdModel[e]).to.equal(fakeData.create[e]); @@ -74,18 +154,32 @@ describe(`models/${modelName}`, () => { let modelDataForUpdate = null; before(async () => { // create a model data before run test - modelDataForUpdate = await models[modelName].create(fakeData.update); + modelDataForUpdate = await models[SPEC_MODEL_NAME].create(fakeData.update); }); it('update a model with find and save', async () => { // TODO: // 1. use model.find(modelId === updatedModel.id) then model.save(); // 2. use data from `fakeData.updateNewData` - // 3. use `findAndUpdatedModel` as target, `modelDataForUpdate.id` as condition. - let findAndUpdatedModel = null; + // 3. use `findAndUpdatedModelData` as target, `modelDataForUpdate.id` as condition. + let findAndUpdatedModelData = await models[SPEC_MODEL_NAME].find({ + where: { + id: modelDataForUpdate.id, + } + }); + findAndUpdatedModelData.name = fakeData.updateNewData.name; + findAndUpdatedModelData.address = fakeData.updateNewData.address; + findAndUpdatedModelData.imageUrl = fakeData.updateNewData.imageUrl; + findAndUpdatedModelData.orderInUnit = fakeData.updateNewData.orderInUnit; + findAndUpdatedModelData.orderRequirement = fakeData.updateNewData.orderRequirement; + findAndUpdatedModelData.phone = fakeData.updateNewData.phone; + findAndUpdatedModelData.startTime = fakeData.updateNewData.startTime; + findAndUpdatedModelData.endTime = fakeData.updateNewData.endTime; + findAndUpdatedModelData.remark = fakeData.updateNewData.remark; + await findAndUpdatedModelData.save(); Object.keys(fakeData.updateNewData).forEach(e => { - expect(findAndUpdatedModel[e]).to.equal(fakeData.updateNewData[e]); + expect(findAndUpdatedModelData[e]).to.equal(fakeData.updateNewData[e]); }); }); @@ -93,11 +187,15 @@ describe(`models/${modelName}`, () => { // TODO: // 1. use model.update()... // 2. use data from `fakeData.updateNewData2` - // 3. use `updateModel` as target, `modelDataForUpdate.id` as condition. - let updateModel = null; + // 3. use `updateModelData` as target, `modelDataForUpdate.id` as condition. + const updateModelData = await modelDataForUpdate.update(fakeData.updateNewData2, { + where: { + id: modelDataForUpdate.id, + } + }); Object.keys(fakeData.updateNewData2).forEach(e => { - expect(updateModel[e]).to.equal(fakeData.updateNewData2[e]); + expect(updateModelData[e]).to.equal(fakeData.updateNewData2[e]); }); }); }); @@ -106,18 +204,22 @@ describe(`models/${modelName}`, () => { let modelDateForFind = null; before(async () => { // create a model before run test - modelDateForFind = await models[modelName].create(fakeData.findOne); + modelDateForFind = await models[SPEC_MODEL_NAME].create(fakeData.findOne); }); it('find a model with findOne', async () => { // TODO: // 1. use model.findOne... // 2. use data from `fakeData.findOne` - // 3. use `findModel` as target, `modelDateForFind.id` as option. - let findModel = null; + // 3. use `findModelData` as target, `modelDateForFind.id` as option. + const findModelData = await models[SPEC_MODEL_NAME].findOne({ + where: { + id: modelDateForFind.id, + } + }); Object.keys(fakeData.findOne).forEach(e => { - expect(findModel[e]).to.equal(fakeData.findOne[e]); + expect(findModelData[e]).to.eql(fakeData.findOne[e]); }); }); }); @@ -125,31 +227,36 @@ describe(`models/${modelName}`, () => { describe('Find all model data', () => { before(async () => { // create 3 models and push into models array. - // await models[modelName].create(data1); - await models[modelName].bulkCreate(fakeData.findAll); + // await models[SPEC_MODEL_NAME].create(data1); + await models[SPEC_MODEL_NAME].bulkCreate(fakeData.findAll); }); it('find all model', async () => { // TODO: // 1. use model.findAll... // 2. use data from `fakeData.findAll` - // 3. use `findAllModel` as target. - let findAllModel = null; + // 3. use `findAllModelData` as target. + const findAllModelData = await models[SPEC_MODEL_NAME].findAll(); - expect(findAllModel.length).to.greaterThan(fakeData.findAll.length - 1); + expect(findAllModelData.length).to.greaterThan(fakeData.findAll.length - 1); }); it('find all with where', async () => { // TODO: // 1. use model.findAll... // 2. use data from `fakeData.findAll` - // 3. use `findAllModel` as target, and use `fakeData.keyword` as option. - let findAllModel = null; - - expect(findAllModel.length).to.greaterThan(fakeData.findAll.length - 1); - Object.keys(fakeData.findAll).forEach(e => { - expect(findAllModel[e]).to.equal(fakeData.findAll[e]); + // 3. use `findAllModelData` as target, and use `fakeData.keyword` as option. + const findAllModelData = await models[SPEC_MODEL_NAME].findAll({ + where: { + name: fakeData.keyword.name, + } }); + // console.log('findAllModelData=>', findAllModelData); + + expect(findAllModelData.length).to.greaterThan(fakeData.findAll.length - 1); + for (const index in findAllModelData) { + expect(findAllModelData[index].name).to.equal(fakeData.findAll[0].name); + } }); }); @@ -157,18 +264,28 @@ describe(`models/${modelName}`, () => { let modelDataForDestroy = null; before(async () => { // create data before run test - modelDataForDestroy = await models[modelName].create(fakeData.destroy); + modelDataForDestroy = await models[SPEC_MODEL_NAME].create(fakeData.destroy); }); it('delete a model data with where', async () => { // TODO: // 1. use model.destroy... // 2. use data from `fakeData.findAll` - // 3. use `findAllModel` as target, and use `modelDataForDestroy.id` as option. - let deleteModel = null; - let findDeleteModel = null; - expect(deleteModel).to.equal(1); - expect(findDeleteModel).to.equal(null); + // 3. use `findAllModelData` as target, and use `modelDataForDestroy.id` as option. + const deleteModelData = await models[SPEC_MODEL_NAME].destroy({ + where: { + id: modelDataForDestroy.id, + } + }); + + const findDeleteModelData = await models[SPEC_MODEL_NAME].find({ + where: { + id: modelDataForDestroy.id, + } + }); + + expect(deleteModelData).to.equal(1); + expect(findDeleteModelData).to.equal(null); }); }); @@ -177,7 +294,7 @@ describe(`models/${modelName}`, () => { // TODO: // 1. create a restaurant with 3 foods. // 2. use model.create with include. - // 3. use `fakeData.create` as restaurant data, use `data` as include foods. + // 3. use `fakeData.create2` as restaurant data, use `data` as include foods. const data = [{ name: 'big_macx1', price: 150, @@ -188,18 +305,18 @@ describe(`models/${modelName}`, () => { name: 'big_macx3', price: 400, }]; - let restaurant = await models.Restaurant.create({ - ...fakeData.create, + + const restaurant = await models[SPEC_MODEL_NAME].create({ + ...fakeData.create2, Food: data, }, { include: [{ model: models.Food, - }], + }] }); - console.log('restaurant=>', restaurant.toJSON()); - expect(restaurant.name).to.be.equal(fakeData.create.name); - expect(restaurant.address).to.be.equal(fakeData.create.address); + expect(restaurant.name).to.be.equal(fakeData.create2.name); + expect(restaurant.address).to.be.equal(fakeData.create2.address); expect(restaurant.Food.length).to.equal(data.length); }); @@ -207,20 +324,34 @@ describe(`models/${modelName}`, () => { // TODO: // 1. create a restaurant and use set() method to set 1 associated model data. // 2. use model.findOne() with include to get model data with associated data. - // 3. use data as food data, use `fakeData.create` as restaurant data. + // 3. use data as food data, use `fakeData.create3` as restaurant data. const data = { name: 'big_mac', price: 150, }; - let createdRest = null; - let food = null; - let findRestWithFood = null; - - expect(findRestWithFood.name).to.be.equal(fakeData.create.name); - expect(findRestWithFood.address).to.be.equal(fakeData.create.address); - expect(findRestWithFood.Foods.length).to.equal(1); - expect(findRestWithFood.UserOrders[0].amount).to.equal(data.name); - expect(findRestWithFood.UserOrders[0].remark).to.equal(data.price); + let createdRestaurant = await models[SPEC_MODEL_NAME].create({ + ...fakeData.create3, + Food: data, + }, { + include: [{ + model: models.Food, + }] + }); + const food = await models.Food.create(data); + await createdRestaurant.setFood(food); + + const findRestaurantWithFood = await models[SPEC_MODEL_NAME].findOne({ + where: { + name: fakeData.create3.name, + }, + include: [models.Food], + }); + + expect(findRestaurantWithFood.name).to.be.equal(fakeData.create3.name); + expect(findRestaurantWithFood.address).to.be.equal(fakeData.create3.address); + expect(findRestaurantWithFood.Food.length).to.equal(1); + expect(findRestaurantWithFood.Food[0].name).to.equal(data.name); + expect(findRestaurantWithFood.Food[0].price).to.equal(data.price); }); }); }); diff --git a/test/unit/models/user.spec.js b/test/unit/models/user.spec.js index 45b320e..fd22f26 100644 --- a/test/unit/models/user.spec.js +++ b/test/unit/models/user.spec.js @@ -53,7 +53,7 @@ const fakeData = { }, }; -describe(`models/${modelName}`, () => { +describe.skip(`models/${modelName}`, () => { before(() => { }); diff --git a/test/unit/models/userOrder.spec.js b/test/unit/models/userOrder.spec.js index dba0aaf..a7daa6f 100644 --- a/test/unit/models/userOrder.spec.js +++ b/test/unit/models/userOrder.spec.js @@ -45,7 +45,7 @@ const fakeData = { }, }; -describe(`models/${modelName}`, () => { +describe.skip(`models/${modelName}`, () => { before(() => { });