From 1fc003636b59052b14f106b413efeadf6c7a1333 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Fri, 8 Sep 2023 14:00:46 +0300 Subject: [PATCH] added model tests for users --- akarpov/files/consts.py | 1 + akarpov/users/api/urls.py | 8 ++++---- akarpov/users/signals.py | 3 ++- akarpov/users/tests/factories.py | 14 +++++++++++++- akarpov/users/tests/test_api_views.py | 0 akarpov/users/tests/test_models.py | 10 ++++++++++ akarpov/utils/faker.py | 20 ++++++++++++++++++++ config/settings/base.py | 2 -- 8 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 akarpov/files/consts.py create mode 100644 akarpov/users/tests/test_api_views.py diff --git a/akarpov/files/consts.py b/akarpov/files/consts.py new file mode 100644 index 0000000..16ff28f --- /dev/null +++ b/akarpov/files/consts.py @@ -0,0 +1 @@ +USER_INITIAL_FILE_UPLOAD = 100 * 1024 * 1024 diff --git a/akarpov/users/api/urls.py b/akarpov/users/api/urls.py index f223d39..fdde70f 100644 --- a/akarpov/users/api/urls.py +++ b/akarpov/users/api/urls.py @@ -11,20 +11,20 @@ urlpatterns = [ - path("", UserListViewSet.as_view(), name="user_list_api"), + path("", UserListViewSet.as_view(), name="list"), path( "self/", UserRetireUpdateSelfViewSet.as_view(), - name="user_get_update_delete_self_api", + name="self", ), path( "id/", UserRetrieveIdViewSet.as_view(), - name="user_retrieve_id_api", + name="get_by_id", ), path( "", UserRetrieveViewSet.as_view(), - name="user_retrieve_username_api", + name="get", ), ] diff --git a/akarpov/users/signals.py b/akarpov/users/signals.py index 2e19442..194b645 100644 --- a/akarpov/users/signals.py +++ b/akarpov/users/signals.py @@ -10,6 +10,7 @@ from django.db.models.signals import pre_save from django.dispatch import receiver +from akarpov.files.consts import USER_INITIAL_FILE_UPLOAD from akarpov.users.models import User from akarpov.users.services.history import ( create_history_note, @@ -21,7 +22,7 @@ def user_create(sender, instance: User, **kwargs): if instance.id is None: # 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) diff --git a/akarpov/users/tests/factories.py b/akarpov/users/tests/factories.py index e304762..5246936 100644 --- a/akarpov/users/tests/factories.py +++ b/akarpov/users/tests/factories.py @@ -5,12 +5,14 @@ from factory import Faker, post_generation from factory.django import DjangoModelFactory +from akarpov.utils.faker import django_image + class UserFactory(DjangoModelFactory): - username = Faker("user_name") email = Faker("email") name = Faker("name") + about = Faker("text") @post_generation 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) + @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: model = get_user_model() + skip_postgeneration_save = False django_get_or_create = ["username"] diff --git a/akarpov/users/tests/test_api_views.py b/akarpov/users/tests/test_api_views.py new file mode 100644 index 0000000..e69de29 diff --git a/akarpov/users/tests/test_models.py b/akarpov/users/tests/test_models.py index 3fe97c1..a4816a2 100644 --- a/akarpov/users/tests/test_models.py +++ b/akarpov/users/tests/test_models.py @@ -1,3 +1,4 @@ +from akarpov.files.consts import USER_INITIAL_FILE_UPLOAD from akarpov.users.models import User @@ -5,3 +6,12 @@ def test_user_create(user: User): password = "123" user.set_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 diff --git a/akarpov/utils/faker.py b/akarpov/utils/faker.py index 8802873..0399428 100644 --- a/akarpov/utils/faker.py +++ b/akarpov/utils/faker.py @@ -1,6 +1,9 @@ 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 PIL import Image class MoneyProvider(PythonProvider): @@ -20,3 +23,20 @@ def configure_factory_faker(factory_faker): def configure_faker(faker): for provider in additional_providers: 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 + ) diff --git a/config/settings/base.py b/config/settings/base.py index 0ee2df4..15abfe0 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -32,8 +32,6 @@ SITE_ID = 1 # https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n 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 USE_TZ = True # https://docs.djangoproject.com/en/dev/ref/settings/#locale-paths