📄 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"]} ] } }; } }else if (name === "countries_references"){ if(dojo.isBrowser){ data = {url: dojo.moduleUrl("tests", "data/countries_references.json").toString() }; }else{ data = {data: { identifier: 'name', label: 'name', items: [ { name:'Africa', type:'continent', children:[{_reference:'Egypt'}, {_reference:'Kenya'}, {_reference:'Sudan'}] }, { name:'Egypt', type:'country' }, { name:'Kenya', type:'country', children:[{_reference:'Nairobi'}, {_reference:'Mombasa'}] }, { name:'Nairobi', type:'city' }, { name:'Mombasa', type:'city' }, { name:'Sudan', type:'country', children:{_reference:'Khartoum'} }, { name:'Khartoum', type:'city' }, { name:'Asia', type:'continent', children:[{_reference:'China'}, {_reference:'India'}, {_reference:'Russia'}, {_reference:'Mongolia'}] }, { name:'China', type:'country' }, { name:'India', type:'country' }, { name:'Russia', type:'country' }, { name:'Mongolia', type:'country' }, { name:'Australia', type:'continent', population:'21 million', children:{_reference:'Commonwealth of Australia'}}, { name:'Commonwealth of Australia', type:'country', population:'21 million'}, { name:'Europe', type:'continent', children:[{_reference:'Germany'}, {_reference:'France'}, {_reference:'Spain'}, {_reference:'Italy'}] }, { name:'Germany', type:'country' }, { name:'France', type:'country' }, { name:'Spain', type:'country' }, { name:'Italy', type:'country' }, { name:'North America', type:'continent', children:[{_reference:'Mexico'}, {_reference:'Canada'}, {_reference:'United States of America'}] }, { name:'Mexico', type:'country', population:'108 million', area:'1,972,550 sq km', children:[{_reference:'Mexico City'}, {_reference:'Guadalajara'}] }, { name:'Mexico City', type:'city', population:'19 million', timezone:'-6 UTC'}, { name:'Guadalajara', type:'city', population:'4 million', timezone:'-6 UTC' }, { name:'Canada', type:'country', population:'33 million', area:'9,984,670 sq km', children:[{_reference:'Ottawa'}, {_reference:'Toronto'}] }, { name:'Ottawa', type:'city', population:'0.9 million', timezone:'-5 UTC'}, { name:'Toronto', type:'city', population:'2.5 million', timezone:'-5 UTC' }, { name:'United States of America', type:'country' }, { name:'South America', type:'continent', children:[{_reference:'Brazil'}, {_reference:'Argentina'}] }, { name:'Brazil', type:'country', population:'186 million' }, { name:'Argentina', type:'country', population:'40 million' } ] } }; } } 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 } },
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -