diff --git a/postgres-protocol/src/types.rs b/postgres-protocol/src/types/mod.rs similarity index 85% rename from postgres-protocol/src/types.rs rename to postgres-protocol/src/types/mod.rs index 7fac2b9d8..46000c407 100644 --- a/postgres-protocol/src/types.rs +++ b/postgres-protocol/src/types/mod.rs @@ -7,6 +7,9 @@ use std::str; use crate::{write_nullable, FromUsize, IsNull, Oid}; +#[cfg(test)] +mod test; + const RANGE_UPPER_UNBOUNDED: u8 = 0b0001_0000; const RANGE_LOWER_UNBOUNDED: u8 = 0b0000_1000; const RANGE_UPPER_INCLUSIVE: u8 = 0b0000_0100; @@ -953,163 +956,3 @@ impl<'a> FallibleIterator for PathPoints<'a> { (len, Some(len)) } } - -#[cfg(test)] -mod test { - use fallible_iterator::FallibleIterator; - use std::collections::HashMap; - - use super::*; - use crate::IsNull; - - #[test] - fn bool() { - let mut buf = vec![]; - bool_to_sql(true, &mut buf); - assert_eq!(bool_from_sql(&buf).unwrap(), true); - - let mut buf = vec![]; - bool_to_sql(false, &mut buf); - assert_eq!(bool_from_sql(&buf).unwrap(), false); - } - - #[test] - fn int2() { - let mut buf = vec![]; - int2_to_sql(0x0102, &mut buf); - assert_eq!(int2_from_sql(&buf).unwrap(), 0x0102); - } - - #[test] - fn int4() { - let mut buf = vec![]; - int4_to_sql(0x0102_0304, &mut buf); - assert_eq!(int4_from_sql(&buf).unwrap(), 0x0102_0304); - } - - #[test] - fn int8() { - let mut buf = vec![]; - int8_to_sql(0x0102_0304_0506_0708, &mut buf); - assert_eq!(int8_from_sql(&buf).unwrap(), 0x0102_0304_0506_0708); - } - - #[test] - #[allow(clippy::float_cmp)] - fn float4() { - let mut buf = vec![]; - float4_to_sql(10343.95, &mut buf); - assert_eq!(float4_from_sql(&buf).unwrap(), 10343.95); - } - - #[test] - #[allow(clippy::float_cmp)] - fn float8() { - let mut buf = vec![]; - float8_to_sql(10343.95, &mut buf); - assert_eq!(float8_from_sql(&buf).unwrap(), 10343.95); - } - - #[test] - fn hstore() { - let mut map = HashMap::new(); - map.insert("hello", Some("world")); - map.insert("hola", None); - - let mut buf = vec![]; - hstore_to_sql(map.iter().map(|(&k, &v)| (k, v)), &mut buf).unwrap(); - assert_eq!( - hstore_from_sql(&buf) - .unwrap() - .collect::>() - .unwrap(), - map - ); - } - - #[test] - fn varbit() { - let len = 12; - let bits = [0b0010_1011, 0b0000_1111]; - - let mut buf = vec![]; - varbit_to_sql(len, bits.iter().cloned(), &mut buf).unwrap(); - let out = varbit_from_sql(&buf).unwrap(); - assert_eq!(out.len(), len); - assert_eq!(out.bytes(), bits); - } - - #[test] - fn array() { - let dimensions = [ - ArrayDimension { - len: 1, - lower_bound: 10, - }, - ArrayDimension { - len: 2, - lower_bound: 0, - }, - ]; - let values = [None, Some(&b"hello"[..])]; - - let mut buf = vec![]; - array_to_sql( - dimensions.iter().cloned(), - 10, - values.iter().cloned(), - |v, buf| match v { - Some(v) => { - buf.extend_from_slice(v); - Ok(IsNull::No) - } - None => Ok(IsNull::Yes), - }, - &mut buf, - ) - .unwrap(); - - let array = array_from_sql(&buf).unwrap(); - assert_eq!(array.has_nulls(), true); - assert_eq!(array.element_type(), 10); - assert_eq!(array.dimensions().collect::>().unwrap(), dimensions); - assert_eq!(array.values().collect::>().unwrap(), values); - } - - #[test] - fn non_null_array() { - let dimensions = [ - ArrayDimension { - len: 1, - lower_bound: 10, - }, - ArrayDimension { - len: 2, - lower_bound: 0, - }, - ]; - let values = [Some(&b"hola"[..]), Some(&b"hello"[..])]; - - let mut buf = vec![]; - array_to_sql( - dimensions.iter().cloned(), - 10, - values.iter().cloned(), - |v, buf| match v { - Some(v) => { - buf.extend_from_slice(v); - Ok(IsNull::No) - } - None => Ok(IsNull::Yes), - }, - &mut buf, - ) - .unwrap(); - - let array = array_from_sql(&buf).unwrap(); - assert_eq!(array.has_nulls(), false); - assert_eq!(array.element_type(), 10); - assert_eq!(array.dimensions().collect::>().unwrap(), dimensions); - assert_eq!(array.values().collect::>().unwrap(), values); - } -} diff --git a/postgres-protocol/src/types/test.rs b/postgres-protocol/src/types/test.rs new file mode 100644 index 000000000..7d2563555 --- /dev/null +++ b/postgres-protocol/src/types/test.rs @@ -0,0 +1,156 @@ +use fallible_iterator::FallibleIterator; +use std::collections::HashMap; + +use super::*; +use crate::IsNull; + +#[test] +fn bool() { + let mut buf = vec![]; + bool_to_sql(true, &mut buf); + assert_eq!(bool_from_sql(&buf).unwrap(), true); + + let mut buf = vec![]; + bool_to_sql(false, &mut buf); + assert_eq!(bool_from_sql(&buf).unwrap(), false); +} + +#[test] +fn int2() { + let mut buf = vec![]; + int2_to_sql(0x0102, &mut buf); + assert_eq!(int2_from_sql(&buf).unwrap(), 0x0102); +} + +#[test] +fn int4() { + let mut buf = vec![]; + int4_to_sql(0x0102_0304, &mut buf); + assert_eq!(int4_from_sql(&buf).unwrap(), 0x0102_0304); +} + +#[test] +fn int8() { + let mut buf = vec![]; + int8_to_sql(0x0102_0304_0506_0708, &mut buf); + assert_eq!(int8_from_sql(&buf).unwrap(), 0x0102_0304_0506_0708); +} + +#[test] +#[allow(clippy::float_cmp)] +fn float4() { + let mut buf = vec![]; + float4_to_sql(10343.95, &mut buf); + assert_eq!(float4_from_sql(&buf).unwrap(), 10343.95); +} + +#[test] +#[allow(clippy::float_cmp)] +fn float8() { + let mut buf = vec![]; + float8_to_sql(10343.95, &mut buf); + assert_eq!(float8_from_sql(&buf).unwrap(), 10343.95); +} + +#[test] +fn hstore() { + let mut map = HashMap::new(); + map.insert("hello", Some("world")); + map.insert("hola", None); + + let mut buf = vec![]; + hstore_to_sql(map.iter().map(|(&k, &v)| (k, v)), &mut buf).unwrap(); + assert_eq!( + hstore_from_sql(&buf) + .unwrap() + .collect::>() + .unwrap(), + map + ); +} + +#[test] +fn varbit() { + let len = 12; + let bits = [0b0010_1011, 0b0000_1111]; + + let mut buf = vec![]; + varbit_to_sql(len, bits.iter().cloned(), &mut buf).unwrap(); + let out = varbit_from_sql(&buf).unwrap(); + assert_eq!(out.len(), len); + assert_eq!(out.bytes(), bits); +} + +#[test] +fn array() { + let dimensions = [ + ArrayDimension { + len: 1, + lower_bound: 10, + }, + ArrayDimension { + len: 2, + lower_bound: 0, + }, + ]; + let values = [None, Some(&b"hello"[..])]; + + let mut buf = vec![]; + array_to_sql( + dimensions.iter().cloned(), + 10, + values.iter().cloned(), + |v, buf| match v { + Some(v) => { + buf.extend_from_slice(v); + Ok(IsNull::No) + } + None => Ok(IsNull::Yes), + }, + &mut buf, + ) + .unwrap(); + + let array = array_from_sql(&buf).unwrap(); + assert_eq!(array.has_nulls(), true); + assert_eq!(array.element_type(), 10); + assert_eq!(array.dimensions().collect::>().unwrap(), dimensions); + assert_eq!(array.values().collect::>().unwrap(), values); +} + +#[test] +fn non_null_array() { + let dimensions = [ + ArrayDimension { + len: 1, + lower_bound: 10, + }, + ArrayDimension { + len: 2, + lower_bound: 0, + }, + ]; + let values = [Some(&b"hola"[..]), Some(&b"hello"[..])]; + + let mut buf = vec![]; + array_to_sql( + dimensions.iter().cloned(), + 10, + values.iter().cloned(), + |v, buf| match v { + Some(v) => { + buf.extend_from_slice(v); + Ok(IsNull::No) + } + None => Ok(IsNull::Yes), + }, + &mut buf, + ) + .unwrap(); + + let array = array_from_sql(&buf).unwrap(); + assert_eq!(array.has_nulls(), false); + assert_eq!(array.element_type(), 10); + assert_eq!(array.dimensions().collect::>().unwrap(), dimensions); + assert_eq!(array.values().collect::>().unwrap(), values); +}