Skip to content

Commit

Permalink
Disable hidden attributes (#1091) by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ethho committed Sep 22, 2024
1 parent 467990e commit 2aab3e8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
22 changes: 12 additions & 10 deletions datajoint/declare.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .errors import DataJointError, _support_filepath_types, FILEPATH_FEATURE_SWITCH
from .attribute_adapter import get_adapter
from .condition import translate_attribute
from .settings import config

UUID_DATA_TYPE = "binary(16)"
MAX_TABLE_NAME_LENGTH = 64
Expand Down Expand Up @@ -311,17 +312,18 @@ def declare(full_table_name, definition, context):
external_stores,
) = prepare_declare(definition, context)

metadata_attr_sql = [
"`_{full_table_name}_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"
]
attribute_sql.extend(
attr.format(
full_table_name=sha1(
full_table_name.replace("`", "").encode("utf-8")
).hexdigest()
if config.get("enable_hidden_attributes", False):
metadata_attr_sql = [
"`_{full_table_name}_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"
]
attribute_sql.extend(
attr.format(
full_table_name=sha1(
full_table_name.replace("`", "").encode("utf-8")
).hexdigest()
)
for attr in metadata_attr_sql
)
for attr in metadata_attr_sql
)

if not primary_key:
raise DataJointError("Table must have a primary key")
Expand Down
1 change: 1 addition & 0 deletions datajoint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"display.show_tuple_count": True,
"database.use_tls": None,
"enable_python_native_blobs": True, # python-native/dj0 encoding support
"enable_hidden_attributes": False,
"filepath_checksum_size_limit": None, # file size limit for when to disable checksums
}
)
Expand Down
57 changes: 51 additions & 6 deletions tests/test_declare.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
import datajoint as dj
import inspect
from datajoint.declare import declare
from datajoint.settings import config


@pytest.fixture(scope="function")
def enable_hidden_attributes():
orig_config_val = config.get("enable_hidden_attributes")
config["enable_hidden_attributes"] = True
yield
if orig_config_val is not None:
config["enable_hidden_attributes"] = orig_config_val


@pytest.fixture(scope="function")
def disable_hidden_attributes():
orig_config_val = config.get("enable_hidden_attributes")
config["enable_hidden_attributes"] = False
yield
if orig_config_val is not None:
config["enable_hidden_attributes"] = orig_config_val


def test_schema_decorator(schema_any):
Expand Down Expand Up @@ -373,9 +392,35 @@ class Table_With_Underscores(dj.Manual):
schema_any(Table_With_Underscores)


def test_hidden_attributes(schema_any):
assert (
list(Experiment().heading._attributes.keys())[-1].split("_")[2] == "timestamp"
)
assert any(a.is_hidden for a in Experiment().heading._attributes.values())
assert not any(a.is_hidden for a in Experiment().heading.attributes.values())
def test_hidden_attributes_default_value():
config_val = config.get("enable_hidden_attributes")
assert config_val is not None and not config_val, \
"Default value for enable_hidden_attributes is not False"


def test_hidden_attributes_enabled(enable_hidden_attributes, schema_any):
orig_config_val = config.get("enable_hidden_attributes")
config["enable_hidden_attributes"] = True

msg = f"{Experiment().heading._attributes=}"
assert any(a.name.endswith("_timestamp") for a in Experiment().heading._attributes.values()), msg
assert any(a.name.startswith("_") for a in Experiment().heading._attributes.values()), msg
assert any(a.is_hidden for a in Experiment().heading._attributes.values()), msg
assert not any(a.is_hidden for a in Experiment().heading.attributes.values()), msg

if orig_config_val is not None:
config["enable_hidden_attributes"] = orig_config_val


def test_hidden_attributes_disabled(disable_hidden_attributes, schema_any):
orig_config_val = config.get("enable_hidden_attributes")
config["enable_hidden_attributes"] = False

msg = f"{Experiment().heading._attributes=}"
assert not any(a.name.endswith("_timestamp") for a in Experiment().heading._attributes.values()), msg
assert not any(a.name.startswith("_") for a in Experiment().heading._attributes.values()), msg
assert not any(a.is_hidden for a in Experiment().heading._attributes.values()), msg
assert not any(a.is_hidden for a in Experiment().heading.attributes.values()), msg

if orig_config_val is not None:
config["enable_hidden_attributes"] = orig_config_val

0 comments on commit 2aab3e8

Please sign in to comment.