Skip to content

Commit 1ed02cc

Browse files
author
mlia
committed
add test functions
1 parent e073161 commit 1ed02cc

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

test/test_extract_aemet.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,21 @@ async def test_save_aemet_minio(mock_debug, mock_check_s3_file_exists, mock_uplo
8686
# Ejecutar la función
8787
await save_aemet(mock_settings_minio, data)
8888

89+
europe_timezone = pytz.timezone("Europe/Madrid")
90+
current_datetime = datetime.datetime.now(europe_timezone).replace(second=0)
91+
formatted_date_day = current_datetime.strftime(
92+
"%Y%m%d"
93+
) # formatted date year|month|day all together
94+
formatted_date_slash = current_datetime.strftime(
95+
"%Y/%m/%d"
96+
)
8997
# Verificar que check_s3_file_exists fue llamado con los parámetros correctos
9098
mock_check_s3_file_exists.assert_called_once_with(
9199
endpoint_url=mock_settings_minio.storage.config.minio.endpoint,
92100
aws_secret_access_key=mock_settings_minio.storage.config.minio.secret_key,
93101
aws_access_key_id=mock_settings_minio.storage.config.minio.access_key,
94102
bucket_name=mock_settings_minio.storage.config.minio.bucket,
95-
object_name='raw/aemet/2024/10/09/aemet_20241009.json' # Aquí se puede especificar el objeto esperado, si es necesario
103+
object_name=f"raw/aemet/{formatted_date_slash}/aemet_{formatted_date_day}.json" # Aquí se puede especificar el objeto esperado, si es necesario
96104
)
97105

98106
# Verificar que se llama a upload_objs
@@ -109,11 +117,19 @@ async def test_save_aemet_local(mock_debug, mock_check_local_file_exists, mock_u
109117

110118
# Ejecutar la función
111119
await save_aemet(mock_settings_local, data)
120+
europe_timezone = pytz.timezone("Europe/Madrid")
121+
current_datetime = datetime.datetime.now(europe_timezone).replace(second=0)
122+
formatted_date_day = current_datetime.strftime(
123+
"%Y%m%d"
124+
) # formatted date year|month|day all together
125+
formatted_date_slash = current_datetime.strftime(
126+
"%Y/%m/%d"
127+
)
112128

113129
# Verificar que check_local_file_exists fue llamado con los parámetros correctos
114130
mock_check_local_file_exists.assert_called_once_with(
115-
Path("/tmp/raw/aemet/2024/10/09"),
116-
"aemet_20241009.json" # Aquí se puede especificar el objeto esperado, si es necesario
131+
Path(f"/tmp/raw/aemet/{formatted_date_slash}"),
132+
f"aemet_{formatted_date_day}.json" # Aquí se puede especificar el objeto esperado, si es necesario
117133
)
118134

119135
# Verificar que no se llama a upload_objs, ya que se guarda localmente

test/test_extract_informo.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from pathlib import Path
77
from loguru import logger
88
import json
9+
import pytz
10+
import datetime
911

1012

1113
###################### get_informo
@@ -151,13 +153,24 @@ async def test_save_informo_minio(mock_upload_objs, mock_check_s3_file_exists, m
151153
# Llamar a la función
152154
await save_informo(mock_settings_minio, mock_data)
153155

156+
date_from_file = mock_data["pms"]["fecha_hora"]
157+
dt = datetime.datetime.strptime(date_from_file, "%d/%m/%Y %H:%M:%S")
158+
formated_date = dt.strftime("%Y-%m-%dT%H%M")
159+
# Get the timezone from Madrid and formated the dates for the object_name of the files
160+
europe_timezone = pytz.timezone("Europe/Madrid")
161+
current_datetime = datetime.datetime.now(europe_timezone).replace(second=0)
162+
print(current_datetime)
163+
formatted_date_slash = current_datetime.strftime(
164+
"%Y/%m/%d"
165+
)
166+
154167
# Verificar que se llamó a check_s3_file_exists con los argumentos correctos
155168
mock_check_s3_file_exists.assert_called_once_with(
156169
endpoint_url=mock_settings_minio.storage.config.minio.endpoint,
157170
aws_secret_access_key=mock_settings_minio.storage.config.minio.secret_key,
158171
aws_access_key_id=mock_settings_minio.storage.config.minio.access_key,
159172
bucket_name=mock_settings_minio.storage.config.minio.bucket,
160-
object_name="raw/informo/2024/10/09/informo_2024-10-09T1430.json"
173+
object_name=f"raw/informo/{formatted_date_slash}/informo_{formated_date}.json"
161174
)
162175

163176
# Verificar que se llamó a upload_objs para subir el archivo
@@ -175,15 +188,25 @@ async def test_save_informo_local(mock_open_func, mock_check_local_file_exists,
175188

176189
# Llamar a la función
177190
await save_informo(mock_settings_local, mock_data)
191+
date_from_file = mock_data["pms"]["fecha_hora"]
192+
dt = datetime.datetime.strptime(date_from_file, "%d/%m/%Y %H:%M:%S")
193+
formated_date = dt.strftime("%Y-%m-%dT%H%M")
194+
# Get the timezone from Madrid and formated the dates for the object_name of the files
195+
europe_timezone = pytz.timezone("Europe/Madrid")
196+
current_datetime = datetime.datetime.now(europe_timezone).replace(second=0)
197+
print(current_datetime)
198+
formatted_date_slash = current_datetime.strftime(
199+
"%Y/%m/%d"
200+
)
178201

179202
# Verificar que se llamó a check_local_file_exists con los argumentos correctos
180203
mock_check_local_file_exists.assert_called_once_with(
181-
Path("/tmp/raw/informo/2024/10/09"),
182-
"informo_2024-10-09T1430.json"
204+
Path(f"/tmp/raw/informo/{formatted_date_slash}"),
205+
f"informo_{formated_date}.json"
183206
)
184207

185208
# Verificar que se abrió el archivo correctamente para escribir los datos
186-
mock_open_func.assert_called_once_with(Path("/tmp/raw/informo/2024/10/09") / "informo_2024-10-09T1430.json", "w")
209+
mock_open_func.assert_called_once_with(Path(f"/tmp/raw/informo/{formatted_date_slash}") / f"informo_{formated_date}.json", "w")
187210

188211
# Verificar que se escribió el contenido JSON en el archivo
189212
mock_open_func().write.assert_called_once_with(json.dumps(mock_data))

0 commit comments

Comments
 (0)