mirror of
https://github.com/nasa/openmct.git
synced 2025-04-09 04:14:32 +00:00
[Persistence] Complete specs for CouchDB adapter
Complete test coverage for CouchDB adapter, WTD-537.
This commit is contained in:
parent
bb719a137a
commit
c303d29768
@ -38,10 +38,6 @@ define(
|
||||
return request(subpath, "PUT", value);
|
||||
}
|
||||
|
||||
function del(subpath, value) {
|
||||
return request(subpath, "DELETE", value);
|
||||
}
|
||||
|
||||
function getIdsFromAllDocs(allDocs) {
|
||||
return allDocs.rows.map(function (r) { return r.id; });
|
||||
}
|
||||
@ -61,7 +57,7 @@ define(
|
||||
revs[response.id] = response.rev;
|
||||
return response.ok;
|
||||
} else {
|
||||
return undefined;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,8 +13,155 @@ define(
|
||||
mockQ,
|
||||
testSpace = "testSpace",
|
||||
testPath = "/test/db",
|
||||
capture,
|
||||
provider;
|
||||
|
||||
function mockPromise(value) {
|
||||
return {
|
||||
then: function (callback) {
|
||||
return mockPromise(callback(value));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(function () {
|
||||
mockHttp = jasmine.createSpy("$http");
|
||||
mockQ = jasmine.createSpyObj("$q", ["when"]);
|
||||
|
||||
mockQ.when.andCallFake(mockPromise);
|
||||
|
||||
// Capture promise results
|
||||
capture = jasmine.createSpy("capture");
|
||||
|
||||
provider = new CouchPersistenceProvider(
|
||||
mockHttp,
|
||||
mockQ,
|
||||
testSpace,
|
||||
testPath
|
||||
);
|
||||
});
|
||||
|
||||
it("reports available spaces", function () {
|
||||
provider.listSpaces().then(capture);
|
||||
expect(capture).toHaveBeenCalledWith([testSpace]);
|
||||
});
|
||||
|
||||
it("lists all available documents", function () {
|
||||
mockHttp.andReturn(mockPromise({
|
||||
data: { rows: [ { id: "a" }, { id: "b" }, { id: "c" } ] }
|
||||
}));
|
||||
provider.listObjects().then(capture);
|
||||
expect(mockHttp).toHaveBeenCalledWith({
|
||||
url: "/test/db/_all_docs", // couch document listing
|
||||
method: "GET"
|
||||
});
|
||||
expect(capture).toHaveBeenCalledWith(["a", "b", "c"]);
|
||||
});
|
||||
|
||||
it("allows object creation", function () {
|
||||
var model = { someKey: "some value" };
|
||||
mockHttp.andReturn(mockPromise({
|
||||
data: { "_id": "abc", "_rev": "xyz", "ok": true }
|
||||
}));
|
||||
provider.createObject("testSpace", "abc", model).then(capture);
|
||||
expect(mockHttp).toHaveBeenCalledWith({
|
||||
url: "/test/db/abc", // couch document listing
|
||||
method: "PUT",
|
||||
data: {
|
||||
"_id": "abc",
|
||||
metadata: jasmine.any(Object),
|
||||
model: model
|
||||
}
|
||||
});
|
||||
expect(capture).toHaveBeenCalledWith(true);
|
||||
});
|
||||
|
||||
it("allows object models to be read back", function () {
|
||||
var model = { someKey: "some value" };
|
||||
mockHttp.andReturn(mockPromise({
|
||||
data: { "_id": "abc", "_rev": "xyz", "model": model }
|
||||
}));
|
||||
provider.readObject("testSpace", "abc").then(capture);
|
||||
expect(mockHttp).toHaveBeenCalledWith({
|
||||
url: "/test/db/abc", // couch document listing
|
||||
method: "GET"
|
||||
});
|
||||
expect(capture).toHaveBeenCalledWith(model);
|
||||
});
|
||||
|
||||
it("allows object update", function () {
|
||||
var model = { someKey: "some value" };
|
||||
|
||||
// First do a read to populate rev tags...
|
||||
mockHttp.andReturn(mockPromise({
|
||||
data: { "_id": "abc", "_rev": "xyz", "model": {} }
|
||||
}));
|
||||
provider.readObject("testSpace", "abc");
|
||||
|
||||
// Now perform an update
|
||||
mockHttp.andReturn(mockPromise({
|
||||
data: { "_id": "abc", "_rev": "uvw", "ok": true }
|
||||
}));
|
||||
provider.updateObject("testSpace", "abc", model).then(capture);
|
||||
expect(mockHttp).toHaveBeenCalledWith({
|
||||
url: "/test/db/abc", // couch document listing
|
||||
method: "PUT",
|
||||
data: {
|
||||
"_id": "abc",
|
||||
"_rev": "xyz",
|
||||
metadata: jasmine.any(Object),
|
||||
model: model
|
||||
}
|
||||
});
|
||||
expect(capture).toHaveBeenCalledWith(true);
|
||||
});
|
||||
|
||||
it("allows object deletion", function () {
|
||||
// First do a read to populate rev tags...
|
||||
mockHttp.andReturn(mockPromise({
|
||||
data: { "_id": "abc", "_rev": "xyz", "model": {} }
|
||||
}));
|
||||
provider.readObject("testSpace", "abc");
|
||||
|
||||
// Now perform an update
|
||||
mockHttp.andReturn(mockPromise({
|
||||
data: { "_id": "abc", "_rev": "uvw", "ok": true }
|
||||
}));
|
||||
provider.deleteObject("testSpace", "abc", {}).then(capture);
|
||||
expect(mockHttp).toHaveBeenCalledWith({
|
||||
url: "/test/db/abc",
|
||||
method: "PUT",
|
||||
data: {
|
||||
"_id": "abc",
|
||||
"_rev": "xyz",
|
||||
"_deleted": true,
|
||||
metadata: jasmine.any(Object),
|
||||
model: {}
|
||||
}
|
||||
});
|
||||
expect(capture).toHaveBeenCalledWith(true);
|
||||
});
|
||||
|
||||
it("reports failure to create objects", function () {
|
||||
var model = { someKey: "some value" };
|
||||
mockHttp.andReturn(mockPromise({
|
||||
data: { "_id": "abc", "_rev": "xyz", "ok": false }
|
||||
}));
|
||||
provider.createObject("testSpace", "abc", model).then(capture);
|
||||
expect(capture).toHaveBeenCalledWith(false);
|
||||
});
|
||||
|
||||
it("returns undefined when objects are not found", function () {
|
||||
// Act like a 404
|
||||
mockHttp.andReturn({
|
||||
then: function (success, fail) {
|
||||
return mockPromise(fail());
|
||||
}
|
||||
});
|
||||
provider.readObject("testSpace", "abc").then(capture);
|
||||
expect(capture).toHaveBeenCalledWith(undefined);
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
);
|
Loading…
x
Reference in New Issue
Block a user