Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create some test data with model_mommy #18

Closed
wants to merge 16 commits into from

Conversation

PetrDlouhy
Copy link
Contributor

@PetrDlouhy PetrDlouhy commented Jun 21, 2019

Test change view with data created by model mommy

TODO:

  • Fix missing test
  • Write documentation how to override the automatic model generation for specific models.

Sorry, something went wrong.

@PetrDlouhy PetrDlouhy changed the title test with mommy data create some test data with model_mommy Jun 21, 2019
@jayvdb
Copy link

jayvdb commented Jun 15, 2020

This is sort of covered with https://github.com/alsur/django-admin-auto-tests , but no objection to it being here.

Could you update the PR to use model-bakery, as model-mommy is discontinued.

@jayvdb
Copy link

jayvdb commented Jun 22, 2020

I've tested this model-bakery jayvdb@2c9debe 👍

@jayvdb
Copy link

jayvdb commented Sep 5, 2020

@PetrDlouhy , this is missing one case

diff --git a/django_admin_smoke_tests/tests.py b/django_admin_smoke_tests/tests.py
index 718fbfb..cbd928f 100644
--- a/django_admin_smoke_tests/tests.py
+++ b/django_admin_smoke_tests/tests.py
@@ -167,7 +167,7 @@ class AdminSiteSmokeTestMixin(object):
         form_field_names = frozenset(getattr(model_admin.form,
             'base_fields', []))
 
-        model_instance = model()
+        model_instance = mommy.make(model)
 
         for attr in attr_set:
             # for now we'll just check attributes, not strings

This comes out in the following, where base.py", line 821, in is_expired is inside a @property. hasattr triggers the property, which can cause any exception. There are lots of solutions for that, but a natural one is to use the model baker to get an instance that has the desired properties all filled in.

======================================================================
ERROR: test_specified_fields (tests.test_admin_smoke.AdminSiteSmokeTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "django_admin_smoke_tests/tests.py", line 31, in test_deco
    fn(self, model, model_admin)
  File "django_admin_smoke_tests/tests.py", line 188, in test_specified_fields
    has_model_attr = hasattr(model_instance, attr)
  File "djstripe/models/base.py", line 821, in is_expired
    return timezone.now() > self.created + timedelta(hours=24)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'datetime.timedelta'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "django_admin_smoke_tests/tests.py", line 50, in test_deco
    six.raise_from(ModelAdminCheckException(
  File "<string>", line 3, in raise_from
django_admin_smoke_tests.tests.ModelAdminCheckException: Above exception occured while running test 'test_specified_fields' on modeladmin djstripe.IdempotencyKeyAdmin (IdempotencyKey)

@PetrDlouhy
Copy link
Contributor Author

I have updated this to use model-bakery and also rebased to #17 (updates to newest Django)

@jayvdb
Copy link

jayvdb commented Aug 15, 2022

I've run the tests on a bunch of Pythons and Djangos, and all seem to have one failure.

.................E......
======================================================================
ERROR: test_change_post (test_project.main.tests.ForbiddenAdminSiteSmokeTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/jayvdb/django/django-admin-smoke-tests/django_admin_smoke_tests/tests.py", line 29, in test_deco
    fn(self, model, model_admin)
  File "/home/jayvdb/django/django-admin-smoke-tests/django_admin_smoke_tests/tests.py", line 316, in test_change_post
    response = model_admin.change_view(request, object_id=str(pk))
  File "/home/jayvdb/django/django-admin-smoke-tests/.tox/py38-dj32/lib/python3.8/site-packages/django/contrib/admin/options.py", line 1660, in change_view
    return self.changeform_view(request, object_id, form_url, extra_context)
  File "/home/jayvdb/django/django-admin-smoke-tests/.tox/py38-dj32/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/home/jayvdb/django/django-admin-smoke-tests/.tox/py38-dj32/lib/python3.8/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/jayvdb/django/django-admin-smoke-tests/.tox/py38-dj32/lib/python3.8/site-packages/django/contrib/admin/options.py", line 1540, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "/home/jayvdb/django/django-admin-smoke-tests/.tox/py38-dj32/lib/python3.8/site-packages/django/contrib/admin/options.py", line 1565, in _changeform_view
    raise PermissionDenied
django.core.exceptions.PermissionDenied

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jayvdb/django/django-admin-smoke-tests/django_admin_smoke_tests/tests.py", line 55, in test_deco
    six.raise_from(
  File "<string>", line 3, in raise_from
django_admin_smoke_tests.tests.ModelAdminCheckException: Above exception occured while running test 'test_change_post' on modeladmin main.ForbiddenPostAdmin (ForbiddenPost)

Really looking forward to this.

@PetrDlouhy PetrDlouhy marked this pull request as draft August 15, 2022 12:25
@PetrDlouhy
Copy link
Contributor Author

PetrDlouhy commented Aug 15, 2022

@jayvdb I know about this (it is visible on automated tests running in GitHub actions for my repository: https://github.com/PetrDlouhy/django-admin-smoke-tests/).
The problem is, that the fact that PermissionDenied is thrown by the POST request is correct. It didn't fail before because it had no data to actually test this behavior.

I tried to fix this with assertRaises but I was not able to make it working on the class using Mixin.

I am planing to change the whole workflow to run test for every model as separate test case. I already have working prototype. It might provide different solution to this problem, so I will return to this after I will finish that.

@PetrDlouhy
Copy link
Contributor Author

PetrDlouhy commented Aug 19, 2022

@jayvdb I have fixed the tests, but I am not able to keep this in separate PR anymore.
I am closing this PR in favor of #24, where are all the changes from here and many more.

I would be very glad if you could test the changes on your project and tell me your opinion on the changes.

@PetrDlouhy PetrDlouhy closed this Aug 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants