Skip to content
Browse files

Finished testing, everything passing

  • Loading branch information...
1 parent 85379bb commit 04f36779528de768e77c597eb1ae1da04648fdcb @pksunkara committed Jan 14, 2012
Showing with 132 additions and 19 deletions.
  1. +1 −1 README.md
  2. +17 −17 nosqlite.coffee
  3. +113 −0 test/database-test.coffee
  4. +1 −1 test/fixtures/test/bob.json
View
2 README.md
@@ -16,7 +16,7 @@ npm install nosqlite
connection = new(require('nosqlite').Connection)();
// Custom path for store
-connection = new(require('nosqlite').Connection('/path/to/store');
+connection = new(require('nosqlite').Connection)('/path/to/store');
```
### Select a database
View
34 nosqlite.coffee
@@ -44,10 +44,9 @@ nosqlite.Connection::database = (name, mode) ->
onto[k] = from[k]
onto
- satisy: (data, cond) ->
- Object.keys(cond).forEach (k) ->
- if data[k] isnt cond[k] then return false
- true
+ satisfy: (data, cond) ->
+ Object.keys(cond).every (k) ->
+ if data[k] is cond[k] then true else false
# Check if db exists
exists: (cb) ->
@@ -87,12 +86,12 @@ nosqlite.Connection::database = (name, mode) ->
# Update doc by id
put: (id, obj, cb) ->
- @get id, (err, data) ->
- data = @project JSON.parse(data), obj
+ @get id, (err, data) =>
+ data = @project data, obj
fs.writeFile @file(id), JSON.stringify(data, null, 2), cb
putSync: (id, obj) ->
- data = @project JSON.parse(@getSync(id)), obj
+ data = @project @getSync(id), obj
fs.writeFileSync @file(id), JSON.stringify(data, null, 2)
# Create doc
@@ -104,27 +103,28 @@ nosqlite.Connection::database = (name, mode) ->
# Find a doc
find: (cond, cb) ->
- fs.readdir @dir, (err, files) ->
- async.map files, (file, callback) ->
- @get path.basename(file, '.json'), (err, data) ->
+ fs.readdir @dir, (err, files) =>
+ async.map files, (file, callback) =>
+ @get path.basename(file, '.json'), (err, data) =>
if @satisfy data, cond then callback err, data else callback err, null
- , cb
+ , (err, files) ->
+ cb err, files.filter (file) -> file?
findSync: (cond) ->
files = fs.readdirSync @dir
- files = files.map (e) ->
+ files = files.map (file) =>
data = @getSync path.basename(file, '.json')
- if @satisy data, cond then data else null
- files.filter (e) -> e?
+ if @satisfy data, cond then data else null
+ files.filter (file) -> file?
# Get all docs
all: (cb) ->
- fs.readdir @dir, (err, files) ->
- async.map files, (file, callback) ->
+ fs.readdir @dir, (err, files) =>
+ async.map files, (file, callback) =>
@get path.basename(file, '.json'), callback
, cb
allSync: ->
files = fs.readdirSync @dir
- files.map (e) ->
+ files.map (file) =>
@getSync path.basename file, '.json'
View
113 test/database-test.coffee
@@ -293,4 +293,117 @@ vows
assert.throws ->
db.deleteSync 'tim'
+ .addBatch
+ 'Database "test"':
+ topic: () ->
+ connection.database 'test'
+
+ 'all()':
+ topic: (db) ->
+ db.all @callback
+
+ 'should be successful': (docs) ->
+ assert.equal docs.length, 1
+ assert.equal docs[0].id, 'bob'
+ assert.equal docs[0].name, 'bob'
+ assert.equal docs[0].age, 35
+
+ 'allSync()':
+ topic: (db) ->
+ db.allSync()
+
+ 'should be successful': (docs) ->
+ assert.equal docs.length, 1
+ assert.equal docs[0].id, 'bob'
+ assert.equal docs[0].name, 'bob'
+ assert.equal docs[0].age, 35
+
+ .addBatch
+ 'Database "test"':
+ topic: () ->
+ connection.database 'test'
+
+ 'find()':
+ topic: (db) ->
+ db.find age: 35, @callback
+
+ 'should be successful': (docs) ->
+ assert.equal docs.length, 1
+ assert.equal docs[0].id, 'bob'
+ assert.equal docs[0].name, 'bob'
+ assert.equal docs[0].age, 35
+
+ 'findSync()':
+ topic: (db) ->
+ db.findSync age: 35
+
+ 'should be successful': (docs) ->
+ assert.equal docs.length, 1
+ assert.equal docs[0].id, 'bob'
+ assert.equal docs[0].name, 'bob'
+ assert.equal docs[0].age, 35
+
+ 'find() empty':
+ topic: (db) ->
+ db.find age: 31, @callback
+
+ 'should be successful': (docs) ->
+ assert.equal docs.length, 0
+
+ 'findSync() empty':
+ topic: (db) ->
+ db.findSync age: 31
+
+ 'should be successful': (docs) ->
+ assert.equal docs.length, 0
+
+ .addBatch
+ 'Database "test"':
+ topic: () ->
+ connection.database 'test'
+
+ 'put()':
+ topic: (db) ->
+ db.put 'bob', age: 31, @callback
+
+ 'should be successful': (err) ->
+ assert.isUndefined err
+
+ .addBatch
+ 'Database "test"':
+ topic: () ->
+ connection.database 'test'
+
+ 'should be saved':
+ topic: (db) ->
+ db.getSync 'bob'
+
+ 'successfully': (data) ->
+ assert.equal data.age, 31
+
+ .addBatch
+ 'Database "test"':
+ topic: () ->
+ connection.database 'test'
+
+ 'putSync()':
+ topic: (db) ->
+ db
+
+ 'should be successful': (db) ->
+ assert.doesNotThrow ->
+ db.putSync 'bob', age: 35
+
+ .addBatch
+ 'Database "test"':
+ topic: () ->
+ connection.database 'test'
+
+ 'should be saved':
+ topic: (db) ->
+ db.getSync 'bob'
+
+ 'successfully': (data) ->
+ assert.equal data.age, 35
+
.export(module)
View
2 test/fixtures/test/bob.json
@@ -2,4 +2,4 @@
"id": "bob",
"name": "bob",
"age": 35
-}
+}

0 comments on commit 04f3677

Please sign in to comment.
Something went wrong with that request. Please try again.