-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.test.js
More file actions
116 lines (99 loc) · 4.11 KB
/
app.test.js
File metadata and controls
116 lines (99 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const request = require('supertest');
const express = require('express');
const mongoose = require('mongoose');
const { MongoMemoryServer } = require('mongodb-memory-server');
const app = express();
// Middleware setup
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
// Model and routes definitions
const sleepData = new mongoose.Schema({
userId: { type: Number, required: true },
hours: { type: Number, required: true }
}, { timestamps: true });
const Sleep = mongoose.model("Sleep", sleepData);
app.get("/", (req, res) => {
res.end("Welcome to sleep Data");
});
app.post("/sleep", async (req, res) => {
const body = req.body;
if (!body || !body.userId || !body.hours) {
return res.status(400).json({ error: "All fields are required" });
}
const userSleepData = await Sleep.create({ userId: body.userId, hours: body.hours });
return res.status(201).json({ message: "User created successfully" });
});
app.get("/sleep/:id", async (req, res) => {
try {
const userIdData = await Sleep.find({ userId: req.params.id }).sort({ createdAt: 1 });
if (!userIdData || userIdData.length === 0) {
return res.status(404).send({ message: "No sleep records found for this user" });
}
res.send(userIdData);
} catch (error) {
res.status(500).send({ message: "An error occurred while fetching sleep records" });
}
});
app.delete("/sleep/:recordId", async (req, res) => {
try {
const result = await Sleep.findByIdAndDelete(req.params.recordId);
if (!result) {
return res.status(404).send({ message: "Sleep record not found" });
}
res.send({ message: "Sleep record deleted successfully" });
} catch (error) {
res.status(500).send({ message: "An error occurred while deleting the sleep record" });
}
});
let mongoServer;
beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
const uri = mongoServer.getUri();
await mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });
});
afterAll(async () => {
await mongoose.disconnect();
await mongoServer.stop();
});
describe("Test the sleep data API", () => {
it("should return welcome message", async () => {
const response = await request(app).get("/");
expect(response.status).toBe(200);
expect(response.text).toBe("Welcome to sleep Data");
});
it("should create a new sleep record", async () => {
const response = await request(app).post("/sleep").send({
userId: 1,
hours: 8
});
expect(response.status).toBe(201);
expect(response.body.message).toBe("User created successfully");
});
it("should return 400 for missing fields", async () => {
const response = await request(app).post("/sleep").send({});
expect(response.status).toBe(400);
expect(response.body.error).toBe("All fields are required");
});
it("should get sleep records for a user", async () => {
await request(app).post("/sleep").send({ userId: 1, hours: 8 });
const response = await request(app).get("/sleep/1");
expect(response.status).toBe(200);
expect(response.body.length).toBeGreaterThan(0);
});
it("should return 404 if no sleep records found", async () => {
const response = await request(app).get("/sleep/999");
expect(response.status).toBe(404);
expect(response.body.message).toBe("No sleep records found for this user");
});
it("should delete a sleep record", async () => {
const record = await Sleep.create({ userId: 1, hours: 8 });
const response = await request(app).delete(`/sleep/${record._id}`);
expect(response.status).toBe(200);
expect(response.body.message).toBe("Sleep record deleted successfully");
});
it("should return 404 if sleep record not found for deletion", async () => {
const response = await request(app).delete("/sleep/507f1f77bcf86cd799439011");
expect(response.status).toBe(404);
expect(response.body.message).toBe("Sleep record not found");
});
});