Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unit test with OSQL Doubles
CLASS ltc_material_stats DEFINITION FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS
FINAL.
PRIVATE SECTION.
TYPES:
tt_mara TYPE TABLE OF mara WITH EMPTY KEY,
tt_makt TYPE TABLE OF makt WITH EMPTY KEY,
tt_marc TYPE TABLE OF marc WITH EMPTY KEY,
tt_plant_aggr TYPE TABLE OF ztest_c_material_plant_aggr WITH EMPTY KEY.
CLASS-DATA:
sr_env TYPE REF TO if_osql_test_environment.
CLASS-METHODS class_setup.
CLASS-METHODS class_teardown.
METHODS setup.
METHODS some_data FOR TESTING RAISING cx_static_check.
METHODS no_data FOR TESTING RAISING cx_static_check.
ENDCLASS.
CLASS ltc_material_stats IMPLEMENTATION.
METHOD class_setup.
sr_env = cl_osql_test_environment=>create(
VALUE #(
( 'MARA' )
( 'MAKT' )
( 'MARC' )
( 'ZTEST_C_MATERIAL_PLANT_AGGR' )
)
).
ENDMETHOD.
METHOD class_teardown.
sr_env->destroy( ).
ENDMETHOD.
METHOD setup.
sr_env->clear_doubles( ).
ENDMETHOD.
METHOD some_data.
DATA(lt_mara) = VALUE tt_mara(
(
matnr = '123'
ersda = '20180505'
ernam = 'John Doe'
)
).
DATA(lt_makt) = VALUE tt_makt(
(
matnr = '123'
spras = sy-langu
maktx = 'Good material'
)
).
DATA(lt_marc) = VALUE tt_marc(
(
matnr = '123'
werks = '456'
pstat = 'L'
)
(
matnr = '123'
werks = '789'
pstat = 'L'
)
).
DATA(lt_aggr) = VALUE tt_plant_aggr(
(
material = '123'
plant = '456'
plantname = 'Perfect plant'
amount = '23.45'
)
(
material = '123'
plant = '789'
plantname = 'Old rusty plant'
amount = '3.1'
)
).
sr_env->insert_test_data(
i_data = lt_mara
).
sr_env->insert_test_data(
i_data = lt_makt
).
sr_env->insert_test_data(
i_data = lt_marc
).
sr_env->insert_test_data(
i_data = lt_aggr
i_parameter_values = VALUE #( ( parameter_name = 'p_year' parameter_value = '2018' ) )
).
DATA(lo_cut) = NEW ztest_material_stats( '123' ).
DATA(lv_exp) = |For material Good material created by John Doe on 05.05.2018 following statistic exist:\n|
&& |\tPlant Perfect plant amount 23.450\n|
&& |\tPlant Old rusty plant amount 3.100\n|.
DATA(lv_act) = lo_cut->get_statistic( 2018 ).
cl_abap_unit_assert=>assert_equals(
act = lv_act
exp = lv_exp
msg = |Failed on some data|
).
ENDMETHOD.
METHOD no_data.
DATA:
lv_error TYPE abap_bool.
DATA(lo_cut) = NEW ztest_material_stats( '123' ).
TRY.
DATA(lv_act) = lo_cut->get_statistic( 2018 ).
CATCH cx_entry_not_found.
lv_error = abap_true.
ENDTRY.
cl_abap_unit_assert=>assert_true(
act = lv_error
msg = |Failed on empty data|
).
ENDMETHOD.
ENDCLASS.
CLASS ztest_material_stats DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
METHODS constructor
IMPORTING
iv_matnr TYPE matnr.
METHODS get_statistic
IMPORTING
iv_year TYPE gjahr
RETURNING
VALUE(rv_statistic) TYPE string
RAISING
cx_entry_not_found.
PRIVATE SECTION.
DATA:
gv_matnr TYPE matnr.
ENDCLASS.
CLASS ztest_material_stats IMPLEMENTATION.
METHOD constructor.
gv_matnr = iv_matnr.
ENDMETHOD.
METHOD get_statistic.
CONSTANTS:
lc_storage TYPE pstat_d VALUE 'L'.
SELECT SINGLE
mara~matnr,
mara~ernam AS name,
mara~ersda AS date,
makt~maktx AS text
FROM mara
LEFT OUTER JOIN makt ON
makt~matnr = mara~matnr AND
makt~spras = @sy-langu
WHERE mara~matnr = @gv_matnr
INTO @DATA(ls_mara).
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE cx_entry_not_found.
ENDIF.
SELECT matnr, werks
FROM marc
WHERE matnr = @gv_matnr
AND pstat = @lc_storage
INTO TABLE @DATA(lt_marc).
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE cx_entry_not_found.
ENDIF.
SELECT *
FROM ztest_c_material_plant_aggr( p_year = @iv_year )
FOR ALL ENTRIES IN @lt_marc
WHERE material = @lt_marc-matnr
AND plant = @lt_marc-werks
INTO TABLE @DATA(lt_plant_stats).
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE cx_entry_not_found.
ENDIF.
rv_statistic = |For material { ls_mara-text } created by { ls_mara-name } on { ls_mara-date DATE = USER } following statistic exist:\n|.
LOOP AT lt_plant_stats ASSIGNING FIELD-SYMBOL(<ls_plant_stats>).
rv_statistic = rv_statistic && |\tPlant { <ls_plant_stats>-plantname } amount { <ls_plant_stats>-amount }\n|.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment