-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
afe9566
commit 789f6f3
Showing
5 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import sys | ||
from pathlib import Path | ||
|
||
import alembic | ||
import alembic.command | ||
from alembic.config import Config | ||
from rich import print | ||
from sqlalchemy import text | ||
|
||
sys.path.append(str(Path(__file__).resolve().parents[2])) | ||
|
||
import factory | ||
import factory.fuzzy | ||
|
||
from fast_zero.db.connection import get_session | ||
from fast_zero.db.models import Todo, TodoState, User | ||
from fast_zero.helpers.security import get_password_hash | ||
|
||
|
||
class UserFactory(factory.Factory): | ||
class Meta: | ||
model = User | ||
|
||
username = factory.Sequence(lambda n: f'test{n}') | ||
email = factory.LazyAttribute(lambda obj: f'{obj.username}@test.com') | ||
password = factory.LazyAttribute( | ||
lambda obj: get_password_hash(f'{obj.username}+password') | ||
) | ||
|
||
|
||
class TodoFactory(factory.Factory): | ||
class Meta: | ||
model = Todo | ||
|
||
title = factory.Faker('text') | ||
description = factory.Faker('text', max_nb_chars=50) | ||
state = factory.fuzzy.FuzzyChoice(TodoState) | ||
user_id = factory.fuzzy.FuzzyInteger(1, 5) | ||
|
||
|
||
alembic_config = Config(Path(__file__).parent.parent.parent / 'alembic.ini') | ||
session = next(get_session()) | ||
|
||
# Delete all tables | ||
print('[bold yellow]Dropping database...[/]') | ||
alembic.command.downgrade(alembic_config, 'base') | ||
session.execute(text('DROP TYPE IF EXISTS todostate')) | ||
session.commit() | ||
print('[bold green]Database dropped![/]') | ||
|
||
# Create all tables | ||
print('[bold yellow]Creating database...[/]') | ||
alembic.command.upgrade(alembic_config, 'head') | ||
print('[bold green]Database created![/]') | ||
|
||
users = UserFactory.create_batch(5) | ||
todos = TodoFactory.create_batch(10) | ||
|
||
users.insert( | ||
0, | ||
User( | ||
username='admin', | ||
password=get_password_hash('admin'), | ||
email='[email protected]', | ||
), | ||
) | ||
|
||
print('[bold yellow]Seeding database...[/]') | ||
session.add_all(users) | ||
session.commit() | ||
session.add_all(todos) | ||
session.commit() | ||
print('[bold green]Database seeded![/]') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.