v1.4 - Login fixes, etc
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
from datetime import date
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy import create_engine, inspect, text
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
from app.core.config import settings
|
||||
from app.db.migrations import bootstrap_schema, sync_tenant_ids
|
||||
from app.db.session import Base
|
||||
from app.main import app
|
||||
from app.models.assumption import FreightCostRule, PackagingCostRule, ProcessCostRule
|
||||
@@ -77,6 +79,8 @@ def test_mix_and_product_cost_breakdown():
|
||||
|
||||
assert mix_result["total_mix_kg"] == 280
|
||||
assert mix_result["mix_cost_per_kg"] == 0.5114
|
||||
assert product_result["client_name"] == "Specialty Feeds"
|
||||
assert product_result["mix_name"] == "Pigeon Mix"
|
||||
assert product_result["finished_product_delivered"] == 14.208
|
||||
assert product_result["distributor_price"] == 18.3329
|
||||
assert product_result["wholesale_price"] == 17.3268
|
||||
@@ -181,3 +185,74 @@ def test_client_access_endpoints():
|
||||
export_response = client.get("/api/powerbi/client-access", headers=headers)
|
||||
assert export_response.status_code == 200
|
||||
assert "client_rows" in export_response.json()
|
||||
|
||||
|
||||
def test_bootstrap_schema_creates_missing_tables_and_patches_legacy_tenant_columns():
|
||||
engine = create_engine(
|
||||
"sqlite://",
|
||||
connect_args={"check_same_thread": False},
|
||||
poolclass=StaticPool,
|
||||
)
|
||||
|
||||
with engine.begin() as connection:
|
||||
connection.execute(
|
||||
text(
|
||||
"""
|
||||
CREATE TABLE client_accounts (
|
||||
id INTEGER PRIMARY KEY,
|
||||
tenant_id VARCHAR(64),
|
||||
name VARCHAR(255),
|
||||
client_code VARCHAR(64),
|
||||
status VARCHAR(32),
|
||||
powerbi_workspace VARCHAR(128),
|
||||
notes TEXT,
|
||||
created_at DATETIME
|
||||
)
|
||||
"""
|
||||
)
|
||||
)
|
||||
connection.execute(
|
||||
text(
|
||||
"""
|
||||
CREATE TABLE raw_materials (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name VARCHAR(255),
|
||||
supplier VARCHAR(255),
|
||||
unit_of_measure VARCHAR(64),
|
||||
kg_per_unit FLOAT,
|
||||
status VARCHAR(32),
|
||||
notes TEXT,
|
||||
created_at DATETIME
|
||||
)
|
||||
"""
|
||||
)
|
||||
)
|
||||
connection.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO client_accounts (id, tenant_id, name, client_code, status)
|
||||
VALUES (1, 'specialty-feeds', 'Specialty Feeds', 'SPEC', 'active')
|
||||
"""
|
||||
)
|
||||
)
|
||||
connection.execute(
|
||||
text(
|
||||
"""
|
||||
INSERT INTO raw_materials (id, name, supplier, unit_of_measure, kg_per_unit, status)
|
||||
VALUES (1, 'Maize', 'Example Supplier', 'tonne', 1000, 'active')
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
report = bootstrap_schema(engine, Base.metadata)
|
||||
synced_rows = sync_tenant_ids(engine)
|
||||
|
||||
assert "products" in report.created_tables
|
||||
assert "raw_materials.tenant_id" in report.added_columns
|
||||
assert "tenant_id" in {column["name"] for column in inspect(engine).get_columns("raw_materials")}
|
||||
assert synced_rows["raw_materials"] == 1
|
||||
|
||||
with engine.begin() as connection:
|
||||
tenant_id = connection.execute(text("SELECT tenant_id FROM raw_materials WHERE id = 1")).scalar_one()
|
||||
|
||||
assert tenant_id == "specialty-feeds"
|
||||
|
||||
Reference in New Issue
Block a user