forked from adwhit/diesel-derive-enum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.rs
99 lines (93 loc) · 2.36 KB
/
rename.rs
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
use diesel::prelude::*;
#[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]
use crate::common::get_connection;
#[derive(Debug, PartialEq, diesel_derive_enum::DbEnum)]
#[DieselType = "Some_Internal_Type"]
pub enum SomeEnum {
#[db_rename = "mod"]
Mod,
#[db_rename = "type"]
typo,
#[db_rename = "with spaces"]
WithASpace,
}
table! {
use diesel::sql_types::Integer;
use super::Some_Internal_Type;
test_rename {
id -> Integer,
renamed -> Some_Internal_Type,
}
}
#[derive(Insertable, Queryable, Identifiable, Debug, PartialEq)]
#[diesel(table_name = test_rename)]
struct TestRename {
id: i32,
renamed: SomeEnum,
}
#[test]
#[cfg(feature = "postgres")]
fn rename_round_trip() {
use diesel::connection::SimpleConnection;
use diesel::insert_into;
let data = vec![
TestRename {
id: 1,
renamed: SomeEnum::Mod,
},
TestRename {
id: 2,
renamed: SomeEnum::WithASpace,
},
];
let connection = &mut get_connection();
connection
.batch_execute(
r#"
CREATE TYPE "Some_External_Type" AS ENUM ('mod', 'type', 'with spaces');
CREATE TABLE test_rename (
id SERIAL PRIMARY KEY,
renamed "Some_External_Type" NOT NULL
);
"#,
)
.unwrap();
let inserted = insert_into(test_rename::table)
.values(&data)
.get_results(connection)
.unwrap();
assert_eq!(data, inserted);
}
#[test]
#[cfg(feature = "mysql")]
fn rename_round_trip() {
use diesel::connection::SimpleConnection;
use diesel::insert_into;
let data = vec![
TestRename {
id: 1,
renamed: SomeEnum::Mod,
},
TestRename {
id: 2,
renamed: SomeEnum::WithASpace,
},
];
let connection = &mut get_connection();
connection
.batch_execute(
r#"
CREATE TEMPORARY TABLE IF NOT EXISTS test_rename (
id SERIAL PRIMARY KEY,
renamed enum('mod', 'type', 'with spaces') NOT NULL
);
"#,
)
.unwrap();
insert_into(test_rename::table)
.values(&data)
.execute(connection)
.unwrap();
let inserted = test_rename::table.load::<TestRename>(connection).unwrap();
assert_eq!(data, inserted);
}