1 | define(["doh", "dojo/store/Memory"], function(doh, Memory){ |
---|
2 | var store = new Memory({ |
---|
3 | data: [ |
---|
4 | {id: 1, name: "one", prime: false, mappedTo: "E", date: new Date(1970, 0, 1) }, |
---|
5 | {id: 2, name: "two", even: true, prime: true, mappedTo: "D", date: new Date(1980, 1, 2) }, |
---|
6 | {id: 3, name: "three", prime: true, mappedTo: "C", date: new Date(1990, 2, 3) }, |
---|
7 | {id: 4, name: "four", even: true, prime: false, mappedTo: null, date: new Date(1972, 3, 6, 12, 1) }, |
---|
8 | {id: 5, name: "five", prime: true, mappedTo: "A", date: new Date(1972, 3, 6, 6, 2) } |
---|
9 | ] |
---|
10 | }); |
---|
11 | doh.register("dojo.tests.store.Memory", |
---|
12 | [ |
---|
13 | function testGet(t){ |
---|
14 | t.is(store.get(1).name, "one"); |
---|
15 | t.is(store.get(4).name, "four"); |
---|
16 | t.t(store.get(5).prime); |
---|
17 | }, |
---|
18 | function testQuery(t){ |
---|
19 | t.is(store.query({prime: true}).length, 3); |
---|
20 | t.is(store.query({even: true})[1].name, "four"); |
---|
21 | }, |
---|
22 | function testQueryWithString(t){ |
---|
23 | t.is(store.query({name: "two"}).length, 1); |
---|
24 | t.is(store.query({name: "two"})[0].name, "two"); |
---|
25 | }, |
---|
26 | function testQueryWithRegExp(t){ |
---|
27 | t.is(store.query({name: /^t/}).length, 2); |
---|
28 | t.is(store.query({name: /^t/})[1].name, "three"); |
---|
29 | t.is(store.query({name: /^o/}).length, 1); |
---|
30 | t.is(store.query({name: /o/}).length, 3); |
---|
31 | }, |
---|
32 | function testQueryWithTestFunction(t){ |
---|
33 | t.is(store.query({id: {test: function(id){ return id < 4;}}}).length, 3); |
---|
34 | t.is(store.query({even: {test: function(even, object){ return even && object.id > 2;}}}).length, 1); |
---|
35 | }, |
---|
36 | function testQueryWithSort(t){ |
---|
37 | t.is(store.query({prime: true}, {sort:[{attribute:"name"}]}).length, 3); |
---|
38 | t.is(store.query({even: true}, {sort:[{attribute:"name"}]})[1].name, "two"); |
---|
39 | t.is(store.query({even: true}, {sort:function(a, b){ |
---|
40 | return a.name < b.name ? -1 : 1; |
---|
41 | }})[1].name, "two"); |
---|
42 | t.is(store.query(null, {sort:[{attribute:"mappedTo"}]})[4].name, "four"); |
---|
43 | |
---|
44 | t.is([ 1, 5, 4, 2, 3 ], store.query({}, { sort: [ { attribute: "date", descending: false } ] }).map(function (item) { |
---|
45 | return item.id; |
---|
46 | })); |
---|
47 | }, |
---|
48 | function testQueryWithPaging(t){ |
---|
49 | t.is(store.query({prime: true}, {start: 1, count: 1}).length, 1); |
---|
50 | t.is(store.query({even: true}, {start: 1, count: 1})[0].name, "four"); |
---|
51 | }, |
---|
52 | function testPutUpdate(t){ |
---|
53 | var four = store.get(4); |
---|
54 | four.square = true; |
---|
55 | store.put(four); |
---|
56 | four = store.get(4); |
---|
57 | t.t(four.square); |
---|
58 | }, |
---|
59 | function testPutNew(t){ |
---|
60 | store.put({ |
---|
61 | id: 6, |
---|
62 | perfect: true |
---|
63 | }); |
---|
64 | t.t(store.get(6).perfect); |
---|
65 | }, |
---|
66 | function testAddDuplicate(t){ |
---|
67 | var threw; |
---|
68 | try{ |
---|
69 | store.add({ |
---|
70 | id: 6, |
---|
71 | perfect: true |
---|
72 | }); |
---|
73 | }catch(e){ |
---|
74 | threw = true; |
---|
75 | } |
---|
76 | t.t(threw); |
---|
77 | }, |
---|
78 | function testAddNew(t){ |
---|
79 | store.add({ |
---|
80 | id: 7, |
---|
81 | prime: true |
---|
82 | }); |
---|
83 | t.t(store.get(7).prime); |
---|
84 | }, |
---|
85 | function testRemove(t){ |
---|
86 | t.t(store.remove(7)); |
---|
87 | t.is(store.get(7), undefined); |
---|
88 | }, |
---|
89 | function testRemoveMissing(t){ |
---|
90 | t.f(store.remove(77)); |
---|
91 | // make sure nothing changed |
---|
92 | t.is(store.get(1).id, 1); |
---|
93 | }, |
---|
94 | function testQueryAfterChanges(t){ |
---|
95 | t.is(store.query({prime: true}).length, 3); |
---|
96 | t.is(store.query({perfect: true}).length, 1); |
---|
97 | }, |
---|
98 | function testIFRSStyleData(t){ |
---|
99 | var anotherStore = new Memory({ |
---|
100 | data: { |
---|
101 | items:[ |
---|
102 | {name: "one", prime: false}, |
---|
103 | {name: "two", even: true, prime: true}, |
---|
104 | {name: "three", prime: true} |
---|
105 | ], |
---|
106 | identifier: "name" |
---|
107 | } |
---|
108 | }); |
---|
109 | t.is(anotherStore.get("one").name,"one"); |
---|
110 | t.is(anotherStore.query({name:"one"})[0].name,"one"); |
---|
111 | }, |
---|
112 | function testAddNewIdAssignment(t){ |
---|
113 | var object = { |
---|
114 | random: true |
---|
115 | }; |
---|
116 | store.add(object); |
---|
117 | t.t(!!object.id); |
---|
118 | } |
---|
119 | ] |
---|
120 | ); |
---|
121 | }); |
---|