|
1 | 1 | usecrate::test_type;
|
2 |
| -use postgres::{Client,NoTls}; |
| 2 | +use postgres::{error::DbError,Client,NoTls}; |
3 | 3 | use postgres_types::{FromSql,ToSql,WrongType};
|
4 | 4 | use std::error::Error;
|
5 | 5 |
|
@@ -131,3 +131,73 @@ fn missing_variant() {
|
131 | 131 | let err = conn.execute("SELECT $1::foo",&[&Foo::Bar]).unwrap_err();
|
132 | 132 | assert!(err.source().unwrap().is::<WrongType>());
|
133 | 133 | }
|
| 134 | + |
| 135 | +#[test] |
| 136 | +fnallow_mismatch_enums(){ |
| 137 | +#[derive(Debug,ToSql,FromSql,PartialEq)] |
| 138 | +#[postgres(allow_mismatch)] |
| 139 | +enumFoo{ |
| 140 | +Bar, |
| 141 | +} |
| 142 | + |
| 143 | +letmut conn =Client::connect("user=postgres host=localhost port=5433",NoTls).unwrap(); |
| 144 | + conn.execute("CREATE TYPE pg_temp.\"Foo\" AS ENUM ('Bar', 'Baz')",&[]) |
| 145 | +.unwrap(); |
| 146 | + |
| 147 | +let row = conn.query_one("SELECT $1::\"Foo\"",&[&Foo::Bar]).unwrap(); |
| 148 | +assert_eq!(row.get::<_,Foo>(0),Foo::Bar); |
| 149 | +} |
| 150 | + |
| 151 | +#[test] |
| 152 | +fnmissing_enum_variant(){ |
| 153 | +#[derive(Debug,ToSql,FromSql,PartialEq)] |
| 154 | +#[postgres(allow_mismatch)] |
| 155 | +enumFoo{ |
| 156 | +Bar, |
| 157 | +Buz, |
| 158 | +} |
| 159 | + |
| 160 | +letmut conn =Client::connect("user=postgres host=localhost port=5433",NoTls).unwrap(); |
| 161 | + conn.execute("CREATE TYPE pg_temp.\"Foo\" AS ENUM ('Bar', 'Baz')",&[]) |
| 162 | +.unwrap(); |
| 163 | + |
| 164 | +let err = conn |
| 165 | +.query_one("SELECT $1::\"Foo\"",&[&Foo::Buz]) |
| 166 | +.unwrap_err(); |
| 167 | +assert!(err.source().unwrap().is::<DbError>()); |
| 168 | +} |
| 169 | + |
| 170 | +#[test] |
| 171 | +fnallow_mismatch_and_renaming(){ |
| 172 | +#[derive(Debug,ToSql,FromSql,PartialEq)] |
| 173 | +#[postgres(name ="foo", allow_mismatch)] |
| 174 | +enumFoo{ |
| 175 | +#[postgres(name ="bar")] |
| 176 | +Bar, |
| 177 | +#[postgres(name ="buz")] |
| 178 | +Buz, |
| 179 | +} |
| 180 | + |
| 181 | +letmut conn =Client::connect("user=postgres host=localhost port=5433",NoTls).unwrap(); |
| 182 | + conn.execute("CREATE TYPE pg_temp.foo AS ENUM ('bar', 'baz', 'buz')",&[]) |
| 183 | +.unwrap(); |
| 184 | + |
| 185 | +let row = conn.query_one("SELECT $1::foo",&[&Foo::Buz]).unwrap(); |
| 186 | +assert_eq!(row.get::<_,Foo>(0),Foo::Buz); |
| 187 | +} |
| 188 | + |
| 189 | +#[test] |
| 190 | +fnwrong_name_and_allow_mismatch(){ |
| 191 | +#[derive(Debug,ToSql,FromSql,PartialEq)] |
| 192 | +#[postgres(allow_mismatch)] |
| 193 | +enumFoo{ |
| 194 | +Bar, |
| 195 | +} |
| 196 | + |
| 197 | +letmut conn =Client::connect("user=postgres host=localhost port=5433",NoTls).unwrap(); |
| 198 | + conn.execute("CREATE TYPE pg_temp.foo AS ENUM ('Bar', 'Baz')",&[]) |
| 199 | +.unwrap(); |
| 200 | + |
| 201 | +let err = conn.query_one("SELECT $1::foo",&[&Foo::Bar]).unwrap_err(); |
| 202 | +assert!(err.source().unwrap().is::<WrongType>()); |
| 203 | +} |