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

var App;

module('CouchDB Adapter', {
  setup: function() {
    App = startApp();
    this.store = App.__container__.lookup('store:main');
    this.db = this.store.adapterFor({ typeKey: 'post' }).get('db');
  },
  teardown: function() {
    stop();
    this.db.destroy(start);
    Ember.run(App, 'destroy');
  }
});

test('Store#find/2', function() {
  var _this = this,
      name = "Rails is omakase";

  stop();
  new Ember.RSVP.Promise(function(resolve, reject) {
    _this.db.put({ _id: 'post::1', name: name }, function(err, resp) {
      if (err) { reject(err); }
      else { resolve(resp); }
    });
  })
  .then(function() {
    return _this.store.find('post', 1);
  })
  .then(function(record) {
    equal(record.get('id'), 1, "Record ID is correct");
    equal(record.get('name'), name, "Record data is correct");
  })
  .catch(function(err) {
    ok(false, "Error: " + err);
  })
  .finally(start);
});

test('Model#save (new record)', function() {
  var _this = this,
      record = Ember.run(function() {
        return _this.store.createRecord('post', { id: 1, name: "Rails is omakase" });
      });

  stop();
  Ember.run(function() {
    record.save().then(function() {
      equal(record.get("isNew"), false, "Record should be saved");
      return _this.db.get('post::1').then(function(doc) {
        ok(doc, "Document should exist in database");
        equal(record.get('data.rev'), doc._rev, "Revision should be set on Record data");
        equal(doc.name, "Rails is omakase", "Data should be set");
      }, function(err) {
        ok(false, "Document should exist in database, message: " + err.message);
      });
    }).catch(function(err) {
      ok(false, "Error: " + err);
    }).finally(start);
  });
});

test('Model#save (existing record)', function() {
  var _this = this,
      names = ["Rails is omakase",
               "Yum sushi"],
      record;

  stop();
  new Ember.RSVP.Promise(function(resolve, reject) {
    _this.db.put({ _id: 'post::1' }, function(err, resp) {
      if (err) { reject(err); }
      else { resolve(resp); }
    });
  })
  .then(function() {
    return _this.store.find('post', 1);
  })
  .then(function(r) {
    record = r;
    record.set('name', names[0]);
    return record.save();
  })
  .then(function() {
    return _this.db.get('post::1');
  })
  .then(function(doc) {
    equal(doc.name, names[0], "Record data is saved on Document");

    record.set('name', names[1]);
    return record.save();
  })
  .then(function() {
    return _this.db.get('post::1');
  })
  .then(function(doc) {
    equal(doc.name, names[1], "Revision is updated on Record");
  })
  .catch(function(err) {
    ok(false, "Error: " + err);
  })
  .finally(start);
});

@@ -1008,18 +1008,14 @@
 ve (
-new record
+create
 )', 
@@ -1043,24 +1043,57 @@
 his = this,%0A
+      name = %22Rails is omakase%22,%0A
       record
@@ -1181,34 +1181,20 @@
 , name: 
-%22Rails is omakase%22
+name
  %7D);%0A   
@@ -1235,20 +1235,23 @@
 tion() %7B
-%0A   
+ return
  record.
@@ -1256,16 +1256,23 @@
 d.save()
+; %7D)%0A  
 .then(fu
@@ -1278,26 +1278,24 @@
 unction() %7B%0A
-  
     equal(re
@@ -1347,26 +1347,24 @@
 aved%22);%0A    
-  
 return _this
@@ -1381,16 +1381,25 @@
 ost::1')
+;%0A  %7D)%0A  
 .then(fu
@@ -1408,28 +1408,24 @@
 tion(doc) %7B%0A
-    
     ok(doc, 
@@ -1458,28 +1458,24 @@
 database%22);%0A
-    
     equal(re
@@ -1548,20 +1548,16 @@
 data%22);%0A
-    
     equa
@@ -1568,34 +1568,20 @@
 c.name, 
-%22Rails is omakase%22
+name
 , %22Data 
@@ -1603,128 +1603,13 @@
 ;%0A  
-    %7D, function(err) %7B%0A        ok(false, %22Document should exist in database, message: %22 + err.message);%0A      %7D);%0A    %7D)
+%7D)%0A  
 .cat
@@ -1623,26 +1623,24 @@
 tion(err) %7B%0A
-  
     ok(false
@@ -1661,18 +1661,16 @@
 err);%0A  
- 
- 
 %7D).final
@@ -1680,22 +1680,16 @@
 start);%0A
-  %7D);%0A
 %7D);%0A%0Ates
@@ -1707,23 +1707,14 @@
 ve (
-existing record
+update
 )', 

Refactor 'Model#save (create)' test