Skip to content

Commit

Permalink
[closes #68] db.find takes an array of ids
Browse files Browse the repository at this point in the history
  • Loading branch information
samselikoff committed May 16, 2015
1 parent 0f3aa49 commit c9b7303
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion addon/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/db-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
41 changes: 39 additions & 2 deletions tests/unit/schema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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');
Expand All @@ -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'}]);
Expand All @@ -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'});
});

0 comments on commit c9b7303

Please sign in to comment.