Skip to content

Commit

Permalink
Add minimal frontend test
Browse files Browse the repository at this point in the history
  • Loading branch information
mbourqui committed May 10, 2017
1 parent 41ba77d commit 6057dbf
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ form field. In order to achieve that, you need to include the javascript in the
form field is used

{% load static %}
<script src="{% static "constrainedfilefield/js/file_checker.js" %}"></script>
<script src="{% static 'constrainedfilefield/js/file_checker.js' %}"></script>

and activate this feature by setting `js_checker=True` when instantiating the
`ConstrainedFileField`.
Expand Down
4 changes: 2 additions & 2 deletions constrainedfilefield/tests/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TestNoModelForm(forms.Form):
blank=True,
upload_to='testfile',
content_types=['image/png'],
max_upload_size=10240)
max_upload_size=10240).formfield()


class TestNoModelJsForm(forms.Form):
Expand All @@ -58,4 +58,4 @@ class TestNoModelJsForm(forms.Form):
upload_to='testfile',
content_types=['image/png'],
max_upload_size=10240,
js_checker=True)
js_checker=True).formfield()
57 changes: 55 additions & 2 deletions constrainedfilefield/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,32 @@

LANGUAGE_CODE = 'en'

ROOT_URLCONF = 'tests.urls'
ROOT_URLCONF = 'constrainedfilefield.tests.urls'

# MEDIA CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/stable/ref/settings/#media-root
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# See: https://docs.djangoproject.com/en/stable/ref/settings/#media-url
MEDIA_URL = '/media/'

# STATIC FILE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = '/static/'

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

LOGIN_URL = '/accounts/login/'

MANAGERS = []
Expand All @@ -49,4 +70,36 @@

USE_I18N = True

TEMPLATE_DIRS = [os.path.join(os.path.dirname(__file__), 'templates')]
# TEMPLATE_DIRS = [os.path.join(os.path.dirname(__file__), 'templates')]
TEMPLATES = [
{
# See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs
'DIRS': [
os.path.join(os.path.dirname(__file__), 'templates'),
],
'OPTIONS': {
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-debug
'debug': DEBUG,
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-loaders
# https://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
# Your stuff: custom template context processors go here
],
},
},
]
20 changes: 5 additions & 15 deletions constrainedfilefield/tests/templates/tests/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,11 @@

<body>

<div class="row">
<label class="col-sm-4 col-form-label text-right" for="{{ form.the_file.id_for_label }}">
{{ form.the_file.label }}
</label>
<div class="col-sm">
{% form_error form.the_file.errors %}
<label class="custom-file">
{{ form.the_file }}
<span class="custom-file-control"></span>
</label>
<small id="fileHelp" class="form-text text-muted">
Max file size is {{ form.the_file.field.max_upload_size }} bytes.
</small>
</div>
</div>
<form action="" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>

<script src="{% static 'constrainedfilefield/js/file_checker.js' %}"></script>

Expand Down
23 changes: 14 additions & 9 deletions constrainedfilefield/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.conf import settings
from django.core.files import File
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
from django.test import TestCase, Client

from constrainedfilefield.tests.forms import TestModelForm, TestModelFormJs, TestModelNoValidateForm, TestElementForm, \
TestNoModelForm, TestNoModelJsForm
Expand All @@ -14,6 +14,9 @@
class ConstrainedFileFieldTest(TestCase):
SAMPLE_FILES_PATH = os.path.join(settings.BASE_DIR, 'sample_files')

# -------
# BACKEND

def test_create_empty_instance(self):
TestModel.objects.create()

Expand Down Expand Up @@ -128,8 +131,6 @@ def test_nomodel_form_ok(self):
content_type='image/png')
self.assertTrue(form.is_valid())

self._check_nomodel_file_url(form.the_file, 'the_file.png')

for uploaded_file in form.files.values():
uploaded_file.close()

Expand All @@ -140,11 +141,19 @@ def test_nomodel_form_js_ok(self):
content_type='image/png')
self.assertTrue(form.is_valid())

self._check_nomodel_file_url(form.the_file, 'the_file.png')

for uploaded_file in form.files.values():
uploaded_file.close()

# -------
# FRONTEND

def test_nomodel_form_js_view_ok(self):
c = Client()
response = c.get('/nomodel/')
# FIXME: validate response content: form is included as expected
assert response.status_code == 200

# -------
# Utilities

def _get_sample_file(self, filename):
Expand All @@ -155,10 +164,6 @@ def _check_file_url(self, filefield, filename):
url = os.path.join(settings.MEDIA_URL, filefield.field.upload_to, filename)
self.assertEqual(filefield.url, url)

def _check_nomodel_file_url(self, filefield, filename):
url = os.path.join(settings.MEDIA_URL, filefield.upload_to, filename)
self.assertEqual(filefield.storage.url(os.path.join(filefield.upload_to,filename)), url)

def _create_bound_test_model_form(self, form_class, orig_filename=None,
dest_filename=None, content_type=None):
if orig_filename and dest_filename and content_type:
Expand Down
3 changes: 2 additions & 1 deletion constrainedfilefield/tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding:utf-8 -*-
from django.conf.urls import url
from constrainedfilefield.tests import views

urlpatterns = [
url(r'', ),
url(r'^nomodel/$', views.nomodel_form, name="nomodel"),
]
27 changes: 27 additions & 0 deletions constrainedfilefield/tests/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.shortcuts import render

from constrainedfilefield.tests import forms


def nomodel_form(request):
"""
Parameters
----------
request: HTTP request
Returns
--------
HttpResponse
with a context dictionary
"""
context = {}
if request.method == 'POST':
form = forms.TestNoModelForm(request.POST, )
if form.is_valid():
pass
else:
pass
else:
context['form'] = forms.TestNoModelForm()
return render(request, 'tests/form.html', context)

0 comments on commit 6057dbf

Please sign in to comment.