Skip to content

Commit

Permalink
Merge pull request CS3219-AY2324S1#85 from Chustinjeng/automated_testing
Browse files Browse the repository at this point in the history
automate test cases for question api
  • Loading branch information
zylee348 authored Nov 8, 2023
2 parents 99b66e8 + 9a008c7 commit d04416b
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 64 deletions.
29 changes: 0 additions & 29 deletions .github/workflows/node.js.yml

This file was deleted.

11 changes: 7 additions & 4 deletions .github/workflows/question-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
pull_request:

jobs:
build-and-test:
build-and-run-server:
runs-on: ubuntu-latest
env:
ENV: PROD
Expand All @@ -23,6 +23,9 @@ jobs:
- name: Install Dependencies
working-directory: ./src/backend/question_backend
run: npm i
- name: Run Tests
working-directory: ./src/backend/question_backend
run: npm test
- name: Install typescript
working-directory: ./src/cypress
run: npm install --save-dev typescript
- name: Run test cases
working-directory: ./src
run: node backend/question_backend/index.js & npx cypress run --spec "cypress/e2e/question_api.cy.js"
31 changes: 0 additions & 31 deletions .github/workflows/user-service.yml

This file was deleted.

118 changes: 118 additions & 0 deletions src/cypress/e2e/question_api.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/// <reference types="Cypress" />

const PORT = 8004;
const apiPath = `http://localhost:${PORT}/routes`;
const checkQuestionExistencePath = "checkQuestionExistence";
const addQuestionPath = "addQuestion";
const updateQuestionPath = "updateQuestion";
const deleteQuestionPath = "deleteQuestion";
var questionID = null;

const testQuestion = {
title: "Test Question",
description: "This is a test question",
category: "testing",
difficulty: "easy",
};

const updatedTestQuestion = {
title: "Updated Test Question",
description: "This is an updated test question",
category: "testing",
difficulty: "easy",
};

describe("Check that a question exists", () => {
it("should exist in question database", () => {
const body = { title: "Two Sum" };
cy.request({
method: "POST",
url: `${apiPath}/${checkQuestionExistencePath}`,
body: body,
}).then((response) => {
const status = response.status;
expect(status).to.be.equal(201);
const data = response.body;
expect(data.exists).to.be.equal(true);
});
});
});

describe("Finding a question that does not exist will show that it does not exist", () => {
it("should show that the question does not exist", () => {
const body = { title: "This question is invalid and does not exist" };
cy.request({
method: "POST",
url: `${apiPath}/${checkQuestionExistencePath}`,
body: body,
}).then((response) => {
const status = response.status;
expect(status).to.be.equal(201);
const data = response.body;
expect(data.exists).to.be.equal(false);
});
});
});



describe("adding a test question", () => {
it("should be able to add a test question", () => {
cy.request({
method: "POST",
url: `${apiPath}/${addQuestionPath}`,
body: testQuestion,
}).then((response) => {
const status = response.status;
questionID = response.body._id;
expect(status).to.be.equal(201);
});
});
});

describe("updating a test question", () => {
it("should be able to update the test question", () => {
const updatedTestQuestionId = {
title: "Updated Test Question",
description: "This is an updated test question",
category: "testing",
difficulty: "easy",
_id: questionID,
};
cy.request({
method: "PATCH",
url: `${apiPath}/${updateQuestionPath}`,
body: updatedTestQuestionId,
}).then((response) => {
const data = response.body;
expect(data.title).to.be.equal(updatedTestQuestionId.title);
expect(data.description).to.be.equal(updatedTestQuestionId.description);
});
});
});

describe("deleting a test question", () => {
it("should be able to delete the test question", () => {
cy.request({
method: "DELETE",
url: `${apiPath}/${deleteQuestionPath}`,
body: updatedTestQuestion,
}).then((response) => {
const status = response.status;
expect(status).to.be.equal(201);
});
});
});

describe("check if the test question is still there", () => {
it("should not still be there", () => {
cy.request({
method: "POST",
url: `${apiPath}/${checkQuestionExistencePath}`,
body: updatedTestQuestion,
}).then((response) => {
const data = response.body;
expect(data.exists).to.be.equal(false);
});
});
});

0 comments on commit d04416b

Please sign in to comment.