1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4
<title>Recordset Tests</title>
5
<script type="text/javascript" src="../../../build/yui/yui-min.js"></script>
8
<body class="yui3-skin-sam">
9
<h1>Recordset Tests</h1>
10
<p><input type="button" value="Run Tests" id="btnRun" disabled=true></p>
12
<script type="text/javascript">
15
filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min',
17
}).use("console", "test", "dump", "recordset", function(Y) {
21
var ASSERT = Y.Assert,
22
ARRAYASSERT = Y.ArrayAssert,
23
BTNRUN = Y.one("#btnRun");
24
BTNRUN.set("disabled", false);
25
Y.on("click", function(e){
28
var myConsole = new Y.Console().render();
31
var testBasic = new Y.Test.Case({
33
initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
35
//---------------------------------------------
36
// Setup and tear down
37
//---------------------------------------------
42
this.rs = new Y.Recordset({records:this.initialData});
44
//Some Ways to access recordset properties
45
//Y.log(rs.getRecordByIndex(0).getValue('a'));
46
//Y.log(rs.get('records').length);
49
tearDown : function () {
53
//---------------------------------------------
55
//---------------------------------------------
57
changeTest: function(i) {
58
this.rs.on('change', function() {
59
//Y.Assert.areEqual(e.index, i);
63
emptyTest: function() {
65
this.rs.on('empty', function() {
67
Y.Assert.isTrue(flag);
72
//---------------------------------------------
74
//---------------------------------------------
76
testCreateEmptyRecordset: function() {
77
var rs = new Y.Recordset(),
80
rs.on('remove', function(e) {
84
//instantiate empty recordset
85
Y.Assert.isTrue(rs instanceof Y.Recordset);
88
//perform operations on it
89
rs.add({a:1, b:2, c:3});
90
Y.ObjectAssert.areEqual(rs.getRecord(0).getValue(), {a:1, b:2, c:3});
92
rs.add([{a:'hey', b:'yo', c:'hi!'}, {a:2, b:5, c:6}]);
93
Y.ObjectAssert.areEqual(rs.getRecord(2).getValue(), {a:2, b:5, c:6});
94
Y.Assert.areEqual(3, rs.getLength());
97
Y.Assert.areEqual(2, rs.getLength());
98
Y.ObjectAssert.areEqual(rem[0].getValue(), {a:2, b:5, c:6});
102
//---------------------------------------------
104
//---------------------------------------------
106
testGetRecords: function() {
107
var newRecord,newRecord1, newRecord2, id;
108
//this.rs.get('records');
109
// var newrs = new Y.Recordset({records: [{a:10,b:10,c:10}]});
110
// newrs.get('records');
113
newRecord1 = this.rs.getRecordByIndex(1);
114
newRecord2 = this.rs.getRecord(1);
116
Y.ObjectAssert.areEqual(newRecord1.getValue(), this.initialData[1]);
117
Y.ObjectAssert.areEqual(newRecord1, newRecord2);
121
newRecord = this.rs.getRecordsByIndex(1,2);
122
Y.ObjectAssert.areEqual(newRecord[0].getValue(), this.initialData[1]);
123
Y.ObjectAssert.areEqual(newRecord[1].getValue(), this.initialData[2]);
126
id = this.rs.getRecordByIndex(0).get('id');
127
newRecord = this.rs.getRecord(id);
128
Y.ObjectAssert.areEqual(newRecord, this.rs.getRecordByIndex(0));
131
//---------------------------------------------
133
//---------------------------------------------
135
testAddSingleRecordToEnd: function() {
136
var recToAdd = {a:'8', b:'9', c:'10'}, retval;
137
retval = this.rs.add(recToAdd);
138
//Test Recordset Length
139
Y.Assert.areEqual(4, this.rs.get('records').length, "Array lengths not equal.");
140
//Assert on last object
142
//This is indirectly checking to make sure that the record that got added has the identical data as the object literal that was passed in.
143
Y.ObjectAssert.areEqual(recToAdd, this.rs.getRecordByIndex(3).getValue());
144
Y.ObjectAssert.areEqual(retval.getRecordByIndex(3).getValue(3), recToAdd);
149
testAddSingleRecordToIndex: function() {
151
recToAdd = {a:'8', b:'9', c:'10'};
153
retval = this.rs.add(recToAdd,i);
154
Y.Assert.areEqual(recToAdd, this.rs.getRecordByIndex(i).getValue());
156
//assertion with output
157
Y.ObjectAssert.areEqual(retval.getRecordByIndex(i).getValue(), recToAdd);
165
testAddMultipleRecordsToEnd: function() {
166
var recsToAdd = [{a:'11', b:'22', c:'33'}, {a:'44', b:'55', c:'66'}];
167
retval = this.rs.add(recsToAdd);
169
//Assertions with recordset
170
Y.ObjectAssert.areEqual(recsToAdd[0], this.rs.getRecordByIndex(3).getValue());
171
Y.ObjectAssert.areEqual(recsToAdd[1], this.rs.getRecordByIndex(4).getValue());
173
//assertions with output
174
Y.ObjectAssert.areEqual(retval.getRecordByIndex(3).getValue(), recsToAdd[0]);
175
Y.ObjectAssert.areEqual(retval.getRecordByIndex(4).getValue(), recsToAdd[1]);
181
testAddMultipleRecordsToIndex: function() {
183
recsToAdd = [{a:'11', b:'22', c:'33'}, {a:'44', b:'55', c:'66'}];
186
retval = this.rs.add(recsToAdd, i);
188
//Assertions with recordset
189
Y.ObjectAssert.areEqual(recsToAdd[0], this.rs.getRecordByIndex(1).getValue());
190
Y.ObjectAssert.areEqual(recsToAdd[1], this.rs.getRecordByIndex(2).getValue());
195
//---------------------------------------------
197
//---------------------------------------------
199
testDeleteSingleRecordFromEnd: function() {
201
this.rs.on('remove', function(e) {
207
Y.ObjectAssert.areEqual(this.initialData[2], rem.removed[0].getValue());
208
Y.Assert.areEqual(2, rem.index);
211
testDeleteSingleRecordFromIndex: function() {
214
this.rs.on('remove', function(e) {
218
Y.ObjectAssert.areEqual(this.initialData[1], rem.removed[0].getValue());
219
Y.Assert.areEqual(1, rem.index);
222
testDeleteRangeOfRecords: function() {
223
//Delete 2 records from index 1
225
this.rs.on('remove', function(e) {
232
Y.ObjectAssert.areEqual(this.initialData[1], rem.removed[0].getValue());
233
Y.ObjectAssert.areEqual(this.initialData[2], rem.removed[1].getValue());
234
Y.Assert.areEqual(1, rem.index);
237
//---------------------------------------------
239
//---------------------------------------------
241
testEmptyRecordSet: function() {
245
Y.Assert.areEqual(0, this.rs.get('records').length);
249
//---------------------------------------------
251
//---------------------------------------------
253
testGetValuesByKey: function() {
256
retval = this.rs.getValuesByKey(key);
258
for (i=0; i < this.initialData.length; i++) {
259
Y.Assert.areEqual(this.initialData[i][key], retval[i]);
263
testGetValuesByKeyWithInvalidKey: function() {
265
retval = this.rs.getValuesByKey(key);
267
for (i=0; i < this.initialData.length; i++) {
268
Y.Assert.isUndefined(retval[i]);
272
testUpdateRecordAtIndex: function() {
273
var newRecord, oldRecord, index=1;
274
var o = {a:'newG', b:'newH', c:'newI'};
275
newRecord = new Y.Record({data:o});
276
oldRecord = this.rs.getRecordByIndex(index);
278
this.rs.on('update', function(e) {
279
Y.ObjectAssert.areEqual(newRecord, e.updated[0], 'Updated record is != to new record');
280
Y.ObjectAssert.areEqual(oldRecord, e.overwritten[0], 'Overwritten record is != to old record');
281
Y.Assert.areEqual(1, e.overwritten.length, 'overwritten array is not of expected length');
282
Y.Assert.areEqual(1, e.updated.length);
283
Y.Assert.areEqual(index, e.index, "correct index was modified");
287
this.rs.update(newRecord, index);
288
Y.ObjectAssert.areEqual(newRecord.getValue(), this.rs.getRecordByIndex(index).getValue());
291
testUpdateRecordAtIndices:function(){
292
var newRecords = [], index=0, oldRecord = [], a, b;
293
oldRecord = this.rs.getRecordsByIndex(index,2);
294
a = new Y.Record({data:{a:'newA', b:'newB', c:'newC'}});
295
b = new Y.Record({data:{a:'newD', b:'newE', c:'newF'}});
300
this.rs.on('update', function(e) {
301
Y.ObjectAssert.areEqual(newRecords, e.updated, 'Updated record is != to new record');
302
Y.ObjectAssert.areEqual(oldRecord, e.overwritten, 'Overwritten record is != to old record');
303
Y.Assert.areEqual(2, e.overwritten.length, 'overwritten array is not of expected length');
304
Y.Assert.areEqual(index, e.index, "correct index was modified");
307
this.rs.update(newRecords, index);
309
//check that the two elements in the recordset are the same as the ones pushed in
310
Y.ObjectAssert.areEqual(a.getValue(), this.rs.getRecordByIndex(0).getValue());
311
Y.ObjectAssert.areEqual(b.getValue(), this.rs.getRecordByIndex(1).getValue());
312
Y.ObjectAssert.areEqual(this.initialData[2], this.rs.getRecordByIndex(2).getValue());
314
//3 initial records + 1 more added (the other was just over-written)= 4 total records in recordset
315
Y.Assert.areEqual(3, this.rs.get('records').length);
320
//---------------------------------------------
322
//---------------------------------------------
324
testDefaultHash: function() {
326
var hashTable = this.rs.get('table');
328
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(0).get('id')], this.rs.getRecordByIndex(0));
329
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(1).get('id')], this.rs.getRecordByIndex(1));
330
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(2).get('id')], this.rs.getRecordByIndex(2));
331
Y.Assert.areEqual(3, Y.Object.size(hashTable));
333
//check hashtable sync when adding
334
this.rs.add({a:'hello', b:'hey', c:'yo!'});
335
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(0).get('id')], this.rs.getRecordByIndex(0));
336
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(1).get('id')], this.rs.getRecordByIndex(1));
337
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(2).get('id')], this.rs.getRecordByIndex(2));
338
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(3).get('id')], this.rs.getRecordByIndex(3));
339
Y.Assert.areEqual(4, Y.Object.size(hashTable));
342
//check hashtable sync when removing 1 object
344
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(0).get('id')], this.rs.getRecordByIndex(0));
345
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(1).get('id')], this.rs.getRecordByIndex(1));
346
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(2).get('id')], this.rs.getRecordByIndex(2));
347
Y.Assert.areEqual(3, Y.Object.size(hashTable));
349
//check hashtable sync when removing multiple records
351
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(0).get('id')], this.rs.getRecordByIndex(0));
352
Y.Assert.areEqual(1, Y.Object.size(hashTable));
355
//update single record
356
this.rs.update({a:'new', b:'record', c:'added'}, 0);
357
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(0).get('id')], this.rs.getRecordByIndex(0));
358
//Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(1).get('id')], this.rs.getRecordByIndex(1));
360
//Y.Assert.areEqual(2, Y.Object.size(hashTable));
361
Y.Assert.areEqual(1, Y.Object.size(hashTable));
364
{a:'new', b:'record', c:'added'},
365
{a:'another', b:'new', c:'record'}
368
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(0).get('id')], this.rs.getRecordByIndex(0));
369
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(1).get('id')], this.rs.getRecordByIndex(1));
370
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(2).get('id')], this.rs.getRecordByIndex(2));
371
Y.Assert.areEqual(3, Y.Object.size(hashTable));
377
//---------------------------------------------
379
//---------------------------------------------
382
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
383
testDefaultSort: function() {
384
this.rs.plug(Y.Plugin.RecordsetSort);
386
//Test ascending order
387
Y.Assert.isFalse(this.rs.sort.get('isSorted'));
390
this.rs.sort.sort('a',false);
392
Y.Assert.isTrue(this.rs.sort.get('isSorted'));
394
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, this.rs.getRecordByIndex(0).getValue());
395
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, this.rs.getRecordByIndex(1).getValue());
396
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, this.rs.getRecordByIndex(2).getValue());
397
Y.Assert.areEqual(3, this.rs.getLength());
400
//Test Descending Order
401
this.rs.sort.sort('c',true);
402
Y.Assert.isTrue(this.rs.sort.get('isSorted'));
404
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, this.rs.getRecordByIndex(0).getValue());
405
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, this.rs.getRecordByIndex(1).getValue());
406
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, this.rs.getRecordByIndex(2).getValue());
407
Y.Assert.areEqual(3, this.rs.getLength());
410
//Test sort when values are equal (sorts by ID)
411
this.rs.sort.sort('b',true);
412
Y.Assert.isTrue(this.rs.sort.get('isSorted'));
414
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, this.rs.getRecordByIndex(0).getValue());
415
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, this.rs.getRecordByIndex(1).getValue());
416
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, this.rs.getRecordByIndex(2).getValue());
417
Y.Assert.areEqual(3, this.rs.getLength());
422
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}]
423
testResort: function() {
424
this.rs.plug(Y.Plugin.RecordsetSort);
426
Y.Assert.isFalse(this.rs.sort.get('isSorted'));
428
//Test ascending order
429
this.rs.sort.sort('a',false);
430
Y.Assert.isTrue(this.rs.sort.get('isSorted'));
432
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, this.rs.getRecordByIndex(0).getValue());
433
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, this.rs.getRecordByIndex(1).getValue());
434
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, this.rs.getRecordByIndex(2).getValue());
435
Y.Assert.areEqual(3, this.rs.getLength());
437
//Add another record to the end of the recordset
438
this.rs.add({a:6,b:5,c:8});
439
Y.Assert.isFalse(this.rs.sort.get('isSorted'));
441
Y.ObjectAssert.areEqual({a:6,b:5,c:8}, this.rs.getRecordByIndex(3).getValue());
444
this.rs.sort.resort();
445
Y.Assert.isTrue(this.rs.sort.get('isSorted'));
447
//make sure the added record was sorted into the recordset using the existing sort properties
448
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, this.rs.getRecordByIndex(0).getValue());
449
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, this.rs.getRecordByIndex(1).getValue());
450
Y.ObjectAssert.areEqual({a:6,b:5,c:8}, this.rs.getRecordByIndex(2).getValue());
451
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, this.rs.getRecordByIndex(3).getValue());
452
Y.Assert.areEqual(4, this.rs.getLength());
455
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}
456
testReverseBeforeSort: function() {
457
a = {a:'a', b:'b', c:'c'},
458
b = {a:'d', b:'e', c:'f'},
459
c = {a:'g', b:'h', c:'i'},
460
d = {a:'j', b:'k', c:'l'};
461
this.rs.plug(Y.Plugin.RecordsetSort);
463
//adding in some records backwards to understand if reversing is working properly
464
this.rs.add([d,c,b,a]);
465
this.rs.sort.reverse();
467
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, this.rs.getRecordByIndex(6).getValue());
468
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, this.rs.getRecordByIndex(5).getValue());
469
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, this.rs.getRecordByIndex(4).getValue());
471
Y.ObjectAssert.areEqual(d, this.rs.getRecordByIndex(3).getValue());
472
Y.ObjectAssert.areEqual(c, this.rs.getRecordByIndex(2).getValue());
473
Y.ObjectAssert.areEqual(b, this.rs.getRecordByIndex(1).getValue());
474
Y.ObjectAssert.areEqual(a, this.rs.getRecordByIndex(0).getValue());
476
Y.Assert.areEqual(7, this.rs.getLength());
479
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}
480
testFlipAfterSort: function() {
481
a = {a:1, b:'b', c:'c'},
482
b = {a:2, b:'e', c:'f'},
483
c = {a:3, b:'h', c:'i'},
484
d = {a:4, b:'k', c:'l'};
485
this.rs.plug(Y.Plugin.RecordsetSort);
487
//adding in some records backwards to understand if reversing is working properly
488
this.rs.add([d,c,b,a]);
490
this.rs.sort.sort('a',true);
494
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, this.rs.getRecordByIndex(0).getValue());
495
Y.ObjectAssert.areEqual(a, this.rs.getRecordByIndex(1).getValue());
497
Y.ObjectAssert.areEqual(b, this.rs.getRecordByIndex(2).getValue());
499
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, this.rs.getRecordByIndex(3).getValue());
500
Y.ObjectAssert.areEqual(c, this.rs.getRecordByIndex(4).getValue());
501
Y.ObjectAssert.areEqual(d, this.rs.getRecordByIndex(5).getValue());
502
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, this.rs.getRecordByIndex(6).getValue());
504
Y.Assert.areEqual(7, this.rs.getLength());
507
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
508
testCustomSort: function() {
511
var a = {a:1, b:1, c:4},
515
//This is just a copy of Y.ArraySort.compare
516
compare = function(a, b, desc) {
517
if(!Y.Lang.isValue(a)) {
518
if(!Y.Lang.isValue(b)) {
525
else if(!Y.Lang.isValue(b)) {
529
if(Y.Lang.isString(a)) {
532
if(Y.Lang.isString(b)) {
536
return (desc) ? 1 : -1;
539
return (desc) ? -1 : 1;
547
//custom sort where if the values in the given fieldA are equal, the values in fieldB are checked
548
customsort = function(recA, recB, field, desc) {
549
var first_sorted = compare(recA.getValue(field), recB.getValue(field), desc);
550
if(first_sorted === 0) {
551
var second_sorted = compare(recA.getValue('b'), recB.getValue('b'), desc);
552
if (second_sorted === 0) {
553
return compare(recA.get("id"), recB.get("id"), desc);
556
return second_sorted;
564
this.rs.plug(Y.Plugin.RecordsetSort);
565
this.rs.add([a,b,c,d]);
566
this.rs.sort.sort('a',false,customsort);
570
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
571
//when the numbers are equal - the object that was added later should be first
572
Y.ObjectAssert.areEqual({a:1, b:1, c:4}, this.rs.getRecordByIndex(0).getValue());
573
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, this.rs.getRecordByIndex(1).getValue());
574
Y.ObjectAssert.areEqual({a:2, b:2, c:5}, this.rs.getRecordByIndex(2).getValue());
575
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, this.rs.getRecordByIndex(3).getValue());
576
Y.ObjectAssert.areEqual({a:3, b:2, c:4}, this.rs.getRecordByIndex(4).getValue());
577
Y.ObjectAssert.areEqual({a:4, b:4, c:7}, this.rs.getRecordByIndex(5).getValue());
578
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, this.rs.getRecordByIndex(6).getValue());
580
Y.Assert.areEqual(7, this.rs.getLength());
583
//---------------------------------------------
585
//---------------------------------------------
589
testSize: function() {
590
Y.Assert.areEqual(3, this.rs.getLength());
593
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
594
testEach: function() {
595
var i = 0, recs = [];
596
this.rs.each(function() {
597
recs[i] = this.getValue();
601
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, recs[0]);
602
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, recs[1]);
603
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, recs[2]);
604
Y.Assert.areEqual(3, recs.length);
607
testSome: function() {
608
var recs=[], i=0, v={};
611
this.rs.some(function() {
625
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, recs[0]);
626
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, recs[1]);
627
Y.Assert.areEqual(2, recs.length);
630
//---------------------------------------------
632
//---------------------------------------------
634
testFilter: function() {
635
this.rs.plug(Y.Plugin.RecordsetFilter);
636
var validator = function(item) {
637
if (item.getValue('b') === 2) {
644
rs = this.rs.filter.filter(validator);
646
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, rs.getRecordByIndex(0).getValue());
647
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, rs.getRecordByIndex(1).getValue());
648
Y.Assert.areEqual(2, rs.getLength());
650
//test a filter that doesn't return anything from recordset
651
validator = function(item) {
652
if (item.getValue('c') === 2) {
660
rs = this.rs.filter.filter(validator);
661
Y.Assert.areEqual(0, rs.getLength());
664
//Test Filter with Key Value pair
665
rs = this.rs.filter.filter('b',2);
666
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, rs.getRecordByIndex(0).getValue());
667
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, rs.getRecordByIndex(1).getValue());
668
Y.Assert.areEqual(2, rs.getLength());
672
testReject: function() {
673
this.rs.plug(Y.Plugin.RecordsetFilter);
675
var validator = function(item) {
676
if (item.getValue('b') === 2) {
684
rs = this.rs.filter.reject(validator);
686
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
687
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, rs.getRecordByIndex(0).getValue());
688
Y.Assert.areEqual(1, rs.getLength());
691
//try it with a validator that always returns false
692
validator = function(item) {
693
if (item.getValue('c') === 2) {
701
rs = this.rs.filter.reject(validator);
702
Y.ObjectAssert.areEqual(this.rs.getRecordByIndex(0).getValue(), rs.getRecordByIndex(0).getValue());
703
Y.ObjectAssert.areEqual(this.rs.getRecordByIndex(1).getValue(), rs.getRecordByIndex(1).getValue());
704
Y.ObjectAssert.areEqual(this.rs.getRecordByIndex(2).getValue(), rs.getRecordByIndex(2).getValue());
706
Y.Assert.areEqual(this.rs.getLength(), rs.getLength());
711
//Map is tested on the actual array because it returns an array of booleans typically (atleast that's the use case I can think of)
712
testMap: function() {
713
var f = function(item) {
714
if (item.getValue('b') === 2) {
721
rs = Y.Array.map(this.rs.get('records'),f);
722
Y.Assert.areEqual(true, rs[0]);
723
Y.Assert.areEqual(false, rs[1]);
724
Y.Assert.areEqual(true, rs[2]);
725
Y.Assert.areEqual(3, rs.length);
728
//---------------------------------------------
730
//---------------------------------------------
732
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
734
testCustomHashTableInitialization: function() {
735
this.rs.plug(Y.Plugin.RecordsetIndexer);
737
var hashA = this.rs.indexer.createTable('a');
738
var hashTables = this.rs.indexer.get('hashTables');
741
Y.ObjectAssert.areEqual(hashA[3].getValue(), {a:3, b:2, c:1});
742
Y.ObjectAssert.areEqual(hashA[9].getValue(), {a:9, b:8, c:7});
743
Y.ObjectAssert.areEqual(hashA[1].getValue(), {a:1, b:2, c:3});
745
//check main table (contains all the subtables)
746
Y.ObjectAssert.areEqual(hashTables.a[3].getValue(), {a:3, b:2, c:1});
747
Y.ObjectAssert.areEqual(hashTables.a[9].getValue(), {a:9, b:8, c:7});
748
Y.ObjectAssert.areEqual(hashTables.a[1].getValue(), {a:1, b:2, c:3});
750
Y.ObjectAssert.areEqual(hashA, hashTables.a);
754
var hashB = this.rs.indexer.createTable('b');
756
Y.ObjectAssert.areEqual(hashTables.b[2].getValue(), {a:1, b:2, c:3});
757
Y.ObjectAssert.areEqual(hashTables.b[8].getValue(), {a:9, b:8, c:7});
758
Y.ObjectAssert.areEqual(hashB[2].getValue(), {a:1, b:2, c:3});
759
Y.ObjectAssert.areEqual(hashB[8].getValue(), {a:9, b:8, c:7});
760
Y.ObjectAssert.areEqual(hashB, hashTables.b);
764
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
765
testCustomHashTableAddingRecord: function() {
766
this.rs.plug(Y.Plugin.RecordsetIndexer);
768
var hashA = this.rs.indexer.createTable('a'),
769
hashC = this.rs.indexer.createTable('c'),
770
hashTables = this.rs.indexer.get('hashTables');
773
this.rs.add({a:'adding', b:'this', c:'record'});
775
Y.ObjectAssert.areEqual(hashA[3].getValue(), {a:3, b:2, c:1});
776
Y.ObjectAssert.areEqual(hashA[9].getValue(), {a:9, b:8, c:7});
777
Y.ObjectAssert.areEqual(hashA[1].getValue(), {a:1, b:2, c:3});
778
Y.ObjectAssert.areEqual(hashA['adding'].getValue(), {a:'adding', b:'this', c:'record'});
779
Y.Assert.areEqual(4, Y.Object.size(hashA));
780
Y.ObjectAssert.areEqual(hashA, hashTables.a);
783
Y.ObjectAssert.areEqual(hashC[1].getValue(), {a:3, b:2, c:1});
784
Y.ObjectAssert.areEqual(hashC[7].getValue(), {a:9, b:8, c:7});
785
Y.ObjectAssert.areEqual(hashC[3].getValue(), {a:1, b:2, c:3});
786
Y.ObjectAssert.areEqual(hashC['record'].getValue(), {a:'adding', b:'this', c:'record'});
787
Y.Assert.areEqual(4, Y.Object.size(hashC));
788
Y.ObjectAssert.areEqual(hashC, hashTables.c);
791
//adding record to a specific index
792
this.rs.add({a:'another', b:'new', c:'record'}, 0);
794
Y.ObjectAssert.areEqual(hashA[3].getValue(), {a:3, b:2, c:1});
795
Y.ObjectAssert.areEqual(hashA[9].getValue(), {a:9, b:8, c:7});
796
Y.ObjectAssert.areEqual(hashA[1].getValue(), {a:1, b:2, c:3});
797
Y.ObjectAssert.areEqual(hashA['adding'].getValue(), {a:'adding', b:'this', c:'record'});
798
Y.ObjectAssert.areEqual(hashA['another'].getValue(), {a:'another', b:'new', c:'record'});
799
Y.Assert.areEqual(5, Y.Object.size(hashA));
800
Y.ObjectAssert.areEqual(hashA, hashTables.a);
803
//'record' will overwrite the previous added record.
804
Y.ObjectAssert.areEqual(hashC[1].getValue(), {a:3, b:2, c:1});
805
Y.ObjectAssert.areEqual(hashC[7].getValue(), {a:9, b:8, c:7});
806
Y.ObjectAssert.areEqual(hashC[3].getValue(), {a:1, b:2, c:3});
807
Y.ObjectAssert.areEqual(hashC['record'].getValue(), {a:'another', b:'new', c:'record'});
808
Y.Assert.areEqual(4, Y.Object.size(hashC));
809
Y.ObjectAssert.areEqual(hashC, hashTables.c);
811
//adding multiple records
812
this.rs.add([{a:'blah', b:'bloo', c:'bleh'},{a:'heres', b:'another', c:'one'}]);
814
Y.ObjectAssert.areEqual(hashA[3].getValue(), {a:3, b:2, c:1});
815
Y.ObjectAssert.areEqual(hashA[9].getValue(), {a:9, b:8, c:7});
816
Y.ObjectAssert.areEqual(hashA[1].getValue(), {a:1, b:2, c:3});
817
Y.ObjectAssert.areEqual(hashA['adding'].getValue(), {a:'adding', b:'this', c:'record'});
818
Y.ObjectAssert.areEqual(hashA['another'].getValue(), {a:'another', b:'new', c:'record'});
819
Y.ObjectAssert.areEqual(hashA['blah'].getValue(), {a:'blah', b:'bloo', c:'bleh'});
820
Y.ObjectAssert.areEqual(hashA['heres'].getValue(), {a:'heres', b:'another', c:'one'});
822
Y.Assert.areEqual(7, Y.Object.size(hashA));
823
Y.ObjectAssert.areEqual(hashA, hashTables.a);
826
//'record' will overwrite the previous added record.
827
Y.ObjectAssert.areEqual(hashC[1].getValue(), {a:3, b:2, c:1});
828
Y.ObjectAssert.areEqual(hashC[7].getValue(), {a:9, b:8, c:7});
829
Y.ObjectAssert.areEqual(hashC[3].getValue(), {a:1, b:2, c:3});
830
Y.ObjectAssert.areEqual(hashC['record'].getValue(), {a:'another', b:'new', c:'record'});
831
Y.ObjectAssert.areEqual(hashC['bleh'].getValue(), {a:'blah', b:'bloo', c:'bleh'});
832
Y.ObjectAssert.areEqual(hashC['one'].getValue(), {a:'heres', b:'another', c:'one'});
833
Y.Assert.areEqual(6, Y.Object.size(hashC));
834
Y.ObjectAssert.areEqual(hashC, hashTables.c);
839
testCustomHashTableRemovingRecord: function() {
840
this.rs.plug(Y.Plugin.RecordsetIndexer);
842
var hashA = this.rs.indexer.createTable('a'),
843
hashC = this.rs.indexer.createTable('c'),
844
hashTables = this.rs.indexer.get('hashTables');
849
Y.ObjectAssert.areEqual(hashA[3].getValue(), {a:3, b:2, c:1});
850
Y.ObjectAssert.areEqual(hashA[9].getValue(), {a:9, b:8, c:7});
851
Y.Assert.areEqual(2, Y.Object.size(hashA));
852
Y.ObjectAssert.areEqual(hashA, hashTables.a);
854
//'record' will overwrite the previous added record.
855
Y.ObjectAssert.areEqual(hashC[1].getValue(), {a:3, b:2, c:1});
856
Y.ObjectAssert.areEqual(hashC[7].getValue(), {a:9, b:8, c:7});
857
Y.Assert.areEqual(2, Y.Object.size(hashC));
858
Y.ObjectAssert.areEqual(hashC, hashTables.c);
861
//try removing more than 1 record at a time
862
this.rs.add({a:1, b:2, c:3});
866
Y.ObjectAssert.areEqual(hashA[1].getValue(), {a:1, b:2, c:3});
867
Y.Assert.areEqual(1, Y.Object.size(hashA));
868
Y.ObjectAssert.areEqual(hashA, hashTables.a);
870
Y.ObjectAssert.areEqual(hashC[3].getValue(), {a:1, b:2, c:3});
871
Y.Assert.areEqual(1, Y.Object.size(hashC));
872
Y.ObjectAssert.areEqual(hashC, hashTables.c);
876
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
878
testCustomHashTableUpdatingRecord: function() {
879
this.rs.plug(Y.Plugin.RecordsetIndexer);
881
var hashA = this.rs.indexer.createTable('a'),
882
hashC = this.rs.indexer.createTable('c'),
883
hashTables = this.rs.indexer.get('hashTables');
885
this.rs.update({a:'one', b:'record', c:'added'}, 1);
887
Y.ObjectAssert.areEqual(hashA[3].getValue(), {a:3, b:2, c:1});
888
Y.ObjectAssert.areEqual(hashA[1].getValue(), {a:1, b:2, c:3});
889
Y.ObjectAssert.areEqual(hashA['one'].getValue(), {a:'one', b:'record', c:'added'});
890
Y.Assert.areEqual(3, Y.Object.size(hashA));
891
Y.ObjectAssert.areEqual(hashA, hashTables.a);
894
//'record' will overwrite the previous added record.
895
Y.ObjectAssert.areEqual(hashC[1].getValue(), {a:3, b:2, c:1});
896
Y.ObjectAssert.areEqual(hashC[3].getValue(), {a:1, b:2, c:3});
897
Y.ObjectAssert.areEqual(hashC['added'].getValue(), {a:'one', b:'record', c:'added'});
898
Y.Assert.areEqual(3, Y.Object.size(hashC));
899
Y.ObjectAssert.areEqual(hashC, hashTables.c);
901
//update multiple records
902
this.rs.update([{a:'adding', b:'this', c:'record'}, {a:'heres', b:'another', c:'one'}], 2);
904
Y.ObjectAssert.areEqual(hashA[3].getValue(), {a:3, b:2, c:1});
905
Y.ObjectAssert.areEqual(hashA['adding'].getValue(), {a:'adding', b:'this', c:'record'});
906
Y.ObjectAssert.areEqual(hashA['heres'].getValue(), {a:'heres', b:'another', c:'one'});
907
Y.Assert.areEqual(4, Y.Object.size(hashA));
908
Y.ObjectAssert.areEqual(hashA, hashTables.a);
911
//'record' will overwrite the previous added record.
912
Y.ObjectAssert.areEqual(hashC[1].getValue(), {a:3, b:2, c:1});
913
Y.ObjectAssert.areEqual(hashC['record'].getValue(), {a:'adding', b:'this', c:'record'});
914
Y.ObjectAssert.areEqual(hashC['one'].getValue(), {a:'heres', b:'another', c:'one'});
915
Y.Assert.areEqual(4, Y.Object.size(hashC));
916
Y.ObjectAssert.areEqual(hashC, hashTables.c);
924
var suite = new Y.Test.Suite({name:"Recordset Test Suite"});
925
suite.add(testBasic);
927
Y.Test.Runner.setName("Recordset Test Runner");
928
Y.Test.Runner.add(suite);