Commit 44628a99 authored by Bryan Kaperick's avatar Bryan Kaperick

Tests are not yet passing. An issue exists with bryanstorage buildQuery.

parent 3598f85f
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
(function (jIO) { (function (jIO) {
"use strict"; "use strict";
// Metadata keys included for internal revisioning, but not shown to user
//var _revision_metadata = ["_revision", "_doc_id"];
/** /**
* The jIO BryanStorage extension * The jIO BryanStorage extension
* *
...@@ -10,36 +13,15 @@ ...@@ -10,36 +13,15 @@
* @constructor * @constructor
*/ */
function BryanStorage(spec) { function BryanStorage(spec) {
//this._sub_storage = jIO.createJIO(spec.sub_storage);
this._sub_storage = jIO.createJIO({ this._sub_storage = jIO.createJIO({
type: "query", type: "query",
sub_storage: { sub_storage: spec.sub_storage
type: "uuid",
sub_storage: spec.sub_storage
}
}); });
} }
BryanStorage.prototype.get = function (id_in) { BryanStorage.prototype.get = function (id_in) {
var substorage = this._sub_storage, return this._sub_storage.get(id_in);
options = {
query: '(_doc_id: "' + id_in + '")',// AND (include_docs: true)',
sort_on: [['_revision', 'descending']]
//include_docs: true
};
return substorage.allDocs(options)
// Return query results if there are any, else throw error
.push(function (query_results) {
var docs = query_results.data.rows;
if (docs.length > 0) {
return substorage.get(docs[0].id);
}
throw new jIO.util.jIOError(
"bryanstorage: cannot find object '" + id_in + "'",
404
);
});
}; };
BryanStorage.prototype.post = function (metadata) { BryanStorage.prototype.post = function (metadata) {
...@@ -48,32 +30,52 @@ ...@@ -48,32 +30,52 @@
}; };
BryanStorage.prototype.put = function (id, new_metadata) { BryanStorage.prototype.put = function (id, new_metadata) {
var storage = this; var storage = this,
new_metadata._doc_id = id; substorage = this._sub_storage,
return storage.get(id) previous_data;
.push(
function (metadata) { return this._sub_storage.get(id)
.push(function (latest_data) {
// Increments existing "_revision" attribute
if (metadata.hasOwnProperty('_revision')) { // Prepare to post the current doc as a deprecated version
new_metadata._revision = metadata._revision + 1; previous_data = latest_data;
} else { previous_data._deprecated = true;
new_metadata._revision = 0; previous_data._doc_id = id;
}
//return storage.post.apply(substorage, new_metadata); // Get most recent deprecated version's _revision attribute
return storage.post(new_metadata); var options = {
}, query: '(_doc_id: "' + id + '")',
function () { sort_on: [['_revision', 'descending']],
// Creates new attribute "_revision" = 0 limit: [0, 1]
new_metadata._revision = 0; };
return storage.post(new_metadata); return substorage.buildQuery(options);
})
.push(function (query_results) {
if (query_results.length > 0) {
var doc_id = query_results[0];
return this._sub_storage.get(doc_id);
} }
); throw new jIO.util.jIOError(
}; "bryanstorage: query returned no results.'",
404
BryanStorage.prototype.allDocs = function (options) { );
//console.log(options); })
return this._sub_storage.allDocs.apply(this._sub_storage, options); .push(function (doc) {
previous_data._revision = doc._revision + 1;
return storage.post(previous_data);
},
function () {
// If the query turned up no results,
// there was exactly 1 version previously.
if (previous_data !== undefined) {
previous_data._revision = 0;
return storage.post(previous_data);
}
})
// No matter what happened, need to put new document in
.push(function () {
return substorage.put(id, new_metadata);
});
}; };
BryanStorage.prototype.allAttachments = function () { BryanStorage.prototype.allAttachments = function () {
...@@ -139,8 +141,27 @@ ...@@ -139,8 +141,27 @@
BryanStorage.prototype.hasCapacity = function () { BryanStorage.prototype.hasCapacity = function () {
return this._sub_storage.hasCapacity.apply(this._sub_storage, arguments); return this._sub_storage.hasCapacity.apply(this._sub_storage, arguments);
}; };
BryanStorage.prototype.buildQuery = function () { BryanStorage.prototype.allDocs = function (options) {
return this._sub_storage.buildQuery.apply(this._sub_storage, arguments); if (options === undefined) {
options = {};
}
console.log("options", options);
/**
if (options === undefined) {
options = {query: ""};
}
options.query = '(' + options.query + ') AND NOT (_deprecated = true)';
console.log("query string: ", options.query);
**/
return this._sub_storage.allDocs.apply(this._sub_storage, options);
//return this._sub_storage.buildQuery.apply(this._sub_storage, options);
};
BryanStorage.prototype.buildQuery = function (options) {
if (options === undefined) {
options = {};
}
console.log("options", options);
return this._sub_storage.buildQuery.apply(this._sub_storage, options);
}; };
jIO.addStorage('bryan', BryanStorage); jIO.addStorage('bryan', BryanStorage);
......
...@@ -47,11 +47,28 @@ ...@@ -47,11 +47,28 @@
module("bryanStorage.revision_history"); module("bryanStorage.revision_history");
test("put and get the correct version", function () { test("put and get the correct version", function () {
stop(); stop();
expect(1); expect(4);
var jio = jIO.createJIO({ var dbname = "testingdb4",
jio = jIO.createJIO({
type: "bryan", type: "bryan",
sub_storage: { sub_storage: {
type: "memory" type: "uuid",
sub_storage: {
//type: "memory"
type: "indexeddb",
database: dbname
}
}
}),
not_bryan = jIO.createJIO({
type: "uuid",
sub_storage: {
type: "query",
sub_storage: {
//type: "memory"
type: "indexeddb",
database: dbname
}
} }
}); });
jio.put("doc1", { jio.put("doc1", {
...@@ -61,13 +78,82 @@ ...@@ -61,13 +78,82 @@
.push(function () {return jio.get("doc1"); }) .push(function () {return jio.get("doc1"); })
.push(function (result) { .push(function (result) {
deepEqual(result, { deepEqual(result, {
"title": "rev0",
"subtitle": "subrev0"
}, "Retrieve first edition of document correctly");
})
.push(function () {
return jio.put("doc1", {
"title": "rev1",
"subtitle": "subrev1"
});
})
.push(function () {
return jio.put("doc1", {
"title": "rev2",
"subtitle": "subrev2"
});
})
.push(function () {
return jio.get("doc1");
})
.push(function (result) {
deepEqual(result, {
"title": "rev2",
"subtitle": "subrev2"
}, "Retrieve second edition of document correctly");
})
.push(function () {
var options = {
//query: ""//title: rev2"
};
//
//
return jio.buildQuery(options);
//return jio.allDocs(options);
//
//
})
.push(function (results) {
console.log("query results: ", results);
equal(results.data.rows.length, 1, "Query only returns latest version");
if (results.data.rows.length > 0) {
return jio.get(results.data.rows[0].id);
}
})
.push(function (result) {
deepEqual(result, {
"title": "rev2",
"subtitle": "subrev2"
}, "Retrieve queried document correctly");
})
// When not_bryan queries the storage, all documents are returned.
.push(function () {
var options = {
query: "",
sort_on: [["_revision", "ascending"]]
};
return jio.allDocs(options);
})
.push(function (results) {
equal(results.length, 2, "should get all 2 revisions.");
if (results.length > 0) {
return not_bryan.get(results[0].id);
}
})
.push(function (results) {
deepEqual(results, {
"title": "rev0", "title": "rev0",
"subtitle": "subrev0", "subtitle": "subrev0",
"_doc_id": "doc1",
"_revision": 0, "_revision": 0,
"_doc_id": "doc1" "_deprecated": true
}, "Retrieve document correctly"); },
"Get the earliest copy of the doc with all metadata.");
}) })
.fail(function (error) { .fail(function (error) {
console.log(error);
ok(false, error); ok(false, error);
}) })
.always(function () { .always(function () {
...@@ -78,13 +164,24 @@ ...@@ -78,13 +164,24 @@
module("bryanStorage.revision_history_multiple_edits"); module("bryanStorage.revision_history_multiple_edits");
test("modify first version but save both", function () { test("modify first version but save both", function () {
stop(); stop();
expect(6); expect(7);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "bryan", type: "bryan",
sub_storage: { sub_storage: {
type: "memory" type: "uuid",
} sub_storage: {
}); type: "indexeddb",
database: "testdb1"
}
}
}),
not_bryan = jIO.createJIO({
type: "uuid",
sub_storage: {
type: "indexeddb",
database: "testdb1"
}
});
jio.put("main_doc", { jio.put("main_doc", {
"title": "rev0", "title": "rev0",
"subtitle": "subrev0" "subtitle": "subrev0"
...@@ -113,49 +210,67 @@ ...@@ -113,49 +210,67 @@
"subtitle": "subrev2" "subtitle": "subrev2"
}); });
}) })
.push(function () {
return jio.put("main_doc", {
"title": "rev3",
"subtitle": "subrev3"
});
})
.push(function () {return jio.get("main_doc"); }) .push(function () {return jio.get("main_doc"); })
.push(function (result) { .push(function (result) {
deepEqual(result, { deepEqual(result, {
"title": "rev2", "title": "rev3",
"subtitle": "subrev2", "subtitle": "subrev3"
"_revision": 2,
"_doc_id": "main_doc"
}, "Retrieve main document correctly"); }, "Retrieve main document correctly");
}) })
.push(function () {return jio.get("other_doc"); }) .push(function () {return jio.get("other_doc"); })
.push(function (result) { .push(function (result) {
deepEqual(result, { deepEqual(result, {
"attr": "version1", "attr": "version1",
"subattr": "subversion1", "subattr": "subversion1"
"_revision": 1,
"_doc_id": "other_doc"
}, "Retrieve other document correctly"); }, "Retrieve other document correctly");
}) })
.push(function () { .push(function () {
return jio.buildQuery({ return jio.buildQuery({
query: '(_doc_id: "main_doc") AND (_revision: 0)', query: ""
sort_on: [['_revision', 'descending']]
}); });
}) })
.push(function (result) { .push(function (result) {
equal(result.length, 1, "Correct number of results returned"); //console.log(result);
equal(result.length, 2, "Empty query returns only non-deprecated docs");
})
.push(function () {
return jio.buildQuery({
query: 'attr: "version1"'
});
})
.push(function (result) {
//console.log("res:", result);
if (result.length > 0) {
return jio.get(result[0].id);
}
})
.push(function (result) {
deepEqual(result, {
"attr": "version1",
"subattr": "subversion1"
}, "Retrieve other document correctly");
}) })
.push(function () { .push(function () {
return jio.buildQuery({ return jio.buildQuery({
query: '(_doc_id: "main_doc") AND (_revision: 1)' query: '(_doc_id: "other_doc")'
}); });
}) })
.push(function (result) { .push(function (result) {
equal(result.length, 1, "Correct number of results returned"); equal(result.length, 0, "Correct number of results returned");
}) })
.push(function () { .push(function () {
return jio.buildQuery({ return jio.buildQuery({
query: '(_doc_id: "other_doc") AND (_revision: 0)' query: '(_revision: 0)'
}); });
}) })
.push(function (result) { .push(function (result) {
equal(result.length, 1, "Correct number of results returned"); equal(result.length, 0, "Correct number of results returned");
}) })
.push(function () { .push(function () {
return jio.buildQuery({ return jio.buildQuery({
...@@ -163,7 +278,52 @@ ...@@ -163,7 +278,52 @@
}); });
}) })
.push(function (result) { .push(function (result) {
equal(result.length, 5, "Correct number of results returned"); equal(result.length, 2, "Correct number of results returned");
})
// When not_bryan queries the storage, all documents are returned.
.push(function () {
var options = {
query: "_doc_id: main_doc",
sort_on: [["_revision", "ascending"]]
};
return not_bryan.buildQuery(options);
})
.push(function (results) {
equal(results.length, 3, "should get all 3 deprecated versions.");
return not_bryan.get(results[0].id);
})
.push(function (results) {
deepEqual(results, {
"title": "rev0",
"subtitle": "subrev0",
"_doc_id": "main_doc",
"_revision": 0,
"_deprecated": true
},
"Get the earliest copy of the doc with all metadata.");
})
// When not_bryan queries the storage, all documents are returned.
.push(function () {
var options = {
query: "_doc_id: main_doc",
sort_on: [["_revision", "ascending"]]
};
return not_bryan.buildQuery(options);
})
.push(function (results) {
return not_bryan.get(results[1].id);
})
.push(function (results) {
deepEqual(results, {
"title": "rev1",
"subtitle": "subrev1",
"_doc_id": "main_doc",
"_revision": 1,
"_deprecated": true
},
"Get the earliest copy of the doc with all metadata.");
}) })
.fail(function (error) { .fail(function (error) {
//console.log(error); //console.log(error);
...@@ -174,6 +334,4 @@ ...@@ -174,6 +334,4 @@
}); });
}); });
}(jIO, QUnit)); }(jIO, QUnit));
\ No newline at end of file
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