Skip to content

Commit

Permalink
Add test checking string length when using bulk insert
Browse files Browse the repository at this point in the history
This used to be broken, so add a test to check that it works after the
changes of the last commit and to avoid breaking it again.
  • Loading branch information
vadz committed Jul 21, 2017
1 parent 51c4c1e commit c6aef12
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/common-tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ class test_context_base
// whatever we do.
virtual bool enable_std_char_padding(session&) const { return true; }

// Return the name of the function for determining the length of a string,
// i.e. "char_length" in standard SQL but often "len" or "length" in
// practice.
virtual std::string get_length_function_name() const = 0;

virtual ~test_context_base()
{
the_test_context_ = NULL;
Expand Down Expand Up @@ -4205,6 +4210,56 @@ TEST_CASE_METHOD(common_tests, "Select without table", "[core][select][dummy_fro
CHECK(plus17 == 17);
}

TEST_CASE_METHOD(common_tests, "String length", "[core][string][length]")
{
soci::session sql(backEndFactory_, connectString_);

auto_table_creator tableCreator(tc_.table_creator_1(sql));

std::string s("123");
sql << "insert into soci_test(str) values(:s)", use(s);

const std::string& len_func = tc_.get_length_function_name();

std::string sout;
size_t slen;
sql << "select str," + len_func + "(str)"
" from soci_test",
into(sout), into(slen);
CHECK(slen == 3);
CHECK(sout.length() == 3);
CHECK(sout == s);

sql << "delete from soci_test";


std::vector<std::string> v;
v.push_back("Hello");
v.push_back("");
v.push_back("whole of varchar(20)");

CHECK_NOTHROW( (sql << "insert into soci_test(str) values(:s)", use(v)) );

std::vector<std::string> vout(10);
std::vector<unsigned int> vlen(10);
sql << "select str," + len_func + "(str)"
" from soci_test"
" order by " + len_func + "(str)",
into(vout), into(vlen);

REQUIRE(vout.size() == 3);
REQUIRE(vlen.size() == 3);

CHECK(vlen[0] == 0);
CHECK(vout[0].length() == 0);

CHECK(vlen[1] == 5);
CHECK(vout[1].length() == 5);

CHECK(vlen[2] == 20);
CHECK(vout[2].length() == 20);
}

} // namespace test_cases

} // namespace tests
Expand Down
5 changes: 5 additions & 0 deletions tests/db2/test-db2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ class test_context :public test_context_base
{
return "to_date('" + pi_datdt_string + "', 'YYYY-MM-DD HH24:MI:SS')";
}

virtual std::string get_length_function_name() const
{
return "length";
}
};


Expand Down
5 changes: 5 additions & 0 deletions tests/firebird/test-firebird.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,11 @@ class test_context : public tests::test_context_base
{
sql.commit();
}

virtual std::string get_length_function_name() const
{
return "char_length";
}
};


Expand Down
5 changes: 5 additions & 0 deletions tests/mysql/test-mysql.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ class test_context : public test_context_base
return false;
}
}

virtual std::string get_length_function_name() const
{
return "char_length";
}
};

#endif // SOCI_TESTS_MYSQL_H_INCLUDED
5 changes: 5 additions & 0 deletions tests/odbc/test-odbc-access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ test_context(backend_factory const &backEnd, std::string const &connectString)
{
return "#" + datdt_string + "#";
}

virtual std::string get_length_function_name() const
{
return "len";
}
};

int main(int argc, char** argv)
Expand Down
5 changes: 5 additions & 0 deletions tests/odbc/test-odbc-db2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class test_context : public test_context_base
{
return "\'" + datdt_string + "\'";
}

virtual std::string get_length_function_name() const
{
return "length";
}
};

struct table_creator_bigint : table_creator_base
Expand Down
5 changes: 5 additions & 0 deletions tests/odbc/test-odbc-mssql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ class test_context : public test_context_base
// on the side of caution and suppose that it's not supported.
return true;
}

virtual std::string get_length_function_name() const
{
return "len";
}
};

int main(int argc, char** argv)
Expand Down
5 changes: 5 additions & 0 deletions tests/odbc/test-odbc-postgresql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ class test_context : public test_context_base
return !m_verDriver.is_initialized() || m_verDriver < odbc_version(9, 3, 400);
}

virtual std::string get_length_function_name() const
{
return "char_length";
}

private:
odbc_version get_driver_version() const
{
Expand Down
5 changes: 5 additions & 0 deletions tests/oracle/test-oracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,11 @@ class test_context :public test_context_base
{
return "to_date('" + datdt_string + "', 'YYYY-MM-DD HH24:MI:SS')";
}

virtual std::string get_length_function_name() const
{
return "length";
}
};

int main(int argc, char** argv)
Expand Down
5 changes: 5 additions & 0 deletions tests/postgresql/test-postgresql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,11 @@ class test_context : public test_context_base
{
return false;
}

virtual std::string get_length_function_name() const
{
return "char_length";
}
};

int main(int argc, char** argv)
Expand Down
5 changes: 5 additions & 0 deletions tests/sqlite3/test-sqlite3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ class test_context : public test_context_base
// SQLite does not support right padded char type.
return false;
}

virtual std::string get_length_function_name() const
{
return "length";
}
};

int main(int argc, char** argv)
Expand Down

0 comments on commit c6aef12

Please sign in to comment.