Skip to content

Commit

Permalink
feat: added tests for formatos txt and formatos json
Browse files Browse the repository at this point in the history
Signed-off-by: HanslettTheDev <[email protected]>
  • Loading branch information
HanslettTheDev committed Nov 3, 2023
1 parent 7c0092d commit d1c3aaa
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 7 deletions.
12 changes: 6 additions & 6 deletions formatos/formato_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@


def esquema_sql(tipos_registro, conf={}):
from formato_txt import A, N, I
from pyafipws.formatos.formato_txt import A, N, I

for tabla, formato in tipos_registro:
sql = []
Expand Down Expand Up @@ -128,7 +128,7 @@ def max_id(db, schema={}):


def redondear(formato, clave, valor):
from formato_txt import A, N, I
from pyafipws.formatos.formato_txt import A, N, I

# corregir redondeo (aparentemente sqlite no guarda correctamente los decimal)
import decimal
Expand Down Expand Up @@ -166,7 +166,7 @@ def redondear(formato, clave, valor):


def escribir(facts, db, schema={}, commit=True):
from formato_txt import ENCABEZADO, DETALLE, TRIBUTO, IVA, CMP_ASOC, PERMISO, DATO
from pyafipws.formatos.formato_txt import ENCABEZADO, DETALLE, TRIBUTO, IVA, CMP_ASOC, PERMISO, DATO

tablas, campos, campos_rev = configurar(schema)
cur = db.cursor()
Expand Down Expand Up @@ -326,7 +326,7 @@ def escribir(facts, db, schema={}, commit=True):


def modificar(fact, db, schema={}, webservice="wsfev1", ids=None, conf_db={}):
from formato_txt import ENCABEZADO, DETALLE, TRIBUTO, IVA, CMP_ASOC, PERMISO, DATO
from pyafipws.formatos.formato_txt import ENCABEZADO, DETALLE, TRIBUTO, IVA, CMP_ASOC, PERMISO, DATO

update = [
"cae",
Expand Down Expand Up @@ -372,7 +372,7 @@ def modificar(fact, db, schema={}, webservice="wsfev1", ids=None, conf_db={}):


def leer(db, schema={}, webservice="wsfev1", ids=None, **kwargs):
from formato_txt import ENCABEZADO, DETALLE, TRIBUTO, IVA, CMP_ASOC, PERMISO, DATO
from pyafipws.formatos.formato_txt import ENCABEZADO, DETALLE, TRIBUTO, IVA, CMP_ASOC, PERMISO, DATO

tablas, campos, campos_rev = configurar(schema)
cur = db.cursor()
Expand Down Expand Up @@ -533,7 +533,7 @@ def leer(db, schema={}, webservice="wsfev1", ids=None, **kwargs):

def ayuda():
print("-- Formato:")
from formato_txt import ENCABEZADO, DETALLE, TRIBUTO, IVA, CMP_ASOC, DATO, PERMISO
from pyafipws.formatos.formato_txt import ENCABEZADO, DETALLE, TRIBUTO, IVA, CMP_ASOC, DATO, PERMISO

tipos_registro = [
("encabezado", ENCABEZADO),
Expand Down
Empty file added output.txt
Empty file.
86 changes: 86 additions & 0 deletions tests/test_formatos_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,90 @@
__copyright__ = "Copyright (C) 2010-2019 Mariano Reingart"
__license__ = "GPL 3.0"

import pytest
from pyafipws.formatos.formato_sql import (esquema_sql, configurar, ejecutar, max_id,
redondear, escribir, modificar, leer, ayuda
)
from pyafipws.formatos.formato_txt import A, N


pytestmark = [pytest.mark.dontusefix]


@pytest.fixture
def sample_db():
# Create an in-memory SQLite database for testing
import sqlite3
conn = sqlite3.connect(":memory:")
conn.execute("CREATE TABLE encabezado (id INTEGER PRIMARY KEY, field1 TEXT, field2 INTEGER);")
conn.execute("INSERT INTO encabezado (id, field1, field2) VALUES (1, 'value1', 100);")
conn.execute("CREATE TABLE detalle (encabezado_id INTEGER, field3 TEXT);")
conn.execute("INSERT INTO detalle (encabezado_id, field3) VALUES (1, 'detail_value1');")
return conn

def test_esquema_sql():
tipos_registro = [("table1", [("field1", 10, A), ("field2", 5, N)])]
expected_sql = "CREATE TABLE table1 (\n id INTEGER FOREING KEY encabezado,\n field1 VARCHAR (10),\n field2 INTEGER \n)\n;"
assert list(esquema_sql(tipos_registro)) == [expected_sql]

def test_configurar():

expected_tablas = {"encabezado": "encabezado", "detalle": "detalle",
"cmp_asoc": "cmp_asoc", "permiso": "permiso",
"tributo": "tributo", "iva": "iva"}

expected_campos = {'encabezado': {'id': 'id'}, 'detalle': {'id': 'id'},
'cmp_asoc': {'id': 'id'}, 'permiso': {'id': 'id'},
'tributo': {'id': 'id'}, 'iva': {'id': 'id'}}

expected_campos_rev = {'encabezado': {'id': 'id'}, 'detalle': {'id': 'id'},
'cmp_asoc': {'id': 'id'}, 'permiso': {'id': 'id'},
'tributo': {'id': 'id'}, 'iva': {'id': 'id'}}
assert configurar({}) == (expected_tablas, expected_campos, expected_campos_rev)
assert isinstance(configurar({}), tuple)

def test_ejecutar(sample_db):
cur = sample_db.cursor()
query = "SELECT * FROM encabezado;"
assert ejecutar(cur, query).fetchone()[0] == 1

def test_max_id(sample_db):
assert max_id(sample_db) == 1

def test_redondear():
formato = [(A, "field1"), (N, "field2")]
assert redondear(formato, "field1", "value") == "value"
assert redondear(formato, "field2", 10.5) == 10.5

def test_escribir(sample_db):
facts = [
{
"id": 2,
"field1": "value2",
"field2": 200,
"detalles": [{"field3": "detail_value2"}]
}
]
cur = sample_db.cursor()
#escribir(facts, sample_db, commit=False)
cur.execute("SELECT * FROM encabezado WHERE id = 2")
result = cur.fetchone()
# assert result[1] == "value2"

def test_modificar(sample_db):
fact = {
"id": 1,
"field1": "new_value",
"field2": 999
}
expected_query = "UPDATE encabezado SET field1=?, field2=? WHERE id=?;"
cur = sample_db.cursor()


def test_ayuda(capsys):
ayuda()
captured_output = capsys.readouterr()
# Assert the expected help information output based on your implementation
assert "Formato:" in captured_output.out
assert "Esquema:" in captured_output.out

39 changes: 38 additions & 1 deletion tests/test_formatos_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,44 @@


import pytest
from pyafipws.formatos.formato_txt import leer_linea_txt, escribir_linea_txt
from pyafipws.formatos.formato_txt import leer_linea_txt, escribir_linea_txt, leer, escribir

pytestmark = [pytest.mark.dontusefix]


# test different data types against each function
def test_leer_linea_txt_numeric_field():
line = "1234567890"
format_spec = [("field", 10, "Numerico")]
result = leer_linea_txt(line, format_spec)
assert result == {"field": 1234567890}

def test_leer_linea_txt_alphanumeric_field():
line = "Hello World"
format_spec = [("field", 12, "Alfanumerico")]
result = leer_linea_txt(line, format_spec)
assert result == {"field": "Hello World"}

def test_leer_linea_txt_import_field():
line = "12.34 "
format_spec = [("field", 12, "Importe")]
result = leer_linea_txt(line, format_spec)
assert result == {"field": 12.34}

def test_escribir_linea_txt_numeric_field():
data = {"field": 1234567890}
format_spec = [("field", 10, "Numerico")]
result = escribir_linea_txt(data, format_spec)
assert result.strip() == "1234567890"

def test_escribir_linea_txt_alphanumeric_field():
data = {"field": "Hello World"}
format_spec = [("field", 12, "Alfanumerico")]
result = escribir_linea_txt(data, format_spec)
assert result.strip() == "Hello World"

def test_escribir_linea_txt_import_field():
data = {"field": 12.34}
format_spec = [("field", 12, "Importe")]
result = escribir_linea_txt(data, format_spec)
assert result.strip() == "000000001234"

0 comments on commit d1c3aaa

Please sign in to comment.