1
0
Fork 0

Finished adding view support to ArangoShell.

This commit is contained in:
Dan Larkin 2017-03-21 11:40:14 -04:00
parent fd4f929825
commit 85132e0eaf
5 changed files with 83 additions and 24 deletions

View File

@ -25,7 +25,7 @@ describe ArangoDB do
doc.headers['content-type'].should eq("application/json; charset=utf-8")
doc.parsed_response['error'].should eq(true)
doc.parsed_response['code'].should eq(400)
doc.parsed_response['errorNum'].should eq(400)
doc.parsed_response['errorNum'].should eq(10)
end
it "creating a view without name" do
@ -40,7 +40,7 @@ describe ArangoDB do
doc.headers['content-type'].should eq("application/json; charset=utf-8")
doc.parsed_response['error'].should eq(true)
doc.parsed_response['code'].should eq(400)
doc.parsed_response['errorNum'].should eq(400)
doc.parsed_response['errorNum'].should eq(10)
end
it "creating a view without type" do
@ -55,7 +55,7 @@ describe ArangoDB do
doc.headers['content-type'].should eq("application/json; charset=utf-8")
doc.parsed_response['error'].should eq(true)
doc.parsed_response['code'].should eq(400)
doc.parsed_response['errorNum'].should eq(400)
doc.parsed_response['errorNum'].should eq(10)
end
it "creating a view with invalid type" do
@ -86,7 +86,7 @@ describe ArangoDB do
doc.headers['content-type'].should eq("application/json; charset=utf-8")
doc.parsed_response['error'].should eq(true)
doc.parsed_response['code'].should eq(400)
doc.parsed_response['errorNum'].should eq(400)
doc.parsed_response['errorNum'].should eq(10)
end
it "duplicate name" do
@ -180,7 +180,7 @@ describe ArangoDB do
doc.headers['content-type'].should eq("application/json; charset=utf-8")
doc.parsed_response['error'].should eq(true)
doc.parsed_response['code'].should eq(400)
doc.parsed_response['errorNum'].should eq(400)
doc.parsed_response['errorNum'].should eq(10)
end
it "modifying a non-existent view" do

View File

@ -80,7 +80,7 @@ void RestViewHandler::createView() {
std::vector<std::string> const& suffixes = _request->suffixes();
if (!suffixes.empty()) {
generateError(rest::ResponseCode::BAD, TRI_ERROR_HTTP_BAD_PARAMETER,
generateError(rest::ResponseCode::BAD, TRI_ERROR_BAD_PARAMETER,
"expecting POST /_api/view");
return;
}
@ -94,7 +94,7 @@ void RestViewHandler::createView() {
VPackSlice body = parsedBody.get()->slice();
auto badParamError = [&]() -> void {
generateError(rest::ResponseCode::BAD, TRI_ERROR_HTTP_BAD_PARAMETER,
generateError(rest::ResponseCode::BAD, TRI_ERROR_BAD_PARAMETER,
"expecting body to be of the form {name: <string>, type: "
"<string>, properties: <object>}");
};
@ -139,7 +139,7 @@ void RestViewHandler::modifyView() {
std::vector<std::string> const& suffixes = _request->suffixes();
if ((suffixes.size() != 2) || (suffixes[1] != "properties")) {
generateError(rest::ResponseCode::BAD, TRI_ERROR_HTTP_BAD_PARAMETER,
generateError(rest::ResponseCode::BAD, TRI_ERROR_BAD_PARAMETER,
"expecting PUT /_api/view/<view-name>/properties");
return;
}
@ -197,7 +197,7 @@ void RestViewHandler::deleteView() {
std::vector<std::string> const& suffixes = _request->suffixes();
if (suffixes.size() != 1) {
generateError(rest::ResponseCode::BAD, TRI_ERROR_HTTP_BAD_PARAMETER,
generateError(rest::ResponseCode::BAD, TRI_ERROR_BAD_PARAMETER,
"expecting DELETE /_api/view/<view-name>");
return;
}
@ -226,7 +226,7 @@ void RestViewHandler::getViews() {
if (suffixes.size() > 2 ||
((suffixes.size() == 2) && (suffixes[1] != "properties"))) {
generateError(rest::ResponseCode::BAD, TRI_ERROR_HTTP_BAD_PARAMETER,
generateError(rest::ResponseCode::BAD, TRI_ERROR_BAD_PARAMETER,
"expecting GET /_api/view[/<view-name>[/properties]]");
return;
}

View File

@ -49,10 +49,15 @@ function ArangoDatabase (connection) {
this[name] = obj;
};
this._views = {};
this._viewList = {};
this._registerView = function (name, obj) {
// store the view in our own list
this._views[name] = obj;
this._viewList[name] = obj;
};
this._unregisterView = function(name) {
if (this._viewList[name] !== undefined) {
delete this._viewList[name];
}
};
}
@ -1096,7 +1101,7 @@ ArangoDatabase.prototype._createView = function (name, type, properties) {
if (nname !== undefined) {
this._registerView(nname, new this._viewConstructor(this, requestResult));
return this[nname];
return this._viewList[nname];
}
return undefined;
@ -1111,9 +1116,9 @@ ArangoDatabase.prototype._views = function () {
arangosh.checkRequestResult(requestResult);
if (requestResult.result !== undefined) {
var views = requestResult.result;
var result = [];
var result = [];
if (requestResult !== undefined) {
var views = requestResult;
var i;
// add all views to object
@ -1123,8 +1128,8 @@ ArangoDatabase.prototype._views = function () {
result.push(view);
}
return result.sort(function (l, r) {
// we assume no two collections have the same name
result = result.sort(function (l, r) {
// we assume no two views have the same name
if (l.name().toLowerCase() < r.name().toLowerCase()) {
return -1;
}
@ -1132,7 +1137,7 @@ ArangoDatabase.prototype._views = function () {
});
}
return undefined;
return result;
};
// //////////////////////////////////////////////////////////////////////////////
@ -1140,8 +1145,9 @@ ArangoDatabase.prototype._views = function () {
// //////////////////////////////////////////////////////////////////////////////
ArangoDatabase.prototype._view = function (id) {
if (this._views[id] && this._views[id] instanceof this._viewConstructor) {
return this._views[id];
if (this._viewList[id] && this._viewList[id] instanceof
this._viewConstructor) {
return this._viewList[id];
}
var url = this._viewurl(id);
var requestResult = this._connection.GET(url);
@ -1160,7 +1166,7 @@ ArangoDatabase.prototype._view = function (id) {
if (name !== undefined) {
this._registerView(name, new this._viewConstructor(this, requestResult));
return this[name];
return this._viewList[name];
}
return null;

View File

@ -184,7 +184,7 @@ ArangoView.prototype.properties = function (properties) {
requestResult = this._database._connection.GET(this._baseurl('properties'));
arangosh.checkRequestResult(requestResult);
}else {
} else {
var body = properties;
requestResult = this._database._connection.PUT(this._baseurl('properties'),
JSON.stringify(body));
@ -207,5 +207,7 @@ ArangoView.prototype.drop = function () {
&& requestResult.errorNum !== internal.errors.ERROR_ARANGO_VIEW_NOT_FOUND.code) {
// check error in case we got anything else but "view not found"
arangosh.checkRequestResult(requestResult);
} else {
this._database._unregisterView(this._name);
}
};

View File

@ -125,11 +125,14 @@ function ViewSuite () {
////////////////////////////////////////////////////////////////////////////
testErrorHandlingModifyUnacceptable : function () {
var abc = db._createView("abc", "logger", {});
assertEqual(abc.name(), "abc");
try {
abc.properties({'bogus': 'junk'});
abc.properties({"bogus": "junk"});
fail();
}
catch (err) {
assertEqual(10, err.errorNum);
assertEqual(ERRORS.ERROR_BAD_PARAMETER.code, 10);
assertEqual(ERRORS.ERROR_BAD_PARAMETER.code, err.errorNum);
abc.drop();
}
@ -167,6 +170,54 @@ function ViewSuite () {
}
},
////////////////////////////////////////////////////////////////////////////
/// @brief retrieve empty list of views
////////////////////////////////////////////////////////////////////////////
testEmptyList : function () {
var views = db._views();
assertTrue(Array.isArray(views));
assertEqual(views.length, 0);
},
////////////////////////////////////////////////////////////////////////////
/// @brief retrieve short list of views
////////////////////////////////////////////////////////////////////////////
testShortList : function () {
var abc = db._createView("abc", "logger", {"level": "WARN"});
assertEqual(abc.name(), "abc");
var def = db._createView("def", "logger", {"level": "DEBUG"});
assertEqual(def.name(), "def");
var views = db._views();
assertTrue(Array.isArray(views));
assertEqual(views.length, 2);
assertEqual(abc.name(), views[0].name());
assertEqual(def.name(), views[1].name());
abc.drop();
def.drop();
},
////////////////////////////////////////////////////////////////////////////
/// @brief modify properties
////////////////////////////////////////////////////////////////////////////
testModifyProperties : function () {
var abc = db._createView("abc", "logger", {"level": "WARN"});
var props = abc.properties();
assertEqual(abc.name(), "abc");
assertEqual(abc.type(), "logger");
assertEqual(props.level, "WARN");
abc.properties({"level": "TRACE"});
abc = db._view("abc");
props = abc.properties();
assertEqual(abc.name(), "abc");
assertEqual(abc.type(), "logger");
assertEqual(props.level, "TRACE");
abc.drop();
}
};
}