1
1
#![ allow( unused_imports, dead_code) ]
2
2
3
- use sea_orm:: { prelude:: * , query:: QuerySelect , FromQueryResult , JoinType , Set } ;
3
+ use sea_orm:: {
4
+ prelude:: * ,
5
+ query:: { QueryOrder , QuerySelect } ,
6
+ FromQueryResult , JoinType , Set ,
7
+ } ;
4
8
5
9
use crate :: common:: TestContext ;
6
10
use common:: bakery_chain:: * ;
@@ -45,55 +49,12 @@ struct BakeryFlat {
45
49
profit : f64 ,
46
50
}
47
51
48
- async fn fill_data ( ctx : & TestContext , link : bool ) {
49
- bakery:: Entity :: insert ( bakery:: ActiveModel {
50
- id : Set ( 42 ) ,
51
- name : Set ( "cool little bakery" . to_string ( ) ) ,
52
- profit_margin : Set ( 4.1 ) ,
53
- } )
54
- . exec ( & ctx. db )
55
- . await
56
- . expect ( "insert succeeds" ) ;
57
-
58
- cake:: Entity :: insert ( cake:: ActiveModel {
59
- id : Set ( 13 ) ,
60
- name : Set ( "Test Cake" . to_owned ( ) ) ,
61
- price : Set ( 2 . into ( ) ) ,
62
- bakery_id : Set ( if link { Some ( 42 ) } else { None } ) ,
63
- gluten_free : Set ( true ) ,
64
- serial : Set ( Uuid :: new_v4 ( ) ) ,
65
- } )
66
- . exec ( & ctx. db )
67
- . await
68
- . expect ( "insert succeeds" ) ;
69
-
70
- baker:: Entity :: insert ( baker:: ActiveModel {
71
- id : Set ( 22 ) ,
72
- name : Set ( "Master Baker" . to_owned ( ) ) ,
73
- contact_details : Set ( json ! ( null) ) ,
74
- bakery_id : Set ( if link { Some ( 42 ) } else { None } ) ,
75
- } )
76
- . exec ( & ctx. db )
77
- . await
78
- . expect ( "insert succeeds" ) ;
79
-
80
- if link {
81
- cakes_bakers:: Entity :: insert ( cakes_bakers:: ActiveModel {
82
- cake_id : Set ( 13 ) ,
83
- baker_id : Set ( 22 ) ,
84
- } )
85
- . exec ( & ctx. db )
86
- . await
87
- . expect ( "insert succeeds" ) ;
88
- }
89
- }
90
-
91
52
#[ sea_orm_macros:: test]
92
53
async fn from_query_result_left_join_does_not_exist ( ) {
93
54
let ctx = TestContext :: new ( "from_query_result_left_join_does_not_exist" ) . await ;
94
55
create_tables ( & ctx. db ) . await . unwrap ( ) ;
95
56
96
- fill_data ( & ctx, false ) . await ;
57
+ seed_data :: init_1 ( & ctx, false ) . await ;
97
58
98
59
let cake: Cake = cake:: Entity :: find ( )
99
60
. select_only ( )
@@ -102,14 +63,15 @@ async fn from_query_result_left_join_does_not_exist() {
102
63
. column_as ( bakery:: Column :: Id , "bakery_id" )
103
64
. column_as ( bakery:: Column :: Name , "bakery_name" )
104
65
. left_join ( bakery:: Entity )
66
+ . order_by_asc ( cake:: Column :: Id )
105
67
. into_model ( )
106
68
. one ( & ctx. db )
107
69
. await
108
70
. expect ( "succeeds to get the result" )
109
71
. expect ( "exactly one model in DB" ) ;
110
72
111
73
assert_eq ! ( cake. id, 13 ) ;
112
- assert_eq ! ( cake. name, "Test Cake " ) ;
74
+ assert_eq ! ( cake. name, "Cheesecake " ) ;
113
75
assert ! ( cake. bakery. is_none( ) ) ;
114
76
115
77
ctx. delete ( ) . await ;
@@ -120,7 +82,7 @@ async fn from_query_result_left_join_exists() {
120
82
let ctx = TestContext :: new ( "from_query_result_left_join_exists" ) . await ;
121
83
create_tables ( & ctx. db ) . await . unwrap ( ) ;
122
84
123
- fill_data ( & ctx, true ) . await ;
85
+ seed_data :: init_1 ( & ctx, true ) . await ;
124
86
125
87
let cake: Cake = cake:: Entity :: find ( )
126
88
. select_only ( )
@@ -129,14 +91,15 @@ async fn from_query_result_left_join_exists() {
129
91
. column_as ( bakery:: Column :: Id , "bakery_id" )
130
92
. column_as ( bakery:: Column :: Name , "bakery_name" )
131
93
. left_join ( bakery:: Entity )
94
+ . order_by_asc ( cake:: Column :: Id )
132
95
. into_model ( )
133
96
. one ( & ctx. db )
134
97
. await
135
98
. expect ( "succeeds to get the result" )
136
99
. expect ( "exactly one model in DB" ) ;
137
100
138
101
assert_eq ! ( cake. id, 13 ) ;
139
- assert_eq ! ( cake. name, "Test Cake " ) ;
102
+ assert_eq ! ( cake. name, "Cheesecake " ) ;
140
103
let bakery = cake. bakery . unwrap ( ) ;
141
104
assert_eq ! ( bakery. id, 42 ) ;
142
105
assert_eq ! ( bakery. title, "cool little bakery" ) ;
@@ -149,7 +112,7 @@ async fn from_query_result_flat() {
149
112
let ctx = TestContext :: new ( "from_query_result_flat" ) . await ;
150
113
create_tables ( & ctx. db ) . await . unwrap ( ) ;
151
114
152
- fill_data ( & ctx, true ) . await ;
115
+ seed_data :: init_1 ( & ctx, true ) . await ;
153
116
154
117
let bakery: BakeryFlat = bakery:: Entity :: find ( )
155
118
. into_model ( )
@@ -170,7 +133,7 @@ async fn from_query_result_nested() {
170
133
let ctx = TestContext :: new ( "from_query_result_nested" ) . await ;
171
134
create_tables ( & ctx. db ) . await . unwrap ( ) ;
172
135
173
- fill_data ( & ctx, true ) . await ;
136
+ seed_data :: init_1 ( & ctx, true ) . await ;
174
137
175
138
let bakery: BakeryDetails = bakery:: Entity :: find ( )
176
139
. select_only ( )
@@ -204,20 +167,21 @@ async fn from_query_result_plain_model() {
204
167
let ctx = TestContext :: new ( "from_query_result_plain_model" ) . await ;
205
168
create_tables ( & ctx. db ) . await . unwrap ( ) ;
206
169
207
- fill_data ( & ctx, true ) . await ;
170
+ seed_data :: init_1 ( & ctx, true ) . await ;
208
171
209
172
let cake: CakePlain = cake:: Entity :: find ( )
210
173
. column ( cakes_bakers:: Column :: CakeId )
211
174
. column ( cakes_bakers:: Column :: BakerId )
212
175
. join ( JoinType :: LeftJoin , cakes_bakers:: Relation :: Cake . def ( ) . rev ( ) )
176
+ . order_by_asc ( cake:: Column :: Id )
213
177
. into_model ( )
214
178
. one ( & ctx. db )
215
179
. await
216
180
. expect ( "succeeds to get the result" )
217
181
. expect ( "exactly one model in DB" ) ;
218
182
219
183
assert_eq ! ( cake. id, 13 ) ;
220
- assert_eq ! ( cake. name, "Test Cake " ) ;
184
+ assert_eq ! ( cake. name, "Cheesecake " ) ;
221
185
assert_eq ! ( cake. price, Decimal :: from( 2 ) ) ;
222
186
let baker = cake. baker . unwrap ( ) ;
223
187
assert_eq ! ( baker. cake_id, 13 ) ;
@@ -245,7 +209,7 @@ async fn from_query_result_optional_field_but_type_error() {
245
209
let ctx = TestContext :: new ( "from_query_result_nested_error" ) . await ;
246
210
create_tables ( & ctx. db ) . await . unwrap ( ) ;
247
211
248
- fill_data ( & ctx, false ) . await ;
212
+ seed_data :: init_1 ( & ctx, false ) . await ;
249
213
250
214
let _: DbErr = cake:: Entity :: find ( )
251
215
. select_only ( )
0 commit comments