Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014aUaQeLnwbb1zTpN7kHeat
131 lines
4.3 KiB
ABAP
131 lines
4.3 KiB
ABAP
CLASS {{p}}t31_test DEFINITION PUBLIC FINAL CREATE PUBLIC
|
|
FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
|
|
|
|
PRIVATE SECTION.
|
|
TYPES ty_stations TYPE STANDARD TABLE OF {{p}}station.
|
|
TYPES ty_moves TYPE STANDARD TABLE OF {{p}}move.
|
|
TYPES ty_id TYPE {{p}}station-station_id.
|
|
|
|
METHODS given
|
|
IMPORTING it_stations TYPE ty_stations
|
|
it_moves TYPE ty_moves.
|
|
|
|
METHODS rebalance
|
|
IMPORTING iv_dry_run TYPE abap_bool DEFAULT abap_false
|
|
EXPORTING ev_applied TYPE i
|
|
ev_rejected TYPE i.
|
|
|
|
METHODS bikes_of
|
|
IMPORTING iv_id TYPE ty_id
|
|
RETURNING VALUE(rv_bikes) TYPE i.
|
|
|
|
METHODS applies_move FOR TESTING.
|
|
METHODS rejects_move FOR TESTING.
|
|
METHODS dry_run FOR TESTING.
|
|
ENDCLASS.
|
|
|
|
|
|
CLASS {{p}}t31_test IMPLEMENTATION.
|
|
|
|
METHOD given.
|
|
DATA(lt_stations) = it_stations.
|
|
DATA(lt_moves) = it_moves.
|
|
|
|
LOOP AT lt_stations ASSIGNING FIELD-SYMBOL(<ls_station>).
|
|
<ls_station>-client = sy-mandt.
|
|
ENDLOOP.
|
|
|
|
LOOP AT lt_moves ASSIGNING FIELD-SYMBOL(<ls_move>).
|
|
<ls_move>-client = sy-mandt.
|
|
ENDLOOP.
|
|
|
|
DELETE FROM {{p}}station.
|
|
DELETE FROM {{p}}move.
|
|
INSERT {{p}}station FROM TABLE @lt_stations.
|
|
INSERT {{p}}move FROM TABLE @lt_moves.
|
|
ENDMETHOD.
|
|
|
|
METHOD rebalance.
|
|
CALL FUNCTION '{{P}}BIKE_REBALANCE'
|
|
EXPORTING
|
|
iv_dry_run = iv_dry_run
|
|
IMPORTING
|
|
ev_applied = ev_applied
|
|
ev_rejected = ev_rejected
|
|
EXCEPTIONS
|
|
OTHERS = 1.
|
|
cl_abap_unit_assert=>assert_subrc( exp = 0 ).
|
|
ENDMETHOD.
|
|
|
|
METHOD bikes_of.
|
|
SELECT SINGLE bikes FROM {{p}}station
|
|
INTO @rv_bikes
|
|
WHERE station_id = @iv_id.
|
|
ENDMETHOD.
|
|
|
|
METHOD applies_move.
|
|
DATA lt_stations TYPE ty_stations.
|
|
DATA lt_moves TYPE ty_moves.
|
|
DATA lv_applied TYPE i.
|
|
DATA lv_rejected TYPE i.
|
|
|
|
lt_stations = VALUE #(
|
|
( station_id = 'S001' station_name = 'Alpha' capacity = 10 bikes = 5 )
|
|
( station_id = 'S002' station_name = 'Beta' capacity = 10 bikes = 0 ) ).
|
|
lt_moves = VALUE #(
|
|
( move_no = 1 source_id = 'S001' target_id = 'S002' quantity = 3 ) ).
|
|
|
|
given( it_stations = lt_stations it_moves = lt_moves ).
|
|
rebalance( IMPORTING ev_applied = lv_applied ev_rejected = lv_rejected ).
|
|
|
|
cl_abap_unit_assert=>assert_equals( act = lv_applied exp = 1 ).
|
|
cl_abap_unit_assert=>assert_equals( act = lv_rejected exp = 0 ).
|
|
cl_abap_unit_assert=>assert_equals( act = bikes_of( 'S001' ) exp = 2 ).
|
|
cl_abap_unit_assert=>assert_equals( act = bikes_of( 'S002' ) exp = 3 ).
|
|
ENDMETHOD.
|
|
|
|
METHOD rejects_move.
|
|
DATA lt_stations TYPE ty_stations.
|
|
DATA lt_moves TYPE ty_moves.
|
|
DATA lv_applied TYPE i.
|
|
DATA lv_rejected TYPE i.
|
|
|
|
lt_stations = VALUE #(
|
|
( station_id = 'S001' station_name = 'Alpha' capacity = 10 bikes = 1 )
|
|
( station_id = 'S002' station_name = 'Beta' capacity = 10 bikes = 0 ) ).
|
|
lt_moves = VALUE #(
|
|
( move_no = 1 source_id = 'S001' target_id = 'S002' quantity = 4 ) ).
|
|
|
|
given( it_stations = lt_stations it_moves = lt_moves ).
|
|
rebalance( IMPORTING ev_applied = lv_applied ev_rejected = lv_rejected ).
|
|
|
|
cl_abap_unit_assert=>assert_equals( act = lv_applied exp = 0 ).
|
|
cl_abap_unit_assert=>assert_equals( act = lv_rejected exp = 1 ).
|
|
cl_abap_unit_assert=>assert_equals( act = bikes_of( 'S001' ) exp = 1 ).
|
|
cl_abap_unit_assert=>assert_equals( act = bikes_of( 'S002' ) exp = 0 ).
|
|
ENDMETHOD.
|
|
|
|
METHOD dry_run.
|
|
DATA lt_stations TYPE ty_stations.
|
|
DATA lt_moves TYPE ty_moves.
|
|
DATA lv_applied TYPE i.
|
|
DATA lv_rejected TYPE i.
|
|
|
|
lt_stations = VALUE #(
|
|
( station_id = 'S001' station_name = 'Alpha' capacity = 10 bikes = 5 )
|
|
( station_id = 'S002' station_name = 'Beta' capacity = 10 bikes = 0 ) ).
|
|
lt_moves = VALUE #(
|
|
( move_no = 1 source_id = 'S001' target_id = 'S002' quantity = 2 ) ).
|
|
|
|
given( it_stations = lt_stations it_moves = lt_moves ).
|
|
rebalance( EXPORTING iv_dry_run = abap_true
|
|
IMPORTING ev_applied = lv_applied ev_rejected = lv_rejected ).
|
|
|
|
cl_abap_unit_assert=>assert_equals( act = lv_applied exp = 1 ).
|
|
cl_abap_unit_assert=>assert_equals( act = lv_rejected exp = 0 ).
|
|
cl_abap_unit_assert=>assert_equals( act = bikes_of( 'S001' ) exp = 5 ).
|
|
cl_abap_unit_assert=>assert_equals( act = bikes_of( 'S002' ) exp = 0 ).
|
|
ENDMETHOD.
|
|
|
|
ENDCLASS.
|