Skip to content

Commit 98fe07c

Browse files
committed
Make more tests deterministic
This commit fixes a lot of randomly failing tests by adding `ORDER BY` clauses to some queries. It also prevents concurrently creating an `INDEX` for the same table (which results in a deadlock in postgres sometimes). (This was tested by running `diesel_tests` in a loop until 100 continuous runs passed)
1 parent 7bb9184 commit 98fe07c

File tree

13 files changed

+112
-44
lines changed

13 files changed

+112
-44
lines changed

diesel_tests/tests/alias.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ fn selecting_basic_data() {
2020
user_alias.field(users::name),
2121
user_alias.field(users::hair_color),
2222
))
23+
.order(user_alias.field(users::name))
2324
.load(connection)
2425
.unwrap();
2526

diesel_tests/tests/boxed_queries.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn boxed_queries_can_differ_conditionally() {
3737
}
3838

3939
let source = |query| match query {
40-
Query::All => users::table.into_boxed(),
40+
Query::All => users::table.order(users::name.desc()).into_boxed(),
4141
Query::Ordered => users::table.order(users::name.desc()).into_boxed(),
4242
Query::One => users::table
4343
.filter(users::name.ne("jim"))
@@ -51,7 +51,7 @@ fn boxed_queries_can_differ_conditionally() {
5151
let jim = find_user_by_name("Jim", connection);
5252

5353
let all = source(Query::All).load(connection);
54-
let expected_data = vec![sean.clone(), tess.clone(), jim.clone()];
54+
let expected_data = vec![tess.clone(), sean.clone(), jim.clone()];
5555
assert_eq!(Ok(expected_data), all);
5656

5757
let ordered = source(Query::Ordered).load(connection);
@@ -69,6 +69,7 @@ fn boxed_queries_implement_select_dsl() {
6969
let data = users::table
7070
.into_boxed()
7171
.select(users::name)
72+
.order(users::name)
7273
.load::<String>(connection);
7374
assert_eq!(Ok(vec!["Sean".into(), "Tess".into()]), data);
7475
}
@@ -92,7 +93,11 @@ fn boxed_queries_implement_filter_dsl() {
9293
#[test]
9394
fn boxed_queries_implement_limit_dsl() {
9495
let connection = &mut connection_with_sean_and_tess_in_users_table();
95-
let data = users::table.into_boxed().limit(1).load(connection);
96+
let data = users::table
97+
.into_boxed()
98+
.limit(1)
99+
.order(users::id)
100+
.load(connection);
96101
let expected_data = vec![find_user_by_name("Sean", connection)];
97102
assert_eq!(Ok(expected_data), data);
98103
}
@@ -104,6 +109,7 @@ fn boxed_queries_implement_offset_dsl() {
104109
.into_boxed()
105110
.limit(1)
106111
.offset(1)
112+
.order(users::id)
107113
.load(connection);
108114
let expected_data = vec![find_user_by_name("Tess", connection)];
109115
assert_eq!(Ok(expected_data), data);
@@ -154,6 +160,7 @@ fn boxed_queries_implement_or_filter() {
154160
.into_boxed()
155161
.filter(users::name.eq("Sean"))
156162
.or_filter(users::name.eq("Tess"))
163+
.order(users::name)
157164
.load(connection);
158165
let expected = vec![
159166
find_user_by_name("Sean", connection),

diesel_tests/tests/combination.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn union() {
1313
NewUser::new("Jim", None),
1414
];
1515
insert_into(users).values(&data).execute(conn).unwrap();
16-
let data = users.load::<User>(conn).unwrap();
16+
let data = users.order(id).load::<User>(conn).unwrap();
1717
let sean = &data[0];
1818
let tess = &data[1];
1919
let jim = &data[2];
@@ -43,7 +43,7 @@ fn union_all() {
4343
NewUser::new("Jim", None),
4444
];
4545
insert_into(users).values(&data).execute(conn).unwrap();
46-
let data = users.load::<User>(conn).unwrap();
46+
let data = users.order(id).load::<User>(conn).unwrap();
4747
let sean = &data[0];
4848
let tess = &data[1];
4949
let jim = &data[2];
@@ -75,10 +75,10 @@ fn intersect() {
7575
NewUser::new("Jim", None),
7676
];
7777
insert_into(users).values(&data).execute(conn).unwrap();
78-
let data = users.load::<User>(conn).unwrap();
79-
let _sean = &data[0];
80-
let tess = &data[1];
81-
let _jim = &data[2];
78+
let data = users.order(name).load::<User>(conn).unwrap();
79+
let _sean = &data[1];
80+
let tess = &data[2];
81+
let _jim = &data[0];
8282

8383
let expected_data = vec![User::new(tess.id, "Tess")];
8484
let data: Vec<_> = users
@@ -171,6 +171,7 @@ fn as_subquery_for_eq_in() {
171171
let out = posts::table
172172
.filter(posts::user_id.eq_any(subquery))
173173
.select(posts::title)
174+
.order_by(posts::title)
174175
.load::<String>(conn)
175176
.unwrap();
176177

diesel_tests/tests/deserialization.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ fn generated_queryable_allows_lifetimes() {
2222
};
2323
assert_eq!(
2424
Ok(expected_user),
25-
users.select((id, name)).first(connection)
25+
users.select((id, name)).order(id).first(connection)
2626
);
2727
assert_eq!(
28-
users.select((id, name)).first::<CowUser<'_>>(connection),
29-
users.select(CowUser::as_select()).first(connection)
28+
users
29+
.select((id, name))
30+
.order(id)
31+
.first::<CowUser<'_>>(connection),
32+
users
33+
.select(CowUser::as_select())
34+
.order(id)
35+
.first(connection)
3036
);
3137
}
3238

diesel_tests/tests/distinct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn distinct_of_multiple_columns() {
178178
.execute(&mut connection)
179179
.unwrap();
180180
let posts = posts::table
181-
.order(posts::id)
181+
.order(posts::title)
182182
.load::<Post>(&mut connection)
183183
.unwrap();
184184

diesel_tests/tests/expressions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ fn function_with_multiple_arguments() {
238238
let expected_data = vec!["black".to_string(), "Tess".to_string()];
239239
let data = users
240240
.select(coalesce(hair_color, name))
241+
.order(id)
241242
.load::<String>(connection);
242243

243244
assert_eq!(Ok(expected_data), data);

diesel_tests/tests/expressions/ops.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ fn adding_literal_to_column() {
88
let connection = &mut connection_with_sean_and_tess_in_users_table();
99

1010
let expected_data = vec![2, 3];
11-
let data = users.select(id + 1).load(connection);
11+
let data = users.select(id + 1).order(id).load(connection);
1212
assert_eq!(Ok(expected_data), data);
1313

1414
let expected_data = vec![3, 4];
15-
let data = users.select(id + 2).load(connection);
15+
let data = users.select(id + 2).order(id).load(connection);
1616
assert_eq!(Ok(expected_data), data);
1717
}
1818

@@ -36,7 +36,7 @@ fn adding_column_to_column() {
3636
let connection = &mut connection_with_sean_and_tess_in_users_table();
3737

3838
let expected_data = vec![2, 4];
39-
let data = users.select(id + id).load(connection);
39+
let data = users.select(id + id).order(id).load(connection);
4040
assert_eq!(Ok(expected_data), data);
4141
}
4242

@@ -47,7 +47,7 @@ fn adding_multiple_times() {
4747
let connection = &mut connection_with_sean_and_tess_in_users_table();
4848

4949
let expected_data = vec![4, 5];
50-
let data = users.select(id + 1 + 2).load(connection);
50+
let data = users.select(id + 1 + 2).order(id).load(connection);
5151
assert_eq!(Ok(expected_data), data);
5252
}
5353

@@ -58,7 +58,7 @@ fn subtracting_literal_from_column() {
5858
let connection = &mut connection_with_sean_and_tess_in_users_table();
5959

6060
let expected_data = vec![0, 1];
61-
let data = users.select(id - 1).load(connection);
61+
let data = users.select(id - 1).order(id).load(connection);
6262
assert_eq!(Ok(expected_data), data);
6363
}
6464

@@ -69,7 +69,7 @@ fn adding_then_subtracting() {
6969
let connection = &mut connection_with_sean_and_tess_in_users_table();
7070

7171
let expected_data = vec![2, 3];
72-
let data = users.select(id + 2 - 1).load(connection);
72+
let data = users.select(id + 2 - 1).order(id).load(connection);
7373
assert_eq!(Ok(expected_data), data);
7474
}
7575

@@ -80,7 +80,7 @@ fn multiplying_column() {
8080
let connection = &mut connection_with_sean_and_tess_in_users_table();
8181

8282
let expected_data = vec![3, 6];
83-
let data = users.select(id * 3).load(connection);
83+
let data = users.select(id * 3).order(id).load(connection);
8484
assert_eq!(Ok(expected_data), data);
8585
}
8686

@@ -91,7 +91,7 @@ fn dividing_column() {
9191
let connection = &mut connection_with_sean_and_tess_in_users_table();
9292

9393
let expected_data = vec![0, 1];
94-
let data = users.select(id / 2).load(connection);
94+
let data = users.select(id / 2).order(id).load(connection);
9595
assert_eq!(Ok(expected_data), data);
9696
}
9797

@@ -192,7 +192,7 @@ fn mix_and_match_all_numeric_ops() {
192192
.unwrap();
193193

194194
let expected_data = vec![4, 6, 7, 9];
195-
let data = users.select(id * 3 / 2 + 4 - 1).load(connection);
195+
let data = users.select(id * 3 / 2 + 4 - 1).order(id).load(connection);
196196
assert_eq!(Ok(expected_data), data);
197197
}
198198

diesel_tests/tests/insert_from_select.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ fn insert_from_table() {
1111
.execute(conn)
1212
.unwrap();
1313

14-
let data = posts.select((user_id, title, body)).load(conn);
14+
let data = posts
15+
.select((user_id, title, body))
16+
.order(user_id)
17+
.load(conn);
1518
let expected = vec![
1619
(1, String::from("Sean"), None::<String>),
1720
(2, String::from("Tess"), None),
@@ -29,7 +32,10 @@ fn insert_from_table_reference() {
2932
.execute(conn)
3033
.unwrap();
3134

32-
let data = posts.select((user_id, title, body)).load(conn);
35+
let data = posts
36+
.select((user_id, title, body))
37+
.order(user_id)
38+
.load(conn);
3339
let expected = vec![
3440
(1, String::from("Sean"), None::<String>),
3541
(2, String::from("Tess"), None),
@@ -50,7 +56,11 @@ fn insert_from_select() {
5056
.execute(conn)
5157
.unwrap();
5258

53-
let data = posts.select(title).load::<String>(conn).unwrap();
59+
let data = posts
60+
.select(title)
61+
.order(title)
62+
.load::<String>(conn)
63+
.unwrap();
5464
let expected = vec!["Sean says hi", "Tess says hi"];
5565
assert_eq!(expected, data);
5666
}
@@ -68,7 +78,11 @@ fn insert_from_select_reference() {
6878
.execute(conn)
6979
.unwrap();
7080

71-
let data = posts.select(title).load::<String>(conn).unwrap();
81+
let data = posts
82+
.select(title)
83+
.order(title)
84+
.load::<String>(conn)
85+
.unwrap();
7286
let expected = vec!["Sean says hi", "Tess says hi"];
7387
assert_eq!(expected, data);
7488
}
@@ -87,7 +101,11 @@ fn insert_from_boxed() {
87101
.execute(conn)
88102
.unwrap();
89103

90-
let data = posts.select(title).load::<String>(conn).unwrap();
104+
let data = posts
105+
.select(title)
106+
.order(title)
107+
.load::<String>(conn)
108+
.unwrap();
91109
let expected = vec!["Sean says hi", "Tess says hi"];
92110
assert_eq!(expected, data);
93111
}
@@ -105,7 +123,11 @@ fn insert_from_boxed_reference() {
105123
.execute(conn)
106124
.unwrap();
107125

108-
let data = posts.select(title).load::<String>(conn).unwrap();
126+
let data = posts
127+
.select(title)
128+
.order(title)
129+
.load::<String>(conn)
130+
.unwrap();
109131
let expected = vec!["Sean says hi", "Tess says hi"];
110132
assert_eq!(expected, data);
111133
}
@@ -255,7 +277,11 @@ fn on_conflict_do_nothing_with_select() {
255277
assert_eq!(0, inserted_rows);
256278
}
257279

258-
let data = posts.select(title).load::<String>(conn).unwrap();
280+
let data = posts
281+
.select(title)
282+
.order(title)
283+
.load::<String>(conn)
284+
.unwrap();
259285
let expected = vec!["Sean says hi", "Tess says hi"];
260286
assert_eq!(expected, data);
261287
}

diesel_tests/tests/joins.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn belongs_to() {
2020
let tess_post = Post::new(2, 2, "World", None);
2121

2222
let expected_data = vec![(seans_post, sean), (tess_post, tess)];
23-
let source = posts::table.inner_join(users::table);
23+
let source = posts::table.inner_join(users::table).order(posts::id);
2424
let actual_data: Vec<_> = source.load(connection).unwrap();
2525

2626
assert_eq!(expected_data, actual_data);
@@ -40,8 +40,8 @@ fn select_single_from_join() {
4040
.unwrap();
4141

4242
let source = posts::table.inner_join(users::table);
43-
let select_name = source.select(users::name);
44-
let select_title = source.select(posts::title);
43+
let select_name = source.select(users::name).order(users::name);
44+
let select_title = source.select(posts::title).order(posts::title);
4545

4646
let expected_names = vec!["Sean".to_string(), "Tess".to_string()];
4747
let actual_names: Vec<String> = select_name.load(connection).unwrap();
@@ -75,7 +75,7 @@ fn select_multiple_from_join() {
7575
("Sean".to_string(), "Hello".to_string()),
7676
("Tess".to_string(), "World".to_string()),
7777
];
78-
let actual_data: Vec<_> = source.load(connection).unwrap();
78+
let actual_data: Vec<_> = source.order(users::name).load(connection).unwrap();
7979

8080
assert_eq!(expected_data, actual_data);
8181
}
@@ -102,7 +102,7 @@ fn join_boxed_query() {
102102
("Sean".to_string(), "Hello".to_string()),
103103
("Tess".to_string(), "World".to_string()),
104104
];
105-
let actual_data: Vec<_> = source.load(connection).unwrap();
105+
let actual_data: Vec<_> = source.order(users::name).load(connection).unwrap();
106106

107107
assert_eq!(expected_data, actual_data);
108108
}

diesel_tests/tests/order.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ fn order_by_column() {
1212
NewUser::new("Jim", None),
1313
];
1414
insert_into(users).values(&data).execute(conn).unwrap();
15-
let data = users.load::<User>(conn).unwrap();
16-
let sean = &data[0];
17-
let tess = &data[1];
18-
let jim = &data[2];
15+
let data = users.order(name).load::<User>(conn).unwrap();
16+
let sean = &data[1];
17+
let tess = &data[2];
18+
let jim = &data[0];
1919

2020
let expected_data = vec![
2121
User::new(jim.id, "Jim"),

0 commit comments

Comments
 (0)