Skip to content

How to load mockup with filtering

atsy edited this page Mar 12, 2016 · 4 revisions

There is a feature in load_data() (and also retrieve()) to filter loaded data (if you are not familiar with load_data method please have a look to How to load mockups). Let's assume we have this raw data:

BUKRS  GJAHR  BELNR    BLART  BLDAT       XBLNR  BKTXT
ZZZZ   2016   1000000  KR     01.01.2016  10/1   Invoice for goods (Test case 1)
ZZZZ   2016   1000001  KZ     10.01.2016  88897  Payment for 10/1 (Test case 1)
ZZZZ   2016   2000000  KR     01.02.2016  10/2   Invoice for goods (Test case 2)
ZZZZ   2016   2000001  KZ     10.02.2016  90822  Payment for 10/2 (Test case 2)
ZZZZ   2016   3000000  KR     01.03.2016  10/3   Invoice for goods (Test case 3)
ZZZZ   2016   3000001  KZ     10.03.2016  76022  Payment for 10/3 (Test case 3)

Assume you want to load just lines with document numbers (BELNR) between 2000000 and 2999999. Maybe you have several test cases in you unit test set and document number range is an indicator of test case (1000000 for case 1, 2000000 for case 2, etc).

Here is how to use filtering

  1. You have to define a structure, each component of which would be named after correspondent field to filter. The type of components must be range with appropriate datatype. The structure may contain multiple ranges (which would all apply as additional filters simultaneously). If the component name is not found in the target structure it is ignored, however each component must be a range table.
  data:
    begin of l_where,
      belnr  type range of belnr_d,
    end of l_where,
    r_belnr  like line of l_where-belnr,
  1. Then you initialize the ranges with desired conditions:
  r_belnr-sign   = 'I'.
  r_belnr-option = 'BT'.
  r_belnr-low    = '0002000000'. " In real code you will probably have some 
  r_belnr-high   = '0002999999'. " variable definning the current test case
  append r_&1 to l_where-&1.
  1. And feed it to load_mockup() as I_WHERE parameter:
  data lt_bkpf type table of bkpf.

  call method o_ml->load_data
    exporting i_obj       = 'TEST1/bkpf' 
              i_strict    = abap_false
              i_where     = l_where     "  <<<<<
    importing e_container = lt_bkpf.
  1. That's it :)

Even simpler since 0.1.6 !

In v0.1.6 two new filtering format were introduced (hopefully simpler ones ;)

  • Plain string filter. A really strainght forward one:
" select just lines with field BELNR = 0002000000
o_ml->load_data( ... i_where = 'BELNR = 0002000000' ... ).
  • Another form of range filter - hash-like structure (or also table) of zcl_mockup_loader=>ty_where type - a pair of string name of a field to filter and range (see REFERENCE.md ).

Should be quite convenient in ABAP 7.4:

types: r_buk type range of bukrs. " Maybe already defined for you somewhere
data(lr) = value r_buk( ( sign = 'I' option = 'EQ' low = '1000' ) ).

o_ml->load_data( ... 
  i_where = value zcl_mockup_loader=>ty_where( name = 'BUKRS' range = ref #( lr ) ) 
  ...).