Skip to content

Commit 15f2042

Browse files
committed
tests: update tests to run on github action
1 parent 9c468f5 commit 15f2042

4 files changed

Lines changed: 299 additions & 243 deletions

File tree

‎loopstructural/gui/modelling/geological_model_tab/feature_details_panel.py‎

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,54 @@
2828
logger = getLogger(__name__)
2929

3030

31+
# Helper functions for retrieving fault dip and pitch from stored data or calculations
32+
def retrieve_dip_value(fault, model_manager):
33+
"""
34+
Retrieve dip value from stored fault data or calculate from normal vector.
35+
36+
Args:
37+
fault: The fault object with a name and fault_normal_vector attribute
38+
model_manager: The model manager with faults dictionary
39+
40+
Returns:
41+
float: The dip angle in degrees
42+
"""
43+
dip = None
44+
if model_manager is not None and fault.name in model_manager.faults:
45+
fault_data = model_manager.faults[fault.name].get('data')
46+
if fault_data is not None and 'dip' in fault_data.columns and not fault_data.empty:
47+
dip = fault_data['dip'].mean()
48+
49+
# Fallback: calculate from normal vector if not found in stored data
50+
if dip is None:
51+
try:
52+
dip = normal_vector_to_strike_and_dip(fault.fault_normal_vector)[0, 1]
53+
except Exception:
54+
dip = 90 # Default value if calculation fails
55+
56+
return dip
57+
58+
59+
def retrieve_pitch_value(fault, model_manager):
60+
"""
61+
Retrieve pitch value from stored fault data.
62+
63+
Args:
64+
fault: The fault object with a name attribute
65+
model_manager: The model manager with faults dictionary
66+
67+
Returns:
68+
float: The pitch angle in degrees (default 0)
69+
"""
70+
pitch = 0
71+
if model_manager is not None and fault.name in model_manager.faults:
72+
fault_data = model_manager.faults[fault.name].get('data')
73+
if fault_data is not None and 'pitch' in fault_data.columns and not fault_data.empty:
74+
pitch = fault_data['pitch'].mean()
75+
76+
return pitch
77+
78+
3179
class BaseFeatureDetailsPanel(QWidget):
3280
def __init__(self, parent=None, *, feature=None, model_manager=None, data_manager=None):
3381
super().__init__(parent)
@@ -579,25 +627,9 @@ def __init__(self, parent=None, *, fault=None, model_manager=None, data_manager=
579627
raise ValueError("Fault must be provided.")
580628
self.fault = fault
581629

582-
# Try to get dip from stored fault data first
583-
dip = None
584-
if model_manager is not None and fault.name in model_manager.faults:
585-
fault_data = model_manager.faults[fault.name].get('data')
586-
if fault_data is not None and 'dip' in fault_data.columns and not fault_data.empty:
587-
dip = fault_data['dip'].mean()
588-
589-
# Fallback: calculate from normal vector if not found in stored data
590-
if dip is None:
591-
try:
592-
dip = normal_vector_to_strike_and_dip(fault.fault_normal_vector)[0, 1]
593-
except Exception:
594-
dip = 90 # Default value if calculation fails
595-
596-
pitch = 0
597-
if model_manager is not None and fault.name in model_manager.faults:
598-
fault_data = model_manager.faults[fault.name].get('data')
599-
if fault_data is not None and 'pitch' in fault_data.columns and not fault_data.empty:
600-
pitch = fault_data['pitch'].mean()
630+
# Retrieve dip and pitch using helper functions
631+
dip = retrieve_dip_value(fault, model_manager)
632+
pitch = retrieve_pitch_value(fault, model_manager)
601633

602634
self.fault_parameters = {
603635
'displacement': fault.displacement,

‎tests/qgis/test_fault_dip_display.py‎

Lines changed: 87 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,54 @@
22

33
"""Test fault dip display in the Geological Features panel.
44
5+
This module tests the logic for retrieving dip and pitch values from fault data,
6+
which is used by FaultFeatureDetailsPanel.
7+
58
Usage from the repo root folder:
69
710
.. code-block:: bash
811
# for whole tests
9-
python -m pytest tests/qgis/test_fault_dip_display.py
12+
python -m unittest tests.qgis.test_fault_dip_display
1013
# for specific test
11-
python -m pytest tests/qgis/test_fault_dip_display.py::test_dip_from_stored_data
14+
python -m unittest tests.qgis.test_fault_dip_display.TestFaultDipRetrieval.test_dip_from_stored_data
1215
"""
1316

14-
import unittest
15-
from unittest.mock import MagicMock, Mock
17+
from qgis.testing import unittest
1618
import pandas as pd
1719
import numpy as np
20+
from LoopStructural.utils import normal_vector_to_strike_and_dip
21+
22+
# Import the helper functions from the actual implementation
23+
from loopstructural.gui.modelling.geological_model_tab.feature_details_panel import (
24+
retrieve_dip_value,
25+
retrieve_pitch_value,
26+
)
27+
28+
29+
class MockFault:
30+
"""Simple fault mock for testing."""
31+
32+
def __init__(self, name="TestFault", normal_vector=None):
33+
"""Initialize mock fault."""
34+
self.name = name
35+
self.fault_normal_vector = normal_vector if normal_vector is not None else np.array([1.0, 0.0, 0.0])
1836

1937

20-
class TestFaultDipDisplay(unittest.TestCase):
21-
"""Test fault dip retrieval and display."""
38+
class MockModelManager:
39+
"""Simple model manager mock for testing."""
40+
41+
def __init__(self):
42+
"""Initialize mock manager."""
43+
self.faults = {}
44+
45+
46+
class TestFaultDipRetrieval(unittest.TestCase):
47+
"""Test dip and pitch value retrieval logic."""
2248

2349
def setUp(self):
24-
"""Set up mock objects for testing."""
25-
# Mock the fault object
26-
self.mock_fault = Mock()
27-
self.mock_fault.name = "TestFault"
28-
self.mock_fault.displacement = 100
29-
self.mock_fault.fault_major_axis = 500
30-
self.mock_fault.fault_minor_axis = 300
31-
self.mock_fault.fault_intermediate_axis = 400
32-
33-
# Mock fault_normal_vector that would give a dip of 90 (vertical fault)
34-
self.mock_fault.fault_normal_vector = np.array([1.0, 0.0, 0.0])
35-
36-
# Mock the model manager
37-
self.mock_model_manager = Mock()
38-
self.mock_model_manager.faults = {}
50+
"""Set up test fixtures."""
51+
self.fault = MockFault()
52+
self.model_manager = MockModelManager()
3953

4054
def test_dip_from_stored_data(self):
4155
"""Test that dip is retrieved from stored fault data when available."""
@@ -47,23 +61,13 @@ def test_dip_from_stored_data(self):
4761
'dip': [45, 45, 45]
4862
})
4963

50-
self.mock_model_manager.faults['TestFault'] = {'data': fault_data}
64+
self.model_manager.faults['TestFault'] = {'data': fault_data}
5165

52-
from loopstructural.gui.modelling.geological_model_tab.feature_details_panel import (
53-
FaultFeatureDetailsPanel
54-
)
66+
# Retrieve dip using the logic that FaultFeatureDetailsPanel uses
67+
dip = retrieve_dip_value(self.fault, self.model_manager)
5568

56-
# Create the panel - this should retrieve dip from stored data
57-
panel = FaultFeatureDetailsPanel(
58-
parent=None,
59-
fault=self.mock_fault,
60-
model_manager=self.mock_model_manager,
61-
data_manager=None
62-
)
63-
64-
# Check that the dip was retrieved from stored data (45 degrees)
65-
# not from the normal vector calculation (which would be 90 degrees)
66-
self.assertEqual(panel.fault_parameters['dip'], 45)
69+
# Should get the average dip from stored data (45 degrees)
70+
self.assertEqual(dip, 45)
6771

6872
def test_dip_fallback_to_normal_vector(self):
6973
"""Test that dip falls back to normal vector calculation when not in stored data."""
@@ -74,73 +78,77 @@ def test_dip_fallback_to_normal_vector(self):
7478
'Z': [0, 0, 0]
7579
})
7680

77-
self.mock_model_manager.faults['TestFault'] = {'data': fault_data}
78-
79-
from loopstructural.gui.modelling.geological_model_tab.feature_details_panel import (
80-
FaultFeatureDetailsPanel
81-
)
82-
from LoopStructural.utils import normal_vector_to_strike_and_dip
81+
self.model_manager.faults['TestFault'] = {'data': fault_data}
8382

8483
# Calculate expected dip from normal vector
85-
expected_dip = normal_vector_to_strike_and_dip(self.mock_fault.fault_normal_vector)[0, 1]
84+
expected_dip = normal_vector_to_strike_and_dip(self.fault.fault_normal_vector)[0, 1]
8685

87-
panel = FaultFeatureDetailsPanel(
88-
parent=None,
89-
fault=self.mock_fault,
90-
model_manager=self.mock_model_manager,
91-
data_manager=None
92-
)
86+
# Retrieve dip
87+
dip = retrieve_dip_value(self.fault, self.model_manager)
9388

9489
# Should fall back to calculating from normal vector
95-
self.assertEqual(panel.fault_parameters['dip'], expected_dip)
90+
self.assertEqual(dip, expected_dip)
9691

9792
def test_dip_default_when_no_data(self):
98-
"""Test that dip defaults to 90 when no fault data exists."""
93+
"""Test that dip falls back to normal vector when no fault data exists."""
9994
# No fault data at all
100-
self.mock_model_manager.faults = {}
95+
self.model_manager.faults = {}
10196

102-
from loopstructural.gui.modelling.geological_model_tab.feature_details_panel import (
103-
FaultFeatureDetailsPanel
104-
)
97+
# Retrieve dip
98+
dip = retrieve_dip_value(self.fault, self.model_manager)
10599

106-
panel = FaultFeatureDetailsPanel(
107-
parent=None,
108-
fault=self.mock_fault,
109-
model_manager=self.mock_model_manager,
110-
data_manager=None
111-
)
100+
# Should calculate from normal vector
101+
self.assertIsInstance(dip, (int, float, np.floating))
102+
self.assertGreaterEqual(dip, 0)
103+
self.assertLessEqual(dip, 90)
104+
105+
def test_dip_with_none_model_manager(self):
106+
"""Test that dip falls back to normal vector when model_manager is None."""
107+
# Calculate expected dip from normal vector
108+
expected_dip = normal_vector_to_strike_and_dip(self.fault.fault_normal_vector)[0, 1]
112109

113-
# Should use a reasonable default or calculate from normal vector
114-
self.assertIsInstance(panel.fault_parameters['dip'], (int, float))
115-
self.assertGreaterEqual(panel.fault_parameters['dip'], 0)
116-
self.assertLessEqual(panel.fault_parameters['dip'], 90)
110+
# Retrieve dip with None model manager
111+
dip = retrieve_dip_value(self.fault, None)
112+
113+
# Should calculate from normal vector
114+
self.assertEqual(dip, expected_dip)
117115

118116
def test_pitch_from_stored_data(self):
119-
"""Test that pitch is also retrieved from stored fault data when available."""
117+
"""Test that pitch is retrieved from stored fault data when available."""
120118
# Create fault data with pitch
121119
fault_data = pd.DataFrame({
122120
'X': [0, 1, 2],
123121
'Y': [0, 1, 2],
124122
'Z': [0, 0, 0],
125-
'dip': [45, 45, 45],
126123
'pitch': [30, 30, 30]
127124
})
128125

129-
self.mock_model_manager.faults['TestFault'] = {'data': fault_data}
126+
self.model_manager.faults['TestFault'] = {'data': fault_data}
127+
128+
# Retrieve pitch
129+
pitch = retrieve_pitch_value(self.fault, self.model_manager)
130130

131-
from loopstructural.gui.modelling.geological_model_tab.feature_details_panel import (
132-
FaultFeatureDetailsPanel
133-
)
131+
# Should get the average pitch from stored data
132+
self.assertEqual(pitch, 30)
133+
134+
def test_pitch_default_when_no_data(self):
135+
"""Test that pitch defaults to 0 when no fault data exists."""
136+
# No fault data at all
137+
self.model_manager.faults = {}
134138

135-
panel = FaultFeatureDetailsPanel(
136-
parent=None,
137-
fault=self.mock_fault,
138-
model_manager=self.mock_model_manager,
139-
data_manager=None
140-
)
139+
# Retrieve pitch
140+
pitch = retrieve_pitch_value(self.fault, self.model_manager)
141+
142+
# Should default to 0
143+
self.assertEqual(pitch, 0)
144+
145+
def test_pitch_with_none_model_manager(self):
146+
"""Test that pitch defaults to 0 when model_manager is None."""
147+
# Retrieve pitch with None model manager
148+
pitch = retrieve_pitch_value(self.fault, None)
141149

142-
# Check that pitch was retrieved from stored data
143-
self.assertEqual(panel.fault_parameters['pitch'], 30)
150+
# Should default to 0
151+
self.assertEqual(pitch, 0)
144152

145153

146154
if __name__ == "__main__":

0 commit comments

Comments
 (0)