Skip to content

Commit 0a06e39

Browse files
committed
Improve Field test coverage (#22)
1 parent 5d5221a commit 0a06e39

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

tests/test_field.py

+35-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import pytest
12
import numpy as np
23
from CADETPythonSimulator.field import Field, FieldInterpolator
34
import matplotlib.pyplot as plt
45
# %% Testing utilities
56

7+
68
def assert_equal(value, expected, message=""):
79
message = f"Test failed: {message}. Expected {expected}, got {value}."
810
assert value == expected, message
@@ -26,47 +28,60 @@ def test_field_initialization():
2628
assert_equal(viscosity.n_dof, 11 * 6, "Scalar field degrees of freedom")
2729

2830
# Vector field
29-
concentration = Field(name="concentration", dimensions=dimensions, n_components=3)
31+
concentration = Field(name="concentration",
32+
dimensions=dimensions, n_components=3)
3033
assert_shape(concentration.shape, (11, 6, 3), "Vector field shape")
31-
assert_equal(concentration.n_dof, 11 * 6 * 3, "Vector field degrees of freedom")
34+
assert_equal(concentration.n_dof, 11 * 6 * 3,
35+
"Vector field degrees of freedom")
3236

3337
# Custom data
3438
data = np.ones((11, 6, 3))
35-
concentration_with_data = Field(name="concentration", dimensions=dimensions, n_components=3, data=data)
36-
assert_shape(concentration_with_data.shape, (11, 6, 3), "Custom data field shape")
39+
concentration_with_data = Field(
40+
name="concentration", dimensions=dimensions, n_components=3, data=data)
41+
assert_shape(concentration_with_data.shape,
42+
(11, 6, 3), "Custom data field shape")
43+
44+
with pytest.raises(ValueError):
45+
viscosity.data = np.ones((1, 2, 3))
3746

3847

3948
# %% Plotting
4049

4150
def test_plotting():
4251
# 1D Plot
4352
dimensions = {"x": np.linspace(0, 10, 11)}
44-
field_1D = Field(name="1D Field", dimensions=dimensions, n_components=2, data=np.random.random((11, 2)))
53+
field_1D = Field(name="1D Field", dimensions=dimensions,
54+
n_components=2, data=np.random.random((11, 2)))
4555
fig, ax = field_1D.plot()
4656
assert isinstance(ax, plt.Axes), "1D plot returns one axis"
4757

4858
# 2D Plot
4959
dimensions = {"x": np.linspace(0, 10, 11), "y": np.linspace(0, 5, 6)}
50-
field_2D = Field(name="2D Field", dimensions=dimensions, n_components=3, data=np.random.random((11, 6, 3)))
60+
field_2D = Field(name="2D Field", dimensions=dimensions,
61+
n_components=3, data=np.random.random((11, 6, 3)))
5162
fig, axes = field_2D.plot()
5263
assert len(axes) == 3, "2D plot returns one axis per component"
5364

5465

5566
# %% Slicing
5667

5768
def test_field_slicing():
58-
dimensions = {"axial": np.linspace(0, 10, 11), "radial": np.linspace(0, 5, 6)}
69+
dimensions = {"axial": np.linspace(
70+
0, 10, 11), "radial": np.linspace(0, 5, 6)}
5971
field = Field(name="concentration", dimensions=dimensions, n_components=3)
6072

6173
# Slice along one dimension
6274
field_sliced = field[{"axial": 0}]
63-
assert_equal(len(field_sliced.dimensions), 1, "Field slicing reduces dimensionality")
75+
assert_equal(len(field_sliced.dimensions), 1,
76+
"Field slicing reduces dimensionality")
6477
assert_shape(field_sliced.shape, (6, 3), "Field slicing shape")
6578

6679
# Slice along all dimensions
6780
field_sliced_all = field[{"axial": 0, "radial": 0}]
68-
assert_equal(len(field_sliced_all.dimensions), 0, "Full slicing removes all dimensions")
69-
assert_shape(field_sliced_all.shape, (3,), "Full slicing results in vector")
81+
assert_equal(len(field_sliced_all.dimensions), 0,
82+
"Full slicing removes all dimensions")
83+
assert_shape(field_sliced_all.shape, (3,),
84+
"Full slicing results in vector")
7085

7186

7287
# %% Normalization
@@ -89,14 +104,15 @@ def test_field_normalization():
89104

90105
# Test 2: Verify data normalization
91106
normalized_data = normalized_field.data
92-
assert np.isclose(np.min(normalized_data), 0.0), "Normalized data minimum is not 0."
93-
assert np.isclose(np.max(normalized_data), 1.0), "Normalized data maximum is not 1."
107+
assert np.isclose(np.min(normalized_data),
108+
0.0), "Normalized data minimum is not 0."
109+
assert np.isclose(np.max(normalized_data),
110+
1.0), "Normalized data maximum is not 1."
94111

95112
# Test 3: Ensure original field is unchanged
96113
assert np.array_equal(field.data, z), "Original field data was modified."
97114

98115

99-
100116
# %% Interpolation and Resampling
101117

102118
def test_temperature_use_case():
@@ -136,7 +152,8 @@ def test_interpolated_field():
136152
"radial": np.linspace(0, 5, 6)
137153
}
138154
data = np.random.random((11, 6, 3))
139-
concentration = Field(name="concentration", dimensions=dimensions, n_components=3, data=data)
155+
concentration = Field(name="concentration",
156+
dimensions=dimensions, n_components=3, data=data)
140157

141158
# Interpolated field
142159
interp_field = FieldInterpolator(concentration)
@@ -146,15 +163,17 @@ def test_interpolated_field():
146163

147164
def test_resampling():
148165
dimensions = {"x": np.linspace(0, 10, 11), "y": np.linspace(0, 5, 6)}
149-
field = Field(name="concentration", dimensions=dimensions, n_components=2, data=np.random.random((11, 6, 2)))
166+
field = Field(name="concentration", dimensions=dimensions,
167+
n_components=2, data=np.random.random((11, 6, 2)))
150168

151169
# Resample one dimension
152170
resampled_field = field.resample({"x": 50})
153171
assert_shape(resampled_field.shape, (50, 6, 2), "Resampling one dimension")
154172

155173
# Resample all dimensions
156174
resampled_field_all = field.resample({"x": 50, "y": 25})
157-
assert_shape(resampled_field_all.shape, (50, 25, 2), "Resampling all dimensions")
175+
assert_shape(resampled_field_all.shape, (50, 25, 2),
176+
"Resampling all dimensions")
158177

159178

160179
def test_field_interpolation_and_derivatives():
@@ -203,6 +222,5 @@ def test_field_interpolation_and_derivatives():
203222

204223
# %% Run tests
205224

206-
import pytest
207225
if __name__ == "__main__":
208226
pytest.main('test_field.py')

0 commit comments

Comments
 (0)