forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert.rs
More file actions
216 lines (180 loc) · 6.49 KB
/
Copy pathinsert.rs
File metadata and controls
216 lines (180 loc) · 6.49 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
use super::schema::*;
use diesel::*;
#[test]
fn insert_records() {
use schema::users::table as users;
let connection = connection();
let new_users: &[_] = &[
NewUser::new("Sean", Some("Black")),
NewUser::new("Tess", None),
];
insert(new_users).into(users).execute(&connection).unwrap();
let actual_users = users.load::<User>(&connection).unwrap();
let expected_users = vec![
User { id: actual_users[0].id, name: "Sean".to_string(), hair_color: Some("Black".to_string()) },
User { id: actual_users[1].id, name: "Tess".to_string(), hair_color: None },
];
assert_eq!(expected_users, actual_users);
}
#[test]
#[cfg(not(any(feature="sqlite", feature="mysql")))]
fn insert_records_using_returning_clause() {
use schema::users::table as users;
let connection = connection();
let new_users: &[_] = &[
NewUser::new("Sean", Some("Black")),
NewUser::new("Tess", None),
];
let inserted_users = insert(new_users).into(users).get_results::<User>(&connection).unwrap();
let expected_users = vec![
User { id: inserted_users[0].id, name: "Sean".to_string(), hair_color: Some("Black".to_string()) },
User { id: inserted_users[1].id, name: "Tess".to_string(), hair_color: None },
];
assert_eq!(expected_users, inserted_users);
}
#[test]
#[cfg(not(any(feature="sqlite", feature="mysql")))]
fn insert_records_with_custom_returning_clause() {
use schema::users::dsl::*;
let connection = connection();
let new_users: &[_] = &[
NewUser::new("Sean", Some("Black")),
NewUser::new("Tess", None),
];
let inserted_users = insert(new_users)
.into(users)
.returning((name, hair_color))
.get_results::<(String, Option<String>)>(&connection)
.unwrap();
let expected_users = vec![
("Sean".to_string(), Some("Black".to_string())),
("Tess".to_string(), None),
];
assert_eq!(expected_users, inserted_users);
}
#[test]
#[cfg(not(feature="mysql"))] // FIXME: Figure out how to handle tests that modify schema
fn batch_insert_with_defaults() {
use schema::users::table as users;
use schema_dsl::*;
let connection = connection();
connection.execute("DROP TABLE users").unwrap();
create_table("users", (
integer("id").primary_key().auto_increment(),
string("name").not_null(),
string("hair_color").not_null().default("'Green'"),
)).execute(&connection).unwrap();
let new_users: &[_] = &[
NewUser::new("Sean", Some("Black")),
NewUser::new("Tess", None),
];
insert(new_users).into(users).execute(&connection).unwrap();
let expected_users = vec![
User { id: 1, name: "Sean".to_string(), hair_color: Some("Black".to_string()) },
User { id: 2, name: "Tess".to_string(), hair_color: Some("Green".to_string()) },
];
let actual_users = users.load(&connection).unwrap();
assert_eq!(expected_users, actual_users);
}
#[test]
#[cfg(not(feature="mysql"))] // FIXME: Figure out how to handle tests that modify schema
fn insert_with_defaults() {
use schema::users::table as users;
use schema_dsl::*;
let connection = connection();
connection.execute("DROP TABLE users").unwrap();
create_table("users", (
integer("id").primary_key().auto_increment(),
string("name").not_null(),
string("hair_color").not_null().default("'Green'"),
)).execute(&connection).unwrap();
insert(&NewUser::new("Tess", None)).into(users).execute(&connection).unwrap();
let expected_users = vec![
User { id: 1, name: "Tess".to_string(), hair_color: Some("Green".to_string()) },
];
let actual_users = users.load(&connection).unwrap();
assert_eq!(expected_users, actual_users);
}
#[test]
#[cfg(not(feature="mysql"))] // FIXME: Figure out how to handle tests that modify schema
fn insert_returning_count_returns_number_of_rows_inserted() {
use schema::users::table as users;
let connection = connection();
connection.execute("DROP TABLE users").unwrap();
connection.execute("CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL,
hair_color VARCHAR NOT NULL DEFAULT 'Green'
)").unwrap();
let new_users: &[_] = &[
BaldUser { name: "Sean".to_string() },
BaldUser { name: "Tess".to_string() },
];
let count = insert(new_users).into(users).execute(&connection).unwrap();
let second_count = insert(&BaldUser { name: "Guy".to_string() }).into(users).execute(&connection).unwrap();
assert_eq!(2, count);
assert_eq!(1, second_count);
}
#[derive(Insertable)]
#[table_name="users"]
struct BaldUser {
name: String,
}
#[derive(Insertable)]
#[table_name="users"]
struct BorrowedUser<'a> {
name: &'a str,
}
#[test]
fn insert_borrowed_content() {
use schema::users::table as users;
let connection = connection();
let new_users: &[_] = &[
BorrowedUser { name: "Sean" },
BorrowedUser { name: "Tess" },
];
insert(new_users).into(users).execute(&connection).unwrap();
let actual_users = users.load::<User>(&connection).unwrap();
let expected_users = vec![
User::new(actual_users[0].id, "Sean"),
User::new(actual_users[1].id, "Tess"),
];
assert_eq!(expected_users, actual_users);
}
#[test]
#[cfg(feature = "sqlite")]
fn insert_on_conflict_replace() {
use schema::users::dsl::*;
use diesel::insert_or_replace;
let connection = connection_with_sean_and_tess_in_users_table();
let mut sean = find_user_by_name("Sean", &connection);
sean.name = "Jim".into();
insert_or_replace(&sean)
.into(users)
.execute(&connection)
.unwrap();
let expected_names = vec!["Jim".into(), "Tess".into()];
let names = users.select(name).order(id).load::<String>(&connection);
assert_eq!(Ok(expected_names), names);
}
#[test]
fn insert_empty_slice() {
let connection = connection();
let inserted_records = insert(&Vec::<NewUser>::new())
.into(users::table)
.execute(&connection);
assert_eq!(Ok(0), inserted_records);
}
#[test]
#[cfg(feature = "postgres")]
fn insert_empty_slice_with_returning() {
let connection = connection();
let insert_one = insert(&Vec::<NewUser>::new())
.into(users::table)
.get_result::<User>(&connection);
let insert_all = insert(&Vec::<NewUser>::new())
.into(users::table)
.get_results::<User>(&connection);
assert_eq!(Ok(None), insert_one.optional());
assert_eq!(Ok(vec![]), insert_all);
}