rust-postgres/tests/types/mod.rs

227 lines
8.0 KiB
Rust
Raw Normal View History

2015-01-03 21:23:11 +00:00
use serialize::json::Json;
2014-07-26 22:35:38 +00:00
use std::collections::HashMap;
use std::f32;
use std::f64;
2014-11-20 18:54:40 +00:00
use std::fmt;
2014-11-15 16:53:31 +00:00
use std::num::Float;
use std::io::net::ip::IpAddr;
2014-07-26 22:35:38 +00:00
2014-11-17 21:46:33 +00:00
use postgres::{Connection, SslMode};
2014-07-26 22:35:38 +00:00
use postgres::types::{ToSql, FromSql};
2014-11-17 17:43:10 +00:00
#[cfg(feature = "uuid")]
mod uuid;
2014-11-18 03:11:32 +00:00
mod time;
2014-07-26 22:35:38 +00:00
2015-01-23 18:44:15 +00:00
fn test_type<T: PartialEq+FromSql+ToSql, S: fmt::Display>(sql_type: &str, checks: &[(T, S)]) {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-07-26 22:35:38 +00:00
for &(ref val, ref repr) in checks.iter() {
let stmt = or_panic!(conn.prepare(&*format!("SELECT {}::{}", *repr, sql_type)));
let result = or_panic!(stmt.query(&[])).next().unwrap().get(0us);
2014-07-26 22:35:38 +00:00
assert!(val == &result);
let stmt = or_panic!(conn.prepare(&*format!("SELECT $1::{}", sql_type)));
let result = or_panic!(stmt.query(&[val])).next().unwrap().get(0us);
2014-07-26 22:35:38 +00:00
assert!(val == &result);
}
}
#[test]
fn test_bool_params() {
test_type("BOOL", &[(Some(true), "'t'"), (Some(false), "'f'"),
2014-07-26 22:35:38 +00:00
(None, "NULL")]);
}
#[test]
fn test_i8_params() {
test_type("\"char\"", &[(Some('a' as i8), "'a'"), (None, "NULL")]);
2014-07-26 22:35:38 +00:00
}
#[test]
fn test_name_params() {
test_type("NAME", &[(Some("hello world".to_string()), "'hello world'"),
2014-07-26 22:35:38 +00:00
(Some("イロハニホヘト チリヌルヲ".to_string()), "'イロハニホヘト チリヌルヲ'"),
(None, "NULL")]);
}
#[test]
fn test_i16_params() {
test_type("SMALLINT", &[(Some(15001i16), "15001"),
2014-07-26 22:35:38 +00:00
(Some(-15001i16), "-15001"), (None, "NULL")]);
}
#[test]
fn test_i32_params() {
test_type("INT", &[(Some(2147483548i32), "2147483548"),
2014-07-26 22:35:38 +00:00
(Some(-2147483548i32), "-2147483548"), (None, "NULL")]);
}
#[test]
fn test_oid_params() {
test_type("OID", &[(Some(2147483548u32), "2147483548"),
(Some(4000000000), "4000000000"), (None, "NULL")]);
}
2014-07-26 22:35:38 +00:00
#[test]
fn test_i64_params() {
test_type("BIGINT", &[(Some(9223372036854775708i64), "9223372036854775708"),
2014-07-26 22:35:38 +00:00
(Some(-9223372036854775708i64), "-9223372036854775708"),
(None, "NULL")]);
}
#[test]
fn test_f32_params() {
test_type("REAL", &[(Some(f32::INFINITY), "'infinity'"),
2014-07-26 22:35:38 +00:00
(Some(f32::NEG_INFINITY), "'-infinity'"),
(Some(1000.55), "1000.55"), (None, "NULL")]);
}
#[test]
fn test_f64_params() {
test_type("DOUBLE PRECISION", &[(Some(f64::INFINITY), "'infinity'"),
2014-07-26 22:35:38 +00:00
(Some(f64::NEG_INFINITY), "'-infinity'"),
(Some(10000.55), "10000.55"),
(None, "NULL")]);
}
#[test]
fn test_varchar_params() {
test_type("VARCHAR", &[(Some("hello world".to_string()), "'hello world'"),
2014-07-26 22:35:38 +00:00
(Some("イロハニホヘト チリヌルヲ".to_string()), "'イロハニホヘト チリヌルヲ'"),
(None, "NULL")]);
}
#[test]
fn test_text_params() {
test_type("TEXT", &[(Some("hello world".to_string()), "'hello world'"),
2014-07-26 22:35:38 +00:00
(Some("イロハニホヘト チリヌルヲ".to_string()), "'イロハニホヘト チリヌルヲ'"),
(None, "NULL")]);
}
#[test]
fn test_bpchar_params() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-10-30 04:26:03 +00:00
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (
2014-07-26 22:35:38 +00:00
id SERIAL PRIMARY KEY,
b CHAR(5)
)", &[]));
2014-10-30 04:26:03 +00:00
or_panic!(conn.execute("INSERT INTO foo (b) VALUES ($1), ($2), ($3)",
&[&Some("12345"), &Some("123"), &None::<&'static str>]));
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT b FROM foo ORDER BY id"));
let res = or_panic!(stmt.query(&[]));
2014-07-26 22:35:38 +00:00
assert_eq!(vec!(Some("12345".to_string()), Some("123 ".to_string()), None),
res.map(|row| row.get(0us)).collect::<Vec<_>>());
2014-07-26 22:35:38 +00:00
}
#[test]
fn test_citext_params() {
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (
id SERIAL PRIMARY KEY,
b CITEXT
)", &[]));
or_panic!(conn.execute("INSERT INTO foo (b) VALUES ($1), ($2), ($3)",
&[&Some("foobar"), &Some("FooBar"), &None::<&'static str>]));
let stmt = or_panic!(conn.prepare("SELECT id FROM foo WHERE b = 'FOOBAR' ORDER BY id"));
let res = or_panic!(stmt.query(&[]));
assert_eq!(vec!(Some(1i32), Some(2i32)), res.map(|row| row.get(0us)).collect::<Vec<_>>());
}
2014-07-26 22:35:38 +00:00
#[test]
fn test_bytea_params() {
test_type("BYTEA", &[(Some(vec!(0u8, 1, 2, 3, 254, 255)), "'\\x00010203feff'"),
2014-07-26 22:35:38 +00:00
(None, "NULL")]);
}
#[test]
fn test_json_params() {
2015-01-03 21:23:11 +00:00
test_type("JSON", &[(Some(Json::from_str("[10, 11, 12]").unwrap()),
2014-07-26 22:35:38 +00:00
"'[10, 11, 12]'"),
2015-01-03 21:23:11 +00:00
(Some(Json::from_str("{\"f\": \"asd\"}").unwrap()),
2014-07-26 22:35:38 +00:00
"'{\"f\": \"asd\"}'"),
(None, "NULL")])
}
#[test]
fn test_jsonb_params() {
2015-01-22 06:39:47 +00:00
if option_env!("TRAVIS").is_some() { return } // Travis doesn't have Postgres 9.4 yet
test_type("JSONB", &[(Some(Json::from_str("[10, 11, 12]").unwrap()),
"'[10, 11, 12]'"),
(Some(Json::from_str("{\"f\": \"asd\"}").unwrap()),
"'{\"f\": \"asd\"}'"),
(None, "NULL")])
}
#[test]
fn test_inet_params() {
2014-12-23 17:10:16 +00:00
test_type("INET", &[(Some("127.0.0.1".parse::<IpAddr>().unwrap()),
"'127.0.0.1'"),
2014-12-23 17:10:16 +00:00
(Some("2001:0db8:85a3:0000:0000:8a2e:0370:7334".parse::<IpAddr>().unwrap()),
2014-12-14 20:27:02 +00:00
"'2001:0db8:85a3:0000:0000:8a2e:0370:7334'"),
(None, "NULL")])
}
#[test]
fn test_cidr_params() {
2014-12-23 17:10:16 +00:00
test_type("CIDR", &[(Some("127.0.0.1".parse::<IpAddr>().unwrap()),
2014-12-14 20:27:02 +00:00
"'127.0.0.1'"),
2014-12-23 17:10:16 +00:00
(Some("2001:0db8:85a3:0000:0000:8a2e:0370:7334".parse::<IpAddr>().unwrap()),
2014-12-14 20:27:02 +00:00
"'2001:0db8:85a3:0000:0000:8a2e:0370:7334'"),
(None, "NULL")])
}
2014-07-26 22:35:38 +00:00
#[test]
fn test_hstore_params() {
2014-12-19 18:43:42 +00:00
macro_rules! make_map {
2014-07-26 22:35:38 +00:00
($($k:expr => $v:expr),+) => ({
let mut map = HashMap::new();
$(map.insert($k, $v);)+
map
})
2014-12-19 18:43:42 +00:00
}
2014-07-26 22:35:38 +00:00
test_type("hstore",
&[(Some(make_map!("a".to_string() => Some("1".to_string()))), "'a=>1'"),
2014-07-26 22:35:38 +00:00
(Some(make_map!("hello".to_string() => Some("world!".to_string()),
"hola".to_string() => Some("mundo!".to_string()),
"what".to_string() => None)),
"'hello=>world!,hola=>mundo!,what=>NULL'"),
(None, "NULL")]);
}
fn test_nan_param<T: Float+ToSql+FromSql>(sql_type: &str) {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
let stmt = or_panic!(conn.prepare(&*format!("SELECT 'NaN'::{}", sql_type)));
let mut result = or_panic!(stmt.query(&[]));
let val: T = result.next().unwrap().get(0us);
2014-07-26 22:35:38 +00:00
assert!(val.is_nan());
let nan: T = Float::nan();
let stmt = or_panic!(conn.prepare(&*format!("SELECT $1::{}", sql_type)));
2014-10-30 04:26:03 +00:00
let mut result = or_panic!(stmt.query(&[&nan]));
let val: T = result.next().unwrap().get(0us);
2014-07-26 22:35:38 +00:00
assert!(val.is_nan())
}
#[test]
fn test_f32_nan_param() {
test_nan_param::<f32>("REAL");
}
#[test]
fn test_f64_nan_param() {
test_nan_param::<f64>("DOUBLE PRECISION");
}
#[test]
fn test_pg_database_datname() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT datname FROM pg_database"));
let mut result = or_panic!(stmt.query(&[]));
2014-07-26 22:35:38 +00:00
let next = result.next().unwrap();
or_panic!(next.get_opt::<usize, String>(0));
2014-10-30 04:26:03 +00:00
or_panic!(next.get_opt::<&str, String>("datname"));
2014-07-26 22:35:38 +00:00
}