Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Commit

Permalink
improve testing over storage module (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-dukhno authored Aug 28, 2020
1 parent 100d970 commit 3108a03
Show file tree
Hide file tree
Showing 23 changed files with 2,034 additions and 496 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

153 changes: 46 additions & 107 deletions src/representation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,6 @@ use sql_types::SqlType;
use sqlparser::ast::Value;
use std::convert::TryFrom;

// owned parallel of Datum but owns the content.
// pub enum Value {
// Null,
// True,
// False,
// Int32(i32),
// Int64(i64),
// Float32(f32),
// Float64(f64),
// String(String),
// // Bytes(Vec<u8>)
// }

/// value shared by the row.
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub enum Datum<'a> {
Expand Down Expand Up @@ -119,7 +106,6 @@ impl<'a> Datum<'a> {
Datum::SqlType(val)
}

// @TODO: Add accessor helper functions.
pub fn as_i16(&self) -> i16 {
match self {
Self::Int16(val) => *val,
Expand Down Expand Up @@ -190,8 +176,6 @@ impl<'a> Datum<'a> {
_ => panic!("invalid use of Datum::as_sql_type"),
}
}

// arithmetic operations
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -425,13 +409,7 @@ pub fn unpack_raw(data: &[u8]) -> Vec<Datum> {
TypeTag::SqlType => {
let val = unsafe { read::<SqlType>(data, &mut index) };
Datum::from_sql_type(val)
} // SqlType::Decimal |
// SqlType::Time |
// SqlType::TimeWithTimeZone |
// SqlType::Timestamp |
// SqlType::TimestampWithTimeZone |
// SqlType::Date |
// SqlType::Interval => unimplemented!()
}
};
res.push(datum)
}
Expand All @@ -442,96 +420,57 @@ pub fn unpack_raw(data: &[u8]) -> Vec<Datum> {
mod tests {
use super::*;

#[test]
fn row_packing_single() {
let datums = vec![Datum::from_bool(true)];
let row = Binary::pack(&datums);
assert_eq!(row, Binary::with_data(vec![0x1]));
}

#[test]
fn row_packing_multiple() {
let datums = vec![Datum::from_bool(true), Datum::from_i32(100000)];
let row = Binary::pack(&datums);
assert_eq!(row, Binary::with_data(vec![0x1, 0x4, 0xa0, 0x86, 0x1, 0x0]));
}

#[test]
fn row_packing_with_floats() {
let datums = vec![
Datum::from_bool(false),
Datum::from_i32(100000),
Datum::from_f32(100.134_21),
];
let row = Binary::pack(&datums);
assert_eq!(
row,
Binary::with_data(vec![0x2, 0x4, 0xa0, 0x86, 0x1, 0x0, 0x7, 0xb7, 0x44, 0xc8, 0x42])
);
}

#[test]
fn row_packing_with_null() {
let datums = vec![Datum::from_bool(true), Datum::from_null(), Datum::from_i32(100000)];
let row = Binary::pack(&datums);
assert_eq!(row, Binary::with_data(vec![0x1, 0x0, 0x4, 0xa0, 0x86, 0x1, 0x0]));
}
#[cfg(test)]
mod pack_unpack_types {
use super::*;

#[test]
fn row_packing_string() {
let datums = vec![Datum::from_bool(true), Datum::from_str("hello")];
let row = Binary::pack(&datums);
assert_eq!(
row,
Binary::with_data(vec![
0x1, 0x9, 0x5, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x68, 0x65, 0x6c, 0x6c, 0x6f
])
);
}
#[test]
fn null() {
let data = vec![Datum::from_null()];
let row = Binary::pack(&data);
assert_eq!(data, row.unpack());
}

#[test]
fn row_unpacking_single() {
let datums = vec![Datum::from_bool(true)];
let row = Binary::pack(&datums);
assert_eq!(row.unpack(), datums);
}
#[test]
fn booleans() {
let data = vec![Datum::from_bool(true)];
let row = Binary::pack(&data);
assert_eq!(data, row.unpack());
}

#[test]
fn row_unpacking_multiple() {
let datums = vec![Datum::from_bool(true), Datum::from_i32(100000)];
let row = Binary::pack(&datums);
assert_eq!(row.unpack(), datums);
}
#[test]
fn floats() {
let data = vec![Datum::from_f32(1000.123), Datum::from_f64(100.134_219_234_555)];
let row = Binary::pack(&data);
assert_eq!(data, row.unpack());
}

#[test]
fn row_unpacking_with_floats() {
let datums = vec![
Datum::from_bool(false),
Datum::from_i32(100000),
Datum::from_f64(100.134_212_309_847),
];
let row = Binary::pack(&datums);
assert_eq!(row.unpack(), datums);
}
#[test]
fn integers() {
let data = vec![Datum::from_i16(100), Datum::from_i32(1_000), Datum::from_i64(10_000)];
let row = Binary::pack(&data);
assert_eq!(data, row.unpack());
}

#[test]
fn row_unpacking_with_null() {
let datums = vec![Datum::from_bool(true), Datum::from_null(), Datum::from_i32(100000)];
let row = Binary::pack(&datums);
assert_eq!(row.unpack(), datums);
}
#[test]
fn unsigned_integers() {
let data = vec![Datum::from_u64(10_000)];
let row = Binary::pack(&data);
assert_eq!(data, row.unpack());
}

#[test]
fn row_unpacking_string() {
let datums = vec![Datum::from_bool(true), Datum::from_str("hello")];
let row = Binary::pack(&datums);
assert_eq!(row.unpack(), datums);
}
#[test]
fn strings() {
let data = vec![Datum::from_string("string".to_owned()), Datum::from_str("hello")];
let row = Binary::pack(&data);
assert_eq!(vec![Datum::from_str("string"), Datum::from_str("hello")], row.unpack());
}

#[test]
fn row_unpacking_sql_type() {
let data = vec![Datum::from_sql_type(SqlType::VarChar(32))];
let row = Binary::pack(&data);
assert_eq!(vec![Datum::from_sql_type(SqlType::VarChar(32))], row.unpack());
#[test]
fn sql_type() {
let data = vec![Datum::from_sql_type(SqlType::VarChar(32))];
let row = Binary::pack(&data);
assert_eq!(data, row.unpack());
}
}
}
Loading

0 comments on commit 3108a03

Please sign in to comment.