recordset.html revision 5d8d3401ad8bc8975c27975c7fffc1c4d18cf81c
<html>
<head>
<title>Recordset Tests</title>
</head>
<body class="yui3-skin-sam">
<h1>Recordset Tests</h1>
<p><input type="button" value="Run Tests" id="btnRun" disabled=true></p>
<script type="text/javascript">
(function() {
YUI({
base: "/build/",
filter: "debug",
logInclude:{"TestRunner":true},
useConsole: true
}).use("console", "test", "dump", "recordset", function(Y) {
// Set up the page
var ASSERT = Y.Assert,
ARRAYASSERT = Y.ArrayAssert,
BTNRUN = Y.get("#btnRun");
BTNRUN.set("disabled", false);
Y.on("click", function(e){
}, BTNRUN);
var myConsole = new Y.Console().render();
var testBasic = new Y.Test.Case({
name: "API Tests",
initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
//---------------------------------------------
// Setup and tear down
//---------------------------------------------
setUp : function () {
//create recordset
//Some Ways to access recordset properties
//Y.log(rs.getRecordByIndex(0).getValue('a'));
},
tearDown : function () {
delete this.rs;
},
//---------------------------------------------
// Event Helpers
//---------------------------------------------
changeTest: function(i) {
this.rs.on('change', function() {
//Y.Assert.areEqual(e.index, i);
});
},
emptyTest: function() {
var flag = false;
this.rs.on('empty', function() {
flag = true;
Y.Assert.isTrue(flag);
});
},
//---------------------------------------------
// Get Records
//---------------------------------------------
testGetRecords: function() {
var newRecord, id;
//Single Record
newRecord = this.rs.getRecordByIndex(1);
//Multiple Records
newRecord = this.rs.getRecordsByIndex(1,2);
Y.ObjectAssert.areEqual(newRecord[0].getValue(), this.initialData[1]);
Y.ObjectAssert.areEqual(newRecord[1].getValue(), this.initialData[2]);
//By ID
id = this.rs.getRecordByIndex(0).getValue('id');
newRecord = this.rs.getRecord(id);
Y.ObjectAssert.areEqual(newRecord, this.rs.getRecordByIndex(0));
},
//---------------------------------------------
// Add Records
//---------------------------------------------
testAddSingleRecordToEnd: function() {
var recToAdd = {a:'8', b:'9', c:'10'}, retval;
retval = this.rs.add(recToAdd);
//Test Recordset Length
Y.Assert.areEqual(4, this.rs.get('records').length, "Array lengths not equal.");
//Assert on last object
//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.
Y.ObjectAssert.areEqual(recToAdd, this.rs.getRecordByIndex(3).getValue());
Y.ObjectAssert.areEqual(retval.getRecordByIndex(3).getValue(3), recToAdd);
this.changeTest(3);
},
testAddSingleRecordToIndex: function() {
var recToAdd, i=2;
recToAdd = {a:'8', b:'9', c:'10'};
retval = this.rs.add(recToAdd,i);
Y.Assert.areEqual(recToAdd, this.rs.getRecordByIndex(i).getValue());
//assertion with output
Y.ObjectAssert.areEqual(retval.getRecordByIndex(i).getValue(), recToAdd);
this.changeTest(i);
},
testAddMultipleRecordsToEnd: function() {
var recsToAdd = [{a:'11', b:'22', c:'33'}, {a:'44', b:'55', c:'66'}];
retval = this.rs.add(recsToAdd);
//Assertions with recordset
Y.ObjectAssert.areEqual(recsToAdd[0], this.rs.getRecordByIndex(3).getValue());
Y.ObjectAssert.areEqual(recsToAdd[1], this.rs.getRecordByIndex(4).getValue());
//assertions with output
Y.ObjectAssert.areEqual(retval.getRecordByIndex(3).getValue(), recsToAdd[0]);
Y.ObjectAssert.areEqual(retval.getRecordByIndex(4).getValue(), recsToAdd[1]);
this.changeTest(3);
},
testAddMultipleRecordsToIndex: function() {
var recsToAdd, i;
recsToAdd = [{a:'11', b:'22', c:'33'}, {a:'44', b:'55', c:'66'}];
i = 1;
retval = this.rs.add(recsToAdd, i);
//Assertions with recordset
Y.ObjectAssert.areEqual(recsToAdd[0], this.rs.getRecordByIndex(1).getValue());
Y.ObjectAssert.areEqual(recsToAdd[1], this.rs.getRecordByIndex(2).getValue());
this.changeTest(i);
},
//---------------------------------------------
// Delete Records
//---------------------------------------------
testDeleteSingleRecordFromEnd: function() {
var rem;
this.rs.on('remove', function(e) {
rem = e;
})
this.changeTest(2);
},
testDeleteSingleRecordFromIndex: function() {
this.changeTest(1);
var rem;
this.rs.on('remove', function(e) {
rem = e;
});
this.rs.remove(1);
},
testDeleteRangeOfRecords: function() {
//Delete 2 records from index 1
var rem;
this.rs.on('remove', function(e) {
rem = e;
});
this.changeTest(1);
this.rs.remove(1,2);
},
//---------------------------------------------
// Empty Recordset
//---------------------------------------------
testEmptyRecordSet: function() {
this.emptyTest(0);
Y.Assert.areEqual(0, this.rs.get('records').length);
},
//---------------------------------------------
// GetValuesByKey
//---------------------------------------------
testGetValuesByKey: function() {
var key, retval, i;
key = 'a';
retval = this.rs.getValuesByKey(key);
for (i=0; i < this.initialData.length; i++) {
}
},
testGetValuesByKeyWithInvalidKey: function() {
var key = 'd';
retval = this.rs.getValuesByKey(key);
Y.Assert.isUndefined(retval[i]);
}
},
/** These are OLD UPDATE TESTS - Update was split up into update() and splice()
*
*
//---------------------------------------------
// Update Records - without overwriteFlag
//---------------------------------------------
testUpdateRecordAtIndexWithoutOverwriteFlag: function() {
var newRecord, index=1, oldRecord;
//Update record at given index with new record
newRecord = new Y.Record({data:{a:'newA', b:'newB', c:'newC'}});
oldRecord = this.rs.getRecordByIndex(index);
this.rs.on('update', function(e) {
Y.ObjectAssert.areEqual(newRecord, e.data.updated[0], 'Updated record is != to new record');
Y.ObjectAssert.areEqual(oldRecord, e.data.overwritten[0], 'Overwritten record is != to old record');
Y.Assert.areEqual(1, e.data.overwritten.length, 'overwritten array is not of expected length');
Y.Assert.areEqual(index, e.index, "correct index was modified");
});
this.rs.update(newRecord, index);
},
testUpdateRecordsAtIndexWithoutOverwriteFlag: function() {
var newRecords = [], index=0, oldRecord, a, b;
oldRecord = this.rs.getRecordByIndex(index);
a = new Y.Record({data:{a:'newA', b:'newB', c:'newC'}});
b = new Y.Record({data:{a:'newD', b:'newE', c:'newF'}});
newRecords.push(a);
newRecords.push(b);
this.rs.on('update', function(e) {
Y.ObjectAssert.areEqual(newRecords, e.data.updated, 'Updated record is != to new record');
Y.ObjectAssert.areEqual(oldRecord, e.data.overwritten[0], 'Overwritten record is != to old record');
Y.Assert.areEqual(1, e.data.overwritten.length, 'overwritten array is not of expected length');
Y.Assert.areEqual(index, e.index, "correct index was modified");
});
this.rs.update(newRecords, index);
//check that the two elements in the recordset are the same as the ones pushed in
//3 initial records + 1 more added (the other was just over-written)= 4 total records in recordset
Y.Assert.areEqual(4, this.rs.get('records').length);
},
//---------------------------------------------
// Update Records - with overwriteFlag
//---------------------------------------------
testUpdateRecordAtIndexWithOverwriteFlag: function() {
var newRecord, oldRecord, index=1;
newRecord = new Y.Record({data: {a:'newG', b:'newH', c:'newI'}});
oldRecord = this.rs.getRecordByIndex(index);
this.rs.on('update', function(e) {
//console.log(e.data);
Y.ObjectAssert.areEqual(newRecord, e.data.updated[0], 'Updated record is != to new record');
Y.ObjectAssert.areEqual(oldRecord, e.data.overwritten[0], 'Overwritten record is != to old record');
Y.Assert.areEqual(1, e.data.overwritten.length, 'overwritten array is not of expected length');
Y.Assert.areEqual(index, e.index, "correct index was modified");
});
this.rs.update(newRecord, index, true);
},
testUpdateRecordsAtIndexWithOverwriteFlag: function() {
var newRecords = [], index=0, oldRecords=[], a, b, c;
oldRecords = this.rs.getRecordsByIndex(index,3);
//console.log(oldRecords);
a = new Y.Record({data:{a:'newA', b:'newB', c:'newC'}});
b = new Y.Record({data:{a:'newD', b:'newE', c:'newF'}});
c = new Y.Record({data:{a:'newG', b:'newH', c:'newI'}});
newRecords.push(a);
newRecords.push(b);
newRecords.push(c);
this.rs.on('update', function(e) {
Y.ObjectAssert.areEqual(newRecords, e.data.updated, 'Updated record is != to new record');
for(var i=0; i < oldRecords.length; i++) {
//Y.ObjectAssert.areEqual(oldRecords[i].getValue(), e.data.overwritten[i].getValue(), 'Overwritten record is != to old record');
}
});
//check that the two elements in the recordset are the same as the ones pushed in
//3 initial records - all 3 overwritten
},
testUpdateRecordWithData: function() {
var oData = {a:'newJ', b:'newK', c:'newL'}, oldRecord, index=2;
oldRecord = this.rs.getRecordByIndex(index);
Y.ObjectAssert.areEqual(oData, e.data.updated[0].getValue(), 'Updated record != equal to new record');
Y.ObjectAssert.areEqual(oldRecord, e.data.overwritten[0], 'Overwritten record != equal to old record');
});
},
*
*<-- END OLD UPDATE TESTS -->
*/
testUpdateRecordAtIndex: function() {
var newRecord, oldRecord, index=1;
var o = {a:'newG', b:'newH', c:'newI'};
newRecord = new Y.Record({data:o});
oldRecord = this.rs.getRecordByIndex(index);
this.rs.on('update', function(e) {
Y.ObjectAssert.areEqual(newRecord, e.updated[0], 'Updated record is != to new record');
Y.ObjectAssert.areEqual(oldRecord, e.overwritten[0], 'Overwritten record is != to old record');
Y.Assert.areEqual(1, e.overwritten.length, 'overwritten array is not of expected length');
Y.Assert.areEqual(index, e.index, "correct index was modified");
});
this.rs.update(newRecord, index);
},
testUpdateRecordAtIndices:function(){
var newRecords = [], index=0, oldRecord = [], a, b;
oldRecord = this.rs.getRecordsByIndex(index,2);
a = new Y.Record({data:{a:'newA', b:'newB', c:'newC'}});
b = new Y.Record({data:{a:'newD', b:'newE', c:'newF'}});
newRecords.push(a);
newRecords.push(b);
this.rs.on('update', function(e) {
Y.ObjectAssert.areEqual(newRecords, e.updated, 'Updated record is != to new record');
Y.ObjectAssert.areEqual(oldRecord, e.overwritten, 'Overwritten record is != to old record');
Y.Assert.areEqual(2, e.overwritten.length, 'overwritten array is not of expected length');
Y.Assert.areEqual(index, e.index, "correct index was modified");
});
this.rs.update(newRecords, index);
//check that the two elements in the recordset are the same as the ones pushed in
//3 initial records + 1 more added (the other was just over-written)= 4 total records in recordset
Y.Assert.areEqual(3, this.rs.get('records').length);
},
//---------------------------------------------
// Hashing in Base
//---------------------------------------------
testDefaultHash: function() {
var hashTable = this.rs.get('table');
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(0).get('id')], this.rs.getRecordByIndex(0));
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(1).get('id')], this.rs.getRecordByIndex(1));
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(2).get('id')], this.rs.getRecordByIndex(2));
Y.Assert.areEqual(3, Y.Object.size(hashTable));
//check hashtable sync when adding
this.rs.add({a:'hello', b:'hey', c:'yo!'});
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(0).get('id')], this.rs.getRecordByIndex(0));
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(1).get('id')], this.rs.getRecordByIndex(1));
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(2).get('id')], this.rs.getRecordByIndex(2));
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(3).get('id')], this.rs.getRecordByIndex(3));
Y.Assert.areEqual(4, Y.Object.size(hashTable));
//check hashtable sync when removing 1 object
this.rs.remove(1);
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(0).get('id')], this.rs.getRecordByIndex(0));
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(1).get('id')], this.rs.getRecordByIndex(1));
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(2).get('id')], this.rs.getRecordByIndex(2));
Y.Assert.areEqual(3, Y.Object.size(hashTable));
//check hashtable sync when removing multiple records
this.rs.remove(0,2);
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(0).get('id')], this.rs.getRecordByIndex(0));
Y.Assert.areEqual(1, Y.Object.size(hashTable));
console.log(hashTable);
},
//---------------------------------------------
// Sort Plugin
//---------------------------------------------
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
testDefaultSort: function() {
//Test ascending order
Y.Assert.isFalse(this.rs.sort.get('isSorted'));
this.rs.sort.sort('a',false);
Y.Assert.isTrue(this.rs.sort.get('isSorted'));
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, this.rs.getRecordByIndex(0).getValue());
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, this.rs.getRecordByIndex(1).getValue());
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, this.rs.getRecordByIndex(2).getValue());
//Test Descending Order
this.rs.sort.sort('c',true);
Y.Assert.isTrue(this.rs.sort.get('isSorted'));
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, this.rs.getRecordByIndex(0).getValue());
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, this.rs.getRecordByIndex(1).getValue());
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, this.rs.getRecordByIndex(2).getValue());
//Test sort when values are equal (sorts by ID)
this.rs.sort.sort('b',true);
Y.Assert.isTrue(this.rs.sort.get('isSorted'));
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, this.rs.getRecordByIndex(0).getValue());
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, this.rs.getRecordByIndex(1).getValue());
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, this.rs.getRecordByIndex(2).getValue());
},
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}]
testResort: function() {
Y.Assert.isFalse(this.rs.sort.get('isSorted'));
//Test ascending order
this.rs.sort.sort('a',false);
Y.Assert.isTrue(this.rs.sort.get('isSorted'));
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, this.rs.getRecordByIndex(0).getValue());
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, this.rs.getRecordByIndex(1).getValue());
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, this.rs.getRecordByIndex(2).getValue());
//Add another record to the end of the recordset
this.rs.add({a:6,b:5,c:8});
Y.Assert.isFalse(this.rs.sort.get('isSorted'));
Y.ObjectAssert.areEqual({a:6,b:5,c:8}, this.rs.getRecordByIndex(3).getValue());
//Refresh Sorter
Y.Assert.isTrue(this.rs.sort.get('isSorted'));
//make sure the added record was sorted into the recordset using the existing sort properties
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, this.rs.getRecordByIndex(0).getValue());
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, this.rs.getRecordByIndex(1).getValue());
Y.ObjectAssert.areEqual({a:6,b:5,c:8}, this.rs.getRecordByIndex(2).getValue());
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, this.rs.getRecordByIndex(3).getValue());
},
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}
testReverseBeforeSort: function() {
a = {a:'a', b:'b', c:'c'},
b = {a:'d', b:'e', c:'f'},
c = {a:'g', b:'h', c:'i'},
d = {a:'j', b:'k', c:'l'};
//adding in some records backwards to understand if reversing is working properly
this.rs.add([d,c,b,a]);
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, this.rs.getRecordByIndex(6).getValue());
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, this.rs.getRecordByIndex(5).getValue());
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, this.rs.getRecordByIndex(4).getValue());
Y.ObjectAssert.areEqual(d, this.rs.getRecordByIndex(3).getValue());
Y.ObjectAssert.areEqual(c, this.rs.getRecordByIndex(2).getValue());
Y.ObjectAssert.areEqual(b, this.rs.getRecordByIndex(1).getValue());
Y.ObjectAssert.areEqual(a, this.rs.getRecordByIndex(0).getValue());
},
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}
testFlipAfterSort: function() {
a = {a:1, b:'b', c:'c'},
b = {a:2, b:'e', c:'f'},
c = {a:3, b:'h', c:'i'},
d = {a:4, b:'k', c:'l'};
//adding in some records backwards to understand if reversing is working properly
this.rs.add([d,c,b,a]);
this.rs.sort.sort('a',true);
//1s
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, this.rs.getRecordByIndex(0).getValue());
Y.ObjectAssert.areEqual(a, this.rs.getRecordByIndex(1).getValue());
//2s
Y.ObjectAssert.areEqual(b, this.rs.getRecordByIndex(2).getValue());
//3s
Y.ObjectAssert.areEqual(c, this.rs.getRecordByIndex(3).getValue());
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, this.rs.getRecordByIndex(4).getValue());
Y.ObjectAssert.areEqual(d, this.rs.getRecordByIndex(5).getValue());
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, this.rs.getRecordByIndex(6).getValue());
},
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
testCustomSort: function() {
var a = {a:1, b:1, c:4},
b = {a:2, b:2, c:5},
c = {a:3, b:2, c:4},
d = {a:4, b:4, c:7},
//This is just a copy of Y.ArraySort.compare
compare = function(a, b, desc) {
if(!Y.Lang.isValue(a)) {
if(!Y.Lang.isValue(b)) {
return 0;
}
else {
return 1;
}
}
else if(!Y.Lang.isValue(b)) {
return -1;
}
if(Y.Lang.isString(a)) {
}
if(Y.Lang.isString(b)) {
}
if(a < b) {
return (desc) ? 1 : -1;
}
else if (a > b) {
return (desc) ? -1 : 1;
}
else {
return 0;
}
},
//custom sort where if the values in the given fieldA are equal, the values in fieldB are checked
customsort = function(recA, recB, field, desc) {
var first_sorted = compare(recA.getValue(field), recB.getValue(field), desc);
if(first_sorted === 0) {
var second_sorted = compare(recA.getValue('b'), recB.getValue('b'), desc);
if (second_sorted === 0) {
}
else {
return second_sorted;
}
}
else {
return first_sorted;
}
};
this.rs.add([a,b,c,d]);
this.rs.sort.sort('a',false,customsort);
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
//when the numbers are equal - the object that was added later should be first
Y.ObjectAssert.areEqual({a:1, b:1, c:4}, this.rs.getRecordByIndex(0).getValue());
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, this.rs.getRecordByIndex(1).getValue());
Y.ObjectAssert.areEqual({a:2, b:2, c:5}, this.rs.getRecordByIndex(2).getValue());
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, this.rs.getRecordByIndex(3).getValue());
Y.ObjectAssert.areEqual({a:3, b:2, c:4}, this.rs.getRecordByIndex(4).getValue());
Y.ObjectAssert.areEqual({a:4, b:4, c:7}, this.rs.getRecordByIndex(5).getValue());
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, this.rs.getRecordByIndex(6).getValue());
},
//---------------------------------------------
// ArrayList Methods
//---------------------------------------------
testSize: function() {
},
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
testEach: function() {
var i = 0, recs = [];
this.rs.each(function() {
recs[i] = this.getValue();
i++;
});
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, recs[0]);
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, recs[1]);
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, recs[2]);
},
testSome: function() {
var recs=[], i=0, v={};
this.rs.some(function() {
v = this.getValue();
if (v.a !== 1) {
recs[i] = v;
i++;
return false;
}
else {
return true;
}
});
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, recs[0]);
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, recs[1]);
},
//---------------------------------------------
// Filter Plugin
//---------------------------------------------
testFilter: function() {
var validator = function(item) {
if (item.getValue('b') === 2) {
return true;
}
else {
return false;
}
},
rs = this.rs.filter.filter(validator);
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, rs.getRecordByIndex(0).getValue());
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, rs.getRecordByIndex(1).getValue());
Y.Assert.areEqual(2, rs.getLength());
//test a filter that doesn't return anything from recordset
validator = function(item) {
if (item.getValue('c') === 2) {
return true;
}
else {
return false;
}
};
rs = this.rs.filter.filter(validator);
Y.Assert.areEqual(0, rs.getLength());
//Test Filter with Key Value pair
rs = this.rs.filter.filter('b',2);
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, rs.getRecordByIndex(0).getValue());
Y.ObjectAssert.areEqual({a:1, b:2, c:3}, rs.getRecordByIndex(1).getValue());
Y.Assert.areEqual(2, rs.getLength());
},
testReject: function() {
var validator = function(item) {
if (item.getValue('b') === 2) {
return true;
}
else {
return false;
}
},
rs = this.rs.filter.reject(validator);
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, rs.getRecordByIndex(0).getValue());
Y.Assert.areEqual(1, rs.getLength());
//try it with a validator that always returns false
validator = function(item) {
if (item.getValue('c') === 2) {
return true;
}
else {
return false;
}
};
rs = this.rs.filter.reject(validator);
},
//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)
testMap: function() {
var f = function(item) {
if (item.getValue('b') === 2) {
return true;
}
else {
return false;
}
},
rs = Y.Array.map(this.rs.get('records'),f);
Y.Assert.areEqual(true, rs[0]);
Y.Assert.areEqual(false, rs[1]);
Y.Assert.areEqual(true, rs[2]);
},
//---------------------------------------------
// Indexer Plugin
//---------------------------------------------
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
testCustomHashTableInitialization: function() {
var hashA = this.rs.indexer.createTable('a');
var hashTables = this.rs.indexer.get('hashTables');
//check hashA
Y.ObjectAssert.areEqual(hashA[3].getValue(), {a:3, b:2, c:1});
Y.ObjectAssert.areEqual(hashA[9].getValue(), {a:9, b:8, c:7});
Y.ObjectAssert.areEqual(hashA[1].getValue(), {a:1, b:2, c:3});
//check main table (contains all the subtables)
Y.ObjectAssert.areEqual(hashTables.a[3].getValue(), {a:3, b:2, c:1});
Y.ObjectAssert.areEqual(hashTables.a[9].getValue(), {a:9, b:8, c:7});
Y.ObjectAssert.areEqual(hashTables.a[1].getValue(), {a:1, b:2, c:3});
Y.ObjectAssert.areEqual(hashA, hashTables.a);
var hashB = this.rs.indexer.createTable('b');
Y.ObjectAssert.areEqual(hashTables.b[2].getValue(), {a:1, b:2, c:3});
Y.ObjectAssert.areEqual(hashTables.b[8].getValue(), {a:9, b:8, c:7});
Y.ObjectAssert.areEqual(hashB[2].getValue(), {a:1, b:2, c:3});
Y.ObjectAssert.areEqual(hashB[8].getValue(), {a:9, b:8, c:7});
Y.ObjectAssert.areEqual(hashB, hashTables.b);
},
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
testCustomHashTableAddingRecord: function() {
var hashA = this.rs.indexer.createTable('a'),
hashC = this.rs.indexer.createTable('c'),
hashTables = this.rs.indexer.get('hashTables');
//add to the end
this.rs.add({a:'adding', b:'this', c:'record'});
Y.ObjectAssert.areEqual(hashA[3].getValue(), {a:3, b:2, c:1});
Y.ObjectAssert.areEqual(hashA[9].getValue(), {a:9, b:8, c:7});
Y.ObjectAssert.areEqual(hashA[1].getValue(), {a:1, b:2, c:3});
Y.ObjectAssert.areEqual(hashA['adding'].getValue(), {a:'adding', b:'this', c:'record'});
Y.Assert.areEqual(4, Y.Object.size(hashA));
Y.ObjectAssert.areEqual(hashA, hashTables.a);
Y.ObjectAssert.areEqual(hashC[1].getValue(), {a:3, b:2, c:1});
Y.ObjectAssert.areEqual(hashC[7].getValue(), {a:9, b:8, c:7});
Y.ObjectAssert.areEqual(hashC[3].getValue(), {a:1, b:2, c:3});
Y.ObjectAssert.areEqual(hashC['record'].getValue(), {a:'adding', b:'this', c:'record'});
Y.Assert.areEqual(4, Y.Object.size(hashC));
Y.ObjectAssert.areEqual(hashC, hashTables.c);
//adding record to a specific index
this.rs.add({a:'another', b:'new', c:'record'}, 0);
Y.ObjectAssert.areEqual(hashA[3].getValue(), {a:3, b:2, c:1});
Y.ObjectAssert.areEqual(hashA[9].getValue(), {a:9, b:8, c:7});
Y.ObjectAssert.areEqual(hashA[1].getValue(), {a:1, b:2, c:3});
Y.ObjectAssert.areEqual(hashA['adding'].getValue(), {a:'adding', b:'this', c:'record'});
Y.ObjectAssert.areEqual(hashA['another'].getValue(), {a:'another', b:'new', c:'record'});
Y.Assert.areEqual(5, Y.Object.size(hashA));
Y.ObjectAssert.areEqual(hashA, hashTables.a);
//'record' will overwrite the previous added record.
Y.ObjectAssert.areEqual(hashC[1].getValue(), {a:3, b:2, c:1});
Y.ObjectAssert.areEqual(hashC[7].getValue(), {a:9, b:8, c:7});
Y.ObjectAssert.areEqual(hashC[3].getValue(), {a:1, b:2, c:3});
Y.ObjectAssert.areEqual(hashC['record'].getValue(), {a:'another', b:'new', c:'record'});
Y.Assert.areEqual(4, Y.Object.size(hashC));
Y.ObjectAssert.areEqual(hashC, hashTables.c);
},
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
testCustomHashTableRemovingRecord: function() {
var hashA = this.rs.indexer.createTable('a'),
hashC = this.rs.indexer.createTable('c'),
hashTables = this.rs.indexer.get('hashTables');
this.rs.remove(2);
Y.ObjectAssert.areEqual(hashA[3].getValue(), {a:3, b:2, c:1});
Y.ObjectAssert.areEqual(hashA[9].getValue(), {a:9, b:8, c:7});
Y.Assert.areEqual(2, Y.Object.size(hashA));
Y.ObjectAssert.areEqual(hashA, hashTables.a);
//'record' will overwrite the previous added record.
Y.ObjectAssert.areEqual(hashC[1].getValue(), {a:3, b:2, c:1});
Y.ObjectAssert.areEqual(hashC[7].getValue(), {a:9, b:8, c:7});
Y.Assert.areEqual(2, Y.Object.size(hashC));
Y.ObjectAssert.areEqual(hashC, hashTables.c);
//try removing more than 1 record at a time
this.rs.add({a:1, b:2, c:3});
this.rs.remove(0,2);
Y.ObjectAssert.areEqual(hashA[1].getValue(), {a:1, b:2, c:3});
Y.Assert.areEqual(1, Y.Object.size(hashA));
Y.ObjectAssert.areEqual(hashA, hashTables.a);
Y.ObjectAssert.areEqual(hashC[3].getValue(), {a:1, b:2, c:3});
Y.Assert.areEqual(1, Y.Object.size(hashC));
Y.ObjectAssert.areEqual(hashC, hashTables.c);
},
testCustomHashTableUpdatingRecord: function() {
var hashA = this.rs.indexer.createTable('a'),
hashC = this.rs.indexer.createTable('c'),
hashTables = this.rs.indexer.get('hashTables');
// this.rs.update({a:'adding', b:'this', c:'record'}, 1);
//
// Y.ObjectAssert.areEqual(hashA[3].getValue(), {a:3, b:2, c:1});
// Y.ObjectAssert.areEqual(hashA[1].getValue(), {a:1, b:2, c:3});
// Y.ObjectAssert.areEqual(hashA['adding'].getValue(), {a:'adding', b:'this', c:'record'});
// Y.Assert.areEqual(3, Y.Object.size(hashA));
// Y.ObjectAssert.areEqual(hashA, hashTables.a);
//
//
// //'record' will overwrite the previous added record.
// Y.ObjectAssert.areEqual(hashC[1].getValue(), {a:3, b:2, c:1});
// Y.ObjectAssert.areEqual(hashC[3].getValue(), {a:1, b:2, c:3});
// Y.ObjectAssert.areEqual(hashC['record'].getValue(), {a:'adding', b:'this', c:'record'});
// Y.Assert.areEqual(3, Y.Object.size(hashC));
// Y.ObjectAssert.areEqual(hashC, hashTables.c);
//
//update multiple records
this.rs.update([{a:'adding', b:'this', c:'record'}, {a:'heres', b:'another', c:'one'}, {a:'three', b:'in', c:'a row'}], 1);
Y.ObjectAssert.areEqual(hashA[3].getValue(), {a:3, b:2, c:1});
Y.ObjectAssert.areEqual(hashA['adding'].getValue(), {a:'adding', b:'this', c:'record'});
Y.ObjectAssert.areEqual(hashA['heres'].getValue(), {a:'heres', b:'another', c:'one'});
Y.ObjectAssert.areEqual(hashA['three'].getValue(), {a:'three', b:'in', c:'a row'});
Y.Assert.areEqual(4, Y.Object.size(hashA));
Y.ObjectAssert.areEqual(hashA, hashTables.a);
//'record' will overwrite the previous added record.
Y.ObjectAssert.areEqual(hashC[1].getValue(), {a:3, b:2, c:1});
Y.ObjectAssert.areEqual(hashC['record'].getValue(), {a:'adding', b:'this', c:'record'});
Y.ObjectAssert.areEqual(hashA['one'].getValue(), {a:'heres', b:'another', c:'one'});
Y.ObjectAssert.areEqual(hashA['a row'].getValue(), {a:'three', b:'in', c:'a row'});
Y.Assert.areEqual(4, Y.Object.size(hashC));
Y.ObjectAssert.areEqual(hashC, hashTables.c);
}
});
var suite = new Y.Test.Suite({name:"Recordset Test Suite"});
suite.add(testBasic);
Y.Test.Runner.setName("Recordset Test Runner");
Y.Test.Runner.add(suite);
});
})();
</script>
</body>
</html>