db_query / tests /test_ciq_sheet_detection.py
DavMelchi's picture
Add selectable CIQ sheet detection
d9f7219
import io
from unittest.mock import patch
import pandas as pd
import pytest
from queries.process_ciq_2g import _read_ciq_df
from queries.process_ciq_3g import read_ciq_3g_brut
from queries.verify_ciq import read_ciq_file
from utils.ciq_excel import read_ciq_excel, select_ciq_sheet_name
class DummyExcelFile:
def __init__(self, sheet_names):
self.sheet_names = sheet_names
def test_select_ciq_sheet_name_uses_single_sheet_without_name_check():
fake_file = io.BytesIO(b"dummy")
with patch("utils.ciq_excel.pd.ExcelFile", return_value=DummyExcelFile(["Feuil1"])):
assert select_ciq_sheet_name(fake_file, technology="2g") == "Feuil1"
def test_select_ciq_sheet_name_detects_exact_multi_sheet_match():
fake_file = io.BytesIO(b"dummy")
with patch(
"utils.ciq_excel.pd.ExcelFile",
return_value=DummyExcelFile(["2G", "3G", "4G"]),
):
assert select_ciq_sheet_name(fake_file, technology="3g") == "3G"
def test_select_ciq_sheet_name_detects_suffix_based_match():
fake_file = io.BytesIO(b"dummy")
with patch(
"utils.ciq_excel.pd.ExcelFile",
return_value=DummyExcelFile(["Summary", "6525_SKS_PITIANGOMA_2G", "Notes"]),
):
assert (
select_ciq_sheet_name(fake_file, technology="2g")
== "6525_SKS_PITIANGOMA_2G"
)
def test_select_ciq_sheet_name_raises_when_no_matching_sheet_exists():
fake_file = io.BytesIO(b"dummy")
with patch(
"utils.ciq_excel.pd.ExcelFile",
return_value=DummyExcelFile(["Summary", "Notes"]),
):
with pytest.raises(ValueError, match="Unable to detect the 2G sheet"):
select_ciq_sheet_name(fake_file, technology="2g")
def test_read_ciq_excel_uses_user_selected_sheet_when_provided():
fake_file = io.BytesIO(b"dummy")
with patch(
"utils.ciq_excel.pd.ExcelFile",
return_value=DummyExcelFile(["2G", "Manual"]),
), patch("utils.ciq_excel.pd.read_excel") as mock_read_excel:
mock_read_excel.return_value = pd.DataFrame({"Sites": ["SITE"]})
read_ciq_excel(fake_file, technology="2g", sheet_name="Manual")
assert mock_read_excel.call_args.kwargs["sheet_name"] == "Manual"
def test_read_ciq_excel_raises_when_user_selected_sheet_is_missing():
fake_file = io.BytesIO(b"dummy")
with patch(
"utils.ciq_excel.pd.ExcelFile",
return_value=DummyExcelFile(["2G", "3G"]),
):
with pytest.raises(ValueError, match="Sheet '4G' does not exist"):
read_ciq_excel(fake_file, technology="2g", sheet_name="4G")
def test_read_ciq_2g_df_reads_detected_2g_sheet():
fake_file = io.BytesIO(b"dummy")
with patch(
"utils.ciq_excel.pd.ExcelFile",
return_value=DummyExcelFile(["3G", "6525_SKS_SIEME_2G", "4G"]),
), patch("utils.ciq_excel.pd.read_excel") as mock_read_excel:
mock_read_excel.return_value = pd.DataFrame(
{
"Sites": ["6525_SKS_SIEME_2G"],
"NOM_CELLULE": ["6525_SKS_SIEME_1_900"],
}
)
df = _read_ciq_df(fake_file)
assert mock_read_excel.call_args.kwargs["sheet_name"] == "6525_SKS_SIEME_2G"
assert df.iloc[0]["Sites"] == "6525_SKS_SIEME_2G"
def test_read_ciq_2g_df_reads_user_selected_sheet():
fake_file = io.BytesIO(b"dummy")
with patch(
"utils.ciq_excel.pd.ExcelFile",
return_value=DummyExcelFile(["2G", "Manual_2G"]),
), patch("utils.ciq_excel.pd.read_excel") as mock_read_excel:
mock_read_excel.return_value = pd.DataFrame(
{"Sites": ["SITE_2G"], "NOM_CELLULE": ["SITE_2G_1_900"]}
)
_read_ciq_df(fake_file, ciq_sheet_name="Manual_2G")
assert mock_read_excel.call_args.kwargs["sheet_name"] == "Manual_2G"
def test_read_ciq_3g_brut_reads_detected_3g_sheet():
fake_file = io.BytesIO(b"dummy")
with patch(
"utils.ciq_excel.pd.ExcelFile",
return_value=DummyExcelFile(["2G", "3G", "4G"]),
), patch("utils.ciq_excel.pd.read_excel") as mock_read_excel:
mock_read_excel.return_value = pd.DataFrame({"Sites": ["SITE_3G"]})
df = read_ciq_3g_brut(fake_file)
assert mock_read_excel.call_args.kwargs["sheet_name"] == "3G"
assert df.iloc[0]["Sites"] == "SITE_3G"
def test_verify_read_ciq_file_reads_detected_4g_sheet():
fake_file = io.BytesIO(b"dummy")
with patch(
"utils.ciq_excel.pd.ExcelFile",
return_value=DummyExcelFile(["2G", "3G", "4G"]),
), patch("utils.ciq_excel.pd.read_excel") as mock_read_excel:
mock_read_excel.return_value = pd.DataFrame({"CellName": ["SITE_4G"]})
df = read_ciq_file(fake_file, technology="4g")
assert mock_read_excel.call_args.kwargs["sheet_name"] == "4G"
assert df.iloc[0]["CellName"] == "SITE_4G"
def test_verify_read_ciq_file_reads_user_selected_sheet():
fake_file = io.BytesIO(b"dummy")
with patch(
"utils.ciq_excel.pd.ExcelFile",
return_value=DummyExcelFile(["4G", "LTE_BACKUP"]),
), patch("utils.ciq_excel.pd.read_excel") as mock_read_excel:
mock_read_excel.return_value = pd.DataFrame({"CellName": ["SITE_4G"]})
read_ciq_file(fake_file, technology="4g", ciq_sheet_name="LTE_BACKUP")
assert mock_read_excel.call_args.kwargs["sheet_name"] == "LTE_BACKUP"