Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

groupOrder spec #80

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions test/unit/models/food.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

describe('models/food', () => {

describe.skip('models/food', () => {
before(function () {
});

Expand Down
94 changes: 61 additions & 33 deletions test/unit/models/groupOrder.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const modelName = 'Order';
const SPEC_MODEL_NAME = 'GroupOrder';
const fakeData = {
create: {
total: faker.random.number(),
Expand Down Expand Up @@ -36,7 +36,7 @@ const fakeData = {
},
};

describe(`models/${modelName}`, () => {
describe(`models/${SPEC_MODEL_NAME}`, () => {
before(() => {
});

Expand All @@ -45,13 +45,13 @@ 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 createdModelData = await models[SPEC_MODEL_NAME].create(fakeData.create);

Object.keys(fakeData.create).forEach(e => {
expect(createdModel[e]).to.equal(fakeData.create[e]);
expect(createdModelData[e]).to.equal(fakeData.create[e]);
});
});
});
Expand All @@ -60,30 +60,40 @@ 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.total = fakeData.updateNewData.total;
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]);
});
});

it('update a model with update method', async () => {
// 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]);
});
});
});
Expand All @@ -92,69 +102,87 @@ describe(`models/${modelName}`, () => {
let modelDateForFind = null;
before(async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(好奇)
before 為什麼要拆開呢?
我看每一筆資料都不一樣
因為都在測試同樣一個 model
可以整理在一個 describe
在 before 統一新增
是有什麼特別的考量麻?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我不是很確定對不對,
像是底下 describe('Find all model data') 它的 before 裡 新增資料是用 .bulkCreate()增加一個陣列型式的多筆資料;
create() 是增加一筆物件資料

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

新增多筆或是新增一筆部會影響到放在同一個 before 裡面做歐
我的問題是同樣一個 model 的測試
before 為什麼要拆開呢?
因為如果要分開的話
應該也有 beforeEach 的 function 可以使用
而且這些 Data 彼此之間也沒有互相干擾

這樣實作沒有問題拉
只是好奇是不是有什麼特別的考量才選擇這樣的方式

// 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.equal(fakeData.findOne[e]);
});
});
});

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: {
total: fakeData.keyword.total
}
});

expect(findAllModelData.length).to.greaterThan(fakeData.findAll.length - 1);
for (const index in findAllModelData) {
expect(findAllModelData[index].total).to.equal(fakeData.findAll[0].total);
}
});
});

describe('Destroy model data', () => {
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].findOne({
where: {
id: modelDataForDestroy.id
}
});

expect(deleteModelData).to.equal(1);
expect(findDeleteModelData).to.equal(null);
});
});
});
2 changes: 1 addition & 1 deletion test/unit/models/passport.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const fakeData = {
},
};

describe(`models/${modelName}`, () => {
describe.skip(`models/${modelName}`, () => {
before(() => {
});

Expand Down
2 changes: 1 addition & 1 deletion test/unit/models/restaurant.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const fakeData = {
},
};

describe(`models/${modelName}`, () => {
describe.skip(`models/${modelName}`, () => {
before(() => {
});

Expand Down
2 changes: 1 addition & 1 deletion test/unit/models/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const fakeData = {
},
};

describe(`models/${modelName}`, () => {
describe.skip(`models/${modelName}`, () => {
before(() => {
});

Expand Down
2 changes: 1 addition & 1 deletion test/unit/models/userOrder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const fakeData = {
},
};

describe(`models/${modelName}`, () => {
describe.skip(`models/${modelName}`, () => {
before(() => {
});

Expand Down