Commit 4fdf47f9 authored by Romain Courteaud's avatar Romain Courteaud

Add qunit scenario test template.

This shows how to use localstorage, indexedDB, dav, replicate.
parent bbbb2fe2
......@@ -5,11 +5,12 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jIO Coverage Scenario</title>
<link rel="stylesheet" href="../node_modules/grunt-contrib-qunit/test/libs/qunit.css" type="text/css" media="screen"/>
<script src="../node_modules/grunt-contrib-qunit/test/libs/qunit.js" type="text/javascript"></script>
<script src="../node_modules/rsvp/dist/rsvp-2.0.4.js"></script>
<script src="../node_modules/renderjs/dist/renderjs-latest.js"></script>
<script src="../dist/jio-latest.js"></script>
<link rel="stylesheet" href="../node_modules/grunt-contrib-qunit/test/libs/qunit.css" type="text/css" media="screen"/>
<script src="../node_modules/grunt-contrib-qunit/test/libs/qunit.js" type="text/javascript"></script>
<script src="scenario.js"></script>
</head>
......
/*global console, btoa*/
/*jslint nomen: true*/
/*global console, btoa, Blob*/
/*jslint nomen: true, maxlen: 200*/
(function (window, QUnit, jIO, rJS) {
"use strict";
var test = QUnit.test,
......@@ -11,34 +11,116 @@
deepEqual = QUnit.deepEqual;
rJS(window)
.ready(function (g) {
return g.run({
"type": "indexeddb",
"database": "test"
});
})
.ready(function (g) {
return g.run({
type: "query",
sub_storage: {
type: "local"
}
});
})
.ready(function (g) {
return g.run({
type: "query",
sub_storage: {
"type": "dav"
}
});
///////////////////////////
// Local storage
///////////////////////////
// return g.run({
// type: "query",
// sub_storage: {
// type: "uuid",
// sub_storage: {
// type: "document",
// document_id: "/",
// sub_storage: {
// type: "local"
// }
// }
// }
// });
///////////////////////////
// Memory storage
///////////////////////////
// return g.run({
// type: "query",
// sub_storage: {
// type: "uuid",
// sub_storage: {
// type: "union",
// storage_list: [{
// type: "memory"
// }]
// }
// }
// });
///////////////////////////
// IndexedDB storage
///////////////////////////
// return g.run({
// type: "query",
// sub_storage: {
// type: "uuid",
// sub_storage: {
// "type": "indexeddb",
// "database": "test"
// }
// }
// });
///////////////////////////
// DAV storage
///////////////////////////
// return g.run({
// type: "query",
// sub_storage: {
// type: "uuid",
// sub_storage: {
// type: "drivetojiomapping",
// sub_storage: {
// "type": "dav",
// "url": "DAVURL",
// "basic_login": btoa("LOGIN:PASSWD")
// }
// }
// }
// });
///////////////////////////
// Qiniu storage
///////////////////////////
// return g.run({
// type: "query",
// sub_storage: {
// type: "uuid",
// sub_storage: {
// "type": "qiniu",
// "bucket": "BUCKET",
// "access_key": "ACCESSKEY",
// "secret_key": "SECRETKEY"
// }
// }
// });
///////////////////////////
// Replicate storage
///////////////////////////
// return g.run({
// type: "query",
// sub_storage: {
// type: "uuid",
// sub_storage: {
// type: "replicate",
// local_sub_storage: {
// type: "memory"
// },
// remote_sub_storage: {
// "type": "memory"
// }
// }
// }
// });
})
.declareMethod('run', function (jio_options) {
test('Test "' + jio_options.type + '"scenario', function () {
var jio;
stop();
expect(9);
expect(14);
try {
jio = jIO.createJIO(jio_options);
......@@ -49,33 +131,40 @@
}
// Try to fetch inexistent document
jio.get({"_id": "inexistent"})
jio.get("inexistent")
.fail(function (error) {
console.error(error);
if (error.status_code !== 404) {
throw error;
}
equal(error.status_code, 404, "404 if inexistent");
// Post a document without ID
return jio.post({"title": "I don't have ID"});
return jio.post({"title": "I don't have ID éà&\n"});
})
.then(function (doc_id) {
ok(doc_id, "Document without ID created");
ok(doc_id, "Document without ID created (" + doc_id + ")");
// Fetch the newly created document
return jio.get({"_id": doc_id});
return RSVP.all([
jio.get(doc_id),
doc_id
]);
})
.then(function (doc) {
var doc_id = doc._id;
delete doc._id;
deepEqual(doc, {"title": "I don't have ID"},
.then(function (result_list) {
var doc = result_list[0],
doc_id = result_list[1];
deepEqual(doc, {"title": "I don't have ID éà&\n"},
"Document correctly fetched");
// Remove the doc
return jio.remove({"_id": doc_id});
return jio.remove(doc_id);
})
.then(function (doc_id) {
ok(doc_id, "Document removed");
// Create some documents to check allDocs
return RSVP.all([
jio.put({"_id": "id1", "title": "1 ID", "int_index": 1}),
jio.put({"_id": "id2", "title": "2 ID", "int_index": 2}),
jio.put({"_id": "id3", "title": "3 ID", "int_index": 3})
jio.put("id1", {"title": "1 ID", "int_index": 1}),
jio.put("id2", {"title": "2 ID", "int_index": 2}),
jio.put("id3", {"title": "3 ID", "int_index": 3})
]);
})
.then(function (all_doc_id) {
......@@ -83,27 +172,28 @@
equal(all_doc_id[1], "id2", "Document 2 correctly created");
equal(all_doc_id[2], "id3", "Document 3 correctly created");
// Default allDocs call
return jio.allDocs();
})
.then(function (result) {
deepEqual(result, {
data: {
rows: [{
id: "id1",
value: {}
}, {
id: "id2",
value: {}
}, {
id: "id3",
value: {}
}],
total_rows: 3
}
}, "default allDocs OK");
// // Default allDocs call
// return jio.allDocs();
// })
// .then(function (result) {
// deepEqual(result, {
// data: {
// rows: [{
// id: "id1",
// value: {}
// }, {
// id: "id2",
// value: {}
// }, {
// id: "id3",
// value: {}
// }],
// total_rows: 3
// }
// }, "default allDocs OK");
// Filter the result
console.log("START ALLDOCS");
return jio.allDocs({
query: 'title: "2 ID"',
select_list: ["int_index"]
......@@ -123,7 +213,65 @@
// XXX Check include docs, sort, limit, select
})
.then(function () {
return jio.getAttachment("inexistent", "enclosure")
.fail(function (error) {
equal(error.status_code, 404, "404 if inexistent");
console.log(error);
});
})
.then(function () {
return jio.put("test.txt", {});
})
.then(function () {
return jio.putAttachment(
"test.txt",
"enclosure",
new Blob(["fooé\nbar"], {type: "text/plain"})
);
})
.then(function () {
ok(true, "Attachment stored");
return jio.getAttachment("test.txt", "enclosure");
})
.then(function (blob) {
return jIO.util.readBlobAsText(blob);
})
.then(function (result) {
equal(result.target.result, "fooé\nbar", "Attachment correctly fetched");
return jio.get("test.txt");
})
.then(function (doc) {
deepEqual(doc, {}, "Document correctly fetched");
return jio.allAttachments("test.txt");
})
.then(function (doc) {
deepEqual(doc, {
enclosure: {}
},
"Attachment list correctly fetched");
return jio.removeAttachment("test.txt", "enclosure");
})
.then(function () {
ok("Attachment removed");
})
.then(function () {
return jio.repair();
})
.fail(function (error) {
console.error("---");
console.error(error.stack);
console.error(error);
ok(false, error);
......@@ -134,5 +282,4 @@
});
});
}(window, QUnit, jIO, rJS));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment