Skip to content

Commit b643940

Browse files
Added modification date update and removed errant files from pyproject.toml (#452)
1 parent f111238 commit b643940

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

src/aind_metadata_mapper/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
import sys
66
import time
7-
from datetime import datetime
7+
from datetime import date, datetime
88
from pathlib import Path
99
from typing import Optional
1010
from urllib.parse import urljoin
@@ -348,6 +348,7 @@ def _write_instrument_to_path(
348348
def save_instrument(
349349
instrument_model: instrument.Instrument | dict | str | Path,
350350
replace: bool = False,
351+
update_modification_date: bool = True,
351352
) -> None:
352353
"""Save instrument and validate round-trip.
353354
@@ -361,6 +362,9 @@ def save_instrument(
361362
a JSON file. If a path (str or Path) is provided, the file is loaded and validated.
362363
replace : bool
363364
If True, overwrite existing record with same instrument_id and modification_date.
365+
update_modification_date : bool
366+
If True (default), set modification_date to today (YYYY-MM-DD). If False,
367+
keep the modification date as passed in the instrument.
364368
365369
Raises
366370
------
@@ -377,6 +381,9 @@ def save_instrument(
377381
if isinstance(instrument_model, dict):
378382
instrument_model = instrument.Instrument.model_validate(instrument_model)
379383

384+
if update_modification_date:
385+
instrument_model = instrument_model.model_copy(update={"modification_date": date.today()})
386+
380387
# Use model_dump_json() and parse to ensure dates are properly serialized
381388
source_dict = json.loads(instrument_model.model_dump_json())
382389
logger.info(f"POSTing instrument to {INSTRUMENT_BASE_URL}")

tests/test_utils.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import shutil
1212
import tempfile
1313
import unittest
14-
from datetime import datetime, timedelta, timezone
14+
from datetime import date, datetime, timedelta, timezone
1515
from pathlib import Path
1616
from types import SimpleNamespace
1717
from unittest.mock import MagicMock, patch
@@ -88,11 +88,40 @@ def test_get_instrument_writes_to_file_when_output_directory_given(self, mock_he
8888
def test_save_instrument_loads_from_filepath(self, mock_post, mock_get):
8989
"""save_instrument loads from file when given a path."""
9090
with open(INSTRUMENT_JSON) as f:
91-
mock_get.return_value = instrument.Instrument.model_validate(json.load(f))
91+
instrument_data = json.load(f)
92+
instrument_data["modification_date"] = date.today().isoformat()
93+
mock_get.return_value = instrument.Instrument.model_validate(instrument_data)
9294
mock_post.return_value = MagicMock(status_code=201)
9395
save_instrument(str(INSTRUMENT_JSON))
9496
mock_post.assert_called_once()
9597

98+
@patch("aind_metadata_mapper.utils.get_instrument")
99+
@patch("aind_metadata_mapper.utils.requests.post")
100+
def test_save_instrument_updates_modification_date_by_default(self, mock_post, mock_get):
101+
"""save_instrument sets modification_date to today when update_modification_date=True."""
102+
with open(INSTRUMENT_JSON) as f:
103+
instrument_data = json.load(f)
104+
instrument_data["modification_date"] = date.today().isoformat()
105+
mock_get.return_value = instrument.Instrument.model_validate(instrument_data)
106+
mock_post.return_value = MagicMock(status_code=201)
107+
save_instrument(str(INSTRUMENT_JSON))
108+
posted_json = mock_post.call_args[1]["json"]
109+
today = date.today().isoformat()
110+
self.assertEqual(posted_json["modification_date"], today)
111+
112+
@patch("aind_metadata_mapper.utils.get_instrument")
113+
@patch("aind_metadata_mapper.utils.requests.post")
114+
def test_save_instrument_preserves_modification_date_when_false(self, mock_post, mock_get):
115+
"""save_instrument keeps original modification_date when update_modification_date=False."""
116+
with open(INSTRUMENT_JSON) as f:
117+
instrument_data = json.load(f)
118+
original_date = instrument_data["modification_date"]
119+
mock_get.return_value = instrument.Instrument.model_validate(instrument_data)
120+
mock_post.return_value = MagicMock(status_code=201)
121+
save_instrument(str(INSTRUMENT_JSON), update_modification_date=False)
122+
posted_json = mock_post.call_args[1]["json"]
123+
self.assertEqual(posted_json["modification_date"], original_date)
124+
96125
@patch("aind_metadata_mapper.utils.get_instrument")
97126
def test_check_existing_instrument(self, mock_get):
98127
"""check_existing_instrument returns True when instrument exists."""

0 commit comments

Comments
 (0)