Skip to content

Commit

Permalink
feat(auth): create tests and factories for authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacDMz committed Nov 12, 2023
1 parent 0a9c650 commit 3921322
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
58 changes: 58 additions & 0 deletions backend/authentication/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# authentication/factories.py
import factory
from .models import SupportEntityType, Support, 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')
verification_partner = factory.SubFactory("content.UserFactory")
social_accounts = [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)

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

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

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

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

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

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

# Create your tests here.
@pytest.mark.django_db
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)
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

0 comments on commit 3921322

Please sign in to comment.