Skip to content

Commit

Permalink
refactor: update test_fetch_data.py to use centralized test configura…
Browse files Browse the repository at this point in the history
…tion

- Replaced hardcoded variables (`resource_id`, `limit`, `columns`, `rename_columns`, `sql_query`) with `test_config` fixture for better reusability and maintainability.
- Updated all test cases (`test_fetch_data_api`, `test_fetch_data_sql`, `test_data_consistency`) to use `test_config` for dynamic parameterization.
- Ensured SQL queries and API calls use values from `test_config`.
- Improved code readability and consistency across test cases.
  • Loading branch information
siddharth7113 committed Jan 2, 2025
1 parent 0ee35ad commit c9bab47
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions tests/test_fetch_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,47 @@
pytest tests/test_fetch_data.py -k "fetch_data"
"""

import pytest
from neso_solar_consumer.fetch_data import fetch_data, fetch_data_using_sql


resource_id = "db6c038f-98af-4570-ab60-24d71ebd0ae5"
limit = 5
columns = ["DATE_GMT", "TIME_GMT", "EMBEDDED_SOLAR_FORECAST"]
rename_columns = {
"DATE_GMT": "start_utc",
"TIME_GMT": "end_utc",
"EMBEDDED_SOLAR_FORECAST": "solar_forecast_kw",
}
sql_query = f'SELECT * from "{resource_id}" LIMIT {limit}'


def test_fetch_data_api():
def test_fetch_data_api(test_config):
"""
Test the fetch_data function to ensure it fetches and processes data correctly via API.
"""
df_api = fetch_data(resource_id, limit, columns, rename_columns)
df_api = fetch_data(
test_config["resource_id"],
test_config["limit"],
test_config["columns"],
test_config["rename_columns"],
)
assert not df_api.empty, "fetch_data returned an empty DataFrame!"
assert set(df_api.columns) == set(
rename_columns.values()
test_config["rename_columns"].values()
), "Column names do not match after renaming!"


def test_fetch_data_sql():
def test_fetch_data_sql(test_config):
"""
Test the fetch_data_using_sql function to ensure it fetches and processes data correctly via SQL.
"""
df_sql = fetch_data_using_sql(sql_query, columns, rename_columns)
sql_query = f'SELECT * FROM "{test_config["resource_id"]}" LIMIT {test_config["limit"]}'
df_sql = fetch_data_using_sql(sql_query, test_config["columns"], test_config["rename_columns"])
assert not df_sql.empty, "fetch_data_using_sql returned an empty DataFrame!"
assert set(df_sql.columns) == set(
rename_columns.values()
test_config["rename_columns"].values()
), "Column names do not match after renaming!"


def test_data_consistency():
def test_data_consistency(test_config):
"""
Validate that the data fetched by fetch_data and fetch_data_using_sql are consistent.
"""
df_api = fetch_data(resource_id, limit, columns, rename_columns)
df_sql = fetch_data_using_sql(sql_query, columns, rename_columns)
assert df_api.equals(
df_sql
), "Data from fetch_data and fetch_data_using_sql are inconsistent!"
sql_query = f'SELECT * FROM "{test_config["resource_id"]}" LIMIT {test_config["limit"]}'
df_api = fetch_data(
test_config["resource_id"],
test_config["limit"],
test_config["columns"],
test_config["rename_columns"],
)
df_sql = fetch_data_using_sql(sql_query, test_config["columns"], test_config["rename_columns"])
assert df_api.equals(df_sql), "Data from fetch_data and fetch_data_using_sql are inconsistent!"

0 comments on commit c9bab47

Please sign in to comment.