Skip to content

Commit

Permalink
Activity tracking tests for endpoints (#25)
Browse files Browse the repository at this point in the history
* tests for /exercises endpoint

- installing jest and supertest
- configuring jest
- configuring server to close after tests
- updating /exercises/add api to return exercise object created

* adding step to run tests for activity tracking

- includes temp changes to run build pipeline after pushing to this branch

* forgot to install packages before running test

* using npm ci instead

* to trigger build on push

* triggering test with changes to activity tracking

* cleaning up the activity tracking workflow file

and triggering a build

* added delete endpoint for weekly targets for testing

* removed redundant lines from delete exercises test

* tests for /targets endpoints

* removing temporary feature branch push trigger from activity tracking workflow
  • Loading branch information
VathsalaAchar committed Mar 28, 2024
1 parent 5925e99 commit db0fa9c
Show file tree
Hide file tree
Showing 8 changed files with 808 additions and 7,877 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/deploy-ActivityTracking.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,36 @@ on:
jobs:
build:
runs-on: ubuntu-latest
services:
mongodb:
image: mongo:7
env:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: cfgmla23
options: >-
--health-cmd "echo 'db.runCommand("ping").ok' | mongosh --quiet"
--health-interval 10s
--health-timeout 5s
--health-retries 5
--name mongo_container
ports:
- 27017:27017
steps:
- name: Checkout code
uses: actions/checkout@v2

# Setting up CI pipeline to run npm tests
- name: Set up node environment for test
uses: actions/setup-node@v3
with:
node-version: '14'

- name: Run npm tests
run: |
cd activity-tracking
npm ci
npm run test
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
Expand Down
8,458 changes: 588 additions & 7,870 deletions activity-tracking/package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions activity-tracking/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"private": true,
"dependencies": {
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"express": "^4.19.2",
"moment": "^2.30.1",
"mongoose": "^7.5.4",
"web-vitals": "^2.1.0"
Expand All @@ -15,10 +16,10 @@
"cypress": "^13.3.0",
"jest": "^29.7.0",
"mongodb-memory-server": "^9.0.0",
"supertest": "^6.3.3"
"supertest": "^6.3.4"
},
"scripts": {
"test": "jest",
"test": "jest --detectOpenHandles",
"cy:open": "cypress open",
"cy:run": "cypress run --config-file ./cypress/cypress.config.js"
},
Expand Down
2 changes: 1 addition & 1 deletion activity-tracking/routes/exercises.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ router.post('/add', async (req, res) => {
});

await newExercise.save();
res.json({ message: 'Exercise added!' });
res.json({ message: 'Exercise added!', data: newExercise });
} catch (error) {
console.error(error);
res.status(400).json({ error: 'Error: ' + error.message });
Expand Down
15 changes: 14 additions & 1 deletion activity-tracking/routes/weeklytargets.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ router.post('/add', async (req, res) => {
otherTarget,
weekStartDate
});

const savedWeeklyTarget = await newWeeklyTarget.save();
return res.status(201).json(savedWeeklyTarget);

Expand Down Expand Up @@ -73,4 +72,18 @@ router.patch('/update', async (req, res) => {
}
});

// DELETE: Delete an exercise by ID
router.delete('/:id', async (req, res) => {
try {
const deletedTargets = await WeeklyTarget.findByIdAndDelete(req.params.id);
if (!deletedTargets) {
res.status(404).json({ error: 'Weekly target not found' });
return;
}
res.json({ message: 'Weekly Target deleted.' });
} catch (error) {
res.status(400).json({ error: 'Error: ' + error.message });
}
});

module.exports = router;
4 changes: 2 additions & 2 deletions activity-tracking/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ app.use((err, req, res, next) => {
});

// Start the server
app.listen(port, () => {
const server = app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});

module.exports = app;
module.exports = { app, server };
90 changes: 90 additions & 0 deletions activity-tracking/test/exercise.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const mongoose = require('mongoose');
const request = require('supertest');
const { app, server } = require('../server');
require("dotenv").config;

const username = "testuser"
const today = new Date()

const exerciseToAdd = {
username: username,
exerciseType: "Running",
description: " ",
duration: 100,
distance: 10,
date: today
}

const exerciseToUpdate = {
username: username,
exerciseType: "Running",
description: " ",
duration: 15,
distance: 1.5,
date: today
}

let exerciseId = " "

afterAll(async () => {
await server.close()
await mongoose.connection.close()
});

describe("GET /exercises/", () => {
it("should return all exercises", async () => {
return request(app)
.get("/exercises/")
.expect('Content-Type', /json/)
.expect(200)
.then((res) => {
expect(res.statusCode).toBe(200);
})
});
});

describe("POST /exercises/add", () => {
test("should create a new exercise", async () => {
return request(app)
.post("/exercises/add")
.send(exerciseToAdd)
.expect(200)
.then(({ body }) => {
exerciseId = body.data._id
expect(body.data.duration).toBe(exerciseToAdd.duration)
expect(body.data.distance).toBe(exerciseToAdd.distance)
})
});
});

describe("GET /exercises/:id", () => {
test("should get exercise for an id", async () => {
return request(app)
.get(`/exercises/${exerciseId}`)
.expect(200)
.expect('Content-Type', /json/)
});
});

describe("PUT /exercises/update/:id", () => {
test("should update an existing exercise", async () => {
return request(app)
.put(`/exercises/update/${exerciseId}`)
.send(exerciseToUpdate)
.expect(200)
.then(({ body }) => {
expect(body.exercise._id).toBe(exerciseId)
expect(body.exercise.duration).toBe(exerciseToUpdate.duration)
expect(body.exercise.distance).toBe(exerciseToUpdate.distance)
})
});
});

describe("DELETE /exercises/:id", () => {
test("should delete an exercise for given id", async () => {
return request(app)
.delete(`/exercises/${exerciseId}`)
.expect(200)
});
});

83 changes: 83 additions & 0 deletions activity-tracking/test/weeklytarget.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const mongoose = require('mongoose');
const request = require('supertest');
const moment = require('moment')
const { app, server } = require('../server');
require("dotenv").config;

const username = "test"
const weekStartDate = moment().startOf('week').toDate()
console.log(weekStartDate)
const weeklyTargetToAdd = {
username: username,
runningTarget: 10,
cyclingTarget: 20,
swimmingTarget: 0,
gymTarget: 100,
otherTarget: 0,
weekStartDate: weekStartDate
}

const weeklyTargetToUpdate = {
username: username,
runningTarget: 10,
cyclingTarget: 45,
swimmingTarget: 10,
gymTarget: 60,
otherTarget: 0,
weekStartDate: weekStartDate
}

let weeklyTargetId = " "

afterAll(async () => {
await server.close()
await mongoose.connection.close()
});

describe("GET /targets/", () => {
it("should return all weekly targets", async () => {
return request(app)
.get("/targets/")
.expect('Content-Type', /json/)
.expect(200)
.then((res) => {
expect(res.statusCode).toBe(200);
})
});
});

describe("POST /targets/add", () => {
test("should create a new weekly target", async () => {
return request(app)
.post("/targets/add")
.send(weeklyTargetToAdd)
.expect(201)
.then(({ body }) => {
weeklyTargetId = body._id
expect(body.username).toBe(username)
expect(body.runningTarget).toBe(weeklyTargetToAdd.runningTarget)
})
});
});

describe("PATCH /targets/update", () => {
test("should create a new weekly target", async () => {
return request(app)
.patch("/targets/update")
.send(weeklyTargetToUpdate)
.expect(200)
.then(({ body }) => {
expect(body._id).toBe(weeklyTargetId)
expect(body.username).toBe(username)
expect(body.runningTarget).toBe(weeklyTargetToUpdate.runningTarget)
})
});
});

describe("DELETE /targets/:id", () => {
test("should delete a target for given id", async () => {
return request(app)
.delete(`/targets/${weeklyTargetId}`)
.expect(200)
});
});

0 comments on commit db0fa9c

Please sign in to comment.