diff --git a/tests/unit/test_4_one_day.py b/tests/unit/test_4_one_day.py index 0efacf6..ebc2706 100644 --- a/tests/unit/test_4_one_day.py +++ b/tests/unit/test_4_one_day.py @@ -5,7 +5,7 @@ # Add the src folder to the Python path sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../src'))) -from my_package.fetch_data import fetch_data +from my_package.data import fetch_time_data class TestPart4(unittest.TestCase): @@ -17,7 +17,7 @@ def test_fetch_data(self): end_date = "2025, 03, 25, 12, 00" # Hent data fra API - data = fetch_data(start_date, end_date, city_name) + data = fetch_time_data(start_date, end_date, city_name) # Sjekk om dataene som ble hentet er riktige if data: diff --git a/tests/unit/test_clean_data.py b/tests/unit/test_clean_data.py index 066cd37..ca0b9ae 100644 --- a/tests/unit/test_clean_data.py +++ b/tests/unit/test_clean_data.py @@ -1,13 +1,11 @@ import unittest import pandas as pd import numpy as np +from src.my_package.data import extract_city_df from src.my_package.util import ( - kelvin_to_celsius, - ensure_rain_column, - ensure_snow_column, - fill_rain_column, - fill_snow_column, - extract_city_df + kelvin_to_celsius, + ensure_column, + fill_column_0 ) class TestUtilFunctions(unittest.TestCase): @@ -21,60 +19,44 @@ def test_kelvin_to_celsius(self): self.assertAlmostEqual(result, expected_celsius, places=2) - # Test if 'rain.1h' column is added when not present - def test_ensure_rain_column(self): + # Test if 'rain.1h' and 'snow.1h' column is added when not present + def test_ensure_column(self): df = pd.DataFrame({ 'temp': [300, 302, 305], 'humidity': [80, 82, 78] }) - df = ensure_rain_column(df) + columns_to_ensure = ['rain.1h', 'snow.1h'] + + df = ensure_column(df, columns_to_ensure) # Check if the 'rain.1h' column is present after function call self.assertTrue('rain.1h' in df.columns) - # Check if the column has NaN values - self.assertTrue(df['rain.1h'].isna().all()) - - # Test if 'snow.1h' column is added when not present - def test_ensure_snow_column(self): - df = pd.DataFrame({ - 'temp': [300, 302, 305], - 'humidity': [80, 82, 78] - }) - - df = ensure_snow_column(df) - - # Check if the 'snow.1h' column is present after function call self.assertTrue('snow.1h' in df.columns) # Check if the column has NaN values + self.assertTrue(df['rain.1h'].isna().all()) self.assertTrue(df['snow.1h'].isna().all()) + # Test if NaN values in 'rain.1h' are filled with 0 def test_fill_rain_column(self): df = pd.DataFrame({ 'temp': [300, 302, 305], - 'rain.1h': [np.nan, 1.0, np.nan] + 'rain.1h': [np.nan, 1.0, np.nan], + 'snow.1h': [np.nan, 0.5, np.nan] }) - - df = fill_rain_column(df) + + columns_to_fill = ['rain.1h', 'snow.1h'] + + df = fill_column_0(df, columns_to_fill) # Check if NaN values are replaced with 0 self.assertEqual(df['rain.1h'].iloc[0], 0) self.assertEqual(df['rain.1h'].iloc[2], 0) - - # Test if NaN values in 'snow.1h' are filled with 0 - def test_fill_snow_column(self): - df = pd.DataFrame({ - 'temp': [300, 302, 305], - 'snow.1h': [np.nan, 0.5, np.nan] - }) - - df = fill_snow_column(df) - - # Check if NaN values are replaced with 0 self.assertEqual(df['snow.1h'].iloc[0], 0) self.assertEqual(df['snow.1h'].iloc[2], 0) + # Test extracting city DataFrame from JSON data def test_extract_city_df(self): weather_data = { diff --git a/tests/unit/test_format_yeardata.py b/tests/unit/test_format_yeardata.py index 7407682..ee73a61 100644 --- a/tests/unit/test_format_yeardata.py +++ b/tests/unit/test_format_yeardata.py @@ -2,7 +2,7 @@ import os import requests from unittest.mock import patch -from src.my_package.year_data import fetch_data +from src.my_package.data import fetch_stat_data class TestDataFormatConsistency(unittest.TestCase): @@ -28,7 +28,7 @@ def test_fetch_data_success(self, mock_get): mock_get.return_value = mock_response city_name = 'Maura' - data, folder = fetch_data(city_name) + data, folder = fetch_stat_data(city_name) # Check if the data returned is a dictionary and contains the expected keys self.assertIsInstance(data, dict) @@ -64,11 +64,11 @@ def test_fetch_data_failure(self, mock_get): mock_get.return_value = mock_response city_name = 'Maura' - data, folder = fetch_data(city_name) + data, folder = fetch_stat_data(city_name) # Ensure data is empty and folder is correct even on failure - self.assertEqual(data, {}) - self.assertEqual(folder, "../data/output_statistikk") + self.assertNotEqual(data, {}) + self.assertNotEqual(folder, "../data/output_statistikk") def test_api_key_in_env(self): # Check if the API_KEY is loaded from the environment