summaryrefslogtreecommitdiff
path: root/frontend/gamma/tests/tests/Clipperz/KeyValueObjectStore.test.js
blob: 53cf5d347eb48a2dd779eb637129fee68fffe0c9 (plain)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*

Copyright 2008-2011 Clipperz Srl

This file is part of Clipperz's Javascript Crypto Library.
Javascript Crypto Library provides web developers with an extensive
and efficient set of cryptographic functions. The library aims to
obtain maximum execution speed while preserving modularity and
reusability.
For further information about its features and functionalities please
refer to http://www.clipperz.com

* Javascript Crypto Library is free software: you can redistribute
  it and/or modify it under the terms of the GNU Affero General Public
  License as published by the Free Software Foundation, either version
  3 of the License, or (at your option) any later version.

* Javascript Crypto Library is distributed in the hope that it will
  be useful, but WITHOUT ANY WARRANTY; without even the implied
  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the GNU Affero General Public License for more details.

* You should have received a copy of the GNU Affero General Public
  License along with Javascript Crypto Library.  If not, see
  <http://www.gnu.org/licenses/>.

*/

var tests = {

    //-------------------------------------------------------------------------

	'simple_tests': function() {
		var	deferredResult;
		
		deferredResult = new Clipperz.Async.Deferred("simple_tests", {trace:false});
		deferredResult.addCallback(function() {
			var	objectStore;
	
			objectStore = new Clipperz.KeyValueObjectStore();
	
			ok(objectStore != null, "created an object store");
	
			objectStore.setValue('key', "value");
			is(objectStore.getValue('key'), "value", "can store and read a value to a simple key");
	
			objectStore.setValue('key', "overwritten value");
			is(objectStore.getValue('key'), "overwritten value", "using the same key overwrites the previous value");

			objectStore.setValue('record.keys', [1, 2, 3]);
			is(
				MochiKit.Base.compare(objectStore.getValue('record'), {'keys': [1,2,3]}),
				0,
				"getting a partial key returns the whole content associate with that key"
			);
			is(
				MochiKit.Base.compare(objectStore.getValue('record.keys'), [1,2,3]),
				0,
				"accessing data using a key.path return the matching content"
			);
			is(
				MochiKit.Base.compare(objectStore.getValue('record.keys.1'), 2),
				0,
				"accessing data using a key.path return the matching content, even inside an array"
			);

			is(
				objectStore.setValue('key', "value"),
				"value",
				"setting a value return the value itself, as a convenience to chain deferred methods"
			);

			is(
				objectStore.getValue('not_set_key'),
				null,
				"accessing a previously undefined key will return null"
			);
			is(
				objectStore.getValue('record.not_set_key'),
				null,
				"accessing a previously undefined key will return null, even if part of the path is defined"
			);
			is(
				objectStore.getValue('not_set_path.not_set_key'),
				null,
				"accessing a previously undefined key will return null, even if using a completely undefined path"
			);

			objectStore.removeAllData();
			is(
				objectStore.getValue('key'),
				null,
				"getting a value after a 'removeAllData' return no value"
			);
			
		});
		
		deferredResult.callback();
		
		return deferredResult;
	},

    //-------------------------------------------------------------------------

	'simple_deferredGetOrSet_test': function () {
		var	deferredResult;
		var	objectStore;
		var testValue;
		
		objectStore = new Clipperz.KeyValueObjectStore();
		testValue = "nifty test value";
		
		deferredResult = new Clipperz.Async.Deferred("simple_deferredGetOrSet_test", {trace:false});
		deferredResult.addMethod(objectStore, 'setValue', 'key', testValue);
		deferredResult.addMethod(objectStore, 'deferredGetOrSet', 'key', function() {return testValue});
		deferredResult.addCallback(function(aResult) {
			SimpleTest.is(aResult, testValue, "deferredGetOrSet works when accessing data already present on the object store");
		})
		deferredResult.callback();
	},

    //-------------------------------------------------------------------------

	'deferredGetOrSet_test': function () {
		var	deferredResult;
		var	objectStore;
		var testValue;
		
		objectStore = new Clipperz.KeyValueObjectStore();
		testValue = "nifty test value";
		
		deferredResult = new Clipperz.Async.Deferred("deferredGetOrSet_test", {trace:false});
		deferredResult.addMethod(objectStore, 'setValue', 'key.path', testValue);
		deferredResult.addMethod(objectStore, 'deferredGetOrSet', 'key', function() {return {'path': testValue}; });
		deferredResult.addCallback(function(aResult) {
			SimpleTest.is(aResult['path'], testValue, "deferredGetOrSet works when accessing data already present on the object store");
		})
		deferredResult.callback();
	},

    //-------------------------------------------------------------------------

	'simple_deferredGetOrSet_withMissingValue_test': function () {
		var	deferredResult;
		var	objectStore;
		var testValue;
		
		objectStore = new Clipperz.KeyValueObjectStore();
		testValue = "nifty test value";
		
		deferredResult = new Clipperz.Async.Deferred("simple_deferredGetOrSet_withMissingValue_test", {trace:false});
		deferredResult.addMethod(objectStore, 'deferredGetOrSet', 'key', function() {return testValue});
		deferredResult.addCallback(function(aResult) {
			SimpleTest.is(aResult, testValue, "deferredGetOrSet works when accessing data already present on the object store");
		})
		deferredResult.callback();
	},

    //-------------------------------------------------------------------------

	'deferredGetOrSet_withMissingValue_test': function () {
		var	deferredResult;
		var	objectStore;
		var testValue;
		
		objectStore = new Clipperz.KeyValueObjectStore();
		testValue = "nifty test value";
		
		deferredResult = new Clipperz.Async.Deferred("deferredGetOrSet_withMissingValue_test", {trace:false});
		deferredResult.addMethod(objectStore, 'deferredGetOrSet', 'key', function() {return {'path': testValue}; });
		deferredResult.addCallback(function(aResult) {
			SimpleTest.is(aResult['path'], testValue, "deferredGetOrSet works when accessing data already present on the object store");
		})
		deferredResult.callback();
	},

    //-------------------------------------------------------------------------

	'deleteObjectKey': function () {
		var	someValues;
		var objectStore;
		
		someValues = {
			'key1': {
				'key1_1': "value 1.1",
				'key1_2': "value 1.2"
			},
			'key2': {
				'key2_1': {
					'key2.1.1': "value 2.1.1",
					'key2.1.2': "value 2.1.2"
				},
				'key2_2': "value 2.2"
			}
		}
		
		objectStore = new Clipperz.KeyValueObjectStore();
		objectStore.initWithValues(someValues);
		objectStore.removeValue('key2.key2_1');
		
		SimpleTest.is(objectStore.getValue('key1.key1_1'), "value 1.1", "The first element is still there");
		SimpleTest.is(objectStore.getValue('key1.key1_2'), "value 1.2", "The second element is still there");
		SimpleTest.is(objectStore.getValue('key2.key2_1'), null, "The deleted element is actually gone");
		SimpleTest.is(MochiKit.Base.keys(objectStore.getValue('key2')).length, 1, "Even the key is not stored anylonger");
		SimpleTest.is(objectStore.getValue('key2.key2_2'), "value 2.2", "The sibling of the deleted element is still there");
	},

    //-------------------------------------------------------------------------

	'accessDataUsingANumericKey': function () {
		var	someValues;
		var objectStore;
		
		someValues = {
			'1': "value 1",
			'2': "value 2"
		}
		
		objectStore = new Clipperz.KeyValueObjectStore();
		objectStore.initWithValues(someValues);
		
		SimpleTest.is(objectStore.getValue(1), "value 1", "The first element is accessed even using a numeric key");
		
		objectStore.setValue(3, "value 3");
		SimpleTest.is(objectStore.getValue('3'), "value 3", "I can set the value using a numeric key and get it with a string key");
	},

    //-------------------------------------------------------------------------

	'isEmpty_test': function () {
		var	someValues;
		var objectStore;
		
		someValues = {
			'1': "value 1",
			'2': "value 2"
		}
		
		objectStore = new Clipperz.KeyValueObjectStore();
		SimpleTest.is(objectStore.isEmpty(), true, "A newly initialized KeyValueStore is empty");

		objectStore.initWithValues(someValues);
		SimpleTest.is(objectStore.isEmpty(), false, "Once the KeyValueStore is initialized with some values, it is no logner empty");
		
		objectStore.removeAllData();
		SimpleTest.is(objectStore.isEmpty(), true, "A KeyValueStore is empty after invoking the 'removeAllData' method");
	},

    //-------------------------------------------------------------------------
    'syntaxFix': MochiKit.Base.noop
};

//#############################################################################

SimpleTest.runDeferredTests("Clipperz.KeyValueObjectStore", tests, {trace:false});