Last active
December 19, 2017 17:24
-
-
Save ThomasCrevoisier/0fdd17d563f7c40c213d8271ede81a15 to your computer and use it in GitHub Desktop.
Run assertion on a mongo collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MongoClient = require("mongodb").MongoClient; | |
const test = require("tape"); | |
function connect (url) { | |
return new Promise((resolve, reject) => { | |
MongoClient.connect(url, function (err, db) { | |
if (err) { | |
reject(err); | |
} else { | |
resolve(db); | |
} | |
}); | |
}); | |
} | |
function onCollection (url, collectionName, action) { | |
connect(url).then(db => { | |
const col = db.collection(collectionName); | |
const cursor = col.find({}); | |
cursor.on("data", doc => { | |
try { | |
action(doc); | |
} catch (_) {} | |
}); | |
cursor.on("end", () => { | |
db.close(); | |
}); | |
}); | |
} | |
onCollection(SOME_URL, "someCollection", (doc) => { | |
runObjectTest(doc); | |
}); | |
function runObjectTest (obj) { | |
test(`${obj.id} - someAttribute should exist`, function (t) { | |
t.plan(1); | |
t.notEqual(obj.someAttribute, undefined); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment