|
| 1 | +"""Tests for delay properties in the Opentrons protocol API.""" |
| 2 | + |
| 3 | +from pydantic import ValidationError |
| 4 | +import pytest |
| 5 | +from typing import Any, Union |
| 6 | +from hypothesis import given, strategies as st, settings |
| 7 | + |
| 8 | +from opentrons.protocol_api._liquid_properties import _build_touch_tip_properties |
| 9 | +from opentrons_shared_data.liquid_classes.liquid_class_definition import ( |
| 10 | + TouchTipProperties, |
| 11 | + LiquidClassTouchTipParams, |
| 12 | +) |
| 13 | + |
| 14 | +from . import ( |
| 15 | + boolean_looking_values, |
| 16 | + invalid_values, |
| 17 | + positive_non_zero_floats_and_ints, |
| 18 | + reasonable_numbers, |
| 19 | + negative_or_zero_floats_and_ints, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +def test_touch_tip_properties_enable_and_disable() -> None: |
| 24 | + """Test enabling and disabling TouchTipProperties.""" |
| 25 | + tp = _build_touch_tip_properties( |
| 26 | + TouchTipProperties( |
| 27 | + enable=False, |
| 28 | + params=LiquidClassTouchTipParams(zOffset=1, mmToEdge=1, speed=100), |
| 29 | + ) |
| 30 | + ) |
| 31 | + tp.enabled = True |
| 32 | + assert tp.enabled is True |
| 33 | + tp.enabled = False |
| 34 | + assert tp.enabled is False |
| 35 | + |
| 36 | + |
| 37 | +def test_touch_tip_properties_none_instantiation_combos() -> None: |
| 38 | + """Test handling of None combinations in TouchTipProperties instantiation.""" |
| 39 | + with pytest.raises(ValidationError): |
| 40 | + _build_touch_tip_properties( |
| 41 | + TouchTipProperties(enable=True, params=LiquidClassTouchTipParams(zOffset=None, mmToEdge=None, speed=None)) # type: ignore |
| 42 | + ) |
| 43 | + with pytest.raises(ValidationError): |
| 44 | + _build_touch_tip_properties( |
| 45 | + TouchTipProperties(enable=None, params=LiquidClassTouchTipParams(zOffset=None, mmToEdge=1, speed=1)) # type: ignore |
| 46 | + ) |
| 47 | + with pytest.raises(ValidationError): |
| 48 | + _build_touch_tip_properties( |
| 49 | + TouchTipProperties(enable=True, params=LiquidClassTouchTipParams(zOffset=1, mmToEdge=None, speed=1)) # type: ignore |
| 50 | + ) |
| 51 | + with pytest.raises(ValidationError): |
| 52 | + _build_touch_tip_properties( |
| 53 | + TouchTipProperties(enable=True, params=LiquidClassTouchTipParams(zOffset=1, mmToEdge=1, speed=None)) # type: ignore |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +@given(bad_value=st.one_of(invalid_values, boolean_looking_values)) |
| 58 | +@settings(deadline=None, max_examples=50) |
| 59 | +def test_touch_tip_properties_enabled_bad_values(bad_value: Any) -> None: |
| 60 | + """Test bad values for TouchTipProperties.enabled.""" |
| 61 | + with pytest.raises(ValidationError): |
| 62 | + _build_touch_tip_properties( |
| 63 | + TouchTipProperties( |
| 64 | + enable=bad_value, |
| 65 | + params=LiquidClassTouchTipParams(zOffset=1, mmToEdge=1, speed=1), |
| 66 | + ) |
| 67 | + ) |
| 68 | + tp = _build_touch_tip_properties( |
| 69 | + TouchTipProperties( |
| 70 | + enable=False, |
| 71 | + params=LiquidClassTouchTipParams(zOffset=1, mmToEdge=1, speed=1), |
| 72 | + ) |
| 73 | + ) |
| 74 | + with pytest.raises(ValueError): |
| 75 | + tp.enabled = bad_value |
| 76 | + |
| 77 | + |
| 78 | +@given(good_value=reasonable_numbers) |
| 79 | +@settings(deadline=None, max_examples=50) |
| 80 | +def test_touch_tip_properties_z_offset(good_value: Union[int, float]) -> None: |
| 81 | + """Test valid z_offset.""" |
| 82 | + _build_touch_tip_properties( |
| 83 | + TouchTipProperties( |
| 84 | + enable=True, |
| 85 | + params=LiquidClassTouchTipParams(zOffset=good_value, mmToEdge=1, speed=10), |
| 86 | + ) |
| 87 | + ) |
| 88 | + tp = _build_touch_tip_properties( |
| 89 | + TouchTipProperties( |
| 90 | + enable=False, |
| 91 | + params=LiquidClassTouchTipParams(zOffset=0, mmToEdge=1, speed=10), |
| 92 | + ) |
| 93 | + ) |
| 94 | + tp.z_offset = good_value |
| 95 | + assert tp.z_offset == float(good_value) |
| 96 | + |
| 97 | + |
| 98 | +@given(bad_value=invalid_values) |
| 99 | +@settings(deadline=None, max_examples=50) |
| 100 | +def test_touch_tip_properties_z_offset_bad_values(bad_value: Any) -> None: |
| 101 | + """Test invalid z_offset values.""" |
| 102 | + with pytest.raises(ValidationError): |
| 103 | + _build_touch_tip_properties( |
| 104 | + TouchTipProperties( |
| 105 | + enable=True, |
| 106 | + params=LiquidClassTouchTipParams( |
| 107 | + zOffset=bad_value, mmToEdge=1, speed=10 |
| 108 | + ), |
| 109 | + ) |
| 110 | + ) |
| 111 | + tp = _build_touch_tip_properties( |
| 112 | + TouchTipProperties( |
| 113 | + enable=False, |
| 114 | + params=LiquidClassTouchTipParams(zOffset=0, mmToEdge=1, speed=10), |
| 115 | + ) |
| 116 | + ) |
| 117 | + with pytest.raises(ValueError): |
| 118 | + tp.z_offset = bad_value |
| 119 | + |
| 120 | + |
| 121 | +@given(good_value=reasonable_numbers) |
| 122 | +@settings(deadline=None, max_examples=50) |
| 123 | +def test_touch_tip_properties_mm_to_edge(good_value: Union[int, float]) -> None: |
| 124 | + """Test valid mm_to_edge.""" |
| 125 | + _build_touch_tip_properties( |
| 126 | + TouchTipProperties( |
| 127 | + enable=True, |
| 128 | + params=LiquidClassTouchTipParams(zOffset=0, mmToEdge=good_value, speed=10), |
| 129 | + ) |
| 130 | + ) |
| 131 | + tp = _build_touch_tip_properties( |
| 132 | + TouchTipProperties( |
| 133 | + enable=False, |
| 134 | + params=LiquidClassTouchTipParams(zOffset=0, mmToEdge=1, speed=10), |
| 135 | + ) |
| 136 | + ) |
| 137 | + tp.mm_to_edge = good_value |
| 138 | + assert tp.mm_to_edge == float(good_value) |
| 139 | + |
| 140 | + |
| 141 | +@given(bad_value=invalid_values) |
| 142 | +@settings(deadline=None, max_examples=50) |
| 143 | +def test_touch_tip_properties_mm_to_edge_bad_values(bad_value: Any) -> None: |
| 144 | + """Test invalid mm_to_edge values.""" |
| 145 | + with pytest.raises(ValidationError): |
| 146 | + _build_touch_tip_properties( |
| 147 | + TouchTipProperties( |
| 148 | + enable=True, |
| 149 | + params=LiquidClassTouchTipParams( |
| 150 | + zOffset=bad_value, mmToEdge=1, speed=10 |
| 151 | + ), |
| 152 | + ) |
| 153 | + ) |
| 154 | + tp = _build_touch_tip_properties( |
| 155 | + TouchTipProperties( |
| 156 | + enable=True, |
| 157 | + params=LiquidClassTouchTipParams(zOffset=0, mmToEdge=1, speed=10), |
| 158 | + ) |
| 159 | + ) |
| 160 | + with pytest.raises(ValueError): |
| 161 | + tp.mm_to_edge = bad_value |
| 162 | + |
| 163 | + |
| 164 | +@given(good_value=positive_non_zero_floats_and_ints) |
| 165 | +@settings(deadline=None, max_examples=50) |
| 166 | +def test_touch_tip_properties_speed(good_value: Union[int, float]) -> None: |
| 167 | + """Test valid speed.""" |
| 168 | + _build_touch_tip_properties( |
| 169 | + TouchTipProperties( |
| 170 | + enable=True, |
| 171 | + params=LiquidClassTouchTipParams(zOffset=0, mmToEdge=1, speed=good_value), |
| 172 | + ) |
| 173 | + ) |
| 174 | + tp = _build_touch_tip_properties( |
| 175 | + TouchTipProperties( |
| 176 | + enable=False, |
| 177 | + params=LiquidClassTouchTipParams(zOffset=0, mmToEdge=1, speed=10), |
| 178 | + ) |
| 179 | + ) |
| 180 | + tp.speed = good_value |
| 181 | + assert tp.speed == float(good_value) |
| 182 | + |
| 183 | + |
| 184 | +@given(bad_value=st.one_of(invalid_values, negative_or_zero_floats_and_ints)) |
| 185 | +@settings(deadline=None, max_examples=50) |
| 186 | +def test_touch_tip_properties_speed_bad_values(bad_value: Any) -> None: |
| 187 | + """Test invalid speed values.""" |
| 188 | + with pytest.raises(ValidationError): |
| 189 | + _build_touch_tip_properties( |
| 190 | + TouchTipProperties( |
| 191 | + enable=True, |
| 192 | + params=LiquidClassTouchTipParams( |
| 193 | + zOffset=0, mmToEdge=1, speed=bad_value |
| 194 | + ), |
| 195 | + ) |
| 196 | + ) |
| 197 | + tp = _build_touch_tip_properties( |
| 198 | + TouchTipProperties( |
| 199 | + enable=False, |
| 200 | + params=LiquidClassTouchTipParams(zOffset=0, mmToEdge=1, speed=10), |
| 201 | + ) |
| 202 | + ) |
| 203 | + with pytest.raises(ValueError): |
| 204 | + tp.speed = bad_value |
0 commit comments