Test para todo el repositorio de orders
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { describe, it } from "node:test";
|
import { before, describe, it } from "node:test";
|
||||||
import { OrderRepository } from "./OrderRepository.js";
|
import { OrderRepository } from "./OrderRepository.js";
|
||||||
import { CreateOrderDTO } from "../domain/Order.js";
|
import { CreateOrderDTO } from "../domain/Order.js";
|
||||||
import { postgresClient } from "../config/config.test.js";
|
import { postgresClient } from "../config/config.test.js";
|
||||||
@@ -15,7 +15,12 @@ const order1 = <CreateOrderDTO>{
|
|||||||
|
|
||||||
describe("Test OrderRepository", {}, () => {
|
describe("Test OrderRepository", {}, () => {
|
||||||
const orderRepo = new OrderRepository(postgresClient)
|
const orderRepo = new OrderRepository(postgresClient)
|
||||||
|
let testId: number | undefined = undefined;
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
const result = await orderRepo.createOrder(order1)
|
||||||
|
testId = result.data.id
|
||||||
|
})
|
||||||
|
|
||||||
it("Insert new Order", async () => {
|
it("Insert new Order", async () => {
|
||||||
const newOrder = order1
|
const newOrder = order1
|
||||||
@@ -32,4 +37,90 @@ describe("Test OrderRepository", {}, () => {
|
|||||||
|
|
||||||
console.log("[T] Creada Order", typeof (result.data.start_date))
|
console.log("[T] Creada Order", typeof (result.data.start_date))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("Find by valid id should return the order", async () => {
|
||||||
|
const result = await orderRepo.getOrderById({ id: testId! })
|
||||||
|
assert(result.error == undefined)
|
||||||
|
assert(result.data != undefined)
|
||||||
|
|
||||||
|
const order = result.data
|
||||||
|
assert(order.id == testId)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Find by correlation id should return a valid order", async () => {
|
||||||
|
const result = await orderRepo.getOrderByQueueId({ correlation_id: order1.correlation_id })
|
||||||
|
assert(result.error == undefined)
|
||||||
|
assert(result.data != undefined)
|
||||||
|
|
||||||
|
const order = result.data
|
||||||
|
assert(order.correlation_id == order1.correlation_id)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Get pending orders should return a list including the test order", async () => {
|
||||||
|
const result = await orderRepo.getPendingOrders()
|
||||||
|
assert(result.error == undefined)
|
||||||
|
assert(Array.isArray(result.data))
|
||||||
|
const found = result.data.find(o => o.id === testId)
|
||||||
|
assert(found != undefined)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Update order status should change status and add history", async () => {
|
||||||
|
const newStatus = "running"
|
||||||
|
const reason = "Test update"
|
||||||
|
const result = await orderRepo.updateOrder({ id: testId!, new_status: newStatus, reason: reason })
|
||||||
|
|
||||||
|
assert(result.error == undefined)
|
||||||
|
assert(result.data != undefined)
|
||||||
|
assert(result.data.status === newStatus)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Finish order should set status to finished and set finish_date", async () => {
|
||||||
|
const result = await orderRepo.finishOrder({ id: testId!, reason: "Test finish" })
|
||||||
|
|
||||||
|
assert(result.error == undefined)
|
||||||
|
assert(result.data != undefined)
|
||||||
|
assert(result.data.status === "finished")
|
||||||
|
assert(result.data.finish_date != null)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Error order (failed) should increment retry_count and set status", async () => {
|
||||||
|
// Create another order for this test
|
||||||
|
const order2 = { ...order1, correlation_id: "fake-error-test" }
|
||||||
|
const createResult = await orderRepo.createOrder(order2)
|
||||||
|
const errTestId = createResult.data.id
|
||||||
|
|
||||||
|
const result = await orderRepo.errorOrder({
|
||||||
|
id: errTestId,
|
||||||
|
status: "failed",
|
||||||
|
reason: "Test failure",
|
||||||
|
error: "Some error",
|
||||||
|
stackTrace: "Some stack"
|
||||||
|
})
|
||||||
|
|
||||||
|
assert(result.error == undefined)
|
||||||
|
assert(result.data != undefined)
|
||||||
|
assert(result.data.status === "failed")
|
||||||
|
assert(result.data.retry_count > 0)
|
||||||
|
assert(result.data.finish_date == null)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("Error order (dlx) should set finish_date", async () => {
|
||||||
|
// Create another order for this test
|
||||||
|
const order3 = { ...order1, correlation_id: "fake-dlx-test" }
|
||||||
|
const createResult = await orderRepo.createOrder(order3)
|
||||||
|
const dlxTestId = createResult.data.id
|
||||||
|
|
||||||
|
const result = await orderRepo.errorOrder({
|
||||||
|
id: dlxTestId,
|
||||||
|
status: "dlx",
|
||||||
|
reason: "Test DLX",
|
||||||
|
error: "Fatal error",
|
||||||
|
stackTrace: "Fatal stack"
|
||||||
|
})
|
||||||
|
|
||||||
|
assert(result.error == undefined)
|
||||||
|
assert(result.data != undefined)
|
||||||
|
assert(result.data.status === "dlx")
|
||||||
|
assert(result.data.finish_date != null)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ export class OrderRepository {
|
|||||||
data: queryResult.rows
|
data: queryResult.rows
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return <Result<string, T>>{
|
return <Result<string, T[]>>{
|
||||||
error: e as string
|
error: e as string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,7 +57,7 @@ export class OrderRepository {
|
|||||||
`
|
`
|
||||||
const values = [data.id]
|
const values = [data.id]
|
||||||
const queryPromise = this.pgClient.query<OrderTracking<any>>(query, values)
|
const queryPromise = this.pgClient.query<OrderTracking<any>>(query, values)
|
||||||
const result = this.getFirst(queryPromise);
|
const result = await this.getFirst(queryPromise);
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ export class OrderRepository {
|
|||||||
`
|
`
|
||||||
const values = [data.correlation_id]
|
const values = [data.correlation_id]
|
||||||
const queryPromise = this.pgClient.query<OrderTracking<any>>(query, values)
|
const queryPromise = this.pgClient.query<OrderTracking<any>>(query, values)
|
||||||
const result = this.getFirst(queryPromise);
|
const result = await this.getFirst(queryPromise);
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,7 +161,7 @@ export class OrderRepository {
|
|||||||
const uOrderTracking = `
|
const uOrderTracking = `
|
||||||
UPDATE order_tracking
|
UPDATE order_tracking
|
||||||
SET
|
SET
|
||||||
status = $2,
|
status = $2::order_status,
|
||||||
update_date = (now() at time zone 'utc')
|
update_date = (now() at time zone 'utc')
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
RETURNING id, status, update_date;
|
RETURNING id, status, update_date;
|
||||||
@@ -188,19 +188,20 @@ export class OrderRepository {
|
|||||||
VALUES (
|
VALUES (
|
||||||
$1, -- ID de la orden
|
$1, -- ID de la orden
|
||||||
$2, -- Estado anterior
|
$2, -- Estado anterior
|
||||||
$3, -- Nuevo estado
|
$3::order_status, -- Nuevo estado
|
||||||
$4 -- Razón (ej: "Consumer processed successfully" o "RabbitMQ NACK")
|
$4 -- Razón (ej: "Consumer processed successfully" o "RabbitMQ NACK")
|
||||||
);
|
)
|
||||||
|
RETURNING id;
|
||||||
`
|
`
|
||||||
const vOrderHistory = [args.id, currentOrder.status, args.new_status, args.reason]
|
const vOrderHistory = [args.id, currentOrder.status, args.new_status, args.reason]
|
||||||
const newOrderHistory = await this.getFirst(
|
const newOrderHistoryResult = await this.getFirst(
|
||||||
client.query<{ id: number, status: string, update_date: string }>(iOrderHistory, vOrderHistory)
|
client.query<{ id: number }>(iOrderHistory, vOrderHistory)
|
||||||
)
|
)
|
||||||
|
|
||||||
if (newOrderHistory.error != undefined) {
|
if (newOrderHistoryResult.error != undefined) {
|
||||||
await client.query("ROLLBACK")
|
await client.query("ROLLBACK")
|
||||||
client.release()
|
client.release()
|
||||||
return updatedOrderResult
|
return newOrderHistoryResult
|
||||||
}
|
}
|
||||||
|
|
||||||
await client.query("COMMIT")
|
await client.query("COMMIT")
|
||||||
@@ -209,6 +210,7 @@ export class OrderRepository {
|
|||||||
client.query<OrderTracking<any>>(qCurrentOrder, vCurrentOrder)
|
client.query<OrderTracking<any>>(qCurrentOrder, vCurrentOrder)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
client.release()
|
||||||
return updatedOrder
|
return updatedOrder
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,17 +270,18 @@ export class OrderRepository {
|
|||||||
$2, -- Estado anterior
|
$2, -- Estado anterior
|
||||||
'finished',
|
'finished',
|
||||||
$3 -- Siempre "finished successfully" a no ser que se especifique otra razón
|
$3 -- Siempre "finished successfully" a no ser que se especifique otra razón
|
||||||
);
|
)
|
||||||
|
RETURNING id;
|
||||||
`
|
`
|
||||||
const vOrderHistory = [args.id, currentOrder.status, args.reason ?? "finished successfully"]
|
const vOrderHistory = [args.id, currentOrder.status, args.reason ?? "finished successfully"]
|
||||||
const newOrderHistory = await this.getFirst(
|
const newOrderHistoryResult = await this.getFirst(
|
||||||
client.query<{ id: number, status: string, update_date: string }>(iOrderHistory, vOrderHistory)
|
client.query<{ id: number }>(iOrderHistory, vOrderHistory)
|
||||||
)
|
)
|
||||||
|
|
||||||
if (newOrderHistory.error != undefined) {
|
if (newOrderHistoryResult.error != undefined) {
|
||||||
await client.query("ROLLBACK")
|
await client.query("ROLLBACK")
|
||||||
client.release()
|
client.release()
|
||||||
return updatedOrderResult
|
return newOrderHistoryResult
|
||||||
}
|
}
|
||||||
|
|
||||||
await client.query("COMMIT")
|
await client.query("COMMIT")
|
||||||
@@ -287,6 +290,7 @@ export class OrderRepository {
|
|||||||
client.query<OrderTracking<any>>(qCurrentOrder, vCurrentOrder)
|
client.query<OrderTracking<any>>(qCurrentOrder, vCurrentOrder)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
client.release()
|
||||||
return updatedOrder
|
return updatedOrder
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -323,9 +327,9 @@ export class OrderRepository {
|
|||||||
const uOrderTracking = `
|
const uOrderTracking = `
|
||||||
UPDATE order_tracking
|
UPDATE order_tracking
|
||||||
SET
|
SET
|
||||||
status = $2,
|
status = $2::order_status,
|
||||||
update_date = (now() at time zone 'utc'),
|
update_date = (now() at time zone 'utc'),
|
||||||
finish_date = CASE WHEN $2 = 'dlx' THEN (now() at time zone 'utc') ELSE null,
|
finish_date = CASE WHEN $2::order_status = 'dlx' THEN (now() at time zone 'utc') ELSE null END,
|
||||||
retry_count = retry_count + 1,
|
retry_count = retry_count + 1,
|
||||||
error_message = $3,
|
error_message = $3,
|
||||||
error_stacktrace = $4
|
error_stacktrace = $4
|
||||||
@@ -354,19 +358,20 @@ export class OrderRepository {
|
|||||||
VALUES (
|
VALUES (
|
||||||
$1, -- ID de la orden
|
$1, -- ID de la orden
|
||||||
$2, -- Estado anterior
|
$2, -- Estado anterior
|
||||||
$3, -- En este caso particular 'dlx' o 'failed'
|
$3::order_status, -- En este caso particular 'dlx' o 'failed'
|
||||||
$4 -- En este caso el motivo de fallo completo
|
$4 -- En este caso el motivo de fallo completo
|
||||||
);
|
)
|
||||||
|
RETURNING id;
|
||||||
`
|
`
|
||||||
const vOrderHistory = [args.id, currentOrder.status, args.reason ?? "finished successfully", args.stackTrace]
|
const vOrderHistory = [args.id, currentOrder.status, args.status, args.reason]
|
||||||
const newOrderHistory = await this.getFirst(
|
const newOrderHistoryResult = await this.getFirst(
|
||||||
client.query<{ id: number, status: string, update_date: string }>(iOrderHistory, vOrderHistory)
|
client.query<{ id: number }>(iOrderHistory, vOrderHistory)
|
||||||
)
|
)
|
||||||
|
|
||||||
if (newOrderHistory.error != undefined) {
|
if (newOrderHistoryResult.error != undefined) {
|
||||||
await client.query("ROLLBACK")
|
await client.query("ROLLBACK")
|
||||||
client.release()
|
client.release()
|
||||||
return updatedOrderResult
|
return newOrderHistoryResult
|
||||||
}
|
}
|
||||||
|
|
||||||
await client.query("COMMIT")
|
await client.query("COMMIT")
|
||||||
@@ -375,6 +380,7 @@ export class OrderRepository {
|
|||||||
client.query<OrderTracking<any>>(qCurrentOrder, vCurrentOrder)
|
client.query<OrderTracking<any>>(qCurrentOrder, vCurrentOrder)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
client.release()
|
||||||
return updatedOrder
|
return updatedOrder
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user