added model tests for users

This commit is contained in:
Alexander Karpov 2023-09-08 14:00:46 +03:00
parent b4ed207fd1
commit 1fc003636b
8 changed files with 50 additions and 8 deletions

1
akarpov/files/consts.py Normal file
View File

@ -0,0 +1 @@
USER_INITIAL_FILE_UPLOAD = 100 * 1024 * 1024

View File

@ -11,20 +11,20 @@
urlpatterns = [ urlpatterns = [
path("", UserListViewSet.as_view(), name="user_list_api"), path("", UserListViewSet.as_view(), name="list"),
path( path(
"self/", "self/",
UserRetireUpdateSelfViewSet.as_view(), UserRetireUpdateSelfViewSet.as_view(),
name="user_get_update_delete_self_api", name="self",
), ),
path( path(
"id/<int:pk>", "id/<int:pk>",
UserRetrieveIdViewSet.as_view(), UserRetrieveIdViewSet.as_view(),
name="user_retrieve_id_api", name="get_by_id",
), ),
path( path(
"<str:username>", "<str:username>",
UserRetrieveViewSet.as_view(), UserRetrieveViewSet.as_view(),
name="user_retrieve_username_api", name="get",
), ),
] ]

View File

@ -10,6 +10,7 @@
from django.db.models.signals import pre_save from django.db.models.signals import pre_save
from django.dispatch import receiver from django.dispatch import receiver
from akarpov.files.consts import USER_INITIAL_FILE_UPLOAD
from akarpov.users.models import User from akarpov.users.models import User
from akarpov.users.services.history import ( from akarpov.users.services.history import (
create_history_note, create_history_note,
@ -21,7 +22,7 @@
def user_create(sender, instance: User, **kwargs): def user_create(sender, instance: User, **kwargs):
if instance.id is None: if instance.id is None:
# give user some space on file share on register # give user some space on file share on register
instance.left_file_upload += 100 * 1024 * 1024 instance.left_file_upload += USER_INITIAL_FILE_UPLOAD
@receiver(user_logged_in) @receiver(user_logged_in)

View File

@ -5,12 +5,14 @@
from factory import Faker, post_generation from factory import Faker, post_generation
from factory.django import DjangoModelFactory from factory.django import DjangoModelFactory
from akarpov.utils.faker import django_image
class UserFactory(DjangoModelFactory): class UserFactory(DjangoModelFactory):
username = Faker("user_name") username = Faker("user_name")
email = Faker("email") email = Faker("email")
name = Faker("name") name = Faker("name")
about = Faker("text")
@post_generation @post_generation
def password(self, create: bool, extracted: Sequence[Any], **kwargs): def password(self, create: bool, extracted: Sequence[Any], **kwargs):
@ -28,6 +30,16 @@ def password(self, create: bool, extracted: Sequence[Any], **kwargs):
) )
self.set_password(password) self.set_password(password)
@post_generation
def image(self, create, extracted, **kwargs):
if extracted:
image_name, image = extracted
else:
image_name = "test.jpg"
image = django_image(image_name, **kwargs)
self.image.save(image_name, image)
class Meta: class Meta:
model = get_user_model() model = get_user_model()
skip_postgeneration_save = False
django_get_or_create = ["username"] django_get_or_create = ["username"]

View File

View File

@ -1,3 +1,4 @@
from akarpov.files.consts import USER_INITIAL_FILE_UPLOAD
from akarpov.users.models import User from akarpov.users.models import User
@ -5,3 +6,12 @@ def test_user_create(user: User):
password = "123" password = "123"
user.set_password(password) user.set_password(password)
assert user.check_password(password) assert user.check_password(password)
def test_auto_file_upload_size(user: User):
size = USER_INITIAL_FILE_UPLOAD
assert user.left_file_upload == size
def test_user_image_create(user: User):
assert user.image

View File

@ -1,6 +1,9 @@
from decimal import Decimal as D from decimal import Decimal as D
from io import BytesIO
from django.core.files.uploadedfile import InMemoryUploadedFile
from faker.providers.python import Provider as PythonProvider from faker.providers.python import Provider as PythonProvider
from PIL import Image
class MoneyProvider(PythonProvider): class MoneyProvider(PythonProvider):
@ -20,3 +23,20 @@ def configure_factory_faker(factory_faker):
def configure_faker(faker): def configure_faker(faker):
for provider in additional_providers: for provider in additional_providers:
faker.add_provider(provider) faker.add_provider(provider)
def django_image(name, size=200, color="red"):
thumb = Image.new(
"RGB",
(
size,
size,
),
color,
)
thumb_io = BytesIO()
thumb.save(thumb_io, format="JPEG")
thumb_io.seek(0)
return InMemoryUploadedFile(
thumb_io, None, name, "image/jpeg", thumb_io.getbuffer().nbytes, None
)

View File

@ -32,8 +32,6 @@
SITE_ID = 1 SITE_ID = 1
# https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n # https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n
USE_I18N = True USE_I18N = True
# https://docs.djangoproject.com/en/dev/ref/settings/#use-l10n
USE_L10N = True
# https://docs.djangoproject.com/en/dev/ref/settings/#use-tz # https://docs.djangoproject.com/en/dev/ref/settings/#use-tz
USE_TZ = True USE_TZ = True
# https://docs.djangoproject.com/en/dev/ref/settings/#locale-paths # https://docs.djangoproject.com/en/dev/ref/settings/#locale-paths