Commit 75e5f35f authored by Matthieu Dorier's avatar Matthieu Dorier
Browse files

added execute test

parent f3890b72
......@@ -8,7 +8,7 @@ namespace sonata {
class Exception : public std::exception {
const std::string& m_error;
std::string m_error;
public:
......
......@@ -5,6 +5,7 @@
#include <type_traits>
#include <string>
#include <spdlog/spdlog.h>
namespace sonata {
......@@ -74,17 +75,17 @@ class UnQLiteVM {
}
void parse_and_throw_error() {
const char *errorBuffer;
int len;
const char *errorBuffer = nullptr;
int len = 0;
unqlite_config(m_db, UNQLITE_CONFIG_JX9_ERR_LOG, &errorBuffer, &len);
std::string error = "UnQLite error: "s;
if(len > 0) {
error += errorBuffer;
error += std::string(errorBuffer, len);
throw Exception(error);
}
unqlite_config(m_db, UNQLITE_CONFIG_ERR_LOG, &errorBuffer, &len);
if(len > 0) {
error += errorBuffer;
error += std::string(errorBuffer, len);
throw Exception(error);
}
error += "(unknown)";
......
......@@ -17,7 +17,11 @@ target_link_libraries(DatabaseTest sonata-test)
add_executable(CollectionTest CollectionTest.cpp)
target_link_libraries(CollectionTest sonata-test)
add_executable(ExecTest ExecTest.cpp)
target_link_libraries(ExecTest sonata-test)
add_test(NAME AdminTest COMMAND ./AdminTest AdminTest.xml)
add_test(NAME ClientTest COMMAND ./ClientTest ClientTest.xml)
add_test(NAME DatabaseTest COMMAND ./DatabaseTest DatabaseText.xml)
add_test(NAME CollectionTest COMMAND ./CollectionTest CollectionTest.xml)
add_test(NAME ExecTest COMMAND ./ExecTest ExecTest.xml)
#include <cppunit/extensions/HelperMacros.h>
#include <sonata/Client.hpp>
#include <sonata/Admin.hpp>
#include <spdlog/spdlog.h>
extern thallium::engine* engine;
class ExecTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( ExecTest );
CPPUNIT_TEST( testExec );
CPPUNIT_TEST_SUITE_END();
static constexpr const char* db_config = "{ \"path\" : \"mydb\" }";
public:
void setUp() {
sonata::Admin admin(*engine);
std::string addr = engine->self();
admin.createDatabase(addr, 0, "mydb", "unqlite", db_config);
}
void tearDown() {
sonata::Admin admin(*engine);
std::string addr = engine->self();
admin.destroyDatabase(addr, 0, "mydb");
}
void testExec() {
sonata::Client client(*engine);
std::string addr = engine->self();
sonata::Database mydb = client.open(addr, 0, "mydb");
std::string code =
"$zCol = 'users';"
"if( db_exists($zCol) ){"
"print \"Collection users already created\\n\";"
"}else{"
"$rc = db_create($zCol);"
"if ( !$rc ){"
"return;"
"}"
"print \"Collection users successfully created\\n\";"
"}";
std::unordered_set<std::string> vars;
std::unordered_map<std::string,std::string> results;
vars.insert("rc");
CPPUNIT_ASSERT_NO_THROW_MESSAGE(
"this call of execute should not throw.",
mydb.execute(code, vars, &results));
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"rc should be true.",
std::string("true"), results["rc"]);
// Assert that the collection the code above created is accessible
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"collection users should exist.",
true, mydb.exists("users"));
// Test with an erroneous code
code = "sdasd{}[2";
CPPUNIT_ASSERT_THROW_MESSAGE(
"this call of execute should throw.",
mydb.execute(code, vars, &results),
sonata::Exception);
}
};
CPPUNIT_TEST_SUITE_REGISTRATION( ExecTest );
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