1extern crate orm_macro_derive;
2
3pub trait OrmRepository {
5 fn find(&self) -> &str;
7 fn create(&self) -> &str;
10 fn update(&self) -> &str;
13 fn delete(&self) -> &str;
15}
16
17#[allow(dead_code)]
18#[cfg(test)]
19mod tests {
20
21 use crate::OrmRepository;
22
23 #[derive(Default, orm_macro_derive::GetRepository)]
24 #[table_name(entity)]
25 #[id(id)]
26 struct Entity {
27 id: i64,
28 title: String,
29 description: String,
30 others: Vec<u32>,
31 another_property: bool,
32 }
33
34 #[derive(Default, orm_macro_derive::GetRepository)]
35 #[table_name(entity)]
36 #[id(id)]
37 struct EntityUpdateDto {
38 title: String,
39 description: String,
40 }
41
42 #[derive(Default, orm_macro_derive::GetRepository)]
43 #[table_name(entity)]
44 #[id(id)]
45 struct EntityFindDto {
46 title: String,
47 others: String,
48 }
49
50 #[derive(Default, orm_macro_derive::GetRepository)]
51 #[table_name(entity)]
52 #[id(id)]
53 struct EntityCreateDto {
54 description: String,
55 }
56
57 #[test]
58 fn find_method_build_select_sql() {
59 assert_eq!(
60 "SELECT title,others FROM entity ",
61 EntityFindDtoOrm::builder().find()
62 )
63 }
64
65 #[cfg(feature = "postgres")]
66 #[test]
67 fn find_by_id_method_builds_sql_postgres() {
68 assert_eq!(
69 "SELECT title,others FROM entity WHERE id = $1",
70 EntityFindDtoOrm::builder().find_by_id()
71 )
72 }
73
74 #[cfg(not(feature = "postgres"))]
75 #[test]
76 fn find_by_id_method_builds_sql() {
77 assert_eq!(
78 "SELECT title,others FROM entity WHERE id = ?",
79 EntityFindDtoOrm::builder().find_by_id()
80 )
81 }
82
83 #[test]
84 fn find_method_build_select_sql_with_main() {
85 assert_eq!(
86 "SELECT id,title,description,others,another_property FROM entity ",
87 EntityOrm::builder().find()
88 )
89 }
90
91 #[cfg(feature = "postgres")]
92 #[test]
93 fn create_method_build_insert_sql() {
94 assert_eq!(
95 "INSERT INTO entity (description) VALUES ($1) RETURNING id,description",
96 EntityCreateDtoOrm::builder().create()
97 )
98 }
99
100 #[cfg(not(feature = "postgres"))]
101 #[test]
102 fn create_method_build_insert_mysql_bindings() {
103 assert_eq!(
104 "INSERT INTO entity (description) VALUES (?) RETURNING id,description",
105 EntityCreateDtoOrm::builder().create()
106 )
107 }
108
109 #[cfg(feature = "postgres")]
110 #[test]
111 fn delete_method_build_delete_sql() {
112 assert_eq!(
113 "DELETE FROM entity WHERE id = $1 RETURNING id,title,description,others,another_property",
114 EntityOrm::builder().delete()
115 )
116 }
117
118 #[cfg(not(feature = "postgres"))]
119 #[test]
120 fn delete_method_build_delete_sql_mysql_bindings() {
121 assert_eq!(
122 "DELETE FROM entity WHERE id = ? RETURNING id,title,description,others,another_property",
123 EntityOrm::builder().delete()
124 )
125 }
126
127 #[cfg(feature = "postgres")]
128 #[test]
129 fn update_method_builds_sql() {
130 assert_eq!(
131 "UPDATE entity SET title = $1,description = $2 WHERE id = $3 RETURNING id,title,description",
132 EntityUpdateDtoOrm::builder().update()
133 )
134 }
135
136 #[cfg(not(feature = "postgres"))]
137 #[test]
138 fn update_method_builds_sql_mysql_bindings() {
139 assert_eq!(
140 "UPDATE entity SET title = ?,description = ? WHERE id = ? RETURNING id,title,description",
141 EntityUpdateDtoOrm::builder().update()
142 )
143 }
144
145 #[cfg(feature = "postgres")]
146 #[test]
147 fn update_method_builds_sql_with_main() {
148 assert_eq!(
149 "UPDATE entity SET id = $1,title = $2,description = $3,others = $4,another_property = $5 WHERE id = $6 RETURNING id,title,description,others,another_property",
150 EntityOrm::builder().update()
151 )
152 }
153
154 #[cfg(not(feature = "postgres"))]
155 #[test]
156 fn test_update_query_with_mysql_binding() {
157 assert_eq!(
158 "UPDATE entity SET id = ?,title = ?,description = ?,others = ?,another_property = ? WHERE id = ? RETURNING id,title,description,others,another_property",
159 EntityOrm::builder().update()
160 )
161 }
162}