34 lines
743 B
Python
34 lines
743 B
Python
import random
|
|
import string
|
|
|
|
|
|
class Factory:
|
|
model = None
|
|
|
|
def definition(self) -> dict:
|
|
raise NotImplementedError
|
|
|
|
def make(self, **overrides) -> dict:
|
|
data = self.definition()
|
|
data.update(overrides)
|
|
return {k: v for k, v in data.items() if v is not None}
|
|
|
|
def create(self, db, **overrides):
|
|
data = self.make(**overrides)
|
|
obj = self.model(**data)
|
|
db.add(obj)
|
|
db.flush()
|
|
return obj
|
|
|
|
|
|
def random_string(length: int = 10) -> str:
|
|
return "".join(random.choices(string.ascii_lowercase, k=length))
|
|
|
|
|
|
def random_email() -> str:
|
|
return f"{random_string(8)}@example.com"
|
|
|
|
|
|
def random_phone() -> str:
|
|
return f"08{random.randint(100000000, 999999999)}"
|