diff --git a/dags/data_utils/matomo_pull/test_matomo_campain_helper.py b/dags/data_utils/matomo_pull/test_matomo_campain_helper.py new file mode 100644 index 0000000..5d0e2b0 --- /dev/null +++ b/dags/data_utils/matomo_pull/test_matomo_campain_helper.py @@ -0,0 +1,48 @@ +from matomo_campaign_helper import process_dataframe_for_campaign +import unittest +import pandas as pd + +class TestDataframeProcessing(unittest.TestCase): + + def setUp(self): + """Setting up mocked data""" + # Sample data with raw columns that match the ones the function will process + self.mapping_raw = pd.DataFrame({ + 'MarketingCampaignsReporting_CombinedKeywordContent': ['Content A'], + 'MarketingCampaignsReporting_CampaignName': ['Campaign 1'], + 'MarketingCampaignsReporting_CampaignSource': ['Source A'], + 'MarketingCampaignsReporting_CampaignMedium': ['Medium A'], + 'MarketingCampaignsReporting_CampaignSourceMedium': ['Source A - Medium A'] + }) + + def test_column_renaming(self): + """Test that the columns are properly renamed.""" + + renamed_df = process_dataframe_for_campaign(self.mapping_raw) + + # Expected renamed columns + renamed_columns = ['campaign_content', 'campaign_name', 'campaign_source', 'campaign_medium'] + + self.assertEqual(list(renamed_df.columns), renamed_columns) + + def test_combined_column_split(self): + """Test that the 'CampaignSourceMedium' column is properly split.""" + + renamed_df = process_dataframe_for_campaign(self.mapping_raw) + + # Check if the 'campaign_source' and 'campaign_medium' columns are correctly split + self.assertEqual(renamed_df['campaign_source'][0], 'Source A') + self.assertEqual(renamed_df['campaign_medium'][0], 'Medium A') + + #Reduntant but kept as documentation + def test_column_dropped(self): + """Test that the 'CampaignSourceMedium' column is dropped after splitting.""" + + renamed_df = process_dataframe_for_campaign(self.mapping_raw) + + # Ensure that 'MarketingCampaignsReporting_CampaignSourceMedium' is no longer in the columns + self.assertNotIn('MarketingCampaignsReporting_CampaignSourceMedium', renamed_df.columns) + + +if __name__ == '__main__': + unittest.main() diff --git a/dags/data_utils/matomo_pull/test_matomo_url.py b/dags/data_utils/matomo_pull/test_matomo_url.py new file mode 100644 index 0000000..c9bb1a4 --- /dev/null +++ b/dags/data_utils/matomo_pull/test_matomo_url.py @@ -0,0 +1,70 @@ +from matomo_url import get_matomo_base_url, construct_url +import unittest +from unittest.mock import patch, MagicMock +from airflow.hooks.base import BaseHook + + +class TestMatomoURLFunctions(unittest.TestCase): + + @patch('airflow.hooks.base.BaseHook.get_connection') + def test_get_matomo_base_url(self, mock_get_connection): + """ + Test that get_matomo_base_url retrieves the URL properly. + """ + + # Mocking connection + mock_connection = MagicMock() + mock_connection.host = 'https://matomo.example.com/' + mock_connection.password = 'test_token' + mock_get_connection.return_value = mock_connection + + # Expected URL + matomo_site_id = 1 + expected_url = 'https://matomo.example.com/index.php?module=API&format=JSON&token_auth=test_token&idSite=1' + + + retrieved_url = get_matomo_base_url(matomo_site_id) + + # Check that the retreived URL matches de expected URL + self.assertEqual(retrieved_url, expected_url) + + @patch('airflow.hooks.base.BaseHook.get_connection') + def test_construct_url(self, mock_get_connection): + """ + Test that construct_url correctly constructs the URL with the proper parameters. + """ + + # Mocking connection + mock_connection = MagicMock() + mock_connection.host = 'https://matomo.example.com/' + mock_connection.password = 'test_token' + mock_get_connection.return_value = mock_connection + + # Setup the base_url and config + + base_url = 'https://matomo.example.com/index.php?module=API&format=JSON&token_auth=test_token&idSite=1' + + config = { + 'date': '2023-03-20', # Date for the API query + 'period': 'day', # Period of the query + 'expanded': 1, # Expanded query for detailed data + 'filter_limit': -1, # No limit on the number of results + } + + day = '2023-03-20' + + result_url = construct_url(base_url, config, day) + + # Expected URL with the real set of parameters + expected_url = ( + 'https://matomo.example.com/index.php?module=API&format=JSON&token_auth=test_token&idSite=1' + '&date=2023-03-20&period=day&expanded=1&filter_limit=-1' + ) + + # Check that the constructed URL matches the expected URL + self.assertEqual(result_url, expected_url) + + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file