Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ jobs:
build:
docker:
- image: themattrix/tox
- image: circleci/postgres:latest
working_directory: ~/django-formidable
steps:
- run:
name: "Update APT stuff"
command: "apt-get update"
- run:
name: "Install postgresql-common Debian package"
command: "apt-get install -y postgresql-common"
- checkout
- run:
name: Run the test suite
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ master (unreleased)

- Extend the test matrix to spread through different DRF versions.
- Drop support for Django 1.10 (EOL was in December 2nd, 2017).
- Launch tests on Postgresql as well as SQLite. Launching these tests will be possible both locally and on Circle-CI. See the "dev" documentation on how you can run tests on your development environment (#161).

Release 3.1.0 (2019-06-03)
==========================
Expand Down
19 changes: 19 additions & 0 deletions demo/demo/settings_test_pg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from .settings import * # noqa
import os

dbhost = os.environ['PGHOST']
dbname = os.environ['PGDATABASE']
dbuser = os.environ['PGUSER']
dbpassword = os.environ['PGPASSWORD']
dbport = os.environ['PGPORT']

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': dbname,
'USER': dbuser,
'PASSWORD': dbpassword,
'HOST': dbhost,
'PORT': dbport,
}
}
7 changes: 7 additions & 0 deletions demo/tests/perfs/test_integration.perf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ UpdateFormTestCase.test_queryset_on_context_form_detail:
- db: 'SELECT ... FROM "django_session" WHERE ("django_session"."expire_date" > # AND "django_session"."session_key" = #)'
- db: 'SELECT ... FROM "formidable_formidable" WHERE "formidable_formidable"."id" = #'
- db: 'SELECT ... FROM "formidable_field" WHERE "formidable_field"."form_id" = # ORDER BY "formidable_field"."order" ASC'
UpdateFormTestCase.test_queryset_on_context_form_detail-pg:
- db: 'SELECT ... FROM "django_session" WHERE ("django_session"."expire_date" > #::timestamptz AND "django_session"."session_key" = #)'
- db: 'SELECT ... FROM "formidable_formidable" WHERE "formidable_formidable"."id" = #'
- db: 'SELECT ... FROM "formidable_field" WHERE "formidable_field"."form_id" = # ORDER BY "formidable_field"."order" ASC'
UpdateFormTestCase.test_queryset_on_get:
- db: 'SELECT ... FROM "formidable_formidable" WHERE "formidable_formidable"."id" = #'
- db: 'SELECT ... FROM "formidable_field" WHERE "formidable_field"."form_id" = # ORDER BY "formidable_field"."order" ASC'
UpdateFormTestCase.test_queryset_on_get-pg:
- db: 'SELECT ... FROM "formidable_formidable" WHERE "formidable_formidable"."id" = #'
- db: 'SELECT ... FROM "formidable_field" WHERE "formidable_field"."form_id" = # ORDER BY "formidable_field"."order" ASC'
186 changes: 179 additions & 7 deletions demo/tests/perfs/test_perfs_rec.perf.yml

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions demo/tests/test_end_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import copy
from functools import reduce
from unittest import skipIf

from django.conf import settings
from django.db import connection
from django.test import TestCase, TransactionTestCase
from django.test.utils import CaptureQueriesContext
Expand Down Expand Up @@ -1219,6 +1221,8 @@ def test_create_sepa(self):

class CreateSerializerTransactionTestCase(TransactionTestCase):

@skipIf('postgresql' in settings.DATABASES['default']['ENGINE'],
"Skip if PG")
def test_unique_transaction(self):
data = copy.deepcopy(CreateSerializerTestCase.data)
data['fields'] = CreateSerializerTestCase.fields_with_items
Expand All @@ -1228,7 +1232,7 @@ def test_unique_transaction(self):
serializer.save()
begin_count = sum(1 for query in capture.captured_queries
if query['sql'] == 'BEGIN')
self.assertEqual(begin_count, 1)
self.assertEqual(begin_count, 1, capture.captured_queries)


class UpdateFormTestCase(TestCase):
Expand Down Expand Up @@ -1528,6 +1532,7 @@ def test_create(self):
serializer = FormidableSerializer(data=self.data)
self.assertTrue(serializer.is_valid(), serializer.errors)
form = serializer.save()
self.assertEqual(form.fields.all()[0].help_text, 'Field Help')
self.assertEqual(form.fields.all()[0].items.all()[0].help_text,
'Item Help')
field = form.fields.first()
self.assertEqual(field.help_text, 'Field Help')
first_item = field.items.order_by('id').first()
self.assertEqual(first_item.help_text, 'Item Help')
13 changes: 11 additions & 2 deletions demo/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from copy import deepcopy

from django.core.urlresolvers import reverse
from django.conf import settings
import django_perf_rec

from freezegun import freeze_time
Expand Down Expand Up @@ -290,17 +291,25 @@ def test_delete_field_on_update(self):
).exists())

def test_queryset_on_get(self):
with django_perf_rec.record(path='perfs/'):
record_name = 'UpdateFormTestCase.test_queryset_on_get'
if 'postgresql' in settings.DATABASES['default']['ENGINE']:
record_name = '{}-pg'.format(record_name)

with django_perf_rec.record(record_name=record_name, path='perfs/'):
self.client.get(reverse(
'formidable:form_detail', args=[self.form.pk])
)

def test_queryset_on_context_form_detail(self):
record_name = 'UpdateFormTestCase.test_queryset_on_context_form_detail'
if 'postgresql' in settings.DATABASES['default']['ENGINE']:
record_name = '{}-pg'.format(record_name)

session = self.client.session
session['role'] = 'padawan'
session.save()

with django_perf_rec.record(path='perfs/'):
with django_perf_rec.record(record_name=record_name, path='perfs/'):
self.client.get(reverse(
'formidable:context_form_detail', args=[self.form.pk])
)
Expand Down
21 changes: 13 additions & 8 deletions demo/tests/test_perfs_rec.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
except ImportError:
from django.core.urlresolvers import reverse

from django.conf import settings
from django_perf_rec import TestCaseMixin
from rest_framework.test import APITestCase

Expand All @@ -26,10 +27,13 @@ def get_yml_dir_path(self):

return os.path.join(self.dir_path, self.yml_dir, '')

def record_performance(self, record_name=None, path=None):
def record_performance(self, record_name, path=None):
if path is None:
path = self.get_yml_dir_path()

if 'postgresql' in settings.DATABASES['default']['ENGINE']:
record_name = '{}-pg'.format(record_name)

return super(TestCase, self).record_performance(
path=path,
record_name=record_name
Expand All @@ -56,7 +60,7 @@ def setUpClass(cls):
)

def test_access_list_perf_rec(self):
with self.record_performance(record_name='accesses-list'):
with self.record_performance(record_name='TestPerfRec.accesses-list'):
url = reverse('formidable:accesses_list')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
Expand All @@ -66,20 +70,21 @@ def test_context_form_details_perf_rec(self):

url = reverse('formidable:context_form_detail', args=(form_id,))

with self.record_performance(record_name='get-context-form'):
with self.record_performance(
record_name='TestPerfRec.get-context-form'):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)

def test_form_create_perf_rec(self):
with self.record_performance(record_name='create-form'):
with self.record_performance(record_name='TestPerfRec.create-form'):
self._create_form()
# Status code is checked in :func:`_create_form()`

def test_form_update_form_without_changes(self):
form_id = self._create_form()

with self.record_performance(
record_name='update-form-without-changes'
record_name='TestPerfRec.update-form-without-changes'
):
url = reverse('formidable:form_detail', args=(form_id, ))
response = self.client.put(
Expand All @@ -92,7 +97,7 @@ def test_form_update_with_changes(self):
form_id = self._create_form()

with self.record_performance(
record_name='update-form-with-changes'
record_name='TestPerfRec.update-form-with-changes'
):
url = reverse('formidable:form_detail', args=(form_id,))
response = self.client.put(
Expand All @@ -104,15 +109,15 @@ def test_form_update_with_changes(self):
def test_retrieve_form_perf_rec(self):
form_id = self._create_form()

with self.record_performance(record_name='retrieve-form'):
with self.record_performance(record_name='TestPerfRec.retrieve-form'):
url = reverse('formidable:form_detail', args=(form_id,))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)

def test_form_validate_perf_rec(self):
form_id = self._create_form()

with self.record_performance(record_name='validate-form'):
with self.record_performance(record_name='TestPerfRec.validate-form'):
url = reverse('formidable:form_validation', args=(form_id,))
response = self.client.post(url, format="json")
# Empty response in the case it's valid.
Expand Down
12 changes: 12 additions & 0 deletions docs/source/dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ Developer's documentation
Testing
-------

Prerequisites
~~~~~~~~~~~~~

If you want to run the whole test suite, you'll need to have a working Postgresql server instance (preferrably the latest), with the ``pg_virtualenv`` tool available.
On Debian, this executable is provided by the package ``postgresql-common``.

If you don't have this tools in your toolbox, then... if you're doing a change that **impacts the performance records**, you won't be able to see or generate the diff, **so your branch tests would fail.**

.. note::

Postgresql driver is only available for Linux or MacOS.

Using tox
~~~~~~~~~

Expand Down
9 changes: 7 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
[tox]
envlist =
django111-py{27,35,36}-drf{38,39,310}
django111-py{27,35,36}-drf{38,39,310}-{sqlite,pg}
spectest
flake8
isort-check
skipsdist = True
skip_missing_interpreters = True

[testenv]
passenv = PGHOST PGDATABASE PGUSER PGPASSWORD PGPORT
whitelist_externals = pg_virtualenv
usedevelop = True
changedir = demo
deps =
Expand All @@ -20,12 +22,15 @@ deps =
drf38: djangorestframework<3.9
drf39: djangorestframework<3.10
drf310: djangorestframework<3.11
; When testing with postgresql
pg: psycopg2-binary
; Requirements from demo project
-rdemo/requirements-demo.pip
commands =
python --version
pip list --format=columns
python manage.py test {posargs}
sqlite: python manage.py test {posargs}
pg: pg_virtualenv python manage.py test --settings=demo.settings_test_pg {posargs}

[testenv:flake8]
basepython = python3.6
Expand Down