<commit_before>// Test an API with all the possible filed types and simple filterings (no deep filtering, no relations)

const { registerAndLogin } = require('../../../test/helpers/auth');
const createModelsUtils = require('../../../test/helpers/models');
const { createAuthRequest } = require('../../../test/helpers/request');

let rq;
let modelsUtils;
let data = {
  products: [],
};

// complete list of existing fields and tests
const product = {
  attributes: [
    {
      name: 'name',
      params: {
        type: 'string',
      },
    },
    {
      name: 'description',
      params: {
        type: 'text',
      },
    },
    {
      name: 'price',
      params: {
        type: 'float',
      },
    },
    {
      name: 'decimal_field',
      params: {
        type: 'decimal',
      },
    },
    {
      name: 'rank',
      params: {
        type: 'integer',
      },
    },
    {
      name: 'big_rank',
      params: {
        type: 'biginteger',
      },
    },
  ],
  connection: 'default',
  name: 'product',
  description: '',
  collectionName: '',
};

const productFixtures = [
  {
    name: 'Product 1',
    description: 'Product description',
    price: 10.99,
    decimal_field: 42.43,
    rank: 42,
    big_rank: 345678912983,
  },
  {
    name: 'Product 2',
    description: 'Product description 2',
    price: 28.31,
    decimal_field: 91.22,
    rank: 82,
    big_rank: 926371623421,
  },
  {
    name: 'Product 3',
    description: 'Product description 3',
    price: 28.31,
    decimal_field: 12.22,
    rank: 91,
    big_rank: 926372323421,
  },
  {
    name: 'Product 4',
    description: 'Product description 4',
    price: null,
    decimal_field: 12.22,
    rank: 99,
    big_rank: 999999999999,
  },
  {
    name: 'Продукт 5, Product 5',
    description: 'Опис на продукт 5',
    price: null,
    decimal_field: 142.43,
    rank: 142,
    big_rank: 345678912983,
  },
];

async function createFixtures() {
  for (let product of productFixtures) {
    const res = await rq({
      method: 'POST',
      url: '/products',
      body: product,
    });

    data.products.push(res.body);
  }
}

async function deleteFixtures() {
  for (let product of data.products) {
    await rq({
      method: 'DELETE',
      url: `/products/${product.id}`,
    });
  }
}

describe('Filtering API', () => {
  beforeAll(async () => {
    const token = await registerAndLogin();
    rq = createAuthRequest(token);

    modelsUtils = createModelsUtils({ rq });
    await modelsUtils.createModels([product]);
    await createFixtures();
  }, 60000);

  afterAll(async () => {
    await deleteFixtures();
    await modelsUtils.deleteModels(['product']);
  }, 60000);

  describe('Basic filters', () => {
    describe('Filter equals', () => {
      test('Should be the default filter', async () => {
        const res = await rq({
          method: 'GET',
          url: '/products',
          qs: {
            name: 'Product 1',
          },
        });

        expect(Array.isArray(res.body)).toBe(true);
        expect(res.body.length).toBe(1);
        expect(res.body[0]).toMatchObject(data.products[0]);
      });

      test('Should be usable with equal suffix', async () => {
        const res = await rq({
          method: 'GET',
          url: '/products',
          qs: {
            name_eq: 'Product 1',
          },
        });

        expect(Array.isArray(res.body)).toBe(true);
        expect(res.body.length).toBe(1);
        expect(res.body[0]).toMatchObject(data.products[0]);
      });

      test('Should return an empty array when no match', async () => {
        const res = await rq({
          method: 'GET',
          url: '/products',
          qs: {
            name_eq: 'Product non existant',
          },
        });

        expect(res.body).toEqual([]);
      });
    });

    describe('Filter not equals', () => {
      test('Should return an array with matching entities', async () => {
        const res = await rq({
          method: 'GET',
          url: '/products',
          qs: {
            name_ne: 'Non existent product ',
          },
        });

        expect(res.body).toEqual(
          expect.arrayContaining(
            data.products.map(o => expect.objectContaining(o))
          )
        );
      });

      test('Should return an empty array when no match', async () => {
        const res = await rq({
          method: 'GET',
          url: '/products',
          qs: {
            name_ne: 'Product 1',
          },
        });

        expect(res.body).toEqual(
          expect.not.arrayContaining([data.products[0]])
        );
      });
    });

    describe('Filter null', () => {
      test('Should return only matching items', async () => {
        const res = await rq({
          method: 'GET',
          url: '/products',
          qs: {
            price_null: true,
          },
        });

        const matching = data.products.filter(x => x.price === null);
        expect(Array.isArray(res.body)).toBe(true);
        expect(res.body.length).toBe(matching.length);
        expect(res.body).toMatchObject(matching);
        expect(res.body).toEqual(expect.arrayContaining(matching));
      });

      test('Should return three matches', async () => {
        const res = await rq({
          method: 'GET',
          url: '/products',
          qs: {
            price_null: false,
          },
        });

        expect(Array.isArray(res.body)).toBe(true);
        expect(res.body.length).toBe(3);<commit_msg>Fixing e2e tests<commit_after>// Test an API with all the possible filed types and simple filterings (no deep filtering, no relations)

const { registerAndLogin } = require('../../../test/helpers/auth');
const createModelsUtils = require('../../../test/helpers/models');
const { createAuthRequest } = require('../../../test/helpers/request');

let rq;
let modelsUtils;
let data = {
  products: [],
};

// complete list of existing fields and tests
const product = {
  attributes: [
    {
      name: 'name',
      params: {
        type: 'string',
      },
    },
    {
      name: 'description',
      params: {
        type: 'text',
      },
    },
    {
      name: 'price',
      params: {
        type: 'float',
      },
    },
    {
      name: 'decimal_field',
      params: {
        type: 'decimal',
      },
    },
    {
      name: 'rank',
      params: {
        type: 'integer',
      },
    },
    {
      name: 'big_rank',
      params: {
        type: 'biginteger',
      },
    },
  ],
  connection: 'default',
  name: 'product',
  description: '',
  collectionName: '',
};

const productFixtures = [
  {
    name: 'Product 1',
    description: 'Product description',
    price: 10.99,
    decimal_field: 42.43,
    rank: 42,
    big_rank: 345678912983,
  },
  {
    name: 'Product 2',
    description: 'Product description 2',
    price: 28.31,
    decimal_field: 91.22,
    rank: 82,
    big_rank: 926371623421,
  },
  {
    name: 'Product 3',
    description: 'Product description 3',
    price: 28.31,
    decimal_field: 12.22,
    rank: 91,
    big_rank: 926372323421,
  },
  {
    name: 'Product 4',
    description: 'Product description 4',
    price: null,
    decimal_field: 12.22,
    rank: 99,
    big_rank: 999999999999,
  },
  {
    name: 'Продукт 5, Product 5',
    description: 'Опис на продукт 5',
    price: null,
    decimal_field: 142.43,
    rank: 142,
    big_rank: 345678912983,
  },
];

async function createFixtures() {
  for (let product of productFixtures) {
    const res = await rq({
      method: 'POST',
      url: '/products',
      body: product,
    });

    data.products.push(res.body);
  }
}

async function deleteFixtures() {
  for (let product of data.products) {
    await rq({
      method: 'DELETE',
      url: `/products/${product.id}`,
    });
  }
}

describe('Filtering API', () => {
  beforeAll(async () => {
    const token = await registerAndLogin();
    rq = createAuthRequest(token);

    modelsUtils = createModelsUtils({ rq });
    await modelsUtils.createModels([product]);
    await createFixtures();
  }, 60000);

  afterAll(async () => {
    await deleteFixtures();
    await modelsUtils.deleteModels(['product']);
  }, 60000);

  describe('Basic filters', () => {
    describe('Filter equals', () => {
      test('Should be the default filter', async () => {
        const res = await rq({
          method: 'GET',
          url: '/products',
          qs: {
            name: 'Product 1',
          },
        });

        expect(Array.isArray(res.body)).toBe(true);
        expect(res.body.length).toBe(1);
        expect(res.body[0]).toMatchObject(data.products[0]);
      });

      test('Should be usable with equal suffix', async () => {
        const res = await rq({
          method: 'GET',
          url: '/products',
          qs: {
            name_eq: 'Product 1',
          },
        });

        expect(Array.isArray(res.body)).toBe(true);
        expect(res.body.length).toBe(1);
        expect(res.body[0]).toMatchObject(data.products[0]);
      });

      test('Should return an empty array when no match', async () => {
        const res = await rq({
          method: 'GET',
          url: '/products',
          qs: {
            name_eq: 'Product non existant',
          },
        });

        expect(res.body).toEqual([]);
      });
    });

    describe('Filter not equals', () => {
      test('Should return an array with matching entities', async () => {
        const res = await rq({
          method: 'GET',
          url: '/products',
          qs: {
            name_ne: 'Non existent product ',
          },
        });

        expect(res.body).toEqual(
          expect.arrayContaining(
            data.products.map(o => expect.objectContaining(o))
          )
        );
      });

      test('Should return an empty array when no match', async () => {
        const res = await rq({
          method: 'GET',
          url: '/products',
          qs: {
            name_ne: 'Product 1',
          },
        });

        expect(res.body).toEqual(
          expect.not.arrayContaining([data.products[0]])
        );
      });
    });

    describe('Filter null', () => {
      test('Should return only matching items', async () => {
        const res = await rq({
          method: 'GET',
          url: '/products',
          qs: {
            price_null: true,
          },
        });

        const matching = data.products.filter(x => x.price === null);
        res.body.sort((a, b) => (a.id > b.id ? 1 : -1));
        expect(Array.isArray(res.body)).toBe(true);
        expect(res.body.length).toBe(matching.length);
        expect(res.body).toMatchObject(matching);
        expect(res.body).toEqual(expect.arrayContaining(matching));
      });

      test('Should return three matches', async () => {
        const res = await rq({
          method: 'GET',
          url: '/products',
          qs: {
            price_null: false,
          },
        });

        expect(Array.isArray(res.body)).toBe(true);
        expect(res.body.length).toBe(3);
{'fraction_non_alphanumeric': 0.158540989526905, 'fraction_numerical': 0.02464788732394366, 'mean_word_length': 1.9073490813648295, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2132999', 'n_tokens_mistral': 3584, 'n_tokens_neox': 3374, 'n_words': 771}