<commit_before>var assert = require('assert');
var Backbone = require('backbone');
var Model = require('../').Model;
var Collection = require('../').Collection;
var Db = require('backbone-db');
var debug = require('debug')('deferred');

var db = new Db("test.model");

var MyModel = Model.extend({
  db: db,
  sync: Db.sync,
  url: function() {
    if(this.isNew()) {
      return '/mymodels';
    } else {
      return '/mymodels/'+this.get(this.idAttribute)
    }
  }
});

describe('#Model', function() {
  it('should have deferred .save', function(t) {
    var m = new MyModel({id:1});
    m.save().then(function() {
      t();
    }, function() {
      assert(false);
    });
  });

  it('should have deferred .save and .fetch', function(t) {
    var m = new MyModel({id:2,"test":"a"});
    m.save().then(function(a) {
      var m2 = new MyModel({id:2});
      m2.fetch().then(function(model) {
        assert(model.get("test") == "a");
        t();
      }, function() {
        assert(false);
      });
    }, function() {
      assert(false);
    });
  });

  it('Should maintain classic behaviour', function(t) {
    var m = new MyModel({id:3,"test":"a"});
    m.save({variable:"123"},{success: function() {
      var m2 = new MyModel({id:3});
      m2.fetch({success: function() {
        assert.equal(m2.get("variable"),"123");
        assert.equal(m2.get("test"),"a")
        var maa = new MyModel({id:123123});

        maa.fetch({success: function(asd) {
          assert(false, "success called while fetching non-existing key")
        }, error: function(err) {
          t();
        }});
      },
      error: function(err) {
        assert.ok(false);
      }});
    }});
  });

  it('Should create empty model and accept variables in .save', function(t) {
    var m = new MyModel();
    m.save({id:123}).then(function() {
      assert.equal(m.get("id"), 123);
      t();
    }, function() {
      assert.ok(false)
    });
  });

  it('Should be destroyable.', function(t) {
    var m = new MyModel();
    m.save({id:123,asd:"asd"}).then(function() {
      assert.equal(m.get("id"), 123);
      m.destroy().then(function() {
        t();
      }).otherwise(function() {
        assert.ok(false);
      }).otherwise(t);
    }, function() {
      assert.ok(false);
    }).otherwise(t);
  });

  it('Should reject .save() promise on failed validation', function(done) {
    var m = new MyModel();
    m.validate = function() {
      return new Error('failed validating');
    };
    m.save({id:123, variable:"test"}).then(function() {
      assert.fail();
    }, function() {
      done();
    }).otherwise(done);
  });
});<commit_msg>Added test for error handling when fetching non-existent model<commit_after>var assert = require('assert');
var Backbone = require('backbone');
var Model = require('../').Model;
var Collection = require('../').Collection;
var Db = require('backbone-db');
var debug = require('debug')('deferred');

var db = new Db("test.model");

var MyModel = Model.extend({
  db: db,
  sync: Db.sync,
  url: function() {
    if(this.isNew()) {
      return '/mymodels';
    } else {
      return '/mymodels/'+this.get(this.idAttribute)
    }
  }
});

describe('#Model', function() {
  it('should have deferred .save', function(t) {
    var m = new MyModel({id:1});
    m.save().then(function() {
      t();
    }, function() {
      assert(false);
    });
  });

  it('should have deferred .save and .fetch', function(t) {
    var m = new MyModel({id:2,"test":"a"});
    m.save().then(function(a) {
      var m2 = new MyModel({id:2});
      m2.fetch().then(function(model) {
        assert(model.get("test") == "a");
        t();
      }, function() {
        assert(false);
      });
    }, function() {
      assert(false);
    });
  });

  it('Should maintain classic behaviour', function(t) {
    var m = new MyModel({id:3,"test":"a"});
    m.save({variable:"123"},{success: function() {
      var m2 = new MyModel({id:3});
      m2.fetch({success: function() {
        assert.equal(m2.get("variable"),"123");
        assert.equal(m2.get("test"),"a")
        var maa = new MyModel({id:123123});

        maa.fetch({success: function(asd) {
          assert(false, "success called while fetching non-existing key")
        }, error: function(err) {
          t();
        }});
      },
      error: function(err) {
        assert.ok(false);
      }});
    }});
  });

  it('Should create empty model and accept variables in .save', function(t) {
    var m = new MyModel();
    m.save({id:123}).then(function() {
      assert.equal(m.get("id"), 123);
      t();
    }, function() {
      assert.ok(false)
    });
  });

  it('Should be destroyable.', function(t) {
    var m = new MyModel();
    m.save({id:123,asd:"asd"}).then(function() {
      assert.equal(m.get("id"), 123);
      m.destroy().then(function() {
        m.fetch().then(function() {
            assert.ok(false);
          },
          function(err) {
            assert(err instanceof(Error), 'It should return an error when not found');
            t();
          }).otherwise(t);
      }).otherwise(function() {
        assert.ok(false);
      }).otherwise(t);
    }, function() {
      assert.ok(false);
    }).otherwise(t);
  });

  it('Should reject .save() promise on failed validation', function(done) {
    var m = new MyModel();
    m.validate = function() {
      return new Error('failed validating');
    };
    m.save({id:123, variable:"test"}).then(function() {
      assert.fail();
    }, function() {
      done();
    }).otherwise(done);
  });
});
{'fraction_non_alphanumeric': 0.20438218390804597, 'fraction_numerical': 0.013649425287356323, 'mean_word_length': 2.5675848814862268, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, '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': '4289560', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1784, 'n_words': 376}