forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotations.rs
More file actions
339 lines (291 loc) · 7.96 KB
/
Copy pathannotations.rs
File metadata and controls
339 lines (291 loc) · 7.96 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use diesel::*;
use schema::*;
#[test]
fn association_where_struct_name_doesnt_match_table_name() {
#[derive(PartialEq, Eq, Debug, Clone, Queryable, Identifiable, Associations)]
#[belongs_to(Post)]
#[table_name = "comments"]
struct OtherComment {
id: i32,
post_id: i32,
}
let connection = connection_with_sean_and_tess_in_users_table();
let sean = find_user_by_name("Sean", &connection);
insert_into(posts::table)
.values(&sean.new_post("Hello", None))
.execute(&connection)
.unwrap();
let post = posts::table.first::<Post>(&connection).unwrap();
insert_into(comments::table)
.values(&NewComment(post.id, "comment"))
.execute(&connection)
.unwrap();
let comment_text = OtherComment::belonging_to(&post)
.select(comments::text)
.first::<String>(&connection);
assert_eq!(Ok("comment".into()), comment_text);
}
#[test]
#[cfg(not(any(feature = "sqlite", feature = "mysql")))]
fn association_where_parent_and_child_have_underscores() {
#[derive(PartialEq, Eq, Debug, Clone, Queryable, Identifiable, Associations)]
#[belongs_to(User)]
pub struct SpecialPost {
id: i32,
user_id: i32,
title: String,
}
#[derive(Insertable)]
#[table_name = "special_posts"]
struct NewSpecialPost {
user_id: i32,
title: String,
}
impl SpecialPost {
fn new(user_id: i32, title: &str) -> NewSpecialPost {
NewSpecialPost {
user_id: user_id,
title: title.to_owned(),
}
}
}
#[derive(PartialEq, Eq, Debug, Clone, Queryable, Identifiable, Associations)]
#[belongs_to(SpecialPost)]
struct SpecialComment {
id: i32,
special_post_id: i32,
}
impl SpecialComment {
fn new(special_post_id: i32) -> NewSpecialComment {
NewSpecialComment {
special_post_id: special_post_id,
}
}
}
#[derive(Insertable)]
#[table_name = "special_comments"]
struct NewSpecialComment {
special_post_id: i32,
}
let connection = connection_with_sean_and_tess_in_users_table();
let sean = find_user_by_name("Sean", &connection);
let new_post = SpecialPost::new(sean.id, "title");
let special_post: SpecialPost = insert_into(special_posts::table)
.values(&new_post)
.get_result(&connection)
.unwrap();
let new_comment = SpecialComment::new(special_post.id);
insert_into(special_comments::table)
.values(&new_comment)
.execute(&connection)
.unwrap();
let comment: SpecialComment = SpecialComment::belonging_to(&special_post)
.first(&connection)
.unwrap();
assert_eq!(special_post.id, comment.special_post_id);
}
// This module has no test functions, as it's only to test compilation.
mod associations_can_have_nullable_foreign_keys {
#![allow(dead_code)]
table! {
foos{
id -> Integer,
}
}
table! {
bars {
id -> Integer,
foo_id -> Nullable<Integer>,
}
}
// This test has no assertions, as it is for compilation purposes only.
#[derive(Identifiable)]
pub struct Foo {
id: i32,
}
#[belongs_to(Foo)]
#[derive(Identifiable, Associations)]
pub struct Bar {
id: i32,
foo_id: Option<i32>,
}
}
// This module has no test functions, as it's only to test compilation.
mod multiple_lifetimes_in_insertable_struct_definition {
#![allow(dead_code)]
use schema::posts;
#[derive(Insertable)]
#[table_name = "posts"]
pub struct MyPost<'a> {
title: &'a str,
body: &'a str,
}
}
mod lifetimes_with_names_other_than_a {
#![allow(dead_code)]
use schema::posts;
#[derive(Insertable)]
#[table_name = "posts"]
pub struct MyPost<'a, 'b> {
id: i32,
title: &'b str,
body: &'a str,
}
}
mod insertable_with_cow {
#![allow(dead_code)]
use schema::posts;
use std::borrow::Cow;
#[derive(Insertable)]
#[table_name = "posts"]
pub struct MyPost<'a> {
id: i32,
title: Cow<'a, str>,
body: Cow<'a, str>,
}
}
mod custom_foreign_keys_are_respected_on_belongs_to {
#![allow(dead_code)]
use schema::User;
table! { special_posts { id -> Integer, author_id -> Integer, } }
#[derive(Identifiable, Associations)]
#[belongs_to(User, foreign_key = "author_id")]
pub struct SpecialPost {
id: i32,
author_id: i32,
}
}
mod derive_identifiable_with_lifetime {
#![allow(dead_code)]
use schema::posts;
#[derive(Identifiable)]
pub struct Post<'a> {
id: &'a i32,
}
}
#[test]
fn derive_identifiable_with_non_standard_pk() {
use diesel::associations::*;
#[derive(Identifiable)]
#[table_name = "posts"]
#[primary_key(foo_id)]
#[allow(dead_code)]
struct Foo<'a> {
id: i32,
foo_id: &'a str,
foo: i32,
}
let foo1 = Foo {
id: 1,
foo_id: "hi",
foo: 2,
};
let foo2 = Foo {
id: 2,
foo_id: "there",
foo: 3,
};
assert_eq!(&"hi", foo1.id());
assert_eq!(&"there", foo2.id());
// Fails to compile if wrong table is generated.
let _: posts::table = Foo::<'static>::table();
}
#[test]
fn derive_identifiable_with_composite_pk() {
use diesel::associations::Identifiable;
#[derive(Identifiable)]
#[primary_key(foo_id, bar_id)]
#[table_name = "posts"]
#[allow(dead_code)]
struct Foo {
id: i32,
foo_id: i32,
bar_id: i32,
foo: i32,
}
let foo1 = Foo {
id: 1,
foo_id: 2,
bar_id: 3,
foo: 4,
};
let foo2 = Foo {
id: 5,
foo_id: 6,
bar_id: 7,
foo: 8,
};
assert_eq!((&2, &3), foo1.id());
assert_eq!((&6, &7), foo2.id());
}
#[test]
fn derive_insertable_with_option_for_not_null_field_with_default() {
#[derive(Insertable)]
#[table_name = "users"]
struct NewUser {
id: Option<i32>,
name: &'static str,
}
let conn = connection();
let data = vec![
NewUser {
id: None,
name: "Jim",
},
NewUser {
id: Some(123),
name: "Bob",
},
];
assert_eq!(
Ok(2),
insert_into(users::table).values(&data).execute(&conn)
);
let users = users::table.load::<User>(&conn).unwrap();
let jim = users.iter().find(|u| u.name == "Jim");
let bob = users.iter().find(|u| u.name == "Bob");
assert!(jim.is_some());
assert_eq!(Some(&User::new(123, "Bob")), bob);
}
#[test]
#[cfg(feature = "postgres")]
fn derive_insertable_with_field_that_cannot_convert_expression_to_nullable() {
use diesel::sql_types::{Serial, Text};
sql_function!(nextval, nextval_t, (a: Text) -> Serial);
#[derive(Insertable)]
#[table_name = "users"]
struct NewUser {
id: nextval<&'static str>,
name: &'static str,
}
let conn = connection();
let data = NewUser {
id: nextval("users_id_seq"),
name: "Jim",
};
assert_eq!(
Ok(1),
insert_into(users::table).values(&data).execute(&conn)
);
let users = users::table.load::<User>(&conn).unwrap();
let jim = users.iter().find(|u| u.name == "Jim");
assert!(jim.is_some());
}
#[test]
fn nested_queryable_derives() {
#[derive(Queryable, Debug, PartialEq)]
struct UserAndPost {
user: User,
post: Post,
}
let conn = connection_with_sean_and_tess_in_users_table();
let sean = find_user_by_name("Sean", &conn);
insert_into(posts::table)
.values(&sean.new_post("Hi", None))
.execute(&conn)
.unwrap();
let post = posts::table.first(&conn).unwrap();
let expected = UserAndPost { user: sean, post };
let actual = users::table.inner_join(posts::table).get_result(&conn);
assert_eq!(Ok(expected), actual);
}