Commit d7261c78 by Wes Freeman

Merge pull request #36 from todvora/master

Initial version of tests
parents eed2fedf 9d4ba115
language: node_js
services: mongodb
node_js:
- "0.11"
install:
- npm install jshint -g
- npm install jasmine-node -g
- npm install mongodb -g
script:
- jshint variety.js
- test/tests.sh
\ No newline at end of file
## Variety tests
Tests are primary configured for [Travis-CI](https://travis-ci.org/variety/variety) platform. See `.travis.yml` in repository (`install` and `script` section).
It's easy to run tests locally, but you need to prepare your environment little bit. This readme assumes linux machine, mac would probably work too.
On windows you need [cygwin](http://www.cygwin.com) or some way how to run bash scripts.
## Dependencies
[MongoDB](http://www.mongodb.org) installed, of course. Tests are written in [Jasmine](http://jasmine.github.io), Behavior-Driven JavaScript test framework.
In order to run Jasmine from command line, [Node.js](http://nodejs.org) is required.
Integration between Node.js and Jasmine ensures [jasmine-node](https://www.npmjs.org/package/jasmine-node) package.
Tests connect to MongoDB via node.js connector [node-mongodb-native](https://github.com/mongodb/node-mongodb-native).
## Run tests locally
Install node dependencies globally. This step is necessary just for the first run. After that, packages are installed and ready to use.
```
npm install jasmine-node -g
npm install mongodb -g
```
Run `test/tests.sh` from repository base page. It will run whole lifecycle of tests.
Main indicator of tests result is [exit code](http://tldp.org/LDP/abs/html/exit-status.html) of script.
In case of everything went well, return code is `0`. In case of tests fail, exit code is set to nonzero. Exit code is monitored by Travis-CI and informs about tests success or fail.
Tests produce verbose log messages for detecting problems and errors.
## Tests lifecycle
- Initialization, prepare data, see `test/init.js`
- Variety analysis, run variety.js against prepared data
- Jasmine tests, see `test/variety_spec.js`
- Resources cleanup, see `test/cleanup.js`
## Used databases and collections
Tests use two databases, `test` and `varietyResults`. In DB `test`, there will be created collection `users`.
Collection is later analyzed by variety and results stored in DB `varietyResults`, collection `usersKeys`.
Cleanup script removes `test.users` and `varietyResults.usersKeys` after tests run. It does not remove any database.
## Contribute
You can extend `variety_spec.js` or create new JavaScript file with extension `_spec.js` (for example `max-depth_spec.js`).
All `_spec.js` files are automatically included in tests by jasmine.
\ No newline at end of file
// clean all resources created in init.js and during Variety execution and tests.
use test;
db.users.drop();
use varietyResults;
db.usersKeys.drop();
\ No newline at end of file
// This script contains initial data from Variety README. It will be called once before test run. All data created here
// should be removed in cleanup.js script. It is not necessary on Travis-CI, but very useful on local environments
use test;
db.users.insert({name: "Tom", bio: "A nice guy.", pets: ["monkey", "fish"], someWeirdLegacyKey: "I like Ike!"});
db.users.insert({name: "Dick", bio: "I swordfight.", birthday: new Date("1974/03/14")});
db.users.insert({name: "Harry", pets: "egret", birthday: new Date("1984/03/14")});
db.users.insert({name: "Geneviève", bio: "Ça va?"});
db.users.insert({name: "Jim", someBinData: new BinData(2,"1234")});
\ No newline at end of file
#!/bin/sh
# Need to link globally installed libraries to local directory. Without this, it is not possible to call 'require(lib)'
# in source codes (jasmine tests). This could be replaced by configuring package.json (define dependencies, test script)
npm link mongodb
# current script directory, used as relative path to test resources
DIRNAME=`dirname $0`
# cumulative return code
RETURNCODE=0
# init scripts, create test data used by variety
mongo test < $DIRNAME/init.js
# check, that import finished correctly, otherwise exit (TODO: should we try to cleanup resources?)
if [ $? -ne 0 ]; then
echo "Failed to initialize tests from file $DIRNAME/init.js"
exit 1
fi
# run variety itself. Analyze collection users in database test
mongo test --eval "var collection = 'users'" $DIRNAME/../variety.js
# in case of fail do not exit, just log, set return code and continue to cleanup
if [ $? -ne 0 ]; then
echo "Failed to execute variety"
RETURNCODE=2
fi
if [ $RETURNCODE -eq 0 ]; then
echo "Running jasmine tests for variety"
jasmine-node $DIRNAME/../test --verbose --captureExceptions
if [ $? -ne 0 ]; then
echo "There ware test errors, see log above!"
RETURNCODE=3
else
echo "Tests finished, no problem detected"
fi
else
echo "tests skipped because of fail when run variety analyzer"
fi
#cleanup resources
mongo test < $DIRNAME/cleanup.js
if [ $? -ne 0 ]; then
echo "Failed to cleanup test resources"
RETURNCODE=4
fi
exit $RETURNCODE
\ No newline at end of file
var mongo = require('mongodb');
var TESTED_COLLECTION_NAME = 'usersKeys';
var CONNECTION_STRING = 'mongodb://127.0.0.1:27017/varietyResults';
var mongoClient = mongo.MongoClient;
var withVarietyDb = function (callback) {
mongoClient.connect(CONNECTION_STRING, function (err, db) {
if (err) throw err;
callback(db);
});
};
var verifyVarietyResultEntry = function (keyName, expectedType, occurrencesCount, percentContaining, doneCallback) {
withVarietyDb(function (db) {
var collection = db.collection(TESTED_COLLECTION_NAME);
collection.findOne({'_id.key': keyName}, function (err, result) {
if (err) throw err;
expect(result).not.toBeNull();
if (result != null) {
expect(result.value.types).toEqual(expectedType);
expect(result.totalOccurrences).toEqual(occurrencesCount);
expect(result.percentContaining).toEqual(percentContaining);
}
db.close();
doneCallback();
});
});
};
describe("Variety results", function () {
it("should verify correct count of results", function (done) {
withVarietyDb(function (db) {
db.collection(TESTED_COLLECTION_NAME).count(function (err, count) {
if (err) throw err;
expect(count).toEqual(7);
db.close();
done();
});
});
});
it("should verify correct '_id' result", function (done) {
verifyVarietyResultEntry("_id", ["ObjectId"], 5, 100, done);
});
it("should verify correct 'name' result", function (done) {
verifyVarietyResultEntry("name", ["String"], 5, 100, done);
});
it("should verify correct 'name' result", function (done) {
verifyVarietyResultEntry("bio", ["String"], 3, 60, done);
});
it("should verify correct 'pets' result", function (done) {
verifyVarietyResultEntry("pets", [ "String", "Array" ], 2, 40, done);
});
it("should verify correct 'birthday' result", function (done) {
verifyVarietyResultEntry("birthday", ["Date"], 2, 40, done);
});
it("should verify correct 'someBinData' result", function (done) {
verifyVarietyResultEntry("someBinData", ["BinData-old"], 1, 20, done);
});
it("should verify correct 'someWeirdLegacyKey' result", function (done) {
verifyVarietyResultEntry("someWeirdLegacyKey", ["String"], 1, 20, done);
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment