Skip to content

Commit

Permalink
add unit test and fix bug (which the unit test caught!)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Donowitz committed Apr 29, 2021
1 parent 95ecafa commit c3abb11
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 2 deletions.
131 changes: 131 additions & 0 deletions src/db/spanner/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,134 @@ macro_rules! params {
}
};
}

#[test]
fn test_params_macro() {
use crate::db::spanner::support::ToSpannerValue;
use googleapis_raw::spanner::v1::type_pb::{Type, TypeCode};
use protobuf::{
well_known_types::{ListValue, Value},
RepeatedField,
};
use std::collections::HashMap;

let (sqlparams, sqlparam_types) = params! {
"String param" => "I am a String".to_owned(),
"i32 param" => 100i32,
"u32 param" => 100u32,
"Vec<String> param" => vec!["I am a String".to_owned()],
"Vec<i32> param" => vec![100i32],
"Vec<u32> param" => vec![100u32],
};

let mut expected_sqlparams = HashMap::new();
let string_value = {
let mut t = Value::new();
t.set_string_value("I am a String".to_owned());
t
};
expected_sqlparams.insert("String param".to_owned(), string_value.clone());

let i32_value = {
let mut t = Value::new();
t.set_string_value(100i32.to_string());
t
};
expected_sqlparams.insert("i32 param".to_owned(), i32_value.clone());

let u32_value = {
let mut t = Value::new();
t.set_string_value(100u32.to_string());
t
};
expected_sqlparams.insert("u32 param".to_owned(), u32_value.clone());

let string_vec_value = {
let mut list = ListValue::new();
list.set_values(RepeatedField::from_vec(vec![string_value]));
let mut value = Value::new();
value.set_list_value(list);
value
};
expected_sqlparams.insert("Vec<String> param".to_owned(), string_vec_value);

let i32_vec_value = {
let mut list = ListValue::new();
list.set_values(RepeatedField::from_vec(vec![i32_value]));
let mut value = Value::new();
value.set_list_value(list);
value
};
expected_sqlparams.insert("Vec<i32> param".to_owned(), i32_vec_value);

let u32_vec_value = {
let mut list = ListValue::new();
list.set_values(RepeatedField::from_vec(vec![u32_value]));
let mut value = Value::new();
value.set_list_value(list);
value
};
expected_sqlparams.insert("Vec<u32> param".to_owned(), u32_vec_value);

let mut expected_sqlparam_types = HashMap::new();

let string_type = {
let mut t = Type::new();
t.set_code(TypeCode::STRING);
t
};
expected_sqlparam_types.insert("String param".to_owned(), string_type);

let i32_type = {
let mut t = Type::new();
t.set_code(TypeCode::INT64);
t
};
expected_sqlparam_types.insert("i32 param".to_owned(), i32_type);

let u32_type = {
let mut t = Type::new();
t.set_code(TypeCode::INT64);
t
};
expected_sqlparam_types.insert("u32 param".to_owned(), u32_type);

let string_vec_type = {
let mut element_type = Type::new();
element_type.set_code(TypeCode::STRING);

let mut vec_type = Type::new();
vec_type.set_code(TypeCode::ARRAY);
vec_type.set_array_element_type(element_type);

vec_type
};
expected_sqlparam_types.insert("Vec<String> param".to_owned(), string_vec_type);

let i32_vec_type = {
let mut element_type = Type::new();
element_type.set_code(TypeCode::INT64);

let mut vec_type = Type::new();
vec_type.set_code(TypeCode::ARRAY);
vec_type.set_array_element_type(element_type);

vec_type
};
expected_sqlparam_types.insert("Vec<i32> param".to_owned(), i32_vec_type);

let u32_vec_type = {
let mut element_type = Type::new();
element_type.set_code(TypeCode::INT64);

let mut vec_type = Type::new();
vec_type.set_code(TypeCode::ARRAY);
vec_type.set_array_element_type(element_type);

vec_type
};
expected_sqlparam_types.insert("Vec<u32> param".to_owned(), u32_vec_type);

assert_eq!(expected_sqlparams, sqlparams);
assert_eq!(expected_sqlparam_types, sqlparam_types);
}
4 changes: 2 additions & 2 deletions src/db/spanner/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ impl SpannerArrayElementType for Vec<String> {
}

impl SpannerArrayElementType for Vec<i32> {
const ARRAY_ELEMENT_TYPE_CODE: TypeCode = TypeCode::STRING;
const ARRAY_ELEMENT_TYPE_CODE: TypeCode = TypeCode::INT64;
}

impl SpannerArrayElementType for Vec<u32> {
const ARRAY_ELEMENT_TYPE_CODE: TypeCode = TypeCode::STRING;
const ARRAY_ELEMENT_TYPE_CODE: TypeCode = TypeCode::INT64;
}

pub fn as_type(v: TypeCode) -> Type {
Expand Down

0 comments on commit c3abb11

Please sign in to comment.