|
1 | 1 | """Tests gather_metadata module""" |
2 | 2 |
|
| 3 | +import json |
3 | 4 | import os |
4 | 5 | import unittest |
5 | | -from datetime import datetime |
| 6 | +from datetime import datetime, timezone |
6 | 7 | from pathlib import Path |
7 | 8 | from unittest.mock import MagicMock, mock_open, patch |
8 | 9 |
|
9 | 10 | from aind_data_schema.components.identifiers import Person |
| 11 | +from aind_data_schema.core.acquisition import Acquisition |
10 | 12 | from aind_data_schema.core.metadata import Metadata |
11 | 13 | from aind_data_schema_models.modalities import Modality |
12 | 14 | from aind_data_schema_models.organizations import Organization |
13 | 15 |
|
14 | | -from aind_metadata_mapper.gather_metadata import ( |
15 | | - GatherMetadataJob, |
16 | | - _metadata_service_helper, |
17 | | -) |
| 16 | +from aind_metadata_mapper.gather_metadata import GatherMetadataJob, _metadata_service_helper |
18 | 17 | from aind_metadata_mapper.models import JobSettings |
19 | 18 |
|
20 | 19 | TEST_DIR = Path(os.path.dirname(os.path.realpath(__file__))) |
@@ -809,6 +808,39 @@ def test_merge_models_acquisition(self): |
809 | 808 | self.assertIsInstance(result, dict) |
810 | 809 | self.assertIn("acquisition_start_time", result) |
811 | 810 |
|
| 811 | + def test_merge_models_datetime_serialization(self): |
| 812 | + """Test that merged models can be JSON serialized (datetime objects converted to strings)""" |
| 813 | + # Create two minimal acquisition models with datetime fields |
| 814 | + now = datetime.now(timezone.utc) |
| 815 | + |
| 816 | + acq1_dict = { |
| 817 | + "subject_id": "test", |
| 818 | + "acquisition_start_time": now.isoformat(), |
| 819 | + "acquisition_end_time": now.isoformat(), |
| 820 | + "instrument_id": "test", |
| 821 | + "acquisition_type": "test", |
| 822 | + "data_streams": [], |
| 823 | + } |
| 824 | + |
| 825 | + acq2_dict = { |
| 826 | + "subject_id": "test", |
| 827 | + "acquisition_start_time": now.isoformat(), |
| 828 | + "acquisition_end_time": now.isoformat(), |
| 829 | + "instrument_id": "test", |
| 830 | + "acquisition_type": "test", |
| 831 | + "data_streams": [], |
| 832 | + } |
| 833 | + |
| 834 | + # Merge the two acquisitions using _merge_models |
| 835 | + merged_dict = self.job._merge_models(Acquisition, [acq1_dict, acq2_dict]) |
| 836 | + |
| 837 | + # Verify the merged dict can be JSON serialized |
| 838 | + # This will fail if merged_dict contains datetime objects instead of strings |
| 839 | + json_str = json.dumps(merged_dict, indent=2) |
| 840 | + self.assertIsInstance(json_str, str) |
| 841 | + # Verify datetime fields are strings, not datetime objects |
| 842 | + self.assertIsInstance(merged_dict.get("acquisition_start_time"), str) |
| 843 | + |
812 | 844 | def test_get_instrument_multiple_files(self): |
813 | 845 | """Test get_instrument with multiple instrument files""" |
814 | 846 | import json |
|
0 commit comments