Skip to content

Commit f839559

Browse files
committed
Added collections and a specific Users collection
1 parent f76c75d commit f839559

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

collections/users.coffee

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
StoredCollection = require '../data/storedCollection'
2+
3+
class Users extends StoredCollection
4+
constructor: () -> super
5+
6+
find: () ->
7+
if arguments.length is 1
8+
criteria = {}
9+
[callback] = arguments
10+
11+
if arguments.length is 2
12+
[criteria, callback] = arguments
13+
14+
super('users', criteria, callback)
15+
16+
all: (callback) -> @find(callback)
17+
18+
module.exports = Users

data/storedCollection.coffee

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
InMemoryDataAdapter = require './adapters/inMemory'
2+
3+
class StoredCollection
4+
constructor: () ->
5+
@_adapter = new InMemoryDataAdapter()
6+
7+
find: (collection, criteria, callback) ->
8+
@_adapter.find(collection, criteria, callback)
9+
10+
module.exports = StoredCollection

test/collections/users_test.coffee

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
vows = require 'vows'
2+
should = require 'should'
3+
Users = require '../../collections/users'
4+
5+
vows.describe('Users (collections/users)').addBatch(
6+
'when find() is called with just a callback': {
7+
topic: () -> getUsers('test1').find(@callback)
8+
9+
'it should call find() in \'users\' collection on base': (err, items) ->
10+
callsToFind.test1.should.have.length(1)
11+
callsToFind.test1[0].should.have.length(3)
12+
callsToFind.test1[0][0].should.equal('users')
13+
14+
'it should call find() with empty criteria on base': (err, items) ->
15+
callsToFind.test1[0][1].should.eql({})
16+
}
17+
18+
'when find() is called with criteria and a callback': {
19+
topic: (users) -> getUsers('test2').find({ 'one': true }, @callback)
20+
21+
'it should call find() in \'users\' collection on base': (err, items) ->
22+
callsToFind.test2.should.have.length(1)
23+
callsToFind.test2[0].should.have.length(3)
24+
callsToFind.test2[0][0].should.equal('users')
25+
26+
'it should call find() with criteria on base': (err, items) ->
27+
callsToFind.test2[0][1].should.eql({ 'one': true })
28+
}
29+
30+
'when all() is called': {
31+
topic: () -> getUsers('test3').all(@callback)
32+
33+
'it should call find() in \'users\' collection on base': (err, items) ->
34+
callsToFind.test3.should.have.length(1)
35+
callsToFind.test3[0].should.have.length(3)
36+
callsToFind.test3[0][0].should.equal('users')
37+
38+
'it should call find() with empty criteria on base': (err, items) ->
39+
callsToFind.test3[0][1].should.eql({})
40+
}
41+
42+
).export(module)
43+
44+
callsToFind = {}
45+
46+
getUsers = (key) ->
47+
callsToFind[key] = []
48+
users = new Users()
49+
users._adapter =
50+
find: (collection, criteria, callback) ->
51+
callsToFind[key].push([collection, criteria, callback])
52+
callback(null, [])
53+
return users
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
vows = require 'vows'
2+
should = require 'should'
3+
StoredCollection = require '../../data/storedCollection'
4+
5+
vows.describe('Base stored collection (data/storedCollection)').addBatch(
6+
'': {
7+
topic: () -> getStoredCollection()
8+
9+
'when find() is called': {
10+
topic: (storedCollection) ->
11+
storedCollection.find('testCollection', {
12+
'one': true
13+
}, @callback)
14+
15+
'it should call the adapter\'s find() method': (err, attributes) ->
16+
callsToFind.should.have.length(1)
17+
18+
'it should pass collection and attributes to the adapter': (err, attributes) ->
19+
callsToFind[0].should.have.length(3)
20+
callsToFind[0][0].should.equal('testCollection')
21+
callsToFind[0][1].should.eql({ 'one': true })
22+
callsToFind[0][2].should.equal(@callback)
23+
}
24+
}
25+
).export(module)
26+
27+
callsToFind = []
28+
29+
getStoredCollection = () ->
30+
storedCollection = new StoredCollection()
31+
storedCollection._adapter =
32+
find: (collection, criteria, callback) ->
33+
callsToFind.push([collection, criteria, callback])
34+
callback(null, criteria)
35+
return storedCollection

0 commit comments

Comments
 (0)