" ! @testing
CLASS zcl_vehicle_info DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
TYPES: BEGIN OF ENUM vehicle_make,
car,
bike,
truck,
END OF ENUM vehicle_make.
TYPES: float TYPE p LENGTH 10 DECIMALS 2.
METHODS calc_fuel_price
IMPORTING
vehicle_type TYPE vehicle_make
fuel_rate TYPE float
distance TYPE float
RETURNING
VALUE(fuel_price) TYPE float.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_vehicle_info IMPLEMENTATION.
METHOD calc_fuel_price.
IF fuel_rate <= 0 or distance <= 0.
fuel_price = 0.
EXIT.
ENDIF.
CASE vehicle_type.
WHEN car.
fuel_price = ( distance * fuel_rate ) / 20.
WHEN bike.
fuel_price = ( distance * fuel_rate ) / 40.
WHEN truck.
fuel_price = ( distance * fuel_rate ) / 5.
ENDCASE.
ENDMETHOD.
ENDCLASS.
"! @testing zcl_vehicle_info
CLASS zcl_vehicle_info_test DEFINITION
FOR TESTING
FINAL
DURATION SHORT
RISK LEVEL HARMLESS
PUBLIC.
PRIVATE SECTION.
METHODS setup.
METHODS test_fuel_price_bike FOR TESTING.
METHODS test_fuel_price_car FOR TESTING.
METHODS test_fuel_price_truck FOR TESTING.
METHODS test_fuel_price_invalid FOR TESTING.
DATA: vehicle_ref TYPE REF TO zcl_vehicle_info.
ENDCLASS.
CLASS zcl_vehicle_info_test IMPLEMENTATION.
METHOD setup.
vehicle_ref = NEW #( ).
ENDMETHOD.
METHOD test_fuel_price_bike.
cl_abap_unit_assert=>assert_equals(
act = vehicle_ref->calc_fuel_price( vehicle_type = vehicle_ref->bike
distance = 200
fuel_rate = 95 )
exp = 475
msg = `Wrong Fuel Price`
quit = cl_aunit_assert=>no ).
ENDMETHOD.
METHOD test_fuel_price_car.
cl_abap_unit_assert=>assert_equals(
act = vehicle_ref->calc_fuel_price( vehicle_type = vehicle_ref->car
distance = 100
fuel_rate = 95 )
exp = 475
msg = `Wrong Fuel Price`
quit = cl_aunit_assert=>no ).
ENDMETHOD.
METHOD test_fuel_price_truck.
cl_abap_unit_assert=>assert_equals(
act = vehicle_ref->calc_fuel_price( vehicle_type = vehicle_ref->truck
distance = 100
fuel_rate = 85 )
exp = 1700
msg = `Wrong Fuel Price`
quit = cl_aunit_assert=>no ).
ENDMETHOD.
METHOD test_fuel_price_invalid.
cl_abap_unit_assert=>assert_equals(
act = vehicle_ref->calc_fuel_price( vehicle_type = vehicle_ref->bike
distance = -10
fuel_rate = 95 )
exp = 0
msg = `Wrong Fuel Price`
quit = cl_aunit_assert=>no ).
ENDMETHOD.
ENDCLASS.
@AbapCatalog.sqlViewName: 'ZDVSMEALD'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Calculate Meal Discount'
define view ZCDS_SMEAL_CALC_DISC
as select from smeal
{
key carrid as Carrid,
key mealnumber as Mealnumber,
mealtype as Mealtype,
case mealtype
when 'VE' then 10
when 'FI' then 20
when 'FL' then 15
else 5
end as disc_percent
}
"! @testing ZCDS_SMEAL_CALC_DISC
CLASS zcl_test_meal_disc DEFINITION FINAL FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS PUBLIC.
PRIVATE SECTION.
CLASS-METHODS class_setup.
CLASS-METHODS class_teardown.
METHODS setup.
METHODS test_veg_disc FOR TESTING.
METHODS test_fish_disc FOR TESTING.
METHODS test_nonveg_disc FOR TESTING.
METHODS test_unknown_disc FOR TESTING.
CLASS-DATA:
environment TYPE REF TO if_cds_test_environment.
ENDCLASS.
CLASS zcl_test_meal_disc IMPLEMENTATION.
METHOD class_setup.
environment = cl_cds_test_environment=>create( i_for_entity = 'ZCDS_SMEAL_CALC_DISC' ).
ENDMETHOD.
METHOD class_teardown.
environment->destroy( ).
ENDMETHOD.
METHOD setup.
environment->clear_doubles( ).
ENDMETHOD.
METHOD test_veg_disc.
DATA: smeal TYPE TABLE OF smeal,
act_results TYPE TABLE OF zcds_smeal_calc_disc,
exp_results TYPE TABLE OF zcds_smeal_calc_disc,
test_data TYPE REF TO if_cds_test_data.
smeal = VALUE #( ( mandant = sy-mandt carrid = 'AA' mealnumber = 10 mealtype = 'VE' ) ).
test_data = cl_cds_test_data=>create( i_data = smeal ).
"Create test double
DATA(smeal_double) = environment->get_double( i_name = 'SMEAL' ).
"Insert data into test double
smeal_double->insert( test_data ).
"Fetch data from test double
SELECT * FROM zcds_smeal_calc_disc INTO TABLE @act_results.
"Prepare expected output
exp_results = VALUE #( ( carrid = 'AA' mealnumber = 10 mealtype = 'VE' disc_percent = 10 ) ).
cl_abap_unit_assert=>assert_equals(
act = act_results[ 1 ]-disc_percent
exp = exp_results[ 1 ]-disc_percent ).
ENDMETHOD.
METHOD test_fish_disc.
DATA: smeal TYPE TABLE OF smeal,
act_results TYPE TABLE OF zcds_smeal_calc_disc,
exp_results TYPE TABLE OF zcds_smeal_calc_disc,
test_data TYPE REF TO if_cds_test_data.
smeal = VALUE #( ( mandant = sy-mandt carrid = 'AA' mealnumber = 10 mealtype = 'FI' ) ).
test_data = cl_cds_test_data=>create( i_data = smeal ).
DATA(smeal_double) = environment->get_double( i_name = 'SMEAL' ).
smeal_double->insert( test_data ).
SELECT * FROM zcds_smeal_calc_disc INTO TABLE @act_results.
exp_results = VALUE #( ( carrid = 'AA' mealnumber = 10 mealtype = 'FI' disc_percent = 20 ) ).
cl_abap_unit_assert=>assert_equals(
act = act_results[ 1 ]-disc_percent
exp = exp_results[ 1 ]-disc_percent ).
ENDMETHOD.
METHOD test_nonveg_disc.
DATA: smeal TYPE TABLE OF smeal,
act_results TYPE TABLE OF zcds_smeal_calc_disc,
exp_results TYPE TABLE OF zcds_smeal_calc_disc,
test_data TYPE REF TO if_cds_test_data.
smeal = VALUE #( ( mandant = sy-mandt carrid = 'AA' mealnumber = 10 mealtype = 'FL' ) ).
test_data = cl_cds_test_data=>create( i_data = smeal ).
DATA(smeal_double) = environment->get_double( i_name = 'SMEAL' ).
smeal_double->insert( test_data ).
SELECT * FROM zcds_smeal_calc_disc INTO TABLE @act_results.
exp_results = VALUE #( ( carrid = 'AA' mealnumber = 10 mealtype = 'FL' disc_percent = 15 ) ).
cl_abap_unit_assert=>assert_equals(
act = act_results[ 1 ]-disc_percent
exp = exp_results[ 1 ]-disc_percent ).
ENDMETHOD.
METHOD test_unknown_disc.
DATA: smeal TYPE TABLE OF smeal,
act_results TYPE TABLE OF zcds_smeal_calc_disc,
exp_results TYPE TABLE OF zcds_smeal_calc_disc,
test_data TYPE REF TO if_cds_test_data.
smeal = VALUE #( ( mandant = sy-mandt carrid = 'AA' mealnumber = 10 mealtype = 'ZZ' ) ).
test_data = cl_cds_test_data=>create( i_data = smeal ).
DATA(smeal_double) = environment->get_double( i_name = 'SMEAL' ).
smeal_double->insert( test_data ).
SELECT * FROM zcds_smeal_calc_disc INTO TABLE @act_results.
exp_results = VALUE #( ( carrid = 'AA' mealnumber = 10 mealtype = 'ZZ' disc_percent = 7 ) ).
cl_abap_unit_assert=>assert_equals(
act = act_results[ 1 ]-disc_percent
exp = exp_results[ 1 ]-disc_percent ).
ENDMETHOD.
ENDCLASS.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
4 | |
3 | |
3 | |
2 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 |