added order_spec.ts
This commit is contained in:
parent
e5655bb06a
commit
bd61cb5646
@ -1,7 +1,9 @@
|
||||
{
|
||||
"spec_dir": "src/tests",
|
||||
"spec_files": [
|
||||
"models/*_[sS]pec.ts",
|
||||
"models/product_spec.ts",
|
||||
"models/user_spec.ts",
|
||||
"models/order_spec.ts",
|
||||
"handlers/*_[sS]pec.ts"
|
||||
],
|
||||
"helpers": [
|
||||
|
@ -1,7 +1,7 @@
|
||||
CREATE TABLE products (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(250) NOT NULL,
|
||||
price INTEGER NOT NULL
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(250) NOT NULL,
|
||||
price INTEGER NOT NULL
|
||||
);
|
||||
|
||||
|
||||
@ -10,20 +10,20 @@ CREATE TABLE users (
|
||||
firstName VARCHAR(250) NOT NULL,
|
||||
lastName VARCHAR(250) NOT NULL,
|
||||
username VARCHAR(250) NOT NULL,
|
||||
password VARCHAR(250) NOT NULL
|
||||
password VARCHAR(250) NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE orders (
|
||||
id SERIAL PRIMARY KEY,
|
||||
status VARCHAR(15),
|
||||
user_id bigint REFERENCES users(id)
|
||||
id SERIAL PRIMARY KEY,
|
||||
status VARCHAR(15),
|
||||
user_id INTEGER NOT NULL REFERENCES users(id)
|
||||
);
|
||||
|
||||
CREATE TABLE order_products (
|
||||
id SERIAL PRIMARY KEY,
|
||||
quantity INTEGER NOT NULL,
|
||||
order_id bigint REFERENCES orders(id),
|
||||
product_id bigint REFERENCES products(id)
|
||||
id SERIAL PRIMARY KEY,
|
||||
quantity INTEGER NOT NULL,
|
||||
order_id INTEGER NOT NULL REFERENCES orders(id),
|
||||
product_id INTEGER NOT NULL REFERENCES products(id)
|
||||
)
|
||||
|
||||
|
||||
|
@ -23,7 +23,7 @@ const index = async (req: Request, res: Response) => {
|
||||
|
||||
const read = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const order = await store.read(req.params.id)
|
||||
const order = await store.read(parseInt(req.params.id))
|
||||
res.json(order)
|
||||
} catch (err) {
|
||||
res.status(400)
|
||||
@ -36,7 +36,7 @@ const create = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const orderInfo: Order = {
|
||||
status: req.body.status,
|
||||
userId: parseInt(req.body.userId)
|
||||
user_id: parseInt(req.body.userId)
|
||||
}
|
||||
|
||||
console.log('orderInfo', orderInfo)
|
||||
@ -52,9 +52,9 @@ const create = async (req: Request, res: Response) => {
|
||||
const addProduct = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const orderProductInfo: OrderProduct = {
|
||||
orderId: parseInt(req.params.id),
|
||||
order_id: parseInt(req.params.id),
|
||||
quantity: parseInt(req.body.quantity),
|
||||
productId: parseInt(req.body.productId)
|
||||
product_id: parseInt(req.body.product_id)
|
||||
}
|
||||
console.log(orderProductInfo)
|
||||
const addedProduct = await store.addProduct(orderProductInfo)
|
||||
|
@ -1,14 +1,16 @@
|
||||
import client from '../database'
|
||||
|
||||
export type Order = {
|
||||
id?: number
|
||||
status: string
|
||||
userId: number
|
||||
user_id: number
|
||||
}
|
||||
|
||||
export type OrderProduct = {
|
||||
id?: number
|
||||
quantity: number
|
||||
orderId: number
|
||||
productId: number
|
||||
order_id: number
|
||||
product_id: number
|
||||
}
|
||||
|
||||
|
||||
@ -31,13 +33,13 @@ export class OrderStore {
|
||||
}
|
||||
}
|
||||
|
||||
async read(userId: string): Promise<Order> {
|
||||
async read(user_id: number): Promise<Order> {
|
||||
try {
|
||||
|
||||
const conn = await client.connect()
|
||||
const sql = 'SELECT * FROM orders WHERE user_id=($1)'
|
||||
|
||||
const result = await conn.query(sql, [userId])
|
||||
const result = await conn.query(sql, [user_id])
|
||||
const order = result.rows[0]
|
||||
|
||||
conn.release()
|
||||
@ -45,7 +47,7 @@ export class OrderStore {
|
||||
return order
|
||||
|
||||
} catch (err) {
|
||||
throw new Error(`Could not find order ${userId}. Error: ${err}`)
|
||||
throw new Error(`Could not find order ${user_id}. Error: ${err}`)
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,7 +57,7 @@ export class OrderStore {
|
||||
const conn = await client.connect()
|
||||
const sql = 'INSERT INTO orders (status, user_id) VALUES ($1, $2) RETURNING *'
|
||||
|
||||
const result = await conn.query(sql, [o.status, o.userId])
|
||||
const result = await conn.query(sql, [o.status, o.user_id])
|
||||
const order = result.rows[0]
|
||||
|
||||
conn.release()
|
||||
@ -63,17 +65,17 @@ export class OrderStore {
|
||||
return order
|
||||
|
||||
} catch (err) {
|
||||
throw new Error(`Could not add a new order ${o.userId}. Error: ${err}`)
|
||||
throw new Error(`Could not add a new order ${o.user_id}. Error: ${err}`)
|
||||
}
|
||||
}
|
||||
|
||||
async addProduct(o: OrderProduct): Promise<Order> {
|
||||
async addProduct(o: OrderProduct): Promise<OrderProduct> {
|
||||
try {
|
||||
|
||||
const conn = await client.connect()
|
||||
const sql = 'INSERT INTO order_products (quantity, order_id, product_id) VALUES ($1, $2, $3) RETURNING *'
|
||||
|
||||
const result = await conn.query(sql, [o.quantity, o.orderId, o.productId])
|
||||
const result = await conn.query(sql, [o.quantity, o.order_id, o.product_id])
|
||||
const order = result.rows[0]
|
||||
|
||||
conn.release()
|
||||
@ -81,7 +83,7 @@ export class OrderStore {
|
||||
return order
|
||||
|
||||
} catch (err) {
|
||||
throw new Error(`Could not add a new orderProducts ${o.orderId}. Error: ${err}`)
|
||||
throw new Error(`Could not add a new orderProducts ${o.order_id}. Error: ${err}`)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,6 +21,7 @@ const updatedProduct: Product = {
|
||||
|
||||
describe("Product handler", () => {
|
||||
|
||||
console.log("PRODUCT")
|
||||
it('Should create a new product', async () => {
|
||||
const response = await request
|
||||
.post("/products")
|
||||
|
@ -0,0 +1,90 @@
|
||||
import { User, UserStore } from '../../models/user'
|
||||
import { Product, ProductStore } from '../../models/product';
|
||||
import { Order, OrderProduct, OrderStore } from '../../models/order'
|
||||
|
||||
const orderStore = new OrderStore()
|
||||
const userStore = new UserStore()
|
||||
const productStore = new ProductStore()
|
||||
|
||||
const testProduct: Product = {
|
||||
id: 2,
|
||||
name: '1984',
|
||||
price: 5
|
||||
}
|
||||
|
||||
const testUser: User = {
|
||||
id: 2,
|
||||
firstname: 'John',
|
||||
lastname: 'Doe',
|
||||
username: 'Jd',
|
||||
password: 'password'
|
||||
}
|
||||
|
||||
const testOrder: Order = {
|
||||
id: 1,
|
||||
status: "active",
|
||||
user_id: 2
|
||||
}
|
||||
|
||||
const testOrderProduct: OrderProduct = {
|
||||
id: 1,
|
||||
quantity: 5,
|
||||
order_id: 1,
|
||||
product_id: 2
|
||||
}
|
||||
|
||||
describe("Order model", () => {
|
||||
|
||||
beforeAll(async () => {
|
||||
await productStore.create(testProduct)
|
||||
await userStore.create(testUser)
|
||||
})
|
||||
|
||||
console.log('ORDER')
|
||||
it('Should have an index method', () => {
|
||||
expect(orderStore.index).toBeDefined();
|
||||
});
|
||||
|
||||
it('Should have a read method', () => {
|
||||
expect(orderStore.read).toBeDefined;
|
||||
});
|
||||
|
||||
it('Should have a create method', () => {
|
||||
expect(orderStore.create).toBeDefined;
|
||||
});
|
||||
|
||||
it('Should have a update method', () => {
|
||||
expect(orderStore.addProduct).toBeDefined;
|
||||
});
|
||||
|
||||
it('Create method should add a new order', async () => {
|
||||
const result = await orderStore.create(testOrder)
|
||||
expect(result).toEqual(testOrder)
|
||||
});
|
||||
|
||||
it('Index method should return a list of orders', async () => {
|
||||
const result = await orderStore.index();
|
||||
expect(result[0]).toEqual(testOrder)
|
||||
})
|
||||
|
||||
|
||||
it('Read method should return a order', async () => {
|
||||
const result = await orderStore.read(2);
|
||||
expect(result).toEqual(testOrder)
|
||||
})
|
||||
|
||||
|
||||
it('addProduct should add a new order_products', async () => {
|
||||
const result = await orderStore.addProduct(testOrderProduct)
|
||||
expect(result).toEqual(testOrderProduct)
|
||||
})
|
||||
|
||||
|
||||
|
||||
afterAll(async () => {
|
||||
await productStore.delete(1)
|
||||
await userStore.delete(1)
|
||||
})
|
||||
|
||||
|
||||
});
|
@ -2,6 +2,18 @@ import { Product, ProductStore } from '../../models/product'
|
||||
|
||||
const store = new ProductStore()
|
||||
|
||||
const testProduct: Product = {
|
||||
id: 1,
|
||||
name: '1984',
|
||||
price: 5,
|
||||
}
|
||||
|
||||
const updatedProduct: Product = {
|
||||
id: 1,
|
||||
name: 'The Dark Tower',
|
||||
price: 7
|
||||
}
|
||||
|
||||
describe("Product model", () => {
|
||||
it('Should have an index method', () => {
|
||||
expect(store.index).toBeDefined();
|
||||
@ -25,44 +37,23 @@ describe("Product model", () => {
|
||||
|
||||
|
||||
it('Create method should add a product', async () => {
|
||||
const result = await store.create({
|
||||
name: '1984',
|
||||
price: 5,
|
||||
|
||||
});
|
||||
expect(result).toEqual({
|
||||
id: 1,
|
||||
name: '1984',
|
||||
price: 5,
|
||||
});
|
||||
const result = await store.create(testProduct);
|
||||
expect(result).toEqual(testProduct);
|
||||
});
|
||||
|
||||
it('Index method should return a list of products', async () => {
|
||||
const result = await store.index();
|
||||
expect(result).toEqual([{
|
||||
id: 1,
|
||||
name: '1984',
|
||||
price: 5,
|
||||
}]);
|
||||
expect(result).toEqual([testProduct]);
|
||||
|
||||
});
|
||||
|
||||
it('Show method should return a product', async () => {
|
||||
it('Read method should return a product', async () => {
|
||||
const result = await store.read(1);
|
||||
expect(result).toEqual({
|
||||
id: 1,
|
||||
name: '1984',
|
||||
price: 5,
|
||||
});
|
||||
expect(result).toEqual(testProduct);
|
||||
|
||||
});
|
||||
|
||||
it('Update method should update a product', async () => {
|
||||
const updatedProduct: Product = {
|
||||
id: 1,
|
||||
name: 'The Dark Tower',
|
||||
price: 7
|
||||
}
|
||||
const result = await store.update(updatedProduct);
|
||||
expect(result).toEqual({
|
||||
id: 1,
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { UserStore } from '../../models/user'
|
||||
import { User, UserStore } from '../../models/user'
|
||||
import bcrypt from 'bcrypt'
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ const pepper = BCRYPT_PASSWORD
|
||||
|
||||
const store = new UserStore()
|
||||
|
||||
const newUser = {
|
||||
const testUser: User = {
|
||||
id: 1,
|
||||
firstname: 'John',
|
||||
lastname: 'Doe',
|
||||
@ -16,7 +16,16 @@ const newUser = {
|
||||
password: 'password'
|
||||
}
|
||||
|
||||
const updatedUser: User = {
|
||||
id: 1,
|
||||
firstname: 'Sara',
|
||||
lastname: 'Doe',
|
||||
username: 'Sd',
|
||||
password: 'password123'
|
||||
}
|
||||
|
||||
describe("User model", () => {
|
||||
console.log("USER")
|
||||
it('Should have an index method', () => {
|
||||
expect(store.index).toBeDefined();
|
||||
});
|
||||
@ -38,12 +47,12 @@ describe("User model", () => {
|
||||
});
|
||||
|
||||
it('Create method should create a new user', async () => {
|
||||
const result = await store.create(newUser);
|
||||
const result = await store.create(testUser);
|
||||
|
||||
expect(result.firstname).toEqual(newUser.firstname)
|
||||
expect(result.lastname).toEqual(newUser.lastname)
|
||||
expect(result.username).toEqual(newUser.username)
|
||||
expect(bcrypt.compareSync(newUser.password + pepper, result.password)).toBeTrue
|
||||
expect(result.firstname).toEqual(testUser.firstname)
|
||||
expect(result.lastname).toEqual(testUser.lastname)
|
||||
expect(result.username).toEqual(testUser.username)
|
||||
expect(bcrypt.compareSync(testUser.password + pepper, result.password)).toBeTrue
|
||||
})
|
||||
|
||||
|
||||
@ -52,19 +61,19 @@ describe("User model", () => {
|
||||
|
||||
|
||||
const result = await store.index();
|
||||
expect(result[0].firstname).toEqual(newUser.firstname)
|
||||
expect(result[0].lastname).toEqual(newUser.lastname)
|
||||
expect(result[0].username).toEqual(newUser.username)
|
||||
expect(bcrypt.compareSync(newUser.password + pepper, result[0].password)).toBeTrue
|
||||
expect(result[0].firstname).toEqual(testUser.firstname)
|
||||
expect(result[0].lastname).toEqual(testUser.lastname)
|
||||
expect(result[0].username).toEqual(testUser.username)
|
||||
expect(bcrypt.compareSync(testUser.password + pepper, result[0].password)).toBeTrue
|
||||
})
|
||||
|
||||
it('Read method should return user', async () => {
|
||||
const result = await store.read(1);
|
||||
|
||||
expect(result.firstname).toEqual(newUser.firstname)
|
||||
expect(result.lastname).toEqual(newUser.lastname)
|
||||
expect(result.username).toEqual(newUser.username)
|
||||
expect(bcrypt.compareSync(newUser.password + pepper, result.password)).toBeTrue
|
||||
expect(result.firstname).toEqual(testUser.firstname)
|
||||
expect(result.lastname).toEqual(testUser.lastname)
|
||||
expect(result.username).toEqual(testUser.username)
|
||||
expect(bcrypt.compareSync(testUser.password + pepper, result.password)).toBeTrue
|
||||
})
|
||||
|
||||
it('Authenticate method', async () => {
|
||||
@ -75,23 +84,15 @@ describe("User model", () => {
|
||||
const result = await store.authenticate(authUser.username, authUser.password);
|
||||
|
||||
if (result) {
|
||||
expect(result.firstname).toEqual(newUser.firstname)
|
||||
expect(result.lastname).toEqual(newUser.lastname)
|
||||
expect(result.username).toEqual(newUser.username)
|
||||
expect(bcrypt.compareSync(newUser.password + pepper, result.password)).toBeTrue
|
||||
expect(result.firstname).toEqual(testUser.firstname)
|
||||
expect(result.lastname).toEqual(testUser.lastname)
|
||||
expect(result.username).toEqual(testUser.username)
|
||||
expect(bcrypt.compareSync(testUser.password + pepper, result.password)).toBeTrue
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
it('Update method should create a new user', async () => {
|
||||
const updatedUser = {
|
||||
id: 1,
|
||||
firstname: 'Sara',
|
||||
lastname: 'Doe',
|
||||
username: 'Sd',
|
||||
password: 'password123'
|
||||
}
|
||||
|
||||
const result = await store.update(updatedUser);
|
||||
|
||||
expect(result.firstname).toEqual(updatedUser.firstname)
|
||||
@ -101,7 +102,7 @@ describe("User model", () => {
|
||||
})
|
||||
|
||||
it('Delete method should delete a user by Id', async () => {
|
||||
const result = await store.delete(newUser.id);
|
||||
const result = await store.delete(1);
|
||||
const checkUsers = await store.index()
|
||||
|
||||
expect(checkUsers).toEqual([])
|
||||
|
Loading…
Reference in New Issue
Block a user