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+
58Usage 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
1618import pandas as pd
1719import 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
146154if __name__ == "__main__" :
0 commit comments