| 1 |
dojo.provide("dojox.data.tests.stores.ServiceStore"); |
| 2 |
dojo.require("dojox.rpc.Service"); |
| 3 |
dojo.require("dojox.rpc.Rest"); |
| 4 |
|
| 5 |
dojo.require("dojox.data.ServiceStore"); |
| 6 |
dojo.require("dojo.data.api.Read"); |
| 7 |
dojox.data.tests.stores.ServiceStore.error = function(t, d, errData){ |
| 8 |
|
| 9 |
|
| 10 |
d.errback(errData); |
| 11 |
}; |
| 12 |
var testServices = new dojox.rpc.Service(require.toUrl("dojox/rpc/tests/resources/test.smd")); |
| 13 |
var jsonStore = new dojox.data.ServiceStore({service:testServices.jsonRestStore}); |
| 14 |
|
| 15 |
doh.register("dojox.data.tests.stores.ServiceStore", |
| 16 |
[ |
| 17 |
{ |
| 18 |
name: "Fetch some items", |
| 19 |
timeout: 10000, |
| 20 |
runTest: function(t) { |
| 21 |
|
| 22 |
|
| 23 |
var d = new doh.Deferred(); |
| 24 |
jsonStore.fetch({query:"query", |
| 25 |
onComplete: function(items, request){ |
| 26 |
t.is(4, items.length); |
| 27 |
d.callback(true); |
| 28 |
}, |
| 29 |
onError: dojo.partial(dojox.data.tests.stores.ServiceStore.error, doh, d) |
| 30 |
}); |
| 31 |
return d; |
| 32 |
} |
| 33 |
}, |
| 34 |
{ |
| 35 |
name: "fetchItemByIdentity, getValue, getValues, hasAttribute,containsValue, getAttributes, getIdentity", |
| 36 |
timeout: 10000, |
| 37 |
runTest: function(t) { |
| 38 |
|
| 39 |
|
| 40 |
var d = new doh.Deferred(); |
| 41 |
jsonStore.fetchItemByIdentity({identity:1, |
| 42 |
onItem: function(item, request){ |
| 43 |
t.is("Object 1", item.name); |
| 44 |
t.is("Object 1",jsonStore.getValue(item,"name")); |
| 45 |
t.t(jsonStore.hasAttribute(item,"name")); |
| 46 |
t.is(jsonStore.getValues(item,"name").length,1); |
| 47 |
t.t(jsonStore.isItem(item)); |
| 48 |
t.t(jsonStore.isItemLoaded(item)); |
| 49 |
t.t(jsonStore.containsValue(item,"name","Object 1")); |
| 50 |
t.f(jsonStore.containsValue(item,"name","Something Else")); |
| 51 |
t.is(jsonStore.getIdentity(item),1); |
| 52 |
t.t(dojo.indexOf(jsonStore.getAttributes(item),"name")>-1); |
| 53 |
t.is("default",jsonStore.getValue(item,"nothing","default")); |
| 54 |
d.callback(true); |
| 55 |
}, |
| 56 |
onError: dojo.partial(dojox.data.tests.stores.ServiceStore.error, doh, d) |
| 57 |
}); |
| 58 |
return d; |
| 59 |
} |
| 60 |
}, |
| 61 |
{ |
| 62 |
name: "createLazyItem", |
| 63 |
timeout: 10000, |
| 64 |
runTest: function(t) { |
| 65 |
|
| 66 |
|
| 67 |
var d = new doh.Deferred(); |
| 68 |
var lazyItem = { |
| 69 |
_loadObject: function(callback){ |
| 70 |
this.name="loaded"; |
| 71 |
delete this._loadObject; |
| 72 |
callback(this); |
| 73 |
} |
| 74 |
}; |
| 75 |
t.f(jsonStore.isItemLoaded(lazyItem)); |
| 76 |
jsonStore.loadItem({item:lazyItem,onItem:function(){ |
| 77 |
t.t(jsonStore.isItemLoaded(lazyItem)); |
| 78 |
t.is(lazyItem.name,"loaded"); |
| 79 |
d.callback(true); |
| 80 |
}}); |
| 81 |
return d; |
| 82 |
} |
| 83 |
}, |
| 84 |
{ |
| 85 |
name: "lazyItem With Fetch", |
| 86 |
timeout: 10000, |
| 87 |
runTest: function(t) { |
| 88 |
var d = new doh.Deferred(); |
| 89 |
jsonStore.idAttribute = "id"; |
| 90 |
jsonStore.syncMode = true; |
| 91 |
jsonStore.fetch({query:"query", |
| 92 |
onComplete: function(items, request){ |
| 93 |
items[0]._loadObject = function(callback){ |
| 94 |
jsonStore.fetch({query:this.id,onComplete:callback}); |
| 95 |
} |
| 96 |
t.t(jsonStore.getValue(items[0],"testArray").length); |
| 97 |
d.callback(true); |
| 98 |
}, |
| 99 |
onError: dojo.partial(dojox.data.tests.stores.ServiceStore.error, doh, d) |
| 100 |
}); |
| 101 |
return d; |
| 102 |
} |
| 103 |
}, |
| 104 |
{ |
| 105 |
name: "ReadAPI: Fetch_20_Streaming", |
| 106 |
timeout: 10000, |
| 107 |
runTest: function(t) { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
var d = new doh.Deferred(); |
| 112 |
var count = 0; |
| 113 |
|
| 114 |
function onItem(item, requestObj){ |
| 115 |
t.assertTrue(typeof item == 'number'); |
| 116 |
count++; |
| 117 |
} |
| 118 |
function onComplete(items, request){ |
| 119 |
t.is(20, count); |
| 120 |
|
| 121 |
d.callback(true); |
| 122 |
} |
| 123 |
|
| 124 |
jsonStore.fetch({ |
| 125 |
query: "bigQuery", |
| 126 |
onBegin: null, |
| 127 |
count: 20, |
| 128 |
onItem: onItem, |
| 129 |
onComplete: onComplete, |
| 130 |
onError: dojo.partial(dojox.data.tests.stores.ServiceStore.error, t, d) |
| 131 |
}); |
| 132 |
return d; |
| 133 |
} |
| 134 |
}, |
| 135 |
function testReadAPI_functionConformance(t){ |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
var readApi = new dojo.data.api.Read(); |
| 142 |
var passed = true; |
| 143 |
|
| 144 |
for(i in readApi){ |
| 145 |
if(i.toString().charAt(0) !== '_') |
| 146 |
{ |
| 147 |
var member = readApi[i]; |
| 148 |
|
| 149 |
if(typeof member === "function"){ |
| 150 |
var testStoreMember = jsonStore [i]; |
| 151 |
if(!(typeof testStoreMember === "function")){ |
| 152 |
passed = false; |
| 153 |
break; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
] |
| 160 |
); |
| 161 |
|