greenly-hiring-test/test-e2e/foodProductFootprint.e2e-test.ts

80 lines
2.7 KiB
TypeScript

import {INestApplication} from "@nestjs/common";
import {Test, TestingModule} from "@nestjs/testing";
import * as request from "supertest";
import {dataSource, GreenlyDataSource} from "../config/dataSource";
import {AppModule} from "../src/app.module";
import {CarbonEmissionFactor} from "../src/carbonEmissionFactor/carbonEmissionFactor.entity";
import {seedTestCarbonEmissionFactors} from "../src/seed-dev-data";
import {FoodProductService} from "../src/foodProduct/foodProduct.service";
import {FoodProduct} from "../src/foodProduct/foodProduct.entity";
import {FoodProductFootprintService} from "../src/foodProductFootprint/foodProductFootprint.service";
beforeAll(async () => {
await dataSource.initialize();
await GreenlyDataSource.cleanDatabase();
});
afterAll(async () => {
await dataSource.destroy();
});
describe("FoodProductFootprintController", () => {
let app: INestApplication;
let defaultProducts: FoodProduct[];
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
await seedTestCarbonEmissionFactors();
const foodProductService = new FoodProductService(dataSource.getRepository(FoodProduct), dataSource.getRepository(CarbonEmissionFactor));
const foodProductFootprintService = new FoodProductFootprintService();
await foodProductFootprintService.computeAndSave(await foodProductService.initialize({
name: "ketchup",
ingredients: [
{ name: "tomato", quantity: 0.9, unit: "kg" },
{ name: "vinegar", quantity: 0.1, unit: "kg" },
],
}));
defaultProducts = await dataSource
.getRepository(FoodProduct)
.find();
});
it("GET /food-product-footprint", async () => {
return request(app.getHttpServer())
.get("/food-product-footprint")
.expect(200)
.expect(({ body }) => {
expect(body).toEqual(defaultProducts);
});
});
it("POST /food-product-footprint", async () => {
const hamCheesePizza = {
name: "hamCheesePizza",
ingredients: [
{ name: "ham", quantity: 0.1, unit: "kg" },
{ name: "cheese", quantity: 0.15, unit: "kg" },
{ name: "tomato", quantity: 0.4, unit: "kg" },
{ name: "flour", quantity: 0.7, unit: "kg" },
{ name: "oliveOil", quantity: 0.3, unit: "kg" },
],
};
return request(app.getHttpServer())
.post("/food-product-footprint")
.send(hamCheesePizza)
.expect(201)
.expect(({ body }) => {
expect(body).toMatchObject(hamCheesePizza);
expect(body.footprint).not.toBeNull();
expect(body.footprint).toBeCloseTo(0.224, 3);
});
});
});