| 1 |
define([ |
| 2 |
'intern!object', |
| 3 |
'intern/chai!assert', |
| 4 |
'../db/has!indexeddb?../db/IndexedDB', |
| 5 |
'../db/SQL', |
| 6 |
'dojo/promise/all', |
| 7 |
'dojo/sniff' |
| 8 |
], function (registerSuite, assert, IndexedDB, SQL, all, has) { |
| 9 |
var data = [ |
| 10 |
{id: 1, name: 'one', prime: false, mappedTo: 'E', words: ['banana']}, |
| 11 |
{id: 2, name: 'two', even: true, prime: true, mappedTo: 'D', words: ['banana', 'orange']}, |
| 12 |
{id: 3, name: 'three', prime: true, mappedTo: 'C', words: ['apple', 'orange']}, |
| 13 |
{id: 4, name: 'four', even: true, prime: false, mappedTo: null}, |
| 14 |
{id: 5, name: 'five', prime: true, mappedTo: 'A'} |
| 15 |
]; |
| 16 |
var dbConfig = { |
| 17 |
version: 5, |
| 18 |
stores: { |
| 19 |
test: { |
| 20 |
name: 10, |
| 21 |
even: {}, |
| 22 |
id: { |
| 23 |
autoIncrement: true, |
| 24 |
preference: 20 |
| 25 |
}, |
| 26 |
words: { |
| 27 |
multiEntry: true, |
| 28 |
preference: 5 |
| 29 |
}, |
| 30 |
mappedTo: { |
| 31 |
indexed: false |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
}; |
| 36 |
if (IndexedDB) { |
| 37 |
registerSuite(testsForDB('dojox/store/db/IndexedDB', IndexedDB)); |
| 38 |
} |
| 39 |
if (window.openDatabase) { |
| 40 |
registerSuite(testsForDB('dojox/store/db/SQL', SQL)); |
| 41 |
} |
| 42 |
function testsForDB(name, DB){ |
| 43 |
|
| 44 |
dbConfig.available = null; |
| 45 |
var db = new DB({dbConfig: dbConfig, storeName: 'test'}); |
| 46 |
function testQuery(query, options, results){ |
| 47 |
if(!results){ |
| 48 |
results = options; |
| 49 |
options = undefined; |
| 50 |
} |
| 51 |
return function(){ |
| 52 |
if(options && options.multi && has('trident')){ |
| 53 |
|
| 54 |
return; |
| 55 |
} |
| 56 |
var i = 0; |
| 57 |
var queryResults = db.query(query, options); |
| 58 |
var total = queryResults.total; |
| 59 |
return queryResults.forEach(function(object){ |
| 60 |
assert.strictEqual(results[i++], object.id); |
| 61 |
}).then(function(){ |
| 62 |
assert.strictEqual(results.length, i); |
| 63 |
if(!options){ |
| 64 |
return queryResults.total.then(function(total){ |
| 65 |
assert.strictEqual(results.length, total); |
| 66 |
}); |
| 67 |
} |
| 68 |
}); |
| 69 |
}; |
| 70 |
} |
| 71 |
return { |
| 72 |
name: name, |
| 73 |
setup: function(){ |
| 74 |
var results = []; |
| 75 |
return db.query({}).forEach(function(object){ |
| 76 |
|
| 77 |
results.push(db.remove(object.id)); |
| 78 |
}).then(function(){ |
| 79 |
return all(results); |
| 80 |
}).then(function(){ |
| 81 |
results = []; |
| 82 |
|
| 83 |
for (var i = 0; i < data.length; i++) { |
| 84 |
results.push(db.put(data[i])); |
| 85 |
} |
| 86 |
return all(results); |
| 87 |
}); |
| 88 |
}, |
| 89 |
"{id: 2}": testQuery({id: 2}, [2]), |
| 90 |
"{name: 'four'}": testQuery({name: 'four'}, [4]), |
| 91 |
"{name: 'two'}": testQuery({name: 'two'}, [2]), |
| 92 |
"{even: true}": testQuery({even: true}, [2, 4]), |
| 93 |
"{even: true, name: 'two'}": testQuery({even: true, name: 'two'}, [2]), |
| 94 |
|
| 95 |
"{mappedTo: 'C'}": testQuery({mappedTo: 'C'}, [3]), |
| 96 |
|
| 97 |
"[{name: 'two'}, {mappedTo: 'C'}, {mappedTo: 'D'}]": testQuery([{name: 'two'}, {mappedTo: 'C'}, {mappedTo: 'D'}], [2, 3]), |
| 98 |
"{id: {from: 1, to: 3}}": testQuery({id: {from: 1, to: 3}}, [1, 2, 3]), |
| 99 |
"{name: {from: 'm', to: 'three'}}": testQuery({name: {from: 'm', to: 'three'}}, [1, 3]), |
| 100 |
"{name: {from: 'one', to: 'three'}}": testQuery({name: {from: 'm', to: 'three'}}, [1, 3]), |
| 101 |
"{name: {from: 'one', excludeFrom: true, to: 'three'}}": testQuery({name: {from: 'one', excludeFrom: true, to: 'three'}}, [3]), |
| 102 |
"{name: {from: 'one', to: 'three', excludeTo: true}}": testQuery({name: {from: 'one', to: 'three', excludeTo: true}}, [1]), |
| 103 |
"{name: {from: 'one', excludeFrom: true, to: 'three', excludeTo: true}}": testQuery({name: {from: 'one', excludeFrom: true, to: 'three', excludeTo: true}}, []), |
| 104 |
"{name: 't*'}": testQuery({name: 't*'}, {sort:[{attribute: "name"}]}, [3, 2]), |
| 105 |
"{name: 'not a number'}": testQuery({name: 'not a number'}, []), |
| 106 |
"{words: {contains: ['orange']}}": testQuery({words: {contains: ['orange']}}, {multi: true}, [2, 3]), |
| 107 |
"{words: {contains: ['or*']}}": testQuery({words: {contains: ['or*']}}, {multi: true}, [2, 3]), |
| 108 |
"{words: {contains: ['apple', 'banana']}}": testQuery({words: {contains: ['apple', 'banana']}}, {multi: true}, []), |
| 109 |
"{words: {contains: ['orange', 'banana']}}": testQuery({words: {contains: ['orange', 'banana']}}, {multi: true}, [2]), |
| 110 |
"{id: {from: 0, to: 4}, words: {contains: ['orange', 'banana']}}": testQuery({id: {from: 0, to: 4}, words: {contains: ['orange', 'banana']}}, {multi: true}, [2]), |
| 111 |
|
| 112 |
"{id: {from: 1, to: 3}}, sort by name +": testQuery({id: {from: 1, to: 3}}, {sort:[{attribute: "name"}]}, [1, 3, 2]), |
| 113 |
"{id: {from: 1, to: 3}}, sort by name -": testQuery({id: {from: 1, to: 3}}, {sort:[{attribute: "name", descending: true}]}, [2, 3, 1]), |
| 114 |
"{id: {from: 0, to: 4}}, paged": testQuery({id: {from: 0, to: 4}}, {start: 1, count: 2}, [2, 3]), |
| 115 |
'db interaction': function(t){ |
| 116 |
return db.get(1).then(function(one){ |
| 117 |
assert.strictEqual(one.id, 1); |
| 118 |
assert.strictEqual(one.name, 'one'); |
| 119 |
assert.strictEqual(one.prime, false); |
| 120 |
assert.strictEqual(one.mappedTo, 'E'); |
| 121 |
return all([db.remove(2), db.remove(4), db.add({id: 6, name: 'six', prime: false, words: ['pineapple', 'orange juice']})]).then(function(){ |
| 122 |
return all([ |
| 123 |
testQuery({name: {from: 's', to: 'u'}}, [6, 3])(), |
| 124 |
testQuery({words: {contains: ['orange*']}}, {multi: true}, [3, 6])() |
| 125 |
]); |
| 126 |
}) |
| 127 |
}); |
| 128 |
}, |
| 129 |
|
| 130 |
}; |
| 131 |
} |
| 132 |
}); |