recordset.html revision df648c649330effe9ab7aeb7c339313f0da428c9
<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.getRecord(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);
});
},
updateTest: function(newRecords, delRecords, testIndex) {
//
// var iD = this.initialData, i=0;
//
// this.rs.on('update', function(e) {
//
// for(; i<newRecords.length; i++) {
// }
//
// i=0;
//
// Y.ObjectAssert.areEqual(delRecords, e.data.overwritten.getValue(), "old record values match up");
// }
//
// Y.Assert.areEqual(testIndex, e.index, "Index match up");
//});
},
//---------------------------------------------
// Add Records
//---------------------------------------------
testGetRecords: function() {
var newRecord;
//Single Record
newRecord = this.rs.getRecord(1);
//Multiple Records
newRecord = this.rs.getRecords(1,2);
Y.ObjectAssert.areEqual(newRecord[0].getValue(), this.initialData[1]);
Y.ObjectAssert.areEqual(newRecord[1].getValue(), this.initialData[2]);
var oRec = new Y.Record({a:'234'});
var obj = {b: '324'};
if (obj instanceof Y.Record) {
// console.log(oRec);
// console.log(obj);
}
},
//---------------------------------------------
// Add Records
//---------------------------------------------
testAddSingleRecordToEnd: function() {
var recToAdd = {a:'8', b:'9', c:'10'}, retval;
retval = this.rs.add(recToAdd);
console.log('retval');
console.log(this.rs.getRecord(3).getValue());
//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.getRecord(3).getValue());
Y.ObjectAssert.areEqual(retval.getRecord(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.getRecord(i).getValue());
//assertion with output
Y.ObjectAssert.areEqual(retval.getRecord(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.getRecord(3).getValue());
Y.ObjectAssert.areEqual(recsToAdd[1], this.rs.getRecord(4).getValue());
//assertions with output
Y.ObjectAssert.areEqual(retval.getRecord(3).getValue(), recsToAdd[0]);
Y.ObjectAssert.areEqual(retval.getRecord(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.getRecord(1).getValue());
Y.ObjectAssert.areEqual(recsToAdd[1], this.rs.getRecord(2).getValue());
this.changeTest(i);
},
//---------------------------------------------
// Delete Records
//---------------------------------------------
testDeleteSingleRecordFromEnd: function() {
this.changeTest(2);
retval = this.rs.remove();
},
testDeleteSingleRecordFromIndex: function() {
this.changeTest(1);
retval = this.rs.remove(1);
},
testDeleteRangeOfRecords: function() {
//Delete 2 records from index 1
this.changeTest(1);
retval = 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.getRecord(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.getRecord(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.getRecord(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.getRecords(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.getRecord(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;
newRecord = new Y.Record({data: {a:'newG', b:'newH', c:'newI'}});
oldRecord = this.rs.getRecord(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);
},
testUpdateRecordAtIndices:function(){
var newRecords = [], index=0, oldRecord = [], a, b;
oldRecord = this.rs.getRecords(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.data.updated, 'Updated record is != to new record');
Y.ObjectAssert.areEqual(oldRecord, e.data.overwritten, 'Overwritten record is != to old record');
Y.Assert.areEqual(2, 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(3, this.rs.get('records').length);
},
//---------------------------------------------
// 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
this.rs.sort.sort('a',false);
//Test Descending Order
this.rs.sort.sort('c',true);
//Test sort when values are equal (sorts by ID)
this.rs.sort.sort('b',true);
},
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}]
testResort: function() {
//Test ascending order
this.rs.sort.sort('a',false);
//Add another record to the end of the recordset
this.rs.add({a:6,b:5,c:8});
Y.ObjectAssert.areEqual({a:6,b:5,c:8}, this.rs.getRecord(3).getValue());
//Refresh Sorter
//make sure the added record was sorted into the recordset using the existing sort properties
Y.ObjectAssert.areEqual({a:6,b:5,c:8}, this.rs.getRecord(2).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(d, this.rs.getRecord(3).getValue());
Y.ObjectAssert.areEqual(c, this.rs.getRecord(2).getValue());
Y.ObjectAssert.areEqual(b, this.rs.getRecord(1).getValue());
Y.ObjectAssert.areEqual(a, this.rs.getRecord(0).getValue());
},
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}
testReverseAfterSort: 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, this.rs.getRecord(1).getValue());
//2s
Y.ObjectAssert.areEqual(b, this.rs.getRecord(2).getValue());
//3s
Y.ObjectAssert.areEqual(c, this.rs.getRecord(4).getValue());
Y.ObjectAssert.areEqual(d, this.rs.getRecord(5).getValue());
},
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
testCustomSort: function() {
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
var 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
var 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);
for (var i=0; i<this.rs.getLength(); i++) {
}
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
},
//---------------------------------------------
// ArrayList Methods
//---------------------------------------------
testArrayList: function() {
},
testSize: function() {
},
testRecord: function() {
var o = {foo:1, bar:"3"},
a,
b;
console.log('a is ');
console.log('b is ');
},
//---------------------------------------------
// Filter Plugin
//---------------------------------------------
testFilterPlugin: function() {
//this.rs.filter.alert();
},
/*
//initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
testFilterByValidator: function() {
var validator = function(item) {
return true;
}
else {
return false;
}
},
oRecs = this.rs.filter.filter(validator);
console.log(oRecs);
},
testFilterByKeyValue: function() {
}
*/
});
suite.add(testBasic);
Y.Test.Runner.setName("Recordset Test Runner");
Y.Test.Runner.add(suite);
});
})();
</script>
</body>
</html>