Skip to content

Commit ae623b0

Browse files
committed
tests: add tests for CqlVarint
1 parent 53cf06c commit ae623b0

File tree

2 files changed

+111
-4
lines changed

2 files changed

+111
-4
lines changed

scylla-cql/src/frame/value_tests.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::frame::value::CqlTimeuuid;
1+
use crate::frame::value::{CqlTimeuuid, CqlVarint};
22
use crate::frame::{response::result::CqlValue, types::RawValue, value::LegacyBatchValuesIterator};
33
use crate::types::serialize::batch::{BatchValues, BatchValuesIterator, LegacyBatchValuesAdapter};
44
use crate::types::serialize::row::{RowSerializationContext, SerializeRow};
@@ -7,8 +7,8 @@ use crate::types::serialize::{CellWriter, RowWriter};
77

88
use super::response::result::{ColumnSpec, ColumnType, TableSpec};
99
use super::value::{
10-
CqlDate, CqlDuration, CqlTime, CqlTimestamp, CqlVarint, LegacyBatchValues,
11-
LegacySerializedValues, MaybeUnset, SerializeValuesError, Unset, Value, ValueList, ValueTooBig,
10+
CqlDate, CqlDuration, CqlTime, CqlTimestamp, LegacyBatchValues, LegacySerializedValues,
11+
MaybeUnset, SerializeValuesError, Unset, Value, ValueList, ValueTooBig,
1212
};
1313
use bigdecimal::BigDecimal;
1414
use bytes::BufMut;
@@ -122,6 +122,31 @@ fn cql_varint_normalization_with_bigint() {
122122
}
123123
}
124124

125+
#[test]
126+
fn cql_varint_serialization() {
127+
let cases_from_the_spec: &[Vec<u8>] = &[
128+
vec![0x00],
129+
vec![0x01],
130+
vec![0x7F],
131+
vec![0x00, 0x80],
132+
vec![0x00, 0x81],
133+
vec![0xFF],
134+
vec![0x80],
135+
vec![0xFF, 0x7F],
136+
];
137+
138+
for b in cases_from_the_spec {
139+
let x = CqlVarint::from_signed_bytes_be_slice(b);
140+
let b_with_len = (b.len() as i32)
141+
.to_be_bytes()
142+
.iter()
143+
.chain(b)
144+
.cloned()
145+
.collect::<Vec<_>>();
146+
assert_eq!(serialized(x, ColumnType::Varint), b_with_len);
147+
}
148+
}
149+
125150
#[test]
126151
fn bigint_serialization() {
127152
let cases_from_the_spec: &[(i64, Vec<u8>)] = &[

scylla/src/transport/cql_types_test.rs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::utils::test_utils::unique_keyspace_name;
1010
use bigdecimal::BigDecimal;
1111
use itertools::Itertools;
1212
use num_bigint::BigInt;
13-
use scylla_cql::frame::value::CqlTimeuuid;
13+
use scylla_cql::frame::value::{CqlTimeuuid, CqlVarint};
1414
use scylla_cql::types::serialize::value::SerializeCql;
1515
use scylla_macros::SerializeCql;
1616
use std::cmp::PartialEq;
@@ -122,6 +122,88 @@ async fn test_varint() {
122122
run_tests::<BigInt>(&tests, "varint").await;
123123
}
124124

125+
#[tokio::test]
126+
async fn test_cql_varint() {
127+
let tests = [
128+
vec![0x00], // 0
129+
vec![0x01], // 1
130+
vec![0x00, 0x01], // 1 (with leading zeros)
131+
vec![0x7F], // 127
132+
vec![0x00, 0x80], // 128
133+
vec![0x00, 0x81], // 129
134+
vec![0xFF], // -1
135+
vec![0x80], // -128
136+
vec![0xFF, 0x7F], // -129
137+
vec![
138+
0x01, 0x8E, 0xE9, 0x0F, 0xF6, 0xC3, 0x73, 0xE0, 0xEE, 0x4E, 0x3F, 0x0A, 0xD2,
139+
], // 123456789012345678901234567890
140+
vec![
141+
0xFE, 0x71, 0x16, 0xF0, 0x09, 0x3C, 0x8C, 0x1F, 0x11, 0xB1, 0xC0, 0xF5, 0x2E,
142+
], // -123456789012345678901234567890
143+
];
144+
145+
let table_name = "cql_varint_tests";
146+
let session: Session = create_new_session_builder().build().await.unwrap();
147+
let ks = unique_keyspace_name();
148+
149+
session
150+
.query(
151+
format!(
152+
"CREATE KEYSPACE IF NOT EXISTS {} WITH REPLICATION = \
153+
{{'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}}",
154+
ks
155+
),
156+
&[],
157+
)
158+
.await
159+
.unwrap();
160+
session.use_keyspace(ks, false).await.unwrap();
161+
162+
session
163+
.query(
164+
format!(
165+
"CREATE TABLE IF NOT EXISTS {} (id int PRIMARY KEY, val varint)",
166+
table_name
167+
),
168+
&[],
169+
)
170+
.await
171+
.unwrap();
172+
173+
let prepared_insert = session
174+
.prepare(format!(
175+
"INSERT INTO {} (id, val) VALUES (0, ?)",
176+
table_name
177+
))
178+
.await
179+
.unwrap();
180+
let prepared_select = session
181+
.prepare(format!("SELECT val FROM {} WHERE id = 0", table_name))
182+
.await
183+
.unwrap();
184+
185+
for test in tests {
186+
let cql_varint = CqlVarint::from_signed_bytes_be_slice(&test);
187+
session
188+
.execute(&prepared_insert, (&cql_varint,))
189+
.await
190+
.unwrap();
191+
192+
let read_values: Vec<CqlVarint> = session
193+
.execute(&prepared_select, &[])
194+
.await
195+
.unwrap()
196+
.rows
197+
.unwrap()
198+
.into_typed::<(CqlVarint,)>()
199+
.map(Result::unwrap)
200+
.map(|row| row.0)
201+
.collect::<Vec<_>>();
202+
203+
assert_eq!(read_values, vec![cql_varint])
204+
}
205+
}
206+
125207
#[tokio::test]
126208
async fn test_decimal() {
127209
let tests = [

0 commit comments

Comments
 (0)