Custom
A custom check enables a user to write complex test condition (expression) for one or more source/ target attributes using Apache Groovy & Java.
In data testing, the source and target dataset attributes are mapped to Groovy variables with equivalent Java data type classes. This allows users to write simple or complex expressions using any pre-built or user-defined Groovy or Java functions. It is important to note that Groovy and Java functions are designed to work with specific data types. For example, a function that adds two numbers together cannot be applied to a column of strings.
Expression Syntax
Below table shows how to use columns and parameters when writing an expression.
How To | Notation | Example |
---|---|---|
Use a source attribute in an expression | S.[column name] | S.[email] |
Use a target attribute in an expression | T.[column name] | T.[business_email] |
Use a parameter key in an expression | #parameter-key# | S.[load_datekey] == #datekey# |
In Groovy/ Java =
is used for assignment and ==
is used for assertion.
- Lists all the source and target attributes, parameter keys and user defined functions that can be used to create an expression
- User can write an expression using Groovy syntax
- User can input sample data value to test the expression
- Outputs test condition (expression) result based on the sample data value
String Examples
Below is the list commonly used string functions.
Lowercase
toLowerCase()
function converts a string to a lowercase.
S.[country].toLowerCase() == S.[country]
Uppercase
toUpperCase()
function converts a string to a uppercase.
S.[state].toUpperCase() in ['NY', 'NJ', 'CT']
Trim
trim()
function removes leading and trailing whitespaces.
S.[email].trim() == T.[email]
Ignore case
equalsIgnoreCase()
Returns true if two strings match irrespective of the case.
S.[name].equalsIgnoreCase(T.[name])
Length
length()
Returns the length of the string.
S.[state_code].length() == 2
Replace string
replace()
Replaces all occurrences of the specified substring with another substring
S.[phone].replace('-') == T.[phone]
Starts with
startsWith()
Returns true if the string starts with the specified substring.
S.[account_name].startsWith('AW')
Ends with
startsWith()
Returns true if the string ends with the specified substring.
S.[account_number].endsWith('XX')
Substring
substring()
Returns a substring of the string.
S.[location].substring(4,6) == 'US'
Capitalize
capitalize()
Converts the first character of the string to uppercase.
S.[firstname] == T.[firstname].capitalize()
Is Integer
isInteger()
Returns true if the string has only numbers.
S.[customerkey].isInteger()
Count
count()
Counts the number of occurrences of a specified value in a string.
S.[accountnumber].count('X') == 2
These are just a few of the many string functions that are available in Groovy. For more information, please refer to the Groovy documentation.
Numeric Examples
Below is the list commonly used numeric/ math functions.
To Float
toFloat()
Transform a Number into a Float .
S.[discount].toFloat() == T.[discount]
Abs
Math.abs()
Returns the absolute value of the number.
Math.abs(S.[count] - T.[count]) == 5
Ceil
Math.ceil()
Returns the smallest integer that is greater than or equal to the number.
Math.ceil(S.[discount]) == T.[discount]
Floor
Math.floor()
Returns the largest integer that is less than or equal to the number.
Math.floor(S.[discount]) == T.[discount]
Round
Math.round()
Returns the closest integer to the number.
Math.round(S.[discount]) == Math.round(T.[discount])
Square root
Math.sqrt()
Returns the square root of the number.
Math.sqrt(S.[column]) in [2,4,8]
Min value
Math.min()
Returns the minimum of two numbers.
Math.min(S.[bloomberg_price] , S.[reuters_price]) == T.[low_ticker_price]
Max value
Math.max()
Returns the maximum of two numbers.
Math.min(S.[bloomberg_price] , S.[reuters_price]) == T.[high_ticker_price]
Regular Expressions
A column can be evaluated against a regular expression using a Match operator ==~
. On successful evaluation it returns TRUE on a strict match.
It can only be applied to string columns. You can convert other datatype columns in to string using toString()
function.
Valid Number
Following regex returns true if the string contains all Numeric values.
S.[customerid] ==~ /[0-9]+/
Email Format
Following regex returns true if it's a valid email format.
S.emailid ==~ /\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
You can create regex patterns and test it using regex 101 - external website
Conditional Statements
Groovy supports If Else and Ternary Operation for evaluating conditional statements. We have observed better performance using Ternary operator.
Following expressions evaluates these conditions.
- If source type is
C, O, LLC
then target should beOrganizations
- Else if source type is
S
then target should beSole Proprietorship
- Otherwise, target type should be
Individual
Ternary Expression
S.customertype in ['C', 'O', 'LLC'] ? T.customertype == 'Organization' :
S.customertype == 'S' ? T.customertype == 'Sole Proprietorship' :
T.customertype == 'Individual'
More information about Ternary operator
If Else Statement
if (S.customertype in ['C', 'O', 'LLC']) {
T.customertype == 'Organization'
} else if (S.customertype == 'S') {
T.customertype == 'Sole Proprietorship'
} else {
T.customertype == 'Individual'
}