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

86 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-04-01 22:25:16 +00:00
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2016-08-22 19:55:22 +00:00
'use strict';
2018-12-07 16:13:48 +00:00
const jdb = require('../');
const DataSource = jdb.DataSource;
const assert = require('assert');
const async = require('async');
const should = require('./init.js');
2014-08-30 08:53:10 +00:00
2018-12-07 16:13:48 +00:00
let db, TransientModel, Person, Widget, Item;
2018-12-07 16:13:48 +00:00
const getTransientDataSource = function(settings) {
2016-04-01 11:48:17 +00:00
return new DataSource('transient', settings);
2014-09-04 13:33:12 +00:00
};
2016-04-01 11:48:17 +00:00
describe('Transient connector', function() {
before(function() {
db = getTransientDataSource();
2016-08-19 17:46:59 +00:00
TransientModel = db.define('TransientModel', {}, {idInjection: false});
2016-04-01 11:48:17 +00:00
2016-08-19 17:46:59 +00:00
Person = TransientModel.extend('Person', {name: String});
2014-08-30 08:53:10 +00:00
Person.attachTo(db);
2016-04-01 11:48:17 +00:00
2016-08-19 17:46:59 +00:00
Widget = db.define('Widget', {name: String});
2014-08-30 08:53:10 +00:00
Item = db.define('Item', {
2016-08-19 17:46:59 +00:00
id: {type: Number, id: true}, name: String,
2014-08-30 08:53:10 +00:00
});
});
2016-04-01 11:48:17 +00:00
2014-08-30 08:53:10 +00:00
it('should respect idInjection being false', function(done) {
should.not.exist(Person.definition.properties.id);
should.exist(Person.definition.properties.name);
2016-04-01 11:48:17 +00:00
2016-08-19 17:46:59 +00:00
Person.create({name: 'Wilma'}, function(err, inst) {
2014-08-30 08:53:10 +00:00
should.not.exist(err);
2016-08-19 17:46:59 +00:00
inst.toObject().should.eql({name: 'Wilma'});
2016-04-01 11:48:17 +00:00
2014-08-30 08:53:10 +00:00
Person.count(function(err, count) {
should.not.exist(err);
count.should.equal(0);
done();
});
});
});
2016-04-01 11:48:17 +00:00
2014-08-30 08:53:10 +00:00
it('should generate a random string id', function(done) {
should.exist(Widget.definition.properties.id);
should.exist(Widget.definition.properties.name);
2016-04-01 11:48:17 +00:00
2014-08-30 08:53:10 +00:00
Widget.definition.properties.id.type.should.equal(String);
2016-04-01 11:48:17 +00:00
2016-08-19 17:46:59 +00:00
Widget.create({name: 'Thing'}, function(err, inst) {
2014-08-30 08:53:10 +00:00
should.not.exist(err);
inst.id.should.match(/^[0-9a-fA-F]{24}$/);
inst.name.should.equal('Thing');
2016-04-01 11:48:17 +00:00
2014-08-30 08:53:10 +00:00
Widget.findById(inst.id, function(err, widget) {
should.not.exist(err);
should.not.exist(widget);
done();
});
});
});
2016-04-01 11:48:17 +00:00
2014-08-30 08:53:10 +00:00
it('should generate a random number id', function(done) {
should.exist(Item.definition.properties.id);
should.exist(Item.definition.properties.name);
2016-04-01 11:48:17 +00:00
2014-08-30 08:53:10 +00:00
Item.definition.properties.id.type.should.equal(Number);
2016-04-01 11:48:17 +00:00
2016-08-19 17:46:59 +00:00
Item.create({name: 'Example'}, function(err, inst) {
2014-08-30 08:53:10 +00:00
should.not.exist(err);
inst.name.should.equal('Example');
2016-04-01 11:48:17 +00:00
2014-08-30 08:53:10 +00:00
Item.count(function(err, count) {
should.not.exist(err);
count.should.equal(0);
done();
});
});
});
});