11import os
22import unittest
3- from datetime import date
3+ from datetime import date , datetime
44from io import StringIO
55from tempfile import TemporaryDirectory
66from typing import ClassVar
99
1010import pandas as pd
1111import testing .postgresql
12+ from astropy .time import Time , TimezoneInfo
13+ from astropy .units import hour
1214from lsst .resources import ResourcePath
1315
1416from rubin_sim .sim_archive import vseqarchive , vseqmetadata
@@ -71,16 +73,24 @@ def setUpClass(cls) -> None:
7173 vseqmetadata .VSARCHIVE_PGPORT = cls .test_database .psycopg2_dsn ()["port" ]
7274 vseqmetadata .VSARCHIVE_PGSCHEMA = TEST_METADATA_DB_SCHEMA
7375
76+ # Set the creation time by hand and assign it directly
77+ # so that if the dayobs rolls over during the test it
78+ # won't cause tests to fail.
79+ creation_time = Time .now ()
80+
7481 # Create a simple simulation using TEST_VISITS data
7582 cls .sim_uuid = cls .vsarch .record_simulation_metadata (
7683 TEST_VISITS ,
7784 "Test simonyi simulation" ,
7885 first_day_obs = "2026-12-01" ,
7986 last_day_obs = "2026-12-02" ,
87+ creation_time = creation_time ,
8088 telescope = "simonyi" ,
8189 )
82- # Store the sim creation date for our test
83- cls .sim_creation_date = date (2026 , 12 , 1 )
90+ # Store the sim creation dayobs
91+ sim_creation_datetime = creation_time .to_datetime (timezone = TimezoneInfo (utc_offset = - 12 * hour ))
92+ assert isinstance (sim_creation_datetime , datetime )
93+ cls .sim_creation_dayobs = sim_creation_datetime .date ()
8494 cls .vsarch .tag (cls .sim_uuid , "prenight" , "nominal" , "ideal" )
8595
8696 # Add visits to the simulation
@@ -148,11 +158,7 @@ def test_get_prenight_index_from_database_with_integer_dayobs(self) -> None:
148158 self .assertEqual (len (result ), 1 )
149159
150160 def test_get_sim_uuid (self ) -> None :
151- # Test the get_sim_uuid function
152- # We should be able to find our test simulation
153- # Using today's date as requested
154- today = date .today ()
155- result = get_sim_uuid (today , 1 , 20261201 )
161+ result = get_sim_uuid (self .sim_creation_dayobs , 1 , 20261201 )
156162
157163 # Should return a UUID
158164 self .assertIsInstance (result , UUID )
@@ -162,18 +168,18 @@ def test_get_sim_uuid(self) -> None:
162168
163169 # Test with date object for day_obs
164170 result2 = get_sim_uuid (
165- today ,
171+ self . sim_creation_dayobs ,
166172 1 ,
167173 date (2026 , 12 , 1 ),
168174 )
169175 self .assertEqual (result2 , self .sim_uuid )
170176
171177 # Test with str for day_obs
172- result3 = get_sim_uuid (today , 1 , "20261201" )
178+ result3 = get_sim_uuid (self . sim_creation_dayobs , 1 , "20261201" )
173179 self .assertEqual (result3 , self .sim_uuid )
174180
175181 # Test with iso str for day_obs
176- result4 = get_sim_uuid (today , 1 , "2026-12-01" )
182+ result4 = get_sim_uuid (self . sim_creation_dayobs , 1 , "2026-12-01" )
177183 self .assertEqual (result4 , self .sim_uuid )
178184
179185 # Test that it raises ValueError for non-existent simulation
@@ -183,7 +189,7 @@ def test_get_sim_uuid(self) -> None:
183189
184190 # Test that it raises ValueError for non-existent daily_id
185191 with self .assertRaises (ValueError ):
186- get_sim_uuid (today , 2 , 20261201 )
192+ get_sim_uuid (self . sim_creation_dayobs , 2 , 20261201 )
187193
188194 def test_get_sim_metadata (self ) -> None :
189195 # Test the get_sim_index_info function
@@ -291,15 +297,15 @@ def test_get_prenight_index_from_bucket(self) -> None:
291297
292298 # Check that the first row has expected data
293299 first_row = result .loc ["6c242afb-edd1-4cea-9f8c-80e0a18b4b75" ]
294- self .assertEqual (first_row ["sim_creation_day_obs" ], " 2026-04-11" )
300+ self .assertEqual (first_row ["sim_creation_day_obs" ], date ( 2026 , 4 , 11 ) )
295301 self .assertEqual (first_row ["daily_id" ], 2 )
296302 self .assertIn ("ideal" , first_row ["tags" ])
297303 self .assertIn ("nominal" , first_row ["tags" ])
298304 self .assertIn ("prenight" , first_row ["tags" ])
299305
300306 # Check that the second row has expected data
301307 second_row = result .loc ["b9405aaf-dfe8-4508-ad90-cb37527dbc27" ]
302- self .assertEqual (second_row ["sim_creation_day_obs" ], " 2026-04-11" )
308+ self .assertEqual (second_row ["sim_creation_day_obs" ], date ( 2026 , 4 , 11 ) )
303309 self .assertEqual (second_row ["daily_id" ], 3 )
304310 self .assertIn ("ideal" , second_row ["tags" ])
305311 self .assertIn ("nominal" , second_row ["tags" ])
0 commit comments