1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/* main() definition for libpqxx test runners.
*/
#include <iostream>
#include <list>
#include <new>
#include <stdexcept>
#include "test_helpers.hxx"
using namespace PGSTD;
using namespace pqxx;
using namespace pqxx::test;
namespace
{
// Does this backend have generate_series()?
bool have_generate_series(const connection_base &c)
{
return c.server_version() >= 80000;
}
string deref_field(const field &f)
{
return f.c_str();
}
} // namespace
namespace pqxx
{
namespace test
{
test_failure::test_failure(const string &ffile, int fline, const string &desc) :
logic_error(desc),
m_file(ffile),
m_line(fline)
{
}
test_failure::~test_failure() throw () {}
base_test::base_test(const string &tname, testfunc func) :
m_name(tname),
m_func(func)
{
register_test(this);
}
base_test::~base_test() {}
const test_map ®ister_test(base_test *tc)
{
static test_map tests;
if (tc) tests[tc->name()] = tc;
return tests;
}
void prepare_series(transaction_base &t, int lowest, int highest)
{
connection_base &conn = t.conn();
// Don't do this for nullconnections, so nullconnection tests can run.
if (conn.is_open() && !have_generate_series(conn))
{
t.exec("CREATE TEMP TABLE series(x integer)");
for (int x=lowest; x <= highest; ++x)
t.exec("INSERT INTO series(x) VALUES (" + to_string(x) + ")");
}
}
string select_series(connection_base &conn, int lowest, int highest)
{
if (have_generate_series(conn))
return
"SELECT generate_series(" +
to_string(lowest) + ", " + to_string(highest) + ")";
return
"SELECT x FROM series "
"WHERE "
"x >= " + to_string(lowest) + " AND "
"x <= " + to_string(highest) + " "
"ORDER BY x";
}
namespace
{
bool drop_table_if_exists(transaction_base &t, const PGSTD::string &table)
{
if (t.conn().server_version() < 80100) return false;
t.exec("DROP TABLE IF EXISTS " + table);
return true;
}
}
void drop_table(transaction_base &t, const PGSTD::string &table)
{
if (drop_table_if_exists(t, table)) return;
dbtransaction *dbt(dynamic_cast<dbtransaction *>(&t));
if (dbt)
{
subtransaction s(*dbt, "drop_table");
try
{
s.exec("DROP TABLE " + table);
}
catch (const sql_error &e)
{
PGSTD::cerr << e.what() << PGSTD::endl;
s.abort();
}
s.commit();
}
else
{
nontransaction *nt(dynamic_cast<nontransaction *>(&t));
try
{
nt->exec("DROP TABLE " + table);
}
catch (const sql_error &e)
{
PGSTD::cerr << e.what() << PGSTD::endl;
}
}
}
void PQXX_NORETURN check_notreached(const char file[], int line, string desc)
{
throw test_failure(file, line, desc);
}
void check(
const char file[],
int line,
bool condition,
const char text[],
string desc)
{
if (!condition)
throw test_failure(
file,
line,
desc + " (failed expression: " + text + ")");
}
void expected_exception(const string &message)
{
cout << "(Expected) " << message << endl;
}
string list_tuple(tuple Obj)
{
return separated_list(", ", Obj.begin(), Obj.end(), deref_field);
}
string list_result(result Obj)
{
if (Obj.empty()) return "<empty>";
return "{" + separated_list("}\n{", Obj) + "}";
}
string list_result_iterator(result::const_iterator Obj)
{
return "<iterator at " + to_string(Obj.rownumber()) + ">";
}
void create_pqxxevents(transaction_base &t)
{
t.exec(
"CREATE TEMP TABLE pqxxevents(year integer, event varchar) "
"ON COMMIT PRESERVE ROWS");
t.exec("INSERT INTO pqxxevents(year, event) VALUES (71, 'jtv')");
t.exec("INSERT INTO pqxxevents(year, event) VALUES (38, 'time_t overflow')");
t.exec(
"INSERT INTO pqxxevents(year, event) VALUES (1, '''911'' WTC attack')");
t.exec("INSERT INTO pqxxevents(year, event) VALUES (81, 'C:\\>')");
t.exec("INSERT INTO pqxxevents(year, event) VALUES (1978, 'bloody\t\tcold')");
t.exec("INSERT INTO pqxxevents(year, event) VALUES (99, '')");
t.exec("INSERT INTO pqxxevents(year, event) VALUES (2002, 'libpqxx')");
t.exec(
"INSERT INTO pqxxevents(year, event) "
"VALUES (1989, 'Ode an die Freiheit')");
t.exec("INSERT INTO pqxxevents(year, event) VALUES (2001, 'New millennium')");
t.exec("INSERT INTO pqxxevents(year, event) VALUES (1974, '')");
t.exec("INSERT INTO pqxxevents(year, event) VALUES (97, 'Asian crisis')");
t.exec(
"INSERT INTO pqxxevents(year, event) VALUES (2001, 'A Space Odyssey')");
}
} // namespace pqxx::test
} // namespace pqxx
int main(int, const char *argv[])
{
const char *const test_name = argv[1];
const test_map &tests = register_test(NULL);
int test_count = 0;
list<string> failed;
for (test_map::const_iterator i = tests.begin(); i != tests.end(); ++i)
if (!test_name || test_name == i->first)
{
cout << endl << "Running: " << i->first << endl;
bool success = false;
try
{
i->second->run();
success = true;
}
catch (const test_failure &e)
{
cerr << "Test failure in " + e.file() + " line " +
to_string(e.line()) << ": " << e.what() << endl;
}
catch (const bad_alloc &)
{
cerr << "Out of memory!" << endl;
}
catch (const feature_not_supported &e)
{
cerr << "Not testing unsupported feature: " << e.what() << endl;
success = true;
--test_count;
}
catch (const sql_error &e)
{
cerr << "SQL error: " << e.what() << endl
<< "Query was: " << e.query() << endl;
}
catch (const exception &e)
{
cerr << "Exception: " << e.what() << endl;
}
catch (...)
{
cerr << "Unknown exception" << endl;
}
if (!success)
{
cerr << "FAILED: " << i->first << endl;
failed.push_back(i->first);
}
++test_count;
}
cout << "Ran " << test_count << " test(s)." << endl;
if (!failed.empty())
{
cerr << "*** " << failed.size() << " test(s) failed: ***" << endl;
for (list<string>::const_iterator i=failed.begin(); i!=failed.end(); ++i)
cerr << "\t" << *i << endl;
}
return int(failed.size());
}
|