Files
canina/backend/test/torob.integration.ts

48 lines
2.6 KiB
TypeScript

import { PrismaClient } from '@prisma/client';
import { TorobController } from '../src/products/torob.controller';
import { PrismaService } from '../src/prisma/prisma.service';
// Dedicated disposable DB only. Never initializes PrismaService's DDL hooks.
describe('Torob real PostgreSQL regression', () => {
let db: PrismaClient;
let controller: TorobController;
const originalUrl = process.env.FRONTEND_URL;
beforeAll(async () => {
const url = new URL(process.env.DATABASE_URL || '');
if (url.hostname !== 'torob-test-db' || url.pathname !== '/torob_test') {
throw new Error('Refusing any DB other than disposable torob-test-db/torob_test');
}
process.env.FRONTEND_URL = 'https://stage.canina.ir';
db = new PrismaClient();
await db.$connect();
const category = await db.category.create({data: {name: 'Fixture', slug: 'fixture'}});
const common = {categoryId: category.id, categorySlug: category.slug, nameFa: 'آزمون', nameEn: 'Fixture', description: 'test fixture', priceValue: '123456.78', priceDisplay: 'fixture', unit: 'g', packageSize: 10, suitableFor: 'DOG', imageUrl: '/uploads/fixture.png'};
await db.product.createMany({data: [
{...common, artNo: 'T1', slug: 'fixture-in', stock: 3, stockStatus: 'IN_STOCK'},
{...common, artNo: 'T2', slug: 'fixture-zero', stock: 0, stockStatus: 'IN_STOCK'},
{...common, artNo: 'T3', slug: 'fixture-out', stock: 3, stockStatus: 'OUT_OF_STOCK'},
{...common, artNo: 'T4', slug: 'fixture-hidden', noIndex: true},
{...common, artNo: 'T5', slug: 'fixture-discontinued', stockStatus: 'DISCONTINUED'},
]});
controller = new TorobController(db as PrismaService);
});
afterAll(async () => {
if (db) await db.$disconnect();
if (originalUrl === undefined) delete process.env.FRONTEND_URL;
else process.env.FRONTEND_URL = originalUrl;
});
it('filters hidden/discontinued rows and preserves DB decimal price, stock and URLs', async () => {
const result = await controller.getTorobProducts();
expect(result.count).toBe(3);
expect(result.results.map(p => p.product_id).sort()).toEqual(['T1', 'T2', 'T3']);
for (const item of result.results) {
const row = await db.product.findUniqueOrThrow({where: {artNo: item.product_id}});
expect(item.price).toBe(Number(row.priceValue));
expect(item.old_price).toBe(Number(row.priceValue));
expect(item.availability).toBe(row.artNo === 'T1' ? 'instock' : 'outofstock');
expect(item.page_url).toBe(`https://stage.canina.ir/shop/${row.slug}`);
expect(item.image_link).toBe('https://stage.canina.ir/api/uploads/fixture.png');
}
});
});