Tiven Wang
Wang Tiven October 08, 2018
425 favorite favorites
bookmark bookmark
share share

Annotations

Consumption

Via these annotations, the specific behavior is defined which is related to the consumption of CDS content. This metadata makes no assumptions about the concrete consumption technology/infrastructure, but it is applicable across multiple consumption technologies (e.g. Analytics or OData).

  • Consumption.derivation: This annotation enables derivation of the value for a parameter or a filter automatically at runtime by selecting a row from a given entity(table).

How to create ABAP CDS View with enabling derivation of the value for parameters

Functions

Date Functions

ABAP CDS - Date Functions and Time Functions

CDS Test Double Framework

CDS Test Double Framework 会切断要测试的 CDS View 的依赖组件(depended-on components (DOC))替换成框架复制的一份依赖组件,避免对原有 ABAP Runtime 对象操作造成数据影响的问题。

Image: CDS Test Double Framework

Image: CDS Test Double Framework

Image: CDS Test Double Framework

SAP ABAP 的包 SABP_UNIT_DOUBLE_CDS 里就是对 ABAP CDS 做测试的框架,里面还有 demo 程序。

Writing Unit Tests Using CDS Test Double Framework

ABAP CDS Unit Test 代码就是普通的 ABAP Unit Test class 代码,只不过其中使用了一些专门的类协助测试环境创建和数据 Mock ,例如类 cl_cds_test_environment 可以用来创建 ABAP CDS 测试环境即 Double 其依赖组件和插入 Mock 数据。

以测试类 CL_CDS_WITH_AGGREGATION 为例,首先设置的是 CDS 测试环境的创建和销毁方法

METHOD class_setup.
  environment = cl_cds_test_environment=>create( i_for_entity = 'CdsFrwk_So_Items_By_TaxRate' ).
ENDMETHOD.

METHOD class_teardown.
  environment->destroy( ).
ENDMETHOD.

METHOD setup.
  environment->clear_doubles( ).
ENDMETHOD.

然后在测试前要为测试环境准备和插入 Mock 数据,使用依赖组件(depended-on components (DOC))类型的内表准备 Mock 数据,使用方法 environment->insert_test_data 向测试环境插入 Mock 数据。

METHOD cuco_1_taxrate_1_item_2_ok.

  "Given
  "Sales Orders
  sales_orders = VALUE #(  ( client = sy-mandt node_key = '01' so_id = 'ID' ) ).
  environment->insert_test_data( i_data = sales_orders ).

  "Given
  "Sales Order Items
  sales_order_items = VALUE #( ( mandt = sy-mandt so_guid = '01' currency_code = 'EUR' gross_amount = '1' tax_rate = '19.00' )
                                ( mandt = sy-mandt so_guid = '01' currency_code = 'EUR' gross_amount = '2' tax_rate = '19.00' ) ).
  environment->insert_test_data( i_data = sales_order_items ).

  " Test
  SELECT * FROM cdsfrwk_so_items_by_taxrate INTO TABLE @act_results.
  
  " Verify
  exp_results = VALUE #( ( so_id = 'ID' currency_code = 'EUR' sum_gross_amount = '3' tax_rate = '19.00' ) ).
  assert_so_items_by_taxrate( exp_results = exp_results ).

ENDMETHOD.

注意这里 Mock 数据内表的类型要使用 ABAP CDS 依赖组件的数据类型,因为 environment->insert_test_data 方法会根据其类型找到相应的依赖组件 Double 出来的对象并插入 Mock 数据。

插入 Mock 数据之后便可以运行 Open SQL 进行实际测试了,使用 Open SQL 读取 ABAP CDS 视图得到想要的结果。这里读取真实的 ABAP CDS View 是怎么得到 Mock 数据的呐?这因为 CDS Test Double Framework 使用了 Open SQL 的一项新功能 Replacement Service, 类 CL_OSQL_REPLACE 可以在 ABAP Unit 单元测试中将数据库访问重定向至访问其它数据库的 Open SQL。如何应用可以参考示例程序 demo_cl_osql_replace

References

Similar Posts

  • SAP Acknowledges
  • SAP in Opensource Foundations
  • SAP HANA Database SAP HANA is an in-memory, column-oriented, relational database management system developed and marketed by SAP SE. Its primary function as database server is to store and retrieve data as requested by the applications. In addition, it performs advanced analytics (predictive analytics, spatial data processing, text analytics, text search, streaming analytics, graph data processing) and includes ETL capabilities as well as an application server.
  • SAP OpenUI5
  • SAP Hybris Cloud for Customer Engage your customers in more meaningful ways with SAP Hybris Cloud for Customer. This cloud CRM portfolio brings sales, customer service, and social CRM together – to help your team form powerful personal connections that drive customer engagement across all channels. Take advantage of rich predictive insight, flawless execution and contextual customer experience relevant to your industry.
  • What's CDS Core Data Services (CDS) is an infrastructure that can be used by developers to create the underlying (persistent) data model which the application services expose to UI clients. CDS is just SQL + Annotation. Annotation is just a Note / Explanation / Comment. Annotation is explained by Evaluation Runtime (Engine). Engine is one of SADL / BOPF / Analytic Manager / Enterprise Search / ABAP / HANA …

Comments

Back to Top