Skip to content

Commit fd610e0

Browse files
tests updated
1 parent 708029a commit fd610e0

File tree

6 files changed

+831
-1531
lines changed

6 files changed

+831
-1531
lines changed

__tests__/errors.test.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,47 @@
1-
// __tests__/error.test.js
2-
const { handler } = require('../index');
1+
// __tests__/errors.test.js
2+
require('dotenv').config(); // loads your .env before tests
3+
const { handler } = require('../index'); // adjust path if your Lambda file is named differently
34

45
describe('Error handling tests', () => {
6+
57
test('GET /users/:id returns 404 if user does not exist', async () => {
68
const event = {
79
httpMethod: 'GET',
8-
path: '/users/9999',
10+
pathParameters: { id: '999999' } // use a non-existing user ID
911
};
1012

11-
// Set RDS credentials
12-
process.env.DB_HOST = process.env.DB_HOST;
13-
process.env.DB_USER = process.env.DB_USER;
14-
process.env.DB_PASSWORD = process.env.DB_PASSWORD;
15-
process.env.DB_NAME = process.env.DB_NAME;
16-
process.env.DB_PORT = process.env.DB_PORT || 5432;
13+
const response = await handler(event);
14+
15+
expect(response.statusCode).toBe(404);
16+
const body = JSON.parse(response.body);
17+
expect(body.message).toBe('User not found');
18+
});
19+
20+
test('PUT /users/:id returns 404 if user does not exist', async () => {
21+
const event = {
22+
httpMethod: 'PUT',
23+
pathParameters: { id: '999999' }, // non-existing ID
24+
body: JSON.stringify({ name: 'Test', email: 'test@example.com' })
25+
};
1726

1827
const response = await handler(event);
1928

2029
expect(response.statusCode).toBe(404);
30+
const body = JSON.parse(response.body);
31+
expect(body.message).toBe('User not found');
32+
});
33+
34+
test('DELETE /users/:id returns 404 if user does not exist', async () => {
35+
const event = {
36+
httpMethod: 'DELETE',
37+
pathParameters: { id: '999999' } // non-existing ID
38+
};
2139

40+
const response = await handler(event);
41+
42+
expect(response.statusCode).toBe(404);
2243
const body = JSON.parse(response.body);
2344
expect(body.message).toBe('User not found');
2445
});
25-
});
2646

47+
});

__tests__/example.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// __tests__/example.test.js
2+
require('dotenv').config(); // loads .env file
23
const { handler } = require('../index');
34

5+
46
describe('Lambda API tests', () => {
57
test('GET /users should return status 200 and a list of users', async () => {
68
const event = {

__tests__/users.test.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// __tests__/users.test.js
2+
require('dotenv').config(); // loads .env file
23
const { handler } = require('../index');
34

5+
46
describe('POST /users endpoint', () => {
57
test('should create a new user and return status 201', async () => {
68
const event = {
@@ -10,12 +12,11 @@ describe('POST /users endpoint', () => {
1012
};
1113

1214
// Set RDS credentials
13-
process.env.DB_HOST = process.env.DB_HOST;
14-
process.env.DB_USER = process.env.DB_USER;
15-
process.env.DB_PASSWORD = process.env.DB_PASSWORD;
16-
process.env.DB_NAME = process.env.DB_NAME;
17-
process.env.DB_PORT = process.env.DB_PORT || 5432;
18-
15+
//process.env.DB_HOST = process.env.DB_HOST;
16+
//process.env.DB_USER = process.env.DB_USER;
17+
//process.env.DB_PASSWORD = process.env.DB_PASSWORD;
18+
//process.env.DB_NAME = process.env.DB_NAME;
19+
//process.env.DB_PORT = process.env.DB_PORT || 5432;
1920
const response = await handler(event);
2021

2122
expect(response.statusCode).toBe(201);

index.js

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
const { Client } = require('pg');
22

33
exports.handler = async (event) => {
4-
5-
// Create a NEW client inside handler (not globally)
6-
const client = new Client({
7-
host: process.env.DB_HOST,
8-
user: process.env.DB_USER,
9-
password: process.env.DB_PASSWORD,
10-
database: process.env.DB_NAME,
11-
port: 5432,
12-
ssl: {
13-
rejectUnauthorized: false // IMPORTANT for RDS
14-
}
4+
// Create a NEW client inside handler
5+
const client = new Client({
6+
host: process.env.DB_HOST,
7+
user: process.env.DB_USER,
8+
password: process.env.DB_PASSWORD,
9+
database: process.env.DB_NAME,
10+
port: 5432,
11+
ssl: { rejectUnauthorized: false } // IMPORTANT for RDS
1512
});
16-
13+
1714
await client.connect();
1815

1916
const method = event.httpMethod;
2017
let body = {};
18+
let statusCode = 200; // default
2119

2220
try {
2321
if (method === 'POST') {
@@ -27,13 +25,25 @@ const client = new Client({
2725
[data.name, data.email]
2826
);
2927
body = res.rows[0];
28+
statusCode = 201; // Created
3029

3130
} else if (method === 'GET') {
3231
const id = event.pathParameters ? event.pathParameters.id : null;
3332
const res = id
3433
? await client.query('SELECT * FROM users WHERE id=$1', [id])
3534
: await client.query('SELECT * FROM users');
36-
body = res.rows;
35+
if (id) {
36+
if (!res.rows[0]) {
37+
body = { message: 'User not found' };
38+
statusCode = 404; // Not found
39+
} else {
40+
body = res.rows[0];
41+
statusCode = 200;
42+
}
43+
} else {
44+
body = res.rows;
45+
statusCode = 200;
46+
}
3747

3848
} else if (method === 'PUT') {
3949
const id = event.pathParameters.id;
@@ -42,29 +52,41 @@ const client = new Client({
4252
'UPDATE users SET name=$1, email=$2 WHERE id=$3 RETURNING *',
4353
[data.name, data.email, id]
4454
);
45-
body = res.rows[0];
55+
if (!res.rows[0]) {
56+
body = { message: 'User not found' };
57+
statusCode = 404;
58+
} else {
59+
body = res.rows[0];
60+
statusCode = 200;
61+
}
4662

4763
} else if (method === 'DELETE') {
4864
const id = event.pathParameters.id;
49-
await client.query('DELETE FROM users WHERE id=$1', [id]);
50-
body = { message: 'Deleted successfully' };
65+
const res = await client.query('DELETE FROM users WHERE id=$1 RETURNING *', [id]);
66+
if (!res.rows[0]) {
67+
body = { message: 'User not found' };
68+
statusCode = 404;
69+
} else {
70+
body = { message: 'Deleted successfully' };
71+
statusCode = 200;
72+
}
5173

5274
} else {
5375
body = { message: 'Unsupported method' };
76+
statusCode = 400;
5477
}
5578

5679
} catch (err) {
5780
body = { error: err.message };
81+
statusCode = 500;
5882

5983
} finally {
6084
await client.end(); // always close the connection
6185
}
6286

6387
return {
64-
statusCode: 200,
88+
statusCode,
6589
headers: { "Content-Type": "application/json" },
6690
body: JSON.stringify(body)
6791
};
6892
};
69-
70-

0 commit comments

Comments
 (0)