import Ember from 'ember';
import { module, test } from 'qunit';

import QueryBuilder from 'ember-flexberry-data/query/builder';
import ODataAdapter from 'ember-flexberry-data/adapters/odata';

import startApp from '../../helpers/start-app';
import config from '../../../../dummy/config/environment';

if (config.APP.testODataService) {
  const randKey = Math.floor(Math.random() * 999);
  const baseUrl = 'http://rtc-web:8081/odatatmp/ember' + randKey;
  const app = startApp();
  const store = app.__container__.lookup('service:store');

  const adapter = ODataAdapter.create();
  Ember.set(adapter, 'host', baseUrl);

  store.reopen({
    adapterFor() {
      return adapter;
    }
  });

  module('query');

  test('adapter | integrated odata | without predicate', (assert) => {
    let builder = new QueryBuilder(store, 'ember-flexberry-dummy-suggestion');

    runTest(assert, store, builder, (data) => {
      assert.ok(data);
    });
  });

  test('adapter | integrated odata | create', (assert) => {
    assert.expect(0);
    let done = assert.async();
    Ember.run(() => {
      store.createRecord('ember-flexberry-dummy-localization', {
        name: 'Test'
      }).save().then(done);
    });
  });
}

function runTest(assert, store, builder, callback) {
  let done = assert.async();
  store.query('ember-flexberry-dummy-suggestion', builder.build()).then((data) => {
    callback(data);
    done();
  });
}

@@ -985,16 +985,32 @@
 %7C create
+ and query by id
 ', (asse
@@ -1036,17 +1036,17 @@
 .expect(
-0
+4
 );%0A    l
@@ -1071,16 +1071,17 @@
 sync();%0A
+%0A
     Embe
@@ -1099,16 +1099,29 @@
  %7B%0A     
+ let record =
  store.c
@@ -1205,20 +1205,743 @@
   %7D)
-.save().then
+;%0A%0A      record%0A        .save()%0A        .then((createdRecord) =%3E %7B%0A          let id = createdRecord.get('id');%0A          assert.ok(id);%0A%0A          // Reload created record by identifier.%0A          let builder = new QueryBuilder(store, 'ember-flexberry-dummy-localization').select('name').byId(id);%0A          return store.query('ember-flexberry-dummy-localization', builder.build());%0A        %7D, (e) =%3E console.log(e.message))%0A        .then((result) =%3E %7B%0A          assert.ok(result);%0A%0A          let firstRecord = result.get('firstObject');%0A          assert.ok(firstRecord);%0A          assert.equal(firstRecord.get('name'), 'Test');%0A          console.log(firstRecord.get('name'));%0A        %7D, (e) =%3E console.log(e.message))%0A        .finally
 (don

Add integrated OData test for creating record and loading it by ID