1
1
use crate :: error:: Result ;
2
2
use crate :: schema:: operation:: RailjsonObject ;
3
3
use crate :: schema:: { OSRDIdentified , ObjectType } ;
4
+ use diesel:: result:: Error as DieselError ;
4
5
use diesel:: sql_types:: { BigInt , Json , Jsonb , Text } ;
5
6
use diesel:: { sql_query, PgConnection , QueryableByName , RunQueryDsl } ;
6
7
use json_patch:: Patch ;
@@ -21,13 +22,20 @@ impl UpdateOperation {
21
22
pub fn apply ( & self , infra_id : i64 , conn : & mut PgConnection ) -> Result < RailjsonObject > {
22
23
// Load object
23
24
24
- let mut obj: DataObject = sql_query ( format ! (
25
+ let mut obj: DataObject = match sql_query ( format ! (
25
26
"SELECT data FROM {} WHERE infra_id = $1 AND obj_id = $2" ,
26
27
self . obj_type. get_table( )
27
28
) )
28
29
. bind :: < BigInt , _ > ( infra_id)
29
30
. bind :: < Text , _ > ( & self . obj_id )
30
- . get_result ( conn) ?;
31
+ . get_result ( conn)
32
+ {
33
+ Ok ( obj) => obj,
34
+ Err ( DieselError :: NotFound ) => {
35
+ return Err ( OperationError :: ObjectNotFound ( self . obj_id . clone ( ) , infra_id) . into ( ) )
36
+ }
37
+ Err ( err) => return Err ( err. into ( ) ) ,
38
+ } ;
31
39
32
40
// Apply and check patch
33
41
let railjson_obj = obj. patch_and_check ( self ) ?;
@@ -43,7 +51,7 @@ impl UpdateOperation {
43
51
. execute ( conn)
44
52
{
45
53
Ok ( 1 ) => Ok ( railjson_obj) ,
46
- Ok ( _) => Err ( OperationError :: ObjectNotFound ( self . obj_id . clone ( ) ) . into ( ) ) ,
54
+ Ok ( _) => Err ( OperationError :: ObjectNotFound ( self . obj_id . clone ( ) , infra_id ) . into ( ) ) ,
47
55
Err ( err) => Err ( err. into ( ) ) ,
48
56
}
49
57
}
@@ -90,7 +98,6 @@ mod tests {
90
98
use crate :: schema:: operation:: OperationError ;
91
99
use crate :: schema:: { OSRDIdentified , ObjectType } ;
92
100
use actix_web:: test as actix_test;
93
- use actix_web:: ResponseError ;
94
101
use diesel:: sql_query;
95
102
use diesel:: sql_types:: { Double , Text } ;
96
103
use diesel:: RunQueryDsl ;
@@ -157,8 +164,8 @@ mod tests {
157
164
158
165
assert ! ( res. is_err( ) ) ;
159
166
assert_eq ! (
160
- res. unwrap_err( ) . status_code ( ) ,
161
- OperationError :: ModifyId . get_status ( )
167
+ res. unwrap_err( ) . get_type ( ) ,
168
+ OperationError :: ModifyId . get_type ( )
162
169
) ;
163
170
} )
164
171
. await ;
@@ -250,4 +257,30 @@ mod tests {
250
257
assert_eq ! ( updated_speed. val, 80.0 ) ;
251
258
} ) . await ;
252
259
}
260
+
261
+ #[ actix_test]
262
+ async fn wrong_id_update_track ( ) {
263
+ test_infra_transaction ( |conn, infra| {
264
+ let update_track = UpdateOperation {
265
+ obj_id : "non_existent_id" . to_string ( ) ,
266
+ obj_type : ObjectType :: TrackSection ,
267
+ railjson_patch : from_str (
268
+ r#"[
269
+ { "op": "replace", "path": "/length", "value": 80.0 }
270
+ ]"# ,
271
+ )
272
+ . unwrap ( ) ,
273
+ } ;
274
+
275
+ let res = update_track. apply ( infra. id . unwrap ( ) , conn) ;
276
+
277
+ assert ! ( res. is_err( ) ) ;
278
+ assert_eq ! (
279
+ res. unwrap_err( ) . get_type( ) ,
280
+ OperationError :: ObjectNotFound ( "non_existent_id" . to_string( ) , infra. id. unwrap( ) )
281
+ . get_type( )
282
+ ) ;
283
+ } )
284
+ . await ;
285
+ }
253
286
}
0 commit comments