forked from strongloop/loopback-connector-remote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelations.test.js
142 lines (127 loc) · 4.21 KB
/
relations.test.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var TaskEmitter = require('strong-task-emitter');
var assert = require('assert');
var helper = require('./helper');
describe('Relations Tests', function() {
var ctx = this;
// Create base models
before(function() {
ctx.serverApp = helper.createRestAppAndListen(3001);
ctx.serverDatasource = helper.createMemoryDataSource();
ctx.ServerModel = helper.createModel({
name: 'TestModel',
app: ctx.serverApp,
datasource: ctx.serverDatasource
});
ctx.remoteApp = helper.createRestAppAndListen(3002);
ctx.remoteDatasource = helper.createRemoteDataSource(ctx.serverApp);
ctx.RemoteModel = helper.createModel({
name: 'TestModel',
app: ctx.remoteApp,
datasource: ctx.remoteDatasource
});
});
// Create relation models
before(function() {
ctx.ServerRelationModel = helper.createModel({
name: 'RelationModel',
app: ctx.serverApp,
datasource: ctx.serverDatasource
});
ctx.RemoteRelationModel = helper.createModel({
name: 'RelationModel',
app: ctx.remoteApp,
datasource: ctx.remoteDatasource
});
ctx.ServerModel.hasMany(ctx.ServerRelationModel,
{foreignKey: 'fooId', as: 'foo'}
);
ctx.ServerRelationModel.belongsTo(ctx.ServerModel,
{foreignKey: 'fooId', as: 'foo'}
);
});
// Create model insances
before(function(done) {
var taskEmitter = new TaskEmitter();
taskEmitter
.task(ctx.ServerModel, 'create', {id: 1})
.task(ctx.RemoteModel, 'create', {id: 2})
.task(ctx.ServerRelationModel, 'create', {id: 1})
.task(ctx.ServerRelationModel, 'create', {id: 2})
.task(ctx.ServerRelationModel, 'create', {id: 3, fooId: 1})
.task(ctx.RemoteRelationModel, 'create', {id: 4, fooId: 2})
.on('done', done);
});
after(function() {
ctx.serverApp.locals.handler.close();
ctx.remoteApp.locals.handler.close();
ctx.serverDatasource = null;
ctx.remoteDatasource = null;
ctx.ServerModel = null;
ctx.RemoteModel = null;
ctx.ServerRelationModel = null;
ctx.RemoteRelationModel = null;
});
it('should find all instances of the ServerModel', function(done) {
ctx.ServerModel.find({}, function(err, instances) {
assert(instances.length === 2);
done();
});
});
it('should find all instances of the RemoteModel', function(done) {
ctx.RemoteModel.find({}, function(err, instances) {
assert(instances.length === 2);
done();
});
});
it('should find all instances of the ServerRelationModel', function(done) {
ctx.ServerRelationModel.find({}, function(err, instances) {
assert(instances.length === 4);
done();
});
});
it('should find all instances of the RemoteRelationModel', function(done) {
ctx.RemoteRelationModel.find({}, function(err, instances) {
assert(instances.length === 4);
done();
});
});
it('should find all relations of the ServerRelationModel',
function(done) {
ctx.ServerRelationModel.find({include: 'foo'}, function(err, instances) {
instances = JSON.parse(JSON.stringify(instances));
assert(instances.length === 4);
instances.forEach(function(i) {
if (i.id === 3 || i.id === 4) assert(i.foo);
});
done();
});
});
it('should find all relations of the RemoteRelationModel',
function(done) {
ctx.RemoteRelationModel.find({include: 'foo'}, function(err, instances) {
instances = JSON.parse(JSON.stringify(instances));
assert(instances.length === 4);
instances.forEach(function(i) {
if (i.id === 3 || i.id === 4) assert(i.foo);
});
done();
});
});
it('should find all relations of the RemoteRelationModel redefined',
function(done) {
ctx.RemoteModel.hasMany(ctx.RemoteRelationModel,
{foreignKey: 'fooId', as: 'foo'}
);
ctx.RemoteRelationModel.belongsTo(ctx.RemoteModel,
{foreignKey: 'fooId', as: 'foo'}
);
ctx.RemoteRelationModel.find({include: 'foo'}, function(err, instances) {
instances = JSON.parse(JSON.stringify(instances));
assert(instances.length === 4);
instances.forEach(function(i) {
if (i.id === 3 || i.id === 4) assert(i.foo);
});
done();
});
});
});