...
Code Block | ||
---|---|---|
| ||
.. .. Some element code not important for this sample... .. def getRetailerSIPrice(retailerSIRecords) { retailerSIRecords = retailerSIRecords.findAll { it.RetailerSIPriceEUR != null } // Remove all null values in RetailerSIPriceEUR def retailerSIPrice if (retailerSIRecords) { retailerSIPrice = retailerSIRecords?.RetailerSIPriceEUR?.sum() / retailerSIRecords?.size() } else { retailerSIPrice = null } return retailerSIPrice } |
Infotip |
---|
CODE REVIEW: The function
The purpose of this function is to calculate the average Retailer Selling Price in Euros (if available) from a collection of retailer sales records. |
...
Info |
---|
NOTE: Function return calculated result for this sample == 11.5. |
Complex Test Definitions
The test definitions cover various This section covers various comples scenarios, including mocking API functions such as findLookupTable or find, mocking the "stream," mocking the result of another element, mocking api.global or api.local variables, mocking functions from a shared library, testing a function that modifies local or global tables and always returns null, and testing a function using a function from another element.
This section contains these following sample test definitiondefinitions:
Mock api function, e.g. findLookupTable or find.
Mock “stream”
Mock another element result
Mock api.global or api.local variables
Mock function from (shared) Library
Test function if it modifies local or global table and always returns null
Test function using function from another element
...
Test Function with Local or Global table
In this section, we discuss testing a function that modifies local or global tables and always returns null. It involves creating a test case to verify the expected behavior when the function returns null, as well as checking if the function modifies the local or global table as intended. This can be accomplished by setting up the necessary input parameters and mock data for the function and then asserting that the returned result matches the expected outcome.
Let’s have a element that modifies local / global table and always returns null. The questions is - how to test such element if return value is always the same (null)? Well, it is not that hard as you may think.
...
Code Block |
---|
if (out.EU3LPG) { for (eu3CountryRecord in api.global.EU3CountryArray) { def highLowRMLogic = null def EU3countryName = eu3CountryRecord.attribute1 def eu3countryRecord = api.local.countryMatrixMap.get(EU3countryName) def targetSIPriceLowRM = eu3countryRecord.get("TargetSIPriceLowRM") def targetSIPriceHighRM = eu3countryRecord.get("TargetSIPriceHighRM") def targetSIPriceHighMaxRM = eu3countryRecord.get("TargetSIPriceHighMaxRM") def targetSIPriceHighMinRM = eu3countryRecord.get("TargetSIPriceHighMinRM") if (targetSIPriceLowRM == null && targetSIPriceHighRM == null) { highLowRMLogic = null } else if (targetSIPriceLowRM == null) { highLowRMLogic = "Adjusted to High Comp Index Target" } else if (targetSIPriceHighMaxRM != null && targetSIPriceHighMinRM != null) { if (targetSIPriceHighMaxRM <= targetSIPriceLowRM && targetSIPriceLowRM <= targetSIPriceHighMinRM) { highLowRMLogic = "Adjusted to Low Comp Index Target" } else if (targetSIPriceLowRM > targetSIPriceHighMinRM) { highLowRMLogic = "Adjusted to High Min Comp Index Target" } else if (targetSIPriceLowRM < targetSIPriceHighMinRM) { highLowRMLogic = "Adjusted to High Max Comp Index Target" } } api.local.countryMatrixMap.get(EU3countryName).put("HighLowRMLogic", highLowRMLogic) } } return null |
Tip |
---|
CODE REVIEW: a snippet of code written in Groovy, that seems to be part of a larger function or process that is evaluating certain conditions to determine a logic called |
The code performs the following operations:
It checks if the
out.EU3LPG
condition is true. If so, it proceeds to iterate over an array of country records using a for-loop.For each country record, it retrieves the country name and then fetches a corresponding record from a map called
countryMatrixMap
.It extracts four attributes from the country record:
TargetSIPriceLowRM
,TargetSIPriceHighRM
,TargetSIPriceHighMaxRM
, andTargetSIPriceHighMinRM
.Several conditional checks are performed to determine the value of
highLowRMLogic
:If both
TargetSIPriceLowRM
andTargetSIPriceHighRM
are null,highLowRMLogic
remains null.If only
TargetSIPriceLowRM
is null,highLowRMLogic
is set to "Adjusted to High Comp Index Target".If both
TargetSIPriceHighMaxRM
andTargetSIPriceHighMinRM
are not null, further conditions are checked to sethighLowRMLogic
to one of the three values: "Adjusted to Low Comp Index Target", "Adjusted to High Min Comp Index Target", or "Adjusted to High Max Comp Index Target" based on comparisons betweenTargetSIPriceLowRM
,TargetSIPriceHighMaxRM
, andTargetSIPriceHighMinRM
.
The resulting
highLowRMLogic
value is then put back into thecountryMatrixMap
for the respective country.Finally, the function returns null.
A now - test for such element can be coded like this:
...