mirror of
https://github.com/lemeow125/DRF_Template.git
synced 2025-09-18 13:39:56 +08:00
Switch to faker for test data
This commit is contained in:
parent
1e88f1ba53
commit
7e9501e75f
13 changed files with 190 additions and 126 deletions
|
@ -1,38 +1,45 @@
|
|||
import pytest
|
||||
import users
|
||||
|
||||
from accounts.models import CustomUser
|
||||
from users import generate_test_users, remove_test_users
|
||||
|
||||
|
||||
def assert_users_created():
|
||||
data = users.get_users_json()
|
||||
def assert_users_exist(USERS: list[CustomUser] = []):
|
||||
"""
|
||||
Asserts that each user in the provided list exists in the database.
|
||||
|
||||
for user in data["users"]:
|
||||
USER = CustomUser.objects.filter(username=user["username"]).first()
|
||||
Args:
|
||||
USERS (list[CustomUser], optional): A list of CustomUser instances to check for existence. Defaults to an empty list.
|
||||
|
||||
# Assert user exists
|
||||
assert USER
|
||||
Raises:
|
||||
AssertionError: If any user in the list does not exist in the database.
|
||||
"""
|
||||
|
||||
if user["is_superuser"]:
|
||||
# Assert is superuser
|
||||
assert USER.is_superuser
|
||||
for USER in USERS:
|
||||
assert CustomUser.objects.filter(username=USER.username).first()
|
||||
|
||||
|
||||
def assert_users_removed():
|
||||
data = users.get_users_json()
|
||||
for user in data["users"]:
|
||||
USER = CustomUser.objects.filter(username=user["username"]).first()
|
||||
def assert_users_removed(USERS: list[CustomUser] = []):
|
||||
"""
|
||||
Asserts that the specified users have been removed from the database.
|
||||
|
||||
# Assert user does not exist
|
||||
assert not USER
|
||||
Args:
|
||||
USERS (list[CustomUser], optional): A list of user objects (dictionaries) containing at least the 'username' key.
|
||||
Defaults to an empty list.
|
||||
|
||||
Raises:
|
||||
AssertionError: If any user in the USERS list still exists in the database.
|
||||
"""
|
||||
for USER in USERS:
|
||||
assert not CustomUser.objects.filter(username=USER.username).first()
|
||||
|
||||
|
||||
@pytest.mark.django_db(transaction=True)
|
||||
def test_user_creation_deletion():
|
||||
"""
|
||||
Test user creation and deletion
|
||||
Test multiple instances of user creations and deletions
|
||||
"""
|
||||
users.generate_test_users()
|
||||
assert_users_created()
|
||||
users.remove_test_users()
|
||||
assert_users_removed()
|
||||
USERS = generate_test_users()
|
||||
assert_users_exist(USERS)
|
||||
remove_test_users(USERS)
|
||||
assert_users_removed(USERS)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue