<commit_before>import Dexie from 'npm:dexie';
import { module, test } from 'qunit';

import QueryBuilder from 'ember-flexberry-data/query/builder';
import IndexedDbAdapter from 'ember-flexberry-data/query/indexeddb-adapter';
import FilterOperator from 'ember-flexberry-data/query/filter-operator';
import { SimplePredicate, ComplexPredicate, StringPredicate, DetailPredicate } from 'ember-flexberry-data/query/predicate';
import Condition from 'ember-flexberry-data/query/condition';

import startApp from '../../helpers/start-app';

const app = startApp();
const store = app.__container__.lookup('service:store');
const dexie = app.__container__.lookup('service:dexie');
const databasePrefix = 'testDB2';
const modelName = 'employee';
const schema = (dbName) => {
  let object = {};
  object[dbName] = {
    1: {
      employee: 'id,Age,Name,Surname,CountryName,Price,Country,Creator,Manager,*Tags',
      creator: 'id,Name,Age',
      country: 'id,Name',
      tag: 'id,Name,Creator'
    },
  };
  return object;
};

module('query');

test('adapter | indexeddb | without predicate', (assert) => {
  let data = {
    employee: [
      { id: 1 },
      { id: 2 },
      { id: 3 },
    ],
  };

  let builder = new QueryBuilder(store, modelName);

  executeTest(data, builder.build(), assert, (result) => {
    assert.ok(result.data);
    assert.equal(result.data.length, 3);
    assert.equal(result.data[0].id, 1);
    assert.equal(result.data[1].id, 2);
    assert.equal(result.data[2].id, 3);
  });
});

test('adapter | indexeddb | simple predicate | eq', (assert) => {
  let data = {
    employee: [
      { id: 1, Name: 'A' },
      { id: 2, Name: 'B' },
      { id: 3, Name: 'B' },
    ],
  };

  let builder = new QueryBuilder(store, modelName).where('Name', FilterOperator.Eq, 'B');

  executeTest(data, builder.build(), assert, (result) => {
    assert.ok(result.data);
    assert.equal(result.data.length, 2);
    assert.equal(result.data[0].id, 2);
    assert.equal(result.data[1].id, 3);
  });
});

test('adapter | indexeddb | simple predicate | eq | null', (assert) => {
  let data = {
    employee: [
      { id: 1, Name: 'A' },
      { id: 2, Name: null },
      { id: 3, Name: 'B' },
    ],
  };

  let builder = new QueryBuilder(store, modelName).where('Name', FilterOperator.Eq, null);

  executeTest(data, builder.build(), assert, (result) => {
    assert.ok(result.data);
    assert.equal(result.data.length, 1);
    assert.equal(result.data[0].id, 2);
  });
});

test('adapter | js | simple predicate | eq | master field', function (assert) {
  let data = {
    employee: [
      { id: 1, Manager: 4 },
      { id: 2 },
      { id: 3, Manager: 5 },
      { id: 4, Name: 'X' },
      { id: 5, Name: 'Y' },
    ],
  };

  let builder = new QueryBuilder(store, modelName).where('Manager.Name', FilterOperator.Eq, 'Y');

  executeTest(data, builder.build(), assert, (result) => {
    assert.ok(result.data);
    assert.equal(result.data.length, 1);
    assert.equal(result.data[0].id, 3);
  });
});

test('adapter | indexeddb | simple predicate | neq', (assert) => {
  let data = {
    employee: [
      { id: 1, Name: 'A', Surname: 'X', Age: 10 },
      { id: 2, Name: 'B', Surname: 'Y', Age: 11 },
      { id: 3, Name: 'C', Surname: 'Z', Age: 12 },
    ],<commit_msg>Test indexeddb-adapter add filter by master pk<commit_after>import Dexie from 'npm:dexie';
import { module, test } from 'qunit';

import QueryBuilder from 'ember-flexberry-data/query/builder';
import IndexedDbAdapter from 'ember-flexberry-data/query/indexeddb-adapter';
import FilterOperator from 'ember-flexberry-data/query/filter-operator';
import { SimplePredicate, ComplexPredicate, StringPredicate, DetailPredicate } from 'ember-flexberry-data/query/predicate';
import Condition from 'ember-flexberry-data/query/condition';

import startApp from '../../helpers/start-app';

const app = startApp();
const store = app.__container__.lookup('service:store');
const dexie = app.__container__.lookup('service:dexie');
const databasePrefix = 'testDB2';
const modelName = 'employee';
const schema = (dbName) => {
  let object = {};
  object[dbName] = {
    1: {
      employee: 'id,Age,Name,Surname,CountryName,Price,Country,Creator,Manager,*Tags',
      creator: 'id,Name,Age',
      country: 'id,Name',
      tag: 'id,Name,Creator'
    },
  };
  return object;
};

module('query');

test('adapter | indexeddb | without predicate', (assert) => {
  let data = {
    employee: [
      { id: 1 },
      { id: 2 },
      { id: 3 },
    ],
  };

  let builder = new QueryBuilder(store, modelName);

  executeTest(data, builder.build(), assert, (result) => {
    assert.ok(result.data);
    assert.equal(result.data.length, 3);
    assert.equal(result.data[0].id, 1);
    assert.equal(result.data[1].id, 2);
    assert.equal(result.data[2].id, 3);
  });
});

test('adapter | indexeddb | simple predicate | eq', (assert) => {
  let data = {
    employee: [
      { id: 1, Name: 'A' },
      { id: 2, Name: 'B' },
      { id: 3, Name: 'B' },
    ],
  };

  let builder = new QueryBuilder(store, modelName).where('Name', FilterOperator.Eq, 'B');

  executeTest(data, builder.build(), assert, (result) => {
    assert.ok(result.data);
    assert.equal(result.data.length, 2);
    assert.equal(result.data[0].id, 2);
    assert.equal(result.data[1].id, 3);
  });
});

test('adapter | indexeddb | simple predicate | eq | null', (assert) => {
  let data = {
    employee: [
      { id: 1, Name: 'A' },
      { id: 2, Name: null },
      { id: 3, Name: 'B' },
    ],
  };

  let builder = new QueryBuilder(store, modelName).where('Name', FilterOperator.Eq, null);

  executeTest(data, builder.build(), assert, (result) => {
    assert.ok(result.data);
    assert.equal(result.data.length, 1);
    assert.equal(result.data[0].id, 2);
  });
});

test('adapter | indexeddb | simple predicate | eq | master pk', function (assert) {
  let data = {
    employee: [
      { id: 1, Manager: 3 },
      { id: 2, Manager: 3 },
      { id: 3 },
    ],
  };

  let builder = new QueryBuilder(store, modelName).where('Manager', FilterOperator.Eq, 3);

  executeTest(data, builder.build(), assert, (result) => {
    assert.ok(result.data);
    assert.equal(result.data.length, 2);
    assert.equal(result.data[0].id, 1);
    assert.equal(result.data[1].id, 2);
  });
});

test('adapter | indexeddb | simple predicate | eq | master field', function (assert) {
  let data = {
    employee: [
      { id: 1, Manager: 4 },
      { id: 2 },
      { id: 3, Manager: 5 },
      { id: 4, Name: 'X' },
      { id: 5, Name: 'Y' },
    ],
  };

  let builder = new QueryBuilder(store, modelName).where('Manager.Name', FilterOperator.Eq, 'Y');

  executeTest(data, builder.build(), assert, (result) => {
    assert.ok(result.data);
    assert.equal(result.data.length, 1);
    assert.equal(result.data[0].id, 3);
  });
});

test('adapter | indexeddb | simple predicate | neq', (assert) => {
  let data = {
    employee: [
      { id: 1, Name: 'A', Surname: 'X', Age: 10 },
      { id: 2, Name: 'B', Surname: 'Y', Age: 11 },
      { id: 3, Name: 'C', Surname: 'Z', Age: 12 },
    ],
{'fraction_non_alphanumeric': 0.18627175743964064, 'fraction_numerical': 0.014177428411005054, 'mean_word_length': 3.2664670658682633, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '4728615', 'n_tokens_mistral': 2544, 'n_tokens_neox': 2408, 'n_words': 560}