Commit 86dd0f68 authored by Xiaowu Zhang's avatar Xiaowu Zhang

add new test for _start&_end

parent 68c4db2b
/*jslint indent: 2, maxlen: 80, nomen: true */ /*jslint indent: 2, maxlen: 80, nomen: true */
/*global module, test, stop, start, expect, ok, deepEqual, location, sinon, /*global module, test, stop, start, expect, ok, deepEqual, location, sinon,
davstorage_spec, RSVP, jIO, test_util, dav_storage, btoa, define, davstorage_spec, RSVP, jIO, test_util, dav_storage, btoa, define,
setTimeout, clearTimeout */ setTimeout, clearTimeout, indexedDB */
// define([module_name], [dependencies], module); // define([module_name], [dependencies], module);
(function (dependencies, module) { (function (dependencies, module) {
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
'qunit' 'qunit'
], function (util, RSVP, jIO) { ], function (util, RSVP, jIO) {
"use strict"; "use strict";
module("Indexeddb Storage"); module("indexeddbStorage");
function success(promise) { function success(promise) {
return new RSVP.Promise(function (resolve, notify) { return new RSVP.Promise(function (resolve, notify) {
promise.then(resolve, resolve, notify); promise.then(resolve, resolve, notify);
...@@ -27,26 +27,19 @@ ...@@ -27,26 +27,19 @@
}); });
} }
test("Scenario", 32, function () { test("Scenario", 46, function () {
// indexedDB.deleteDatabase("jio:test"); indexedDB.deleteDatabase("jio:test");
var server, shared = {}, jio = jIO.createJIO( var server, shared = {}, jio = jIO.createJIO(
{"type" : "indexeddb", {"type" : "indexeddb",
"database" : "test" "database" : "test"
}, },
{"workspace": {}} {"workspace": {}}
); );
stop(); stop();
server = {restore: function () { server = {restore: function () {
return; return;
}}; }};
function postNewDocument() { function postNewDocument() {
return jio.post({"title": "Unique ID"}); return jio.post({"title": "Unique ID"});
} }
...@@ -378,6 +371,85 @@ ...@@ -378,6 +371,85 @@
}); });
} }
function getFirstAttachmentRange1() {
return jio.getAttachment({"_id": "a",
"_attachment": "aa",
"_start": 0});
}
function getFirstAttachmentRangeTest1(answer) {
var blob = answer.data;
answer.data = "<blob>";
return jIO.util.readBlobAsText(blob).then(function (e) {
deepEqual(blob.type, "text/plain", "Check blob type");
deepEqual(e.target.result, "aab", "Check blob text content");
deepEqual(answer, {
"attachment": "aa",
"data": "<blob>",
"id": "a",
"method": "getAttachment",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Get first attachment with range :_start:0, _end:undefined");
}, function (err) {
deepEqual(err, "no error", "Check blob text content");
});
}
function getFirstAttachmentRange2() {
return jio.getAttachment({"_id": "a",
"_attachment": "aa",
"_start": 0,
"_end": 1});
}
function getFirstAttachmentRangeTest2(answer) {
var blob = answer.data;
answer.data = "<blob>";
return jIO.util.readBlobAsText(blob).then(function (e) {
deepEqual(blob.type, "text/plain", "Check blob type");
deepEqual(e.target.result, "a", "Check blob text content");
deepEqual(answer, {
"attachment": "aa",
"data": "<blob>",
"id": "a",
"method": "getAttachment",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Get first attachment with range :_start:0, _end:1");
}, function (err) {
deepEqual(err, "no error", "Check blob text content");
});
}
function getFirstAttachmentRange3() {
return jio.getAttachment({"_id": "a",
"_attachment": "aa",
"_start": 1,
"_end": 3});
}
function getFirstAttachmentRangeTest3(answer) {
var blob = answer.data;
answer.data = "<blob>";
return jIO.util.readBlobAsText(blob).then(function (e) {
deepEqual(blob.type, "text/plain", "Check blob type");
deepEqual(e.target.result, "ab", "Check blob text content");
deepEqual(answer, {
"attachment": "aa",
"data": "<blob>",
"id": "a",
"method": "getAttachment",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Get first attachment with range :_start:1, _end:3");
}, function (err) {
deepEqual(err, "no error", "Check blob text content");
});
}
function getSecondAttachment() { function getSecondAttachment() {
...@@ -398,12 +470,84 @@ ...@@ -398,12 +470,84 @@
"result": "success", "result": "success",
"status": 200, "status": 200,
"statusText": "Ok" "statusText": "Ok"
}, "Get first attachment"); }, "Get second attachment");
}, function (err) { }, function (err) {
deepEqual(err, "no error", "Check blob text content"); deepEqual(err, "no error", "Check blob text content");
}); });
} }
function getSecondAttachmentRange1() {
return success(jio.getAttachment({"_id": "a",
"_attachment": "ab",
"_start": -1}));
}
function getSecondAttachmentRangeTest1(answer) {
deepEqual(answer, {
"attachment": "ab",
"error": "not_found",
"id": "a",
"message": "_start and _end must be positive",
"method": "getAttachment",
"reason": "invalide _start, _end",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "get attachment with _start or _end negative -> 404 Not Found");
}
function getSecondAttachmentRange2() {
return success(jio.getAttachment({"_id": "a",
"_attachment": "ab",
"_start": 1,
"_end": 0}));
}
function getSecondAttachmentRangeTest2(answer) {
deepEqual(answer, {
"attachment": "ab",
"error": "not_found",
"id": "a",
"message": "start is great then end",
"method": "getAttachment",
"reason": "invalide offset",
"result": "error",
"status": 404,
"statusText": "Not Found"
}, "get attachment with _start > _end -> 404 Not Found");
}
function getSecondAttachmentRange3() {
return jio.getAttachment({"_id": "a",
"_attachment": "ab",
"_start": 1,
"_end": 2});
}
function getSecondAttachmentRangeTest3(answer) {
var blob = answer.data;
answer.data = "<blob>";
return jIO.util.readBlobAsText(blob).then(function (e) {
deepEqual(blob.type, "text/plain", "Check blob type");
deepEqual(e.target.result, "b", "Check blob text content");
deepEqual(answer, {
"attachment": "ab",
"data": "<blob>",
"id": "a",
"method": "getAttachment",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "Get second attachment with range :_start:1, _end:3");
}, function (err) {
deepEqual(err, "no error", "Check blob text content");
});
}
function getLastDocument() { function getLastDocument() {
return jio.get({"_id": "a"}); return jio.get({"_id": "a"});
} }
...@@ -621,8 +765,14 @@ ...@@ -621,8 +765,14 @@
then(updateLastDocument).then(updateLastDocumentTest). then(updateLastDocument).then(updateLastDocumentTest).
// getA a 200 // getA a 200
then(getFirstAttachment).then(getFirstAttachmentTest). then(getFirstAttachment).then(getFirstAttachmentTest).
then(getFirstAttachmentRange1).then(getFirstAttachmentRangeTest1).
then(getFirstAttachmentRange2).then(getFirstAttachmentRangeTest2).
then(getFirstAttachmentRange3).then(getFirstAttachmentRangeTest3).
// getA b 200 // getA b 200
then(getSecondAttachment).then(getSecondAttachmentTest). then(getSecondAttachment).then(getSecondAttachmentTest).
then(getSecondAttachmentRange1).then(getSecondAttachmentRangeTest1).
then(getSecondAttachmentRange2).then(getSecondAttachmentRangeTest2).
then(getSecondAttachmentRange3).then(getSecondAttachmentRangeTest3).
// get 200 // get 200
then(getLastDocument).then(getLastDocumentTest). then(getLastDocument).then(getLastDocumentTest).
// removeA b 204 // removeA b 204
......
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