From 949cfd0274f4347e8102fa2ae57d89055bf105bc Mon Sep 17 00:00:00 2001 From: Derek Maggio Date: Wed, 1 May 2024 10:02:25 -0700 Subject: [PATCH] chore: separate some logic --- .../strategy/final_strategies.py | 81 +++++++++++++++++++ .../helper_strategies.py} | 79 +----------------- .../test_deck_configuration.py | 6 +- 3 files changed, 85 insertions(+), 81 deletions(-) create mode 100644 test-data-generation/src/test_data_generation/deck_configuration/strategy/final_strategies.py rename test-data-generation/src/test_data_generation/deck_configuration/{strategies.py => strategy/helper_strategies.py} (58%) diff --git a/test-data-generation/src/test_data_generation/deck_configuration/strategy/final_strategies.py b/test-data-generation/src/test_data_generation/deck_configuration/strategy/final_strategies.py new file mode 100644 index 00000000000..9bf70180f96 --- /dev/null +++ b/test-data-generation/src/test_data_generation/deck_configuration/strategy/final_strategies.py @@ -0,0 +1,81 @@ +"""Test data generation for deck configuration tests.""" +from hypothesis import assume, strategies as st +from test_data_generation.deck_configuration.datashapes import ( + Column, + DeckConfiguration, + Slot, + PossibleSlotContents as PSC, +) + +from test_data_generation.deck_configuration.strategy.helper_strategies import a_column + + +def _above_or_below_is_module_or_trash(col: Column, slot: Slot) -> bool: + """Return True if the deck has a module above or below the specified slot.""" + above = col.slot_above(slot) + below = col.slot_below(slot) + + return (above is not None and above.contents.is_module_or_trash_bin()) or ( + below is not None and below.contents.is_module_or_trash_bin() + ) + + +@st.composite +def a_deck_configuration_with_a_module_or_trash_slot_above_or_below_a_heater_shaker( + draw: st.DrawFn, +) -> DeckConfiguration: + """Generate a deck with a module or trash bin fixture above or below a heater shaker.""" + deck = draw( + st.builds( + DeckConfiguration.from_cols, + col1=a_column("1"), + col2=a_column( + "2", content_options=[PSC.LABWARE_SLOT, PSC.MAGNETIC_BLOCK_MODULE] + ), + col3=a_column("3"), + ) + ) + column = deck.column_by_number(draw(st.sampled_from(["1", "3"]))) + + assume(column.number_of(PSC.HEATER_SHAKER_MODULE) in [1, 2]) + for slot in column.slots: + if slot.contents is PSC.HEATER_SHAKER_MODULE: + assume(_above_or_below_is_module_or_trash(column, slot)) + deck.override_with_column(column) + + return deck + + +@st.composite +def a_deck_configuration_with_invalid_fixture_in_col_2( + draw: st.DrawFn, +) -> DeckConfiguration: + """Generate a deck with an invalid fixture in column 2.""" + POSSIBLE_FIXTURES = [ + PSC.LABWARE_SLOT, + PSC.TEMPERATURE_MODULE, + PSC.HEATER_SHAKER_MODULE, + PSC.TRASH_BIN, + PSC.MAGNETIC_BLOCK_MODULE, + ] + INVALID_FIXTURES = [ + PSC.HEATER_SHAKER_MODULE, + PSC.TRASH_BIN, + PSC.TEMPERATURE_MODULE, + ] + column2 = draw(a_column("2", content_options=POSSIBLE_FIXTURES)) + num_invalid_fixtures = len( + [True for slot in column2.slots if slot.contents.is_one_of(INVALID_FIXTURES)] + ) + assume(num_invalid_fixtures > 0) + + deck = draw( + st.builds( + DeckConfiguration.from_cols, + col1=a_column("1"), + col2=st.just(column2), + col3=a_column("3"), + ) + ) + + return deck diff --git a/test-data-generation/src/test_data_generation/deck_configuration/strategies.py b/test-data-generation/src/test_data_generation/deck_configuration/strategy/helper_strategies.py similarity index 58% rename from test-data-generation/src/test_data_generation/deck_configuration/strategies.py rename to test-data-generation/src/test_data_generation/deck_configuration/strategy/helper_strategies.py index 7aa558eb3c8..17950f63a39 100644 --- a/test-data-generation/src/test_data_generation/deck_configuration/strategies.py +++ b/test-data-generation/src/test_data_generation/deck_configuration/strategy/helper_strategies.py @@ -1,18 +1,13 @@ """Test data generation for deck configuration tests.""" from typing import List -from hypothesis import assume, strategies as st +from hypothesis import strategies as st from test_data_generation.deck_configuration.datashapes import ( Column, - DeckConfiguration, Row, Slot, PossibleSlotContents as PSC, ) -from test_data_generation.deck_configuration.deck_evaluation import ( - above_or_below_is_module_or_trash, -) - @st.composite def a_slot( @@ -120,75 +115,3 @@ def a_column( d=a_slot(row="d", col=col, content_options=content_options), ) ) - - -def _above_or_below_is_module_or_trash(col: Column, slot: Slot) -> bool: - """Return True if the deck has a module above or below the specified slot.""" - above = col.slot_above(slot) - below = col.slot_below(slot) - - return (above is not None and above.contents.is_module_or_trash_bin()) or ( - below is not None and below.contents.is_module_or_trash_bin() - ) - - - -@st.composite -def a_deck_configuration_with_a_module_or_trash_slot_above_or_below_a_heater_shaker( - draw: st.DrawFn, -) -> DeckConfiguration: - """Generate a deck with a module or trash bin fixture above or below a heater shaker.""" - deck = draw( - st.builds( - DeckConfiguration.from_cols, - col1=a_column("1"), - col2=a_column( - "2", content_options=[PSC.LABWARE_SLOT, PSC.MAGNETIC_BLOCK_MODULE] - ), - col3=a_column("3"), - ) - ) - column = deck.column_by_number(draw(st.sampled_from(["1", "3"]))) - - assume(column.number_of(PSC.HEATER_SHAKER_MODULE) in [1, 2]) - for slot in column.slots: - if slot.contents is PSC.HEATER_SHAKER_MODULE: - assume(_above_or_below_is_module_or_trash(column, slot)) - deck.override_with_column(column) - - return deck - - -@st.composite -def a_deck_configuration_with_invalid_fixture_in_col_2( - draw: st.DrawFn, -) -> DeckConfiguration: - """Generate a deck with an invalid fixture in column 2.""" - POSSIBLE_FIXTURES = [ - PSC.LABWARE_SLOT, - PSC.TEMPERATURE_MODULE, - PSC.HEATER_SHAKER_MODULE, - PSC.TRASH_BIN, - PSC.MAGNETIC_BLOCK_MODULE, - ] - INVALID_FIXTURES = [ - PSC.HEATER_SHAKER_MODULE, - PSC.TRASH_BIN, - PSC.TEMPERATURE_MODULE, - ] - column2 = draw(a_column("2", content_options=POSSIBLE_FIXTURES)) - num_invalid_fixtures = len( - [True for slot in column2.slots if slot.contents.is_one_of(INVALID_FIXTURES)] - ) - assume(num_invalid_fixtures > 0) - - deck = draw( - st.builds( - DeckConfiguration.from_cols, - col1=a_column("1"), - col2=st.just(column2), - col3=a_column("3"), - ) - ) - - return deck diff --git a/test-data-generation/tests/test_data_generation/deck_configuration/test_deck_configuration.py b/test-data-generation/tests/test_data_generation/deck_configuration/test_deck_configuration.py index 5b8df54dfd9..02c4f125187 100644 --- a/test-data-generation/tests/test_data_generation/deck_configuration/test_deck_configuration.py +++ b/test-data-generation/tests/test_data_generation/deck_configuration/test_deck_configuration.py @@ -2,7 +2,7 @@ from hypothesis import given, settings, HealthCheck from test_data_generation.deck_configuration.datashapes import DeckConfiguration -from test_data_generation.deck_configuration.strategies import ( +from test_data_generation.deck_configuration.strategy.final_strategies import ( a_deck_configuration_with_a_module_or_trash_slot_above_or_below_a_heater_shaker, a_deck_configuration_with_invalid_fixture_in_col_2, ) @@ -18,7 +18,7 @@ suppress_health_check=[HealthCheck.filter_too_much, HealthCheck.too_slow], ) def test_above_below_heater_shaker(deck_config: DeckConfiguration) -> None: - """I hypothesize, that every deck configuration with a non-labware slot fixture above or below a heater-shaker is invalid.""" + """I hypothesize, that any deck configuration with a non-labware slot fixture above or below a heater-shaker is invalid.""" print(deck_config) # TODO: create protocol and run analysis @@ -35,7 +35,7 @@ def test_above_below_heater_shaker(deck_config: DeckConfiguration) -> None: suppress_health_check=[HealthCheck.filter_too_much, HealthCheck.too_slow], ) def test_invalid_fixture_in_col_2(deck_config: DeckConfiguration) -> None: - """I hypothesize, that every deck configuration that contains at least one, Heater-Shaker, Trash Bin, or Temperature module, in column 2 is invalid.""" + """I hypothesize, that any deck configuration that contains at least one, Heater-Shaker, Trash Bin, or Temperature module, in column 2 is invalid.""" print(deck_config) # TODO: Same as above