Skip to content

Commit f0bdf05

Browse files
authored
CPP-835 Port prepare from existing tests to Google test framework (#316)
1 parent b04eca2 commit f0bdf05

File tree

6 files changed

+130
-178
lines changed

6 files changed

+130
-178
lines changed

cpp-driver/gtests/src/integration/objects/session.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,21 @@ class Session : public Object<CassSession, cass_session_free> {
217217
return Prepared(cass_future_get_prepared(prepare_future.get()));
218218
}
219219

220+
/**
221+
* Create a prepared statement from an existing statement inheriting the existing statement's
222+
* settings.
223+
*
224+
* @param statement The existing statement
225+
* @param assert_ok True if error code for future should be asserted
226+
* CASS_OK; false otherwise (default: true)
227+
* @return Prepared statement generated by the server for the query
228+
*/
229+
Prepared prepare_from_existing(Statement statement, bool assert_ok = true) {
230+
Future prepare_future = cass_session_prepare_from_existing(get(), statement.get());
231+
prepare_future.wait(assert_ok);
232+
return Prepared(cass_future_get_prepared(prepare_future.get()));
233+
}
234+
220235
/**
221236
* Get a schema snapshot
222237
*

cpp-driver/gtests/src/integration/tests/test_prepared.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616

1717
#include "integration.hpp"
18+
#include "testing.hpp"
19+
20+
using namespace datastax::internal::testing;
1821

1922
/**
2023
* Prepared integration tests; common operations
@@ -108,3 +111,89 @@ CASSANDRA_INTEGRATION_TEST_F(PreparedTests, PreparedIDUnchangedDuringReprepare)
108111
EXPECT_EQ(CASS_OK, result.error_code());
109112
EXPECT_EQ(1u, logger_.count());
110113
}
114+
115+
/**
116+
* Verify that a statement is correctly prepared from an existing simple
117+
* statement. The settings from the original statement should be inherited.
118+
*
119+
* @since 2.8
120+
*/
121+
CASSANDRA_INTEGRATION_TEST_F(PreparedTests, PrepareFromExistingSimpleStatement) {
122+
CHECK_FAILURE;
123+
124+
use_keyspace(keyspace_name_);
125+
126+
session_.execute(
127+
format_string(CASSANDRA_KEY_VALUE_TABLE_FORMAT, table_name_.c_str(), "int", "int"));
128+
session_.execute(
129+
format_string(CASSANDRA_KEY_VALUE_INSERT_FORMAT, table_name_.c_str(), "1", "99"));
130+
131+
DowngradingConsistencyRetryPolicy retry_policy;
132+
Statement statement(format_string(CASSANDRA_SELECT_VALUE_FORMAT, table_name_.c_str(), "?"), 1);
133+
134+
// Set unique settings to validate later
135+
statement.set_consistency(CASS_CONSISTENCY_LOCAL_QUORUM);
136+
statement.set_serial_consistency(CASS_CONSISTENCY_SERIAL);
137+
statement.set_request_timeout(99999u);
138+
statement.set_retry_policy(retry_policy);
139+
140+
// Prepare from the existing bound statement
141+
Statement bound_statement = session_.prepare_from_existing(statement).bind();
142+
143+
// Validate that the bound statement inherited the settings from the original statement
144+
EXPECT_EQ(get_consistency(bound_statement.get()), CASS_CONSISTENCY_LOCAL_QUORUM);
145+
EXPECT_EQ(get_serial_consistency(bound_statement.get()), CASS_CONSISTENCY_SERIAL);
146+
EXPECT_EQ(get_request_timeout_ms(bound_statement.get()), 99999u);
147+
EXPECT_EQ(get_retry_policy(bound_statement.get()), retry_policy.get());
148+
149+
bound_statement.bind<Integer>(0, Integer(1));
150+
151+
Result result = session_.execute(bound_statement);
152+
ASSERT_EQ(result.row_count(), 1);
153+
EXPECT_EQ(result.first_row().column_by_name<Integer>("value").value(), 99);
154+
}
155+
156+
/**
157+
* Verify that a statement is correctly prepared from an existing bound
158+
* statement. The settings from the original bound statement should be
159+
* inherited.
160+
*
161+
* @since 2.8
162+
*/
163+
CASSANDRA_INTEGRATION_TEST_F(PreparedTests, PrepareFromExistingBoundStatement) {
164+
CHECK_FAILURE;
165+
166+
use_keyspace(keyspace_name_);
167+
168+
session_.execute(
169+
format_string(CASSANDRA_KEY_VALUE_TABLE_FORMAT, table_name_.c_str(), "int", "int"));
170+
session_.execute(
171+
format_string(CASSANDRA_KEY_VALUE_INSERT_FORMAT, table_name_.c_str(), "1", "99"));
172+
173+
Statement bound_statement1 =
174+
session_.prepare(format_string(CASSANDRA_SELECT_VALUE_FORMAT, table_name_.c_str(), "?"))
175+
.bind();
176+
177+
DowngradingConsistencyRetryPolicy retry_policy;
178+
179+
// Set unique settings to validate later
180+
bound_statement1.set_consistency(CASS_CONSISTENCY_LOCAL_QUORUM);
181+
bound_statement1.set_serial_consistency(CASS_CONSISTENCY_SERIAL);
182+
bound_statement1.set_request_timeout(99999u);
183+
bound_statement1.set_retry_policy(retry_policy);
184+
185+
// Prepare from the existing bound statement
186+
Statement bound_statement2 = session_.prepare_from_existing(bound_statement1).bind();
187+
188+
// Validate that the bound statement inherited the settings from the original statement
189+
EXPECT_EQ(get_consistency(bound_statement2.get()), CASS_CONSISTENCY_LOCAL_QUORUM);
190+
EXPECT_EQ(get_serial_consistency(bound_statement2.get()), CASS_CONSISTENCY_SERIAL);
191+
EXPECT_EQ(get_request_timeout_ms(bound_statement2.get()), 99999u);
192+
EXPECT_EQ(get_retry_policy(bound_statement2.get()), retry_policy.get());
193+
194+
bound_statement2.bind<Integer>(0, Integer(1));
195+
196+
Result result = session_.execute(bound_statement2);
197+
ASSERT_EQ(result.row_count(), 1);
198+
EXPECT_EQ(result.first_row().column_by_name<Integer>("value").value(), 99);
199+
}

cpp-driver/src/testing.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "request_handler.hpp"
2828
#include "result_response.hpp"
2929
#include "session.hpp"
30+
#include "statement.hpp"
3031

3132
namespace datastax { namespace internal { namespace testing {
3233

@@ -77,4 +78,20 @@ uint64_t get_host_latency_average(CassSession* session, String ip_address, int p
7778
return 0;
7879
}
7980

81+
CassConsistency get_consistency(const CassStatement* statement) {
82+
return statement->from()->consistency();
83+
}
84+
85+
CassConsistency get_serial_consistency(const CassStatement* statement) {
86+
return statement->from()->serial_consistency();
87+
}
88+
89+
uint64_t get_request_timeout_ms(const CassStatement* statement) {
90+
return statement->from()->request_timeout_ms();
91+
}
92+
93+
const CassRetryPolicy* get_retry_policy(const CassStatement* statement) {
94+
return CassRetryPolicy::to(statement->from()->retry_policy().get());
95+
}
96+
8097
}}} // namespace datastax::internal::testing

cpp-driver/src/testing.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ CASS_EXPORT uint64_t get_time_since_epoch_in_ms();
3939

4040
CASS_EXPORT uint64_t get_host_latency_average(CassSession* session, String ip_address, int port);
4141

42+
CASS_EXPORT CassConsistency get_consistency(const CassStatement* statement);
43+
44+
CASS_EXPORT CassConsistency get_serial_consistency(const CassStatement* statement);
45+
46+
CASS_EXPORT uint64_t get_request_timeout_ms(const CassStatement* statement);
47+
48+
CASS_EXPORT const CassRetryPolicy* get_retry_policy(const CassStatement* statement);
49+
4250
}}} // namespace datastax::internal::testing
4351

4452
#endif

cpp-driver/test/ccm_bridge/src/process.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,4 @@ void Process::on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
127127
delete[] buf->base;
128128
}
129129

130-
}; // namespace utils
130+
} // namespace utils

cpp-driver/test/integration_tests/src/test_prepared_existing.cpp

Lines changed: 0 additions & 177 deletions
This file was deleted.

0 commit comments

Comments
 (0)