Skip to content

Commit

Permalink
Merge pull request #72 from AbdulrhmanGoni/tests/write-new-three-test…
Browse files Browse the repository at this point in the history
…s-files

Write new three e2e tests files
  • Loading branch information
AbdulrhmanGoni authored Oct 28, 2024
2 parents 60d21f9 + 365176b commit 575006a
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/e2e/datasets/getDatasetById.test.ts
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();
});
86 changes: 86 additions & 0 deletions tests/e2e/datasets/updateDataset.test.ts
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();
});
107 changes: 107 additions & 0 deletions tests/e2e/instructions/updateInstruction.test.ts
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();
});

0 comments on commit 575006a

Please sign in to comment.