Add category-list factory and seed categories in B4W smoke test
This commit is contained in:
+29
-1
@@ -1,4 +1,5 @@
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from faker import Faker
|
||||
@@ -6,10 +7,37 @@ from polyfactory.factories.pydantic_factory import ModelFactory
|
||||
from polyfactory.fields import Use
|
||||
|
||||
from src.models.b4w import B4wExport
|
||||
from src.models.category import Category
|
||||
from src.models.comdirect import ComdirectCreditCard, ComdirectDepot, ComdirectGiro
|
||||
from src.models.transactions import Transaction
|
||||
|
||||
DEFAULT_CATEGORY = "root_category:parent_category:leaf_category"
|
||||
CATEGORY_PATHS = tuple(
|
||||
line.strip()
|
||||
for line in (
|
||||
Path(__file__).resolve().parents[1]
|
||||
/ "scripts/initial_load/categories/categories.txt"
|
||||
).read_text(encoding="utf-8").splitlines()
|
||||
if line.strip()
|
||||
)
|
||||
|
||||
|
||||
def _category_from_path(path: str) -> Category:
|
||||
category = Category.from_str(path)
|
||||
if category is None:
|
||||
raise ValueError("Category list contains an empty path")
|
||||
return category
|
||||
|
||||
|
||||
class CategoryFactory:
|
||||
__faker__ = Faker("de_DE")
|
||||
|
||||
@classmethod
|
||||
def build(cls) -> Category:
|
||||
return _category_from_path(cls.__faker__.random_element(CATEGORY_PATHS))
|
||||
|
||||
@staticmethod
|
||||
def build_all() -> list[Category]:
|
||||
return [_category_from_path(path) for path in CATEGORY_PATHS]
|
||||
|
||||
|
||||
def _faker_or_none(faker: Faker, non_default: Any, default: Any = None, p_value=0.5):
|
||||
|
||||
Reference in New Issue
Block a user