Skip to content

Commit

Permalink
Convert tests to mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Feb 19, 2012
1 parent 111b8f0 commit 4864ab7
Show file tree
Hide file tree
Showing 25 changed files with 848 additions and 696 deletions.
6 changes: 5 additions & 1 deletion lib/Client.coffee
Expand Up @@ -8,6 +8,7 @@ module.exports = class Client
constructor: (options = {}) ->
@options = options
@name = options.name or 'ron'
@records = []
if @options.redis
@redis = @options.redis
else
Expand All @@ -17,7 +18,10 @@ module.exports = class Client

define: (options) ->
name = if typeof options is 'string' then options else options.name
@[name] = new Table @, options
@records[name] = new Table @, options

get: (type) ->
@records[type]

quit: (callback) ->
@redis.quit (err, status) ->
Expand Down
31 changes: 30 additions & 1 deletion lib/Table.coffee
Expand Up @@ -53,6 +53,25 @@ module.exports = class Table
else
@schema.properties[property]

###
Cast record values to their correct type
----------------------------------------
Traverse all the record properties and update
values with correct types.
###
type: (records) ->
s = @schema
isArray = Array.isArray records
records = [records] if ! isArray
for record, i in records
continue unless record?
if typeof record is 'object'
for property, value of record
if s.properties[property]?.type is 'int' and value?
record[property] = parseInt value, 10
else if typeof record is 'number' or typeof record is 'string'
records[i] = parseInt record

###
Define a property as an identifier
----------------------------------
Expand All @@ -65,6 +84,7 @@ module.exports = class Table
# Set the property
if property?
@schema.properties[property] = {} unless @schema.properties[property]?
@schema.properties[property].type = 'int'
@schema.properties[property].identifier = true
@schema.identifier = property
@
Expand Down Expand Up @@ -227,6 +247,7 @@ module.exports = class Table
Create a new record.
###
create: (records, callback) ->
$ = @
s = @schema
isArray = Array.isArray records
records = [records] if ! isArray
Expand Down Expand Up @@ -277,6 +298,7 @@ module.exports = class Table
return callback err if err
for result in results
return callback new Error 'Corrupted user database ' if result[0] is not "0"
# $.type records
callback null, if isArray then records else records[0]

###
Expand All @@ -287,6 +309,7 @@ module.exports = class Table
are present if the record exists or null if it doesn't.
###
exists: (records, callback) ->
$ = @
s = @schema
isArray = Array.isArray records
records = [records] if ! isArray
Expand All @@ -304,12 +327,13 @@ module.exports = class Table
multi.hget "#{s.db}:#{s.name}:#{record}", s.identifier
multi.exec (err, recordIds) ->
return callback err if err
$.type recordIds
callback null, if isArray then recordIds else recordIds[0]

###
Create or extract one or several ids.
-------------------------------------
The method doesn't hit the database to check the existance of an id if provided.
The method doesn't hit the database to check the existance of an id if it is already provided.
Set the provided object to null if an id couldn't be found.
todo: With no argument, generate an new id
Expand All @@ -324,6 +348,7 @@ module.exports = class Table
if arguments.length is 2
callback = options
options = {}
$ = @
s = @schema
isArray = Array.isArray records
records = [records] if not isArray
Expand Down Expand Up @@ -360,12 +385,14 @@ module.exports = class Table
if not options.object
records = for record in records
if record? then record[s.identifier] else record
#$.type records
return callback null, if isArray then records else records[0]
multi = redis.multi cmds
multi.exec (err, results) ->
if not options.object
records = for record in records
record[s.identifier]
$.type records
callback null, if isArray then records else records[0]

###
Expand All @@ -385,6 +412,7 @@ module.exports = class Table
options = {}
if Array.isArray options
options = {properties: options}
$ = @
s = @schema
isArray = Array.isArray records
records = [records] if ! isArray
Expand All @@ -411,6 +439,7 @@ module.exports = class Table
multi = redis.multi cmds
multi.exec (err, values) ->
return callback err if err
$.type records
callback null, if isArray then records else records[0]

list: (options, callback) ->
Expand Down
32 changes: 20 additions & 12 deletions package.json
@@ -1,23 +1,31 @@
{
"name": "ron",
"description": "Redis ORM for NodeJs.",
"keywords": ["redis", "orm", "database", "nosql"],
"version": "0.0.3",
"description": "Redis ORM for NodeJs",
"homepage": "http://www.adaltas.com/projects/node-ron",
"author": "David Worms <david@adaltas.com>",
"contributors": [
{ "name": "David Worms", "email": "david@adaltas.com" }
],
"repository": {
"type": "git",
"url": "https://github.com/wdavidw/node-ron"
},
"bugs": {
"email": "open@adaltas.com",
"url": "http://github.com/wdavidw/node-ron/issues"
},
"dependencies": {
"redis": "latest"
},
"devDependencies": {
"expresso": "latest"
"mocha": "latest",
"should": "latest"
},
"engines": {
"node": ">= 0.3.0"
},
"keywords": ["redis", "orm", "database", "nosql"],
"repository": {
"type": "git",
"url": "https://github.com/wdavidw/node-redis-orm.git"
"contributors": [
{ "name": "David Worms", "email": "david@adaltas.com" }
],
"main": "index",
"engines": { "node": ">= 0.4.0" },
"scripts": {
"test": "mocha"
}
}
42 changes: 42 additions & 0 deletions test.coffee
@@ -0,0 +1,42 @@

class Dog
dogName: "fido"
constructor: (@dogName) ->

doStuff: ->
console.log('the dog is walking');
sayHello.call(this);

sayHello = ->
console.log("Hi! I'm "+@dogName);

ralph = new Dog("ralph");
ralph.doStuff();

peter = new Dog("peter");
ralph.doStuff();
peter.doStuff();
ralph.doStuff();

###
class GetSet
schema = {}
constructor: (sch = {}) ->
schema = sch
set: (key, value) ->
schema[key] = value
get: (key) ->
schema[key]
gs1 = new GetSet
gs2 = new GetSet
gs1.set 'a', 'b'
gs2.set 'a', 'c'
console.log gs1.get 'a'
###
34 changes: 0 additions & 34 deletions test/AllTest.coffee

This file was deleted.

32 changes: 18 additions & 14 deletions test/Client.coffee
@@ -1,19 +1,23 @@

assert = require 'assert'
should = require 'should'

config = require '../conf/test'
Ron = require '../index'

ron = Ron config

module.exports =
'init': (exit) ->
exit()
'Test hash # with string': (exit) ->
assert.eql ron.hash('1'), '356a192b7913b04c54574d18c28d46e6395428ab'
exit()
'Test hash # with number': (exit) ->
assert.eql ron.hash(1), '356a192b7913b04c54574d18c28d46e6395428ab'
exit()
'destroy': (exit) ->
ron.quit exit
describe 'client', ->

ron = Ron config

it 'init', (next) ->
next()

it 'Test hash # with string', (next) ->
ron.hash('1').should.eql '356a192b7913b04c54574d18c28d46e6395428ab'
next()

it 'Test hash # with number', (next) ->
ron.hash(1).should.eql '356a192b7913b04c54574d18c28d46e6395428ab'
next()

it 'destroy', (next) ->
ron.quit next
33 changes: 0 additions & 33 deletions test/CountTest.coffee

This file was deleted.

29 changes: 0 additions & 29 deletions test/CreateValidationTest.coffee

This file was deleted.

0 comments on commit 4864ab7

Please sign in to comment.