From 55ffd09abee8d5b164499d04f16e7d59fb2efe78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Tue, 1 Jun 2010 18:29:53 +0200 Subject: [PATCH] feature #192: More tests for TemplateSQL, added also support for testing MySQL. --- src/template/test/template_sql.cc | 356 ++++++++++++++++++++++++++---- 1 file changed, 319 insertions(+), 37 deletions(-) diff --git a/src/template/test/template_sql.cc b/src/template/test/template_sql.cc index 4947360a7b..4083a4f055 100644 --- a/src/template/test/template_sql.cc +++ b/src/template/test/template_sql.cc @@ -1,17 +1,20 @@ #include "TemplateSQL.h" #include "SqliteDB.h" #include "SqlDB.h" +#include "MySqlDB.h" #include "Log.h" #include #include #include +#include #include #include #include #include #include +#include using namespace std; @@ -46,6 +49,8 @@ public: {return TemplateSQL::replace_attribute(db,attr);} int insert_attribute(SqlDB * db, Attribute * attr) {return TemplateSQL::insert_attribute(db,attr);} + int remove_attribute(SqlDB * db, const string& name) + {return TemplateSQL::remove_attribute(db,name);}; /* --------------------------------------------------------------------- */ int id(){return TemplateSQL::id;}; }; @@ -56,31 +61,37 @@ public: class TemplateSQLTest : public CppUnit::TestFixture { + + CPPUNIT_TEST_SUITE (TemplateSQLTest); + + CPPUNIT_TEST (test_insert); + CPPUNIT_TEST (test_update); + CPPUNIT_TEST (test_select); + CPPUNIT_TEST (test_drop); + CPPUNIT_TEST (test_replace_attribute); + CPPUNIT_TEST (test_insert_attribute); + CPPUNIT_TEST (test_remove_attribute); + + CPPUNIT_TEST_SUITE_END (); + private: SqlDB *db; - string filename; + string db_name; string template_ok; string template_xml; - static void log( - const char * module, - const Log::MessageType type, - const ostringstream& message, - const char * filename = 0, - Log::MessageType clevel = Log::ERROR) - { - cerr << message.str() << endl; - }; - public: + // Global flag to use either Sqlite or MySQL + static bool mysql; + /* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */ TemplateSQLTest() { - filename = "template_sql.db"; + db_name = "ONE_test_database"; template_ok = "#This line is a comment\n" @@ -110,17 +121,51 @@ public: void setUp() { - ostringstream db_bs("CREATE TABLE template (id INTEGER, name TEXT," - " type INTEGER, value TEXT)"); - - db = new SqliteDB(filename); - CPPUNIT_ASSERT(db->exec(db_bs)== 0); + if (mysql) + { + db = new MySqlDB("localhost","oneadmin","onepass",NULL); + + ostringstream oss1; + oss1 << "DROP DATABASE IF EXISTS " << db_name; + db->exec(oss1); + + ostringstream oss; + oss << "CREATE DATABASE " << db_name; + db->exec(oss); + + ostringstream oss2; + oss2 << "use " << db_name; + db->exec(oss2); + } + else + { + unlink(db_name.c_str()); + + db = new SqliteDB(db_name); + } + + ostringstream db_bs( + "CREATE TABLE IF NOT EXISTS template (id INTEGER, " + "name TEXT, type INTEGER, value TEXT)"); + + CPPUNIT_ASSERT(db->exec(db_bs) == 0); } void tearDown() { - delete db; - unlink(filename.c_str()); + if (mysql) + { + ostringstream oss; + oss << "DROP DATABASE IF EXISTS " << db_name; + db->exec(oss); + } + else + { + unlink(db_name.c_str()); + } + + if( db != 0 ) + delete db; } /* ********************************************************************* */ @@ -155,6 +200,77 @@ public: /* --------------------------------------------------------------------- */ + void test_update() + { + + // Update only makes sense with a table with primary keys + //----------------------------------------------- // + tearDown(); + + + if (mysql) + { + db = new MySqlDB("localhost","oneadmin","onepass",NULL); + + ostringstream oss1; + oss1 << "DROP DATABASE IF EXISTS " << db_name; + db->exec(oss1); + + ostringstream oss; + oss << "CREATE DATABASE " << db_name; + db->exec(oss); + + ostringstream oss2; + oss2 << "use " << db_name; + db->exec(oss2); + } + else + { + unlink(db_name.c_str()); + + db = new SqliteDB(db_name); + } + + ostringstream db_bs("CREATE TABLE IF NOT EXISTS template (id INTEGER, " + "name VARCHAR(256), type INTEGER, value TEXT, " + "PRIMARY KEY(id,name))"); + + CPPUNIT_ASSERT(db->exec(db_bs) == 0); + + //----------------------------------------------- // + + int rc; + string att_name = "NEW_ATT"; + string value = ""; + + + TSQL t ("template",0); + TSQL t2("template",0); + + // Insert template t into the DB + t.insert(db); + + // Add a new vector attribute to t, and insert it into the DB + SingleAttribute * s_att = new SingleAttribute(att_name, "some value"); + t.insert_attribute(db, ((Attribute*)s_att)); + + + // Now replace the attribute's value + s_att->replace("a new value"); + + // And update the DB + t.update(db); + + // Read from the DB to t2 + rc = t2.select(db); + CPPUNIT_ASSERT( rc == 0 ); + + t2.get(att_name, value); + CPPUNIT_ASSERT( value == "a new value" ); + } + + /* --------------------------------------------------------------------- */ + void test_select() { char * error = 0; @@ -179,39 +295,205 @@ public: CPPUNIT_ASSERT( t2_xml == template_xml ); } - /* ********************************************************************* */ - /* ********************************************************************* */ + /* --------------------------------------------------------------------- */ - static CppUnit::TestSuite * suite() + void test_drop() { - CppUnit::TestSuite *ts=new CppUnit::TestSuite("TemplateSQL Tests"); - - ts->addTest(new CppUnit::TestCaller( - "bootstrap() Test", - &TemplateSQLTest::test_insert)); - - ts->addTest(new CppUnit::TestCaller( - "insert() Test", - &TemplateSQLTest::test_insert)); + char * error = 0; + string str = ""; - ts->addTest(new CppUnit::TestCaller( - "select() Test", - &TemplateSQLTest::test_select)); + TSQL t ("template",0); + TSQL t2("template",0); - return ts; + t.parse(template_ok,&error); + t.insert(db); + + if ( error != 0 ) + { + free(error); + } + + // Drop the template from the DB + t.drop(db); + + // Try to read it from the DB + CPPUNIT_ASSERT( t2.select(db) == 0 ); + // It should be empty + CPPUNIT_ASSERT( t2.to_str(str) == "" ); + } + + /* --------------------------------------------------------------------- */ + + void test_replace_attribute() + { + int rc; + string att_name = "NEW_ATT"; + string value = ""; + + + TSQL t ("template",0); + TSQL t2("template",0); + + // Add a new vector attribute to t, and insert it into the DB + VectorAttribute * v_att = new VectorAttribute(att_name); + v_att->replace("A", "A value"); + v_att->replace("B", "B value"); + v_att->replace("C", "C value"); + + t.insert(db); + t.insert_attribute(db, ((Attribute*)v_att)); + + + + // Now replace its value, with a single value attribute + SingleAttribute * s_att = new SingleAttribute(att_name, "some value"); + + t.replace_attribute(db, ((Attribute*)s_att) ); + + + // Read from the DB to t2 + rc = t2.select(db); + CPPUNIT_ASSERT( rc == 0 ); + + t2.get(att_name, value); + CPPUNIT_ASSERT( value == "some value" ); + } + + /* --------------------------------------------------------------------- */ + + void test_insert_attribute() + { + Attribute * att; + int rc; + string att_name = "NEW_ATT"; + string value = ""; + + + TSQL t ("template",0); + TSQL t2("template",0); + + // Add a new attribute to t, and insert it into the DB + att = new SingleAttribute(att_name, "some value"); + + t.insert(db); + t.insert_attribute(db, att); + + // Read from the DB to t2 + rc = t2.select(db); + CPPUNIT_ASSERT( rc == 0 ); + + t2.get(att_name, value); + CPPUNIT_ASSERT( value == "some value" ); + } + + /* --------------------------------------------------------------------- */ + + void test_remove_attribute() + { + Attribute * att; + int rc; + string att_name = "NEW_ATT"; + string value = ""; + + + TSQL t ("template",0); + TSQL t2("template",0); + + // Add a new attribute to t, and insert it into the DB + att = new SingleAttribute(att_name, "some value"); + + t.insert(db); + t.insert_attribute(db, att); + + t.remove_attribute(db, att_name); + + // Read from the DB to t2 + rc = t2.select(db); + CPPUNIT_ASSERT( rc == 0 ); + + t2.get(att_name, value); + CPPUNIT_ASSERT( value == "" ); } }; +/* ************************************************************************* */ + +bool TemplateSQLTest::mysql; + /* ************************************************************************* */ /* ************************************************************************* */ /* ************************************************************************* */ +static void show_options () +{ + cout << "Options:\n"; + cout << " -h --help Show this help\n" + " -s --sqlite Run Sqlite tests (default)\n" + " -m --mysql Run MySQL tests\n" + " -l --log Keep the log file, test.log\n"; +} + int main(int argc, char ** argv) { + + // Option flags + bool sqlite_flag = true; + bool log_flag = false; + + // Long options + const struct option long_opt[] = + { + { "sqlite", 0, NULL, 's'}, + { "mysql", 0, NULL, 'm'}, + { "log", 0, NULL, 'l'}, + { "help", 0, NULL, 'h'} + }; + + int c; + while ((c = getopt_long (argc, argv, "smlh", long_opt, NULL)) != -1) + switch (c) + { + case 'm': + sqlite_flag = false; + break; + case 'l': + log_flag = true; + break; + case 'h': + show_options(); + return 0; + } + + CppUnit::TextUi::TestRunner tr; - + tr.addTest(TemplateSQLTest::suite()); + + NebulaLog::init_log_system(NebulaLog::FILE, Log::DEBUG, "test.log"); + NebulaLog::log("Test", Log::INFO, "Test started"); + + + if (sqlite_flag) + { + TemplateSQLTest::mysql = false; + NebulaLog::log("Test", Log::INFO, "Running Sqlite tests..."); + cout << "\nRunning Sqlite tests...\n"; + } + else + { + TemplateSQLTest::mysql = true; + NebulaLog::log("Test", Log::INFO, "Running MySQL tests..."); + cout << "\nRunning MySQL tests...\n"; + } + + tr.run(); - + + + if (!log_flag) + remove("test.log"); + + NebulaLog::finalize_log_system(); + return 0; }