From 7cb0d10b88872a413730931851cca8b8e790dea8 Mon Sep 17 00:00:00 2001 From: frankknoll Date: Tue, 6 Jun 2023 17:55:44 +0200 Subject: [PATCH] starting CountryCountsByBatchcodeTable2JsonConverterTest --- ...tryCountsByBatchcodeTable2JsonConverter.py | 19 +++++++ ...ountsByBatchcodeTable2JsonConverterTest.py | 52 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 src/CountryCountsByBatchcodeTable2JsonConverter.py create mode 100644 src/CountryCountsByBatchcodeTable2JsonConverterTest.py diff --git a/src/CountryCountsByBatchcodeTable2JsonConverter.py b/src/CountryCountsByBatchcodeTable2JsonConverter.py new file mode 100644 index 00000000000..5d7c0145f40 --- /dev/null +++ b/src/CountryCountsByBatchcodeTable2JsonConverter.py @@ -0,0 +1,19 @@ +import pandas as pd + +class CountryCountsByBatchcodeTable2JsonConverter: + + @staticmethod + def convert2Json(countryCountsByBatchcodeTable): + return (countryCountsByBatchcodeTable + .reset_index(level = 'COUNTRY') + .rename( + columns = + { + 'COUNTRY': 'countries', + 'COUNTRY_COUNT_BY_VAX_LOT Clicked': 'frequencies guessed', + 'COUNTRY_COUNT_BY_VAX_LOT Before Deletion': 'frequencies before deletion' + }) + .groupby('VAX_LOT') + .apply(lambda countryCountsTable: countryCountsTable.to_dict('list')) + .rename('HISTOGRAM_DESCRIPTION') + .to_frame()) diff --git a/src/CountryCountsByBatchcodeTable2JsonConverterTest.py b/src/CountryCountsByBatchcodeTable2JsonConverterTest.py new file mode 100644 index 00000000000..cbcd27dd29f --- /dev/null +++ b/src/CountryCountsByBatchcodeTable2JsonConverterTest.py @@ -0,0 +1,52 @@ +import unittest +import pandas as pd +from pandas.testing import assert_frame_equal +from TestHelper import TestHelper +from CountryCountsByBatchcodeTable2JsonConverter import CountryCountsByBatchcodeTable2JsonConverter + +class CountryCountsByBatchcodeTable2JsonConverterTest(unittest.TestCase): + + def test_convertCountryCountsByBatchcodeTable2Json(self): + # Given + countryCountsByBatchcodeTable = TestHelper.createDataFrame( + columns = ['COUNTRY_COUNT_BY_VAX_LOT Clicked', 'COUNTRY_COUNT_BY_VAX_LOT Before Deletion'], + data = [ [10, 20], + [15, 30], + [70, 80]], + index = pd.MultiIndex.from_tuples( + names = ['VAX_LOT', 'COUNTRY'], + tuples = [('!D0181', 'Germany'), + ('!D0181', 'Hungary'), + ('# 009C01A', 'Germany')])) + + # When + jsonTable = CountryCountsByBatchcodeTable2JsonConverter.convert2Json(countryCountsByBatchcodeTable) + + # Then + assert_frame_equal( + jsonTable, + TestHelper.createDataFrame( + columns = ['HISTOGRAM_DESCRIPTION'], + data = [ + [ + { + "countries": ["Germany", "Hungary"], + "frequencies guessed": [10, 15], + "frequencies before deletion": [20, 30] + } + ], + [ + { + "countries": ["Germany"], + "frequencies guessed": [70], + "frequencies before deletion": [80] + } + ] + ], + index = pd.Index( + [ + '!D0181', + '# 009C01A' + ], + name = 'VAX_LOT')), + check_dtype = True)