Skip to content

Commit

Permalink
SERVER-15574 Exception in benchrun when a #VARIABLE is used without a…
Browse files Browse the repository at this point in the history
… corrosponding let op

Closes #824

Signed-off-by: Benety Goh <benety@mongodb.com>
  • Loading branch information
Alvin Richards authored and benety committed Oct 10, 2014
1 parent 9743c37 commit 3e4e3d5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/mongo/scripting/bson_template_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,14 @@ namespace mongo {
const BSONObj& in,
BSONObjBuilder& out) {
// in = { #VARIABLE: "x" }
BSONElement varEle = btl->_varMap[in["#VARIABLE"].String()].firstElement();;
out.appendAs(varEle, fieldName);
return StatusSuccess;
BSONElement varEle = btl->_varMap[in["#VARIABLE"].String()].firstElement();
if ( !varEle.eoo() ) {
out.appendAs(varEle, fieldName);
return StatusSuccess;
}
else {
return StatusOpEvaluationError;
}
}

} // end namespace mongo
25 changes: 25 additions & 0 deletions src/mongo/scripting/bson_template_evaluator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,31 @@ namespace mongo {
ASSERT_EQUALS(obj2.firstElement().str().length(), 19U);
}

// Test #VARIABLE
TEST(BSONTemplateEvaluatorTest, VARIABLE) {
BsonTemplateEvaluator *t = new BsonTemplateEvaluator();
int value1;

// Test failure when the variable has not been set
// {id: { #VARIABLE: "foo" } }
BSONObjBuilder builder1;
BSONObj innerObj = BSON( "#VARIABLE" << "foo" );
ASSERT_EQUALS( BsonTemplateEvaluator::StatusOpEvaluationError,
t->evaluate(BSON("id" << innerObj), builder1) );

// Test success when the variable has been set
// test2 := 42
// {id: { #VARIABLE: "test2" } }
t->setVariable("test2", BSON( "test2" << 42 ).getField("test2") );
BSONObjBuilder builder2;
innerObj = BSON( "#VARIABLE" << "test2" );
ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess,
t->evaluate(BSON("id" << innerObj), builder2) );
BSONObj obj2 = builder2.obj();
value1 = obj2["id"].numberInt();
ASSERT_EQUALS(value1, 42);
}

// Test template recursion and other general features
TEST(BSONTemplateEvaluatorTest, NESTING) {

Expand Down

0 comments on commit 3e4e3d5

Please sign in to comment.