| 21 | #include <iostream> |
| 22 | |
| 23 | int TestPostgreSQLDatabase(int /*argc*/, char* /*argv*/[]) |
| 24 | { |
| 25 | // This test requires the user in VTK_PSQL_TEST_URL to have |
| 26 | // permission to create and drop the database named in that URL |
| 27 | // as well as tables in that database. That user must also be |
| 28 | // able to connect to the "template1" database (which initdb |
| 29 | // creates and should be present on all systems -- we do NOT |
| 30 | // support non-standard configurations where this is not true). |
| 31 | vtkPostgreSQLDatabase* db = |
| 32 | vtkPostgreSQLDatabase::SafeDownCast(vtkSQLDatabase::CreateFromURL(VTK_PSQL_TEST_URL)); |
| 33 | std::string realDatabase = db->GetDatabaseName(); |
| 34 | db->SetDatabaseName("template1"); // This is guaranteed to exist |
| 35 | bool status = db->Open(); |
| 36 | if (!status) |
| 37 | { |
| 38 | std::cerr << "Couldn't open database.\nError message: \"" << db->GetLastErrorText() << "\"\n"; |
| 39 | db->Delete(); |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | vtkStringArray* dbNames = db->GetDatabases(); |
| 44 | std::cout << "Database list:\n"; |
| 45 | for (int dbi = 0; dbi < dbNames->GetNumberOfValues(); ++dbi) |
| 46 | { |
| 47 | std::cout << "+ " << dbNames->GetValue(dbi) << std::endl; |
| 48 | } |
| 49 | dbNames->Delete(); |
| 50 | if (!db->CreateDatabase(realDatabase.c_str(), true)) |
| 51 | { |
| 52 | std::cerr << "Error: " << db->GetLastErrorText() << std::endl; |
| 53 | } |
| 54 | |
| 55 | vtkSQLQuery* query = db->GetQueryInstance(); |
| 56 | |
| 57 | // Force a database connection close |
| 58 | // This also forces us to connect to the database named in the test URL. |
| 59 | std::string fauxDatabase = realDatabase + "blarney"; |
| 60 | db->SetDatabaseName(fauxDatabase.c_str()); |
| 61 | db->SetDatabaseName(realDatabase.c_str()); |
| 62 | |
| 63 | if (!db->Open()) |
| 64 | { |
| 65 | std::cerr << "Error: " << db->GetLastErrorText() << std::endl; |
| 66 | } |
| 67 | |
| 68 | // Test that bad queries fail without segfaulting... |
| 69 | std::string dropQuery("DROP TABLE people"); |
| 70 | std::cout << dropQuery << std::endl; |
| 71 | query->SetQuery(dropQuery.c_str()); |
| 72 | if (!query->Execute()) |
| 73 | { |
| 74 | std::cout << "Drop query did not succeed (this result *** was *** expected). The last message: " |
| 75 | << std::endl; |
| 76 | std::cout << " " << query->GetLastErrorText() << std::endl; |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | std::cerr << "The query \"DROP TABLE people\" succeeded when it should not have.\n"; |
nothing calls this directly
no test coverage detected