|
1 | 1 | mod date_and_time; |
2 | 2 | mod ops; |
3 | 3 |
|
4 | | -use schema::{connection, NewUser, TestBackend}; |
| 4 | +use schema::{connection, DropTable, NewUser, TestBackend}; |
5 | 5 | use schema::users::dsl::*; |
6 | 6 | use diesel::*; |
7 | 7 | use diesel::backend::Backend; |
@@ -67,6 +67,58 @@ fn test_count_max() { |
67 | 67 | assert_eq!(Ok(None::<i32>), source.first(&connection)); |
68 | 68 | } |
69 | 69 |
|
| 70 | +#[cfg(feature = "postgres")] |
| 71 | +table! { |
| 72 | + number_arrays (na) { |
| 73 | + na -> Array<Integer>, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +#[test] |
| 78 | +#[cfg(feature = "postgres")] |
| 79 | +fn test_min_max_of_array() { |
| 80 | + use self::number_arrays::dsl::*; |
| 81 | + |
| 82 | + let connection = connection(); |
| 83 | + connection |
| 84 | + .execute("CREATE TABLE number_arrays ( na INTEGER[] PRIMARY KEY )") |
| 85 | + .unwrap(); |
| 86 | + let _bomb = DropTable { |
| 87 | + connection: &connection, |
| 88 | + table_name: "number_arrays", |
| 89 | + }; |
| 90 | + |
| 91 | + insert_into(number_arrays) |
| 92 | + .values(&vec![ |
| 93 | + na.eq(vec![1, 1, 100]), |
| 94 | + na.eq(vec![1, 5, 5]), |
| 95 | + na.eq(vec![5, 0]), |
| 96 | + ]) |
| 97 | + .execute(&connection) |
| 98 | + .unwrap(); |
| 99 | + |
| 100 | + let max_query = number_arrays.select(max(na)); |
| 101 | + let min_query = number_arrays.select(min(na)); |
| 102 | + assert_eq!(Ok(Some(vec![5, 0])), max_query.first(&connection)); |
| 103 | + assert_eq!(Ok(Some(vec![1, 1, 100])), min_query.first(&connection)); |
| 104 | + |
| 105 | + delete(number_arrays.filter(na.eq(vec![5, 0]))) |
| 106 | + .execute(&connection) |
| 107 | + .unwrap(); |
| 108 | + assert_eq!(Ok(Some(vec![1, 5, 5])), max_query.first(&connection)); |
| 109 | + assert_eq!(Ok(Some(vec![1, 1, 100])), min_query.first(&connection)); |
| 110 | + |
| 111 | + delete(number_arrays.filter(na.eq(vec![1, 1, 100]))) |
| 112 | + .execute(&connection) |
| 113 | + .unwrap(); |
| 114 | + assert_eq!(Ok(Some(vec![1, 5, 5])), max_query.first(&connection)); |
| 115 | + assert_eq!(Ok(Some(vec![1, 5, 5])), min_query.first(&connection)); |
| 116 | + |
| 117 | + delete(number_arrays).execute(&connection).unwrap(); |
| 118 | + assert_eq!(Ok(None::<Vec<i32>>), max_query.first(&connection)); |
| 119 | + assert_eq!(Ok(None::<Vec<i32>>), min_query.first(&connection)); |
| 120 | +} |
| 121 | + |
70 | 122 | #[test] |
71 | 123 | fn max_returns_same_type_as_expression_being_maximized() { |
72 | 124 | let connection = connection(); |
|
0 commit comments