|
| 1 | +"""Tests for psyflow.BlockUnit.""" |
| 2 | + |
| 3 | +from functools import partial |
| 4 | +import unittest |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | +from types import SimpleNamespace |
| 7 | + |
| 8 | +try: |
| 9 | + import numpy # noqa: F401 |
| 10 | + from psychopy import core, logging # noqa: F401 |
| 11 | + _HAS_DEPS = True |
| 12 | +except ImportError: |
| 13 | + _HAS_DEPS = False |
| 14 | + |
| 15 | +if _HAS_DEPS: |
| 16 | + from psyflow.BlockUnit import BlockUnit |
| 17 | + |
| 18 | + |
| 19 | +def _make_settings(**overrides): |
| 20 | + """Create a minimal settings-like object.""" |
| 21 | + defaults = { |
| 22 | + "trials_per_block": 3, |
| 23 | + "block_seed": [42], |
| 24 | + } |
| 25 | + defaults.update(overrides) |
| 26 | + return SimpleNamespace(**defaults) |
| 27 | + |
| 28 | + |
| 29 | +def _make_block(**overrides): |
| 30 | + """Build a BlockUnit without calling __init__ (avoids PsychoPy window).""" |
| 31 | + block = BlockUnit.__new__(BlockUnit) |
| 32 | + defaults = dict( |
| 33 | + block_id="test", |
| 34 | + block_idx=0, |
| 35 | + n_trials=3, |
| 36 | + settings=_make_settings(), |
| 37 | + win=MagicMock(), |
| 38 | + kb=MagicMock(), |
| 39 | + seed=42, |
| 40 | + conditions=None, |
| 41 | + results=[], |
| 42 | + meta={}, |
| 43 | + _on_start=[], |
| 44 | + _on_end=[], |
| 45 | + ) |
| 46 | + defaults.update(overrides) |
| 47 | + for k, v in defaults.items(): |
| 48 | + setattr(block, k, v) |
| 49 | + return block |
| 50 | + |
| 51 | + |
| 52 | +@unittest.skipUnless(_HAS_DEPS, "requires numpy and psychopy") |
| 53 | +class TestRunTrialGuards(unittest.TestCase): |
| 54 | + """run_trial() should reject invalid state with clear errors.""" |
| 55 | + |
| 56 | + def test_conditions_none_raises_runtime_error(self): |
| 57 | + block = _make_block(conditions=None) |
| 58 | + |
| 59 | + with self.assertRaises(RuntimeError) as ctx: |
| 60 | + block.run_trial(lambda win, kb, s, c: {"rt": 0.5}) |
| 61 | + |
| 62 | + self.assertIn("conditions", str(ctx.exception).lower()) |
| 63 | + |
| 64 | + def test_func_returning_none_raises_type_error(self): |
| 65 | + block = _make_block(conditions=["A"]) |
| 66 | + |
| 67 | + def bad_trial_func(win, kb, settings, cond): |
| 68 | + return None |
| 69 | + |
| 70 | + with self.assertRaises(TypeError) as ctx: |
| 71 | + block.run_trial(bad_trial_func) |
| 72 | + |
| 73 | + self.assertIn("dict", str(ctx.exception).lower()) |
| 74 | + |
| 75 | + def test_partial_trial_func_raises_type_error(self): |
| 76 | + block = _make_block(conditions=["A"]) |
| 77 | + |
| 78 | + def bad_trial_func(win, kb, settings, cond): |
| 79 | + return None |
| 80 | + |
| 81 | + with self.assertRaises(TypeError) as ctx: |
| 82 | + block.run_trial(partial(bad_trial_func)) |
| 83 | + |
| 84 | + self.assertIn("bad_trial_func", str(ctx.exception)) |
| 85 | + |
| 86 | + |
| 87 | +@unittest.skipUnless(_HAS_DEPS, "requires numpy and psychopy") |
| 88 | +class TestLoggingBlockInfo(unittest.TestCase): |
| 89 | + """logging_block_info() should handle list-backed conditions.""" |
| 90 | + |
| 91 | + def test_counts_python_list_conditions(self): |
| 92 | + block = _make_block(conditions=["A", "B", "A"]) |
| 93 | + |
| 94 | + with patch("psyflow.BlockUnit.logging.data") as mock_log: |
| 95 | + block.logging_block_info() |
| 96 | + |
| 97 | + messages = [call.args[0] for call in mock_log.call_args_list] |
| 98 | + self.assertTrue( |
| 99 | + any( |
| 100 | + "Blockdist:" in msg and "'A': 2" in msg and "'B': 1" in msg |
| 101 | + for msg in messages |
| 102 | + ), |
| 103 | + msg=f"Unexpected log messages: {messages!r}", |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + unittest.main() |
0 commit comments