Skip to content

Commit

Permalink
Tighten the checks in get_affected_rows() unit tests
Browse files Browse the repository at this point in the history
Check that a partial update does throw an exception instead of checking
that it may throw it and check that get_affected_rows() returns the
actual number of affected rows instead of returning something non zero.

Still account for the possibility of the number of affected rows
possibly being zero as at least the PostgreSQL ODBC driver behaves like
this, even if all the other tested configurations return 1.
  • Loading branch information
vadz committed Jan 28, 2025
1 parent 92632ff commit e4b8a7d
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions tests/common/test-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3260,25 +3260,26 @@ TEST_CASE_METHOD(common_tests, "Get affected rows", "[core][affected-rows]")
w[1] = "a"; // this invalid value may cause an exception.
statement st6 = (sql.prepare <<
"insert into soci_test(val) values(:val)", use(w));
try { st6.execute(true); }
catch(...) {}
CHECK_THROWS_AS(st6.execute(true), soci_error);

// confirm the partial insertion.
int val = 0;
sql << "select count(val) from soci_test", into(val);
if(val != 0)
{
// Notice that some ODBC drivers don't return the number of updated
// rows at all in the case of partially executed statement like this
// one, while MySQL ODBC driver wrongly returns 2 affected rows even
// though only one was actually inserted.
//
// So we can't check for "get_affected_rows() == val" here, it would
// fail in too many cases -- just check that the backend doesn't lie to
// us about no rows being affected at all (even if it just honestly
// admits that it has no idea by returning -1).
CHECK(st6.get_affected_rows() != 0);
// This is a fatal error for ODBC backend with PostgreSQL driver, see
// https://github.com/postgresql-interfaces/psqlodbc/issues/89, and while
// this might be a bug, it doesn't seem totally unreasonable, so allow
// either this behaviour or partial success here.
const int affected_rows = st6.get_affected_rows();
if ( affected_rows == 0 )
{
WARN("Database doesn't return partial success for bulk operations.");
}
else
{
CHECK(affected_rows == 1);
}

// Check that the number of actually inserted rows is the same.
int val = -1;
sql << "select count(val) from soci_test", into(val);
CHECK(val == affected_rows);
}

// test fix for: Backend is not set properly with connection pool (pull #5)
Expand Down

0 comments on commit e4b8a7d

Please sign in to comment.