| 1 |
extern crate time_ as time; |
| 2 |
|
| 3 |
use std::net::SocketAddr; |
| 4 |
use std::ops::Bound; |
| 5 |
|
| 6 |
use sqlx::postgres::types::{Oid, PgCiText, PgInterval, PgMoney, PgRange}; |
| 7 |
use sqlx::postgres::Postgres; |
| 8 |
use sqlx_test::{new, test_decode_type, test_prepared_type, test_type}; |
| 9 |
|
| 10 |
use sqlx_core::executor::Executor; |
| 11 |
use sqlx_core::types::Text; |
| 12 |
use std::str::FromStr; |
| 13 |
|
| 14 |
test_type!(null<Option<i16>>(Postgres, |
| 15 |
"NULL::int2" == None::<i16> |
| 16 |
)); |
| 17 |
|
| 18 |
test_type!(null_vec<Vec<Option<i16>>>(Postgres, |
| 19 |
"array[10,NULL,50]::int2[]" == vec![Some(10_i16), None, Some(50)], |
| 20 |
)); |
| 21 |
|
| 22 |
test_type!(null_array<[Option<i16>; 3]>(Postgres, |
| 23 |
"array[10,NULL,50]::int2[]" == vec![Some(10_i16), None, Some(50)], |
| 24 |
)); |
| 25 |
|
| 26 |
test_type!(bool<bool>(Postgres, |
| 27 |
"false::boolean" == false, |
| 28 |
"true::boolean" == true |
| 29 |
)); |
| 30 |
|
| 31 |
test_type!(bool_vec<Vec<bool>>(Postgres, |
| 32 |
"array[true,false,true]::bool[]" == vec![true, false, true], |
| 33 |
)); |
| 34 |
|
| 35 |
test_type!(bool_array<[bool; 3]>(Postgres, |
| 36 |
"array[true,false,true]::bool[]" == vec![true, false, true], |
| 37 |
)); |
| 38 |
|
| 39 |
test_type!(byte_vec<Vec<u8>>(Postgres, |
| 40 |
"E'\\\\xDEADBEEF'::bytea" |
| 41 |
== vec![0xDE_u8, 0xAD, 0xBE, 0xEF], |
| 42 |
"E'\\\\x'::bytea" |
| 43 |
== Vec::<u8>::new(), |
| 44 |
"E'\\\\x0000000052'::bytea" |
| 45 |
== vec![0_u8, 0, 0, 0, 0x52] |
| 46 |
)); |
| 47 |
|
| 48 |
|
| 49 |
test_prepared_type!(byte_slice<&[u8]>(Postgres, |
| 50 |
"E'\\\\xDEADBEEF'::bytea" |
| 51 |
== &[0xDE_u8, 0xAD, 0xBE, 0xEF][..], |
| 52 |
"E'\\\\x0000000052'::bytea" |
| 53 |
== &[0_u8, 0, 0, 0, 0x52][..] |
| 54 |
)); |
| 55 |
|
| 56 |
test_type!(byte_array_empty<[u8; 0]>(Postgres, |
| 57 |
"E'\\\\x'::bytea" == [0_u8; 0], |
| 58 |
)); |
| 59 |
|
| 60 |
test_type!(byte_array<[u8; 4]>(Postgres, |
| 61 |
"E'\\\\xDEADBEEF'::bytea" == [0xDE_u8, 0xAD, 0xBE, 0xEF], |
| 62 |
)); |
| 63 |
|
| 64 |
test_type!(str<&str>(Postgres, |
| 65 |
"'this is foo'" == "this is foo", |
| 66 |
"''" == "", |
| 67 |
"'identifier'::name" == "identifier", |
| 68 |
"'five'::char(4)" == "five", |
| 69 |
"'more text'::varchar" == "more text", |
| 70 |
"'case insensitive searching'::citext" == "case insensitive searching", |
| 71 |
)); |
| 72 |
|
| 73 |
test_type!(string<String>(Postgres, |
| 74 |
"'this is foo'" == format!("this is foo"), |
| 75 |
)); |
| 76 |
|
| 77 |
test_type!(string_vec<Vec<String>>(Postgres, |
| 78 |
"array['one','two','three']::text[]" |
| 79 |
== vec!["one","two","three"], |
| 80 |
|
| 81 |
"array['', '\"']::text[]" |
| 82 |
== vec!["", "\""], |
| 83 |
|
| 84 |
"array['Hello, World', '', 'Goodbye']::text[]" |
| 85 |
== vec!["Hello, World", "", "Goodbye"], |
| 86 |
)); |
| 87 |
|
| 88 |
test_type!(string_array<[String; 3]>(Postgres, |
| 89 |
"array['one','two','three']::text[]" == ["one","two","three"], |
| 90 |
)); |
| 91 |
|
| 92 |
test_type!(i8( |
| 93 |
Postgres, |
| 94 |
"0::\"char\"" == 0_i8, |
| 95 |
"120::\"char\"" == 120_i8, |
| 96 |
)); |
| 97 |
|
| 98 |
test_type!(Oid(Postgres, "325235::oid" == Oid(325235),)); |
| 99 |
|
| 100 |
test_type!(i16( |
| 101 |
Postgres, |
| 102 |
"-2144::smallint" == -2144_i16, |
| 103 |
"821::smallint" == 821_i16, |
| 104 |
)); |
| 105 |
|
| 106 |
test_type!(i32( |
| 107 |
Postgres, |
| 108 |
"94101::int" == 94101_i32, |
| 109 |
"-5101::int" == -5101_i32 |
| 110 |
)); |
| 111 |
|
| 112 |
test_type!(i32_vec<Vec<i32>>(Postgres, |
| 113 |
"'{5,10,50,100}'::int[]" == vec![5_i32, 10, 50, 100], |
| 114 |
"'{1050}'::int[]" == vec![1050_i32], |
| 115 |
"'{}'::int[]" == Vec::<i32>::new(), |
| 116 |
"'{1,3,-5}'::int[]" == vec![1_i32, 3, -5] |
| 117 |
)); |
| 118 |
|
| 119 |
test_type!(i32_array_empty<[i32; 0]>(Postgres, |
| 120 |
"'{}'::int[]" == [0_i32; 0], |
| 121 |
)); |
| 122 |
|
| 123 |
test_type!(i32_array<[i32; 4]>(Postgres, |
| 124 |
"'{5,10,50,100}'::int[]" == [5_i32, 10, 50, 100], |
| 125 |
)); |
| 126 |
|
| 127 |
test_type!(i64(Postgres, "9358295312::bigint" == 9358295312_i64)); |
| 128 |
|
| 129 |
test_type!(f32(Postgres, "9419.122::real" == 9419.122_f32)); |
| 130 |
|
| 131 |
test_type!(f64( |
| 132 |
Postgres, |
| 133 |
"939399419.1225182::double precision" == 939399419.1225182_f64 |
| 134 |
)); |
| 135 |
|
| 136 |
test_type!(f64_vec<Vec<f64>>(Postgres, |
| 137 |
"'{939399419.1225182,-12.0}'::float8[]" == vec![939399419.1225182_f64, -12.0] |
| 138 |
)); |
| 139 |
|
| 140 |
test_decode_type!(bool_tuple<(bool,)>(Postgres, "row(true)" == (true,))); |
| 141 |
|
| 142 |
test_decode_type!(num_tuple<(i32, i64, f64,)>(Postgres, "row(10,515::int8,3.124::float8)" == (10,515,3.124))); |
| 143 |
|
| 144 |
test_decode_type!(empty_tuple<()>(Postgres, "row()" == ())); |
| 145 |
|
| 146 |
test_decode_type!(string_tuple<(String, String, String)>(Postgres, |
| 147 |
"row('one','two','three')" |
| 148 |
== ("one".to_string(), "two".to_string(), "three".to_string()), |
| 149 |
|
| 150 |
"row('', '\"', '\"\"\"\"\"\"')" |
| 151 |
== ("".to_string(), "\"".to_string(), "\"\"\"\"\"\"".to_string()), |
| 152 |
|
| 153 |
"row('Hello, World', '', 'Goodbye')" |
| 154 |
== ("Hello, World".to_string(), "".to_string(), "Goodbye".to_string()) |
| 155 |
)); |
| 156 |
|
| 157 |
#[cfg(feature = "uuid")] |
| 158 |
test_type!(uuid<sqlx::types::Uuid>(Postgres, |
| 159 |
"'b731678f-636f-4135-bc6f-19440c13bd19'::uuid" |
| 160 |
== sqlx::types::Uuid::parse_str("b731678f-636f-4135-bc6f-19440c13bd19").unwrap(), |
| 161 |
"'00000000-0000-0000-0000-000000000000'::uuid" |
| 162 |
== sqlx::types::Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap() |
| 163 |
)); |
| 164 |
|
| 165 |
#[cfg(feature = "uuid")] |
| 166 |
test_type!(uuid_vec<Vec<sqlx::types::Uuid>>(Postgres, |
| 167 |
"'{b731678f-636f-4135-bc6f-19440c13bd19,00000000-0000-0000-0000-000000000000}'::uuid[]" |
| 168 |
== vec![ |
| 169 |
sqlx::types::Uuid::parse_str("b731678f-636f-4135-bc6f-19440c13bd19").unwrap(), |
| 170 |
sqlx::types::Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap() |
| 171 |
] |
| 172 |
)); |
| 173 |
|
| 174 |
#[cfg(feature = "ipnetwork")] |
| 175 |
test_type!(ipnetwork<sqlx::types::ipnetwork::IpNetwork>(Postgres, |
| 176 |
"'127.0.0.1'::inet" |
| 177 |
== "127.0.0.1" |
| 178 |
.parse::<sqlx::types::ipnetwork::IpNetwork>() |
| 179 |
.unwrap(), |
| 180 |
"'8.8.8.8/24'::inet" |
| 181 |
== "8.8.8.8/24" |
| 182 |
.parse::<sqlx::types::ipnetwork::IpNetwork>() |
| 183 |
.unwrap(), |
| 184 |
"'::ffff:1.2.3.0'::inet" |
| 185 |
== "::ffff:1.2.3.0" |
| 186 |
.parse::<sqlx::types::ipnetwork::IpNetwork>() |
| 187 |
.unwrap(), |
| 188 |
"'2001:4f8:3:ba::/64'::inet" |
| 189 |
== "2001:4f8:3:ba::/64" |
| 190 |
.parse::<sqlx::types::ipnetwork::IpNetwork>() |
| 191 |
.unwrap(), |
| 192 |
"'192.168'::cidr" |
| 193 |
== "192.168.0.0/24" |
| 194 |
.parse::<sqlx::types::ipnetwork::IpNetwork>() |
| 195 |
.unwrap(), |
| 196 |
"'::ffff:1.2.3.0/120'::cidr" |
| 197 |
== "::ffff:1.2.3.0/120" |
| 198 |
.parse::<sqlx::types::ipnetwork::IpNetwork>() |
| 199 |
.unwrap(), |
| 200 |
)); |
| 201 |
|
| 202 |
#[cfg(feature = "mac_address")] |
| 203 |
test_type!(mac_address<sqlx::types::mac_address::MacAddress>(Postgres, |
| 204 |
"'00:01:02:03:04:05'::macaddr" |
| 205 |
== "00:01:02:03:04:05" |
| 206 |
.parse::<sqlx::types::mac_address::MacAddress>() |
| 207 |
.unwrap() |
| 208 |
)); |
| 209 |
|
| 210 |
#[cfg(feature = "bit-vec")] |
| 211 |
test_type!(bitvec<sqlx::types::BitVec>( |
| 212 |
Postgres, |
| 213 |
|
| 214 |
"B'01101001'" == sqlx::types::BitVec::from_bytes(&[0b0110_1001]), |
| 215 |
|
| 216 |
"B'110'" == { |
| 217 |
let mut bit_vec = sqlx::types::BitVec::with_capacity(4); |
| 218 |
bit_vec.push(true); |
| 219 |
bit_vec.push(true); |
| 220 |
bit_vec.push(false); |
| 221 |
bit_vec |
| 222 |
}, |
| 223 |
|
| 224 |
"B'01101'::bit(5)" == { |
| 225 |
let mut bit_vec = sqlx::types::BitVec::with_capacity(5); |
| 226 |
bit_vec.push(false); |
| 227 |
bit_vec.push(true); |
| 228 |
bit_vec.push(true); |
| 229 |
bit_vec.push(false); |
| 230 |
bit_vec.push(true); |
| 231 |
bit_vec |
| 232 |
}, |
| 233 |
)); |
| 234 |
|
| 235 |
#[cfg(feature = "ipnetwork")] |
| 236 |
test_type!(ipnetwork_vec<Vec<sqlx::types::ipnetwork::IpNetwork>>(Postgres, |
| 237 |
"'{127.0.0.1,8.8.8.8/24}'::inet[]" |
| 238 |
== vec![ |
| 239 |
"127.0.0.1".parse::<sqlx::types::ipnetwork::IpNetwork>().unwrap(), |
| 240 |
"8.8.8.8/24".parse::<sqlx::types::ipnetwork::IpNetwork>().unwrap() |
| 241 |
] |
| 242 |
)); |
| 243 |
|
| 244 |
#[cfg(feature = "mac_address")] |
| 245 |
test_type!(mac_address_vec<Vec<sqlx::types::mac_address::MacAddress>>(Postgres, |
| 246 |
"'{01:02:03:04:05:06,FF:FF:FF:FF:FF:FF}'::macaddr[]" |
| 247 |
== vec![ |
| 248 |
"01:02:03:04:05:06".parse::<sqlx::types::mac_address::MacAddress>().unwrap(), |
| 249 |
"FF:FF:FF:FF:FF:FF".parse::<sqlx::types::mac_address::MacAddress>().unwrap() |
| 250 |
] |
| 251 |
)); |
| 252 |
|
| 253 |
#[cfg(feature = "chrono")] |
| 254 |
mod chrono { |
| 255 |
use super::*; |
| 256 |
use sqlx::types::chrono::{ |
| 257 |
DateTime, FixedOffset, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc, |
| 258 |
}; |
| 259 |
|
| 260 |
type PgTimeTz = sqlx::postgres::types::PgTimeTz<NaiveTime, FixedOffset>; |
| 261 |
|
| 262 |
test_type!(chrono_date<NaiveDate>(Postgres, |
| 263 |
"DATE '2001-01-05'" == NaiveDate::from_ymd_opt(2001, 1, 5).unwrap(), |
| 264 |
"DATE '2050-11-23'" == NaiveDate::from_ymd_opt(2050, 11, 23).unwrap() |
| 265 |
)); |
| 266 |
|
| 267 |
test_type!(chrono_time<NaiveTime>(Postgres, |
| 268 |
"TIME '05:10:20.115100'" == NaiveTime::from_hms_micro_opt(5, 10, 20, 115100).unwrap() |
| 269 |
)); |
| 270 |
|
| 271 |
test_type!(chrono_date_time<NaiveDateTime>(Postgres, |
| 272 |
"'2019-01-02 05:10:20'::timestamp" == NaiveDate::from_ymd_opt(2019, 1, 2).unwrap().and_hms_opt(5, 10, 20).unwrap() |
| 273 |
)); |
| 274 |
|
| 275 |
test_type!(chrono_date_time_vec<Vec<NaiveDateTime>>(Postgres, |
| 276 |
"array['2019-01-02 05:10:20']::timestamp[]" |
| 277 |
== vec![NaiveDate::from_ymd_opt(2019, 1, 2).unwrap().and_hms_opt(5, 10, 20).unwrap()] |
| 278 |
)); |
| 279 |
|
| 280 |
test_type!(chrono_date_time_tz_utc<DateTime::<Utc>>(Postgres, |
| 281 |
"TIMESTAMPTZ '2019-01-02 05:10:20.115100'" |
| 282 |
== Utc.from_utc_datetime( |
| 283 |
&NaiveDate::from_ymd_opt(2019, 1, 2).unwrap().and_hms_micro_opt(5, 10, 20, 115100).unwrap(), |
| 284 |
) |
| 285 |
)); |
| 286 |
|
| 287 |
test_type!(chrono_date_time_tz<DateTime::<FixedOffset>>(Postgres, |
| 288 |
"TIMESTAMPTZ '2019-01-02 05:10:20.115100+06:30'" |
| 289 |
== FixedOffset::east_opt(60 * 60 * 6 + 1800).unwrap().ymd(2019, 1, 2).and_hms_micro_opt(5, 10, 20, 115100).unwrap() |
| 290 |
)); |
| 291 |
|
| 292 |
test_type!(chrono_date_time_tz_vec<Vec<DateTime::<Utc>>>(Postgres, |
| 293 |
"array['2019-01-02 05:10:20.115100']::timestamptz[]" |
| 294 |
== vec![ |
| 295 |
Utc.from_utc_datetime( |
| 296 |
&NaiveDate::from_ymd_opt(2019, 1, 2).unwrap().and_hms_micro_opt(5, 10, 20, 115100).unwrap(), |
| 297 |
) |
| 298 |
] |
| 299 |
)); |
| 300 |
|
| 301 |
test_type!(chrono_time_tz<PgTimeTz>(Postgres, |
| 302 |
"TIMETZ '05:10:20.115100+00'" == PgTimeTz { time: NaiveTime::from_hms_micro_opt(5, 10, 20, 115100).unwrap(), offset: FixedOffset::east_opt(0).unwrap() }, |
| 303 |
"TIMETZ '05:10:20.115100+06:30'" == PgTimeTz { time: NaiveTime::from_hms_micro_opt(5, 10, 20, 115100).unwrap(), offset: FixedOffset::east_opt(60 * 60 * 6 + 1800).unwrap() }, |
| 304 |
"TIMETZ '05:10:20.115100-05'" == PgTimeTz { time: NaiveTime::from_hms_micro_opt(5, 10, 20, 115100).unwrap(), offset: FixedOffset::west_opt(60 * 60 * 5).unwrap() }, |
| 305 |
"TIMETZ '05:10:20+02'" == PgTimeTz { time: NaiveTime::from_hms_opt(5, 10, 20).unwrap(), offset: FixedOffset::east_opt(60 * 60 * 2 ).unwrap() } |
| 306 |
)); |
| 307 |
} |
| 308 |
|
| 309 |
#[cfg(feature = "time")] |
| 310 |
mod time_tests { |
| 311 |
use super::*; |
| 312 |
use sqlx::types::time::{Date, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset}; |
| 313 |
use time::macros::{date, time}; |
| 314 |
|
| 315 |
type PgTimeTz = sqlx::postgres::types::PgTimeTz<Time, UtcOffset>; |
| 316 |
|
| 317 |
test_type!(time_date<Date>( |
| 318 |
Postgres, |
| 319 |
"DATE '2001-01-05'" == date!(2001 - 1 - 5), |
| 320 |
"DATE '2050-11-23'" == date!(2050 - 11 - 23) |
| 321 |
)); |
| 322 |
|
| 323 |
test_type!(time_time<Time>( |
| 324 |
Postgres, |
| 325 |
"TIME '05:10:20.115100'" == time!(5:10:20.115100), |
| 326 |
"TIME '05:10:20'" == time!(5:10:20) |
| 327 |
)); |
| 328 |
|
| 329 |
test_type!(time_date_time<PrimitiveDateTime>( |
| 330 |
Postgres, |
| 331 |
"TIMESTAMP '2019-01-02 05:10:20'" == date!(2019 - 1 - 2).with_time(time!(5:10:20)), |
| 332 |
"TIMESTAMP '2019-01-02 05:10:20.1151'" == date!(2019 - 1 - 2).with_time(time!(5:10:20.115100)) |
| 333 |
)); |
| 334 |
|
| 335 |
test_type!(time_timestamp<OffsetDateTime>( |
| 336 |
Postgres, |
| 337 |
"TIMESTAMPTZ '2019-01-02 05:10:20.115100'" |
| 338 |
== date!(2019 - 1 - 2) |
| 339 |
.with_time(time!(5:10:20.115100)) |
| 340 |
.assume_utc() |
| 341 |
)); |
| 342 |
|
| 343 |
test_prepared_type!(time_time_tz<PgTimeTz>(Postgres, |
| 344 |
"TIMETZ '05:10:20.115100+00'" == PgTimeTz { time: time!(5:10:20.115100), offset: UtcOffset::from_whole_seconds(0).unwrap() }, |
| 345 |
"TIMETZ '05:10:20.115100+00'" == PgTimeTz { time: time!(5:10:20.115100), offset: UtcOffset::from_whole_seconds(0).unwrap() }, |
| 346 |
"TIMETZ '05:10:20.115100+06:30'" == PgTimeTz { time: time!(5:10:20.115100), offset: UtcOffset::from_whole_seconds(60 * 60 * 6 + 1800).unwrap() }, |
| 347 |
"TIMETZ '05:10:20.115100-05'" == PgTimeTz { time: time!(5:10:20.115100), offset: UtcOffset::from_whole_seconds(-(60 * 60 * 5)).unwrap() }, |
| 348 |
"TIMETZ '05:10:20+02'" == PgTimeTz { time: time!(5:10:20), offset: UtcOffset::from_whole_seconds(60 * 60 * 2 ).unwrap() } |
| 349 |
)); |
| 350 |
} |
| 351 |
|
| 352 |
#[cfg(feature = "json")] |
| 353 |
mod json { |
| 354 |
use super::*; |
| 355 |
use serde_json::value::RawValue as JsonRawValue; |
| 356 |
use serde_json::{json, Value as JsonValue}; |
| 357 |
use sqlx::postgres::PgRow; |
| 358 |
use sqlx::types::Json; |
| 359 |
use sqlx::{Executor, Row}; |
| 360 |
use sqlx_test::new; |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
test_type!(json<JsonValue>( |
| 366 |
Postgres, |
| 367 |
"SELECT ({0}::jsonb is not distinct from $1::jsonb)::int4, {0} as _2, $2 as _3", |
| 368 |
"'\"Hello, World\"'::json" == json!("Hello, World"), |
| 369 |
"'\"😎\"'::json" == json!("😎"), |
| 370 |
"'\"🙋♀️\"'::json" == json!("🙋♀️"), |
| 371 |
"'[\"Hello\", \"World!\"]'::json" == json!(["Hello", "World!"]) |
| 372 |
)); |
| 373 |
|
| 374 |
test_type!(json_vec<Vec<JsonValue>>( |
| 375 |
Postgres, |
| 376 |
"SELECT ({0}::jsonb[] is not distinct from $1::jsonb[])::int4, {0} as _2, $2 as _3", |
| 377 |
"array['\"😎\"'::json, '\"🙋♀️\"'::json]::json[]" == vec![json!("😎"), json!("🙋♀️")], |
| 378 |
)); |
| 379 |
|
| 380 |
test_type!(json_array<[JsonValue; 2]>( |
| 381 |
Postgres, |
| 382 |
"SELECT ({0}::jsonb[] is not distinct from $1::jsonb[])::int4, {0} as _2, $2 as _3", |
| 383 |
"array['\"😎\"'::json, '\"🙋♀️\"'::json]::json[]" == [json!("😎"), json!("🙋♀️")], |
| 384 |
)); |
| 385 |
|
| 386 |
test_type!(jsonb<JsonValue>( |
| 387 |
Postgres, |
| 388 |
"'\"Hello, World\"'::jsonb" == json!("Hello, World"), |
| 389 |
"'\"😎\"'::jsonb" == json!("😎"), |
| 390 |
"'\"🙋♀️\"'::jsonb" == json!("🙋♀️"), |
| 391 |
"'[\"Hello\", \"World!\"]'::jsonb" == json!(["Hello", "World!"]) |
| 392 |
)); |
| 393 |
|
| 394 |
test_type!(jsonb_array<Vec<JsonValue>>( |
| 395 |
Postgres, |
| 396 |
"array['\"😎\"'::jsonb, '\"🙋♀️\"'::jsonb]::jsonb[]" == vec![json!("😎"), json!("🙋♀️")], |
| 397 |
)); |
| 398 |
|
| 399 |
#[derive(serde::Deserialize, serde::Serialize, Debug, PartialEq)] |
| 400 |
struct Friend { |
| 401 |
name: String, |
| 402 |
age: u32, |
| 403 |
} |
| 404 |
|
| 405 |
test_type!(json_struct<Json<Friend>>(Postgres, |
| 406 |
"'{\"name\":\"Joe\",\"age\":33}'::jsonb" == Json(Friend { name: "Joe".to_string(), age: 33 }) |
| 407 |
)); |
| 408 |
|
| 409 |
test_type!(json_struct_vec<Vec<Json<Friend>>>(Postgres, |
| 410 |
"array['{\"name\":\"Joe\",\"age\":33}','{\"name\":\"Bob\",\"age\":22}']::jsonb[]" |
| 411 |
== vec![ |
| 412 |
Json(Friend { name: "Joe".to_string(), age: 33 }), |
| 413 |
Json(Friend { name: "Bob".to_string(), age: 22 }), |
| 414 |
] |
| 415 |
)); |
| 416 |
|
| 417 |
#[sqlx_macros::test] |
| 418 |
async fn test_json_raw_value() -> anyhow::Result<()> { |
| 419 |
let mut conn = new::<Postgres>().await?; |
| 420 |
|
| 421 |
|
| 422 |
let row: PgRow = conn |
| 423 |
.fetch_one("SELECT '{\"hello\": \"world\"}'::jsonb") |
| 424 |
.await?; |
| 425 |
|
| 426 |
let value: &JsonRawValue = row.try_get(0)?; |
| 427 |
|
| 428 |
assert_eq!(value.get(), "{\"hello\": \"world\"}"); |
| 429 |
|
| 430 |
|
| 431 |
let row: PgRow = conn |
| 432 |
.fetch_one(sqlx::query("SELECT '{\"hello\": \"world\"}'::jsonb")) |
| 433 |
.await?; |
| 434 |
|
| 435 |
let value: &JsonRawValue = row.try_get(0)?; |
| 436 |
|
| 437 |
assert_eq!(value.get(), "{\"hello\": \"world\"}"); |
| 438 |
|
| 439 |
Ok(()) |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
#[cfg(feature = "bigdecimal")] |
| 444 |
test_type!(bigdecimal<sqlx::types::BigDecimal>(Postgres, |
| 445 |
|
| 446 |
|
| 447 |
"0::numeric" == "0".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 448 |
|
| 449 |
"1::numeric" == "1".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 450 |
"10000::numeric" == "10000".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 451 |
"0.1::numeric" == "0.1".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 452 |
"0.01::numeric" == "0.01".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 453 |
"0.012::numeric" == "0.012".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 454 |
"0.0123::numeric" == "0.0123".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 455 |
"0.01234::numeric" == "0.01234".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 456 |
"0.012345::numeric" == "0.012345".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 457 |
"0.0123456::numeric" == "0.0123456".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 458 |
"0.01234567::numeric" == "0.01234567".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 459 |
"0.012345678::numeric" == "0.012345678".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 460 |
"0.0123456789::numeric" == "0.0123456789".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 461 |
"0.002::numeric" == "0.002".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 462 |
"0.0002::numeric" == "0.0002".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 463 |
"0.00002::numeric" == "0.00002".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 464 |
"0.000002::numeric" == "0.000002".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 465 |
"0.0000002::numeric" == "0.0000002".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 466 |
"0.00000002::numeric" == "0.00000002".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 467 |
"12.34::numeric" == "12.34".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 468 |
"12345.6789::numeric" == "12345.6789".parse::<sqlx::types::BigDecimal>().unwrap(), |
| 469 |
)); |
| 470 |
|
| 471 |
#[cfg(feature = "bigdecimal")] |
| 472 |
test_type!(numrange_bigdecimal<PgRange<sqlx::types::BigDecimal>>(Postgres, |
| 473 |
"'(1.3,2.4)'::numrange" == PgRange::from( |
| 474 |
(Bound::Excluded("1.3".parse::<sqlx::types::BigDecimal>().unwrap()), |
| 475 |
Bound::Excluded("2.4".parse::<sqlx::types::BigDecimal>().unwrap()))) |
| 476 |
)); |
| 477 |
|
| 478 |
#[cfg(any(postgres_14, postgres_15))] |
| 479 |
test_type!(cube<sqlx::postgres::types::PgCube>(Postgres, |
| 480 |
"cube(2)" == sqlx::postgres::types::PgCube::Point(2.), |
| 481 |
"cube(2.1)" == sqlx::postgres::types::PgCube::Point(2.1), |
| 482 |
"cube(2,3)" == sqlx::postgres::types::PgCube::OneDimensionInterval(2., 3.), |
| 483 |
"cube(2.2,-3.4)" == sqlx::postgres::types::PgCube::OneDimensionInterval(2.2, -3.4), |
| 484 |
"cube(array[2,3])" == sqlx::postgres::types::PgCube::ZeroVolume(vec![2., 3.]), |
| 485 |
"cube(array[2,3],array[4,5])" == sqlx::postgres::types::PgCube::MultiDimension(vec![vec![2.,3.],vec![4.,5.]]), |
| 486 |
"cube(array[2,3,4],array[4,5,6])" == sqlx::postgres::types::PgCube::MultiDimension(vec![vec![2.,3.,4.],vec![4.,5.,6.]]), |
| 487 |
)); |
| 488 |
|
| 489 |
#[cfg(any(postgres_14, postgres_15))] |
| 490 |
test_type!(_cube<Vec<sqlx::postgres::types::PgCube>>(Postgres, |
| 491 |
"array[cube(2),cube(2)]" == vec![sqlx::postgres::types::PgCube::Point(2.), sqlx::postgres::types::PgCube::Point(2.)], |
| 492 |
"array[cube(2.2,-3.4)]" == vec![sqlx::postgres::types::PgCube::OneDimensionInterval(2.2, -3.4)], |
| 493 |
)); |
| 494 |
|
| 495 |
#[cfg(any(postgres_12, postgres_13, postgres_14, postgres_15))] |
| 496 |
test_type!(point<sqlx::postgres::types::PgPoint>(Postgres, |
| 497 |
"point(2.2,-3.4)" ~= sqlx::postgres::types::PgPoint { x: 2.2, y:-3.4 }, |
| 498 |
)); |
| 499 |
|
| 500 |
#[cfg(any(postgres_12, postgres_13, postgres_14, postgres_15))] |
| 501 |
test_type!(_point<Vec<sqlx::postgres::types::PgPoint>>(Postgres, |
| 502 |
"array[point(2,3),point(2.1,3.4)]" @= vec![sqlx::postgres::types::PgPoint { x:2., y: 3. }, sqlx::postgres::types::PgPoint { x:2.1, y: 3.4 }], |
| 503 |
"array[point(2.2,-3.4)]" @= vec![sqlx::postgres::types::PgPoint { x: 2.2, y: -3.4 }], |
| 504 |
)); |
| 505 |
|
| 506 |
#[cfg(any(postgres_12, postgres_13, postgres_14, postgres_15))] |
| 507 |
test_type!(line<sqlx::postgres::types::PgLine>(Postgres, |
| 508 |
"line('{1.1, -2.2, 3.3}')" == sqlx::postgres::types::PgLine { a: 1.1, b:-2.2, c: 3.3 }, |
| 509 |
"line('((0.0, 0.0), (1.0,1.0))')" == sqlx::postgres::types::PgLine { a: 1., b: -1., c: 0. }, |
| 510 |
)); |
| 511 |
|
| 512 |
#[cfg(feature = "rust_decimal")] |
| 513 |
test_type!(decimal<sqlx::types::Decimal>(Postgres, |
| 514 |
"0::numeric" == sqlx::types::Decimal::from_str("0").unwrap(), |
| 515 |
"1::numeric" == sqlx::types::Decimal::from_str("1").unwrap(), |
| 516 |
"10000::numeric" == sqlx::types::Decimal::from_str("10000").unwrap(), |
| 517 |
"0.1::numeric" == sqlx::types::Decimal::from_str("0.1").unwrap(), |
| 518 |
"0.01234::numeric" == sqlx::types::Decimal::from_str("0.01234").unwrap(), |
| 519 |
"12.34::numeric" == sqlx::types::Decimal::from_str("12.34").unwrap(), |
| 520 |
"12345.6789::numeric" == sqlx::types::Decimal::from_str("12345.6789").unwrap(), |
| 521 |
|
| 522 |
"17.905625985174584660842500258::numeric" == sqlx::types::Decimal::from_str("17.905625985174584660842500258").unwrap(), |
| 523 |
"-17.905625985174584660842500258::numeric" == sqlx::types::Decimal::from_str("-17.905625985174584660842500258").unwrap(), |
| 524 |
)); |
| 525 |
|
| 526 |
#[cfg(feature = "rust_decimal")] |
| 527 |
test_type!(numrange_decimal<PgRange<sqlx::types::Decimal>>(Postgres, |
| 528 |
"'(1.3,2.4)'::numrange" == PgRange::from( |
| 529 |
(Bound::Excluded(sqlx::types::Decimal::from_str("1.3").unwrap()), |
| 530 |
Bound::Excluded(sqlx::types::Decimal::from_str("2.4").unwrap()))), |
| 531 |
)); |
| 532 |
|
| 533 |
const EXC2: Bound<i32> = Bound::Excluded(2); |
| 534 |
const EXC3: Bound<i32> = Bound::Excluded(3); |
| 535 |
const INC1: Bound<i32> = Bound::Included(1); |
| 536 |
const INC2: Bound<i32> = Bound::Included(2); |
| 537 |
const UNB: Bound<i32> = Bound::Unbounded; |
| 538 |
|
| 539 |
test_type!(int4range<PgRange<i32>>(Postgres, |
| 540 |
"'(,)'::int4range" == PgRange::from((UNB, UNB)), |
| 541 |
"'(,]'::int4range" == PgRange::from((UNB, UNB)), |
| 542 |
"'(,2)'::int4range" == PgRange::from((UNB, EXC2)), |
| 543 |
"'(,2]'::int4range" == PgRange::from((UNB, EXC3)), |
| 544 |
"'(1,)'::int4range" == PgRange::from((INC2, UNB)), |
| 545 |
"'(1,]'::int4range" == PgRange::from((INC2, UNB)), |
| 546 |
"'(1,2]'::int4range" == PgRange::from((INC2, EXC3)), |
| 547 |
"'[,)'::int4range" == PgRange::from((UNB, UNB)), |
| 548 |
"'[,]'::int4range" == PgRange::from((UNB, UNB)), |
| 549 |
"'[,2)'::int4range" == PgRange::from((UNB, EXC2)), |
| 550 |
"'[,2]'::int4range" == PgRange::from((UNB, EXC3)), |
| 551 |
"'[1,)'::int4range" == PgRange::from((INC1, UNB)), |
| 552 |
"'[1,]'::int4range" == PgRange::from((INC1, UNB)), |
| 553 |
"'[1,2)'::int4range" == PgRange::from((INC1, EXC2)), |
| 554 |
"'[1,2]'::int4range" == PgRange::from((INC1, EXC3)), |
| 555 |
)); |
| 556 |
|
| 557 |
test_prepared_type!(interval<PgInterval>( |
| 558 |
Postgres, |
| 559 |
"INTERVAL '1h'" |
| 560 |
== PgInterval { |
| 561 |
months: 0, |
| 562 |
days: 0, |
| 563 |
microseconds: 3_600_000_000 |
| 564 |
}, |
| 565 |
"INTERVAL '-1 hours'" |
| 566 |
== PgInterval { |
| 567 |
months: 0, |
| 568 |
days: 0, |
| 569 |
microseconds: -3_600_000_000 |
| 570 |
}, |
| 571 |
"INTERVAL '3 months 12 days 1h 15 minutes 10 second '" |
| 572 |
== PgInterval { |
| 573 |
months: 3, |
| 574 |
days: 12, |
| 575 |
microseconds: (3_600 + 15 * 60 + 10) * 1_000_000 |
| 576 |
}, |
| 577 |
"INTERVAL '03:10:20.116100'" |
| 578 |
== PgInterval { |
| 579 |
months: 0, |
| 580 |
days: 0, |
| 581 |
microseconds: (3 * 3_600 + 10 * 60 + 20) * 1_000_000 + 116100 |
| 582 |
}, |
| 583 |
)); |
| 584 |
|
| 585 |
test_prepared_type!(money<PgMoney>(Postgres, "123.45::money" == PgMoney(12345))); |
| 586 |
|
| 587 |
test_prepared_type!(money_vec<Vec<PgMoney>>(Postgres, |
| 588 |
"array[123.45,420.00,666.66]::money[]" == vec![PgMoney(12345), PgMoney(42000), PgMoney(66666)], |
| 589 |
)); |
| 590 |
|
| 591 |
test_prepared_type!(citext_array<Vec<PgCiText>>(Postgres, |
| 592 |
"array['one','two','three']::citext[]" == vec![ |
| 593 |
PgCiText("one".to_string()), |
| 594 |
PgCiText("two".to_string()), |
| 595 |
PgCiText("three".to_string()), |
| 596 |
], |
| 597 |
)); |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
#[cfg(any(postgres_14, postgres_15))] |
| 602 |
test_type!(ltree<sqlx::postgres::types::PgLTree>(Postgres, |
| 603 |
"'Foo.Bar.Baz.Quux'::ltree" == sqlx::postgres::types::PgLTree::from_str("Foo.Bar.Baz.Quux").unwrap(), |
| 604 |
"'Alpha.Beta.Delta.Gamma'::ltree" == sqlx::postgres::types::PgLTree::try_from_iter(["Alpha", "Beta", "Delta", "Gamma"]).unwrap(), |
| 605 |
)); |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
#[cfg(any(postgres_14, postgres_15))] |
| 610 |
test_type!(ltree_vec<Vec<sqlx::postgres::types::PgLTree>>(Postgres, |
| 611 |
"array['Foo.Bar.Baz.Quux', 'Alpha.Beta.Delta.Gamma']::ltree[]" == |
| 612 |
vec![ |
| 613 |
sqlx::postgres::types::PgLTree::from_str("Foo.Bar.Baz.Quux").unwrap(), |
| 614 |
sqlx::postgres::types::PgLTree::try_from_iter(["Alpha", "Beta", "Delta", "Gamma"]).unwrap() |
| 615 |
] |
| 616 |
)); |
| 617 |
|
| 618 |
#[sqlx_macros::test] |
| 619 |
async fn test_text_adapter() -> anyhow::Result<()> { |
| 620 |
#[derive(sqlx::FromRow, Debug, PartialEq, Eq)] |
| 621 |
struct Login { |
| 622 |
user_id: i32, |
| 623 |
socket_addr: Text<SocketAddr>, |
| 624 |
#[cfg(feature = "time")] |
| 625 |
login_at: time::OffsetDateTime, |
| 626 |
} |
| 627 |
|
| 628 |
let mut conn = new::<Postgres>().await?; |
| 629 |
|
| 630 |
conn.execute( |
| 631 |
r#" |
| 632 |
CREATE TEMPORARY TABLE user_login ( |
| 633 |
user_id INT PRIMARY KEY, |
| 634 |
socket_addr TEXT NOT NULL, |
| 635 |
login_at TIMESTAMPTZ NOT NULL DEFAULT NOW() |
| 636 |
); |
| 637 |
"#, |
| 638 |
) |
| 639 |
.await?; |
| 640 |
|
| 641 |
let user_id = 1234; |
| 642 |
let socket_addr: SocketAddr = "198.51.100.47:31790".parse().unwrap(); |
| 643 |
|
| 644 |
sqlx::query("INSERT INTO user_login (user_id, socket_addr) VALUES ($1, $2)") |
| 645 |
.bind(user_id) |
| 646 |
.bind(Text(socket_addr)) |
| 647 |
.execute(&mut conn) |
| 648 |
.await?; |
| 649 |
|
| 650 |
let last_login: Login = |
| 651 |
sqlx::query_as("SELECT * FROM user_login ORDER BY login_at DESC LIMIT 1") |
| 652 |
.fetch_one(&mut conn) |
| 653 |
.await?; |
| 654 |
|
| 655 |
assert_eq!(last_login.user_id, user_id); |
| 656 |
assert_eq!(*last_login.socket_addr, socket_addr); |
| 657 |
|
| 658 |
Ok(()) |
| 659 |
} |