/
lib.rs
216 lines (188 loc) · 5.04 KB
/
lib.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#[macro_use]
extern crate diesel;
use std::time::SystemTime;
#[cfg(test)]
use diesel::debug_query;
#[cfg(test)]
use diesel::pg::Pg;
use diesel::prelude::*;
table! {
posts {
id -> BigInt,
title -> Text,
body -> Text,
draft -> Bool,
publish_at -> Timestamp,
visit_count -> Integer,
}
}
#[derive(Queryable, Identifiable, AsChangeset)]
pub struct Post {
pub id: i64,
pub title: String,
pub body: String,
pub draft: bool,
pub publish_at: SystemTime,
pub visit_count: i32,
}
pub fn publish_all_posts(conn: &PgConnection) -> QueryResult<usize> {
use posts::dsl::*;
diesel::update(posts).set(draft.eq(false)).execute(conn)
}
#[test]
fn examine_sql_from_publish_all_posts() {
use posts::dsl::*;
assert_eq!(
"UPDATE \"posts\" SET \"draft\" = $1 -- binds: [false]",
debug_query(&diesel::update(posts).set(draft.eq(false))).to_string()
);
}
pub fn publish_pending_posts(conn: &PgConnection) -> QueryResult<usize> {
use diesel::dsl::now;
use posts::dsl::*;
diesel::update(posts)
.filter(publish_at.lt(now))
.set(draft.eq(false))
.execute(conn)
}
#[test]
fn examine_sql_from_publish_pending_posts() {
use diesel::dsl::now;
use posts::dsl::*;
let query = diesel::update(posts)
.filter(publish_at.lt(now))
.set(draft.eq(false));
assert_eq!(
"UPDATE \"posts\" SET \"draft\" = $1 \
WHERE \"posts\".\"publish_at\" < CURRENT_TIMESTAMP \
-- binds: [false]",
debug_query(&query).to_string()
);
}
pub fn publish_post(post: &Post, conn: &PgConnection) -> QueryResult<usize> {
diesel::update(post)
.set(posts::draft.eq(false))
.execute(conn)
}
#[test]
fn examine_sql_from_publish_post() {
let post = Post {
id: 1,
title: "".into(),
body: "".into(),
draft: false,
publish_at: SystemTime::now(),
visit_count: 0,
};
assert_eq!(
"UPDATE \"posts\" SET \"draft\" = $1 WHERE \"posts\".\"id\" = $2 \
-- binds: [false, 1]",
debug_query(&diesel::update(&post).set(posts::draft.eq(false))).to_string()
);
}
pub fn increment_visit_counts(conn: &PgConnection) -> QueryResult<usize> {
use posts::dsl::*;
diesel::update(posts)
.set(visit_count.eq(visit_count + 1))
.execute(conn)
}
#[test]
fn examine_sql_from_increment_visit_counts() {
use posts::dsl::*;
assert_eq!(
"UPDATE \"posts\" SET \"visit_count\" = \"posts\".\"visit_count\" + $1 \
-- binds: [1]",
debug_query::<Pg, _>(&diesel::update(posts).set(visit_count.eq(visit_count + 1)))
.to_string()
);
}
pub fn hide_everything(conn: &PgConnection) -> QueryResult<usize> {
use posts::dsl::*;
diesel::update(posts)
.set((
title.eq("[REDACTED]"),
body.eq("This post has been classified"),
))
.execute(conn)
}
#[test]
fn examine_sql_from_hide_everything() {
use posts::dsl::*;
let query = diesel::update(posts).set((
title.eq("[REDACTED]"),
body.eq("This post has been classified"),
));
assert_eq!(
"UPDATE \"posts\" SET \"title\" = $1, \"body\" = $2 \
-- binds: [\"[REDACTED]\", \"This post has been classified\"]",
debug_query::<Pg, _>(&query).to_string()
);
}
pub fn update_from_post_fields(post: &Post, conn: &PgConnection) -> QueryResult<usize> {
diesel::update(posts::table).set(post).execute(conn)
}
#[test]
fn examine_sql_from_update_post_fields() {
let now = SystemTime::now();
let post = Post {
id: 1,
title: "".into(),
body: "".into(),
draft: false,
publish_at: now,
visit_count: 0,
};
let sql = format!(
"UPDATE \"posts\" SET \
\"title\" = $1, \
\"body\" = $2, \
\"draft\" = $3, \
\"publish_at\" = $4, \
\"visit_count\" = $5 \
-- binds: [\
\"\", \
\"\", \
false, \
{:?}, \
0\
]",
now
);
assert_eq!(
sql,
debug_query(&diesel::update(posts::table).set(&post)).to_string()
);
}
pub fn update_with_option(conn: &PgConnection) -> QueryResult<usize> {
#[derive(AsChangeset)]
#[table_name = "posts"]
struct PostForm<'a> {
title: Option<&'a str>,
body: Option<&'a str>,
}
diesel::update(posts::table)
.set(&PostForm {
title: None,
body: Some("My new post"),
})
.execute(conn)
}
#[test]
fn examine_sql_from_update_with_option() {
#[derive(AsChangeset)]
#[table_name = "posts"]
struct PostForm<'a> {
title: Option<&'a str>,
body: Option<&'a str>,
}
let post_form = PostForm {
title: None,
body: Some("My new post"),
};
let query = diesel::update(posts::table).set(&post_form);
assert_eq!(
"UPDATE \"posts\" SET \"body\" = $1 \
-- binds: [\"My new post\"]",
debug_query::<Pg, _>(&query).to_string()
);
}