-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from AbdulrhmanGoni/tests/write-new-three-test…
…s-files Write new three e2e tests files
- Loading branch information
Showing
3 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { describe, expect, it, afterAll } from "bun:test"; | ||
import { request } from "../.."; | ||
import operationsResultsMessages from "../../../src/constants/operationsResultsMessages"; | ||
import DatasetsModel from "../../../src/models/DatasetsModel"; | ||
import { fakeDatasets } from "../../fake-data/fakeDatasets"; | ||
import RecentActivitiesModel from "../../../src/models/RecentActivitiesModel"; | ||
import { Types } from "mongoose"; | ||
|
||
const path = "datasets/:datasetId"; | ||
|
||
describe(`GET /${path}`, () => { | ||
it("Should return an error because the dataset is not existent", async () => { | ||
const randomDatasetId = new Types.ObjectId(); | ||
const { resBody, status } = await request.GET( | ||
path.replace(":datasetId", randomDatasetId.toHexString()) | ||
); | ||
|
||
expect(status).toBe(400); | ||
expect(resBody.message).toBe( | ||
operationsResultsMessages.noDataset(randomDatasetId.toHexString()) | ||
); | ||
}); | ||
|
||
it("Should return the targeted dataset by its id", async () => { | ||
const targetedDataset = fakeDatasets[0]; | ||
const targetedDatasetId = targetedDataset._id.toHexString(); | ||
await DatasetsModel.create({ | ||
_id: process.env.TESTING_USER_ID, | ||
datasets: [targetedDataset], | ||
}); | ||
|
||
const { resBody, status } = await request.GET( | ||
path.replace(":datasetId", targetedDatasetId) | ||
); | ||
|
||
expect(status).toBe(200); | ||
expect(resBody.data._id).toBe(targetedDatasetId); | ||
expect(resBody.data).toMatchObject({ | ||
_id: targetedDatasetId, | ||
name: targetedDataset.name, | ||
description: targetedDataset.description, | ||
instructionsCount: 0, | ||
createdAt: expect.any(String), | ||
updatedAt: expect.any(String), | ||
}); | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await DatasetsModel.deleteMany(); | ||
await RecentActivitiesModel.deleteMany(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { afterAll, describe, expect, it, beforeAll } from "bun:test"; | ||
import { request } from "../.."; | ||
import { fakeDatasets } from "../../fake-data/fakeDatasets"; | ||
import DatasetsModel from "../../../src/models/DatasetsModel"; | ||
import RecentActivitiesModel from "../../../src/models/RecentActivitiesModel"; | ||
import operationsResultsMessages from "../../../src/constants/operationsResultsMessages"; | ||
import { emptyUpdateDatasetBodyMessage } from "../../../src/middlewares/updateDatasetInputValidator"; | ||
|
||
beforeAll(async () => { | ||
await DatasetsModel.create({ | ||
_id: process.env.TESTING_USER_ID, | ||
datasets: fakeDatasets, | ||
}); | ||
}); | ||
|
||
const path = "datasets/:datasetId"; | ||
|
||
describe(`PATCH /${path}`, () => { | ||
it("Should update the name of the dataset successflly", async () => { | ||
const fakeDatasetToUpdate = fakeDatasets[0]; | ||
const fakeDatasetIdToUpdate = fakeDatasetToUpdate._id.toHexString(); | ||
|
||
const updateData = { name: "New Dataset Name" }; | ||
|
||
const { resBody, status } = await request.PATCH( | ||
path.replace(":datasetId", fakeDatasetIdToUpdate), | ||
updateData | ||
); | ||
|
||
expect(status).toBe(200); | ||
expect(resBody.data).toMatchObject({ | ||
name: updateData.name, | ||
description: expect.any(String), | ||
instructionsCount: 0, | ||
_id: expect.toBeObjectId(), | ||
createdAt: expect.any(String), | ||
updatedAt: expect.any(String), | ||
}); | ||
expect(resBody.message).toBe( | ||
operationsResultsMessages.successfulDatasetUpdate | ||
); | ||
}); | ||
|
||
it("Should update the description of the dataset successflly", async () => { | ||
const fakeDatasetToUpdate = fakeDatasets[1]; | ||
const fakeDatasetIdToUpdate = fakeDatasetToUpdate._id.toHexString(); | ||
|
||
const updateData = { description: "New Dataset Description" }; | ||
|
||
const { resBody, status } = await request.PATCH( | ||
path.replace(":datasetId", fakeDatasetIdToUpdate), | ||
updateData | ||
); | ||
|
||
expect(status).toBe(200); | ||
expect(resBody.data).toMatchObject({ | ||
name: expect.any(String), | ||
description: updateData.description, | ||
instructionsCount: 0, | ||
_id: expect.toBeObjectId(), | ||
createdAt: expect.any(String), | ||
updatedAt: expect.any(String), | ||
}); | ||
expect(resBody.message).toBe( | ||
operationsResultsMessages.successfulDatasetUpdate | ||
); | ||
}); | ||
|
||
it("Should return an error because the request body is empty", async () => { | ||
const fakeDatasetToUpdate = fakeDatasets[2]; | ||
const fakeDatasetIdToUpdate = fakeDatasetToUpdate._id.toHexString(); | ||
|
||
const { resBody, status } = await request.PATCH( | ||
path.replace(":datasetId", fakeDatasetIdToUpdate), | ||
{} | ||
); | ||
|
||
expect(status).toBe(400); | ||
expect(resBody.message).toBe(emptyUpdateDatasetBodyMessage); | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await DatasetsModel.deleteMany(); | ||
await RecentActivitiesModel.deleteMany(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { afterAll, describe, expect, it } from "bun:test"; | ||
import { request } from "../.."; | ||
import { getRandomFakeDataset } from "../../fake-data/fakeDatasets"; | ||
import DatasetsModel from "../../../src/models/DatasetsModel"; | ||
import InstructionModel from "../../../src/models/InstructionModel"; | ||
import RecentActivitiesModel from "../../../src/models/RecentActivitiesModel"; | ||
import operationsResultsMessages from "../../../src/constants/operationsResultsMessages"; | ||
import { emptyUpdateInstructionBodyMessage } from "../../../src/middlewares/updateInstructionInputValidator"; | ||
import { getFakeInstructions } from "../../fake-data/fakeInstructions"; | ||
|
||
const path = "instructions"; | ||
|
||
describe(`PATCH /${path}`, async () => { | ||
const fakeDataset = getRandomFakeDataset(); | ||
const fakeDatasetId = fakeDataset._id; | ||
const fakeInstructions = getFakeInstructions(fakeDatasetId); | ||
await DatasetsModel.create({ | ||
_id: process.env.TESTING_USER_ID, | ||
datasets: [fakeDataset], | ||
}); | ||
await InstructionModel.insertMany(fakeInstructions); | ||
|
||
it("Should update the `systemMessage` field of the instruction successflly", async () => { | ||
const fakeInstructionToUpdate = fakeInstructions[0]; | ||
const fakeInstructionIdToUpdate = fakeInstructionToUpdate._id.toHexString(); | ||
|
||
const updateData = { | ||
systemMessage: "New system message for the instruction", | ||
}; | ||
const searchParams = new URLSearchParams(); | ||
searchParams.set("instructionId", fakeInstructionIdToUpdate); | ||
searchParams.set("datasetId", fakeDatasetId.toHexString()); | ||
|
||
const { resBody, status } = await request.PATCH( | ||
`${path}?${searchParams}`, | ||
updateData | ||
); | ||
|
||
expect(status).toBe(200); | ||
expect(resBody.data).toMatchObject({ | ||
_id: fakeInstructionIdToUpdate, | ||
systemMessage: updateData.systemMessage, | ||
question: expect.any(String), | ||
answer: expect.any(String), | ||
datasetId: fakeDatasetId.toHexString(), | ||
createdAt: expect.any(String), | ||
updatedAt: expect.any(String), | ||
}); | ||
expect(resBody.message).toBe( | ||
operationsResultsMessages.successfulInstructionUpdate | ||
); | ||
}); | ||
|
||
it("Should update `question` & `answer` fields of the instruction successflly", async () => { | ||
const fakeInstructionToUpdate = fakeInstructions[1]; | ||
const fakeInstructionIdToUpdate = fakeInstructionToUpdate._id.toHexString(); | ||
|
||
const updateData = { | ||
question: "Is this the new question for the instruction?", | ||
answer: "Yes, this is the new question for the instruction?", | ||
}; | ||
const searchParams = new URLSearchParams(); | ||
searchParams.set("instructionId", fakeInstructionIdToUpdate); | ||
searchParams.set("datasetId", fakeDatasetId.toHexString()); | ||
|
||
const { resBody, status } = await request.PATCH( | ||
`${path}?${searchParams}`, | ||
updateData | ||
); | ||
|
||
expect(status).toBe(200); | ||
expect(resBody.data).toMatchObject({ | ||
_id: fakeInstructionIdToUpdate, | ||
...updateData, | ||
systemMessage: expect.any(String), | ||
datasetId: fakeDatasetId.toHexString(), | ||
createdAt: expect.any(String), | ||
updatedAt: expect.any(String), | ||
}); | ||
expect(resBody.message).toBe( | ||
operationsResultsMessages.successfulInstructionUpdate | ||
); | ||
}); | ||
|
||
it("Should return an error because the request body is empty", async () => { | ||
const fakeInstructionToUpdate = fakeInstructions[2]; | ||
const fakeInstructionIdToUpdate = fakeInstructionToUpdate._id.toHexString(); | ||
|
||
const searchParams = new URLSearchParams(); | ||
searchParams.set("instructionId", fakeInstructionIdToUpdate); | ||
searchParams.set("datasetId", fakeDatasetId.toHexString()); | ||
|
||
const { resBody, status } = await request.PATCH( | ||
`${path}?${searchParams}`, | ||
{} | ||
); | ||
|
||
expect(status).toBe(400); | ||
expect(resBody.message).toBe(emptyUpdateInstructionBodyMessage); | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await DatasetsModel.deleteMany(); | ||
await InstructionModel.deleteMany(); | ||
await RecentActivitiesModel.deleteMany(); | ||
}); |