Skip to content

Commit

Permalink
more tests...
Browse files Browse the repository at this point in the history
  • Loading branch information
idugalic committed Nov 1, 2024
1 parent 624373f commit 82aba1b
Showing 1 changed file with 144 additions and 9 deletions.
153 changes: 144 additions & 9 deletions lib/domain_test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { restaurantDecider, restaurantView } from "./domain.ts";
import {
orderDecider,
orderView,
restaurantDecider,
restaurantView,
} from "./domain.ts";
import {
orderCommandSchema,
orderEventSchema,
restaurantCommandSchema,
restaurantEventSchema,
} from "./api_schema.ts";
import { ViewSpecification } from "../test_specification.ts";
import { DeciderSpecification } from "../test_specification.ts";
import type { RestaurantCommand, RestaurantEvent } from "./api.ts";
import type {
OrderCommand,
OrderEvent,
RestaurantCommand,
RestaurantEvent,
} from "./api.ts";

// #########################################################################
// A convinient testing specififcation for the Decider - GIVEN / WHEN / THEN
// #########################################################################

// Json representation of the command
const createRestaurantCommandJson = `
{
"commandId": "691490bb-c4d3-45b8-99d0-efcf20e353ag",
"decider": "Restaurant",
"kind": "CreateRestaurantCommand",
"id": "691490bb-c4d3-45b8-99d0-efcf20e353ao",
Expand Down Expand Up @@ -52,7 +65,6 @@ const restaurantCreatedEventJson = `
// Json representation of the command
const changeRestaurantMenuCommandJson = `
{
"commandId": "691490bb-c4d3-45b8-99d0-efcf20e353ag",
"decider": "Restaurant",
"kind": "ChangeRestaurantMenuCommand",
"id": "691490bb-c4d3-45b8-99d0-efcf20e353ao",
Expand All @@ -70,7 +82,6 @@ const changeRestaurantMenuCommandJson = `

const placeOrderCommandJson = `
{
"commandId": "691490bb-c4d3-45b8-99d0-efcf20e353ag",
"decider": "Restaurant",
"kind": "PlaceOrderCommand",
"id": "691490bb-c4d3-45b8-99d0-efcf20e353ao",
Expand All @@ -83,6 +94,28 @@ const placeOrderCommandJson = `
}
`;

const createOrderCommandJson = `
{
"decider": "Order",
"kind": "CreateOrderCommand",
"restaurantId": "691490bb-c4d3-45b8-99d0-efcf20e353ao",
"id": "691490bb-c4d3-45b8-99d0-efcf20e353ag",
"menuItems": [
{"menuItemId": "1", "name": "Salad2", "price": "18.59"},
{"menuItemId": "2", "name": "Soup2", "price": "16.94"},
{"menuItemId": "3", "name": "Steak2", "price": "19.89"}
]
}
`;

const markOrderAsPreparedCommandJson = `
{
"decider": "Order",
"kind": "MarkOrderAsPreparedCommand",
"id": "691490bb-c4d3-45b8-99d0-efcf20e353ag"
}
`;

// Json representation of the expected event(s)
const restaurantMenuChangedEventJson = `
{
Expand Down Expand Up @@ -110,7 +143,7 @@ const restaurantOrderPlacedEventJson = `
"kind": "RestaurantOrderPlacedEvent",
"id": "691490bb-c4d3-45b8-99d0-efcf20e353ao",
"orderId": "691490bb-c4d3-45b8-99d0-efcf20e353ag",
"menuItems": [
"menuItems": [
{"menuItemId": "1", "name": "Salad2", "price": "18.59"},
{"menuItemId": "2", "name": "Soup2", "price": "16.94"},
{"menuItemId": "3", "name": "Steak2", "price": "19.89"}
Expand All @@ -135,6 +168,32 @@ const restaurantOrderNotPlacedEventJson = `
}
`;

const orderCreatedEventJson = `
{
"version": 1,
"decider": "Order",
"kind": "OrderCreatedEvent",
"restaurantId": "691490bb-c4d3-45b8-99d0-efcf20e353ao",
"id": "691490bb-c4d3-45b8-99d0-efcf20e353ag",
"menuItems": [
{"menuItemId": "1", "name": "Salad2", "price": "18.59"},
{"menuItemId": "2", "name": "Soup2", "price": "16.94"},
{"menuItemId": "3", "name": "Steak2", "price": "19.89"}
],
"final": false
}
`;

const orderPreparedEventJson = `
{
"version": 1,
"decider": "Order",
"kind": "OrderPreparedEvent",
"id": "691490bb-c4d3-45b8-99d0-efcf20e353ag",
"final": false
}
`;

// The Decider test for the restaurant create command
Deno.test(function createRestaurantDeciderTest() {
// Parse the restaurant command from the request / Zod validation/parsing
Expand Down Expand Up @@ -176,7 +235,7 @@ Deno.test(function changeRestaurantMenuDeciderTest() {
JSON.parse(restaurantMenuChangedEventJson),
);

// Run the second test specification for the restaurant decider
// Run the test specification for the restaurant decider
DeciderSpecification.for(restaurantDecider)
.given([restaurantCreatedEvent])
.when(changeRestaurantMenuCommand)
Expand All @@ -203,7 +262,7 @@ Deno.test(function placeOrderAtRestaurantDeciderTest() {
JSON.parse(restaurantOrderPlacedEventJson),
);

// Run the second test specification for the restaurant decider
// Run the test specification for the restaurant decider
DeciderSpecification.for(restaurantDecider)
.given([restaurantCreatedEvent])
.when(placeOrderCommand)
Expand All @@ -224,14 +283,65 @@ Deno.test(function placeOrderAtRestaurantWithErrorDeciderTest() {
JSON.parse(restaurantOrderNotPlacedEventJson),
);

// Run the second test specification for the restaurant decider
// Run the test specification for the restaurant decider
DeciderSpecification.for(restaurantDecider)
// Given NO restaurant previously created!!!
.given([])
.when(placeOrderCommand)
.then([restaurantOrderNotPlacedEvent]);
});

// The Decider test for the create order command
Deno.test(function createOrderDeciderTest() {
// Parse the order command from the request / Zod validation/parsing
const createOrderCommand: OrderCommand = orderCommandSchema
.parse(
JSON.parse(createOrderCommandJson),
);

// Parse the order event.
const orderCreatedEvent: OrderEvent = orderEventSchema
.parse(
JSON.parse(orderCreatedEventJson),
);

// Run the test specification for the order decider
DeciderSpecification.for(orderDecider)
.given([])
.when(createOrderCommand)
.then([orderCreatedEvent]);
});

// The Decider test for the create order command
Deno.test(function markOrderAsPeparedDeciderTest() {
// Parse the order command from the request / Zod validation/parsing
const markOrderAsPreparedCommand: OrderCommand = orderCommandSchema
.parse(
JSON.parse(markOrderAsPreparedCommandJson),
);

// Parse the order event.
const orderCreatedEvent: OrderEvent = orderEventSchema
.parse(
JSON.parse(orderCreatedEventJson),
);
// Parse the order event.
const orderPreparedEvent: OrderEvent = orderEventSchema
.parse(
JSON.parse(orderPreparedEventJson),
);

// Run the test specification for the order decider
DeciderSpecification.for(orderDecider)
.given([orderCreatedEvent])
.when(markOrderAsPreparedCommand)
.then([orderPreparedEvent]);
});

// ###############################################################
// A convinient testing specififcation for the View - GIVEN / THEN
// ###############################################################

Deno.test(function reastaurantCreatedViewTest() {
// Parse the restaurant event.
const restaurantCreatedEvent: RestaurantEvent = restaurantEventSchema
Expand Down Expand Up @@ -286,4 +396,29 @@ Deno.test(function restaurantMenuChangedViewTest() {
});
});

Deno.test(function orderPreparedViewTest() {
// Parse the order event.
const orderCreatedEvent: OrderEvent = orderEventSchema
.parse(
JSON.parse(orderCreatedEventJson),
);
const orderPreparedEvent: OrderEvent = orderEventSchema
.parse(
JSON.parse(orderPreparedEventJson),
);

ViewSpecification.for(orderView)
.given([orderCreatedEvent, orderPreparedEvent])
.then({
orderId: "691490bb-c4d3-45b8-99d0-efcf20e353ag",
restaurantId: "691490bb-c4d3-45b8-99d0-efcf20e353ao",
menuItems: [
{ menuItemId: "1", name: "Salad2", price: "18.59" },
{ menuItemId: "2", name: "Soup2", price: "16.94" },
{ menuItemId: "3", name: "Steak2", price: "19.89" },
],
status: "PREPARED",
});
});

// Run the tests: `deno test`

0 comments on commit 82aba1b

Please sign in to comment.