-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiesel-interaction.rs
More file actions
218 lines (187 loc) · 5.33 KB
/
Copy pathdiesel-interaction.rs
File metadata and controls
218 lines (187 loc) · 5.33 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
#![cfg_attr(feature = "cargo-clippy", allow(redundant_field_names))]
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_derive_intermediate;
use diesel::dsl::sql;
use diesel::prelude::*;
use diesel::sqlite::SqliteConnection;
table! {
mycologists {
id -> Integer,
rust_count -> Integer,
}
}
table! {
rusts {
id -> Integer,
mycologist_id -> Integer,
life_cycle_stage -> Integer,
}
}
table! {
petri_dishes {
id -> Integer,
mycologist_id -> Integer,
size -> Integer,
}
}
table! {
mikes {
id -> Integer,
rust_count -> Integer,
}
}
mod items {
use super::{mikes, mycologists, rusts};
#[derive(DieselIntermediate, Debug, Clone, PartialEq, Identifiable, Insertable, Queryable)]
#[intermediate_derive(Debug, PartialEq, Insertable)]
#[table_name = "mycologists"]
pub struct Mycologist {
#[intermediate_exclude]
pub id: i32,
pub rust_count: i32,
}
#[derive(DieselIntermediate, Debug, Clone, PartialEq, Identifiable, Insertable, Queryable)]
#[intermediate_derive(Debug, PartialEq, Insertable)]
#[intermediate_table_name = "mikes"]
#[table_name = "mycologists"]
pub struct Scientist {
#[intermediate_exclude]
pub id: i32,
pub rust_count: i32,
}
#[derive(
DieselIntermediate,
Debug,
Clone,
PartialEq,
Identifiable,
Insertable,
Queryable,
Associations,
)]
#[intermediate_derive(Clone, Debug, PartialEq, Insertable)]
#[table_name = "rusts"]
#[belongs_to(Mycologist)]
pub struct Rust {
#[intermediate_exclude]
pub id: i32,
#[intermediate_exclude(Captured)]
pub mycologist_id: i32,
pub life_cycle_stage: i32,
}
}
use items::*;
#[cfg(test)]
fn setup() -> SqliteConnection {
let conn = SqliteConnection::establish(":memory:").unwrap();
let setup = sql::<diesel::sql_types::Bool>(
"
CREATE TABLE mycologists (
id INTEGER PRIMARY KEY AUTOINCREMENT,
rust_count INTEGER NOT NULL
)",
);
setup
.execute(&conn)
.expect("Can't create table: mycologists");
let setup = sql::<diesel::sql_types::Bool>(
"
CREATE TABLE rusts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
mycologist_id INTEGER NOT NULL,
life_cycle_stage INTEGER,
FOREIGN KEY(mycologist_id) REFERENCES mycologists(id)
)",
);
setup.execute(&conn).expect("Can't create table: rusts");
let setup = sql::<diesel::sql_types::Bool>(
"
CREATE TABLE mikes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
rust_count INTEGER
)",
);
setup.execute(&conn).expect("Can't create table: mikes");
conn
}
#[test]
fn can_insert_mycologist() {
let conn = setup();
let obj = NewMycologist { rust_count: 156 };
diesel::insert_into(mycologists::table)
.values(&obj)
.execute(&conn)
.expect("Couldn't insert struct into mycologists");
let found: Vec<Mycologist> = mycologists::table.load(&conn).unwrap();
assert_eq!(
found,
vec![Mycologist {
id: 1,
rust_count: 156,
},]
);
}
#[test]
fn can_insert_intermediate() {
let conn = setup();
let new_rust = NewRust {
life_cycle_stage: 0,
};
let mike = NewMycologist { rust_count: 0 };
diesel::insert_into(mycologists::table)
.values(&mike)
.execute(&conn)
.expect("Couldn't insert struct into mycologists");
let created_mike: Mycologist = mycologists::table
.order(mycologists::id.desc())
.first(&conn)
.unwrap();
let _similar_mike = Mycologist::from_new_mycologist(created_mike.id, mike);
let captured_rust = CapturedRust {
mycologist_id: created_mike.id,
life_cycle_stage: new_rust.life_cycle_stage,
};
let captured_rust_from_new = CapturedRust::from_new_rust(6, new_rust.clone());
let _rust_from_captured = Rust::from_captured_rust(7, captured_rust_from_new);
let _rust_from_new = Rust::from_new_rust(8, 9, new_rust);
diesel::insert_into(rusts::table)
.values(&captured_rust)
.execute(&conn)
.expect("Couldn't insert captured_rust into table");
let created = rusts::table.load::<Rust>(&conn).unwrap();
assert_eq!(
created,
vec![Rust {
id: 1,
mycologist_id: 1,
life_cycle_stage: 0,
},]
);
let rusts = Rust::belonging_to(&created_mike)
.load::<Rust>(&conn)
.expect("couldn't load rusts belonging to mike");
assert_eq!(
rusts,
vec![Rust {
id: 1,
mycologist_id: 1,
life_cycle_stage: 0,
},]
);
}
#[test]
fn can_insert_into_intermediate_table() {
let conn = setup();
let mike = NewScientist { rust_count: 12 };
diesel::insert_into(mikes::table)
.values(&mike)
.execute(&conn)
.expect("Couldn't insert mike into scientists table");
let fetched_mike = mikes::table.load::<Scientist>(&conn).unwrap();
diesel::insert_into(mycologists::table)
.values(&fetched_mike)
.execute(&conn)
.expect("Couldn't insert mike into mycologists table");
}