Skip to content

Commit

Permalink
Make more tests deterministic
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
weiznich committed Feb 2, 2024
1 parent 7bb9184 commit 98fe07c
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 44 deletions.
1 change: 1 addition & 0 deletions diesel_tests/tests/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fn selecting_basic_data() {
user_alias.field(users::name),
user_alias.field(users::hair_color),
))
.order(user_alias.field(users::name))
.load(connection)
.unwrap();

Expand Down
13 changes: 10 additions & 3 deletions diesel_tests/tests/boxed_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn boxed_queries_can_differ_conditionally() {
}

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

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

let ordered = source(Query::Ordered).load(connection);
Expand All @@ -69,6 +69,7 @@ fn boxed_queries_implement_select_dsl() {
let data = users::table
.into_boxed()
.select(users::name)
.order(users::name)
.load::<String>(connection);
assert_eq!(Ok(vec!["Sean".into(), "Tess".into()]), data);
}
Expand All @@ -92,7 +93,11 @@ fn boxed_queries_implement_filter_dsl() {
#[test]
fn boxed_queries_implement_limit_dsl() {
let connection = &mut connection_with_sean_and_tess_in_users_table();
let data = users::table.into_boxed().limit(1).load(connection);
let data = users::table
.into_boxed()
.limit(1)
.order(users::id)
.load(connection);
let expected_data = vec![find_user_by_name("Sean", connection)];
assert_eq!(Ok(expected_data), data);
}
Expand All @@ -104,6 +109,7 @@ fn boxed_queries_implement_offset_dsl() {
.into_boxed()
.limit(1)
.offset(1)
.order(users::id)
.load(connection);
let expected_data = vec![find_user_by_name("Tess", connection)];
assert_eq!(Ok(expected_data), data);
Expand Down Expand Up @@ -154,6 +160,7 @@ fn boxed_queries_implement_or_filter() {
.into_boxed()
.filter(users::name.eq("Sean"))
.or_filter(users::name.eq("Tess"))
.order(users::name)
.load(connection);
let expected = vec![
find_user_by_name("Sean", connection),
Expand Down
13 changes: 7 additions & 6 deletions diesel_tests/tests/combination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn union() {
NewUser::new("Jim", None),
];
insert_into(users).values(&data).execute(conn).unwrap();
let data = users.load::<User>(conn).unwrap();
let data = users.order(id).load::<User>(conn).unwrap();
let sean = &data[0];
let tess = &data[1];
let jim = &data[2];
Expand Down Expand Up @@ -43,7 +43,7 @@ fn union_all() {
NewUser::new("Jim", None),
];
insert_into(users).values(&data).execute(conn).unwrap();
let data = users.load::<User>(conn).unwrap();
let data = users.order(id).load::<User>(conn).unwrap();
let sean = &data[0];
let tess = &data[1];
let jim = &data[2];
Expand Down Expand Up @@ -75,10 +75,10 @@ fn intersect() {
NewUser::new("Jim", None),
];
insert_into(users).values(&data).execute(conn).unwrap();
let data = users.load::<User>(conn).unwrap();
let _sean = &data[0];
let tess = &data[1];
let _jim = &data[2];
let data = users.order(name).load::<User>(conn).unwrap();
let _sean = &data[1];
let tess = &data[2];
let _jim = &data[0];

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

Expand Down
12 changes: 9 additions & 3 deletions diesel_tests/tests/deserialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ fn generated_queryable_allows_lifetimes() {
};
assert_eq!(
Ok(expected_user),
users.select((id, name)).first(connection)
users.select((id, name)).order(id).first(connection)
);
assert_eq!(
users.select((id, name)).first::<CowUser<'_>>(connection),
users.select(CowUser::as_select()).first(connection)
users
.select((id, name))
.order(id)
.first::<CowUser<'_>>(connection),
users
.select(CowUser::as_select())
.order(id)
.first(connection)
);
}

Expand Down
2 changes: 1 addition & 1 deletion diesel_tests/tests/distinct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn distinct_of_multiple_columns() {
.execute(&mut connection)
.unwrap();
let posts = posts::table
.order(posts::id)
.order(posts::title)
.load::<Post>(&mut connection)
.unwrap();

Expand Down
1 change: 1 addition & 0 deletions diesel_tests/tests/expressions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ fn function_with_multiple_arguments() {
let expected_data = vec!["black".to_string(), "Tess".to_string()];
let data = users
.select(coalesce(hair_color, name))
.order(id)
.load::<String>(connection);

assert_eq!(Ok(expected_data), data);
Expand Down
18 changes: 9 additions & 9 deletions diesel_tests/tests/expressions/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ fn adding_literal_to_column() {
let connection = &mut connection_with_sean_and_tess_in_users_table();

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

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

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

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

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

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

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

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

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

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

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

let expected_data = vec![3, 6];
let data = users.select(id * 3).load(connection);
let data = users.select(id * 3).order(id).load(connection);
assert_eq!(Ok(expected_data), data);
}

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

let expected_data = vec![0, 1];
let data = users.select(id / 2).load(connection);
let data = users.select(id / 2).order(id).load(connection);
assert_eq!(Ok(expected_data), data);
}

Expand Down Expand Up @@ -192,7 +192,7 @@ fn mix_and_match_all_numeric_ops() {
.unwrap();

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

Expand Down
40 changes: 33 additions & 7 deletions diesel_tests/tests/insert_from_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ fn insert_from_table() {
.execute(conn)
.unwrap();

let data = posts.select((user_id, title, body)).load(conn);
let data = posts
.select((user_id, title, body))
.order(user_id)
.load(conn);
let expected = vec![
(1, String::from("Sean"), None::<String>),
(2, String::from("Tess"), None),
Expand All @@ -29,7 +32,10 @@ fn insert_from_table_reference() {
.execute(conn)
.unwrap();

let data = posts.select((user_id, title, body)).load(conn);
let data = posts
.select((user_id, title, body))
.order(user_id)
.load(conn);
let expected = vec![
(1, String::from("Sean"), None::<String>),
(2, String::from("Tess"), None),
Expand All @@ -50,7 +56,11 @@ fn insert_from_select() {
.execute(conn)
.unwrap();

let data = posts.select(title).load::<String>(conn).unwrap();
let data = posts
.select(title)
.order(title)
.load::<String>(conn)
.unwrap();
let expected = vec!["Sean says hi", "Tess says hi"];
assert_eq!(expected, data);
}
Expand All @@ -68,7 +78,11 @@ fn insert_from_select_reference() {
.execute(conn)
.unwrap();

let data = posts.select(title).load::<String>(conn).unwrap();
let data = posts
.select(title)
.order(title)
.load::<String>(conn)
.unwrap();
let expected = vec!["Sean says hi", "Tess says hi"];
assert_eq!(expected, data);
}
Expand All @@ -87,7 +101,11 @@ fn insert_from_boxed() {
.execute(conn)
.unwrap();

let data = posts.select(title).load::<String>(conn).unwrap();
let data = posts
.select(title)
.order(title)
.load::<String>(conn)
.unwrap();
let expected = vec!["Sean says hi", "Tess says hi"];
assert_eq!(expected, data);
}
Expand All @@ -105,7 +123,11 @@ fn insert_from_boxed_reference() {
.execute(conn)
.unwrap();

let data = posts.select(title).load::<String>(conn).unwrap();
let data = posts
.select(title)
.order(title)
.load::<String>(conn)
.unwrap();
let expected = vec!["Sean says hi", "Tess says hi"];
assert_eq!(expected, data);
}
Expand Down Expand Up @@ -255,7 +277,11 @@ fn on_conflict_do_nothing_with_select() {
assert_eq!(0, inserted_rows);
}

let data = posts.select(title).load::<String>(conn).unwrap();
let data = posts
.select(title)
.order(title)
.load::<String>(conn)
.unwrap();
let expected = vec!["Sean says hi", "Tess says hi"];
assert_eq!(expected, data);
}
Expand Down
10 changes: 5 additions & 5 deletions diesel_tests/tests/joins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn belongs_to() {
let tess_post = Post::new(2, 2, "World", None);

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

assert_eq!(expected_data, actual_data);
Expand All @@ -40,8 +40,8 @@ fn select_single_from_join() {
.unwrap();

let source = posts::table.inner_join(users::table);
let select_name = source.select(users::name);
let select_title = source.select(posts::title);
let select_name = source.select(users::name).order(users::name);
let select_title = source.select(posts::title).order(posts::title);

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

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

assert_eq!(expected_data, actual_data);
}
Expand Down
8 changes: 4 additions & 4 deletions diesel_tests/tests/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ fn order_by_column() {
NewUser::new("Jim", None),
];
insert_into(users).values(&data).execute(conn).unwrap();
let data = users.load::<User>(conn).unwrap();
let sean = &data[0];
let tess = &data[1];
let jim = &data[2];
let data = users.order(name).load::<User>(conn).unwrap();
let sean = &data[1];
let tess = &data[2];
let jim = &data[0];

let expected_data = vec![
User::new(jim.id, "Jim"),
Expand Down
Loading

0 comments on commit 98fe07c

Please sign in to comment.