|
11 | 11 | import shutil |
12 | 12 | import tempfile |
13 | 13 | import unittest |
14 | | -from datetime import datetime, timedelta, timezone |
| 14 | +from datetime import date, datetime, timedelta, timezone |
15 | 15 | from pathlib import Path |
16 | 16 | from types import SimpleNamespace |
17 | 17 | from unittest.mock import MagicMock, patch |
@@ -88,11 +88,40 @@ def test_get_instrument_writes_to_file_when_output_directory_given(self, mock_he |
88 | 88 | def test_save_instrument_loads_from_filepath(self, mock_post, mock_get): |
89 | 89 | """save_instrument loads from file when given a path.""" |
90 | 90 | 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) |
92 | 94 | mock_post.return_value = MagicMock(status_code=201) |
93 | 95 | save_instrument(str(INSTRUMENT_JSON)) |
94 | 96 | mock_post.assert_called_once() |
95 | 97 |
|
| 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 | + |
96 | 125 | @patch("aind_metadata_mapper.utils.get_instrument") |
97 | 126 | def test_check_existing_instrument(self, mock_get): |
98 | 127 | """check_existing_instrument returns True when instrument exists.""" |
|
0 commit comments