Skip to content

Commit

Permalink
add universal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hannhegg committed Apr 22, 2025
1 parent cf4f89b commit 2dc34ca
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 43 deletions.
4 changes: 2 additions & 2 deletions tests/unit/test_4_one_day.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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:
Expand Down
54 changes: 18 additions & 36 deletions tests/unit/test_clean_data.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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 = {
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/test_format_yeardata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2dc34ca

Please sign in to comment.