-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
types.rs
148 lines (119 loc) · 4.74 KB
/
types.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use sqlx::sqlite::{Sqlite, SqliteRow};
use sqlx_core::row::Row;
use sqlx_test::new;
use sqlx_test::test_type;
test_type!(null<Option<i32>>(Sqlite,
"NULL" == None::<i32>
));
test_type!(bool(Sqlite, "FALSE" == false, "TRUE" == true));
test_type!(i32(Sqlite, "94101" == 94101_i32));
test_type!(i64(Sqlite, "9358295312" == 9358295312_i64));
// NOTE: This behavior can be surprising. Floating-point parameters are widening to double which can
// result in strange rounding.
test_type!(f32(Sqlite, "3.1410000324249268" == 3.141f32 as f64 as f32));
test_type!(f64(Sqlite, "939399419.1225182" == 939399419.1225182_f64));
test_type!(str<String>(Sqlite,
"'this is foo'" == "this is foo",
"cast(x'7468697320006973206E756C2D636F6E7461696E696E67' as text)" == "this \0is nul-containing",
"''" == ""
));
test_type!(bytes<Vec<u8>>(Sqlite,
"X'DEADBEEF'"
== vec![0xDE_u8, 0xAD, 0xBE, 0xEF],
"X''"
== Vec::<u8>::new(),
"X'0000000052'"
== vec![0_u8, 0, 0, 0, 0x52]
));
#[cfg(feature = "json")]
mod json_tests {
use super::*;
use serde_json::{json, Value as JsonValue};
use sqlx::types::Json;
use sqlx_test::test_type;
test_type!(json<JsonValue>(
Sqlite,
"'\"Hello, World\"'" == json!("Hello, World"),
"'\"😎\"'" == json!("😎"),
"'\"🙋♀️\"'" == json!("🙋♀️"),
"'[\"Hello\",\"World!\"]'" == json!(["Hello", "World!"])
));
#[derive(serde::Deserialize, serde::Serialize, Debug, PartialEq)]
struct Friend {
name: String,
age: u32,
}
test_type!(json_struct<Json<Friend>>(
Sqlite,
"\'{\"name\":\"Joe\",\"age\":33}\'" == Json(Friend { name: "Joe".to_string(), age: 33 })
));
// NOTE: This is testing recursive (and transparent) usage of the `Json` wrapper. You don't
// need to wrap the Vec in Json<_> to make the example work.
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
struct Customer {
json_column: Json<Vec<i64>>,
}
test_type!(json_struct_json_column<Json<Customer>>(
Sqlite,
"\'{\"json_column\":[1,2]}\'" == Json(Customer { json_column: Json(vec![1, 2]) })
));
#[sqlx_macros::test]
async fn it_json_extracts() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let value = sqlx::query("select JSON_EXTRACT(JSON('{ \"number\": 42 }'), '$.number') = ?1")
.bind(42_i32)
.try_map(|row: SqliteRow| row.try_get::<bool, _>(0))
.fetch_one(&mut conn)
.await?;
assert_eq!(true, value);
Ok(())
}
}
#[cfg(feature = "chrono")]
mod chrono {
use super::*;
use sqlx::types::chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime, TimeZone, Utc};
test_type!(chrono_naive_date_time<NaiveDateTime>(Sqlite, "SELECT datetime({0}) is datetime(?), {0}, ?",
"'2019-01-02 05:10:20'" == NaiveDate::from_ymd(2019, 1, 2).and_hms(5, 10, 20)
));
test_type!(chrono_date_time_utc<DateTime::<Utc>>(Sqlite, "SELECT datetime({0}) is datetime(?), {0}, ?",
"'1996-12-20T00:39:57+00:00'" == Utc.ymd(1996, 12, 20).and_hms(0, 39, 57)
));
test_type!(chrono_date_time_fixed_offset<DateTime::<FixedOffset>>(Sqlite, "SELECT datetime({0}) is datetime(?), {0}, ?",
"'2016-11-08T03:50:23-05:00'" == DateTime::<Utc>::from(FixedOffset::west(5 * 3600).ymd(2016, 11, 08).and_hms(3, 50, 23))
));
}
#[cfg(feature = "bstr")]
mod bstr {
use super::*;
use sqlx::types::bstr::BString;
test_type!(bstring<BString>(Sqlite,
"cast('abc123' as blob)" == BString::from(&b"abc123"[..]),
"x'0001020304'" == BString::from(&b"\x00\x01\x02\x03\x04"[..])
));
}
#[cfg(feature = "git2")]
mod git2 {
use super::*;
use sqlx::types::git2::Oid;
test_type!(oid<Oid>(
Sqlite,
"x'0000000000000000000000000000000000000000'" == Oid::zero(),
"x'000102030405060708090a0b0c0d0e0f10111213'"
== Oid::from_str("000102030405060708090a0b0c0d0e0f10111213").unwrap()
));
}
#[cfg(feature = "uuid")]
test_type!(uuid<sqlx::types::Uuid>(Sqlite,
"x'b731678f636f4135bc6f19440c13bd19'"
== sqlx::types::Uuid::parse_str("b731678f-636f-4135-bc6f-19440c13bd19").unwrap(),
"x'00000000000000000000000000000000'"
== sqlx::types::Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap()
));
#[cfg(feature = "uuid")]
test_type!(uuid_hyphenated<sqlx::types::uuid::adapter::Hyphenated>(Sqlite,
"'b731678f-636f-4135-bc6f-19440c13bd19'"
== sqlx::types::Uuid::parse_str("b731678f-636f-4135-bc6f-19440c13bd19").unwrap().to_hyphenated(),
"'00000000-0000-0000-0000-000000000000'"
== sqlx::types::Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap().to_hyphenated()
));