@@ -505,12 +505,16 @@ pub mod tests {
505
505
506
506
#[ rstest]
507
507
async fn get_returns_corresponding_rolling_stock ( db_pool : Data < DbPool > ) {
508
+ // GIVEN
508
509
let name = "fast_rolling_stock_get_returns_corresponding_rolling_stock" ;
509
510
let app = create_test_service ( ) . await ;
510
511
let rolling_stock = named_fast_rolling_stock ( name, db_pool) . await ;
511
512
let req = rolling_stock_get_request ( rolling_stock. id ( ) ) ;
513
+
514
+ // WHEN
512
515
let response = call_service ( & app, req) . await ;
513
516
517
+ // THEN
514
518
let response_body: RollingStock = assert_status_and_read ! ( response, StatusCode :: OK ) ;
515
519
assert_eq ! ( response_body. common. name, name) ;
516
520
}
@@ -526,11 +530,13 @@ pub mod tests {
526
530
527
531
#[ rstest]
528
532
async fn create_and_delete_unlocked_rolling_stock_successfully ( ) {
533
+ // GIVEN
529
534
let app = create_test_service ( ) . await ;
530
535
let mut rolling_stock: RollingStockModel = get_fast_rolling_stock (
531
536
"fast_rolling_stock_create_and_delete_unlocked_rolling_stock_successfully" ,
532
537
) ;
533
538
539
+ // WHEN
534
540
let post_response = call_service (
535
541
& app,
536
542
TestRequest :: post ( )
@@ -540,19 +546,20 @@ pub mod tests {
540
546
)
541
547
. await ;
542
548
543
- //Check rolling_stock creation
549
+ // THEN
550
+ // Check rolling_stock creation
544
551
let response_body: RollingStock = assert_status_and_read ! ( post_response, StatusCode :: OK ) ;
545
552
let rolling_stock_id: i64 = response_body. id ;
546
553
rolling_stock. id = Some ( response_body. id ) ;
547
554
let expected_body = RollingStock :: from ( rolling_stock. clone ( ) ) ;
548
555
assert_eq ! ( response_body, expected_body) ;
549
556
550
- //Check rolling_stock deletion
557
+ // Check rolling_stock deletion
551
558
let delete_request = rolling_stock_delete_request ( rolling_stock_id) ;
552
559
let delete_response = call_service ( & app, delete_request) . await ;
553
560
assert_eq ! ( delete_response. status( ) , StatusCode :: NO_CONTENT ) ;
554
561
555
- //Check rolling_stock does not exist anymore
562
+ // Check rolling_stock does not exist anymore
556
563
let get_request = rolling_stock_get_request ( rolling_stock_id) ;
557
564
let get_response = call_service ( & app, get_request) . await ;
558
565
assert_eq ! ( get_response. status( ) , StatusCode :: NOT_FOUND ) ;
@@ -578,13 +585,15 @@ pub mod tests {
578
585
579
586
#[ rstest]
580
587
async fn create_rolling_stock_with_base_power_class_empty ( db_pool : Data < DbPool > ) {
588
+ // GIVEN
581
589
let app = create_test_service ( ) . await ;
582
590
let mut rolling_stock: RollingStockModel = get_fast_rolling_stock (
583
591
"fast_rolling_stock_create_rolling_stock_with_base_power_class_empty" ,
584
592
) ;
585
593
586
594
rolling_stock. base_power_class = Some ( Some ( "" . to_string ( ) ) ) ;
587
595
596
+ // WHEN
588
597
let post_response = call_service (
589
598
& app,
590
599
TestRequest :: post ( )
@@ -594,18 +603,21 @@ pub mod tests {
594
603
)
595
604
. await ;
596
605
606
+ // THEN
597
607
check_create_gave_400 ( db_pool, post_response) . await ;
598
608
}
599
609
600
610
#[ rstest]
601
611
async fn create_rolling_stock_with_duplicate_name ( db_pool : Data < DbPool > ) {
612
+ // GIVEN
602
613
let name = "fast_rolling_stock_create_rolling_stock_with_duplicate_name" ;
603
614
let fast_rolling_stock = named_fast_rolling_stock ( name, db_pool. clone ( ) ) . await ;
604
615
let app = create_test_service ( ) . await ;
605
616
let mut rolling_stock: RollingStockModel = get_fast_rolling_stock ( name) ;
606
617
607
618
rolling_stock. name = fast_rolling_stock. model . name . clone ( ) ;
608
619
620
+ // WHEN
609
621
let post_response = call_service (
610
622
& app,
611
623
TestRequest :: post ( )
@@ -615,15 +627,19 @@ pub mod tests {
615
627
)
616
628
. await ;
617
629
630
+ // THEN
618
631
check_create_gave_400 ( db_pool, post_response) . await ;
619
632
}
620
633
621
634
#[ rstest]
622
635
async fn update_and_delete_locked_rolling_stock_fails ( db_pool : Data < DbPool > ) {
636
+ // GIVEN
623
637
let app = create_test_service ( ) . await ;
624
638
let rolling_stock: RollingStockModel = get_fast_rolling_stock (
625
639
"fast_rolling_stock_update_and_delete_locked_rolling_stock_fails" ,
626
640
) ;
641
+
642
+ // WHEN
627
643
let post_response = call_service (
628
644
& app,
629
645
TestRequest :: post ( )
@@ -632,11 +648,13 @@ pub mod tests {
632
648
. to_request ( ) ,
633
649
)
634
650
. await ;
651
+
652
+ // THEN
635
653
let locked_rolling_stock: RollingStock =
636
654
assert_status_and_read ! ( post_response, StatusCode :: OK ) ;
637
655
let rolling_stock_id = locked_rolling_stock. id ;
638
656
639
- //Check rolling_stock update fails
657
+ // Check rolling_stock update fails
640
658
let patch_response = call_service (
641
659
& app,
642
660
TestRequest :: patch ( )
@@ -651,7 +669,7 @@ pub mod tests {
651
669
RollingStockError :: RollingStockIsLocked { rolling_stock_id }
652
670
) ;
653
671
654
- //Check rolling_stock deletion fails
672
+ // Check rolling_stock deletion fails
655
673
let delete_request = rolling_stock_delete_request ( rolling_stock_id) ;
656
674
let delete_response = call_service ( & app, delete_request) . await ;
657
675
assert_eq ! ( delete_response. status( ) , StatusCode :: BAD_REQUEST ) ;
@@ -660,12 +678,12 @@ pub mod tests {
660
678
RollingStockError :: RollingStockIsLocked { rolling_stock_id }
661
679
) ;
662
680
663
- //Check rolling_stock still exists
681
+ // Check rolling_stock still exists
664
682
let get_request = rolling_stock_get_request ( rolling_stock_id) ;
665
683
let get_response = call_service ( & app, get_request) . await ;
666
684
assert_eq ! ( get_response. status( ) , StatusCode :: OK ) ;
667
685
668
- //Delete rolling_stock to clean db
686
+ // Delete rolling_stock to clean db
669
687
let _ = RollingStockModel :: delete ( db_pool, rolling_stock_id) . await ;
670
688
}
671
689
@@ -717,6 +735,7 @@ pub mod tests {
717
735
718
736
#[ rstest]
719
737
async fn update_unlocked_rolling_stock ( db_pool : Data < DbPool > ) {
738
+ // GIVEN
720
739
let app = create_test_service ( ) . await ;
721
740
let fast_rolling_stock = named_fast_rolling_stock (
722
741
"fast_rolling_stock_update_unlocked_rolling_stock" ,
@@ -729,6 +748,7 @@ pub mod tests {
729
748
get_other_rolling_stock ( "other_rolling_stock_update_unlocked_rolling_stock" ) ;
730
749
rolling_stock. id = Some ( rolling_stock_id) ;
731
750
751
+ // WHEN
732
752
let response = call_service (
733
753
& app,
734
754
TestRequest :: patch ( )
@@ -738,13 +758,14 @@ pub mod tests {
738
758
)
739
759
. await ;
740
760
761
+ // THEN
741
762
let response_body: RollingStock = assert_status_and_read ! ( response, StatusCode :: OK ) ;
742
763
743
764
let rolling_stock = retrieve_existing_rolling_stock ( & db_pool, rolling_stock_id)
744
765
. await
745
766
. unwrap ( ) ;
746
767
747
- //Assert rolling_stock version is 1
768
+ // Assert rolling_stock version is 1
748
769
assert_eq ! ( rolling_stock. version. unwrap( ) , 1 ) ;
749
770
750
771
let expected_body = RollingStock :: from ( rolling_stock) ;
@@ -790,8 +811,11 @@ pub mod tests {
790
811
791
812
#[ rstest]
792
813
async fn update_locked_successfully ( db_pool : Data < DbPool > ) {
814
+ // GIVEN
793
815
let app = create_test_service ( ) . await ;
794
816
let rolling_stock = get_fast_rolling_stock ( "fast_rolling_stock_update_locked_successfully" ) ;
817
+
818
+ // WHEN
795
819
let post_response = call_service (
796
820
& app,
797
821
TestRequest :: post ( )
@@ -800,31 +824,33 @@ pub mod tests {
800
824
. to_request ( ) ,
801
825
)
802
826
. await ;
827
+
828
+ // THEN
803
829
let response_body: RollingStock = assert_status_and_read ! ( post_response, StatusCode :: OK ) ;
804
830
let rolling_stock_id = response_body. id ;
805
831
assert ! ( !response_body. locked) ;
806
832
807
- //Lock rolling_stock
833
+ // Lock rolling_stock
808
834
let request = rolling_stock_locked_request ( rolling_stock_id, true ) ;
809
835
let response = call_service ( & app, request) . await ;
810
836
assert_eq ! ( response. status( ) , StatusCode :: NO_CONTENT ) ;
811
- //Assert rolling_stock is locked
837
+ // Assert rolling_stock is locked
812
838
let rolling_stock = retrieve_existing_rolling_stock ( & db_pool, rolling_stock_id)
813
839
. await
814
840
. unwrap ( ) ;
815
841
assert ! ( rolling_stock. locked. unwrap( ) ) ;
816
842
817
- //Unlock rolling_stock
843
+ // Unlock rolling_stock
818
844
let request = rolling_stock_locked_request ( rolling_stock_id, false ) ;
819
845
let response = call_service ( & app, request) . await ;
820
846
assert_eq ! ( response. status( ) , StatusCode :: NO_CONTENT ) ;
821
- //Assert rolling_stock is unlocked
847
+ // Assert rolling_stock is unlocked
822
848
let rolling_stock = retrieve_existing_rolling_stock ( & db_pool, rolling_stock_id)
823
849
. await
824
850
. unwrap ( ) ;
825
851
assert ! ( !rolling_stock. locked. unwrap( ) ) ;
826
852
827
- //Delete rolling_stock
853
+ // Delete rolling_stock
828
854
call_service ( & app, rolling_stock_delete_request ( rolling_stock_id) ) . await ;
829
855
}
830
856
@@ -845,20 +871,25 @@ pub mod tests {
845
871
846
872
#[ rstest]
847
873
async fn check_usage_no_train_schedule_for_this_rolling_stock ( db_pool : Data < DbPool > ) {
874
+ // GIVEN
848
875
let rolling_stock = named_fast_rolling_stock (
849
876
"fast_rolling_stock_check_usage_no_train_schedule_for_this_rolling_stock" ,
850
877
db_pool,
851
878
)
852
879
. await ;
853
880
let app = create_test_service ( ) . await ;
854
881
let rolling_stock_id = rolling_stock. id ( ) ;
882
+
883
+ // WHEN
855
884
let response = call_service (
856
885
& app,
857
886
TestRequest :: get ( )
858
887
. uri ( format ! ( "/rolling_stock/{}/check_usage" , rolling_stock_id) . as_str ( ) )
859
888
. to_request ( ) ,
860
889
)
861
890
. await ;
891
+
892
+ // THEN
862
893
let response_body: UsageResponse = assert_status_and_read ! ( response, StatusCode :: OK ) ;
863
894
let expected_body = UsageResponse { usage : Vec :: new ( ) } ;
864
895
assert_eq ! ( response_body, expected_body) ;
@@ -980,23 +1011,28 @@ pub mod tests {
980
1011
981
1012
#[ rstest]
982
1013
async fn get_power_restrictions_list ( db_pool : Data < DbPool > ) {
1014
+ // GIVEN
983
1015
let app = create_test_service ( ) . await ;
984
1016
let rolling_stock =
985
1017
named_fast_rolling_stock ( "fast_rolling_stock_get_power_restrictions_list" , db_pool)
986
1018
. await ;
1019
+ let power_restrictions = rolling_stock
1020
+ . model
1021
+ . power_restrictions
1022
+ . clone ( )
1023
+ . unwrap ( )
1024
+ . unwrap ( ) ;
1025
+
1026
+ // WHEN
987
1027
let response = call_service (
988
1028
& app,
989
1029
TestRequest :: get ( )
990
1030
. uri ( "/rolling_stock/power_restrictions" )
991
1031
. to_request ( ) ,
992
1032
)
993
1033
. await ;
994
- let power_restrictions = rolling_stock
995
- . model
996
- . power_restrictions
997
- . clone ( )
998
- . unwrap ( )
999
- . unwrap ( ) ;
1034
+
1035
+ // THEN
1000
1036
assert ! ( power_restrictions. to_string( ) . contains( & "C2" . to_string( ) ) ) ;
1001
1037
let response_body: Vec < String > = read_body_json ( response) . await ;
1002
1038
assert ! ( response_body. contains( & "C2" . to_string( ) ) ) ;
0 commit comments