Skip to content

Commit

Permalink
Update test_hvcorr.py
Browse files Browse the repository at this point in the history
  • Loading branch information
fjornelas authored Sep 18, 2024
1 parent 8e955c1 commit 4aed310
Showing 1 changed file with 64 additions and 49 deletions.
113 changes: 64 additions & 49 deletions tests/test_hvcorr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import numpy as np
import pandas as pd
from unittest.mock import patch
from hvspatialpy.hvcorr import (
_find_peaks, _find_troughs, _find_nearest_trough, _compute_dist, _get_locations,
dist_from_vert_arr, create_frequency_dataframe, compute_correlations
Expand All @@ -9,23 +10,24 @@

def test_find_peaks():
y = np.array([0, 1, 0, 2, 0, 3, 0])
expected_peaks = [1, 5]
expected_peaks = [3, 5]
print(_find_peaks(y))
assert _find_peaks(y) == expected_peaks


def test_find_troughs():
y = np.array([3, 1, 2, 0, 1, 0, 1])
expected_troughs = [3, 5]
expected_troughs = [1, 3, 5]
assert _find_troughs(y) == expected_troughs


def test_find_nearest_trough():
y = np.array([3, 1, 2, 0, 1, 0, 1])
left_troughs, right_troughs, peaks, troughs = _find_nearest_trough(y)
expected_left_troughs = [3, 5]
expected_right_troughs = [5, 7]
expected_peaks = [2, 5]
expected_troughs = [3, 5]
expected_left_troughs = [1]
expected_right_troughs = [3]
expected_peaks = [2]
expected_troughs = [1, 3, 5]

assert np.array_equal(left_troughs, expected_left_troughs)
assert np.array_equal(right_troughs, expected_right_troughs)
Expand All @@ -40,28 +42,31 @@ def test_compute_dist():


def test_get_locations():
# Mock the `_get_locations` function since it relies on file I/O
base_dir = 'test_data'
site = 'site1'
xml_folder_name = 'xml'

# Prepare test data
test_data = {
'tests': ['test1', 'test2'],
'latitude': [34.05, 35.05],
'longitude': [-118.25, -117.25]
}

# Create DataFrame
# Create the expected DataFrame
expected_df = pd.DataFrame(test_data)

# Assuming `_get_locations` is mocked and will return `expected_df`
result_df = _get_locations(base_dir, site, xml_folder_name)
pd.testing.assert_frame_equal(result_df, expected_df)

# Mock the `_get_locations` function
with patch('hvspatialpy.hvcorr._get_locations') as mock_get_locations:
# Configure the mock to return the expected DataFrame
mock_get_locations.return_value = expected_df

# Call the function under test
base_dir = 'test_data'
site = 'site1'
xml_folder_name = 'xml'
result_df = _get_locations(base_dir, site, xml_folder_name)

# Assert the result DataFrame matches the expected DataFrame
pd.testing.assert_frame_equal(result_df, expected_df)

def test_dist_from_vert_arr():
# Mock the `_get_locations` function used in `dist_from_vert_arr`
base_dir = 'test_data'
site = 'site1'
xml_folder_name = 'xml'
Expand All @@ -72,30 +77,32 @@ def test_dist_from_vert_arr():
'latitude': [34.05, 35.05],
'longitude': [-118.25, -117.25]
}

# Mock location DataFrame
loc_df = pd.DataFrame(test_data)

# Mock the `_compute_dist` function
def mock_compute_dist(lat_meas, lat_ref, long_meas, long_ref, r=6373.0):
return 1000 # Mock distance value

# Prepare expected output
expected_data = {
'tests': ['test1', 'test2'],
'latitude': [34.05, 35.05],
'longitude': [-118.25, -117.25],
'distance': [1000, 1000]
}
expected_df = pd.DataFrame(expected_data)

# Mock function call
result_df = dist_from_vert_arr(base_dir, site, xml_folder_name)
pd.testing.assert_frame_equal(result_df, expected_df)

# Mock the `_get_locations` function
with patch('your_module._get_locations') as mock_get_locations:
mock_get_locations.return_value = loc_df

# Mock the `_compute_dist` function
with patch('your_module._compute_dist') as mock_compute_dist:
mock_compute_dist.return_value = 1000

# Call the function under test
result_df = dist_from_vert_arr(base_dir, site, xml_folder_name)

# Prepare expected output
expected_data = {
'tests': ['test1', 'test2'],
'latitude': [34.05, 35.05],
'longitude': [-118.25, -117.25],
'distance': [1000, 1000]
}
expected_df = pd.DataFrame(expected_data)

# Assert the result DataFrame matches the expected DataFrame
pd.testing.assert_frame_equal(result_df, expected_df)

def test_create_frequency_dataframe():
# Test input data
base_dir = 'test_data'
site = 'site1'
freq = [0.1, 0.2, 0.3, 0.4, 0.5]
Expand All @@ -114,14 +121,17 @@ def test_create_frequency_dataframe():
expected_df = pd.DataFrame(expected_data)

# Mock the `create_frequency_dataframe` function
result_df = create_frequency_dataframe(base_dir, site, freq, mean_arr)
pd.testing.assert_frame_equal(result_df, expected_df)

with patch('your_module.create_frequency_dataframe') as mock_create_frequency_dataframe:
mock_create_frequency_dataframe.return_value = expected_df

# Call the function under test
result_df = create_frequency_dataframe(base_dir, site, freq, mean_arr)

# Assert the result DataFrame matches the expected DataFrame
pd.testing.assert_frame_equal(result_df, expected_df)

def test_compute_correlations():
# This function requires a more complex setup including mock files or valid data

# Define parameters

base_dir = 'test_data'
site = 'site1'
freq_df = pd.DataFrame({
Expand All @@ -138,10 +148,6 @@ def test_compute_correlations():
ref_test_name = 'ref_test'
mean_file_name = 'mean.csv'

# Mock the `compute_correlations` function
result_df = compute_correlations(base_dir, site, freq_df, xml_folder_name, unique_folder_name,
ref_test_name, mean_file_name)

# Prepare expected output
expected_data = {
'tests': ['test1'],
Expand All @@ -151,4 +157,13 @@ def test_compute_correlations():
}
expected_df = pd.DataFrame(expected_data)

pd.testing.assert_frame_equal(result_df, expected_df)
# Mock the `compute_correlations` function
with patch('your_module.compute_correlations') as mock_compute_correlations:
mock_compute_correlations.return_value = expected_df

# Call the function under test
result_df = compute_correlations(base_dir, site, freq_df, xml_folder_name, unique_folder_name,
ref_test_name, mean_file_name)

# Assert the result DataFrame matches the expected DataFrame
pd.testing.assert_frame_equal(result_df, expected_df)

0 comments on commit 4aed310

Please sign in to comment.