db_query / test.py
DavMelchi's picture
Improve Testing
b542543
import os
import pandas as pd
import pytest
from unittest.mock import patch, MagicMock
from queries.process_all_db import (
all_dbs,
process_all_tech_db,
process_all_tech_db_with_stats,
)
from utils.utils_vars import UtilsVars
from geopy.distance import geodesic
from lat_lon_parser import parse, to_str_deg_min_sec
class TestProcessAllDB:
def setup_method(self):
UtilsVars.all_db_dfs = []
UtilsVars.final_all_database = None
def test_all_dbs(self):
filepath = r"C:\Users\David\Documents\PROJECTS\2023\PROJET 2023\DUMP\DUMP\NOVEMBRE\20241127_21145_27112024_Dump.xml.gz.xlsb"
all_dbs(filepath)
assert len(UtilsVars.all_db_dfs) == 8
assert isinstance(UtilsVars.all_db_dfs[0], pd.DataFrame)
def test_process_all_tech_db(self):
filepath = r"C:\Users\David\Documents\PROJECTS\2023\PROJET 2023\DUMP\DUMP\NOVEMBRE\20241127_21145_27112024_Dump.xml.gz.xlsb"
process_all_tech_db(filepath)
assert UtilsVars.final_all_database is not None
def test_process_all_tech_db_with_stats(self):
filepath = r"C:\Users\David\Documents\PROJECTS\2023\PROJET 2023\DUMP\DUMP\NOVEMBRE\20241127_21145_27112024_Dump.xml.gz.xlsb"
process_all_tech_db_with_stats(filepath)
assert UtilsVars.final_all_database is not None
# def test_all_dbs_empty_file(self):
# filepath = r"C:\Users\HP\Desktop\LTE\PROJET 2023\DUMP\2024\SEPTEMBRE\empty.xlsb"
# all_dbs(filepath)
# assert len(UtilsVars.all_db_dfs) == 0
# def test_process_all_tech_db_empty_file(self):
# filepath = r"C:\Users\HP\Desktop\LTE\PROJET 2023\DUMP\2024\SEPTEMBRE\empty.xlsb"
# process_all_tech_db(filepath)
# assert UtilsVars.final_all_database is None
# def test_process_all_tech_db_with_stats_empty_file(self):
# filepath = r"C:\Users\HP\Desktop\LTE\PROJET 2023\DUMP\2024\SEPTEMBRE\empty.xlsb"
# process_all_tech_db_with_stats(filepath)
# assert UtilsVars.final_all_database is None
class TestCoreDumpPage:
@patch('streamlit.file_uploader')
def test_core_dump_parsing(self, mock_file_uploader):
# Mock file content with properly formatted hex values
mock_content = """
Global cell ID = 1234ABCD
LA cell name = TestCell
3G service area number = 5678EFAB
3G service area name = TestArea
"""
# Create a mock file object
mock_file = MagicMock()
mock_file.read.return_value = mock_content.encode('utf-8')
mock_file_uploader.return_value = [mock_file]
# Since we can't fully test the Streamlit app without initializing it,
# we'll just test that our mock is set up correctly
assert mock_file_uploader.return_value is not None
assert isinstance(mock_file_uploader.return_value, list)
assert len(mock_file_uploader.return_value) == 1
class TestDistanceCalculator:
def test_calculate_distance(self):
# Test the geodesic distance calculation
coord1 = (40.7128, -74.0060) # New York
coord2 = (34.0522, -118.2437) # Los Angeles
distance = geodesic(coord1, coord2).meters
# The distance should be approximately 3935742 meters
assert 3900000 < distance < 4000000
class TestGpsConverter:
def test_dms_to_decimal_conversion(self):
# Test DMS to decimal conversion
dms_lat = "40°26'46\"N"
dms_lon = "79°58'56\"W"
decimal_lat = parse(dms_lat)
decimal_lon = parse(dms_lon)
assert round(decimal_lat, 4) == 40.4461
assert round(decimal_lon, 4) == -79.9822
def test_decimal_to_dms_conversion(self):
# Test decimal to DMS conversion
decimal_lat = 40.4461
decimal_lon = -79.9822
dms_lat = to_str_deg_min_sec(decimal_lat)
dms_lon = to_str_deg_min_sec(decimal_lon)
# Add N/S and E/W indicators
dms_lat = dms_lat + "N" if decimal_lat >= 0 else dms_lat.replace("-", "") + "S"
dms_lon = dms_lon + "E" if decimal_lon >= 0 else dms_lon.replace("-", "") + "W"
# Check for approximate values since formatting might vary slightly
assert "40°" in dms_lat
assert "26'" in dms_lat
assert "N" in dms_lat
assert "79°" in dms_lon
assert "58'" in dms_lon
assert "W" in dms_lon
class TestMultiPointsDistanceCalculator:
def test_multi_points_distance_calculation(self):
# Create a sample DataFrame with multiple points
data = {
'lat1': [40.7128, 37.7749],
'lon1': [-74.0060, -122.4194],
'lat2': [34.0522, 41.8781],
'lon2': [-118.2437, -87.6298]
}
df = pd.DataFrame(data)
# Calculate distances using the same function as in the app
def calculate_distance(row, lat1_col='lat1', lon1_col='lon1', lat2_col='lat2', lon2_col='lon2'):
coord1 = (row[lat1_col], row[lon1_col])
coord2 = (row[lat2_col], row[lon2_col])
return geodesic(coord1, coord2).meters
df['distance_meters'] = df.apply(lambda row: calculate_distance(row), axis=1)
# Verify distances are calculated correctly
assert 3900000 < df.loc[0, 'distance_meters'] < 4000000 # NY to LA
assert 2000000 < df.loc[1, 'distance_meters'] < 3000000 # SF to Chicago
class TestSectorKmlGenerator:
@patch('pandas.read_excel')
def test_sector_kml_generation(self, mock_read_excel):
# Mock DataFrame for sector KML generation
mock_df = pd.DataFrame({
'Latitude': [40.7128, 34.0522],
'Longitude': [-74.0060, -118.2437],
'Azimuth': [90, 180],
'Name': ['Sector1', 'Sector2']
})
mock_read_excel.return_value = mock_df
# Import here to avoid streamlit initialization during testing
# This is a placeholder for the actual test
# The actual implementation would test the KML generation logic
assert len(mock_df) == 2
assert 'Azimuth' in mock_df.columns
class TestDatabasePage:
@patch('streamlit.file_uploader')
@patch('queries.process_gsm.process_gsm_data_to_excel')
def test_process_gsm_database(self, mock_process_gsm, mock_file_uploader):
# Mock file upload
mock_file = MagicMock()
mock_file_uploader.return_value = mock_file
# Since we can't directly test the Streamlit app functions without initializing it,
# we'll just test that our mocks are set up correctly
assert mock_file_uploader.return_value is not None
assert mock_process_gsm is not None
@patch('streamlit.file_uploader')
@patch('queries.process_lte.process_lte_data_to_excel')
def test_process_lte_database(self, mock_process_lte, mock_file_uploader):
# Mock file upload
mock_file = MagicMock()
mock_file_uploader.return_value = mock_file
# Since we can't directly test the Streamlit app functions without initializing it,
# we'll just test that our mocks are set up correctly
assert mock_file_uploader.return_value is not None
assert mock_process_lte is not None