loopback-datasource-juggler/test/transient.test.js

82 lines
2.3 KiB
JavaScript
Raw Normal View History

2014-08-30 08:53:10 +00:00
var jdb = require('../');
var DataSource = jdb.DataSource;
var assert = require('assert');
var async = require('async');
var should = require('./init.js');
var db, TransientModel, Person, Widget, Item;
var getTransientDataSource = function(settings) {
2014-09-04 16:01:04 +00:00
return new DataSource('transient', settings);
2014-09-04 13:33:12 +00:00
};
2014-08-30 08:53:10 +00:00
describe('Transient connector', function () {
before(function () {
db = getTransientDataSource();
2014-08-30 08:53:10 +00:00
TransientModel = db.define('TransientModel', {}, { idInjection: false });
Person = TransientModel.extend('Person', {name: String});
Person.attachTo(db);
Widget = db.define('Widget', {name: String});
Item = db.define('Item', {
id: {type: Number, id: true}, name: String
});
});
it('should respect idInjection being false', function(done) {
should.not.exist(Person.definition.properties.id);
should.exist(Person.definition.properties.name);
Person.create({ name: 'Wilma' }, function(err, inst) {
should.not.exist(err);
inst.toObject().should.eql({ name: 'Wilma' });
Person.count(function(err, count) {
should.not.exist(err);
count.should.equal(0);
done();
});
});
});
it('should generate a random string id', function(done) {
should.exist(Widget.definition.properties.id);
should.exist(Widget.definition.properties.name);
Widget.definition.properties.id.type.should.equal(String);
Widget.create({ name: 'Thing' }, function(err, inst) {
should.not.exist(err);
inst.id.should.match(/^[0-9a-fA-F]{24}$/);
inst.name.should.equal('Thing');
Widget.findById(inst.id, function(err, widget) {
should.not.exist(err);
should.not.exist(widget);
done();
});
});
});
it('should generate a random number id', function(done) {
should.exist(Item.definition.properties.id);
should.exist(Item.definition.properties.name);
Item.definition.properties.id.type.should.equal(Number);
Item.create({ name: 'Example' }, function(err, inst) {
should.not.exist(err);
inst.name.should.equal('Example');
Item.count(function(err, count) {
should.not.exist(err);
count.should.equal(0);
done();
});
});
});
});