Debian Codegraph @

dojox/store/tests/transaction.js

raw symbols SCIP
1 define([
2 'intern!object',
3 'intern/chai!assert',
4 '../transaction',
5 'dojo/Deferred',
6 'dojo/promise/all',
7 'dojo/_base/declare',
8 'dojo/store/Memory',
9 'dojo/store/util/QueryResults'
10 ], function (registerSuite, assert, transaction, Deferred, all, declare, Memory, QueryResults) {
11
12 var started = 0;
13 function anAsyncMethod(query){
14 return function(){
15 started++;
16 var results = this.inherited(arguments);
17 if(results && results.then){
18 return results;
19 }
20 var deferred = new Deferred();
21 setTimeout(function(){
22 deferred.resolve(results);
23 }, 10);
24 return query ? new QueryResults(deferred) : deferred;
25 }
26 }
27 var AsyncMemory = declare(Memory, {
28 get: anAsyncMethod(),
29 put: anAsyncMethod(),
30 add: anAsyncMethod(),
31 query: anAsyncMethod(true)
32 });
33 var data = [
34 {id: 1, name: 'one', prime: false, mappedTo: 'E', words: ['banana']},
35 {id: 2, name: 'two', even: true, prime: true, mappedTo: 'D', words: ['banana', 'orange']},
36 {id: 3, name: 'three', prime: true, mappedTo: 'C', words: ['apple', 'orange']},
37 {id: 4, name: 'four', even: true, prime: false, mappedTo: null},
38 {id: 5, name: 'five', prime: true, mappedTo: 'A'}
39 ];
40 var masterStore = new AsyncMemory({
41 data: data
42 });
43 var cachingStore = new AsyncMemory();
44 var masterStore2 = new AsyncMemory({
45 data: []
46 });
47 var cachingStore2 = new AsyncMemory();
48 var logStore = new AsyncMemory();
49
50 var transactionStore = transaction({
51 masterStore: masterStore,
52 cachingStore: cachingStore,
53 transactionLogStore: logStore
54 });
55 var transactionStore2 = transaction({
56 masterStore: masterStore2,
57 cachingStore: cachingStore2,
58 transactionLogStore: logStore
59 });
60 registerSuite({
61 name: "transaction",
62 'auto-commit': function(){
63 // initially in auto-commit mode
64 var operations = [];
65 operations.push(transactionStore.add(
66 {id: 6, name: 'six'}
67 ));
68 assert.strictEqual(masterStore.data.length, 6);
69 operations.push(transactionStore.remove(6));
70 assert.strictEqual(masterStore.data.length, 5);
71 return all(operations);
72 },
73 'commit transaction': function(){
74 var operations = [];
75 var transaction = transactionStore.transaction();
76 operations.push(transactionStore.put(
77 {id: 6, name: 'six', perfect: true}
78 ));
79 operations.push(transactionStore.add(
80 {id: 7, name: 'seven'}
81 ));
82 operations.push(transactionStore.remove(3));
83 // make sure the master store hasn't been updated yet
84 assert.strictEqual(masterStore.data.length, 5);
85 return all(operations).then(function(){
86 // make sure it is in the caching store
87 operations.push(transactionStore.get(6).then(function(six){
88 assert.deepEqual(six, {id: 6, name: 'six', perfect: true});
89 }));
90 assert.strictEqual(cachingStore.data.length, 2);
91 assert.strictEqual(logStore.data.length, 3);
92 return transaction.commit().then(function(){
93 operations = [];
94 operations.push(transactionStore.get(6).then(function(six){
95 assert.deepEqual(six, {id: 6, name: 'six', perfect: true});
96 }));
97 operations.push(transactionStore.get(7).then(function(seven){
98 assert.deepEqual(seven, {id: 7, name: 'seven'});
99 }));
100 operations.push(transactionStore.get(3).then(function(gone){
101 assert.strictEqual(gone, undefined);
102 }));
103 return all(operations);
104 });
105 });
106 },
107 'multi-store transaction': function(){
108 var operations = [];
109 var transaction = transactionStore.transaction();
110 transactionStore2.transaction();
111 assert.strictEqual(logStore.data.length, 0);
112 operations.push(transactionStore.add(
113 {id: 8, name: 'eight'}
114 ));
115 operations.push(transactionStore2.put(
116 {id: 'a', name: 'A'}
117 ));
118 // make sure the master stores haven't been updated yet
119 assert.strictEqual(masterStore.data.length, 6);
120 assert.strictEqual(masterStore2.data.length, 0);
121
122 return all(operations).then(function(){
123 // but the cache should be updated
124 assert.strictEqual(cachingStore2.data.length, 1);
125 assert.strictEqual(logStore.data.length, 2);
126 return transaction.commit().then(function(){
127 operations = [];
128 operations.push(transactionStore.get(8).then(function(six){
129 assert.deepEqual(six, {id: 8, name: 'eight'});
130 }));
131 operations.push(transactionStore2.get('a').then(function(seven){
132 assert.deepEqual(seven, {id: 'a', name: 'A'});
133 }));
134 return all(operations);
135 });
136 });
137 }
138
139 });
140 });