Commit 3c3d9a39 authored by Romain Courteaud's avatar Romain Courteaud

ReplicateStorage: allow to sync only some documents.

Definition is done by a jIO.allDocs query
parent 7e85fd8e
...@@ -36,12 +36,15 @@ ...@@ -36,12 +36,15 @@
} }
function ReplicateStorage(spec) { function ReplicateStorage(spec) {
this._query_options = spec.query || {};
this._local_sub_storage = jIO.createJIO(spec.local_sub_storage); this._local_sub_storage = jIO.createJIO(spec.local_sub_storage);
this._remote_sub_storage = jIO.createJIO(spec.remote_sub_storage); this._remote_sub_storage = jIO.createJIO(spec.remote_sub_storage);
this._signature_hash = "_replicate_" + generateHash( this._signature_hash = "_replicate_" + generateHash(
JSON.stringify(spec.local_sub_storage) + JSON.stringify(spec.local_sub_storage) +
JSON.stringify(spec.remote_sub_storage) JSON.stringify(spec.remote_sub_storage) +
JSON.stringify(this._query_options)
); );
this._signature_sub_storage = jIO.createJIO({ this._signature_sub_storage = jIO.createJIO({
type: "document", type: "document",
...@@ -236,7 +239,7 @@ ...@@ -236,7 +239,7 @@
return queue return queue
.push(function () { .push(function () {
return RSVP.all([ return RSVP.all([
source.allDocs(), source.allDocs(context._query_options),
context._signature_sub_storage.allDocs() context._signature_sub_storage.allDocs()
]); ]);
}) })
......
...@@ -44,8 +44,10 @@ ...@@ -44,8 +44,10 @@
ok(jio.__storage._remote_sub_storage instanceof jio.constructor); ok(jio.__storage._remote_sub_storage instanceof jio.constructor);
equal(jio.__storage._remote_sub_storage.__type, "replicatestorage500"); equal(jio.__storage._remote_sub_storage.__type, "replicatestorage500");
deepEqual(jio.__storage._query_options, {});
equal(jio.__storage._signature_hash, equal(jio.__storage._signature_hash,
"_replicate_7b54b9b5183574854e5870beb19b15152a36ef4e"); "_replicate_7209dfbcaff00f6637f939fdd71fa896793ed385");
ok(jio.__storage._signature_sub_storage instanceof jio.constructor); ok(jio.__storage._signature_sub_storage instanceof jio.constructor);
equal(jio.__storage._signature_sub_storage.__type, "document"); equal(jio.__storage._signature_sub_storage.__type, "document");
...@@ -60,6 +62,27 @@ ...@@ -60,6 +62,27 @@
}); });
test("accept query", function () {
var jio = jIO.createJIO({
type: "replicate",
local_sub_storage: {
type: "replicatestorage200"
},
remote_sub_storage: {
type: "replicatestorage500"
},
query: {query: 'portal_type: "Foo"', limit: [0, 1234567890]}
});
deepEqual(
jio.__storage._query_options,
{query: 'portal_type: "Foo"', limit: [0, 1234567890]}
);
equal(jio.__storage._signature_hash,
"_replicate_623653d45a4e770a2c9f6b71e3144d18ee1b5bec");
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// replicateStorage.get // replicateStorage.get
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -994,7 +1017,7 @@ ...@@ -994,7 +1017,7 @@
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document: " + equal(error.message, "Cannot find document: " +
"_replicate_2be6c0851d60bcd9afe829e7133a136d266c779c"); "_replicate_e0cd4a29dc7c74a9de1d7a9cdbfcbaa776863d67");
equal(error.status_code, 404); equal(error.status_code, 404);
}) })
.then(function () { .then(function () {
...@@ -1008,7 +1031,7 @@ ...@@ -1008,7 +1031,7 @@
.fail(function (error) { .fail(function (error) {
ok(error instanceof jIO.util.jIOError); ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document: " + equal(error.message, "Cannot find document: " +
"_replicate_2be6c0851d60bcd9afe829e7133a136d266c779c"); "_replicate_e0cd4a29dc7c74a9de1d7a9cdbfcbaa776863d67");
equal(error.status_code, 404); equal(error.status_code, 404);
}) })
.fail(function (error) { .fail(function (error) {
...@@ -1088,4 +1111,102 @@ ...@@ -1088,4 +1111,102 @@
}); });
}); });
test("sync all documents by default", function () {
stop();
expect(5);
var context = this;
function Storage200DefaultQuery() {
return this;
}
Storage200DefaultQuery.prototype.get = function () {
ok(true, "get 200 check repair called");
return {};
};
Storage200DefaultQuery.prototype.hasCapacity = function () {
return true;
};
Storage200DefaultQuery.prototype.buildQuery = function (query) {
deepEqual(query, {});
return [];
};
Storage200DefaultQuery.prototype.allAttachments = function () {
ok(true, "allAttachments 200 check repair called");
return {};
};
jIO.addStorage(
'replicatestorage200defaultquery',
Storage200DefaultQuery
);
this.jio = jIO.createJIO({
type: "replicate",
local_sub_storage: {
type: "replicatestorage200defaultquery"
},
remote_sub_storage: {
type: "replicatestorage200defaultquery"
}
});
return context.jio.repair()
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("sync can be restricted to some documents", function () {
stop();
expect(5);
var context = this,
query = {query: 'portal_type: "Foo"', limit: [0, 1234567890]};
function Storage200CustomQuery() {
return this;
}
Storage200CustomQuery.prototype.get = function () {
ok(true, "get 200 check repair called");
return {};
};
Storage200CustomQuery.prototype.hasCapacity = function () {
return true;
};
Storage200CustomQuery.prototype.buildQuery = function (options) {
deepEqual(options, query);
return [];
};
Storage200CustomQuery.prototype.allAttachments = function () {
ok(true, "allAttachments 200 check repair called");
return {};
};
jIO.addStorage(
'replicatestorage200customquery',
Storage200CustomQuery
);
this.jio = jIO.createJIO({
type: "replicate",
local_sub_storage: {
type: "replicatestorage200customquery"
},
remote_sub_storage: {
type: "replicatestorage200customquery"
},
query: {query: 'portal_type: "Foo"', limit: [0, 1234567890]}
});
return context.jio.repair()
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
}(jIO, QUnit)); }(jIO, QUnit));
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