integration layer; merge work on indicators from forks with existing ingestion pipeline
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
import io
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import create_engine, text
|
||||
|
||||
from webapi.imports.connectors.csv_ import CsvConnector
|
||||
from webapi.imports.connectors.errors import ConnectorError
|
||||
from webapi.imports.connectors.excel import ExcelConnector
|
||||
from webapi.imports.connectors.json_ import JsonConnector
|
||||
from webapi.imports.connectors.sql import SqlConnector
|
||||
|
||||
|
||||
def test_csv_connector_reads_columns_and_rows():
|
||||
raw = b"nombre,edad\nAna,30\nLuis,40\n"
|
||||
connector = CsvConnector(raw)
|
||||
columns = {c.name: c.inferred_type for c in connector.get_columns()}
|
||||
assert columns == {"nombre": "string", "edad": "integer"}
|
||||
preview = connector.preview(limit=1)
|
||||
assert preview.total_rows == 2
|
||||
assert len(preview.sample_rows) == 1
|
||||
# Cells stay text; typing is the import schema's job (see CsvConnector).
|
||||
rows = list(connector.read_rows({"nombre": "pac_nombre", "edad": "pac_edad"}))
|
||||
assert rows == [{"pac_nombre": "Ana", "pac_edad": "30"}, {"pac_nombre": "Luis", "pac_edad": "40"}]
|
||||
|
||||
|
||||
def test_csv_connector_keeps_leading_zeros_and_blank_cells():
|
||||
raw = b"cp,exp\n01000,007\n,H-2\n"
|
||||
connector = CsvConnector(raw)
|
||||
assert {c.name: c.inferred_type for c in connector.get_columns()} == {"cp": "string", "exp": "string"}
|
||||
rows = list(connector.read_rows({"cp": "pac_codigo_postal", "exp": "pac_id_hospital"}))
|
||||
assert rows == [
|
||||
{"pac_codigo_postal": "01000", "pac_id_hospital": "007"},
|
||||
{"pac_codigo_postal": None, "pac_id_hospital": "H-2"},
|
||||
]
|
||||
|
||||
|
||||
def test_csv_connector_single_column_file():
|
||||
connector = CsvConnector(b"id\nH3\nH4\n")
|
||||
assert [c.name for c in connector.get_columns()] == ["id"]
|
||||
assert connector.metadata()["delimiter"] == ","
|
||||
|
||||
|
||||
def test_csv_connector_sniffs_semicolons_and_strips_excel_bom():
|
||||
raw = "\ufeffExpediente;Sexo\nH-1;F\n".encode("utf-8")
|
||||
connector = CsvConnector(raw)
|
||||
assert [c.name for c in connector.get_columns()] == ["Expediente", "Sexo"]
|
||||
assert connector.metadata() == {"delimiter": ";", "encoding": "utf-8-sig"}
|
||||
|
||||
|
||||
def test_csv_connector_falls_back_to_cp1252():
|
||||
raw = "nombre\nPeña\n".encode("cp1252")
|
||||
connector = CsvConnector(raw)
|
||||
assert list(connector.read_rows({"nombre": "n"})) == [{"n": "Peña"}]
|
||||
|
||||
|
||||
def test_csv_connector_bad_bytes_raise_connector_error():
|
||||
with pytest.raises(ConnectorError):
|
||||
CsvConnector(b"\xff\xfe\x00\x01not valid utf-8 csv bytes")
|
||||
|
||||
|
||||
def test_json_connector_reads_list_of_records():
|
||||
raw = b'[{"nombre": "Ana", "edad": 30}, {"nombre": "Luis", "edad": 40}]'
|
||||
connector = JsonConnector(raw)
|
||||
assert {c.name for c in connector.get_columns()} == {"nombre", "edad"}
|
||||
rows = list(connector.read_rows({"nombre": "pac_nombre"}))
|
||||
assert rows == [{"pac_nombre": "Ana"}, {"pac_nombre": "Luis"}]
|
||||
|
||||
|
||||
def test_json_connector_reads_wrapped_data_key():
|
||||
raw = b'{"data": [{"nombre": "Ana"}]}'
|
||||
connector = JsonConnector(raw)
|
||||
assert {c.name for c in connector.get_columns()} == {"nombre"}
|
||||
|
||||
|
||||
def test_json_connector_bad_json_raises_connector_error():
|
||||
with pytest.raises(ConnectorError):
|
||||
JsonConnector(b"{not valid json")
|
||||
|
||||
|
||||
def test_excel_connector_reads_columns_and_rows():
|
||||
pd = pytest.importorskip("pandas")
|
||||
buffer = io.BytesIO()
|
||||
pd.DataFrame({"nombre": ["Ana", "Luis"], "edad": [30, 40]}).to_excel(buffer, index=False, engine="openpyxl")
|
||||
connector = ExcelConnector(buffer.getvalue())
|
||||
assert connector.list_sheets() == ["Sheet1"]
|
||||
assert {c.name for c in connector.get_columns()} == {"nombre", "edad"}
|
||||
rows = list(connector.read_rows({"nombre": "pac_nombre"}))
|
||||
assert rows == [{"pac_nombre": "Ana"}, {"pac_nombre": "Luis"}]
|
||||
|
||||
|
||||
def test_excel_connector_bad_bytes_raise_connector_error():
|
||||
with pytest.raises(ConnectorError):
|
||||
ExcelConnector(b"not an excel file")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sqlite_source_url(tmp_path):
|
||||
db_path = tmp_path / "source.db"
|
||||
url = f"sqlite:///{db_path}"
|
||||
engine = create_engine(url)
|
||||
with engine.begin() as conn:
|
||||
conn.execute(text("CREATE TABLE patients (nombre TEXT, edad INTEGER)"))
|
||||
conn.execute(text("INSERT INTO patients (nombre, edad) VALUES ('Ana', 30), ('Luis', 40)"))
|
||||
return url
|
||||
|
||||
|
||||
def test_sql_connector_introspection_and_rows(sqlite_source_url):
|
||||
connector = SqlConnector(sqlite_source_url)
|
||||
connector.test_connection()
|
||||
assert connector.get_dialect() == "sqlite"
|
||||
assert "patients" in connector.list_tables()
|
||||
columns = {c.name for c in connector.list_columns("patients")}
|
||||
assert columns == {"nombre", "edad"}
|
||||
|
||||
connector.select_table("patients")
|
||||
preview = connector.preview()
|
||||
assert preview.total_rows == 2
|
||||
rows = list(connector.read_rows({"nombre": "pac_nombre", "edad": "pac_edad"}))
|
||||
assert rows == [{"pac_nombre": "Ana", "pac_edad": 30}, {"pac_nombre": "Luis", "pac_edad": 40}]
|
||||
|
||||
|
||||
def test_sql_connector_bad_connection_string_raises_connector_error():
|
||||
# may fail either at construction (unknown dialect) or at connect time;
|
||||
# both must surface as ConnectorError rather than a raw sqlalchemy error
|
||||
with pytest.raises(ConnectorError):
|
||||
connector = SqlConnector("not-a-real://connection-string")
|
||||
connector.test_connection()
|
||||
Reference in New Issue
Block a user