Package: great-assertions
This library is inspired by the Great Expectations library. The library
has made the various expectations found in Great Expectations available
when using the inbuilt python unittest assertions.Installpipinstallgreat-assertionsCode example Pandasfromgreat_assertionsimportGreatAssertionsimportpandasaspdclassGreatAssertionTests(GreatAssertions):deftest_expect_table_row_count_to_equal(self):df=pd.DataFrame({"col_1":[100,200,300],"col_2":[10,20,30]})self.expect_table_row_count_to_equal(df,3)Code example PySparkfromgreat_assertionsimportGreatAssertionsfrompyspark.sqlimportSparkSessionclassGreatAssertionTests(GreatAssertions):defsetUp(self):self.spark=SparkSession.builder.getOrCreate()deftest_expect_table_row_count_to_equal(self):df=self.spark.createDataFrame([{"col_1":100,"col_2":10},{"col_1":200,"col_2":20},{"col_1":300,"col_2":30},])self.expect_table_row_count_to_equal(df,3)List of available assertionsPandasPySparkexpect_table_row_count_to_equalwhite_check_mark::white_check_mark::expect_table_row_count_to_be_greater_thanwhite_check_mark::white_check_mark::expect_table_row_count_to_be_less_thanwhite_check_mark::white_check_mark::expect_table_has_no_duplicate_rowswhite_check_mark::white_check_mark::expect_column_value_to_equalwhite_check_mark::white_check_mark::expect_column_values_to_be_betweenwhite_check_mark::white_check_mark::expect_column_values_to_match_regexwhite_check_mark::white_check_mark::expect_column_values_to_be_in_setwhite_check_mark::white_check_mark::expect_column_values_to_be_of_typewhite_check_mark::white_check_mark::expect_table_columns_to_match_ordered_listwhite_check_mark::white_check_mark::expect_table_columns_to_match_setwhite_check_mark::white_check_mark::expect_date_range_to_be_more_thanwhite_check_mark::white_check_mark::expect_date_range_to_be_less_thanwhite_check_mark::white_check_mark::expect_date_range_to_be_betweenwhite_check_mark::white_check_mark::expect_column_mean_to_be_betweenwhite_check_mark::white_check_mark::expect_column_value_counts_percent_to_be_betweenwhite_check_mark::white_check_mark::expect_frame_equalwhite_check_mark::white_check_mark::expect_column_has_no_duplicate_rowswhite_check_mark::white_check_mark::expect_column_value_to_equal_ifwhite_check_mark::white_check_mark::expect_column_value_to_be_greater_ifwhite_check_mark::white_check_mark::Assertion DescriptionsFor a description of the assertions seeAssertion
DefinitionsRunning the testsExecuting the tests still require unittest, the following options have
been tested with the examples provided.Option 1importunittestsuite=unittest.TestLoader().loadTestsFromTestCase(GreatAssertionTests)runner=unittest.TextTestRunner(verbosity=2)runner.run(suite)Options 2if__name__=='__main__':unittest.main()Pie Charts and TablesFor a more visual representation of the results, when using in Databricks or Jupyter Notebooks.
The results can be outputted as tables or pie-chart.importunittestfromgreat_assertionsimportGreatAssertionResult,GreatAssertionsclassDisplayTest(GreatAssertions):deftest_pass1(self):assertTrueisTruedeftest_fail(self):assert"Hello"=="World"suite=unittest.TestLoader().loadTestsFromTestCase(DisplayTest)test_runner=unittest.runner.TextTestRunner(resultclass=GreatAssertionResult)result=test_runner.run(suite)result.to_barh()#Also available: result.to_pie()result.to_results_table()result.to_full_results_table()Runnng with XML-RunnerTo run with xml-runner, there is no difference to how it’s currently used.
However you will not be able to get method like to_results_table as these use a differentresultclassimportxmlrunnersuite=unittest.TestLoader().loadTestsFromTestCase(DisplayTest)test_runner=xmlrunner.XMLRunner(output="test-results")test_runner.run(suite)Production MonitoringThe assertions provided by GA will also allow the validation of the any environment including Production.
Currently GA only supports saving the results to Spark, for example databricks.Once the run has completed there is asavemethod, as seen below.importxmlrunnersuite=unittest.TestLoader().loadTestsFromTestCase(DisplayTest)test_runner=xmlrunner.XMLRunner(output="test-results")result=test_runner.run(suite)result.save(format="databricks")The image below shows a simple graph of the accumulation of tests over test run.
However much more complex analysis can be performed with the extended data being generated by GA.The extended table of results contains the following:run_idtimestampmethodinformationtest_idstatusextended202112220930292021-12-22 09:30:29test_fail8Traceback (most recent call last…13Fail{“id”: 13, “name”: “expect_date_range_to_be_less_than”, “values”: {“expected_max_date”: “2019-05-13”, “actual_max_date”: “2019-05-13”}}202112220930292021-12-22 09:30:29test_fail9Traceback (most recent call last…14Fail{“id”: 14, “name”: “expect_date_range_to_be_more_than”, “values”: {“expected_min_date”: “2015-10-01”, “actual_min_date”: “2015-10-01”}}From the extended column you can get further details about the type test, which was executed and the results.
For example if we look at the testexpect_table_row_count_to_be_less_thanwe should assert that the max row should not be breached.In the code below, the expected was 100 and the actual was 205, which caused the test to fail.
Therefore Analysts can query the extended data to get a picture of the size of the breach.extended={"id":2,"name":expect_table_row_count_to_be_less_than,"values":{"exp_max_count":100,"act_count":205,},}In production monitoring these types of results can allow the prevention of skewed results.
For example, if you had a result, where the expected values were withing a range of 0-100
and you got an exceptionally large value.The large value could cause business functionality to be skewed such that a defect could causes
damage or loss of income or incorrect reporting to a downstream system.Therefore, GA will allow you to provide benchmarks to the production validation and an
experienced analyst can create reports on top of the data.An example of the extended dataset:NotesIf you get an arrows function warning when running in Databricks, this will happen
because a toPandas() method is being used for many of the assertions. The plan is
to remove Pandas conversion for pure PySpark code. If this is an issue, please raise
an issue so this method can be prioritised. For now, it’s advisable to make sure the
datasets are not too big, which cause the driver to crash.DevelopmentTo create a development environment, create a virtualenv and make a
development installationvirtualenv ve
source ve/bin/activateTo run tests, just use pytest(ve) pytest