Skip to content

Commit

Permalink
Switch all test to pytest from django default test package
Browse files Browse the repository at this point in the history
  • Loading branch information
riadelimemmedov committed Feb 7, 2024
1 parent 162d5cb commit e5e468a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ jobs:
run: |
python -m poetry install
- name: Set django settings path
run: $env:DJANGO_SETTINGS_MODULE="config.settings"

- name: Run tests
run: poetry run manage.py test apps
run: poetry run pytest .

- name: Run tests with pytest
run: poetry run pytest --cov=./ --cov-report=xml
Expand Down
2 changes: 1 addition & 1 deletion backend/config/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
register(factories.UserFactory)


#! api_client
# !api_client
@pytest.fixture
def api_client():
return APIClient
19 changes: 16 additions & 3 deletions backend/config/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@
User = get_user_model()


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

email = factory.Sequence(lambda n: "normal_user{}@gmail.com".format(n))
password = "foo12345"
is_active = True
is_staff = False
is_superuser = False

@classmethod
def create_user(cls, **kwargs):
kwargs["is_staff"] = False
kwargs["is_superuser"] = False
return super().create(**kwargs)

@classmethod
def create_superuser(cls, **kwargs):
if not kwargs["is_superuser"] or not kwargs["is_staff"]:
raise ValueError("Superuser must have is_superuser=True and is_staff=True.")
else:
kwargs["is_staff"] = True
kwargs["is_superuser"] = True
return super().create(**kwargs)
43 changes: 39 additions & 4 deletions backend/config/tests/users/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,46 @@
pytestmark = pytest.mark.django_db


#! TestUsersManagers
# !TestUsersManagers
class TestUsersManagers:
def test_create_user(self, user_factory):
user = user_factory.create()
assert user.email == "normal_user{}@gmail.com".format(0)
assert user.is_active
user = user_factory.create_user(email="normal_user@gmail.com", password="foo")
assert user.email == "normal_user@gmail.com"
assert user.is_active is True
assert user.is_staff is False
assert user.is_superuser is False
try:
# username is None for the AbstractUser option
# username does not exist for the AbstractBaseUser option
assert user.username is None
except (
AttributeError
): # Raised when an attribute reference (see Attribute references) or assignment fails
pass

def test_create_superuser(self, user_factory):
admin_user = user_factory.create_superuser(
email="super_user@gmail.com",
password="foo",
is_superuser=True,
is_staff=True,
)
assert admin_user.email == "super_user@gmail.com"
assert admin_user.is_active is True
assert admin_user.is_staff is True
assert admin_user.is_superuser is True
try:
# username is None for the AbstractUser option
# username does not exist for the AbstractBaseUser option
assert admin_user.username is None
except (
AttributeError
): # Raised when an attribute reference (see Attribute references) or assignment fails
pass
with pytest.raises(ValueError):
user_factory.create_superuser(
email="super_user@gmail.com",
password="foo",
is_superuser=False,
is_staff=False,
)

0 comments on commit e5e468a

Please sign in to comment.