Skip to content

Instantly share code, notes, and snippets.

@seanhess
Created September 7, 2010 21:35
Show Gist options
  • Save seanhess/569160 to your computer and use it in GitHub Desktop.
Save seanhess/569160 to your computer and use it in GitHub Desktop.
# I need a way to clear the cached dependencies. Or more acurately, have them be different
# for each tree.
exports.basics = (assert) ->
dep = dependency.container find
mongo = dep('wrapper/mongo')
db = mongo.db "localhost", 27017, "test"
db.collection 'mongo.basics'
db.toString() # basic coverage for syntax errors
db.mongo.basics.toString() # same
db.mongo.basics.drop (err) ->
if err then throw new Error err
db.mongo.basics.save {_id: "one", property: "value"}, (err, doc) ->
if err then throw err
db.mongo.basics.findOne (err, doc) ->
if err then throw err
assert.equal doc._id, "one"
assert.equal doc.property, "value"
db.close()
exports.finding = (assert) ->
dep = dependency.container find
mongo = dep('wrapper/mongo')
db = mongo.db "localhost", 27017, "test"
db.collection 'mongo.finding'
collection = db.mongo.finding
collection.drop (err) ->
if err then throw err
collection.save {_id: "A", color: "red", size: 8}, (err, doc) -> if err then throw err
collection.save {_id: "B", color: "red", size: 6}, (err, doc) -> if err then throw err
collection.save {_id: "C", color: "blue", size: 5}, (err, doc) -> if err then throw err
collection.save {_id: "D", color: "blue", size: 4}, (err, doc) -> if err then throw err
collection.find({}, {color:1}).limit(2).skip(1).sort({_id:1}).toArray (err, docs) ->
if err then throw err
assert.equal docs[0]._id, "B"
assert.equal docs[0].size, null
assert.equal docs[1]._id, "C"
collection.findOne {_id:"B"}, (err, doc) ->
if err then throw err
assert.equal doc._id, "B"
# Test Each
ids = ['A','B','C','D']
eachIndex = 0
collection.find().each (err, doc) ->
if err then throw err
if doc?
assert.equal ids[eachIndex], doc._id
eachIndex++
collection.count (err, num) ->
if err then throw err
assert.equal num, 4
cursor = collection.find()
cursor.next (err, doc) ->
if err then throw err
assert.equal doc._id, "A"
cursor.next (err, doc) ->
assert.equal doc._id, "B"
db.close()
exports.maintenance = (assert) ->
dep = dependency.container find
mongo = dep('wrapper/mongo')
db = mongo.db "localhost", 27017, "test"
db.collection 'mongo.maintenance'
maintenance = db.mongo.maintenance
maintenance.drop (err) ->
if err then throw err
# Test double-drop
maintenance.drop (err) ->
if err then throw err
maintenance.save {_id: "A", property: "value"}
maintenance.save {_id: "B", property: "value"}
maintenance.save {_id: "C", property: "value"}
maintenance.find({_id: "A"}).explain (err, explanation) ->
if err then throw err
assert.equal explanation.nscanned, 1
db.lastError (err, something) ->
if err then throw err
assert.ok true
db.close()
exports.saving = (assert) ->
dep = dependency.container find
mongo = dep('wrapper/mongo')
db = mongo.db "localhost", 27017, "test"
db.collection 'mongo.saving'
saving = db.mongo.saving
saving.drop (err) ->
if err then throw err
# Test guaranteed ordering
saving.save {_id: "A"}
saving.save {_id: "B"}
saving.save {_id: "C"}
saving.find().toArray (err, docs) ->
if err then throw err
assert.equal docs.length, 3
# upsert
saving.update {_id: "D"}, {$set: {updated: "yep"}}, true, (err, docs) ->
if err then throw err
saving.update {_id: "B"}, {_id: "B", updated: "yep"}, (err, docs) ->
if err then throw err
saving.update {updated: "yep"}, {$set: {something: true}}, false, true, (err, docs) ->
if err then throw err
saving.find({updated: "yep"}).count (err, count) ->
if err then throw err
assert.equal count, 2
saving.find({something:true}).count (err, count) ->
if err then throw err
assert.equal count, 2
saving.remove {something:true}, (err) ->
if err then throw err
saving.find().count (err, count) ->
if err then throw err
assert.equal count, 2
# test inserting several
saving.insert [{a: "E"},{a: "F"},{a: "G"}], (err) ->
if err then throw err
saving.find().count (err, count) ->
if err then throw err
assert.equal count, 5, "Inserting and count"
db.close()
# db.close()
exports.indexing = (assert) ->
dep = dependency.container find
mongo = dep('wrapper/mongo')
db = mongo.db "localhost", 27017, "test"
db.collection 'mongo.indexing'
coll = db.mongo.indexing
coll.drop (err) ->
if err then throw err
# Test guaranteed ordering
coll.insert {_id: "A", property:1, name: "bob"}
coll.insert {_id: "B", property:2, name: "henry"}
coll.insert {_id: "C", property:3, name: "bob"}
# db.close()
coll.ensureIndex {property:1}, (err, name) ->
if err then throw err
coll.find({property:2}).explain (err, explanation) ->
if err then throw err
assert.equal explanation.nscanned, 1
coll.dropIndexes (err) ->
if err then throw err
coll.find({property:2}).explain (err, explanation) ->
if err then throw err
assert.equal explanation.nscanned, 3
db.close()
# coll.ensureIndex {name:1}, {unique: true}, (err, name) -> if err then throw err
exports.nextAndInsert = (assert) ->
dep = dependency.container find
mongo = dep('wrapper/mongo')
db = mongo.db "localhost", 27017, "test"
db.collection 'mongo.next'
db.mongo.next.drop (err) ->
assert.ifError err
db.mongo.next.find().next (err) ->
assert.ifError
db.mongo.next.insert [{_id:"next"},{_id:"next2"}], (err) ->
db.mongo.next.count (err, count) ->
assert.ifError err
assert.notEqual count, 1, "Insert only saved one document"
assert.equal count, 2, "Insert saved unknown number #{count}"
db.mongo.next.find().next (err, doc) ->
assert.ifError err
assert.ok doc?
db.mongo.next.find().limit(1).skip(1).next (err, doc) ->
assert.ifError err
assert.ok doc?, "Missing second doc inserted"
assert.equal doc._id, "next2"
db.close()
sys.puts "[ OK ] testMongoWrapper"
exports.dropping = (assert) ->
dep = dependency.container find
mongo = dep('wrapper/mongo')
db = mongo.db "localhost", 27017, "test-dropping"
db.collection 'something'
db.something.save {_id:"woot"}, (err) ->
assert.ifError err
db.dropAllCollections (err) ->
assert.ifError err
# Takes too long to test
# db.dropDatabase (err) ->
# assert.ifError err
db.close()
exports.errors = (assert) ->
dep = dependency.container errorFind
mongo = dep('wrapper/mongo')
db = mongo.db "localhost", 27017, "test"
db.collection 'mongo.errors'
verifyError = (err, something) -> assert.notEqual err, null, "Missing Fake Error"
db.mongo.errors.find().toArray verifyError
db.mongo.errors.find().explain verifyError
db.mongo.errors.find().each verifyError
db.mongo.errors.find().count verifyError
db.mongo.errors.find().next verifyError
db.mongo.errors.ensureIndex null, verifyError
db.mongo.errors.dropIndexes verifyError
db.mongo.errors.insert null, verifyError
db.mongo.errors.remove null, verifyError
db.mongo.errors.drop verifyError
db.mongo.errors.save null, verifyError
db.mongo.errors.update null, null, verifyError
db.mongo.errors.count verifyError
db.lastError verifyError
# Why doesn't it ever hit the opening thing?
exports.noop = (assert) ->
dep = dependency.container find
mongo = dep 'wrapper/mongo'
db = mongo.db "localhost", 27017, "test"
db.collection 'mongo.noop'
# Mess it up
# Give it a noop
db.mongo.noop.save {_id: "woot"}
db.mongo.noop.find().toArray (err, docs) ->
assert.ok true
db.close()
exports.reopen = (assert) ->
dep = dependency.container find
mongo = dep 'wrapper/mongo'
db = mongo.db "localhost", 27017, "test"
db.collection 'mongo.reopen'
db.mongo.reopen.save {_id:"one"}, (err, doc) ->
assert.ifError
db.close()
db.mongo.reopen.find().toArray (err, docs) ->
assert.ifError err
assert.ok (docs.length is 1), "Couldn't find doc in test reopen"
db.close()
exports.collections = (assert) ->
dep = dependency.main()
mongo = dep 'wrapper/mongo'
db = mongo.db 'localhost', 27017, 'test'
db.collection 'system.indexes'
assert.equal 'system.indexes', db.system.indexes.name(), "Mongo didn't work with a compound name"
assert.equal 'test', db.name(), "Mongo renamed the databases name of the compound index"
exports.recycle = (assert) ->
dep = dependency.main()
mongo = dep 'wrapper/mongo'
db = mongo.db 'localhost', 27017, 'test'
db.collection 'mongo.recycle'
db.mongo.recycle.save {_id:"nothing"}
done = 0
next = ->
done++
if done > 3*mongo.MaximumConnectionsBeforeRecycling
return db.close()
db.mongo.recycle.find().one (err, doc) ->
assert.ifError err
assert.ok doc?, "couldn't find document in mongo/recycle"
next()
next()
# module.exports = { nothing: -> }
if module is require.main
# exports.basics assert
# exports.reopen assert
# return exports.dropping assert
for name, test of module.exports
test assert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment