-
Notifications
You must be signed in to change notification settings - Fork 0
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
passport spec #77
base: develop
Are you sure you want to change the base?
passport spec #77
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -5,47 +5,56 @@ const fakeData = { | |||
create: { | ||||
token: faker.name.findName(), | ||||
workspaceName: faker.name.findName(), | ||||
passwordHash: faker.internet.password(), | ||||
}, | ||||
update: { | ||||
token: faker.name.findName(), | ||||
workspaceName: faker.name.findName(), | ||||
passwordHash: faker.internet.password(), | ||||
}, | ||||
updateNewData: { | ||||
token: faker.name.findName(), | ||||
workspaceName: faker.name.findName(), | ||||
passwordHash: faker.internet.password(), | ||||
}, | ||||
updateNewData2: { | ||||
token: faker.name.findName(), | ||||
workspaceName: faker.name.findName(), | ||||
passwordHash: faker.internet.password(), | ||||
}, | ||||
findOne: { | ||||
token: 'faker.name.findName()', | ||||
workspaceName: faker.name.findName(), | ||||
passwordHash: faker.internet.password(), | ||||
}, | ||||
findAll: [ | ||||
{ | ||||
token: 'faker.name.findName()', | ||||
workspaceName: faker.name.findName(), | ||||
passwordHash: faker.internet.password(), | ||||
}, | ||||
{ | ||||
token: 'faker.name.findName()', | ||||
workspaceName: faker.name.findName(), | ||||
passwordHash: faker.internet.password(), | ||||
}, | ||||
{ | ||||
token: 'faker.name.findName()', | ||||
workspaceName: faker.name.findName(), | ||||
passwordHash: faker.internet.password(), | ||||
}, | ||||
], | ||||
destroy: { | ||||
token: faker.name.findName(), | ||||
workspaceName: faker.name.findName(), | ||||
passwordHash: faker.internet.password(), | ||||
}, | ||||
keyword: { | ||||
token: 'faker.name.findName()', | ||||
}, | ||||
}; | ||||
|
||||
describe(`models/${modelName}`, () => { | ||||
describe.only(`models/${modelName}`, () => { | ||||
before(() => { | ||||
}); | ||||
|
||||
|
@@ -54,10 +63,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; | ||||
let createdModel = await models[modelName].create(fakeData.create); | ||||
|
||||
Object.keys(fakeData.create).forEach(e => { | ||||
expect(createdModel[e]).to.equal(fakeData.create[e]); | ||||
|
@@ -77,23 +86,55 @@ describe(`models/${modelName}`, () => { | |||
// 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; | ||||
|
||||
Object.keys(fakeData.updateNewData).forEach(e => { | ||||
expect(findAndUpdatedModel[e]).to.equal(fakeData.updateNewData[e]); | ||||
}); | ||||
try { | ||||
let findAndUpdatedModel = await models[modelName].findOne({ | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 前面是用 他回傳的應該還是個物件 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 這裡的變數是我命名的QQ 因為這個單元測試有指定操作方法,這裏想表達的含義是該 model data 是需要使用 find 去做 update,所以我用這個 不過命名的確有改空間 |
||||
where: { | ||||
id: modelDataForUpdate.id, | ||||
}, | ||||
}); | ||||
|
||||
if (findAndUpdatedModel) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 這邊就不用包 if 了,因為這是 unit test,所以 findAndUpdatedModel 一定不能是 null 包 if 反而如果有問題不能發現 |
||||
findAndUpdatedModel.token = fakeData.updateNewData.token; | ||||
findAndUpdatedModel.workspaceName = | ||||
fakeData.updateNewData.workspaceName; | ||||
findAndUpdatedModel.passwordHash = | ||||
fakeData.updateNewData.passwordHash; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 這是測試建立 data 的麻? L96 - L103 我應該會拉出去寫一個function
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. QQ 這裡是我建好的 init data 缺了一個 notNull 欄位的資料,所以我請 @chenpion 直接補上 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 了解! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 正常是不會,但在測試 update() / destroy() 時會需要在 before 中建立,因為必須要先有資料才能做以上兩個操作 以這裡來說, @chenpion 這裡的變動是寫在 spec 最前端,所以應該比較沒有疑義? 參考:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 了解 |
||||
|
||||
await findAndUpdatedModel.save(); | ||||
|
||||
Object.keys(fakeData.updateNewData).forEach(e => { | ||||
expect(findAndUpdatedModel[e]).to.equal(fakeData.updateNewData[e]); | ||||
}); | ||||
} | ||||
} catch (error) { | ||||
console.log('find and save => ', error); | ||||
} | ||||
}); | ||||
|
||||
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; | ||||
|
||||
Object.keys(fakeData.updateNewData2).forEach(e => { | ||||
expect(updateModel[e]).to.equal(fakeData.updateNewData2[e]); | ||||
}); | ||||
try { | ||||
await models[modelName].update(fakeData.updateNewData2, { | ||||
where: { | ||||
id: modelDataForUpdate.id, | ||||
}, | ||||
}); | ||||
|
||||
let updateModel = await models[modelName].findOne({ | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 這一個 modelName 是一個常數 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 這裏跟 user.spec 是一樣的原因, 建議 @chenpion 改為 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 了解 如果變數可以直接體現出這個 Model Name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 因為我等於是寫了第一份 spec 之後,改這個 modelName 再複製出來,就可以讓單元測試的大部分程式碼共用於全部的 model,我再針對某些 model 彼此特殊的關係開細部測試規格給大家即可 而你說的直接從變數名稱上看到是哪個 model 這件事,因為以 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 辨識度上應該還好 |
||||
where: { | ||||
id: modelDataForUpdate.id, | ||||
}, | ||||
}); | ||||
|
||||
Object.keys(fakeData.updateNewData2).forEach(e => { | ||||
expect(updateModel[e]).to.equal(fakeData.updateNewData2[e]); | ||||
}); | ||||
} catch (error) { | ||||
console.log('update => ', error); | ||||
} | ||||
}); | ||||
}); | ||||
|
||||
|
@@ -109,7 +150,11 @@ describe(`models/${modelName}`, () => { | |||
// 1. use model.findOne... | ||||
// 2. use data from `fakeData.findOne` | ||||
// 3. use `findModel` as target, `modelDateForFind.id` as option. | ||||
let findModel = null; | ||||
let findModel = await models[modelName].findOne({ | ||||
where: { | ||||
id: modelDateForFind.id, | ||||
}, | ||||
}); | ||||
|
||||
Object.keys(fakeData.findOne).forEach(e => { | ||||
expect(findModel[e]).to.equal(fakeData.findOne[e]); | ||||
|
@@ -129,7 +174,7 @@ describe(`models/${modelName}`, () => { | |||
// 1. use model.findAll... | ||||
// 2. use data from `fakeData.findAll` | ||||
// 3. use `findAllModel` as target. | ||||
let findAllModel = null; | ||||
let findAllModel = await models[modelName].findAll(); | ||||
|
||||
expect(findAllModel.length).to.greaterThan(fakeData.findAll.length - 1); | ||||
}); | ||||
|
@@ -139,11 +184,15 @@ describe(`models/${modelName}`, () => { | |||
// 1. use model.findAll... | ||||
// 2. use data from `fakeData.findAll` | ||||
// 3. use `findAllModel` as target, and use `fakeData.keyword` as option. | ||||
let findAllModel = null; | ||||
let findAllModel = await models[modelName].findAll({ | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同上 |
||||
where: fakeData.keyword, | ||||
}); | ||||
|
||||
expect(findAllModel.length).to.greaterThan(fakeData.findAll.length - 1); | ||||
Object.keys(fakeData.findAll).forEach(e => { | ||||
expect(findAllModel[e]).to.equal(fakeData.findAll[e]); | ||||
fakeData.findAll.forEach(i => { | ||||
Object.keys(i).forEach(e => { | ||||
expect(findAllModel[e]).to.equal(fakeData.findAll[e]); | ||||
}); | ||||
}); | ||||
}); | ||||
}); | ||||
|
@@ -160,8 +209,16 @@ describe(`models/${modelName}`, () => { | |||
// 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; | ||||
let deleteModel = await models[modelName].destroy({ | ||||
where: { | ||||
id: modelDataForDestroy.id, | ||||
}, | ||||
}); | ||||
let findDeleteModel = await models[modelName].findOne({ | ||||
where: { | ||||
id: modelDataForDestroy.id, | ||||
}, | ||||
}); | ||||
expect(deleteModel).to.equal(1); | ||||
expect(findDeleteModel).to.equal(null); | ||||
}); | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tomas 建議如果沒有重新賦值會使用 const 來宣告