transient.test.js
2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// 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
'use strict';
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) {
return new DataSource('transient', settings);
};
describe('Transient connector', function() {
before(function() {
db = getTransientDataSource();
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();
});
});
});
});