parse-osmos-driver
Version:
A Parse SDK Driver for the OSMOS ODM
287 lines (218 loc) • 6.35 kB
JavaScript
;
var assert = require('chai').assert;
var async = require('async');
var Osmos = require('osmos-lite');
var Schema = Osmos.Schema;
var Model = Osmos.Model;
var Parse = require('../lib');
var ParseClient = require('parse').Parse;
ParseClient.initialize("IWKWMYsKxe6BnJnQo42BmhYEhWGGPxiOv9BH2Xut", "FPqW4KvQM9ybBeiurLhrtkTTTpT84WLwR8TUk9sN");
var model;
var schema = new Schema(
'parse', {
type: 'object',
required: ['name', 'email'],
properties: {
name: {
type: 'string'
},
email: {
type: 'string',
format: 'email'
},
objectId: {
type: 'string',
minLength: 10,
maxLength: 10
}
}
}
);
schema.primaryKey = 'objectId';
describe('The Parse driver', function () {
beforeEach(function (done) {
var driver = new Parse(ParseClient);
Osmos.drivers.register('parse', driver);
model = new Model('ParsePerson', schema, 'person', 'parse');
model.transformers._id = {
get: function (value) {
if (!value) return value;
return value.toHexString ? value.toHexString() : value;
}
};
done();
});
afterEach(function (done) {
new ParseClient.Query(ParseClient.Object.extend('person'))
.limit(1000)
.find()
.then(function (docs) {
if (docs) {
docs.forEach(function (doc) {
doc.destroy();
});
done();
}
}, function () {
done(); // nothing to delete
});
});
it('should allow creating new documents', function (done) {
model.create(function (err, doc) {
assert.notOk(err);
assert.instanceOf(doc, Osmos.Document);
done();
});
});
it('should allow posting documents and reading their key', function (done) {
model.create(function (err, doc) {
doc.name = 'Marco';
doc.email = 'marcot@tabini.ca';
assert.notOk(doc.primaryKey, 'primary key is set for some reason!');
doc.save(function (err) {
assert.notOk(err, 'an error has been returned at save');
assert.isString(doc.primaryKey, 'primaryKey is not set or is not a string');
done();
});
});
});
it('should allow updating individual fields independently', function (done) {
model.create(function (err, doc) {
doc.name = 'Joe';
doc.email = 'joe@example.org';
doc.save(function (err) {
assert.notOk(err);
model.get(doc.primaryKey, function (err, doc2) {
async.parallel(
[
function (cb) {
doc2.name = 'Joe';
doc2.save(cb);
},
function (cb) {
doc.email = 'joe@example.org';
doc.save(cb);
}
],
function () {
model.get(doc.primaryKey, function (err, doc3) {
//assert.notOk(err, err.message);
assert.isObject(doc3);
assert.equal(doc3.name, 'Joe');
assert.equal(doc3.email, 'joe@example.org');
done();
});
}
);
});
});
});
});
it('should allow putting and retrieving documents by their key', function (done) {
model.create(function (err, doc) {
doc.name = 'Marco';
doc.email = 'marcot@tabini.ca';
doc.save(function (err) {
assert.notOk(err);
var key = doc.primaryKey;
model.get(key, function (err, doc) {
assert.notOk(err);
assert.isObject(doc);
assert.instanceOf(doc, Osmos.Document);
assert.equal(doc.name, 'Marco');
assert.equal(doc.email, 'marcot@tabini.ca');
done();
});
});
});
});
it('should allow deleting documents by their key', function (done) {
model.create(function (err, doc) {
doc.name = 'Marco';
doc.email = 'marcot@tabini.ca';
doc.save(function (err) {
assert.notOk(err);
assert.ok(doc.primaryKey);
doc.del(function (err) {
assert.notOk(err, 'message not deleted');
model.get(doc.primaryKey, function (err, doc) {
assert.notOk(doc, 'Document not deleted!');
done();
});
});
});
});
});
it('should allow querying for individual documents', function (done) {
model.create(function (err, doc) {
doc.name = 'Marco';
doc.email = 'marcot@tabini.ca';
doc.save(function () {
model.findOne({
email: 'marcot@tabini.ca'
},
function (err, result) {
assert.notOk(err);
assert.isObject(result);
assert.equal(doc.email, 'marcot@tabini.ca');
done();
}
);
});
});
});
it('should allow querying for multiple documents based on secondary indices', function (done) {
model.create(function (err, doc) {
doc.name = 'Marco';
doc.email = 'jose@tabini.ca';
doc.save();
model.find({
email: 'jose@tabini.ca'
},
function (err, result) {
assert.notOk(err);
assert.isArray(result, result);
result.forEach(function (doc) {
assert.equal(doc.email, 'jose@tabini.ca');
assert.equal(doc, {});
});
done();
});
});
});
it('should return multiple documents when using find()', function (done) {
async.series(
[
function (cb) {
model.create(function (err, doc) {
//assert.notOk(err);
doc.name = 'Marco';
doc.email = 'marcot@tabini.ca';
doc.save(cb);
});
},
function (cb) {
model.create(function (err, doc) {
assert.notOk(err);
doc.name = 'Marco';
doc.email = 'marcot@tabini.ca';
doc.save(cb);
});
},
function (cb) {
model.find({
email: 'marcot@tabini.ca'
},
function (err, docs) {
assert.notOk(err);
assert.isArray(docs);
assert.equal(docs.length, 2);
cb(null);
}
);
}
],
done
);
});
});