Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saxix committed Apr 6, 2015
1 parent f1aaf54 commit c938fdf
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 20 deletions.
22 changes: 22 additions & 0 deletions tests/conftest.py
@@ -1,3 +1,8 @@
from django.contrib.auth.models import User
from django_dynamic_fixture import G
import django_webtest
import pytest


def pytest_configure(config):
try:
Expand All @@ -7,3 +12,20 @@ def pytest_configure(config):
django.setup()
except ImportError:
pass


@pytest.fixture(scope='function')
def app(request):
wtm = django_webtest.WebTestMixin()
wtm.csrf_checks = False
wtm._patch_settings()
request.addfinalizer(wtm._unpatch_settings)
return django_webtest.DjangoTestApp()



@pytest.fixture(scope='function')
def users():
return G(User, n=2, is_staff=False, is_active=False)


18 changes: 18 additions & 0 deletions tests/test_api.py
Expand Up @@ -7,6 +7,7 @@
from django.http import HttpResponse
from django.contrib.auth.models import Permission
from django.test import TestCase

if six.PY3:
import csv
elif six.PY2:
Expand Down Expand Up @@ -89,6 +90,23 @@ def test_export_as_csv(self):
else:
self.assertEquals(csv_dump, '"Field 1";"Field 2"\r\n"1";"4"\r\n"2";"5"\r\n"3";"ӼӳӬԖԊ"\r\n')

def test_dialect(self):
fields = ['field1', 'field2']
header = ['Field 1', 'Field 2']
Row = namedtuple('Row', fields)
rows = [Row(1, 4),
Row(2, 5),
Row(3, 'ӼӳӬԖԊ')]
mem = six.StringIO()
export_as_csv(queryset=rows, fields=fields, header=header,
out=mem, options={'dialect': 'excel'})
mem.seek(0)
csv_dump = mem.read()
if six.PY2:
self.assertEquals(csv_dump.decode('utf8'), u'Field 1,Field 2\r\n1,4\r\n2,5\r\n3,ӼӳӬԖԊ\r\n')
else:
self.assertEquals(csv_dump, 'Field 1,Field 2\r\n1,4\r\n2,5\r\n3,ӼӳӬԖԊ\r\n')


class TestExportAsExcel(TestCase):
def test_default_params(self):
Expand Down
22 changes: 16 additions & 6 deletions tests/test_exports.py
@@ -1,10 +1,16 @@
# -*- encoding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from django.utils.encoding import smart_text
import io
import six
import xlrd
import csv
import mock

if six.PY2:
import unicodecsv as csv
else:
import csv

from django.utils.encoding import smart_text
from django_webtest import WebTest
from django_dynamic_fixture import G
from django.contrib.auth.models import User
Expand Down Expand Up @@ -155,18 +161,22 @@ def test_no_permission(self):
assert six.b('Sorry you do not have rights to execute this action') in res.body

def test_success(self):
with user_grant_permission(self.user, ['auth.change_user', 'auth.adminactions_export_user']):
with user_grant_permission(self.user, ['demo.change_demomodel',
'demo.adminactions_export_demomodel']):
res = self.app.get('/', user='user')
res = res.click('Users')
res = res.click('Demo models')
form = res.forms['changelist-form']
form['action'] = self.action_name
# form.set('_selected_action', True, 1)
self._select_rows(form)
res = form.submit()
res = res.form.submit('apply')
io = six.StringIO(smart_text(res.body))
if six.PY2:
buff = io.BytesIO(res.body)
else:
buff = io.StringIO(smart_text(res.body))
csv_reader = csv.reader(buff)

csv_reader = csv.reader(io)
self.assertEqual(len(list(csv_reader)), 2)

def test_custom_filename(self):
Expand Down
14 changes: 0 additions & 14 deletions tests/test_transaction.py
Expand Up @@ -11,20 +11,6 @@
from adminactions.signals import adminaction_end


@pytest.fixture(scope='function')
def users():
return G(User, n=2, is_staff=False, is_active=False)


@pytest.fixture(scope='function')
def app(request):
wtm = django_webtest.WebTestMixin()
wtm.csrf_checks = False
wtm._patch_settings()
request.addfinalizer(wtm._unpatch_settings)
return django_webtest.DjangoTestApp()


@pytest.mark.django_db(transaction=True)
def test_nocommit():
with compat.nocommit():
Expand Down
20 changes: 20 additions & 0 deletions tests/test_views.py
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import pytest
import datetime
from django.core.urlresolvers import reverse
from django.utils import dateformat


@pytest.mark.django_db(transaction=True)
def test_format_date(app):
d = datetime.datetime.now()

url = reverse('adminactions.format_date')
fmt = 'd-m-Y'
res = app.get("{}?fmt={}".format(url, fmt))
assert res.body == dateformat.format(d, fmt)

fmt = 'd mm Y'
res = app.get("{}?fmt={}".format(url, fmt))
assert res.body == dateformat.format(d, fmt)

0 comments on commit c938fdf

Please sign in to comment.