-
Notifications
You must be signed in to change notification settings - Fork 6
/
str_auto_owned.rs
196 lines (174 loc) · 5.37 KB
/
str_auto_owned.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use crate::util::connect_localhost;
use chrono::NaiveDateTime;
use tiberius::Uuid;
use tiberius_derive::FromRow;
mod util;
#[allow(non_snake_case)]
#[derive(FromRow, Debug, Clone, PartialEq)]
#[tiberius_derive(auto)]
struct TestRow {
pub Id: i32,
pub VarCharRow: String,
pub NVarCharRow: String,
pub UuidRow: Uuid,
pub LongRow: i64,
pub DateTimeRow: chrono::NaiveDateTime,
pub SmallIntRow: i16,
pub BitRow: bool,
pub FloatRow: f32,
pub DoubleRow: f64,
pub RealRow: f32,
}
#[allow(non_snake_case)]
#[derive(FromRow, Debug, Clone, PartialEq)]
#[tiberius_derive(auto, by_position)]
struct TestRowByPositionAuto {
pub Id: i32,
pub VarCharRow: String,
pub NVarCharRow: String,
pub UuidRow: Uuid,
pub LongRow: i64,
pub DateTimeRow: chrono::NaiveDateTime,
pub SmallIntRow: i16,
pub BitRow: bool,
pub FloatRow: f32,
pub DoubleRow: f64,
pub RealRow: f32,
}
#[allow(non_snake_case)]
#[derive(FromRow, Debug, Clone, PartialEq, Default)]
#[tiberius_derive(auto)]
struct TestRowNullable {
pub Id: i32,
pub VarCharRow: Option<String>,
pub NVarCharRow: Option<String>,
pub UuidRow: Option<Uuid>,
pub LongRow: Option<i64>,
pub DateTimeRow: Option<chrono::NaiveDateTime>,
pub SmallIntRow: Option<i16>,
pub BitRow: Option<bool>,
pub FloatRow: Option<f32>,
// pub DoubleRow: Option<f64>, // borked. Breaks because tiberius inteprets a a nullable float field as F32(None)
pub RealRow: Option<f32>,
}
#[tokio::test]
async fn by_ref_auto_not_null() -> Result<(), tiberius::error::Error> {
let mut client = connect_localhost().await.unwrap();
let query = r"
SELECT
[Id],[VarCharRow],[NVarCharRow],[UuidRow],[LongRow],[DateTimeRow],[SmallIntRow],[BitRow],[FloatRow],[DoubleRow],[RealRow]
FROM
[TiberiusDeriveTest].[dbo].[TestRow]
WHERE VarCharRow is not null
ORDER BY ID
";
let rows = client
.simple_query(query)
.await?
.into_first_result()
.await?;
let rows = rows
.iter()
.map(TestRow::from_row)
.collect::<Result<Vec<_>, _>>()?;
let expected_time =
NaiveDateTime::parse_from_str("2021-01-01 00:00:00", "%Y-%m-%d %H:%M:%S").unwrap();
let expected = TestRow {
Id: 1,
VarCharRow: "varchar".to_owned(),
NVarCharRow: "nvarchar".to_owned(),
LongRow: 9999999999999999,
SmallIntRow: 2,
BitRow: true,
FloatRow: 10.123123125,
DoubleRow: 99.1231231258,
RealRow: 10.5,
UuidRow: Uuid::parse_str("89e022ce-d3b6-43a7-a359-4618571487a6").unwrap(),
DateTimeRow: expected_time,
};
assert_eq!(&expected, rows.first().unwrap());
Ok(())
}
#[tokio::test]
async fn by_ref_by_position_auto_not_null() -> Result<(), tiberius::error::Error> {
let mut client = connect_localhost().await.unwrap();
let query = r"
SELECT
[Id],[VarCharRow],[NVarCharRow],[UuidRow],[LongRow],[DateTimeRow],[SmallIntRow],[BitRow],[FloatRow],[DoubleRow],[RealRow]
FROM
[TiberiusDeriveTest].[dbo].[TestRow]
WHERE VarCharRow is not null
ORDER BY ID
";
let rows = client
.simple_query(query)
.await?
.into_first_result()
.await?;
let rows = rows
.iter()
.map(TestRowByPositionAuto::from_row)
.collect::<Result<Vec<_>, _>>()?;
let expected_time =
NaiveDateTime::parse_from_str("2021-01-01 00:00:00", "%Y-%m-%d %H:%M:%S").unwrap();
let expected = TestRowByPositionAuto {
Id: 1,
VarCharRow: "varchar".to_owned(),
NVarCharRow: "nvarchar".to_owned(),
LongRow: 9999999999999999,
SmallIntRow: 2,
BitRow: true,
FloatRow: 10.123123125,
DoubleRow: 99.1231231258,
RealRow: 10.5,
UuidRow: Uuid::parse_str("89e022ce-d3b6-43a7-a359-4618571487a6").unwrap(),
DateTimeRow: expected_time,
};
assert_eq!(&expected, rows.first().unwrap());
Ok(())
}
#[tokio::test]
async fn by_ref_auto_nullable() -> Result<(), tiberius::error::Error> {
let mut client = connect_localhost().await.unwrap();
let query = r"
SELECT
[Id],[VarCharRow],[NVarCharRow],[UuidRow],[LongRow],[DateTimeRow],[SmallIntRow],[BitRow],[FloatRow],
-- [DoubleRow],
[RealRow]
FROM
[TiberiusDeriveTest].[dbo].[TestRow]
ORDER BY ID
";
let rows = client
.simple_query(query)
.await?
.into_first_result()
.await?;
let rows = rows
.iter()
.map(TestRowNullable::from_row)
.collect::<Result<Vec<_>, _>>()?;
let expected_time =
NaiveDateTime::parse_from_str("2021-01-01 00:00:00", "%Y-%m-%d %H:%M:%S").unwrap();
let expected_1 = TestRowNullable {
Id: 1,
VarCharRow: "varchar".to_owned().into(),
NVarCharRow: "nvarchar".to_owned().into(),
LongRow: 9999999999999999.into(),
SmallIntRow: 2.into(),
BitRow: true.into(),
FloatRow: 10.123123125.into(),
RealRow: 10.5.into(),
UuidRow: Uuid::parse_str("89e022ce-d3b6-43a7-a359-4618571487a6")
.unwrap()
.into(),
DateTimeRow: expected_time.into(),
};
let expected_2 = TestRowNullable {
Id: 2,
..TestRowNullable::default()
};
assert_eq!(expected_1, rows[0]);
assert_eq!(expected_2, rows[1]);
Ok(())
}