forked from adwhit/diesel-derive-enum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_style.rs
141 lines (133 loc) · 3.59 KB
/
value_style.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
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
use diesel::prelude::*;
#[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]
use crate::common::get_connection;
#[derive(Debug, PartialEq, diesel_derive_enum::DbEnum)]
#[DieselType = "Stylized_Internal_Type"]
#[PgType = "Stylized_External_Type"]
#[DbValueStyle = "PascalCase"]
pub enum StylizedEnum {
FirstVariant,
secondThing,
third_item,
FOURTH_VALUE,
#[db_rename = "crazy fifth"]
cRaZy_FiFtH,
}
table! {
use diesel::sql_types::Integer;
use super::Stylized_Internal_Type;
test_value_style {
id -> Integer,
value -> Stylized_Internal_Type,
}
}
#[derive(Insertable, Queryable, Identifiable, Debug, PartialEq)]
#[diesel(table_name = test_value_style)]
struct TestStylized {
id: i32,
value: StylizedEnum,
}
fn sample_data() -> Vec<TestStylized> {
vec![
TestStylized {
id: 1,
value: StylizedEnum::FirstVariant,
},
TestStylized {
id: 2,
value: StylizedEnum::secondThing,
},
TestStylized {
id: 3,
value: StylizedEnum::third_item,
},
TestStylized {
id: 4,
value: StylizedEnum::FOURTH_VALUE,
},
TestStylized {
id: 5,
value: StylizedEnum::cRaZy_FiFtH,
},
]
}
#[test]
#[cfg(feature = "postgres")]
fn stylized_round_trip() {
use diesel::connection::SimpleConnection;
use diesel::insert_into;
let data = sample_data();
let connection = &mut get_connection();
connection
.batch_execute(
r#"
CREATE TYPE "Stylized_External_Type" AS ENUM (
'FirstVariant', 'SecondThing', 'ThirdItem', 'FourthValue', 'crazy fifth');
CREATE TABLE test_value_style (
id SERIAL PRIMARY KEY,
value "Stylized_External_Type" NOT NULL
);
"#,
)
.unwrap();
let inserted = insert_into(test_value_style::table)
.values(&data)
.get_results(connection)
.unwrap();
assert_eq!(data, inserted);
}
#[test]
#[cfg(feature = "mysql")]
fn stylized_round_trip() {
use diesel::connection::SimpleConnection;
use diesel::insert_into;
let data = sample_data();
let connection = &mut get_connection();
connection
.batch_execute(
r#"
CREATE TEMPORARY TABLE IF NOT EXISTS test_value_style (
id SERIAL PRIMARY KEY,
value enum('FirstVariant', 'SecondThing', 'ThirdItem', 'FourthValue', 'crazy fifth')
NOT NULL
);
"#,
)
.unwrap();
insert_into(test_value_style::table)
.values(&data)
.execute(connection)
.unwrap();
let inserted = test_value_style::table
.load::<TestStylized>(connection)
.unwrap();
assert_eq!(data, inserted);
}
#[test]
#[cfg(feature = "sqlite")]
fn stylized_round_trip() {
use diesel::connection::SimpleConnection;
use diesel::insert_into;
let data = sample_data();
let connection = &mut get_connection();
connection
.batch_execute(
r#"
CREATE TABLE test_value_style (
id SERIAL PRIMARY KEY,
value TEXT CHECK(value IN (
'FirstVariant', 'SecondThing', 'ThirdItem', 'FourthValue', 'crazy fifth'
)) NOT NULL
);
"#,
)
.unwrap();
insert_into(test_value_style::table)
.values(&data)
.execute(connection)
.unwrap();
let inserted = test_value_style::table
.load::<TestStylized>(connection)
.unwrap();
assert_eq!(data, inserted);
}