Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/spyglass/utils/mixins/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,15 @@ def _test_mode(self) -> bool:
Avoids circular import. Prevents prompt on delete.

Note: Using @property instead of @cached_property so we always get
current value from dj.config, even if test_mode changes after first access.
current value from settings, even if test_mode changes after first access.

Used by ...
- BaseMixin._spyglass_version
- HelpersMixin
"""
import datajoint as dj
from spyglass.settings import config as sg_config

# Check dj.config directly instead of importing module-level variable
# which gets stale if load_config() is called after initial import
return dj.config.get("custom", {}).get("test_mode", False)
return sg_config.test_mode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this config a dictionary? I think this needs...

Suggested change
return sg_config.test_mode
return sg_config.get('test_mode', False)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched it to just import test_mode directly


@cached_property
def _spyglass_version(self):
Expand Down
39 changes: 39 additions & 0 deletions tests/utils/test_dj_helper_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,42 @@ def test_deprecation_factory(caplog, common):
assert (
"Deprecation:" in caplog.text
), "No deprecation warning logged on migrated table."


def test_str_to_bool():
"""Test str_to_bool function handles various string inputs correctly.

This test verifies that str_to_bool properly converts string values
to booleans, which is essential for fixing issue #1528 where string
"false" was incorrectly evaluated as True.
"""
from spyglass.utils.dj_helper_fn import str_to_bool

# Test truthy strings (case insensitive)
assert str_to_bool("true") is True
assert str_to_bool("True") is True
assert str_to_bool("TRUE") is True
assert str_to_bool("t") is True
assert str_to_bool("T") is True
assert str_to_bool("yes") is True
assert str_to_bool("YES") is True
assert str_to_bool("y") is True
assert str_to_bool("Y") is True
assert str_to_bool("1") is True

# Test falsy strings
assert str_to_bool("false") is False
assert str_to_bool("False") is False
assert str_to_bool("FALSE") is False
assert str_to_bool("no") is False
assert str_to_bool("n") is False
assert str_to_bool("0") is False
assert str_to_bool("") is False
assert str_to_bool("random_string") is False

# Test non-string inputs
assert str_to_bool(True) is True
assert str_to_bool(False) is False
assert str_to_bool(None) is False
assert str_to_bool(0) is False
assert str_to_bool(1) is True
22 changes: 22 additions & 0 deletions tests/utils/test_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,25 @@ def test_mixin_del_orphans(dj_conn, Mixin, MixinChild):
Mixin().delete_orphans(dry_run=False, safemode=False)
post_del = Mixin().fetch1("id")
assert post_del == 0, "Delete orphans not working."


def test_test_mode_property_uses_settings(schema_test, Mixin):
"""Test that _test_mode property uses spyglass.settings.config.

Verifies fix for issue #1528 where string "false" was incorrectly
evaluated as True. The property should now use spyglass.settings.config.test_mode
which properly converts strings to booleans via str_to_bool().
"""
schema_test(Mixin)

# The _test_mode property should return a boolean
test_mode_value = Mixin()._test_mode
assert isinstance(test_mode_value, bool), (
"_test_mode should return a boolean value"
)

# In test environment, test_mode should be True
# (set by load_config fixture in conftest.py)
assert test_mode_value is True, (
"_test_mode should be True in test environment"
)
Loading