const models = require('vn-loopback/server/server').models;

describe('Termograph createThermograph()', () => {
    const thermographId = '99999-1';
    const model = 'DISPOSABLE';
    const temperatureFk = 'COOL';
    const warehouseId = 1;
    const ctx = {req: {accessToken: {userId: 9}}};

    it(`should create a thermograph which is saved in both thermograph and travelThermograph`, async() => {
        const tx = await models.Thermograph.beginTransaction({});

        try {
            const options = {transaction: tx};

            const createdThermograph = await models.Thermograph.createThermograph(ctx, thermographId, model, temperatureFk, warehouseId, options);

            expect(createdThermograph.id).toEqual(thermographId);
            expect(createdThermograph.model).toEqual(model);

            const createdTravelThermograpth = await models.TravelThermograph.findOne({where: {thermographFk: thermographId}}, options);

            expect(createdTravelThermograpth.warehouseFk).toEqual(warehouseId);
            expect(createdTravelThermograpth.temperatureFk).toEqual(temperatureFk);

            await tx.rollback();
        } catch (e) {
            await tx.rollback();
            throw e;
        }
    });

    it(`should throw an error when trying to created repeated thermograph`, async() => {
        const tx = await models.Thermograph.beginTransaction({});

        let error;

        try {
            const options = {transaction: tx};

            await models.Thermograph.createThermograph(ctx, thermographId, model, temperatureFk, warehouseId, options);
            await models.Thermograph.createThermograph(ctx, thermographId, model, temperatureFk, warehouseId, options);

            await tx.rollback();
        } catch (e) {
            await tx.rollback();
            error = e;
        }

        expect(error.message).toBe('This thermograph id already exists');
    });
});