Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagegroovy
..
.. 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 getRetailerSIPrice takes an argument retailerSIRecords, which is expected to be a collection of records. Here's a breakdown of what this function does:

  1. It filters the retailerSIRecords to include only those records where RetailerSIPriceEUR is not null.

  2. It declares a variable retailerSIPrice without initializing it.

  3. It checks if retailerSIRecords is not empty:

    • If not empty, it calculates the sum of RetailerSIPriceEUR across all records and divides it by the number of records (average price), then assigns this value to retailerSIPrice.

    • If empty, it sets retailerSIPrice to null.

  4. Finally, it returns the value of retailerSIPrice.

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 highLowRMLogic based on various target price attributes for countries within the EU3 region.

The code performs the following operations:

  1. 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.

  2. For each country record, it retrieves the country name and then fetches a corresponding record from a map called countryMatrixMap.

  3. It extracts four attributes from the country record: TargetSIPriceLowRM, TargetSIPriceHighRM, TargetSIPriceHighMaxRM, and TargetSIPriceHighMinRM.

  4. Several conditional checks are performed to determine the value of highLowRMLogic:

    • If both TargetSIPriceLowRM and TargetSIPriceHighRM are null, highLowRMLogic remains null.

    • If only TargetSIPriceLowRM is null, highLowRMLogic is set to "Adjusted to High Comp Index Target".

    • If both TargetSIPriceHighMaxRM and TargetSIPriceHighMinRM are not null, further conditions are checked to set highLowRMLogic 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 between TargetSIPriceLowRM, TargetSIPriceHighMaxRM, and TargetSIPriceHighMinRM.

  5. The resulting highLowRMLogic value is then put back into the countryMatrixMap for the respective country.

  6. Finally, the function returns null.

A now - test for such element can be coded like this:

...