Refactor test cases for improved readability and consistency
- Updated test functions in various test files to enhance code clarity by formatting long lines and improving indentation. - Adjusted assertions to use multi-line formatting for better readability. - Added new test cases for theme settings API to ensure proper functionality. - Ensured consistent use of line breaks and spacing across test files for uniformity.
This commit is contained in:
@@ -27,7 +27,9 @@ def _percentile(values: List[float], percentile: float) -> float:
|
||||
return sorted_values[lower] * (1 - weight) + sorted_values[upper] * weight
|
||||
|
||||
|
||||
def generate_report(simulation_results: List[Dict[str, float]]) -> Dict[str, Union[float, int]]:
|
||||
def generate_report(
|
||||
simulation_results: List[Dict[str, float]],
|
||||
) -> Dict[str, Union[float, int]]:
|
||||
"""Aggregate basic statistics for simulation outputs."""
|
||||
|
||||
values = _extract_results(simulation_results)
|
||||
@@ -63,7 +65,7 @@ def generate_report(simulation_results: List[Dict[str, float]]) -> Dict[str, Uni
|
||||
|
||||
std_dev = pstdev(values) if len(values) > 1 else 0.0
|
||||
summary["std_dev"] = std_dev
|
||||
summary["variance"] = std_dev ** 2
|
||||
summary["variance"] = std_dev**2
|
||||
|
||||
var_95 = summary["percentile_5"]
|
||||
summary["value_at_risk_95"] = var_95
|
||||
|
||||
32
services/security.py
Normal file
32
services/security.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any, Union
|
||||
|
||||
from jose import jwt
|
||||
from passlib.context import CryptContext
|
||||
|
||||
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
||||
SECRET_KEY = "your-secret-key" # Change this in production
|
||||
ALGORITHM = "HS256"
|
||||
|
||||
pwd_context = CryptContext(schemes=["pbkdf2_sha256"], deprecated="auto")
|
||||
|
||||
|
||||
def create_access_token(
|
||||
subject: Union[str, Any], expires_delta: Union[timedelta, None] = None
|
||||
) -> str:
|
||||
if expires_delta:
|
||||
expire = datetime.utcnow() + expires_delta
|
||||
else:
|
||||
expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||
to_encode = {"exp": expire, "sub": str(subject)}
|
||||
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
||||
return encoded_jwt
|
||||
|
||||
|
||||
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||
return pwd_context.verify(plain_password, hashed_password)
|
||||
|
||||
|
||||
def get_password_hash(password: str) -> str:
|
||||
return pwd_context.hash(password)
|
||||
@@ -7,6 +7,7 @@ from typing import Dict, Mapping
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from models.application_setting import ApplicationSetting
|
||||
from models.theme_setting import ThemeSetting # Import ThemeSetting model
|
||||
|
||||
CSS_COLOR_CATEGORY = "theme"
|
||||
CSS_COLOR_VALUE_TYPE = "color"
|
||||
@@ -92,7 +93,9 @@ def get_css_color_settings(db: Session) -> Dict[str, str]:
|
||||
return values
|
||||
|
||||
|
||||
def update_css_color_settings(db: Session, updates: Mapping[str, str]) -> Dict[str, str]:
|
||||
def update_css_color_settings(
|
||||
db: Session, updates: Mapping[str, str]
|
||||
) -> Dict[str, str]:
|
||||
"""Persist provided CSS color overrides and return the final values."""
|
||||
|
||||
if not updates:
|
||||
@@ -176,8 +179,10 @@ def _validate_functional_color(value: str) -> None:
|
||||
|
||||
def _ensure_component_count(value: str, expected: int) -> None:
|
||||
if not value.endswith(")"):
|
||||
raise ValueError("Color function expressions must end with a closing parenthesis")
|
||||
inner = value[value.index("(") + 1 : -1]
|
||||
raise ValueError(
|
||||
"Color function expressions must end with a closing parenthesis"
|
||||
)
|
||||
inner = value[value.index("(") + 1: -1]
|
||||
parts = [segment.strip() for segment in inner.split(",")]
|
||||
if len(parts) != expected:
|
||||
raise ValueError(
|
||||
@@ -206,3 +211,20 @@ def list_css_env_override_rows(
|
||||
}
|
||||
)
|
||||
return rows
|
||||
|
||||
|
||||
def save_theme_settings(db: Session, theme_data: dict):
|
||||
theme = db.query(ThemeSetting).first() or ThemeSetting()
|
||||
for key, value in theme_data.items():
|
||||
setattr(theme, key, value)
|
||||
db.add(theme)
|
||||
db.commit()
|
||||
db.refresh(theme)
|
||||
return theme
|
||||
|
||||
|
||||
def get_theme_settings(db: Session):
|
||||
theme = db.query(ThemeSetting).first()
|
||||
if theme:
|
||||
return {c.name: getattr(theme, c.name) for c in theme.__table__.columns}
|
||||
return {}
|
||||
|
||||
@@ -25,12 +25,13 @@ def _ensure_positive_span(span: float, fallback: float) -> float:
|
||||
return span if span and span > 0 else fallback
|
||||
|
||||
|
||||
def _compile_parameters(parameters: Sequence[Dict[str, float]]) -> List[SimulationParameter]:
|
||||
def _compile_parameters(
|
||||
parameters: Sequence[Dict[str, float]],
|
||||
) -> List[SimulationParameter]:
|
||||
compiled: List[SimulationParameter] = []
|
||||
for index, item in enumerate(parameters):
|
||||
if "value" not in item:
|
||||
raise ValueError(
|
||||
f"Parameter at index {index} must include 'value'")
|
||||
raise ValueError(f"Parameter at index {index} must include 'value'")
|
||||
name = str(item.get("name", f"param_{index}"))
|
||||
base_value = float(item["value"])
|
||||
distribution = str(item.get("distribution", "normal")).lower()
|
||||
@@ -43,8 +44,11 @@ def _compile_parameters(parameters: Sequence[Dict[str, float]]) -> List[Simulati
|
||||
|
||||
if distribution == "normal":
|
||||
std_dev = item.get("std_dev")
|
||||
std_dev_value = float(std_dev) if std_dev is not None else abs(
|
||||
base_value) * DEFAULT_STD_DEV_RATIO or 1.0
|
||||
std_dev_value = (
|
||||
float(std_dev)
|
||||
if std_dev is not None
|
||||
else abs(base_value) * DEFAULT_STD_DEV_RATIO or 1.0
|
||||
)
|
||||
compiled.append(
|
||||
SimulationParameter(
|
||||
name=name,
|
||||
|
||||
Reference in New Issue
Block a user