Skip to content

Commit

Permalink
Add alternative tests for postgres adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
aguspdana committed Feb 4, 2025
1 parent 725b90c commit 288fd2a
Show file tree
Hide file tree
Showing 5 changed files with 492 additions and 5 deletions.
31 changes: 30 additions & 1 deletion tests/adapters/postgresDB/mocks/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,33 @@ VALUES
('hook2', 'b'),
('hook3', 'c'),
('hook4', 'a'),
('hook5', 'b');
('hook5', 'b');

-- Hotel
-------------------------------------------------------------------------------

INSERT INTO "Hotel" ("id", "name", "location") VALUES
('h1', 'Grand Palace Hotel', 'New York, USA'),
('h2', 'Ocean View Resort', 'Miami, USA'),
('h3', 'Mountain Lodge', 'Denver, USA');

INSERT INTO "Room" ("id", "hotelId", "pricePerNight", "isAvailable") VALUES
('r1', 'h1', 150.00, TRUE),
('r2', 'h1', 200.00, FALSE),
('r3', 'h2', 180.00, TRUE),
('r4', 'h2', 220.00, TRUE),
('r5', 'h3', 100.00, FALSE);

INSERT INTO "Guest" ("id", "name", "email", "phone") VALUES
('g1', 'John Doe', 'john.doe@example.com', '123-456-7890'),
('g2', 'Jane Smith', 'jane.smith@example.com', '987-654-3210'),
('g3', 'Robert Brown', 'robert.brown@example.com', NULL);

INSERT INTO "Booking" ("id", "roomId", "guestId", "checkIn", "checkOut", "status", "totalCost") VALUES
('b1', 'r2', 'g1', '2024-02-01', '2024-02-05', 'checked-in', 800.00),
('b2', 'r5', 'g2', '2024-03-10', '2024-03-15', 'reserved', 500.00),
('b3', 'r3', 'g3', '2024-01-20', '2024-01-25', 'checked-out', 900.00);

INSERT INTO "Payment" ("id", "bookingId", "amountPaid", "paymentDate") VALUES
('p1', 'b1', 800.00, '2024-02-01 14:30:00'),
('p2', 'b3', 900.00, '2024-01-20 10:00:00');
42 changes: 41 additions & 1 deletion tests/adapters/postgresDB/mocks/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,44 @@ CREATE TABLE IF NOT EXISTS "Employee" (
"id" TEXT PRIMARY KEY,
"name" TEXT NOT NULL,
"companyId" TEXT REFERENCES "Company"("id")
);
);

-- Hotel
-------------------------------------------------------------------------------

CREATE TABLE IF NOT EXISTS "Hotel" (
"id" TEXT PRIMARY KEY,
"name" TEXT NOT NULL,
"location" VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS "Room" (
"id" TEXT PRIMARY KEY,
"hotelId" TEXT REFERENCES "Hotel"("id"),
"pricePerNight" DECIMAL(10,2) NOT NULL,
"isAvailable" BOOLEAN DEFAULT TRUE
);

CREATE TABLE IF NOT EXISTS "Guest" (
"id" TEXT PRIMARY KEY,
"name" VARCHAR(255) NOT NULL,
"email" VARCHAR(255) UNIQUE,
"phone" VARCHAR(20)
);

CREATE TABLE IF NOT EXISTS "Booking" (
"id" TEXT PRIMARY KEY,
"roomId" TEXT REFERENCES "Room"("id"),
"guestId" TEXT REFERENCES "Guest"("id"),
"checkIn" DATE NOT NULL,
"checkOut" DATE NOT NULL,
"status" VARCHAR(20) NOT NULL DEFAULT 'reserved', -- 'reserved', 'checked-in', 'checked-out', 'canceled'
"totalCost" DECIMAL(10,2) NOT NULL
);

CREATE TABLE IF NOT EXISTS "Payment" (
"id" TEXT PRIMARY KEY,
"bookingId" TEXT REFERENCES "Booking"("id"),
"amountPaid" DECIMAL(10,2) NOT NULL,
"paymentDate" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
150 changes: 150 additions & 0 deletions tests/mocks/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1143,5 +1143,155 @@ export const schema: BormSchema = {
company: { cardinality: 'ONE', dbConfig: { db: 'postgres', fields: [{ path: 'companyId', type: 'TEXT' }] } },
},
},
'Hotel': {
idFields: ['id'],
defaultDBConnector: { id: 'default', path: 'Hotel' },
dataFields: [
{ ...id },
{ ...name, validations: { required: true } },
{
path: 'location',
contentType: 'TEXT',
},
],
linkFields: [
{
path: 'rooms',
cardinality: 'MANY',
relation: 'Room',
plays: 'hotel',
target: 'relation',
},
],
},
'Room': {
idFields: ['id'],
defaultDBConnector: { id: 'default', path: 'Room' },
dataFields: [
{ ...id },
{
path: 'pricePerNight',
contentType: 'NUMBER_DECIMAL',
validations: { required: true },
},
{
path: 'isAvailable',
contentType: 'BOOLEAN',
validations: { required: true },
},
],
linkFields: [
{
path: 'bookings',
cardinality: 'MANY',
relation: 'Booking',
plays: 'room',
target: 'relation',
},
{
path: 'guests',
cardinality: 'MANY',
relation: 'Booking',
plays: 'room',
target: 'role',
},
],
roles: {
hotel: { cardinality: 'ONE', dbConfig: { db: 'postgres', fields: [{ path: 'hotelId', type: 'TEXT' }] } },
},
},
'Guest': {
idFields: ['id'],
defaultDBConnector: { id: 'default', path: 'Guest' },
dataFields: [
{ ...id },
{ ...name, validations: { required: true } },
{
path: 'email',
contentType: 'TEXT',
validations: { required: true },
},
{
path: 'phone',
contentType: 'TEXT',
},
],
linkFields: [
{
path: 'bookings',
cardinality: 'MANY',
relation: 'Booking',
plays: 'guest',
target: 'relation',
},
{
path: 'rooms',
cardinality: 'MANY',
relation: 'Booking',
plays: 'guest',
target: 'role',
},
],
},
'Booking': {
idFields: ['id'],
defaultDBConnector: { id: 'default', path: 'Booking' },
dataFields: [
{ ...id },
{
path: 'checkIn',
contentType: 'DATE',
validations: { required: true },
},
{
path: 'checkOut',
contentType: 'DATE',
validations: { required: true },
},
{
path: 'status',
contentType: 'TEXT',
validations: { required: true, enum: ['reserved', 'checked-in', 'checked-out', 'canceled'] },
},
{
path: 'totalCost',
contentType: 'NUMBER_DECIMAL',
validations: { required: true },
},
],
linkFields: [
{
path: 'payments',
cardinality: 'MANY',
relation: 'Payment',
plays: 'booking',
target: 'relation',
},
],
roles: {
room: { cardinality: 'ONE', dbConfig: { db: 'postgres', fields: [{ path: 'roomId', type: 'TEXT' }] } },
guest: { cardinality: 'ONE', dbConfig: { db: 'postgres', fields: [{ path: 'guestId', type: 'TEXT' }] } },
},
},
'Payment': {
idFields: ['id'],
defaultDBConnector: { id: 'default', path: 'Payment' },
dataFields: [
{ ...id },
{
path: 'amountPaid',
contentType: 'NUMBER_DECIMAL',
validations: { required: true },
},
{
path: 'paymentDate',
contentType: 'DATE',
validations: { required: true },
},
],
roles: {
booking: { cardinality: 'ONE', dbConfig: { db: 'postgres', fields: [{ path: 'bookingId', type: 'TEXT' }] } },
},
},
},
} as const;
6 changes: 3 additions & 3 deletions tests/postgres.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ docker run \
-d \
postgres

sleep 2
sleep 1

# Create the tables
docker run \
Expand All @@ -58,7 +58,7 @@ docker run \
postgres \
psql -h localhost -p 5432 -U ${USER} -d ${DB} -f /tests/schema.sql

sleep 2
sleep 1

# Insert data
docker run \
Expand All @@ -70,7 +70,7 @@ docker run \
postgres \
psql -h localhost -p 5432 -U ${USER} -d ${DB} -f /tests/data.sql

sleep 2
sleep 1

# Run tests and capture output
if CONTAINER_NAME=${CONTAINER_NAME} npx vitest run "${VITEST_ARGS[@]}"; then
Expand Down
Loading

0 comments on commit 288fd2a

Please sign in to comment.