session_test: add checking set_timestamp methods
@@ -913,3 +913,111 @@ async fn test_await_timed_schema_agreement() {
.await
.unwrap();
}
+
+#[tokio::test]
+async fn test_timestamp() {
+ let uri = std::env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9042".to_string());
+ let session = SessionBuilder::new().known_node(uri).build().await.unwrap();
+
+ session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1}", &[]).await.unwrap();
+ session
+ .query("DROP TABLE IF EXISTS ks.t_timestamp;", &[])
+ .await
+ .unwrap();
+ session
+ .query(
+ "CREATE TABLE IF NOT EXISTS ks.t_timestamp (a text, b text, primary key (a))",
+ &[],
+ )
+ .await
+ .unwrap();
+
+ session.await_schema_agreement().await.unwrap();
+
+ let query_str = "INSERT INTO ks.t_timestamp (a, b) VALUES (?, ?)";
+
+ // test regular query timestamps
+
+ let mut regular_query = Query::new(query_str.to_string());
+
+ regular_query.set_timestamp(Some(420));
+ session
+ .query(regular_query.clone(), ("regular query", "higher timestamp"))
+ .await
+ .unwrap();
+
+ regular_query.set_timestamp(Some(42));
+ session
+ .query(regular_query.clone(), ("regular query", "lower timestamp"))
+ .await
+ .unwrap();
+
+ // test prepared statement timestamps
+
+ let mut prepared_statement = session.prepare(query_str).await.unwrap();
+
+ prepared_statement.set_timestamp(Some(420));
+ session
+ .execute(&prepared_statement, ("prepared query", "higher timestamp"))
+ .await
+ .unwrap();
+
+ prepared_statement.set_timestamp(Some(42));
+ session
+ .execute(&prepared_statement, ("prepared query", "lower timestamp"))
+ .await
+ .unwrap();
+
+ // test batch statement timestamps
+
+ let mut batch: Batch = Default::default();
+ batch.append_statement(regular_query);
+ batch.append_statement(prepared_statement);
+
+ batch.set_timestamp(Some(420));
+ session
+ .batch(
+ &batch,
+ (
+ ("first query in batch", "higher timestamp"),
+ ("second query in batch", "higher timestamp"),
+ ),
+ )
+ .await
+ .unwrap();
+
+ batch.set_timestamp(Some(42));
+ session
+ .batch(
+ &batch,
+ (
+ ("first query in batch", "lower timestamp"),
+ ("second query in batch", "lower timestamp"),
+ ),
+ )
+ .await
+ .unwrap();
+
+ let mut results = session
+ .query("SELECT a, b, WRITETIME(b) FROM ks.t_timestamp", &[])
+ .await
+ .unwrap()
+ .rows
+ .unwrap()
+ .into_typed::<(String, String, i64)>()
+ .map(Result::unwrap)
+ .collect::<Vec<_>>();
+ results.sort();
+
+ let expected_results = [
+ ("first query in batch", "higher timestamp", 420),
+ ("prepared query", "higher timestamp", 420),
+ ("regular query", "higher timestamp", 420),
+ ("second query in batch", "higher timestamp", 420),
+ ]
+ .iter()
+ .map(|(x, y, t)| (x.to_string(), y.to_string(), *t))
+ .collect::<Vec<_>>();
+
+ assert_eq!(results, expected_results);
+}
