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 @@
(function (jIO) {
"use strict";
// Metadata keys included for internal revisioning, but not shown to user
//var _revision_metadata = ["_revision", "_doc_id"];
/**
* The jIO BryanStorage extension
*
......@@ -10,36 +13,15 @@
* @constructor
*/
function BryanStorage(spec) {
//this._sub_storage = jIO.createJIO(spec.sub_storage);
this._sub_storage = jIO.createJIO({
type: "query",
sub_storage: {
type: "uuid",
sub_storage: spec.sub_storage
}
});
}
BryanStorage.prototype.get = function (id_in) {
var substorage = this._sub_storage,
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
);
});
return this._sub_storage.get(id_in);
};
BryanStorage.prototype.post = function (metadata) {
......@@ -48,32 +30,52 @@
};
BryanStorage.prototype.put = function (id, new_metadata) {
var storage = this;
new_metadata._doc_id = id;
return storage.get(id)
.push(
function (metadata) {
// Increments existing "_revision" attribute
if (metadata.hasOwnProperty('_revision')) {
new_metadata._revision = metadata._revision + 1;
} else {
new_metadata._revision = 0;
var storage = this,
substorage = this._sub_storage,
previous_data;
return this._sub_storage.get(id)
.push(function (latest_data) {
// Prepare to post the current doc as a deprecated version
previous_data = latest_data;
previous_data._deprecated = true;
previous_data._doc_id = id;
// Get most recent deprecated version's _revision attribute
var options = {
query: '(_doc_id: "' + id + '")',
sort_on: [['_revision', 'descending']],
limit: [0, 1]
};
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);
}
//return storage.post.apply(substorage, new_metadata);
return storage.post(new_metadata);
throw new jIO.util.jIOError(
"bryanstorage: query returned no results.'",
404
);
})
.push(function (doc) {
previous_data._revision = doc._revision + 1;
return storage.post(previous_data);
},
function () {
// Creates new attribute "_revision" = 0
new_metadata._revision = 0;
return storage.post(new_metadata);
// 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);
}
);
};
BryanStorage.prototype.allDocs = function (options) {
//console.log(options);
return this._sub_storage.allDocs.apply(this._sub_storage, options);
})
// No matter what happened, need to put new document in
.push(function () {
return substorage.put(id, new_metadata);
});
};
BryanStorage.prototype.allAttachments = function () {
......@@ -139,8 +141,27 @@
BryanStorage.prototype.hasCapacity = function () {
return this._sub_storage.hasCapacity.apply(this._sub_storage, arguments);
};
BryanStorage.prototype.buildQuery = function () {
return this._sub_storage.buildQuery.apply(this._sub_storage, arguments);
BryanStorage.prototype.allDocs = function (options) {
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);
......
......@@ -47,11 +47,28 @@
module("bryanStorage.revision_history");
test("put and get the correct version", function () {
stop();
expect(1);
var jio = jIO.createJIO({
expect(4);
var dbname = "testingdb4",
jio = jIO.createJIO({
type: "bryan",
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", {
......@@ -61,13 +78,82 @@
.push(function () {return jio.get("doc1"); })
.push(function (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",
"subtitle": "subrev0",
"_doc_id": "doc1",
"_revision": 0,
"_doc_id": "doc1"
}, "Retrieve document correctly");
"_deprecated": true
},
"Get the earliest copy of the doc with all metadata.");
})
.fail(function (error) {
console.log(error);
ok(false, error);
})
.always(function () {
......@@ -78,11 +164,22 @@
module("bryanStorage.revision_history_multiple_edits");
test("modify first version but save both", function () {
stop();
expect(6);
expect(7);
var jio = jIO.createJIO({
type: "bryan",
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", {
......@@ -113,49 +210,67 @@
"subtitle": "subrev2"
});
})
.push(function () {
return jio.put("main_doc", {
"title": "rev3",
"subtitle": "subrev3"
});
})
.push(function () {return jio.get("main_doc"); })
.push(function (result) {
deepEqual(result, {
"title": "rev2",
"subtitle": "subrev2",
"_revision": 2,
"_doc_id": "main_doc"
"title": "rev3",
"subtitle": "subrev3"
}, "Retrieve main document correctly");
})
.push(function () {return jio.get("other_doc"); })
.push(function (result) {
deepEqual(result, {
"attr": "version1",
"subattr": "subversion1",
"_revision": 1,
"_doc_id": "other_doc"
"subattr": "subversion1"
}, "Retrieve other document correctly");
})
.push(function () {
return jio.buildQuery({
query: '(_doc_id: "main_doc") AND (_revision: 0)',
sort_on: [['_revision', 'descending']]
query: ""
});
})
.push(function (result) {
//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) {
equal(result.length, 1, "Correct number of results returned");
//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 () {
return jio.buildQuery({
query: '(_doc_id: "main_doc") AND (_revision: 1)'
query: '(_doc_id: "other_doc")'
});
})
.push(function (result) {
equal(result.length, 1, "Correct number of results returned");
equal(result.length, 0, "Correct number of results returned");
})
.push(function () {
return jio.buildQuery({
query: '(_doc_id: "other_doc") AND (_revision: 0)'
query: '(_revision: 0)'
});
})
.push(function (result) {
equal(result.length, 1, "Correct number of results returned");
equal(result.length, 0, "Correct number of results returned");
})
.push(function () {
return jio.buildQuery({
......@@ -163,7 +278,52 @@
});
})
.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) {
//console.log(error);
......@@ -175,5 +335,3 @@
});
}(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