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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
use diesel::prelude::*;
table! {
users {
id -> Integer,
name -> VarChar,
hair_color -> Nullable<VarChar>,
}
}
#[test]
fn simple_struct_definition() {
struct NewUser {
name: String,
hair_color: String,
}
impl_Insertable! {
(users)
struct NewUser {
name: String,
hair_color: String,
}
}
let conn = connection();
let new_user = NewUser {
name: "Sean".into(),
hair_color: "Black".into(),
};
::insert_into(users::table)
.values(&new_user)
.execute(&conn)
.unwrap();
let saved = users::table
.select((users::name, users::hair_color))
.load::<(String, Option<String>)>(&conn);
let expected = vec![("Sean".to_string(), Some("Black".to_string()))];
assert_eq!(Ok(expected), saved);
}
macro_rules! test_struct_definition {
($test_name:ident, $($struct_def:tt)*) => {
#[test]
fn $test_name() {
use prelude::*;
__diesel_parse_as_item!($($struct_def)*);
impl_Insertable! {
(users)
$($struct_def)*
}
let conn = connection();
let new_user = NewUser { name: "Sean".into(), hair_color: None };
::insert_into(users::table).values(&new_user).execute(&conn).unwrap();
let saved = users::table.select((users::name, users::hair_color))
.load::<(String, Option<String>)>(&conn);
let expected = vec![("Sean".to_string(), Some("Green".to_string()))];
assert_eq!(Ok(expected), saved);
}
}
}
test_struct_definition! {
struct_with_option_field,
struct NewUser {
name: String,
hair_color: Option<String>,
}
}
test_struct_definition! {
pub_struct_definition,
pub struct NewUser {
name: String,
hair_color: Option<String>,
}
}
test_struct_definition! {
struct_with_pub_field,
pub struct NewUser {
pub name: String,
hair_color: Option<String>,
}
}
test_struct_definition! {
struct_with_pub_option_field,
pub struct NewUser {
name: String,
pub hair_color: Option<String>,
}
}
test_struct_definition! {
named_struct_with_borrowed_body,
struct NewUser<'a> {
name: &'a str,
hair_color: Option<&'a str>,
}
}
test_struct_definition! {
named_struct_without_trailing_comma,
struct NewUser<'a> {
name: &'a str,
hair_color: Option<&'a str>
}
}
#[test]
fn named_struct_with_renamed_field() {
struct NewUser {
my_name: String,
hair_color: String,
}
impl_Insertable! {
(users)
struct NewUser {
#[column_name(name)]
my_name: String,
hair_color: String,
}
}
let conn = connection();
let new_user = NewUser {
my_name: "Sean".into(),
hair_color: "Black".into(),
};
::insert_into(users::table)
.values(&new_user)
.execute(&conn)
.unwrap();
let saved = users::table
.select((users::name, users::hair_color))
.load::<(String, Option<String>)>(&conn);
let expected = vec![("Sean".to_string(), Some("Black".to_string()))];
assert_eq!(Ok(expected), saved);
}
#[test]
fn named_struct_with_renamed_option_field() {
struct NewUser {
my_name: String,
my_hair_color: Option<String>,
}
impl_Insertable! {
(users)
struct NewUser {
#[column_name(name)]
my_name: String,
#[column_name(hair_color)]
my_hair_color: Option<String>,
}
}
let conn = connection();
let new_user = NewUser {
my_name: "Sean".into(),
my_hair_color: None,
};
::insert_into(users::table)
.values(&new_user)
.execute(&conn)
.unwrap();
let saved = users::table
.select((users::name, users::hair_color))
.load::<(String, Option<String>)>(&conn);
let expected = vec![("Sean".to_string(), Some("Green".to_string()))];
assert_eq!(Ok(expected), saved);
}
#[test]
fn tuple_struct() {
struct NewUser<'a>(&'a str, Option<&'a str>);
impl_Insertable! {
(users)
struct NewUser<'a>(
#[column_name(name)]
pub &'a str,
#[column_name(hair_color)]
Option<&'a str>,
);
}
let conn = connection();
let new_user = NewUser("Sean", None);
::insert_into(users::table)
.values(&new_user)
.execute(&conn)
.unwrap();
let saved = users::table
.select((users::name, users::hair_color))
.load::<(String, Option<String>)>(&conn);
let expected = vec![("Sean".to_string(), Some("Green".to_string()))];
assert_eq!(Ok(expected), saved);
}
#[test]
fn tuple_struct_without_trailing_comma() {
struct NewUser<'a>(&'a str, Option<&'a str>);
impl_Insertable! {
(users)
struct NewUser<'a>(
#[column_name(name)]
pub &'a str,
#[column_name(hair_color)]
Option<&'a str>
);
}
let conn = connection();
let new_user = NewUser("Sean", None);
::insert_into(users::table)
.values(&new_user)
.execute(&conn)
.unwrap();
let saved = users::table
.select((users::name, users::hair_color))
.load::<(String, Option<String>)>(&conn);
let expected = vec![("Sean".to_string(), Some("Green".to_string()))];
assert_eq!(Ok(expected), saved);
}
#[test]
fn named_struct_with_unusual_reference_type() {
struct NewUser<'a> {
name: &'a String,
hair_color: Option<&'a String>,
}
impl_Insertable! {
(users)
struct NewUser<'a> {
name: &'a String,
hair_color: Option<&'a String>,
}
}
let conn = connection();
let sean = "Sean".to_string();
let black = "Black".to_string();
let new_user = NewUser {
name: &sean,
hair_color: Some(&black),
};
::insert_into(users::table)
.values(&new_user)
.execute(&conn)
.unwrap();
let saved = users::table
.select((users::name, users::hair_color))
.load(&conn);
let expected = vec![(sean.clone(), Some(black.clone()))];
assert_eq!(Ok(expected), saved);
}
cfg_if! {
if #[cfg(feature = "sqlite")] {
fn connection() -> ::test_helpers::TestConnection {
let conn = ::test_helpers::connection();
conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR NOT NULL, hair_color VARCHAR DEFAULT 'Green')").unwrap();
conn
}
} else if #[cfg(feature = "postgres")] {
fn connection() -> ::test_helpers::TestConnection {
let conn = ::test_helpers::connection();
conn.execute("DROP TABLE IF EXISTS users").unwrap();
conn.execute("CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR NOT NULL, hair_color VARCHAR DEFAULT 'Green')").unwrap();
conn
}
#[test]
fn insertable_with_slice_of_borrowed() {
table! {
posts {
id -> Serial,
tags -> Array<Text>,
}
}
struct NewPost<'a> { tags: &'a [&'a str], }
impl_Insertable! { (posts) struct NewPost<'a> { tags: &'a [&'a str], } }
let conn = ::test_helpers::connection();
conn.execute("DROP TABLE IF EXISTS posts").unwrap();
conn.execute("CREATE TABLE posts (id SERIAL PRIMARY KEY, tags TEXT[] NOT NULL)").unwrap();
let new_post = NewPost { tags: &["hi", "there"] };
::insert_into(posts::table).values(&new_post).execute(&conn).unwrap();
let saved = posts::table.select(posts::tags).load::<Vec<String>>(&conn);
let expected = vec![vec![String::from("hi"), String::from("there")]];
assert_eq!(Ok(expected), saved);
}
} else if #[cfg(feature = "mysql")] {
fn connection() -> ::test_helpers::TestConnection {
let conn = ::test_helpers::connection_no_transaction();
conn.execute("DROP TABLE IF EXISTS users").unwrap();
conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY AUTO_INCREMENT, name TEXT NOT NULL, hair_color VARCHAR(255) DEFAULT 'Green')").unwrap();
conn.begin_test_transaction().unwrap();
conn
}
} else {
compile_error!(
"At least one backend must be used to test this crate.\n \
Pass argument `--features \"<backend>\"` with one or more of the following backends, \
'mysql', 'postgres', or 'sqlite'. \n\n \
ex. cargo test --features \"mysql postgres sqlite\"\n"
);
}
}
|