77 lines
2.7 KiB
Python
77 lines
2.7 KiB
Python
import traceback
|
|
from decimal import Decimal
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
from test_erp_gateway import ENV
|
|
|
|
from production_analytics.erp import ErpFiberBookingGateway, ErpReadError, ErpSettings
|
|
|
|
|
|
@pytest.fixture
|
|
def db(monkeypatch):
|
|
connect = MagicMock()
|
|
monkeypatch.setattr("production_analytics.erp.gateway.pymssql.connect", connect)
|
|
connection = connect.return_value.__enter__.return_value
|
|
cursor = connection.cursor.return_value.__enter__.return_value
|
|
cursor.fetchall.return_value = [("12026000994 ", "41070418 ", "Fiber ", Decimal("6877.80"))]
|
|
return connect, connection, cursor
|
|
|
|
|
|
def gateway():
|
|
return ErpFiberBookingGateway(ErpSettings.from_environment(ENV))
|
|
|
|
|
|
def test_order_query_is_parameterized_positive_and_not_shift_bounded(db):
|
|
connect, connection, cursor = db
|
|
rows = gateway().get_fiber_bookings("12026000994")
|
|
assert len(rows) == 1
|
|
assert rows[0].production_order == "12026000994"
|
|
assert rows[0].material_number == "41070418"
|
|
assert rows[0].material_name == "Fiber"
|
|
assert rows[0].quantity_kg == 6877.8
|
|
sql, parameters = cursor.execute.call_args.args
|
|
assert parameters == ("12026000994",)
|
|
assert "[Fertigungsauftragsnummer] = %s" in sql
|
|
assert "[Fasermenge] > 0" in sql
|
|
assert "NULLIF(LTRIM(RTRIM([Rohstoffnummer])), '') IS NOT NULL" in sql
|
|
assert "Zeitstempel" not in sql
|
|
assert "12026000994" not in sql
|
|
connect.assert_called_once_with(
|
|
server="localhost",
|
|
port=49601,
|
|
database="NV_DWH",
|
|
user=ENV["ERP_DB_USER"],
|
|
password=ENV["ERP_DB_PASSWORD"],
|
|
login_timeout=10,
|
|
timeout=10,
|
|
)
|
|
connection.commit.assert_not_called()
|
|
connection.cursor.return_value.__exit__.assert_called_once()
|
|
connect.return_value.__exit__.assert_called_once()
|
|
|
|
|
|
def test_no_rows_returns_empty(db):
|
|
db[2].fetchall.return_value = []
|
|
assert gateway().get_fiber_bookings("123") == []
|
|
|
|
|
|
@pytest.mark.parametrize("order", [None, "", " ", "\x00"])
|
|
def test_invalid_order_rejected_before_connect(db, order):
|
|
with pytest.raises(ValueError):
|
|
gateway().get_fiber_bookings(order)
|
|
db[0].assert_not_called()
|
|
|
|
|
|
def test_read_failure_does_not_expose_credentials(db):
|
|
db[2].execute.side_effect = RuntimeError(ENV["ERP_DB_PASSWORD"])
|
|
with pytest.raises(ErpReadError, match="read failed") as caught:
|
|
gateway().get_fiber_bookings("123")
|
|
assert ENV["ERP_DB_PASSWORD"] not in "".join(traceback.format_exception(caught.value))
|
|
|
|
|
|
def test_invalid_numeric_row_rejected(db):
|
|
db[2].fetchall.return_value = [("123", "M1", "Fiber", float("nan"))]
|
|
with pytest.raises(ErpReadError, match="invalid row"):
|
|
gateway().get_fiber_bookings("123")
|