Compare commits
5 Commits
4ee7373b3f
...
317437fedc
Author | SHA1 | Date | |
---|---|---|---|
317437fedc | |||
26286984c2 | |||
9ae31c0146 | |||
87fd8fa96f | |||
c3401e732f |
@ -1,11 +1,2 @@
|
|||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
class BaseData(TestCase):
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def setUpTestData(cls):
|
|
||||||
User.objects.create_user(
|
|
||||||
username='testpeter',
|
|
||||||
password='Develop123'
|
|
||||||
).save()
|
|
@ -14,7 +14,7 @@ class ModelTestCase:
|
|||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.object = self.model.objects.get(id=1)
|
self.object = self.model.objects.get(id=1)
|
||||||
self.model_name = type(self.model).__name__
|
self.model_name = self.model.__name__
|
||||||
|
|
||||||
def _test_field(self, field_name, field_class, must_have={}, must_not_have={}):
|
def _test_field(self, field_name, field_class, must_have={}, must_not_have={}):
|
||||||
'''
|
'''
|
||||||
|
@ -11,7 +11,6 @@ from django.contrib.auth.models import User
|
|||||||
|
|
||||||
from lostplaces_app.models import PlaceImage, Place
|
from lostplaces_app.models import PlaceImage, Place
|
||||||
from lostplaces_app.tests.models import SubmittableTestCase
|
from lostplaces_app.tests.models import SubmittableTestCase
|
||||||
from lostplaces_app.tests.models.test_place_model import PlaceTestCase
|
|
||||||
|
|
||||||
from easy_thumbnails.fields import ThumbnailerImageField
|
from easy_thumbnails.fields import ThumbnailerImageField
|
||||||
|
|
||||||
@ -37,6 +36,9 @@ class TestPlaceImage(SubmittableTestCase, TestCase):
|
|||||||
place.tags.add('I a tag', 'testlocation')
|
place.tags.add('I a tag', 'testlocation')
|
||||||
place.save()
|
place.save()
|
||||||
|
|
||||||
|
if not os.path.isdir(settings.MEDIA_ROOT):
|
||||||
|
os.mkdir(settings.MEDIA_ROOT)
|
||||||
|
|
||||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
if not os.path.isfile(os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg')):
|
if not os.path.isfile(os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg')):
|
||||||
shutil.copyfile(
|
shutil.copyfile(
|
||||||
|
@ -6,7 +6,6 @@ from django.db import models
|
|||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
from lostplaces_app.models import Place
|
from lostplaces_app.models import Place
|
||||||
from lostplaces_app.tests import BaseData
|
|
||||||
from lostplaces_app.tests.models import SubmittableTestCase
|
from lostplaces_app.tests.models import SubmittableTestCase
|
||||||
|
|
||||||
|
|
||||||
|
@ -0,0 +1,28 @@
|
|||||||
|
from django.test import Client
|
||||||
|
|
||||||
|
class ViewTestCase:
|
||||||
|
view = None
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.view_name = self.view.__name__
|
||||||
|
self. client = Client()
|
||||||
|
|
||||||
|
def _test_has_context_key(self, response, context_key):
|
||||||
|
self.assertTrue( context_key in response.context,
|
||||||
|
msg='Expecting the context of %s to have an attribute \'%s\'' % (
|
||||||
|
self.view_name,
|
||||||
|
context_key
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
def _test_form(self, response, context_key, form_class):
|
||||||
|
self._test_has_context_key(response, context_key)
|
||||||
|
self.assertEqual(
|
||||||
|
type(response.context[context_key]),
|
||||||
|
form_class,
|
||||||
|
msg='Expecting %s\'s context.%s to be of the type %s' % (
|
||||||
|
self.view_name,
|
||||||
|
context_key,
|
||||||
|
form_class.__name__
|
||||||
|
)
|
||||||
|
)
|
@ -1,12 +1,20 @@
|
|||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from django.test import TestCase, Client
|
from django.test import TestCase
|
||||||
from django.urls import reverse_lazy
|
from django.urls import reverse_lazy
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
from lostplaces_app.models import Place
|
from lostplaces_app.models import Place
|
||||||
|
from lostplaces_app.views import (
|
||||||
|
PlaceCreateView,
|
||||||
|
PlaceListView
|
||||||
|
)
|
||||||
|
from lostplaces_app.forms import PlaceImageCreateForm, PlaceForm
|
||||||
|
from lostplaces_app.tests.views import ViewTestCase
|
||||||
|
|
||||||
class TestPlaceCreateView(TestCase):
|
class TestPlaceCreateView(ViewTestCase, TestCase):
|
||||||
|
|
||||||
|
view = PlaceCreateView
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
@ -27,26 +35,39 @@ class TestPlaceCreateView(TestCase):
|
|||||||
place.tags.add('I a tag', 'testlocation')
|
place.tags.add('I a tag', 'testlocation')
|
||||||
place.save()
|
place.save()
|
||||||
|
|
||||||
def setUp(self):
|
def test_has_forms(self):
|
||||||
self. client = Client()
|
|
||||||
|
|
||||||
def test_url_logged_in(self):
|
|
||||||
self.client.login(username='testpeter', password='Develop123')
|
self.client.login(username='testpeter', password='Develop123')
|
||||||
response = self.client.get(reverse_lazy('place_detail', kwargs={'pk': 1}))
|
response = self.client.get(reverse_lazy('place_create'))
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
|
|
||||||
def test_url_not_logged_in(self):
|
self._test_form(response, 'place_image_form', PlaceImageCreateForm)
|
||||||
url = reverse_lazy('place_detail', kwargs={'pk': 1})
|
self._test_form(response, 'place_form', PlaceForm)
|
||||||
response = self.client.get(url)
|
|
||||||
self.assertRedirects(
|
class TestPlaceListView(ViewTestCase, TestCase):
|
||||||
response=response,
|
view = PlaceListView
|
||||||
expected_url='?'.join([str(reverse_lazy('login')), 'next=/place/1/']),
|
|
||||||
status_code=302,
|
@classmethod
|
||||||
target_status_code=200,
|
def setUpTestData(cls):
|
||||||
msg_prefix='''Accesing PlaceDetailView while not logged should
|
user = User.objects.create_user(
|
||||||
redirect to login page with redirect params
|
username='testpeter',
|
||||||
''',
|
password='Develop123'
|
||||||
fetch_redirect_response=True
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
place = Place.objects.create(
|
||||||
|
name='Im a place',
|
||||||
|
submitted_when=datetime.datetime.now(),
|
||||||
|
submitted_by=user.explorer,
|
||||||
|
location='Testtown',
|
||||||
|
latitude=50.5,
|
||||||
|
longitude=7.0,
|
||||||
|
description='This is just a test, do not worry'
|
||||||
|
)
|
||||||
|
place.tags.add('I a tag', 'testlocation')
|
||||||
|
place.save()
|
||||||
|
|
||||||
|
def test_list_view(self):
|
||||||
|
self.client.login(username='testpeter', password='Develop123')
|
||||||
|
response = self.client.get(reverse_lazy('place_list'))
|
||||||
|
|
||||||
|
self._test_has_context_key(response, 'place_map_center')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user