From c9b730310059f8ce6f909909a2afd7067bc187de Mon Sep 17 00:00:00 2001 From: Sam Selikoff Date: Tue, 7 Apr 2015 00:19:59 -0400 Subject: [PATCH] [closes #68] db.find takes an array of ids --- addon/schema.js | 2 +- tests/unit/db-test.js | 6 ++++++ tests/unit/schema-test.js | 41 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/addon/schema.js b/addon/schema.js index e7a779216..d3a316ea7 100644 --- a/addon/schema.js +++ b/addon/schema.js @@ -63,7 +63,7 @@ export default function(db) { this._find = function(type, ids) { var collection = pluralize(type); - if (!db[collection]) { + if (db[collection]) { return null; } var attrs = db[collection].find(ids); diff --git a/tests/unit/db-test.js b/tests/unit/db-test.js index ae175d579..f18117638 100644 --- a/tests/unit/db-test.js +++ b/tests/unit/db-test.js @@ -149,6 +149,12 @@ test('returns a record whose id is a string that start with numbers', function(a assert.deepEqual(contact, {id: '123-456', name: 'Epona'}); }); +test('returns multiple record that matche an arrya of ids', function(assert) { + var contacts = db.contacts.find([1, 2]); + + assert.deepEqual(contacts, [{id: 1, name: 'Zelda'}, {id: 2, name: 'Link'}]); +}); + module('mirage:db#where', { beforeEach: function() { diff --git a/tests/unit/schema-test.js b/tests/unit/schema-test.js index 2c35fce75..14a11b007 100644 --- a/tests/unit/schema-test.js +++ b/tests/unit/schema-test.js @@ -9,7 +9,7 @@ test('it can be instantiated', function(assert) { assert.ok(schema); }); -test('it can create models', function(assert) { +test('it can create registered models', function(assert) { var schema = new Schema(new Db()); var User = Model.extend(); @@ -22,6 +22,14 @@ test('it can create models', function(assert) { assert.deepEqual(user.attrs, {id: 1, name: 'Link'}); }); +test('it cannot create models that havnet been registered', function(assert) { + var schema = new Schema(new Db()); + + assert.throws(function() { + schema.user.create({name: 'Link'}); + }); +}); + test('it can return all models', function(assert) { var db = new Db(); db.createCollection('users'); @@ -38,7 +46,20 @@ test('it can return all models', function(assert) { assert.deepEqual(users[1].attrs, {id: 2, name: 'Zelda'}); }); -test('it can find models', function(assert) { +test('it returns an empty array when no models exist', function(assert) { + var db = new Db(); + db.createCollection('users'); + var schema = new Schema(db); + + var User = Model.extend(); + schema.register('user', User); + + var users = schema.user.all(); + + assert.equal(users.length, 0); +}); + +test('it can find a model by id', function(assert) { var db = new Db(); db.createCollection('users'); db.users.insert([{id: 1, name: 'Link'}, {id: 2, name: 'Zelda'}]); @@ -53,3 +74,19 @@ test('it can find models', function(assert) { assert.deepEqual(zelda.attrs, {id: 2, name: 'Zelda'}); }); +test('it can find multiple models by ids', function(assert) { + var db = new Db(); + db.createCollection('users'); + db.users.insert([{id: 1, name: 'Link'}, {id: 2, name: 'Zelda'}]); + var schema = new Schema(db); + + var User = Model.extend(); + schema.register('user', User); + + var users = schema.user.find([1, 2]); + + assert.ok(users[0] instanceof User); + assert.equal(users.length, 2); + assert.deepEqual(users[1].attrs, {id: 2, name: 'Zelda'}); +}); +