An Approach to Unit Testing ADX Functions
Our application contains many functions that return data stored in Azure Data Explorer (ADX). We wrote these functions in Kusto Query Language (KQL) and each function returns a set of data based on the arguments passed. Although developers tested these functions as they wrote them, we needed a way to validate that the functions continued to work as the code and the data changed. Automated Unit testing is an essential part of any application development life cycle. It validates that code works properly and minimizes the risk that future code changes will break existing functionality. In this article, I will discuss the approach we took in automating the testing of ADX functions.
The KQL Assert Function
ADX does not ship with a unit testing framework, but KQL has a static assert function that can be used to test functions and queries.
The assert function accepts two arguments: a Boolean condition and a string. If the Boolean condition returns false, an error is raised, and the string is returned. We can use this to test that our ADX functions perform as expected.
A Sample Test
For example, the following code tests if table1 contains any rows.
The code above throws an exception and outputs “table1 should return some rows” if no data is in table1. We can be more deliberate in our tests and test the flow of data into the table (via Update Policies or into Materialized views) if we populate our tables with well-known data. ADX provides the .ingest command, which allows us to script data ingestion. For small datasets, we can use the .ingest inline command. For larger datasets, we can ingest from Azure storage. Assume we have an AssetHistory table with the data below:
The getassetHistory function listed below filters the AssetHistory table by the input arguments. However, it also has some logic to ignore filters if an argument is not passed to the function.
We can use assert to write some tests. We know in advance the data in the table, so we know how many rows the function should return for a given set of input arguments. Here are some examples:
As you can see, each of the tests above passes a distinct set of arguments to the function, so that each test filters the table for a different time range. Each test validates that the function correctly applies the filters by verifying that it returns the correct number of rows. But it also validates that the function handles things correctly when a null argument is passed or when an argument (e.g., locationInPolygon) is omitted.
We can even test passing dynamic data, as in this function call, which passes a polygon object to the function:
Each of the tests described above expects the data to be in a well-known state, therefore it is important to initialize the data correctly.
For our project, we created automated scripts that would recreate all the database objects; then populate the tables with test data. We run this script before each test run, so we always start with a clean database. This made the data predictable, and it made our function calls idempotent with a given set of inputs.
Other Testing Approaches
Alternatively, we can test a query by using the appropriate SDK to call it from a program written in Java, .NET, or some other high-level language. This has the advantage of allowing us to write tests in a robust testing framework, such as xUnit, or JUnit.
However, this approach adds complexity to our tests and introduces the possibility that errors may occur in our code outside ADX. Keeping all our tests in ADX tends to reduce the amount of code we need to write and focuses the tests on the KQL functions we have written.
The code in this article is based on work done by the Microsoft Commercial Software Engineering GT team. Many of the original tests were written for our customer by Braden Eriksen.
The KQL tests above show how to use the assert function to create automated Unit Tests that validate that our ADX functions are working correctly. This is a powerful tool for improving and maintaining code quality.
You can read more about this pattern at the Microsoft Documentation site.