Skip to content

Commit 38df0ed

Browse files
committed
DoubleArray Class Integration Tests
1 parent 3574fc0 commit 38df0ed

5 files changed

Lines changed: 246 additions & 11 deletions

File tree

pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ markers =
33
unit: Unit tests that run quickly and test small pieces of functionality
44
integration: Integration tests that check multiple components together
55
core: Core functionality tests
6+
synergy: Tests the Synergy class
7+
double_array: Tests the DoubleArray class

src/moldflow/double_array.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,60 +35,63 @@ def val(self, index: int) -> float:
3535
Get the value at the specified index.
3636
3737
Args:
38-
index (int): The index of the value to get.
38+
index (int): index between 0 and double_array.size-1 (inclusive).
3939
4040
Returns:
41-
float: The value at the specified index.
41+
The value at the specified index.
4242
"""
4343
process_log(__name__, LogMessage.FUNCTION_CALL, locals(), name="val")
4444
check_type(index, int)
4545
return self.double_array.Val(index)
4646

47-
def add_double(self, value: float) -> None:
47+
def add_double(self, value: float | int) -> None:
4848
"""
49-
Add a double value to the array.
49+
Adds a double value to the end of the array.
5050
5151
Args:
52-
value (float): The value to add.
52+
value (float | int): The value to add.
5353
"""
5454
process_log(__name__, LogMessage.FUNCTION_CALL, locals(), name="add_double")
5555
check_type(value, (int, float))
5656
self.double_array.AddDouble(value)
5757

58-
def to_list(self) -> list[float]:
58+
def to_list(self) -> list[float | int]:
5959
"""
6060
Convert the double array to a list of floats.
6161
6262
Returns:
63-
list[float]: The list of floats.
63+
The list of values.
6464
"""
6565
process_log(__name__, LogMessage.FUNCTION_CALL, locals(), name="to_list")
6666

6767
vb_array = self.double_array.ToVBSArray()
6868
return list(vb_array)
6969

70-
def from_list(self, values: list[float]) -> None:
70+
def from_list(self, values: list[float | int] | tuple[float | int]) -> int:
7171
"""
7272
Convert a list of floats to a double array.
7373
7474
Args:
75-
values (list[float]): The list of floats to convert.
75+
values (list[float | int] | tuple[float | int]): The list of floats to convert.
76+
77+
Returns:
78+
The number of elements added.
7679
"""
7780
process_log(__name__, LogMessage.FUNCTION_CALL, locals(), name="from_list")
7881

7982
check_type(values, (list, tuple))
8083
for value in values:
8184
check_type(value, (int, float))
8285

83-
self.double_array.FromVBSArray(list(values))
86+
return self.double_array.FromVBSArray(list(values))
8487

8588
@property
8689
def size(self) -> int:
8790
"""
8891
Get the size of the array.
8992
9093
Returns:
91-
int: The size of the array.
94+
The size of the array.
9295
"""
9396
process_log(__name__, LogMessage.PROPERTY_GET, locals(), name="size")
9497
return self.double_array.Size
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Configuration and constants for integration tests.
3+
"""
4+
5+
import pytest
6+
from moldflow import Synergy
7+
8+
9+
@pytest.fixture(scope="class")
10+
def synergy():
11+
"""
12+
Fixture to create a real Synergy instance for integration testing.
13+
"""
14+
synergy_instance = Synergy()
15+
yield synergy_instance
16+
synergy_instance.quit(False)
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
"""
2+
Integration tests for DoubleArray Wrapper Class of moldflow-api module.
3+
4+
These tests focus on testing the actual functionality and behavior
5+
of the DoubleArray class with real Moldflow Synergy COM objects.
6+
"""
7+
8+
import pytest
9+
from moldflow import DoubleArray, Synergy
10+
11+
12+
@pytest.mark.integration
13+
@pytest.mark.double_array
14+
class TestIntegrationDoubleArray:
15+
"""
16+
Integration test suite for the DoubleArray class.
17+
"""
18+
19+
@pytest.fixture
20+
def double_array(self, synergy: Synergy):
21+
"""
22+
Fixture to create a real DoubleArray instance for integration testing.
23+
"""
24+
return synergy.create_double_array()
25+
26+
def _check_double_array_size(self, double_array: DoubleArray, expected_size: int):
27+
"""
28+
Verify the size of the double array.
29+
"""
30+
assert double_array.size == expected_size
31+
32+
def _check_value_at_index(self, double_array: DoubleArray, index: int, expected_value: float):
33+
"""
34+
Verify the value at a specific index in the array.
35+
"""
36+
assert double_array.val(index) == expected_value
37+
38+
@pytest.mark.synergy
39+
def test_create_double_array(self, synergy: Synergy):
40+
"""
41+
Test that DoubleArray can be created from Synergy instance.
42+
"""
43+
double_array = synergy.create_double_array()
44+
self._check_double_array_size(double_array, 0)
45+
46+
def test_add_double_single_value(self, double_array: DoubleArray):
47+
"""
48+
Test adding a single double value to the array.
49+
"""
50+
self._check_double_array_size(double_array, 0)
51+
52+
double_array.add_double(42.5)
53+
self._check_double_array_size(double_array, 1)
54+
self._check_value_at_index(double_array, 0, 42.5)
55+
56+
@pytest.mark.parametrize("values", [[1.0, 2.5, 3.7, 0, 1, 0.0, -1.1]])
57+
def test_add_double_multiple_values(self, double_array: DoubleArray, values: list[float | int]):
58+
"""
59+
Test adding multiple double values to the array.
60+
"""
61+
self._check_double_array_size(double_array, 0)
62+
63+
for i, value in enumerate(values):
64+
double_array.add_double(value)
65+
self._check_double_array_size(double_array, i + 1)
66+
self._check_value_at_index(double_array, i, float(value))
67+
68+
# Verify all values are still correct after all additions
69+
self._check_double_array_size(double_array, len(values))
70+
for i, value in enumerate(values):
71+
self._check_value_at_index(double_array, i, float(value))
72+
73+
def test_val_method_indexing(self, double_array: DoubleArray):
74+
"""
75+
Test accessing values by index using the val method.
76+
"""
77+
test_values = [10.5, -20.25, 0.0, 100.123, -5.75]
78+
79+
# Add test values
80+
for value in test_values:
81+
double_array.add_double(value)
82+
83+
# Test accessing each value by index
84+
for i, expected_value in enumerate(test_values):
85+
self._check_value_at_index(double_array, i, expected_value)
86+
87+
@pytest.mark.parametrize("size", [1, 5, 10])
88+
def test_size_property(self, double_array: DoubleArray, size: int):
89+
"""
90+
Test that the size property correctly reflects the number of values.
91+
"""
92+
self._check_double_array_size(double_array, 0)
93+
94+
for i in range(size):
95+
double_array.add_double(float(i * 1.5))
96+
self._check_double_array_size(double_array, i + 1)
97+
98+
self._check_double_array_size(double_array, size)
99+
100+
def test_to_list_empty_array(self, double_array: DoubleArray):
101+
"""
102+
Test converting an empty double array to a list.
103+
"""
104+
result = double_array.to_list()
105+
assert result == []
106+
assert isinstance(result, list)
107+
108+
@pytest.mark.parametrize("values", [[0.0, -1, 10.25, -5.75]])
109+
def test_to_list_populated_array(self, double_array: DoubleArray, values: list[float | int]):
110+
"""
111+
Test converting a populated double array to a list.
112+
"""
113+
# Add values to array
114+
for value in values:
115+
double_array.add_double(value)
116+
117+
# Convert to list
118+
result = double_array.to_list()
119+
120+
# Verify the result
121+
assert isinstance(result, list)
122+
assert len(result) == len(values)
123+
assert result == values
124+
125+
def test_from_list_empty_list(self, double_array: DoubleArray):
126+
"""
127+
Test creating a double array from an empty list.
128+
"""
129+
double_array.from_list([])
130+
self._check_double_array_size(double_array, 0)
131+
assert double_array.to_list() == []
132+
133+
@pytest.mark.parametrize("values", [[0.0, -1, 10.25, -5.75]])
134+
def test_from_list_populated_list(
135+
self, double_array: DoubleArray, values: list[float | int] | tuple
136+
):
137+
"""
138+
Test creating a double array from a populated list.
139+
"""
140+
double_array.from_list(values)
141+
142+
# Verify size
143+
self._check_double_array_size(double_array, len(values))
144+
145+
# Verify values
146+
for i, expected_value in enumerate(values):
147+
self._check_value_at_index(double_array, i, float(expected_value))
148+
149+
# Verify to_list conversion
150+
result = double_array.to_list()
151+
assert len(result) == len(values)
152+
assert result == values
153+
154+
def test_round_trip_conversion(self, double_array: DoubleArray):
155+
"""
156+
Test round-trip conversion: list -> DoubleArray -> list.
157+
"""
158+
original_values = [1.5, -2.25, 0.0, 100.123, -5.75, 42.0]
159+
160+
# Convert list to DoubleArray
161+
double_array.from_list(original_values)
162+
163+
# Convert back to list
164+
result_values = double_array.to_list()
165+
166+
# Verify round-trip conversion
167+
assert len(result_values) == len(original_values)
168+
assert result_values == original_values
169+
170+
def test_round_trip_conversion2(self, synergy: Synergy):
171+
"""
172+
Test round-trip conversion: list -> DoubleArray -> list.
173+
"""
174+
double_array = synergy.create_double_array()
175+
double_array2 = synergy.create_double_array()
176+
original_values = [1.5, -2.25, 0.0, 100.123, -5.75, 42.0]
177+
178+
for value in original_values:
179+
double_array.add_double(value)
180+
181+
result_values = double_array.to_list()
182+
183+
double_array2.from_list(result_values)
184+
185+
for i, value in enumerate(original_values):
186+
self._check_value_at_index(double_array, i, float(value))
187+
self._check_value_at_index(double_array2, i, float(value))
188+
189+
def test_reference_behavior(self, double_array: DoubleArray):
190+
"""
191+
Test reference behavior of DoubleArray.
192+
"""
193+
double_array.add_double(1.1)
194+
double_array_copy = double_array
195+
self._check_double_array_size(double_array_copy, 1)
196+
self._check_value_at_index(double_array_copy, 0, 1.1)
197+
198+
double_array_copy.add_double(2.2)
199+
self._check_double_array_size(double_array_copy, 2)
200+
self._check_value_at_index(double_array_copy, 0, 1.1)
201+
self._check_value_at_index(double_array_copy, 1, 2.2)
202+
self._check_value_at_index(double_array, 1, 2.2)
203+
204+
double_array_copy2 = DoubleArray(double_array.double_array)
205+
self._check_double_array_size(double_array_copy2, 2)
206+
self._check_value_at_index(double_array_copy2, 0, 1.1)
207+
self._check_value_at_index(double_array_copy2, 1, 2.2)
208+
209+
double_array_copy2.add_double(3.3)
210+
self._check_double_array_size(double_array_copy2, 3)
211+
self._check_value_at_index(double_array_copy2, 0, 1.1)
212+
self._check_value_at_index(double_array_copy2, 1, 2.2)
213+
self._check_value_at_index(double_array_copy2, 2, 3.3)

tests/api/unit_tests/test_unit_double_array.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313

1414
@pytest.mark.unit
15+
@pytest.mark.double_array
1516
class TestUnitDoubleArray:
1617
"""
1718
Unit Test Suite for the DoubleArray class.

0 commit comments

Comments
 (0)