Skip to content

Commit

Permalink
adding test to Escribir_Datos_Osiris(), adding env variable to Docker…
Browse files Browse the repository at this point in the history
…file and changing time for datetime
  • Loading branch information
natilou committed Apr 11, 2023
1 parent bb1c244 commit 65ea761
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ COPY Pipfile Pipfile.lock ./

RUN pipenv install --system --deploy --ignore-pipfile

ENV file_directory="Subida Osiris"

COPY . .

CMD ["python3", "src/subida.py"]
13 changes: 6 additions & 7 deletions src/subida.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import shutil
import pandas as pd
import numpy as np
import datetime
import time
from datetime import datetime
import traceback

from driver_email import enviar_mail_con_adjuntos
Expand Down Expand Up @@ -113,14 +112,14 @@ def Preparacion_Cuentas():
df_sub = df_sub.drop('riesgo', inplace=False, axis=1)
try:
df_sub.to_csv(
f'Subida Osiris/{time.strftime("(%H.%M hs) -")} subida_cartera_{name}.csv',
f'Subida Osiris/{datetime.now().strftime("(%H.%M hs) -")} subida_cartera_{name}.csv',
sep=';',
encoding='latin_1',
index=False
)
except Exception:
df_sub.to_csv(
f'Subida Osiris/{time.strftime("(%H.%M hs) -")} subida_cartera_{name}.csv',
f'Subida Osiris/{datetime.now().strftime("(%H.%M hs) -")} subida_cartera_{name}.csv',
sep=';',
encoding='ANSI',
index=False
Expand Down Expand Up @@ -185,7 +184,7 @@ def Preparacion_Cuentas_Comafi():
df_os['IDSucursal(17)'] = '1'
df_os['subcliente'] = df['subcliente']

name_folder = f'Subida Osiris/{time.strftime("(%H.%M hs) -")} {nombre_cartera}'
name_folder = f'Subida Osiris/{datetime.now().strftime("(%H.%M hs) -")} {nombre_cartera}'
if os.path.isdir(name_folder):
shutil.rmtree(name_folder)
os.mkdir(name_folder)
Expand Down Expand Up @@ -332,14 +331,14 @@ def Preparacion_Datos_Comafi():
print('Guardando planilla subida...')
try:
df_subida.to_csv(
f'Subida Osiris/{time.strftime("(%H.%M hs) -")}DATOS_EMERIX_subida_telefono.csv',
f'Subida Osiris/{datetime.now().strftime("(%H.%M hs) -")}DATOS_EMERIX_subida_telefono.csv',
sep=';',
index=False,
encoding='ANSI'
)
except Exception:
df_subida.to_csv(
f'Subida Osiris/{time.strftime("(%H.%M hs) -")}DATOS_EMERIX_subida_telefono.csv',
f'Subida Osiris/{datetime.now().strftime("(%H.%M hs) -")}DATOS_EMERIX_subida_telefono.csv',
sep=';',
index=False,
encoding='ANSI'
Expand Down
10 changes: 6 additions & 4 deletions src/write_data_osiris.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@

from datetime import datetime
import os
import pandas as pd
import time
from constants.constants import DATA_UPLOADER_HEADER


def Escribir_Datos_Osiris(df, filename, cols_df, cols_osiris):
def Escribir_Datos_Osiris(df: pd.DataFrame, filename: str, cols_df: list[str], cols_osiris: list[str]):

Control_Carpeta_Subida()

df_subida = pd.DataFrame(columns=DATA_UPLOADER_HEADER)
df_subida[cols_osiris] = df[cols_df]
try:
df_subida.to_csv(
f'Subida Osiris/{time.strftime("(%H.%M hs) -")} {filename}',
f'{os.getenv("file_directory", "Subida Osiris")}/{datetime.now().strftime("(%H.%M hs) -")} {filename}',
sep=';',
index=False,
encoding='latin_1'
)

except Exception:
df_subida.to_csv(
f'Subida Osiris/{time.strftime("(%H.%M hs) -")} {filename}',
f'{os.getenv("file_directory", "Subida Osiris")}/{datetime.now().strftime("(%H.%M hs) -")} {filename}',
sep=';',
index=False,
encoding='ANSI'
Expand Down
95 changes: 95 additions & 0 deletions tests/test_write_data_osiris.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from freezegun import freeze_time
import pandas as pd
import pytest
import tempfile
from unittest import mock
from src.write_data_osiris import Escribir_Datos_Osiris


@pytest.mark.parametrize("mock_df, mock_filename, mock_cols_df, mock_cols_osiris", [
(
pd.DataFrame(
{
"Cuenta": "3333",
"TEL": '123455678',
"OBS": "RIESGO 1",
"ID_FONO": "8",
},
index=[0]
),
'RIESGO_telefonos.csv',
['Cuenta', 'ID_FONO', 'TEL', 'OBS'],
[
"ID Cuenta o Nro. de Asig. (0)",
"ID Tipo de Teléfono (17)",
"Nro. de Teléfono (18)",
"Obs. de Teléfono (19)"
]
)
]
)
class TestWriteDataOsiris:

@mock.patch('src.write_data_osiris.os.getenv')
def test_create_file_in_existing_folder_get_by_env_variable(
self,
mock_get_env,
mock_df,
mock_filename,
mock_cols_df,
mock_cols_osiris,
):

with tempfile.TemporaryDirectory() as tmpdirname:
mock_get_env.return_value = tmpdirname
Escribir_Datos_Osiris(
df=mock_df,
filename=mock_filename,
cols_df=mock_cols_df,
cols_osiris=mock_cols_osiris,
)

@freeze_time("2023-04-08 18:45:00")
@mock.patch.object(pd.DataFrame, 'to_csv')
def test_create_file_in_existing_folder_without_env_variable(
self,
mock_to_csv,
mock_df,
mock_filename,
mock_cols_df,
mock_cols_osiris,
):

Escribir_Datos_Osiris(
df=mock_df,
filename=mock_filename,
cols_df=mock_cols_df,
cols_osiris=mock_cols_osiris,
)

mock_to_csv.assert_called_once()
mock_to_csv.assert_called_once_with(
f'{"Subida Osiris"}/(18.45 hs) - {mock_filename}',
sep=';',
index=False,
encoding='latin_1'
)

@mock.patch('src.write_data_osiris.os.getenv')
def test_raise_exception_folder_doesnt_exist(
self,
mock_get_env,
mock_df,
mock_filename,
mock_cols_df,
mock_cols_osiris,
):

with pytest.raises(Exception):
mock_get_env.return_value = "Another folder"
Escribir_Datos_Osiris(
df=mock_df,
filename=mock_filename,
cols_df=mock_cols_df,
cols_osiris=mock_cols_osiris,
)

0 comments on commit 65ea761

Please sign in to comment.