This repository has been archived by the owner on Oct 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEntityManager.test.js
239 lines (238 loc) · 7.81 KB
/
EntityManager.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"use strict";
describe("cassandra-jpa::EntityManager", function ()
{
const assert = require("assert");
const async = require("async");
const m = require("..");
let jpaConfig = new m.JPAConfiguration();
let fooMetaModel = new m.tests.FooMetaModel(jpaConfig);
let entityManager;
let foo = new m.tests.Foo({
name: "test",
created: new Date(),
entity: new m.Entity(),
entities: [new m.Entity(), new m.Entity()]
});
let id = foo.id;
let cb, cq;
before(function ()
{
let emFactory = m.Persistence.createEntityManagerFactory("Foo", jpaConfig);
entityManager = emFactory.createEntityManager(fooMetaModel);
cb = entityManager.getCriteriaBuilder();
cq = cb.createQuery();
assert.equal(entityManager instanceof m.EntityManager, true);
assert.equal(entityManager.metaModel instanceof m.tests.FooMetaModel, true);
});
it("should convert entity toRow", function ()
{
let row = fooMetaModel.toRow(foo);
assert.equal(typeof row.id === "string", true);
assert.equal(typeof row.entity === "string", true);
assert.equal(row.entities.length, 2);
assert.equal(row.entities[0] instanceof m.tests.Foo, false);
});
it("should convert row to entity", function ()
{
let row = fooMetaModel.toRow(foo);
let entity = fooMetaModel.fromRow(row);
assert.equal(entity instanceof m.tests.Foo, true);
assert.equal(typeof entity.id === "string", true);
assert.equal(entity.entity instanceof m.Entity, true);
assert.equal(entity.entities.length, 2);
assert.equal(entity.entities[0] instanceof m.Entity, true);
});
it("should drop Table foo", function (done)
{
entityManager.dropTable(function (error, res)
{
assert.equal(error, null);
return done();
});
});
it("should create Table foo", function (done)
{
entityManager.createTable(function (error, res)
{
assert.equal(error, null);
return done();
});
});
it("should insert Indexes to Table foo", function (done)
{
entityManager.insertIndexes(function (error, res)
{
assert.equal(error, null);
return done();
});
});
it("should truncate Table foo", function (done)
{
entityManager.truncate(function (error, res)
{
assert.equal(error, null);
return done();
});
});
it("should persist Foo", function (done)
{
entityManager.persist(foo, function (error, result)
{
assert.equal(error, null);
return done();
});
});
it("should findOne Foo by criteriaQuery", function (done)
{
let op1 = cb.equal("id", foo.id);
let q1 = cb.and([op1]);
let criteriaQuery = cq.where(cb.and([op1]));
assert(foo.name === "test");
entityManager.findOne(function (error, res)
{
assert.equal(error, null);
assert(res instanceof m.tests.Foo);
assert(res.id === id);
assert.equal(res.name, "test");
return done();
}, criteriaQuery);
});
it("should update foo", function (done)
{
let newFoo;
async.series([function (callback)
{
let op1 = cb.equal("id", foo.id);
let op2 = cb.equal("name", foo.name);
let criteriaQuery = cq.where(cb.and([op1, op2]));
entityManager.findOne(function (error, res)
{
assert.equal(error, null);
newFoo = res;
assert(newFoo instanceof m.tests.Foo);
assert(newFoo.id === id);
assert(newFoo.enabled === true);
return callback(error, res);
}, criteriaQuery);
}, function (callback)
{
newFoo.enabled = false;
let op1 = cb.equal("id", foo.id);
let op2 = cb.equal("name", newFoo.name);
let criteriaQuery = cq.where(cb.and([op1, op2]));
entityManager.updateByCriteria(newFoo, function (error, res)
{
assert.equal(error, null);
return callback(error, res);
}, criteriaQuery);
}, function (callback)
{
let op1 = cb.equal("id", foo.id);
let op2 = cb.equal("name", newFoo.name);
let criteriaQuery = cq.where(cb.and([op1, op2]));
entityManager.findOne(function (error, res)
{
assert.equal(error, null);
assert(res instanceof m.tests.Foo);
assert(res.id === id);
assert(res.enabled === false);
return callback(error, res);
}, criteriaQuery);
}], function (err, results)
{
assert.equal(err, null);
return done();
});
});
it("should findAll Foo by criteriaQuery", function (done)
{
let op1 = cb.equal("id", foo.id);
let op2 = cb.equal("name", foo.name);
let criteriaQuery = cq.where(cb.and([op1, op2]));
entityManager.findAll(function (error, res)
{
assert.equal(error, null);
assert.equal(res.length, 1);
let newFoo = res[0];
assert(newFoo.id === id);
assert(newFoo.enabled === false);
return done();
}, criteriaQuery);
});
it("should remove Foo by criteriaQuery", function (done)
{
let op1 = cb.equal("id", foo.id);
let criteriaQuery = cq.where(cb.and([op1]));
entityManager.removeByCriteria(function (error, res)
{
assert.equal(error, null);
return done();
}, criteriaQuery);
});
it("should persist All Foo", function (done)
{
async.series([function (callback)
{
let foos = [];
for (let i = 0; i < 3; i++)
{
let f = new m.tests.Foo({
name: "manyFoos"
});
foos.push(f);
}
entityManager.persistAll(foos, function (error, res)
{
assert.equal(error, null);
return callback(error, res);
});
}, function (callback)
{
let op = cb.equal("name", "manyFoos");
let criteriaQuery = cq.where(cb.and([op]));
entityManager.findAll(function (error, res)
{
assert.equal(error, null);
assert.equal(res.length, 3);
return callback(error, res);
}, criteriaQuery);
}], function (err, results)
{
assert.equal(err, null);
return done();
});
});
it("should persist All Foo as bunch", function (done)
{
async.series([function (callback)
{
let foos = [];
for (let i = 0; i < 300; i++)
{
let f = new m.tests.Foo({
name: "manyBunchFoos"
});
foos.push(f);
}
entityManager.persistBunch(foos, function (error, res)
{
assert.equal(error, null);
return callback(error, res);
});
}, function (callback)
{
let op = cb.equal("name", "manyBunchFoos");
let criteriaQuery = cq.where(cb.and([op]));
entityManager.findAll(function (error, res)
{
assert.equal(error, null);
assert.equal(res.length, 300);
return callback(error, res);
}, criteriaQuery);
}], function (err, results)
{
assert.equal(err, null);
return done();
});
});
});