recordset.html revision 468f077f3ad52463360d087a5ea7c7d495bbc530
0N/A<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4028N/A<html>
0N/A<head>
0N/A<title>Recordset Tests</title>
0N/A<script type="text/javascript" src="/build/yui/yui-debug.js"></script>
0N/A</head>
2362N/A
0N/A<body class="yui3-skin-sam">
2362N/A<h1>Recordset Tests</h1>
0N/A<p><input type="button" value="Run Tests" id="btnRun" disabled=true></p>
0N/A
0N/A<script type="text/javascript">
0N/A(function() {
0N/A YUI({
0N/A base: "/build/",
0N/A filters: {recordset:"debug"},
0N/A logInclude:{"TestRunner":true},
0N/A useConsole: true
0N/A }).use("console", "test", "dump", "recordset", function(Y) {
0N/A
2362N/A
2362N/A // Set up the page
2362N/A var ASSERT = Y.Assert,
0N/A ARRAYASSERT = Y.ArrayAssert,
0N/A BTNRUN = Y.get("#btnRun");
0N/A BTNRUN.set("disabled", false);
0N/A Y.on("click", function(e){
0N/A Y.Test.Runner.run();
0N/A }, BTNRUN);
0N/A var myConsole = new Y.Console().render();
0N/A
0N/A
0N/A var testBasic = new Y.Test.Case({
0N/A name: "API Tests",
0N/A initialData: [{a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3}],
0N/A
0N/A //---------------------------------------------
0N/A // Setup and tear down
0N/A //---------------------------------------------
0N/A
0N/A setUp : function () {
0N/A
0N/A //create recordset
0N/A this.rs = new Y.Recordset({records:this.initialData});
0N/A
0N/A //Some Ways to access recordset properties
0N/A //Y.log(rs.getRecord(0).getValue('a'));
0N/A //Y.log(rs.get('records').length);
0N/A },
0N/A
0N/A tearDown : function () {
0N/A delete this.rs;
0N/A },
0N/A
0N/A //---------------------------------------------
0N/A // Event Helpers
0N/A //---------------------------------------------
0N/A
0N/A recordsetChangedEventTest: function(i) {
0N/A this.rs.on('recordsetChangedEvent', function(e) {
0N/A Y.Assert.areEqual(e.index, i);
0N/A });
0N/A },
0N/A
0N/A recordsetEmptiedEventTest: function() {
0N/A var flag = false;
0N/A this.rs.on('recordsetEmptiedEvent', function() {
0N/A flag = true;
0N/A Y.Assert.isTrue(flag);
0N/A });
0N/A
0N/A },
0N/A
0N/A recordsetUpdatedEventTest: function(newRecord, testIndex) {
0N/A var iD = this.initialData;
0N/A this.rs.on('recordsetUpdatedEvent', function(e) {
0N/A Y.ObjectAssert.areEqual(iD[testIndex], e.oldRecord.getValue(), "Old record values match up");
0N/A Y.ObjectAssert.areEqual(newRecord.getValue(), e.newRecord.getValue(), "New record values match up");
0N/A delete iD;
0N/A });
0N/A },
0N/A //---------------------------------------------
0N/A // Add Records
0N/A //---------------------------------------------
0N/A
0N/A testGetRecords: function() {
0N/A var newRecord;
0N/A
0N/A //Single Record
0N/A newRecord = this.rs.getRecord(1);
0N/A Y.ObjectAssert.areEqual(newRecord.getValue(), this.initialData[1]);
0N/A
0N/A //Multiple Records
0N/A newRecord = this.rs.getRecords(1,2);
0N/A Y.ObjectAssert.areEqual(newRecord[0].getValue(), this.initialData[1]);
0N/A Y.ObjectAssert.areEqual(newRecord[1].getValue(), this.initialData[2]);
0N/A
0N/A var oRec = new Y.Record({a:'234'});
0N/A var obj = {b: '324'};
0N/A
0N/A if (obj instanceof Y.Record) {
0N/A // console.log(oRec);
0N/A // console.log(oRec.constructor.NAME);
0N/A // console.log(obj);
0N/A Y.Assert.fail();
0N/A }
0N/A },
0N/A
0N/A
0N/A //---------------------------------------------
0N/A // Add Records
0N/A //---------------------------------------------
0N/A
0N/A testAddSingleRecordToEnd: function() {
0N/A var recToAdd = {a:'8', b:'9', c:'10'};
0N/A retval = this.rs.add(recToAdd);
0N/A
0N/A //Test Recordset Length
0N/A Y.Assert.areEqual(4, this.rs.get('records').length, "Array lengths not equal.");
0N/A //Assert on last object
0N/A
0N/A //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.
0N/A Y.ObjectAssert.areEqual(recToAdd, this.rs.getRecord(3).getValue());
0N/A Y.ObjectAssert.areEqual(retval.data[0].getValue(), recToAdd);
0N/A this.recordsetChangedEventTest(3);
0N/A
0N/A },
0N/A
0N/A testAddSingleRecordToIndex: function() {
0N/A var recToAdd, i=2;
0N/A recToAdd = {a:'8', b:'9', c:'10'};
0N/A
0N/A retval = this.rs.add(recToAdd,i);
0N/A Y.Assert.areEqual(recToAdd, this.rs.getRecord(i).getValue());
0N/A
0N/A //assertion with output
0N/A Y.ObjectAssert.areEqual(retval.data[0].getValue(), recToAdd);
0N/A
0N/A this.recordsetChangedEventTest(i);
0N/A
0N/A
0N/A
0N/A },
0N/A
0N/A testAddMultipleRecordsToEnd: function() {
0N/A var recsToAdd = [{a:'11', b:'22', c:'33'}, {a:'44', b:'55', c:'66'}];
0N/A retval = this.rs.add(recsToAdd);
0N/A
0N/A //Assertions with recordset
0N/A Y.ObjectAssert.areEqual(recsToAdd[0], this.rs.getRecord(3).getValue());
0N/A Y.ObjectAssert.areEqual(recsToAdd[1], this.rs.getRecord(4).getValue());
0N/A
0N/A //assertions with output
0N/A Y.ObjectAssert.areEqual(retval.data[0].getValue(), recsToAdd[0]);
0N/A Y.ObjectAssert.areEqual(retval.data[1].getValue(), recsToAdd[1]);
0N/A
0N/A this.recordsetChangedEventTest(3);
0N/A
0N/A },
0N/A
0N/A testAddMultipleRecordsToIndex: function() {
0N/A var recsToAdd, i;
0N/A recsToAdd = [{a:'11', b:'22', c:'33'}, {a:'44', b:'55', c:'66'}];
0N/A i = 1;
0N/A
0N/A retval = this.rs.add(recsToAdd, i);
0N/A
0N/A //Assertions with recordset
0N/A Y.ObjectAssert.areEqual(recsToAdd[0], this.rs.getRecord(1).getValue());
0N/A Y.ObjectAssert.areEqual(recsToAdd[1], this.rs.getRecord(2).getValue());
0N/A
0N/A this.recordsetChangedEventTest(i);
0N/A },
0N/A
0N/A //---------------------------------------------
0N/A // Delete Records
0N/A //---------------------------------------------
0N/A
0N/A testDeleteSingleRecordFromEnd: function() {
0N/A this.recordsetChangedEventTest(2);
0N/A
0N/A retval = this.rs.remove();
0N/A Y.ObjectAssert.areEqual(this.initialData[2], retval.data[0].getValue());
0N/A Y.Assert.areEqual(2, retval.index);
0N/A },
0N/A
0N/A testDeleteSingleRecordFromIndex: function() {
0N/A this.recordsetChangedEventTest(1);
0N/A
0N/A retval = this.rs.remove(1);
0N/A Y.ObjectAssert.areEqual(this.initialData[1], retval.data[0].getValue());
0N/A Y.Assert.areEqual(1, retval.index);
0N/A },
0N/A
0N/A testDeleteRangeOfRecords: function() {
0N/A //Delete 2 records from index 1
0N/A this.recordsetChangedEventTest(1);
0N/A
0N/A retval = this.rs.remove(1,2);
0N/A Y.ObjectAssert.areEqual(this.initialData[1], retval.data[0].getValue());
0N/A Y.ObjectAssert.areEqual(this.initialData[2], retval.data[1].getValue());
0N/A Y.Assert.areEqual(1, retval.index);
0N/A },
0N/A
0N/A //---------------------------------------------
0N/A // Empty Recordset
0N/A //---------------------------------------------
0N/A
0N/A testEmptyRecordSet: function() {
0N/A
0N/A this.recordsetEmptiedEventTest(0);
0N/A this.rs.empty();
0N/A Y.Assert.areEqual(0, this.rs.get('records').length);
0N/A
0N/A },
0N/A
0N/A //---------------------------------------------
0N/A // GetValuesByKey
0N/A //---------------------------------------------
0N/A
0N/A testGetValuesByKey: function() {
0N/A var key, retval, i;
0N/A key = 'a';
0N/A retval = this.rs.getValuesByKey(key);
0N/A
0N/A for (i=0; i < this.initialData.length; i++) {
0N/A Y.Assert.areEqual(this.initialData[i][key], retval[i]);
0N/A }
0N/A },
0N/A
0N/A testGetValuesByKeyWithInvalidKey: function() {
0N/A var key = 'd';
0N/A retval = this.rs.getValuesByKey(key);
0N/A
0N/A for (i=0; i < this.initialData.length; i++) {
0N/A Y.Assert.isUndefined(retval[i]);
0N/A }
0N/A },
0N/A
0N/A //---------------------------------------------
0N/A // Update Records - without overwriteFlag
0N/A //---------------------------------------------
0N/A
0N/A testUpdateRecordAtIndexWithoutOverwriteFlag: function() {
0N/A var newRecord, index=1;
0N/A
0N/A //Update record at given index with new record
0N/A newRecord = new Y.Record({data:{a:'newA', b:'newB', c:'newC'}});
0N/A
0N/A this.recordsetUpdatedEventTest(newRecord, index);
0N/A this.rs.update(newRecord, index);
0N/A Y.ObjectAssert.areEqual(newRecord.getValue(), this.rs.getRecord(index).getValue());
0N/A },
0N/A
0N/A testUpdateRecordsAtIndexWithoutOverwriteFlag: function() {
0N/A var newRecords = [], index=0;
0N/A var a = new Y.Record({data:{a:'newA', b:'newB', c:'newC'}}),
0N/A b = new Y.Record({data:{a:'newD', b:'newE', c:'newF'}});
0N/A
0N/A newRecords.push(a);
0N/A newRecords.push(b);
0N/A
0N/A this.rs.update(newRecords, index);
0N/A
0N/A //check that the two elements in the recordset are the same as the ones pushed in
0N/A Y.ObjectAssert.areEqual(a.getValue(), this.rs.getRecord(0).getValue());
0N/A Y.ObjectAssert.areEqual(b.getValue(), this.rs.getRecord(1).getValue());
0N/A Y.ObjectAssert.areEqual(this.initialData[1], this.rs.getRecord(2).getValue());
0N/A
0N/A //3 initial records + 1 more added (the other was just over-written)= 4 total records in recordset
0N/A Y.Assert.areEqual(4, this.rs.get('records').length);
4632N/A
4632N/A
4632N/A },
0N/A
0N/A testUpdateRecordAtIndexWithOverwriteFlag: function() {
0N/A var oRec, oData, index=1;
0N/A
0N/A oRec = new Y.Record({data: {a:'newG', b:'newH', c:'newI'}});
0N/A this.rs.update(oRec, index, true);
0N/A
0N/A Y.ObjectAssert.areEqual(oRec.getValue(), this.rs.getRecord(1).getValue());
0N/A },
0N/A
0N/A testUpdateRecordWithData: function() {
0N/A var oData = {a:'newJ', b:'newK', c:'newL'};
0N/A this.rs.update(oData, 2);
0N/A console.log(this.rs.getRecord(2).getValue());
0N/A Y.ObjectAssert.areEqual(oData, this.rs.getRecord(2).getValue());
0N/A }
0N/A
0N/A });
0N/A
0N/A var suite = new Y.Test.Suite({name:"Recordset Test Suite"});
0N/A suite.add(testBasic);
0N/A
0N/A Y.Test.Runner.setName("Recordset Test Runner");
0N/A Y.Test.Runner.add(suite);
0N/A Y.Test.Runner.run();
0N/A });
0N/A})();
0N/A</script>
0N/A</body>
0N/A</html>
0N/A