Skip to content

Commit 443f422

Browse files
Jorge Fernandez HernandezJorge Fernandez Hernandez
authored andcommitted
GAIAMNGT-1700 Fix test to make use of a mocked datetime
1 parent db87123 commit 443f422

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

astroquery/gaia/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import shutil
1919
import zipfile
2020
from collections.abc import Iterable
21-
from datetime import datetime, timezone
21+
import datetime
2222

2323
from astropy import units
2424
from astropy import units as u
@@ -232,7 +232,7 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
232232

233233
output_file_specified = False
234234

235-
now = datetime.now(timezone.utc)
235+
now = datetime.datetime.now(datetime.timezone.utc)
236236
if not dump_to_file:
237237
now_formatted = now.strftime("%Y%m%d_%H%M%S")
238238
temp_dirname = "temp_" + now_formatted

astroquery/gaia/tests/test_gaiatap.py

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
1515
1616
"""
17+
import datetime
1718
import os
1819
import zipfile
1920
from pathlib import Path
@@ -85,6 +86,22 @@
8586
RADIUS = 1 * u.deg
8687
SKYCOORD = SkyCoord(ra=19 * u.deg, dec=20 * u.deg, frame="icrs")
8788

89+
FAKE_TIME = datetime.datetime(2024, 1, 1, 0, 0, 59)
90+
91+
92+
@pytest.fixture
93+
def patch_datetime_now(monkeypatch):
94+
class mydatetime(datetime.datetime):
95+
@classmethod
96+
def now(cls, tz=None):
97+
return FAKE_TIME
98+
99+
monkeypatch.setattr(datetime, 'datetime', mydatetime)
100+
101+
102+
def test_patch_datetime(patch_datetime_now):
103+
assert datetime.datetime.now() == FAKE_TIME
104+
88105

89106
@pytest.fixture(scope="module")
90107
def column_attrs():
@@ -170,7 +187,10 @@ def mock_querier():
170187

171188

172189
@pytest.fixture(scope="function")
173-
def mock_datalink_querier():
190+
def mock_datalink_querier(patch_datetime_now):
191+
192+
assert datetime.datetime.now(datetime.timezone.utc) == FAKE_TIME
193+
174194
conn_handler = DummyConnHandler()
175195
tapplus = TapPlus(url="http://test:1111/tap", connhandler=conn_handler)
176196

@@ -781,9 +801,15 @@ def test_cone_search_and_changing_MAIN_GAIA_TABLE(mock_querier_async):
781801
assert "name_from_class" in job.parameters["query"]
782802

783803

784-
@pytest.mark.parametrize("overwrite_output_file", [False, True])
804+
@pytest.mark.parametrize("overwrite_output_file", [True])
785805
def test_datalink_querier_load_data_vot_exception(mock_datalink_querier, overwrite_output_file):
786-
file_final = os.path.join(os.getcwd(), 'datalink_output.zip')
806+
assert datetime.datetime.now(datetime.timezone.utc) == FAKE_TIME
807+
808+
now = datetime.datetime.now(datetime.timezone.utc)
809+
output_file = 'datalink_output_' + now.strftime("%Y%m%dT%H%M%S") + '.zip'
810+
811+
file_final = Path(os.getcwd() + '/' + output_file)
812+
787813
Path(file_final).touch()
788814

789815
assert os.path.exists(file_final)
@@ -973,7 +999,10 @@ def test_datalink_querier_load_data_fits(mock_datalink_querier_fits):
973999

9741000

9751001
def test_load_data_vot(monkeypatch, tmp_path, tmp_path_factory):
976-
path = Path(os.getcwd() + '/' + 'datalink_output.zip')
1002+
now = datetime.datetime.now(datetime.timezone.utc)
1003+
output_file = 'datalink_output_' + now.strftime("%Y%m%dT%H%M%S") + '.zip'
1004+
1005+
path = Path(os.getcwd() + '/' + output_file)
9771006

9781007
with open(DL_PRODUCTS_VOT, 'rb') as file:
9791008
zip_bytes = file.read()
@@ -988,7 +1017,7 @@ def load_data_monkeypatched(self, params_dict, output_file, verbose):
9881017
"RETRIEVAL_TYPE": "epoch_photometry",
9891018
"DATA_STRUCTURE": "INDIVIDUAL",
9901019
"USE_ZIP_ALWAYS": "true"}
991-
assert output_file == os.getcwd() + '/' + 'datalink_output.zip'
1020+
assert str(path) == output_file
9921021
assert verbose is True
9931022

9941023
monkeypatch.setattr(TapPlus, "load_data", load_data_monkeypatched)
@@ -1007,7 +1036,10 @@ def load_data_monkeypatched(self, params_dict, output_file, verbose):
10071036

10081037
@pytest.mark.skip(reason="Thes fits files generate an error relatate to the unit 'log(cm.s**-2)")
10091038
def test_load_data_fits(monkeypatch, tmp_path, tmp_path_factory):
1010-
path = Path(os.getcwd() + '/' + 'datalink_output.zip')
1039+
now = datetime.datetime.now(datetime.timezone.utc)
1040+
output_file = 'datalink_output_' + now.strftime("%Y%m%dT%H%M%S") + '.zip'
1041+
1042+
path = Path(os.getcwd() + '/' + output_file)
10111043

10121044
with open(DL_PRODUCTS_FITS, 'rb') as file:
10131045
zip_bytes = file.read()
@@ -1040,7 +1072,10 @@ def load_data_monkeypatched(self, params_dict, output_file, verbose):
10401072

10411073

10421074
def test_load_data_csv(monkeypatch, tmp_path, tmp_path_factory):
1043-
path = Path(os.getcwd() + '/' + 'datalink_output.zip')
1075+
now = datetime.datetime.now(datetime.timezone.utc)
1076+
output_file = 'datalink_output_' + now.strftime("%Y%m%dT%H%M%S") + '.zip'
1077+
1078+
path = Path(os.getcwd() + '/' + output_file)
10441079

10451080
with open(DL_PRODUCTS_CSV, 'rb') as file:
10461081
zip_bytes = file.read()
@@ -1055,7 +1090,7 @@ def load_data_monkeypatched(self, params_dict, output_file, verbose):
10551090
"RETRIEVAL_TYPE": "epoch_photometry",
10561091
"DATA_STRUCTURE": "INDIVIDUAL",
10571092
"USE_ZIP_ALWAYS": "true"}
1058-
assert output_file == os.getcwd() + '/' + 'datalink_output.zip'
1093+
assert str(path) == output_file
10591094
assert verbose is True
10601095

10611096
monkeypatch.setattr(TapPlus, "load_data", load_data_monkeypatched)
@@ -1073,7 +1108,10 @@ def load_data_monkeypatched(self, params_dict, output_file, verbose):
10731108

10741109

10751110
def test_load_data_ecsv(monkeypatch, tmp_path, tmp_path_factory):
1076-
path = Path(os.getcwd() + '/' + 'datalink_output.zip')
1111+
now = datetime.datetime.now(datetime.timezone.utc)
1112+
output_file = 'datalink_output_' + now.strftime("%Y%m%dT%H%M%S") + '.zip'
1113+
1114+
path = Path(os.getcwd() + '/' + output_file)
10771115

10781116
with open(DL_PRODUCTS_ECSV, 'rb') as file:
10791117
zip_bytes = file.read()
@@ -1088,7 +1126,7 @@ def load_data_monkeypatched(self, params_dict, output_file, verbose):
10881126
"RETRIEVAL_TYPE": "epoch_photometry",
10891127
"DATA_STRUCTURE": "INDIVIDUAL",
10901128
"USE_ZIP_ALWAYS": "true"}
1091-
assert output_file == os.getcwd() + '/' + 'datalink_output.zip'
1129+
assert str(path) == output_file
10921130
assert verbose is True
10931131

10941132
monkeypatch.setattr(TapPlus, "load_data", load_data_monkeypatched)
@@ -1106,7 +1144,10 @@ def load_data_monkeypatched(self, params_dict, output_file, verbose):
11061144

11071145

11081146
def test_load_data_linking_parameter(monkeypatch, tmp_path):
1109-
path = Path(os.getcwd() + '/' + 'datalink_output.zip')
1147+
now = datetime.datetime.now(datetime.timezone.utc)
1148+
output_file = 'datalink_output_' + now.strftime("%Y%m%dT%H%M%S") + '.zip'
1149+
1150+
path = Path(os.getcwd() + '/' + output_file)
11101151

11111152
with open(DL_PRODUCTS_VOT, 'rb') as file:
11121153
zip_bytes = file.read()
@@ -1121,7 +1162,7 @@ def load_data_monkeypatched(self, params_dict, output_file, verbose):
11211162
"RETRIEVAL_TYPE": "epoch_photometry",
11221163
"DATA_STRUCTURE": "INDIVIDUAL",
11231164
"USE_ZIP_ALWAYS": "true"}
1124-
assert output_file == os.getcwd() + '/' + 'datalink_output.zip'
1165+
assert str(path) == output_file
11251166
assert verbose is True
11261167

11271168
monkeypatch.setattr(TapPlus, "load_data", load_data_monkeypatched)
@@ -1140,7 +1181,10 @@ def load_data_monkeypatched(self, params_dict, output_file, verbose):
11401181

11411182
@pytest.mark.parametrize("linking_param", ['TRANSIT_ID', 'IMAGE_ID'])
11421183
def test_load_data_linking_parameter_with_values(monkeypatch, tmp_path, linking_param):
1143-
path = Path(os.getcwd() + '/' + 'datalink_output.zip')
1184+
now = datetime.datetime.now(datetime.timezone.utc)
1185+
output_file = 'datalink_output_' + now.strftime("%Y%m%dT%H%M%S") + '.zip'
1186+
1187+
path = Path(os.getcwd() + '/' + output_file)
11441188

11451189
with open(DL_PRODUCTS_VOT, 'rb') as file:
11461190
zip_bytes = file.read()

0 commit comments

Comments
 (0)