|
| 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) |
0 commit comments