Changeset 1367

Show
Ignore:
Timestamp:
08/09/08 00:32:11 (5 months ago)
Author:
jtv
Message:

Converted test 009 to test framework.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ChangeLog

    r1366 r1367  
    66 test/test_helpers.hxx, test085.cxx: 
    77  - Moved string_traits<result> into test helpers for reuse. 
    8  test/test008.cxx
     8 test/test008.cxx, test/test009.cxx
    99  - Converted to test framework. 
    10102008-08-04  Jeroen T. Vermeulen <jtv@xs4all.nl> 
  • trunk/test/test009.cxx

    r1138 r1367  
    88#include <pqxx/transaction> 
    99 
     10#include "test_helpers.hxx" 
     11 
    1012using namespace PGSTD; 
    1113using namespace pqxx; 
     
    1416// Test program for libpqxx.  Create a table and write data to it, using 
    1517// tablewriter's back_insert_iterator. 
    16 // 
    17 // Usage: test009 [connect-string] [table] [column] 
    18 // 
    19 // Where the connect-string is a set of connection options in Postgresql's 
    20 // PQconnectdb() format, eg. "dbname=template1" to select from a database 
    21 // called template1, or "host=foo.bar.net user=smith" to connect to a backend 
    22 // running on host foo.bar.net, logging in as user smith. 
    23 // 
    24 // The default table name is "testtable." 
    2518 
    2619namespace 
     
    7164  size_t Rows = 0; 
    7265 
    73   if (!Count[0][0].to(Rows)) throw runtime_error("NULL row count!"); 
     66  const bool have_rowcount = Count[0][0].to(Rows); 
     67  PQXX_CHECK(have_rowcount, "NULL row count."); 
    7468  cout << Rows << " rows in table." << endl; 
    7569 
    76   if (Rows != Contents.size()) 
    77     throw runtime_error("Found " + 
    78                         string(Count[0][0].c_str()) + 
    79                         " rows in table--after writing " + 
    80                         to_string(Contents.size()) + 
    81                         "!"); 
    82  
     70  PQXX_CHECK_EQUAL( 
     71        Rows, 
     72        Contents.size(), 
     73        "Number of rows in table is not what I wrote."); 
    8374  // TODO: Compare table contents to Contents 
    8475} 
     76 
     77void test_009(connection_base &, transaction_base &T) 
     78{ 
     79  PrepareContents(); 
     80 
     81  // Select our original and destination table names 
     82  const string TableName = "pqxxtesttable"; 
     83 
     84  const string Column = "content"; 
     85 
     86  // Create table.  If the table already existed, better to fail now. 
     87  stringstream ctq; 
     88  ctq << "CREATE TABLE " << TableName << '(' << Column << " VARCHAR)"; 
     89  T.exec(ctq); 
     90 
     91  FillTable(T, TableName, Column); 
     92  CheckTable(T, TableName); 
     93 
     94  T.exec(("DROP TABLE " + TableName).c_str()); 
     95  T.commit(); 
    8596} 
    8697 
    87 int main(int argc, char *argv[]) 
     98} // namespace 
     99 
     100 
     101int main() 
    88102{ 
    89   try 
    90   { 
    91     const char *ConnStr = argv[1]; 
    92  
    93     PrepareContents(); 
    94  
    95     // Set up two connections to the backend: one to read our original table, 
    96     // and another to write our copy 
    97     connection C(ConnStr); 
    98  
    99     // Select our original and destination table names 
    100     string TableName = "pqxxtesttable"; 
    101     if (argc > 2) TableName = argv[2]; 
    102  
    103     string Column = "content"; 
    104     if (argc > 3) Column = argv[3]; 
    105  
    106     work T(C, "test9"); 
    107  
    108     // Create table.  If the table already existed, better to fail now. 
    109     stringstream ctq; 
    110     ctq << "CREATE TABLE " << TableName << '(' << Column << " VARCHAR)"; 
    111     T.exec(ctq); 
    112  
    113     FillTable(T, TableName, Column); 
    114     CheckTable(T, TableName); 
    115  
    116     T.exec(("DROP TABLE " + TableName).c_str()); 
    117     T.commit(); 
    118   } 
    119   catch (const sql_error &e) 
    120   { 
    121     // If we're interested in the text of a failed query, we can write separate 
    122     // exception handling code for this type of exception 
    123     cerr << "SQL error: " << e.what() << endl 
    124          << "Query was: '" << e.query() << "'" << endl; 
    125     return 1; 
    126   } 
    127   catch (const exception &e) 
    128   { 
    129     // All exceptions thrown by libpqxx are derived from std::exception 
    130     cerr << "Exception: " << e.what() << endl; 
    131     return 2; 
    132   } 
    133   catch (...) 
    134   { 
    135     // This is really unexpected (see above) 
    136     cerr << "Unhandled exception" << endl; 
    137     return 100; 
    138   } 
    139  
    140   return 0; 
     103  test::TestCase<> test009("test_009", test_009); 
     104  return test::pqxxtest(test009); 
    141105} 
    142106