|
5 | 5 | const chai = require('chai'); |
6 | 6 | const sinon = require('sinon'); |
7 | 7 | const Chance = require('chance'); |
| 8 | +const Joi = require('joi'); |
8 | 9 | const { Datastore } = require('@google-cloud/datastore'); |
9 | 10 | const { argv } = require('yargs'); |
10 | 11 |
|
@@ -291,6 +292,73 @@ describe('Model (Integration Tests)', () => { |
291 | 292 | }); |
292 | 293 | }); |
293 | 294 |
|
| 295 | + describe('update()', () => { |
| 296 | + describe('transaction()', () => { |
| 297 | + it('should update entity inside a transaction', () => { |
| 298 | + const userSchema = new Schema({ |
| 299 | + name: { joi: Joi.string().required() }, |
| 300 | + lastname: { joi: Joi.string() }, |
| 301 | + password: { joi: Joi.string() }, |
| 302 | + coins: { joi: Joi.number().integer().min(0) }, |
| 303 | + email: { joi: Joi.string().email() }, |
| 304 | + createdAt: { joi: Joi.date() }, |
| 305 | + access_token: { joi: [Joi.string(), Joi.number()] }, |
| 306 | + birthyear: { joi: Joi.number().integer().min(1900).max(2013) }, |
| 307 | + }, { joi: true }); |
| 308 | + |
| 309 | + const User = gstore.model('ModelTestsTransaction-User', userSchema); |
| 310 | + |
| 311 | + function transferCoins(fromUser, toUser, amount) { |
| 312 | + return new Promise((resolve, reject) => { |
| 313 | + const transaction = gstore.transaction(); |
| 314 | + return transaction.run() |
| 315 | + .then(async () => { |
| 316 | + await User.update(fromUser.entityKey.id, { |
| 317 | + coins: fromUser.coins - amount, |
| 318 | + }, null, null, transaction); |
| 319 | + |
| 320 | + await User.update(toUser.entityKey.id, { |
| 321 | + coins: toUser.coins + amount, |
| 322 | + }, null, null, transaction); |
| 323 | + |
| 324 | + transaction.commit() |
| 325 | + .then(async () => { |
| 326 | + const [user1, user2] = await User.get([ |
| 327 | + fromUser.entityKey.id, |
| 328 | + toUser.entityKey.id, |
| 329 | + ], null, null, null, { preserveOrder: true }); |
| 330 | + expect(user1.name).equal('User1'); |
| 331 | + expect(user1.coins).equal(0); |
| 332 | + expect(user2.name).equal('User2'); |
| 333 | + expect(user2.coins).equal(1050); |
| 334 | + resolve(); |
| 335 | + }).catch((err) => { |
| 336 | + reject(err); |
| 337 | + }); |
| 338 | + }).catch((err) => { |
| 339 | + transaction.rollback(); |
| 340 | + reject(err); |
| 341 | + }); |
| 342 | + }); |
| 343 | + } |
| 344 | + |
| 345 | + const fromUser = new User({ name: 'User1', coins: 1000 }); |
| 346 | + const toUser = new User({ name: 'User2', coins: 50 }); |
| 347 | + |
| 348 | + return fromUser.save() |
| 349 | + .then(({ entityKey }) => { |
| 350 | + addKey(entityKey); |
| 351 | + return toUser.save(); |
| 352 | + }) |
| 353 | + .then(({ entityKey }) => { |
| 354 | + addKey(entityKey); |
| 355 | + return transferCoins(fromUser, toUser, 1000); |
| 356 | + }); |
| 357 | + }); |
| 358 | + }); |
| 359 | + }); |
| 360 | + |
| 361 | + |
294 | 362 | describe('hooks', () => { |
295 | 363 | it('post delete hook should set scope on entity instance', () => { |
296 | 364 | const schema = new Schema({ name: { type: 'string' } }); |
|
0 commit comments