Skip to content

Commit

Permalink
feat: add data generation for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
SquakR committed Feb 4, 2022
1 parent a2ba2bc commit fa3b08f
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 2 deletions.
2 changes: 1 addition & 1 deletion graphene_django_filter/input_type_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def create_filter_input_type(
)
for root in roots
},
'or': graphene.InputField(lambda: input_type, description='Or field'),
'and': graphene.InputField(lambda: input_type, description='And field'),
'or': graphene.InputField(lambda: input_type, description='Or field'),
},
),
)
Expand Down
127 changes: 126 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ graphene-django = "^2.15.0"
django-filter = "^21.1"
stringcase = "^1.2.0"
anytree = "^2.8.0"
django-seed = "^0.3.1"
psycopg2-binary = "^2.9.3"

[tool.poetry.dev-dependencies]
flake8 = "^4.0.1"
Expand Down
84 changes: 84 additions & 0 deletions tests/data_generation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""Data generation for testing."""

from datetime import datetime
from functools import reduce
from itertools import count, repeat

from django.utils.timezone import make_aware
from django_seed import Seed
from django_seed.seeder import Seeder

from .models import Task, TaskGroup, User


def generate_data() -> None:
"""Generate data for testing."""
seeder: Seeder = Seed.seeder()
generate_users(seeder)
generate_tasks(seeder)
generate_task_groups(seeder)
seeder.execute()
set_task_groups_tasks()


def generate_users(seeder: Seeder) -> None:
"""Generate user data for testing."""
number_generator = iter(count())
seeder.add_entity(User, 5, {'birthday': datetime.strptime('01/01/2000', '%m/%d/%Y')})
seeder.add_entity(User, 10, {'is_active': False})
seeder.add_entity(
User, 15, {
'email': lambda ie: f'john_doe{next(number_generator)}@domain.com',
},
)
seeder.add_entity(User, 20, {'first_name': 'Jane'})
seeder.add_entity(User, 25, {'last_name': 'Dou'})


def generate_tasks(seeder: Seeder) -> None:
"""Generate task data for testing."""
seeder.add_entity(
Task, 15, {
'created_at': make_aware(datetime.strptime('01/01/2019', '%m/%d/%Y')),
'completed_at': make_aware(datetime.strptime('02/01/2019', '%m/%d/%Y')),
},
)
user_id_generator = iter(reduce(lambda acc, i: [*acc, *repeat(i, 5)], range(1, 6), []))
seeder.add_entity(
Task, 15, {
'user_id': lambda ie: next(user_id_generator),
'created_at': make_aware(datetime.strptime('01/01/2020', '%m/%d/%Y')),
'completed_at': make_aware(datetime.strptime('02/01/2020', '%m/%d/%Y')),
},
)
number_generator = iter(count())
seeder.add_entity(
Task, 20, {
'name': lambda ie: f'Important task №{next(number_generator)}',
'created_at': make_aware(datetime.strptime('01/01/2021', '%m/%d/%Y')),
'completed_at': make_aware(datetime.strptime('02/01/2021', '%m/%d/%Y')),
},
)
seeder.add_entity(
Task, 25, {
'description': 'This task in very important',
},
)


def generate_task_groups(seeder: Seeder) -> None:
"""Generate task groups data for testing."""
number_generator = iter(count())
priority_generator = iter(count())
seeder.add_entity(
TaskGroup, 15, {
'name': lambda ie: f'Task group №{next(number_generator)}',
'priority': lambda ie: next(priority_generator),
},
)


def set_task_groups_tasks() -> None:
"""Set tasks for task groups after data generation."""
for i, task_group in enumerate(TaskGroup.objects.all()):
task_group.tasks.set(range(i * 5 + 1, i * 5 + 5 + 1))
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'django.contrib.contenttypes',
'django.contrib.auth',
'django_filters',
'django_seed',
'tests',
)

Expand Down

0 comments on commit fa3b08f

Please sign in to comment.