diff --git a/test/residual_test/residual_test.py b/test/residual_test/residual_test.py index 8984c6e7daaae8a7a68e9289ca1e074c3f8f8611..726b7bbf8f41b8d3aea0f6b75eb0d684dadf7c3c 100644 --- a/test/residual_test/residual_test.py +++ b/test/residual_test/residual_test.py @@ -1,90 +1,79 @@ from geomagio.residual import SpreadsheetAbsolutesFactory from numpy.testing import assert_almost_equal +import pytest -def get_calculations(): - """ - Tests functionality of SpreadsheetAbsolutesFactory and recalculation of absolutes - """ - # establish SpreadsheetAbsolutesFactory for reading test data from Excel - saf = SpreadsheetAbsolutesFactory() - # Read spreadsheet containing test data - reading = saf.parse_spreadsheet("etc/residual/DED-20140952332.xlsm") - # establish original absolute object - original = reading.absolutes_index() - # recalculate absolute object using Calculation.py - reading.update_absolutes() - # establish recalculated absolute object - result = reading.absolutes_index() +class test_functions: + @staticmethod + def get_absolutes(tmp_path): + """ + Tests functionality of SpreadsheetAbsolutesFactory and recalculation of absolutes + """ + # establish SpreadsheetAbsolutesFactory for reading test data from Excel + saf = SpreadsheetAbsolutesFactory() + # Read spreadsheet containing test data + reading = saf.parse_spreadsheet(path=tmp_path) + # establish original absolute object + original = reading.absolutes_index() + # recalculate absolute object using Calculation.py + reading.update_absolutes() + # establish recalculated absolute object + result = reading.absolutes_index() + return original, result + + @staticmethod + def assert_absolutes(original, result): + """ + Compares calculation results to original absolutes from spreadsheet + """ + assert_almost_equal( + [original["H"].absolute, original["H"].baseline], + [result["H"].absolute, result["H"].baseline], + decimal=4, + verbose=True, + ) + assert_almost_equal( + [original["D"].absolute, original["D"].baseline], + [result["D"].absolute, result["D"].baseline], + decimal=4, + verbose=True, + ) + assert_almost_equal( + [original["Z"].absolute, original["Z"].baseline], + [result["Z"].absolute, result["Z"].baseline], + decimal=4, + verbose=True, + ) + + +@pytest.fixture +def test_session(): + return test_functions -def test_DED_20140952332(): +def test_DED_20140952332(test_session): """ Compare calulations to original absolutes obejct from Spreadsheet. Tests gathering of Dedhorse's metadata for use by calculations. Tests calculations for measurements in units of DMS. """ - # establish SpreadsheetAbsolutesFactory for reading test data from Excel - saf = SpreadsheetAbsolutesFactory() - # Read spreadsheet containing test data - reading = saf.parse_spreadsheet("etc/residual/DED-20140952332.xlsm") - # establish original absolute object - original = reading.absolutes_index() - # recalculate absolute object using Calculation.py - reading.update_absolutes() - # establish recalculated absolute object - result = reading.absolutes_index() - assert_almost_equal( - [original["H"].absolute, original["H"].baseline], - [result["H"].absolute, result["H"].baseline], - decimal=4, - verbose=True, - ) - assert_almost_equal( - [original["D"].absolute, original["D"].baseline], - [result["D"].absolute, result["D"].baseline], - decimal=4, - verbose=True, - ) - assert_almost_equal( - [original["Z"].absolute, original["Z"].baseline], - [result["Z"].absolute, result["Z"].baseline], - decimal=4, - verbose=True, + # gather absolute from DED test data and recalculate + original, result = test_session.get_absolutes( + tmp_path="etc/residual/DED-20140952332.xlsm" ) + # test results with original spreadsheet values + test_session.assert_absolutes(original, result) -def test_BRW_20133650000(): +def test_BRW_20133650000(test_session): """ Compare calulations to original absolutes obejct from Spreadsheet. Tests gathering of BRW's metadata for use by calculations. Tests calculations for measurements in units of DM. """ - # establish SpreadsheetAbsolutesFactory for reading test data from Excel - saf = SpreadsheetAbsolutesFactory() - # Read spreadsheet containing test data - reading = saf.parse_spreadsheet("etc/residual/BRW-20133650000.xlsm") - # establish original absolute object - original = reading.absolutes_index() - # recalculate absolute object using Calculation.py - reading.update_absolutes() - # establish recalculated absolute object - result = reading.absolutes_index() - assert_almost_equal( - [original["H"].absolute, original["H"].baseline], - [result["H"].absolute, result["H"].baseline], - decimal=4, - verbose=True, - ) - assert_almost_equal( - [original["D"].absolute, original["D"].baseline], - [result["D"].absolute, result["D"].baseline], - decimal=4, - verbose=True, - ) - assert_almost_equal( - [original["Z"].absolute, original["Z"].baseline], - [result["Z"].absolute, result["Z"].baseline], - decimal=4, - verbose=True, + # gather absolute from BRW test data and recalculate + original, result = test_session.get_absolutes( + tmp_path="etc/residual/BRW-20133650000.xlsm" ) + # test results with original spreadsheet values + test_session.assert_absolutes(original, result)