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
241 lines (203 loc) · 6.25 KB
/
Copy pathannotations.rs
File metadata and controls
241 lines (203 loc) · 6.25 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
use diesel::*;
use schema::*;
// FIXME: Bring this test back once we can figure out how to allow multiple structs
// on the same table to use `#[belongs_to]` without overlapping the `SelectableExpression`
// impls
// #[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);
// let post: Post = insert(&sean.new_post("Hello", None)).into(posts::table)
// .get_result(&connection).unwrap();
// insert(&NewComment(post.id, "comment")).into(comments::table)
// .execute(&connection).unwrap();
// let comment_text = OtherComment::belonging_to(&post).select(special_comments::text)
// .first::<String>(&connection);
// assert_eq!(Ok("comment".into()), comment_text);
// }
#[test]
fn association_where_parent_and_child_have_underscores() {
#[derive(PartialEq, Eq, Debug, Clone, Queryable, Identifiable, Associations)]
#[has_many(special_comments)]
#[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(&new_post).into(special_posts::table)
.get_result(&connection).unwrap();
let new_comment = SpecialComment::new(special_post.id);
insert(&new_comment).into(special_comments::table)
.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.
#[has_many(bars)]
#[derive(Identifiable, Associations)]
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 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(&data).into(users::table).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);
}