Skip to content

Commit

Permalink
Add SQLAlchemy support
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Dec 11, 2023
1 parent 591ac33 commit 35ffc27
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 124 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ test-release:
source $(VENV) && twine upload --repository testpypi dist/*

mypy:
source $(VENV) && mypy .
source $(VENV) && mypy fake.py

%:
@:
16 changes: 13 additions & 3 deletions examples/sqlalchemy/article/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
from fake import (
FACTORY,
FileSystemStorage,
SQLAlchemyModelFactory,
SubFactory,
post_save,
pre_save,
trait,
)
from sqlalchemy_model_factory import SQLAlchemyModelFactory

from article.models import Article, User
from config import SESSION

__author__ = "Artur Barseghyan <artur.barseghyan@gmail.com>"
__copyright__ = "2023 Artur Barseghyan"
Expand All @@ -20,13 +21,16 @@
"UserFactory",
)

# Build paths inside the project like this: BASE_DIR / 'subdir'.
# Storage config. Build paths inside the project like this: BASE_DIR / 'subdir'
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_ROOT = BASE_DIR / "media"

STORAGE = FileSystemStorage(root_path=MEDIA_ROOT, rel_path="tmp")


def get_session():
return SESSION()


class UserFactory(SQLAlchemyModelFactory):
"""User factory."""

Expand All @@ -44,6 +48,9 @@ class Meta:
model = User
get_or_create = ("username",)

class MetaSQLAlchemy:
get_session = get_session

@trait
def is_admin_user(self, instance: User) -> None:
instance.is_superuser = True
Expand Down Expand Up @@ -74,6 +81,9 @@ class ArticleFactory(SQLAlchemyModelFactory):
class Meta:
model = Article

class MetaSQLAlchemy:
get_session = get_session

@pre_save
def _pre_save_method(self, instance):
instance.pre_save_called = True
Expand Down
3 changes: 1 addition & 2 deletions examples/sqlalchemy/article/tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import unittest

from sqlalchemy_model_factory import SESSION

from article.factories import ArticleFactory, UserFactory
from config import SESSION

__author__ = "Artur Barseghyan <artur.barseghyan@gmail.com>"
__copyright__ = "2023 Artur Barseghyan"
Expand Down
16 changes: 16 additions & 0 deletions examples/sqlalchemy/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

__author__ = "Artur Barseghyan <artur.barseghyan@gmail.com>"
__copyright__ = "2023 Artur Barseghyan"
__license__ = "MIT"
__all__ = (
"DATABASE_URL",
"ENGINE",
"SESSION",
)

# SQLAlchemy
DATABASE_URL = "sqlite:///test_database.db"
ENGINE = create_engine(DATABASE_URL)
SESSION = scoped_session(sessionmaker(bind=ENGINE))
12 changes: 6 additions & 6 deletions examples/sqlalchemy/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ def main():
"""Run administrative tasks based on command line arguments."""
sys.path.insert(0, os.path.abspath(os.path.join("..", "..")))
sys.path.insert(0, os.path.abspath("."))

from article.models import Base # noqa
from config import ENGINE # noqa

Base.metadata.create_all(ENGINE)

parser = argparse.ArgumentParser(
description="Management script for the project."
)
parser.add_argument("command", help="The command to run (test or shell)")

args = parser.parse_args()

from sqlalchemy_model_factory import ENGINE

from article.models import Base

Base.metadata.create_all(ENGINE)

if args.command == "test":
run_tests()
elif args.command == "shell":
Expand Down
105 changes: 0 additions & 105 deletions examples/sqlalchemy/sqlalchemy_model_factory.py

This file was deleted.

Loading

0 comments on commit 35ffc27

Please sign in to comment.