recordset.html revision 8213b4dd7b4809bae2906f654b95102cd6c96d38
18c2aff776a775d34a4c9893a4c72e0434d68e36artem<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
18c2aff776a775d34a4c9893a4c72e0434d68e36artem<script type="text/javascript" src="/build/yui/yui-debug.js"></script>
18c2aff776a775d34a4c9893a4c72e0434d68e36artem<p><input type="button" value="Run Tests" id="btnRun" disabled=true></p>
18c2aff776a775d34a4c9893a4c72e0434d68e36artem(function() {
18c2aff776a775d34a4c9893a4c72e0434d68e36artem filter: "debug",
18c2aff776a775d34a4c9893a4c72e0434d68e36artem logInclude:{"TestRunner":true},
18c2aff776a775d34a4c9893a4c72e0434d68e36artem useConsole: true
18c2aff776a775d34a4c9893a4c72e0434d68e36artem }).use("console", "test", "dump", "recordset", function(Y) {
18c2aff776a775d34a4c9893a4c72e0434d68e36artem // Set up the page
18c2aff776a775d34a4c9893a4c72e0434d68e36artem var ASSERT = Y.Assert,
18c2aff776a775d34a4c9893a4c72e0434d68e36artem ARRAYASSERT = Y.ArrayAssert,
18c2aff776a775d34a4c9893a4c72e0434d68e36artem BTNRUN = Y.one("#btnRun");
18c2aff776a775d34a4c9893a4c72e0434d68e36artem BTNRUN.set("disabled", false);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem Y.on("click", function(e){
18c2aff776a775d34a4c9893a4c72e0434d68e36artem }, BTNRUN);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem var myConsole = new Y.Console().render();
18c2aff776a775d34a4c9893a4c72e0434d68e36artem var testBasic = new Y.Test.Case({
18c2aff776a775d34a4c9893a4c72e0434d68e36artem name: "API Tests",
18c2aff776a775d34a4c9893a4c72e0434d68e36artem initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
18c2aff776a775d34a4c9893a4c72e0434d68e36artem //---------------------------------------------
18c2aff776a775d34a4c9893a4c72e0434d68e36artem // Setup and tear down
18c2aff776a775d34a4c9893a4c72e0434d68e36artem //---------------------------------------------
18c2aff776a775d34a4c9893a4c72e0434d68e36artem setUp : function () {
18c2aff776a775d34a4c9893a4c72e0434d68e36artem //create recordset
7014882c6a3672fd0e5d60200af8643ae53c5928Richard Lowe this.rs = new Y.Recordset({records:this.initialData});
7014882c6a3672fd0e5d60200af8643ae53c5928Richard Lowe //Some Ways to access recordset properties
18c2aff776a775d34a4c9893a4c72e0434d68e36artem tearDown : function () {
18c2aff776a775d34a4c9893a4c72e0434d68e36artem //---------------------------------------------
18c2aff776a775d34a4c9893a4c72e0434d68e36artem // Event Helpers
18c2aff776a775d34a4c9893a4c72e0434d68e36artem //---------------------------------------------
18c2aff776a775d34a4c9893a4c72e0434d68e36artem changeTest: function(i) {
18c2aff776a775d34a4c9893a4c72e0434d68e36artem this.rs.on('change', function() {
18c2aff776a775d34a4c9893a4c72e0434d68e36artem emptyTest: function() {
18c2aff776a775d34a4c9893a4c72e0434d68e36artem var flag = false;
18c2aff776a775d34a4c9893a4c72e0434d68e36artem this.rs.on('empty', function() {
18c2aff776a775d34a4c9893a4c72e0434d68e36artem flag = true;
18c2aff776a775d34a4c9893a4c72e0434d68e36artem //---------------------------------------------
18c2aff776a775d34a4c9893a4c72e0434d68e36artem // Instantiation
var rs = new Y.Recordset(),
rs.on('remove', function(e) {
rem = e.removed;
rs.add({a:1, b:2, c:3});
rs.add([{a:'hey', b:'yo', c:'hi!'}, {a:2, b:5, c:6}]);
rs.remove(2);
Y.ObjectAssert.areEqual(rem[0].getValue(), {a:2, b:5, c:6});
//this.rs.get('records');
// var newrs = new Y.Recordset({records: [{a:10,b:10,c:10}]});
// newrs.get('records');
newRecord1 = this.rs.getRecordByIndex(1);
newRecord2 = this.rs.getRecord(1);
Y.ObjectAssert.areEqual(newRecord1, newRecord2);
newRecord = this.rs.getRecordsByIndex(1,2);
id = this.rs.getRecordByIndex(0).get('id');
newRecord = this.rs.getRecord(id);
retval = this.rs.add(recToAdd);
//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.
this.changeTest(3);
retval = this.rs.add(recToAdd,i);
this.changeTest(i);
retval = this.rs.add(recsToAdd);
this.changeTest(3);
retval = this.rs.add(recsToAdd, i);
this.changeTest(i);
this.rs.on('remove', function(e) {
this.changeTest(2);
this.changeTest(1);
this.rs.on('remove', function(e) {
this.rs.remove(1);
this.rs.on('remove', function(e) {
this.changeTest(1);
this.rs.remove(1,2);
this.emptyTest(0);
retval = this.rs.getValuesByKey(key);
Y.Assert.isUndefined(retval[i]);
newRecord = new Y.Record({data:o});
oldRecord = this.rs.getRecordByIndex(index);
this.rs.on('update', function(e) {
this.rs.update(newRecord, index);
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) {
this.rs.update(newRecords, index);
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));
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));
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));
this.rs.remove(0,2);
Y.ObjectAssert.areEqual(hashTable[this.rs.getRecordByIndex(0).get('id')], this.rs.getRecordByIndex(0));
this.rs.update({a:'new', b:'record', c:'added'}, 0);
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));
this.rs.update([{a:'new', b:'record', c:'added'},{a:'another', b:'new', c:'record'}], 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));
//console.log(hashTable);
this.rs.sort.sort('a',false);
this.rs.sort.sort('c',true);
this.rs.sort.sort('b',true);
this.rs.sort.sort('a',false);
this.rs.add({a:6,b:5,c:8});
this.rs.add([d,c,b,a]);
this.rs.add([d,c,b,a]);
this.rs.sort.sort('a',true);
//This is just a copy of Y.ArraySort.compare
if(!Y.Lang.isValue(a)) {
if(!Y.Lang.isValue(b)) {
else if(!Y.Lang.isValue(b)) {
if(Y.Lang.isString(a)) {
if(Y.Lang.isString(b)) {
this.rs.add([a,b,c,d]);
this.rs.sort.sort('a',false,customsort);
this.rs.each(function() {
recs[i] = this.getValue();
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]);
this.rs.some(function() {
v = this.getValue();
if (v.a !== 1) {
Y.ObjectAssert.areEqual({a:3, b:2, c:1}, recs[0]);
Y.ObjectAssert.areEqual({a:9, b:8, c:7}, recs[1]);
if (item.getValue('b') === 2) {
rs = this.rs.filter.filter(validator);
if (item.getValue('c') === 2) {
rs = this.rs.filter.filter(validator);
rs = this.rs.filter.filter('b',2);
if (item.getValue('b') === 2) {
rs = this.rs.filter.reject(validator);
if (item.getValue('c') === 2) {
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)
if (item.getValue('b') === 2) {
Y.Assert.areEqual(true, rs[0]);
Y.Assert.areEqual(false, rs[1]);
Y.Assert.areEqual(true, rs[2]);
var hashA = this.rs.indexer.createTable('a');
var hashTables = this.rs.indexer.get('hashTables');
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});
var hashB = this.rs.indexer.createTable('b');
Y.ObjectAssert.areEqual(hashB[2].getValue(), {a:1, b:2, c:3});
Y.ObjectAssert.areEqual(hashB[8].getValue(), {a:9, b:8, c:7});
var hashA = this.rs.indexer.createTable('a'),
hashC = this.rs.indexer.createTable('c'),
hashTables = this.rs.indexer.get('hashTables');
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.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'});
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.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'});
this.rs.add([{a:'blah', b:'bloo', c:'bleh'},{a:'heres', b:'another', c:'one'}]);
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.ObjectAssert.areEqual(hashA['blah'].getValue(), {a:'blah', b:'bloo', c:'bleh'});
Y.ObjectAssert.areEqual(hashA['heres'].getValue(), {a:'heres', b:'another', c:'one'});
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.ObjectAssert.areEqual(hashC['bleh'].getValue(), {a:'blah', b:'bloo', c:'bleh'});
Y.ObjectAssert.areEqual(hashC['one'].getValue(), {a:'heres', b:'another', c:'one'});
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.ObjectAssert.areEqual(hashC[1].getValue(), {a:3, b:2, c:1});
Y.ObjectAssert.areEqual(hashC[7].getValue(), {a:9, b:8, c:7});
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.ObjectAssert.areEqual(hashC[3].getValue(), {a:1, b:2, c:3});
var hashA = this.rs.indexer.createTable('a'),
hashC = this.rs.indexer.createTable('c'),
hashTables = this.rs.indexer.get('hashTables');
this.rs.update({a:'one', b:'record', c:'added'}, 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['one'].getValue(), {a:'one', b:'record', c:'added'});
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['added'].getValue(), {a:'one', b:'record', c:'added'});
this.rs.update([{a:'adding', b:'this', c:'record'}, {a:'heres', b:'another', c:'one'}], 2);
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(hashC[1].getValue(), {a:3, b:2, c:1});
Y.ObjectAssert.areEqual(hashC['record'].getValue(), {a:'adding', b:'this', c:'record'});
Y.ObjectAssert.areEqual(hashC['one'].getValue(), {a:'heres', b:'another', c:'one'});
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);