Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#540 Feat(Authentication) create tests and factories in authentication #540

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions backend/authentication/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import factory

from .models import Support, SupportEntityType, User, UserResource, UserTask, UserTopic


class SupportEntityTypeFactory(factory.django.DjangoModelFactory):
class Meta:
model = SupportEntityType

name = factory.Faker("word")


class SupportFactory(factory.django.DjangoModelFactory):
class Meta:
model = Support

supporter_type = factory.SubFactory(SupportEntityTypeFactory)
supporter_entity = factory.Faker("random_int", min=1, max=100)
supported_type = factory.SubFactory(SupportEntityTypeFactory)
supported_entity = factory.Faker("random_int", min=1, max=100)


class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = User

user_name = factory.Faker("user_name")
name = factory.Faker("name")
password = factory.Faker("password")
location = factory.Faker("city")
description = factory.Faker("text", max_nb_chars=500)
verified = factory.Faker("boolean")
verification_method = factory.Faker("word")
social_accounts = factory.List([factory.Faker("user_name") for _ in range(3)])
private = factory.Faker("boolean")
high_risk = factory.Faker("boolean")
total_flags = factory.Faker("random_int", min=0, max=100)
creation_date = factory.Faker("date_time_this_decade", before_now=True)
deletion_date = factory.Faker("date_time_this_decade", before_now=False)

# Workaround for the build method
# Does not work with the create method at the moment
# verification_partner field references itself
@factory.post_generation
def verification_partner(self, create, extracted, **kwargs):
if not create:
# Simple build, do nothing.
return
if extracted:
pass


class UserResourceFactory(factory.django.DjangoModelFactory):
class Meta:
model = UserResource

user_id = factory.SubFactory(UserFactory)
resource_id = factory.SubFactory("content.factories.ResourceFactory")


class UserTaskFactory(factory.django.DjangoModelFactory):
class Meta:
model = UserTask

user_id = factory.SubFactory(UserFactory)
task_id = factory.SubFactory("content.factories.TaskFactory")


class UserTopicFactory(factory.django.DjangoModelFactory):
class Meta:
model = UserTopic

user_id = factory.SubFactory(UserFactory)
topic_id = factory.SubFactory("content.factories.TopicFactory")
18 changes: 16 additions & 2 deletions backend/authentication/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
from django.test import TestCase
from .factories import SupportEntityTypeFactory, SupportFactory, UserFactory, UserResourceFactory, UserTaskFactory, UserTopicFactory

# Create your tests here.

def test_str_methods() -> None:
support_entity_type = SupportEntityTypeFactory.build()
support = SupportFactory.build()
user = UserFactory.build()
user_resource = UserResourceFactory.build()
user_task = UserTaskFactory.build()
user_topic = UserTopicFactory.build()

assert str(support_entity_type) == support_entity_type.name
assert str(support) == str(support.id)
assert str(user) == user.username
assert str(user_resource) == str(user_resource.id)
assert str(user_task) == str(user_task.id)
assert str(user_topic) == str(user_topic.id)
1 change: 1 addition & 0 deletions backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
"DEFAULT_THROTTLE_RATES": {"anon": "7/min", "user": "10/min"},
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
'DEFAULT_PAGINATION_ORDERS_OBJECTS': False,
"PAGE_SIZE": 20,
}

Expand Down
62 changes: 62 additions & 0 deletions backend/content/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import datetime
import random

import factory

from .models import Resource, ResourceTopic, Task, Topic


class ResourceFactory(factory.django.DjangoModelFactory):
class Meta:
model = Resource

name = factory.Faker("name")
description = factory.Faker("paragraph")
topics = factory.Faker("words", nb=1)
location = factory.Faker("address")
url = factory.Faker("url")
total_flags = random.randint(1, 100)
private = random.choice([True, False])
creation_date = factory.LazyFunction(datetime.datetime.now)
deletion_date = factory.LazyFunction(datetime.datetime.now)


class TaskFactory(factory.django.DjangoModelFactory):
class Meta:
model = Task

name = factory.Faker("word")
description = factory.Faker("paragraph")
location = factory.Faker("address")
tags = factory.List([factory.Faker("word") for _ in range(10)])
creation_date = factory.LazyFunction(datetime.datetime.now)
deletion_date = factory.LazyFunction(datetime.datetime.now)


class TopicFactory(factory.django.DjangoModelFactory):
class Meta:
model = Topic

name = factory.Faker("word")
active = random.choice([True, False])
description = factory.Faker("paragraph")
creation_date = factory.LazyFunction(datetime.datetime.now)
creation_date = factory.LazyFunction(datetime.datetime.now)
deprecation_date = factory.Faker("date")


class ResourceTopicFactory(factory.django.DjangoModelFactory):
class Meta:
model = ResourceTopic

resource_id = factory.SubFactory(ResourceFactory)
topic_id = factory.SubFactory(TopicFactory)


class TopicFormatFactory(factory.django.DjangoModelFactory):
"""Placeholder
We do not have Factory for events.Format, therefore can not use a Subfactory yet.
format_id = models.ForeignKey("events.Format", on_delete=models.CASCADE)
"""

pass
2 changes: 1 addition & 1 deletion backend/content/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .factory import ResourceFactory, TaskFactory, TopicFactory, ResourceTopicFactory
from .factories import ResourceFactory, TaskFactory, TopicFactory, ResourceTopicFactory
import pytest


Expand Down
3 changes: 2 additions & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ line_length = 88
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "backend.settings"
python_files = "tests.py test_*.py *_tests.py"
addopts = "-vv --nomigrations --cov=. --cov-report=html --cov-report=term"
addopts = "--nomigrations --cov=. --cov-report=html --cov-report=term"


[tool.coverage.run]
omit = [
Expand Down
Loading