summaryrefslogtreecommitdiff
path: root/frontend/gamma/tests/tests/Clipperz/KeyValueObjectStore.test.js
Unidiff
Diffstat (limited to 'frontend/gamma/tests/tests/Clipperz/KeyValueObjectStore.test.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/gamma/tests/tests/Clipperz/KeyValueObjectStore.test.js255
1 files changed, 255 insertions, 0 deletions
diff --git a/frontend/gamma/tests/tests/Clipperz/KeyValueObjectStore.test.js b/frontend/gamma/tests/tests/Clipperz/KeyValueObjectStore.test.js
new file mode 100644
index 0000000..53cf5d3
--- a/dev/null
+++ b/frontend/gamma/tests/tests/Clipperz/KeyValueObjectStore.test.js
@@ -0,0 +1,255 @@
1/*
2
3Copyright 2008-2011 Clipperz Srl
4
5This file is part of Clipperz's Javascript Crypto Library.
6Javascript Crypto Library provides web developers with an extensive
7and efficient set of cryptographic functions. The library aims to
8obtain maximum execution speed while preserving modularity and
9reusability.
10For further information about its features and functionalities please
11refer to http://www.clipperz.com
12
13* Javascript Crypto Library is free software: you can redistribute
14 it and/or modify it under the terms of the GNU Affero General Public
15 License as published by the Free Software Foundation, either version
16 3 of the License, or (at your option) any later version.
17
18* Javascript Crypto Library is distributed in the hope that it will
19 be useful, but WITHOUT ANY WARRANTY; without even the implied
20 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 See the GNU Affero General Public License for more details.
22
23* You should have received a copy of the GNU Affero General Public
24 License along with Javascript Crypto Library. If not, see
25 <http://www.gnu.org/licenses/>.
26
27*/
28
29var tests = {
30
31 //-------------------------------------------------------------------------
32
33 'simple_tests': function() {
34 vardeferredResult;
35
36 deferredResult = new Clipperz.Async.Deferred("simple_tests", {trace:false});
37 deferredResult.addCallback(function() {
38 varobjectStore;
39
40 objectStore = new Clipperz.KeyValueObjectStore();
41
42 ok(objectStore != null, "created an object store");
43
44 objectStore.setValue('key', "value");
45 is(objectStore.getValue('key'), "value", "can store and read a value to a simple key");
46
47 objectStore.setValue('key', "overwritten value");
48 is(objectStore.getValue('key'), "overwritten value", "using the same key overwrites the previous value");
49
50 objectStore.setValue('record.keys', [1, 2, 3]);
51 is(
52 MochiKit.Base.compare(objectStore.getValue('record'), {'keys': [1,2,3]}),
53 0,
54 "getting a partial key returns the whole content associate with that key"
55 );
56 is(
57 MochiKit.Base.compare(objectStore.getValue('record.keys'), [1,2,3]),
58 0,
59 "accessing data using a key.path return the matching content"
60 );
61 is(
62 MochiKit.Base.compare(objectStore.getValue('record.keys.1'), 2),
63 0,
64 "accessing data using a key.path return the matching content, even inside an array"
65 );
66
67 is(
68 objectStore.setValue('key', "value"),
69 "value",
70 "setting a value return the value itself, as a convenience to chain deferred methods"
71 );
72
73 is(
74 objectStore.getValue('not_set_key'),
75 null,
76 "accessing a previously undefined key will return null"
77 );
78 is(
79 objectStore.getValue('record.not_set_key'),
80 null,
81 "accessing a previously undefined key will return null, even if part of the path is defined"
82 );
83 is(
84 objectStore.getValue('not_set_path.not_set_key'),
85 null,
86 "accessing a previously undefined key will return null, even if using a completely undefined path"
87 );
88
89 objectStore.removeAllData();
90 is(
91 objectStore.getValue('key'),
92 null,
93 "getting a value after a 'removeAllData' return no value"
94 );
95
96 });
97
98 deferredResult.callback();
99
100 return deferredResult;
101 },
102
103 //-------------------------------------------------------------------------
104
105 'simple_deferredGetOrSet_test': function () {
106 vardeferredResult;
107 varobjectStore;
108 var testValue;
109
110 objectStore = new Clipperz.KeyValueObjectStore();
111 testValue = "nifty test value";
112
113 deferredResult = new Clipperz.Async.Deferred("simple_deferredGetOrSet_test", {trace:false});
114 deferredResult.addMethod(objectStore, 'setValue', 'key', testValue);
115 deferredResult.addMethod(objectStore, 'deferredGetOrSet', 'key', function() {return testValue});
116 deferredResult.addCallback(function(aResult) {
117 SimpleTest.is(aResult, testValue, "deferredGetOrSet works when accessing data already present on the object store");
118 })
119 deferredResult.callback();
120 },
121
122 //-------------------------------------------------------------------------
123
124 'deferredGetOrSet_test': function () {
125 vardeferredResult;
126 varobjectStore;
127 var testValue;
128
129 objectStore = new Clipperz.KeyValueObjectStore();
130 testValue = "nifty test value";
131
132 deferredResult = new Clipperz.Async.Deferred("deferredGetOrSet_test", {trace:false});
133 deferredResult.addMethod(objectStore, 'setValue', 'key.path', testValue);
134 deferredResult.addMethod(objectStore, 'deferredGetOrSet', 'key', function() {return {'path': testValue}; });
135 deferredResult.addCallback(function(aResult) {
136 SimpleTest.is(aResult['path'], testValue, "deferredGetOrSet works when accessing data already present on the object store");
137 })
138 deferredResult.callback();
139 },
140
141 //-------------------------------------------------------------------------
142
143 'simple_deferredGetOrSet_withMissingValue_test': function () {
144 vardeferredResult;
145 varobjectStore;
146 var testValue;
147
148 objectStore = new Clipperz.KeyValueObjectStore();
149 testValue = "nifty test value";
150
151 deferredResult = new Clipperz.Async.Deferred("simple_deferredGetOrSet_withMissingValue_test", {trace:false});
152 deferredResult.addMethod(objectStore, 'deferredGetOrSet', 'key', function() {return testValue});
153 deferredResult.addCallback(function(aResult) {
154 SimpleTest.is(aResult, testValue, "deferredGetOrSet works when accessing data already present on the object store");
155 })
156 deferredResult.callback();
157 },
158
159 //-------------------------------------------------------------------------
160
161 'deferredGetOrSet_withMissingValue_test': function () {
162 vardeferredResult;
163 varobjectStore;
164 var testValue;
165
166 objectStore = new Clipperz.KeyValueObjectStore();
167 testValue = "nifty test value";
168
169 deferredResult = new Clipperz.Async.Deferred("deferredGetOrSet_withMissingValue_test", {trace:false});
170 deferredResult.addMethod(objectStore, 'deferredGetOrSet', 'key', function() {return {'path': testValue}; });
171 deferredResult.addCallback(function(aResult) {
172 SimpleTest.is(aResult['path'], testValue, "deferredGetOrSet works when accessing data already present on the object store");
173 })
174 deferredResult.callback();
175 },
176
177 //-------------------------------------------------------------------------
178
179 'deleteObjectKey': function () {
180 varsomeValues;
181 var objectStore;
182
183 someValues = {
184 'key1': {
185 'key1_1': "value 1.1",
186 'key1_2': "value 1.2"
187 },
188 'key2': {
189 'key2_1': {
190 'key2.1.1': "value 2.1.1",
191 'key2.1.2': "value 2.1.2"
192 },
193 'key2_2': "value 2.2"
194 }
195 }
196
197 objectStore = new Clipperz.KeyValueObjectStore();
198 objectStore.initWithValues(someValues);
199 objectStore.removeValue('key2.key2_1');
200
201 SimpleTest.is(objectStore.getValue('key1.key1_1'), "value 1.1", "The first element is still there");
202 SimpleTest.is(objectStore.getValue('key1.key1_2'), "value 1.2", "The second element is still there");
203 SimpleTest.is(objectStore.getValue('key2.key2_1'), null, "The deleted element is actually gone");
204 SimpleTest.is(MochiKit.Base.keys(objectStore.getValue('key2')).length, 1, "Even the key is not stored anylonger");
205 SimpleTest.is(objectStore.getValue('key2.key2_2'), "value 2.2", "The sibling of the deleted element is still there");
206 },
207
208 //-------------------------------------------------------------------------
209
210 'accessDataUsingANumericKey': function () {
211 varsomeValues;
212 var objectStore;
213
214 someValues = {
215 '1': "value 1",
216 '2': "value 2"
217 }
218
219 objectStore = new Clipperz.KeyValueObjectStore();
220 objectStore.initWithValues(someValues);
221
222 SimpleTest.is(objectStore.getValue(1), "value 1", "The first element is accessed even using a numeric key");
223
224 objectStore.setValue(3, "value 3");
225 SimpleTest.is(objectStore.getValue('3'), "value 3", "I can set the value using a numeric key and get it with a string key");
226 },
227
228 //-------------------------------------------------------------------------
229
230 'isEmpty_test': function () {
231 varsomeValues;
232 var objectStore;
233
234 someValues = {
235 '1': "value 1",
236 '2': "value 2"
237 }
238
239 objectStore = new Clipperz.KeyValueObjectStore();
240 SimpleTest.is(objectStore.isEmpty(), true, "A newly initialized KeyValueStore is empty");
241
242 objectStore.initWithValues(someValues);
243 SimpleTest.is(objectStore.isEmpty(), false, "Once the KeyValueStore is initialized with some values, it is no logner empty");
244
245 objectStore.removeAllData();
246 SimpleTest.is(objectStore.isEmpty(), true, "A KeyValueStore is empty after invoking the 'removeAllData' method");
247 },
248
249 //-------------------------------------------------------------------------
250 'syntaxFix': MochiKit.Base.noop
251};
252
253//#############################################################################
254
255SimpleTest.runDeferredTests("Clipperz.KeyValueObjectStore", tests, {trace:false});