require('#{APPNAME}/core');
require('#{APPNAME}/couchdb_adapter');

Ember.ENV.TESTING = true;

var get = Ember.get;
var set = Ember.set;

var adapter;
var store;
var ajaxUrl;
var ajaxType;
var ajaxHash;

var person;
var Person;

var expectUrl = function(url, desc) {
  equal(ajaxUrl, url, "the URL is " + desc);
};

var expectType = function(type) {
  equal(type, ajaxType, "the HTTP method is " + type);
};

var expectData = function(hash) {
  deepEqual(hash, ajaxHash.data, "the hash was passed along");
};

var expectState = function(state, value, p) {
  p = p || person;

  if (value === undefined) {
    value = true;
  }

  var flag = "is" + state.charAt(0).toUpperCase() + state.substr(1);
  equal(get(p, flag), value, "the person is " + (value === false ? "not ": "") + state);
};

module("CouchDBAdapter", {
  setup: function() {
    adapter = CouchDBAdapter.create({
      ajax: function(url, type, hash) {
        var success = hash.success,
        self = this;

        ajaxUrl = url;
        ajaxType = type;
        ajaxHash = hash;

        if (success) {
          hash.success = function(json) {
            success.call(self, json);
          };
        }
      }
    });

    store = DS.Store.create({
      adapter: adapter
    });

    Person = DS.Model.extend({
      name: DS.attr('string')
    });
  },

  teardown: function() {
    adapter.destroy();
    store.destroy();
  }
});

test("is defined",
function() {
  ok(CouchDBAdapter !== undefined, "CouchDBAdapter is undefined");
});

test("is a subclass of DS.Adapter",
function() {
  ok(DS.Adapter.detect(CouchDBAdapter), "CouchDBAdapter is a subclass of DS.Adapter");
});

test("creating a person makes a POST to /db with data hash",
function() {
  person = store.createRecord(Person, {
    name: 'Tobias Fünke'
  });

  expectState('new');
  store.commit();
  expectState('saving');

  expectUrl('/db', 'the database name');
  expectType('POST');
  expectData({
    name: "Tobias Fünke"
  });

  ajaxHash.success({
    ok: true,
    id: "abc",
    rev: "1-abc"
  });
  expectState('saving', false);

  equal(person, store.find(Person, 'abc'), "it's possible to find the person by the returned ID");
  equal(get(person, '_rev'), '1-abc', "the revision is stored on the data");
});

test("updating a person makes a PUT to /db/:id with data hash",
function() {
  store.load(Person, {
    id: 'abc',
    rev: '1-abc',
    name: 'Tobias Fünke'
  });

  person = store.find(Person, 'abc');

  expectState('new', false);
  expectState('loaded');
  expectState('dirty', false);

  set(person, 'name', 'Nelly Fünke');

  expectState('dirty');
  store.commit();
  expectState('saving');

  expectUrl('/db/abc', 'the database name with the record ID');
  expectType('PUT');
  expectData({
    "_id": "abc",
    "_rev": "1-abc",
    name: "Tobias Fünke"
  });

  ajaxHash.success({
    ok: true,
    id: 'abc',
    rev: '2-def'
  });
  expectState('saving', false);

  equal(person, store.find(Person, 'abc'), "the same person is retrieved by the same ID");
  equal(get(person, 'name'), 'Nelly Fünke', "the data is preserved");
  equal(get(person, '_rev'), '2-def', "the revision is updated");
});

test("deleting a person makes a DELETE to /db/:id",
function() {
  store.load(Person, {
    id: 'abc',
    rev: '1-abc',
    name: "Tobias Fünke"
  });

  person = store.find(Person, "abc");

  expectState('new', false);
  expectState('loaded');
  expectState('dirty', false);

  person.deleteRecord();

  expectState('dirty');
  expectState('deleted');
  store.commit();
  expectState('saving');

  expectUrl("/db/abc?rev=1-abc", "the database name with the record ID and rev as parameter");
  expectType("DELETE");

  ajaxHash.success({
    ok: true,
    rev: '2-abc'
  });
  expectState('deleted');
});

@@ -3762,12 +3762,679 @@
 eted');%0A%7D);%0A
+%0Atest(%22bulkCommit=true makes a POST to /db/_bulk_docs%22,%0Afunction() %7B%0A  person = store.createRecord(Person, %7B%0A    name: 'Tobias F%C3%BCnke'%0A  %7D);%0A%0A  adapter.set('bulkCommit', true);%0A%0A  expectState('new');%0A  store.commit();%0A  expectState('saving');%0A%0A  expectUrl('/db/_bulk_docs', 'the database name');%0A  expectType('POST');%0A  expectData(%7B%0A    docs: %5B%7B%0A      name: %22Tobias F%C3%BCnke%22%0A    %7D%5D%0A  %7D%0A  );%0A%0A  ajaxHash.success(%5B%7B%0A    %22id%22: 'abc',%0A    %22rev%22: '1-abc',%0A  %7D%5D);%0A  expectState('saving', false);%0A%0A  equal(person, store.find(Person, 'abc'), %22it's possible to find the person by the returned ID%22);%0A  equal(get(person, '_rev'), '1-abc', %22the revision is stored on the data%22);%0A%7D);

Add basic test when bulkCommit is set to true