Changeset 1364

Show
Ignore:
Timestamp:
08/08/08 23:56:23 (5 months ago)
Author:
jtv
Message:

Converted test008 to test framework.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ChangeLog

    r1361 r1364  
     12008-08-09  Jeroen T. Vermeulen <jtv@xs4all.nl> 
     2 test/test_helpers.hxx, test085.cxx: 
     3  - Moved string_traits<result> into test helpers for reuse. 
     4 test/test008.cxx: 
     5  - Converted to test framework. 
    162008-08-04  Jeroen T. Vermeulen <jtv@xs4all.nl> 
    27 test/test004.cxx, test/test005.cxx, test/test006.cxx, test/test007.cxx: 
  • trunk/test/test008.cxx

    r1138 r1364  
    77#include <pqxx/transaction> 
    88 
     9#include "test_helpers.hxx" 
     10 
    911using namespace PGSTD; 
    1012using namespace pqxx; 
     
    1416// may be faster than a conventional query.  A tablereader is really a frontend 
    1517// for a PostgreSQL COPY TO stdout command. 
    16 // 
    17 // Usage: test008 [connect-string] [table] 
    18 // 
    19 // Where 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 "pqxxevents" as used by other test programs. 
    25 // PostgreSQL currently implements pg_tables as a view, which cannot be read by 
    26 // using the COPY command.  Otherwise, pg_tables would have made a better 
    27 // default value here. 
    28 int main(int argc, char *argv[]) 
     18 
     19namespace 
    2920{ 
    30   try 
     21void test_008(connection_base &, transaction_base &T) 
     22
     23  const string Table = "pqxxevents"; 
     24 
     25  vector<string> R, First; 
     26 
     27  // Set up a tablereader stream to read data from table pg_tables 
     28  tablereader Stream(T, Table); 
     29 
     30  // Read results into string vectors and print them 
     31  for (int n=0; (Stream >> R); ++n) 
    3132  { 
    32     // Set up a connection to the backend 
    33     connection C(argv[1])
     33    // Keep the first row for later consistency check 
     34    if (n == 0) First = R
    3435 
    35     string Table = "pqxxevents"; 
    36     if (argc > 2) Table = argv[2]; 
    37  
    38     // Begin a transaction acting on our current connection 
    39     work T(C, "test8"); 
    40  
    41     vector<string> R, First; 
    42  
    43     // Set up a tablereader stream to read data from table pg_tables 
    44     tablereader Stream(T, Table); 
    45  
    46     // Read results into string vectors and print them 
    47     for (int n=0; (Stream >> R); ++n) 
    48     { 
    49       // Keep the first row for later consistency check 
    50       if (n == 0) First = R; 
    51  
    52       cout << n << ":\t" << separated_list("\t",R.begin(),R.end()) << endl; 
    53       R.clear(); 
    54     } 
    55  
    56     Stream.complete(); 
    57  
    58     // Verify the contents we got for the first row 
    59     if (!First.empty()) 
    60     { 
    61       tablereader Verify(T, Table); 
    62       string Line; 
    63  
    64       if (!Verify.get_raw_line(Line)) 
    65         throw logic_error("tablereader got rows the first time around, " 
    66                           "but none the second time!"); 
    67  
    68       cout << "First tuple was: " << endl << Line << endl; 
    69  
    70       Verify.tokenize(Line, R); 
    71       if (R != First) 
    72         throw logic_error("Got different results re-parsing first tuple!"); 
    73     } 
    74   } 
    75   catch (const sql_error &e) 
    76   { 
    77     // If we're interested in the text of a failed query, we can write separate 
    78     // exception handling code for this type of exception 
    79     cerr << "SQL error: " << e.what() << endl 
    80          << "Query was: '" << e.query() << "'" << endl; 
    81     return 1; 
    82   } 
    83   catch (const exception &e) 
    84   { 
    85     // All exceptions thrown by libpqxx are derived from std::exception 
    86     cerr << "Exception: " << e.what() << endl; 
    87     return 2; 
    88   } 
    89   catch (...) 
    90   { 
    91     // This is really unexpected (see above) 
    92     cerr << "Unhandled exception" << endl; 
    93     return 100; 
     36    cout << n << ":\t" << separated_list("\t",R.begin(),R.end()) << endl; 
     37    R.clear(); 
    9438  } 
    9539 
    96   return 0; 
     40  Stream.complete(); 
     41 
     42  // Verify the contents we got for the first row 
     43  if (!First.empty()) 
     44  { 
     45    tablereader Verify(T, Table); 
     46    string Line; 
     47 
     48    const bool outcome(Verify.get_raw_line(Line)); 
     49    PQXX_CHECK( 
     50        outcome, 
     51        "tablereader got rows the first time around, but not the second time."); 
     52 
     53    cout << "First tuple was: " << endl << Line << endl; 
     54 
     55    Verify.tokenize(Line, R); 
     56    PQXX_CHECK_EQUAL(R, First, "Got different results re-parsing first tuple."); 
     57  } 
    9758} 
    9859 
     60} // namespace 
     61 
     62 
     63int main() 
     64{ 
     65  test::TestCase<> test008("test_008", test_008); 
     66  return test::pqxxtest(test008); 
     67} 
     68