Skip to content

Commit 3da69fd

Browse files
committed
Add pytest tests for object column type
- schema_object.py: Test table definitions for object type - test_object.py: Comprehensive tests covering: - Storage path generation utilities - Insert with file, folder, and stream - Fetch returning ObjectRef - ObjectRef methods (read, open, download, listdir, walk, verify) - Staged insert operations - Error cases - conftest.py: Object storage fixtures for testing
1 parent 08838f6 commit 3da69fd

File tree

3 files changed

+853
-0
lines changed

3 files changed

+853
-0
lines changed

tests/conftest.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,3 +903,68 @@ def channel(schema_any):
903903
@pytest.fixture
904904
def trash(schema_any):
905905
return schema.UberTrash()
906+
907+
908+
# Object storage fixtures
909+
from . import schema_object
910+
911+
912+
@pytest.fixture
913+
def object_storage_config(tmpdir_factory):
914+
"""Create object storage configuration for testing."""
915+
location = str(tmpdir_factory.mktemp("object_storage"))
916+
return {
917+
"project_name": "test_project",
918+
"protocol": "file",
919+
"location": location,
920+
"token_length": 8,
921+
}
922+
923+
924+
@pytest.fixture
925+
def mock_object_storage(object_storage_config, monkeypatch):
926+
"""Mock object storage configuration in datajoint config."""
927+
# Store original config
928+
original_object_storage = getattr(dj.config, "_object_storage", None)
929+
930+
# Create a mock ObjectStorageSettings-like object
931+
class MockObjectStorageSettings:
932+
def __init__(self, config):
933+
self.project_name = config["project_name"]
934+
self.protocol = config["protocol"]
935+
self.location = config["location"]
936+
self.token_length = config.get("token_length", 8)
937+
self.partition_pattern = config.get("partition_pattern")
938+
self.bucket = config.get("bucket")
939+
self.endpoint = config.get("endpoint")
940+
self.access_key = config.get("access_key")
941+
self.secret_key = config.get("secret_key")
942+
self.secure = config.get("secure", True)
943+
self.container = config.get("container")
944+
945+
mock_settings = MockObjectStorageSettings(object_storage_config)
946+
947+
# Patch the object_storage attribute
948+
monkeypatch.setattr(dj.config, "object_storage", mock_settings)
949+
950+
yield object_storage_config
951+
952+
# Restore original
953+
if original_object_storage is not None:
954+
monkeypatch.setattr(dj.config, "object_storage", original_object_storage)
955+
956+
957+
@pytest.fixture
958+
def schema_obj(connection_test, prefix, mock_object_storage):
959+
"""Schema for object type tests."""
960+
schema = dj.Schema(
961+
prefix + "_object",
962+
context=schema_object.LOCALS_OBJECT,
963+
connection=connection_test,
964+
)
965+
schema(schema_object.ObjectFile)
966+
schema(schema_object.ObjectFolder)
967+
schema(schema_object.ObjectMultiple)
968+
schema(schema_object.ObjectWithOther)
969+
yield schema
970+
schema.drop()

tests/schema_object.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Schema definitions for object type tests.
3+
"""
4+
5+
import datajoint as dj
6+
7+
LOCALS_OBJECT = locals()
8+
9+
10+
class ObjectFile(dj.Manual):
11+
"""Table for testing object type with files."""
12+
13+
definition = """
14+
file_id : int
15+
---
16+
data_file : object # stored file
17+
"""
18+
19+
20+
class ObjectFolder(dj.Manual):
21+
"""Table for testing object type with folders."""
22+
23+
definition = """
24+
folder_id : int
25+
---
26+
data_folder : object # stored folder
27+
"""
28+
29+
30+
class ObjectMultiple(dj.Manual):
31+
"""Table for testing multiple object attributes."""
32+
33+
definition = """
34+
record_id : int
35+
---
36+
raw_data : object # raw data file
37+
processed : object # processed data file
38+
"""
39+
40+
41+
class ObjectWithOther(dj.Manual):
42+
"""Table for testing object type with other attributes."""
43+
44+
definition = """
45+
subject_id : int
46+
session_id : int
47+
---
48+
name : varchar(100)
49+
data_file : object
50+
notes : varchar(255)
51+
"""

0 commit comments

Comments
 (0)