📄 readonlyitemfiletesttemplates.js
字号:
if(!dojo._hasResource["tests.data.readOnlyItemFileTestTemplates"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.dojo._hasResource["tests.data.readOnlyItemFileTestTemplates"] = true;dojo.provide("tests.data.readOnlyItemFileTestTemplates");dojo.require("dojo.data.api.Read");dojo.require("dojo.data.api.Identity");dojo.require("dojo.date");dojo.require("dojo.date.stamp");tests.data.readOnlyItemFileTestTemplates.registerTestsForDatastore = function(/* String */ datastoreClassName){ // summary: // Given the name of a datastore class to use, this function creates // a set of unit tests for that datastore, and registers the new test // group with the doh test framework. The new unit tests are based // on a set of "template" unit tests. var datastoreClass = dojo.getObject(datastoreClassName); var testTemplates = tests.data.readOnlyItemFileTestTemplates.testTemplates; var testsForDatastore = []; var makeNewTestFunction = function(template){ return function(t){return template.runTest(datastoreClass, t);}; }; for(var i = 0; i < testTemplates.length; ++i) { var testTemplate = testTemplates[i]; var test = {}; test.name = testTemplate.name; test.runTest = makeNewTestFunction(testTemplate); testsForDatastore.push(test); } var testGroupName = "tests.data.readOnlyItemFileTestTemplates, with datastore " + datastoreClassName; doh.register(testGroupName, testsForDatastore);};//-----------------------------------------------------// testFile data-setstests.data.readOnlyItemFileTestTemplates.getTestData = function(name){ var data = null; if(name === "countries"){ if(dojo.isBrowser){ data = {url: dojo.moduleUrl("tests", "data/countries.json").toString() }; }else{ data = {data: { identifier:"abbr", label:"name", items:[ {abbr:"ec", name:"Ecuador", capital:"Quito"}, {abbr:'eg', name:'Egypt', capital:'Cairo'}, {abbr:'sv', name:'El Salvador', capital:'San Salvador'}, {abbr:'gq', name:'Equatorial Guinea', capital:'Malabo'}, {abbr:'er', name:'Eritrea', capital:'Asmara'}, {abbr:'ee', name:'Estonia', capital:'Tallinn'}, {abbr:'et', name:'Ethiopia', capital:'Addis Ababa'} ] } }; } }else if(name === "countries_withNull"){ if(dojo.isBrowser){ data = {url: dojo.moduleUrl("tests", "data/countries_withNull.json").toString() }; }else{ data = {data: { identifier:"abbr", items:[ {abbr:"ec", name:null, capital:"Quito"}, {abbr:'eg', name:null, capital:'Cairo'}, {abbr:'sv', name:'El Salvador', capital:'San Salvador'}, {abbr:'gq', name:'Equatorial Guinea', capital:'Malabo'}, {abbr:'er', name:'Eritrea', capital:'Asmara'}, {abbr:'ee', name:null, capital:'Tallinn'}, {abbr:'et', name:'Ethiopia', capital:'Addis Ababa'} ] } }; } }else if(name === "countries_withoutid"){ if(dojo.isBrowser){ data = {url: dojo.moduleUrl("tests", "data/countries_withoutid.json").toString() }; }else{ data = {data: { label: "name", items:[ {abbr:"ec", name:null, capital:"Quito"}, {abbr:'eg', name:null, capital:'Cairo'}, {abbr:'sv', name:'El Salvador', capital:'San Salvador'}, {abbr:'gq', name:'Equatorial Guinea', capital:'Malabo'}, {abbr:'er', name:'Eritrea', capital:'Asmara'}, {abbr:'ee', name:null, capital:'Tallinn'}, {abbr:'et', name:'Ethiopia', capital:'Addis Ababa'} ] } }; } }else if (name === "countries_withBoolean"){ if(dojo.isBrowser){ data = {url: dojo.moduleUrl("tests", "data/countries_withBoolean.json").toString() }; }else{ data = {data: { identifier:"abbr", items:[ {abbr:"ec", name:"Ecuador", capital:"Quito", real:true}, {abbr:'eg', name:'Egypt', capital:'Cairo', real:true}, {abbr:'sv', name:'El Salvador', capital:'San Salvador', real:true}, {abbr:'gq', name:'Equatorial Guinea', capital:'Malabo', real:true}, {abbr:'er', name:'Eritrea', capital:'Asmara', real:true}, {abbr:'ee', name:'Estonia', capital:'Tallinn', real:true}, {abbr:'et', name:'Ethiopia', capital:'Addis Ababa', real:true}, {abbr:'ut', name:'Utopia', capital:'Paradise', real:false} ] } }; } }else if (name === "countries_withDates"){ if(dojo.isBrowser){ data = {url: dojo.moduleUrl("tests", "data/countries_withDates.json").toString() }; }else{ data = {data: { identifier:"abbr", items:[ {abbr:"ec", name:"Ecuador", capital:"Quito"}, {abbr:'eg', name:'Egypt', capital:'Cairo'}, {abbr:'sv', name:'El Salvador', capital:'San Salvador'}, {abbr:'gq', name:'Equatorial Guinea', capital:'Malabo'}, {abbr:'er', name:'Eritrea', capital:'Asmara', independence:{_type:'Date', _value:"1993-05-24T00:00:00Z"}}, // May 24, 1993, {abbr:'ee', name:'Estonia', capital:'Tallinn', independence:{_type:'Date', _value:"1991-08-20T00:00:00Z"}}, // August 20, 1991 {abbr:'et', name:'Ethiopia', capital:'Addis Ababa'} ] } }; } }else if (name === "geography_hierarchy_small"){ if(dojo.isBrowser){ data = {url: dojo.moduleUrl("tests", "data/geography_hierarchy_small.json").toString() }; }else{ data = {data: { items:[ { name:'Africa', countries:[ { name:'Egypt', capital:'Cairo' }, { name:'Kenya', capital:'Nairobi' }, { name:'Sudan', capital:'Khartoum' }]}, { name:'Australia', capital:'Canberra' }, { name:'North America', countries:[ { name:'Canada', population:'33 million', cities:[ { name:'Toronto', population:'2.5 million' }, { name:'Alberta', population:'1 million' } ]}, { name: 'United States of America', capital: 'Washington DC', states:[ { name: 'Missouri'}, { name: 'Arkansas'} ]} ]} ] }}; } }else if (name === "data_multitype"){ if(dojo.isBrowser){ data = {url: dojo.moduleUrl("tests", "data/data_multitype.json").toString() }; }else{ data = {data: { "identifier": "count", "label": "count", items: [ { count: 1, value: "true" }, { count: 2, value: true }, { count: 3, value: "false"}, { count: 4, value: false }, { count: 5, value: true }, { count: 6, value: true }, { count: 7, value: "true" }, { count: 8, value: "true" }, { count: 9, value: "false"}, { count: 10, value: false }, { count: 11, value: [false, false]}, { count: "12", value: [false, "true"]} ] } }; } } return data;};//-----------------------------------------------------// testTemplatestests.data.readOnlyItemFileTestTemplates.testTemplates = [ { name: "Identity API: fetchItemByIdentity()", runTest: function(datastore, t){ // summary: // Simple test of the fetchItemByIdentity function of the store. var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries")); var d = new doh.Deferred(); function onItem(item){ t.assertTrue(item !== null); if(item !== null){ var name = store.getValue(item,"name"); t.assertEqual(name, "El Salvador"); } d.callback(true); } function onError(errData){ t.assertTrue(false); d.errback(errData); } store.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError}); return d; // Deferred } }, { name: "Identity API: fetchItemByIdentity() notFound", runTest: function(datastore, t){ // summary: // Simple test of the fetchItemByIdentity function of the store. // description: // Simple test of the fetchItemByIdentity function of the store. var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries")); var d = new doh.Deferred(); function onItem(item){ t.assertTrue(item === null); d.callback(true); } function onError(errData){ t.assertTrue(false); d.errback(errData); } store.fetchItemByIdentity({identity: "sv_not", onItem: onItem, onError: onError}); return d; // Deferred } }, { name: "Identity API: getIdentityAttributes()", runTest: function(datastore, t){ // summary: // Simple test of the getIdentityAttributes function. // description: // Simple test of the getIdentityAttributes function. var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries")); var d = new doh.Deferred(); function onItem(item){ t.assertTrue(item !== null) var identifiers = store.getIdentityAttributes(item); t.assertTrue(dojo.isArray(identifiers)); t.assertEqual(1, identifiers.length); t.assertEqual("abbr", identifiers[0]); d.callback(true); } function onError(errData){ t.assertTrue(false); d.errback(errData); } store.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError}); return d; // Deferred } }, { name: "Identity API: fetchItemByIdentity() commentFilteredJson", runTest: function(datastore, t){ // summary: // Simple test of the fetchItemByIdentity function of the store. // description: // Simple test of the fetchItemByIdentity function of the store. // This tests loading a comment-filtered json file so that people using secure // data with this store can bypass the JavaSceipt hijack noted in Fortify's // paper. if(dojo.isBrowser){ var store = new datastore({url: dojo.moduleUrl("tests", "data/countries_commentFiltered.json").toString()}); var d = new doh.Deferred(); function onItem(item){ t.assertTrue(item !== null); var name = store.getValue(item,"name"); t.assertEqual(name, "El Salvador"); d.callback(true); } function onError(errData){ t.assertTrue(false); d.errback(errData); } store.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError}); return d; // Deferred } } }, { name: "Identity API: fetchItemByIdentity() nullValue", runTest: function(datastore, t){ // summary: // Simple test of the fetchItemByIdentity function of the store, checling a null value. // description: // Simple test of the fetchItemByIdentity function of the store, checking a null value. // This tests handling attributes in json that were defined as null properly. // Introduced because of tracker: #3153 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries_withNull")); var d = new doh.Deferred(); function onItem(item){ t.assertTrue(item !== null); var name = store.getValue(item,"name"); t.assertEqual(name, null); d.callback(true); } function onError(errData){ t.assertTrue(false); d.errback(errData); } store.fetchItemByIdentity({identity: "ec", onItem: onItem, onError: onError}); return d; // Deferred } }, { name: "Identity API: fetchItemByIdentity() booleanValue", runTest: function(datastore, t){ // summary: // Simple test of the fetchItemByIdentity function of the store, checking a boolean value. // description: // Simple test of the fetchItemByIdentity function of the store, checking a boolean value. var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries_withBoolean")); var d = new doh.Deferred(); function onItem(item){ t.assertTrue(item !== null); var name = store.getValue(item,"name"); t.assertEqual(name, "Utopia"); var real = store.getValue(item,"real"); t.assertEqual(real, false); d.callback(true); } function onError(errData){ t.assertTrue(false); d.errback(errData); } store.fetchItemByIdentity({identity: "ut", onItem: onItem, onError: onError}); return d; // Deferred } }, { name: "Identity API: fetchItemByIdentity() withoutSpecifiedIdInData", runTest: function(datastore, t){ // summary: // Simple test of bug #4691, looking up something by assigned id, not one specified in the JSON data. // description: // Simple test of bug #4691, looking up something by assigned id, not one specified in the JSON data. var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries_withoutid")); var d = new doh.Deferred(); function onItem(item){ t.assertTrue(item !== null); var name = store.getValue(item,"name"); t.assertEqual(name, "El Salvador"); d.callback(true); } function onError(errData){ t.assertTrue(false); d.errback(errData); } store.fetchItemByIdentity({identity: "2", onItem: onItem, onError: onError}); return d; // Deferred } }, { name: "Identity API: getIdentity()", runTest: function(datastore, t){ // summary: // Simple test of the getIdentity function of the store. // description: // Simple test of the getIdentity function of the store. var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries")); var d = new doh.Deferred(); function onItem(item){ t.assertTrue(item !== null); t.assertTrue(store.getIdentity(item) === "sv"); d.callback(true); } function onError(errData){ t.assertTrue(false); d.errback(errData); } store.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError}); return d; // Deferred } }, { name: "Identity API: getIdentity() withoutSpecifiedId", runTest: function(datastore, t){ // summary: // Simple test of the #4691 bug // description: // Simple test of the #4691 bug var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries_withoutid")); var d = new doh.Deferred(); function onItem(item, request){ t.assertTrue(item !== null); t.assertTrue(store.getIdentity(item) === 2); d.callback(true); } function onError(errData, request){ t.assertTrue(false); d.errback(errData); } store.fetch({ query:{abbr: "sv"}, onItem: onItem, onError: onError}); return d; // Deferred } }, { name: "Read API: fetch() all", runTest: function(datastore, t){ // summary: // Simple test of a basic fetch on ItemFileReadStore. // description: // Simple test of a basic fetch on ItemFileReadStore. var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries")); var d = new doh.Deferred(); function completedAll(items, request){ t.is(7, items.length); d.callback(true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -