Skip to content

Commit

Permalink
Add test for TimestampCreateView's custom form_valid fixes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
jwokaty committed Oct 19, 2020
1 parent 2e564a1 commit afbab07
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
3 changes: 2 additions & 1 deletion common_files/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""CommonFiles tests"""

from common_files.tests.active_tabular_inline import ActiveTabularInlineTest
from common_files.tests.base import BaseAdminTest
from common_files.tests.start_time import StartTimeListFilterTest
from common_files.tests.timestamp import TimestampAdminTest
from common_files.tests.timestamp_active import TimestampActiveAdminTest
from common_files.tests.timestamp_create_view import TimestampCreateViewTest
from common_files.tests.utils import CommonFilesUtilsTest
from common_files.tests.active_tabular_inline import ActiveTabularInlineTest
65 changes: 65 additions & 0 deletions common_files/tests/timestamp_create_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""TimestampCreateViewTest"""

from django.contrib.auth.models import User
from django.db import connection
from django.db import models
from django.db.utils import ProgrammingError
from django.test import RequestFactory
from django.test import TestCase

from common_files.models.timestamp import Timestamp
from common_files.views.timestamp import TimestampCreateView


class TimestampCreateViewTest(TestCase):
"""TimestampCreateViewTest tests timestamp set at validation"""

@classmethod
def setUpClass(cls):
class TimestampChild(Timestamp):
"""Test model"""
name = models.CharField(max_length=100)

def __str__(self):
return self.name

class Meta:
app_label = 'common_files'

class TimestampChildView(TimestampCreateView):
"""Test view"""

model = TimestampChild
fields = ['name']
success_url = '/success'

cls.model = TimestampChild
cls.view = TimestampChildView
try:
with connection.schema_editor() as editor:
editor.create_model(cls.model)
super().setUpClass()
except ProgrammingError:
pass

request_factory = RequestFactory()
cls.request = request_factory.get('/common_files/view/timestampchild')

def test_form_valid_with_anonymous_user(self):
"""Test form_valid has request with anonymous user"""
self.request.user = None
view_i = self.view(**{'request': self.request})
modelform = view_i.get_form_class()
form = modelform(data={'name': 'testing'})
http_request = view_i.form_valid(form)
self.assertEqual(http_request.url, '/success')

def test_form_valid_with_known_user(self):
"""Test form_valid has request with known user"""
self.request.user = User.objects.create(username='fb',
email='[email protected]')
view_i = self.view(**{'request': self.request})
modelform = view_i.get_form_class()
form = modelform(data={'name': 'testing'})
http_request = view_i.form_valid(form)
self.assertEqual(http_request.url, '/success')

0 comments on commit afbab07

Please sign in to comment.