UNPKG

2.48 kBJavaScriptView Raw
1// Copyright IBM Corp. 2014,2019. All Rights Reserved.
2// Node module: loopback-datasource-juggler
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6'use strict';
7const jdb = require('../');
8const DataSource = jdb.DataSource;
9const assert = require('assert');
10const async = require('async');
11const should = require('./init.js');
12
13let db, TransientModel, Person, Widget, Item;
14
15const getTransientDataSource = function(settings) {
16 return new DataSource('transient', settings);
17};
18
19describe('Transient connector', function() {
20 before(function() {
21 db = getTransientDataSource();
22 TransientModel = db.define('TransientModel', {}, {idInjection: false});
23
24 Person = TransientModel.extend('Person', {name: String});
25 Person.attachTo(db);
26
27 Widget = db.define('Widget', {name: String});
28 Item = db.define('Item', {
29 id: {type: Number, id: true}, name: String,
30 });
31 });
32
33 it('should respect idInjection being false', function(done) {
34 should.not.exist(Person.definition.properties.id);
35 should.exist(Person.definition.properties.name);
36
37 Person.create({name: 'Wilma'}, function(err, inst) {
38 should.not.exist(err);
39 inst.toObject().should.eql({name: 'Wilma'});
40
41 Person.count(function(err, count) {
42 should.not.exist(err);
43 count.should.equal(0);
44 done();
45 });
46 });
47 });
48
49 it('should generate a random string id', function(done) {
50 should.exist(Widget.definition.properties.id);
51 should.exist(Widget.definition.properties.name);
52
53 Widget.definition.properties.id.type.should.equal(String);
54
55 Widget.create({name: 'Thing'}, function(err, inst) {
56 should.not.exist(err);
57 inst.id.should.match(/^[0-9a-fA-F]{24}$/);
58 inst.name.should.equal('Thing');
59
60 Widget.findById(inst.id, function(err, widget) {
61 should.not.exist(err);
62 should.not.exist(widget);
63 done();
64 });
65 });
66 });
67
68 it('should generate a random number id', function(done) {
69 should.exist(Item.definition.properties.id);
70 should.exist(Item.definition.properties.name);
71
72 Item.definition.properties.id.type.should.equal(Number);
73
74 Item.create({name: 'Example'}, function(err, inst) {
75 should.not.exist(err);
76 inst.name.should.equal('Example');
77
78 Item.count(function(err, count) {
79 should.not.exist(err);
80 count.should.equal(0);
81 done();
82 });
83 });
84 });
85});