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

describe('AgencyTerm filter()', () => {
    const authUserId = 9;
    const today = Date.vnNew();
    today.setHours(2, 0, 0, 0);

    it('should return all results matching the filter', async() => {
        const tx = await models.AgencyTerm.beginTransaction({});

        try {
            const options = {transaction: tx};
            const filter = {};
            const ctx = {req: {accessToken: {userId: authUserId}}};

            const agencyTerms = await models.AgencyTerm.filter(ctx, filter, options);
            const firstAgencyTerm = agencyTerms[0];

            expect(firstAgencyTerm.routeFk).toEqual(1);
            expect(agencyTerms.length).toEqual(5);

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

    it('should return results matching "search" searching by integer', async() => {
        let ctx = {
            args: {
                search: 1,
            }
        };

        let result = await app.models.AgencyTerm.filter(ctx);

        expect(result.length).toEqual(1);
        expect(result[0].routeFk).toEqual(1);
    });

    it('should return results matching "search" searching by string', async() => {
        let ctx = {
            args: {
                search: 'Plants SL',
            }
        };

        let result = await app.models.AgencyTerm.filter(ctx);

        expect(result.length).toEqual(2);
    });

    it('should return results matching "from" and "to"', async() => {
        const tx = await models.Buy.beginTransaction({});
        const options = {transaction: tx};

        try {
            const from = Date.vnNew();
            from.setHours(0, 0, 0, 0);

            const to = Date.vnNew();
            to.setHours(23, 59, 59, 999);

            const ctx = {
                args: {
                    from: from,
                    to: to
                }
            };

            const results = await models.AgencyTerm.filter(ctx, options);

            expect(results.length).toBe(5);

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

    it('should return results matching "agencyModeFk"', async() => {
        let ctx = {
            args: {
                agencyModeFk: 1,
            }
        };

        let result = await app.models.AgencyTerm.filter(ctx);

        expect(result.length).toEqual(1);
        expect(result[0].routeFk).toEqual(1);
    });

    it('should return results matching "agencyFk"', async() => {
        let ctx = {
            args: {
                agencyFk: 2,
            }
        };

        let result = await app.models.AgencyTerm.filter(ctx);

        expect(result.length).toEqual(1);
        expect(result[0].routeFk).toEqual(2);
    });
});