Permalink
Browse files

Merge branch 'feature/improve-tests' into develop

  • Loading branch information...
aseemk committed Oct 6, 2012
2 parents 1cf0e92 + 5297a02 commit 18f5c9d5c8ce16f34fb2838ae01ffbd1be757052
Showing with 354 additions and 335 deletions.
  1. +7 −7 lib/Node._coffee
  2. +4 −3 package.json
  3. +116 −102 test/crud._coffee
  4. +145 −124 test/cypher._coffee
  5. +79 −80 test/gremlin._coffee
  6. +0 −3 test/index._coffee
  7. +3 −0 test/mocha.opts
  8. +0 −16 test/profile._coffee
View
@@ -214,16 +214,16 @@ module.exports = class Node extends PropertyContainer
type: type
, _
+ # client or database error:
if response.statusCode isnt status.CREATED
- # database error
- message = ''
- switch response.statusCode
+ {message, exception} = response.body or {}
+ message or= exception or switch response.statusCode
when status.BAD_REQUEST
- message = response.body?.message or
- response.body?.exception or
- "Invalid createRelationship: #{from.id} #{type} #{to.id} w/ data: #{JSON.stringify data}"
+ "Invalid createRelationship: #{from.id} #{type} #{to.id} w/ data: #{JSON.stringify data}"
when status.CONFLICT
- message = '"to" node, or the node specified by the URI not found'
+ '"to" node, or the node specified by the URI not found'
+ else
+ throw response # e.g. internal server error
throw new Error message
# success
View
@@ -16,7 +16,9 @@
"streamline": "~0.4.0"
},
"devDependencies": {
- "codo": "~1.5.1"
+ "codo": "~1.5.1",
+ "expect.js": "~0.1.2",
+ "mocha": "~1.3.0"
},
"engines": {
"node": ">= 0.6"
@@ -26,8 +28,7 @@
"clean": "rm lib/*.js",
"codo": "codo && codo --server",
"postinstall": "npm run build",
- "profile": "_coffee test/profile",
- "test": "_coffee test"
+ "test": "mocha"
},
"keywords": [
"neo4j", "graph", "database", "driver", "rest", "api", "client"
View
@@ -1,122 +1,136 @@
# this file is in streamline syntax!
# https://github.com/Sage/streamlinejs
-assert = require 'assert'
+expect = require 'expect.js'
neo4j = require '..'
db = new neo4j.GraphDatabase 'http://localhost:7474'
+# data we're going to use:
danielData =
name: 'Daniel'
-
aseemData =
name: 'Aseem'
-daniel = db.createNode danielData
-aseem = db.createNode aseemData
-
-assert.strictEqual daniel.exists, false, 'Node should not exist'
-assert.strictEqual daniel.self, null, 'Node self should be null'
- # TODO should this really be tested? is @self a public API?
- # maybe it should just have a better name than the misleading 'self'?
-
-# test futures here by saving both aseem and daniel in parallel:
-futures = [daniel.save(), aseem.save()]
-future _ for future in futures
-
-assert.ok daniel.exists, 'Node should exist'
-assert.ok daniel.self, 'node.self should not be null' # TODO see above
-assert.deepEqual daniel.data, danielData, 'Sent and received data should match'
-
-assert.ok aseem.exists, 'Node should exist'
-assert.ok aseem.self, 'node.self should not be null' # TODO see above
-assert.deepEqual aseem.data, aseemData, 'Sent and received data should match'
+# instances we're going to reuse across tests:
+daniel = null
+aseem = null
+relationship = null
+
+@crud =
+
+ 'create nodes': (_) ->
+ daniel = db.createNode danielData
+ aseem = db.createNode aseemData
+
+ expect(daniel).to.be.an 'object'
+ expect(daniel.exists).to.be false
+ expect(daniel.self).to.be null
+ # TODO should this really be tested? is @self a public API?
+ # maybe it should just have a better name than 'self'? like url?
+
+ 'save nodes': (_) ->
+ # test futures here by saving both aseem and daniel in parallel:
+ futures = [daniel.save(), aseem.save()]
+ future _ for future in futures
+
+ expect(daniel.exists).to.be true
+ expect(daniel.self).to.be.a 'string' # TODO see above
+ expect(daniel.self).to.not.equal aseem.self # TODO see above
+ expect(daniel.data).to.eql danielData
+
+ expect(aseem.exists).to.be true
+ expect(aseem.self).to.be.a 'string' # TODO see above
+ expect(aseem.self).to.not.equal daniel.self # TODO see above
+ expect(aseem.data).to.eql aseemData
+
+ 'create realtionships': (_) ->
+ relationship = daniel.createRelationshipTo aseem, 'follows', {created: Date.now()}, _
+ testRelationship relationship
+
+ # in this case, the start and end *should* be our instances
+ expect(relationship.start).to.be daniel
+ expect(relationship.end).to.be aseem
+
+ 'fetch relationships': (_) ->
+ # test futures by *initiating* getRelationships() for both aseem and daniel in
+ # parallel. note how we'll still "collect" (process) the futures in sequence.
+ danielFuture = daniel.getRelationships('follows')
+ aseemFuture = aseem.getRelationships('follows')
+
+ relationships = danielFuture _
+ testRelationships relationships
+
+ # in this case, the start *should* be our instance
+ expect(relationships[0].start).to.be daniel
+
+ relationships = aseemFuture _
+ testRelationships relationships
+
+ # in this case, the end *should* be our instance
+ expect(relationships[0].end).to.be aseem
+
+ 'traverse nodes': (_) ->
+ # same parallel lookups using futures:
+ danielFuture = daniel.getRelationshipNodes('follows')
+ aseemFuture = aseem.getRelationshipNodes('follows')
+
+ nodes = danielFuture _
+ expect(nodes).to.be.an 'array'
+ expect(nodes).to.have.length 1
+ expect(nodes[0]).to.be.an 'object'
+ expect(nodes[0].exists).to.be true
+ expect(nodes[0].self).to.equal aseem.self # TODO see above
+ expect(nodes[0].data).to.eql aseemData
+
+ # TODO see how this is misleading? we don't respect or report direction!
+ nodes = aseemFuture _
+ expect(nodes).to.be.an 'array'
+ expect(nodes).to.have.length 1
+ expect(nodes[0]).to.be.an 'object'
+ expect(nodes[0].exists).to.be true
+ expect(nodes[0].self).to.equal daniel.self # TODO see above
+ expect(nodes[0].data).to.eql danielData
+
+ 'index nodes': (_) ->
+ daniel.index 'users', 'name', 'Daniel', _
+ node = db.getIndexedNode 'users', 'name', 'Daniel', _
+ expect(node).to.be.an 'object'
+ expect(node.exists).to.be true
+ # TODO FIXME we're not unindexing these nodes after each test, so in fact the
+ # returned node and data might be from a previous test!
+ # expect(node.self).to.equal daniel.self # TODO see above
+ # expect(node.data).to.eql danielData
+
+ 'index relationships': (_) ->
+ relationship.index 'follows', 'name', 'Daniel', _
+ rel = db.getIndexedRelationship 'follows', 'name', 'Daniel', _
+ expect(rel).to.be.an 'object'
+ expect(rel.exists).to.be true
+ expect(rel.self).to.be.a 'string' # TODO see above
+ expect(rel.type).to.be 'follows'
+
+ # TODO delete tests! that's the 'd' in 'crud'!
testRelationship = (relationship) ->
- assert.ok relationship
- assert.ok relationship.exists, 'Relationship should exist'
- assert.ok relationship.self, 'relationship.self should not be null' # TODO see above
- assert.equal relationship.type, 'follows', 'Relationship type should be "follows".'
+ expect(relationship).to.be.an 'object'
+ expect(relationship.exists).to.be true
+ expect(relationship.self).to.be.a 'string' # TODO see above
+ expect(relationship.type).to.be 'follows'
- # in some cases, the start/end nodes may not be "filled".
- # they also may not point to the same instances we have.
- #assert.equal relationship.start, daniel
- #assert.equal relationship.end, aseem
+ # in some cases, the start/end nodes may not be "filled", so these are
+ # commented out for now:
+ # expect(relationship.start).to.eql daniel
+ # expect(relationship.end).to.eql aseem
# TEMP so for the time being, we're testing that at least
# their "selves" match. not sure if this is a public API.
- assert.ok relationship.start, 'Relationship should have a start node.'
- assert.ok relationship.end, 'Relationship should have an end node.'
- assert.equal relationship.start.self, daniel.self
- assert.equal relationship.end.self, aseem.self
+ expect(relationship.start).to.be.an 'object'
+ expect(relationship.end).to.be.an 'object'
+ expect(relationship.start.self).to.equal daniel.self
+ expect(relationship.end.self).to.equal aseem.self
testRelationships = (relationships) ->
- assert.ok relationships
- assert.ok relationships.length, 'Relationships should be an array.'
- assert.equal relationships.length, 1, 'There should only be one relationship.'
- testRelationship(relationships[0])
-
-relationship = daniel.createRelationshipTo aseem, 'follows', {created: Date.now()}, _
-testRelationship(relationship)
-
-# in this case, the start and end *should* be our instances
-assert.strictEqual relationship.start, daniel
-assert.strictEqual relationship.end, aseem
-
-# TODO it would be good to test streamline futures support here if/when we
-# port the library to streamline style. we could test it here by invoking
-# getRelationships() on both daniel and aseem in parallel, then waiting for
-# both of them to return (i.e. collecting/syncing both futures).
-
-# test futures by *initiating* getRelationships() for both aseem and daniel in
-# parallel. note how we'll still "collect" (process) the futures in sequence.
-danielFuture = daniel.getRelationships 'follows'
-aseemFuture = aseem.getRelationships 'follows'
-
-relationships = danielFuture _
-testRelationships(relationships)
-
-# in this case, the start *should* be our instance
-assert.equal relationships[0].start, daniel
-
-relationships = aseemFuture _
-testRelationships(relationships)
-
-# in this case, the end *should* be our instance
-assert.equal relationships[0].end, aseem
-
-# same parallel lookups using futures:
-danielFuture = daniel.getRelationshipNodes 'follows'
-aseemFuture = aseem.getRelationshipNodes 'follows'
-
-nodes = danielFuture _
-assert.ok nodes
-assert.ok nodes.length
-assert.equal nodes.length, 1
-assert.ok nodes[0]
-assert.ok nodes[0].exists
-assert.ok nodes[0].self # TODO see above
-assert.deepEqual nodes[0].data, aseemData
-
-# TODO see how this is misleading? we don't respect or report direction!
-nodes = aseemFuture _
-assert.ok nodes
-assert.ok nodes.length
-assert.equal nodes.length, 1
-assert.ok nodes[0]
-assert.ok nodes[0].exists
-assert.ok nodes[0].self # TODO see above
-assert.deepEqual nodes[0].data, danielData
-
-daniel.index 'users', 'name', 'Daniel', _
-node = db.getIndexedNode 'users', 'name', 'Daniel', _
-assert.ok node
-
-relationship.index 'follows', 'name', 'Daniel', _
-rel = db.getIndexedRelationship 'follows', 'name', 'Daniel', _
-assert.ok rel
-
-# TODO delete tests! that's the 'd' in 'crud'!
-
-# just to ensure that no sorts of silent transformation errors plagued us
-console.log 'passed CRUD tests'
+ expect(relationships).to.be.an 'array'
+ expect(relationships).to.have.length 1
+ testRelationship relationships[0]
Oops, something went wrong.

0 comments on commit 18f5c9d

Please sign in to comment.