Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update db.test.ts #1132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 78 additions & 36 deletions week-10/1-postgres-simple/src/db/__tests__/db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,56 +25,98 @@ describe('User Database Operations', () => {
expect(user.rows[0]).toHaveProperty('name', name);
expect(user.rows[0].password).toBe(password);
});
test('getUser retrieves a user by ID', async () => {import { client } from '../..';
import { createUser, getUser } from '../user';
import { createTables, dropTables } from '../setup';
import { createTodo, updateTodo, getTodos } from '../todo';

beforeAll(async () => {
await client.connect();
await dropTables();
await createTables();
});

afterAll(async () => {
await client.end();
});

describe('User Database Operations', () => {
let userId;

beforeAll(async () => {
const username = 'testuser';
const password = 'testpass';
const name = 'Test User';

// Insert a test user and retrieve their ID for subsequent tests
await createUser(username, password, name);
const result = await client.query('SELECT id FROM users WHERE username = $1', [username]);
userId = result.rows[0].id;
});

test('createUser inserts a new user into the database', async () => {
const result = await client.query('SELECT * FROM users WHERE id = $1', [userId]);
const user = result.rows[0];

expect(user).toHaveProperty('username', 'testuser');
expect(user).toHaveProperty('name', 'Test User');
expect(user.password).toBe('testpass');
});

test('getUser retrieves a user by ID', async () => {
// Assuming an existing user with ID 1 for this test
const userId = 1;
const user = await getUser(userId);

expect(user).toHaveProperty('id', userId);
expect(user).toHaveProperty('username');
expect(user).toHaveProperty('name');
expect(user).toHaveProperty('username', 'testuser');
expect(user).toHaveProperty('name', 'Test User');
});
});


describe('Todo Operations', () => {
let userId: number;
let userId;

beforeAll(async () => {
// Assuming you have a function to get a user by username for test setup
const res = await client.query('SELECT id FROM users WHERE username = $1', ['testuser']);
userId = res.rows[0].id;
const result = await client.query('SELECT id FROM users WHERE username = $1', ['testuser']);
userId = result.rows[0].id;
});

test('createTodo inserts a new todo for a user', async () => {
const title = 'Test Todo';
const description = 'Test Description';
const todo = await createTodo(userId, title, description);

expect(todo).toHaveProperty('id');
expect(todo.title).toEqual(title);
expect(todo.description).toEqual(description);
expect(todo.done).toEqual(false);
const title = 'Test Todo';
const description = 'Test Description';

const todo = await createTodo(userId, title, description);
expect(todo).toMatchObject({
title,
description,
done: false,
});
});

test('updateTodo marks a todo as done', async () => {
// First, create a todo to update
const { id: todoId } = await createTodo(userId, 'Update Test', 'To be updated');

const updatedTodo = await updateTodo(todoId);
expect(updatedTodo.done).toEqual(true);
const { id: todoId } = await createTodo(userId, 'Update Test', 'To be updated');

const updatedTodo = await updateTodo(todoId);
expect(updatedTodo.done).toBe(true);
});

test('getTodos retrieves all todos for a user', async () => {
// Assuming there are already todos created in previous tests
const todos = await getTodos(userId);

expect(todos.length).toBeGreaterThan(0);
todos.forEach(todo => {
expect(todo).toHaveProperty('id');
expect(todo.user_id).toEqual(userId);
});
const todos = await getTodos(userId);

expect(todos.length).toBeGreaterThan(0);
todos.forEach(todo => {
expect(todo).toHaveProperty('id');
expect(todo.user_id).toBe(userId);
});
});
});

});

// Assuming an existing user with ID 1 for this test
const userId = 1;
const user = await getUser(userId);

expect(user).toHaveProperty('id', userId);
expect(user).toHaveProperty('username');
expect(user).toHaveProperty('name');
});
});