-
Notifications
You must be signed in to change notification settings - Fork 1
[Advanced] Group Data Suites
The library exposes the GroupDataSuite class (defined in data_checks/classes/group_data_suite.py) which provides an additional abstraction on top of DataSuite.
Suppose you want to run the same check over many different objects. For example assume you have ItemCheck that checks if a certain Item is valid or not using various rules (i.e. quality, price, etc.). You have hundreds of these items. You could:
- Create a suite for each item and run them individually
- Pass the item as an argument to each of the check's rules for all items
However both methods are tedious and inefficient. A
GroupDataSuiteallows you to easily define a group of items and then run the same checks on each item in the group. Begin by subclassing theGroupDataSuiteclass:
from data_checks.classes.group_data_suite import GroupDataSuite
class MyFirstGroupDataSuite(GroupDataSuite):
passThen override the required class methods:
class MyFirstGroupDataSuite(GroupDataSuite):
@classmethod
def suite_config(cls) -> dict:
return {}
@classmethod
def group_name(cls) -> str:
return "item"
@classmethod
def group(cls) -> list:
return [
Item("item1", ...),
Item("item2", ...),
...
]
@classmethod
def group_checks(cls) -> list[type[Check] | str]:
return [
ItemCheck
]In the above example we define several methods:
-
suite_config(cls) -> dict: Define the suite's configuration. The same as inDataSuite(see Data Suite). -
group_name(cls) -> str: Identifier for each element in the group. This is the attribute that will be used to get each element in the specified checks. For example, if the group name is "item", then each element in the group will be accessed throughself.itemin each check. -
group(cls) -> list: List of group's elements. Each element will be passed into the specified checks. Each element must be JSON serializable. In each check, this element can be accessed throughself.group_name(i.e.self.itemin this example). -
group_checks(cls) -> list[type[Check] | str]: Checks to be run on each element in the group. For example:with group[ CheckClass1, CheckClass2, ... ]will run[ element1, element2, ... ]CheckClass1onelement1,CheckClass1onelement2,CheckClass2onelement1, andCheckClass2onelement2.
โIMPORTANT
Note that we are overridinggroup_checksand NOTchecks.
โIMPORTANT
Note that group name and values need to have a__str__method defined (see ๐ Warnings).
๐ That's it! ๐ GroupDataSuite can be instantiated and run in the same manner as DataSuite (see Instantiating and Running Suites ). Now you can access the group name and value in your checks:
from data_checks.data_check import DataCheck
class ItemCheck(DataCheck):
...
def rule_discount_limit(self):
item: Item = self.item
assert_that(
item.discount,
less_than_or_equal_to(
item.price,
),
f"discount should not be greater than price for productId: {item.product_id}",
)
...See the full example in examples/operations/inventory/inventory_suite.py.