From bc3c3a11a7500f98e6b2a714e0abcb8833296fd6 Mon Sep 17 00:00:00 2001 From: frankknoll Date: Tue, 3 Oct 2023 21:49:37 +0200 Subject: [PATCH] starting RegionCountsByClickedBatchcodeProviderTest --- ...CountryCountsByClickedBatchcodeProvider.py | 1 + .../RegionCountsByClickedBatchcodeProvider.py | 28 +++++++++++++++++++ ...ionCountsByClickedBatchcodeProviderTest.py | 25 +++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 src/GoogleAnalytics/RegionCountsByClickedBatchcodeProvider.py create mode 100644 src/GoogleAnalytics/RegionCountsByClickedBatchcodeProviderTest.py diff --git a/src/CountryCountsByClickedBatchcodeProvider.py b/src/CountryCountsByClickedBatchcodeProvider.py index 917cb176c64..1a2ddeb5390 100644 --- a/src/CountryCountsByClickedBatchcodeProvider.py +++ b/src/CountryCountsByClickedBatchcodeProvider.py @@ -42,6 +42,7 @@ class CountryCountsByClickedBatchcodeProvider: }, index_columns = ['COUNTRY']) + # FK-TODO: move this method to a new class named GoogleAnalytics.GoogleAnalyticsReader @staticmethod def _read_csv(file, columns, index_columns): dataframe = pd.read_csv(file, index_col = 0, skiprows = [0, 1, 2, 3, 4, 5, 7]) diff --git a/src/GoogleAnalytics/RegionCountsByClickedBatchcodeProvider.py b/src/GoogleAnalytics/RegionCountsByClickedBatchcodeProvider.py new file mode 100644 index 00000000000..dd80a9790be --- /dev/null +++ b/src/GoogleAnalytics/RegionCountsByClickedBatchcodeProvider.py @@ -0,0 +1,28 @@ +import pandas as pd +from GoogleAnalytics.ResolutionProvider import Resolution, ResolutionProvider +from CountryCountsByClickedBatchcodeProvider import CountryCountsByClickedBatchcodeProvider + +class RegionCountsByClickedBatchcodeProvider: + + @staticmethod + def getRegionCountsByClickedBatchcode(file): + cityCountsByClickedBatchcodeTable = RegionCountsByClickedBatchcodeProvider._getCityCountsByClickedBatchcode(file) + return (cityCountsByClickedBatchcodeTable + .groupby(['VAX_LOT', 'COUNTRY', 'REGION']) + .agg(REGION_COUNT_BY_VAX_LOT = + pd.NamedAgg( + column = 'CITY_COUNT_BY_VAX_LOT', + aggfunc = sum))) + + # FK-TODO: delegate same method CountryCountsByClickedBatchcodeProvider._getCityCountsByClickedBatchcode() to here + @staticmethod + def _getCityCountsByClickedBatchcode(file): + return CountryCountsByClickedBatchcodeProvider._read_csv( + file = file, + columns = { + 'Country': 'COUNTRY', + 'Region': 'REGION', + 'City': 'CITY', + 'Event count': 'CITY_COUNT_BY_VAX_LOT' + }, + index_columns = ['COUNTRY', 'REGION', 'CITY']) diff --git a/src/GoogleAnalytics/RegionCountsByClickedBatchcodeProviderTest.py b/src/GoogleAnalytics/RegionCountsByClickedBatchcodeProviderTest.py new file mode 100644 index 00000000000..8e7da42bbdb --- /dev/null +++ b/src/GoogleAnalytics/RegionCountsByClickedBatchcodeProviderTest.py @@ -0,0 +1,25 @@ +import unittest +from pandas.testing import assert_frame_equal +from TestHelper import TestHelper +import pandas as pd +from GoogleAnalytics.RegionCountsByClickedBatchcodeProvider import RegionCountsByClickedBatchcodeProvider + +class RegionCountsByClickedBatchcodeProviderTest(unittest.TestCase): + + def test_getRegionCountsByClickedBatchcode(self): + # Given + + # When + regionCountsByClickedBatchcodeTable = RegionCountsByClickedBatchcodeProvider.getRegionCountsByClickedBatchcode('src/testdata/GoogleAnalytics/CountryByBatchcode 20230730-20230929.csv') + + # Then + assert_frame_equal( + regionCountsByClickedBatchcodeTable, + TestHelper.createDataFrame( + columns = ['REGION_COUNT_BY_VAX_LOT'], + data = [ [100], + [10 + 20]], + index = pd.MultiIndex.from_tuples( + names = ['VAX_LOT', 'COUNTRY', 'REGION'], + tuples = [['#003B21A', 'United States', 'California'], + ['000086A', 'Germany', 'Bavaria']])))