Skip to content

Commit 1f76130

Browse files
authored
Merge pull request #278 from novafloss/add-tests-perfs
#153 Added perf-rec tests for the views
2 parents 9a5cb1f + 246ecfa commit 1f76130

File tree

5 files changed

+308
-0
lines changed

5 files changed

+308
-0
lines changed

CHANGELOG.rst

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ master (unreleased)
77

88
- Added tests for the validators mapping
99
- Minor syntax changes
10+
- Added perf rec tests
1011

1112
Release 1.0.2 (2017-10-10)
1213
==========================
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"fields": [
3+
{
4+
"id": "text",
5+
"slug": "text",
6+
"label": "text",
7+
"type_id": "text",
8+
"description": "text",
9+
"accesses": []
10+
},
11+
{
12+
"id": "email",
13+
"slug": "email",
14+
"label": "email",
15+
"type_id": "email",
16+
"description": "email",
17+
"accesses": []
18+
}
19+
],
20+
"description": "Test description",
21+
"label": "Test form label",
22+
"conditions": []
23+
}

demo/tests/fixtures/form-data.json

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"fields": [
3+
{
4+
"id": "title",
5+
"slug": "title",
6+
"label": "title",
7+
"type_id": "title",
8+
"description": "title",
9+
"accesses": []
10+
},
11+
12+
{
13+
"id": "name",
14+
"slug": "name",
15+
"label": "Name",
16+
"type_id": "text",
17+
"description": "Name",
18+
"accesses": []
19+
},
20+
{
21+
"id": "name2",
22+
"slug": "name2",
23+
"label": "Name2",
24+
"type_id": "text",
25+
"description": "Name2",
26+
"accesses": [
27+
{
28+
"access_id": "padawan",
29+
"level": "READONLY"
30+
}
31+
]
32+
},
33+
{
34+
"id": "checkbox",
35+
"slug": "checkbox",
36+
"label": "checkbox",
37+
"type_id": "checkbox",
38+
"description": "checkbox",
39+
"accesses": []
40+
},
41+
{
42+
"id": "text",
43+
"slug": "text",
44+
"label": "text",
45+
"type_id": "text",
46+
"description": "text",
47+
"accesses": []
48+
},
49+
{
50+
"id": "text1",
51+
"slug": "text1",
52+
"label": "text1",
53+
"type_id": "text",
54+
"description": "text",
55+
"accesses": []
56+
}
57+
],
58+
"description": "Test description",
59+
"label": "Test form label",
60+
"conditions": []
61+
}
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
accesses-list: []
2+
create-form:
3+
- db: 'SELECT (#) AS "a" FROM "django_session" WHERE "django_session"."session_key" = # LIMIT #'
4+
- db: SAVEPOINT `#`
5+
- db: INSERT INTO "django_session" (...) SELECT ...
6+
- db: RELEASE SAVEPOINT `#`
7+
- db: SAVEPOINT `#`
8+
- db: 'UPDATE "django_session" SET ... WHERE "django_session"."session_key" = #'
9+
- db: RELEASE SAVEPOINT `#`
10+
- db: 'SELECT ... FROM "django_session" WHERE ("django_session"."expire_date" > # AND "django_session"."session_key" = #)'
11+
- db: SAVEPOINT `#`
12+
- db: INSERT INTO "formidable_formidable" (...) VALUES (...)
13+
- db: RELEASE SAVEPOINT `#`
14+
- db: 'SELECT ... FROM "formidable_field" WHERE "formidable_field"."form_id" = # ORDER BY "formidable_field"."order" ASC'
15+
get-context-form:
16+
- db: 'SELECT ... FROM "django_session" WHERE ("django_session"."expire_date" > # AND "django_session"."session_key" = #)'
17+
- db: 'SELECT ... FROM "formidable_formidable" WHERE "formidable_formidable"."id" = #'
18+
- db: 'SELECT ... FROM "formidable_field" WHERE "formidable_field"."form_id" = # ORDER BY "formidable_field"."order" ASC'
19+
retrieve-form:
20+
- db: 'SELECT ... FROM "django_session" WHERE ("django_session"."expire_date" > # AND "django_session"."session_key" = #)'
21+
- db: 'SELECT ... FROM "formidable_formidable" WHERE "formidable_formidable"."id" = #'
22+
- db: 'SELECT ... FROM "formidable_field" WHERE "formidable_field"."form_id" = # ORDER BY "formidable_field"."order" ASC'
23+
update-form-with-changes:
24+
- db: 'SELECT ... FROM "django_session" WHERE ("django_session"."expire_date" > # AND "django_session"."session_key" = #)'
25+
- db: 'SELECT ... FROM "formidable_formidable" WHERE "formidable_formidable"."id" = #'
26+
- db: SAVEPOINT `#`
27+
- db: 'UPDATE "formidable_formidable" SET ... WHERE "formidable_formidable"."id" = #'
28+
- db: 'SELECT "formidable_field"."slug" FROM "formidable_field" WHERE "formidable_field"."form_id" = #'
29+
- db: 'SELECT ... FROM "formidable_field" WHERE "formidable_field"."form_id" = #'
30+
- db: INSERT INTO "formidable_field" (...) VALUES (...)
31+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
32+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
33+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
34+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
35+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
36+
- db: INSERT INTO "formidable_field" (...) VALUES (...)
37+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
38+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
39+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
40+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
41+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
42+
- db: RELEASE SAVEPOINT `#`
43+
- db: 'SELECT ... FROM "formidable_field" WHERE "formidable_field"."form_id" = # ORDER BY "formidable_field"."order" ASC'
44+
- db: SELECT ... FROM "formidable_item" WHERE "formidable_item"."field_id" IN (...) ORDER BY "formidable_item"."order" ASC
45+
- db: SELECT ... FROM "formidable_default" WHERE "formidable_default"."field_id" IN (...)
46+
- db: SELECT ... FROM "formidable_validation" WHERE "formidable_validation"."field_id" IN (...)
47+
- db: SELECT ... FROM "formidable_access" WHERE "formidable_access"."field_id" IN (...)
48+
update-form-without-changes:
49+
- db: 'SELECT ... FROM "django_session" WHERE ("django_session"."expire_date" > # AND "django_session"."session_key" = #)'
50+
- db: 'SELECT ... FROM "formidable_formidable" WHERE "formidable_formidable"."id" = #'
51+
- db: SAVEPOINT `#`
52+
- db: 'UPDATE "formidable_formidable" SET ... WHERE "formidable_formidable"."id" = #'
53+
- db: 'SELECT "formidable_field"."slug" FROM "formidable_field" WHERE "formidable_field"."form_id" = #'
54+
- db: 'SELECT ... FROM "formidable_field" WHERE "formidable_field"."form_id" = #'
55+
- db: INSERT INTO "formidable_field" (...) VALUES (...)
56+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
57+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
58+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
59+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
60+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
61+
- db: INSERT INTO "formidable_field" (...) VALUES (...)
62+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
63+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
64+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
65+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
66+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
67+
- db: INSERT INTO "formidable_field" (...) VALUES (...)
68+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
69+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
70+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
71+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
72+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
73+
- db: INSERT INTO "formidable_field" (...) VALUES (...)
74+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
75+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
76+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
77+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
78+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
79+
- db: INSERT INTO "formidable_field" (...) VALUES (...)
80+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
81+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
82+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
83+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
84+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
85+
- db: INSERT INTO "formidable_field" (...) VALUES (...)
86+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
87+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
88+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
89+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
90+
- db: INSERT INTO "formidable_access" (...) VALUES (...)
91+
- db: RELEASE SAVEPOINT `#`
92+
- db: 'SELECT ... FROM "formidable_field" WHERE "formidable_field"."form_id" = # ORDER BY "formidable_field"."order" ASC'
93+
- db: SELECT ... FROM "formidable_item" WHERE "formidable_item"."field_id" IN (...) ORDER BY "formidable_item"."order" ASC
94+
- db: SELECT ... FROM "formidable_default" WHERE "formidable_default"."field_id" IN (...)
95+
- db: SELECT ... FROM "formidable_validation" WHERE "formidable_validation"."field_id" IN (...)
96+
- db: SELECT ... FROM "formidable_access" WHERE "formidable_access"."field_id" IN (...)
97+
validate-form:
98+
- db: 'SELECT ... FROM "django_session" WHERE ("django_session"."expire_date" > # AND "django_session"."session_key" = #)'
99+
- db: 'SELECT ... FROM "formidable_formidable" WHERE "formidable_formidable"."id" = #'
100+
- db: 'SELECT ... FROM "formidable_field" WHERE "formidable_field"."form_id" = # ORDER BY "formidable_field"."order" ASC'

demo/tests/test_perfs_rec.py

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# -*- coding: utf-8 -*-
2+
import os
3+
import json
4+
5+
from django.test import TestCase as OrigTestCase
6+
7+
try:
8+
from django.urls import reverse
9+
except ImportError:
10+
from django.core.urlresolvers import reverse
11+
12+
from django_perf_rec import TestCaseMixin
13+
14+
15+
class TestCase(TestCaseMixin, OrigTestCase):
16+
"""
17+
Add use perf rec mixin
18+
"""
19+
20+
dir_path = os.path.dirname(os.path.realpath(__file__))
21+
yml_dir = 'perfs'
22+
23+
def get_yml_dir_path(self):
24+
"""
25+
Generate a path for the file
26+
"""
27+
28+
return os.path.join(self.dir_path, self.yml_dir, '')
29+
30+
def record_performance(self, record_name=None, path=None):
31+
if path is None:
32+
path = self.get_yml_dir_path()
33+
34+
return super(TestCase, self).record_performance(
35+
path=path,
36+
record_name=record_name
37+
)
38+
39+
40+
class TestPerfRec(TestCase):
41+
"""
42+
Log the queries to DB
43+
"""
44+
45+
fixtures_folder = 'fixtures'
46+
47+
@classmethod
48+
def setUpClass(cls):
49+
super(TestPerfRec, cls).setUpClass()
50+
fixtures_path = os.path.join(cls.dir_path, cls.fixtures_folder)
51+
52+
cls.form_data = json.load(
53+
open(os.path.join(fixtures_path, 'form-data.json'))
54+
)
55+
cls.form_data_changed = json.load(
56+
open(os.path.join(fixtures_path, 'form-data-changed.json'))
57+
)
58+
59+
def test_access_list_perf_rec(self):
60+
with self.record_performance(record_name='accesses-list'):
61+
url = reverse('formidable:accesses_list')
62+
self.client.get(url)
63+
64+
def test_context_form_details_perf_rec(self):
65+
form_id = self._create_form()
66+
67+
with self.record_performance(record_name='get-context-form'):
68+
url = reverse('formidable:context_form_detail', args=(form_id,))
69+
self.client.get(url)
70+
71+
def test_form_create_perf_rec(self):
72+
with self.record_performance(record_name='create-form'):
73+
self._create_form()
74+
75+
def test_form_update_form_without_changes(self):
76+
form_id = self._create_form()
77+
78+
with self.record_performance(
79+
record_name='update-form-without-changes'
80+
):
81+
url = reverse('formidable:form_detail', args=(form_id, ))
82+
self.client.put(
83+
url, data=json.dumps(self.form_data),
84+
content_type='application/json'
85+
)
86+
87+
def test_form_update_with_changes(self):
88+
form_id = self._create_form()
89+
90+
with self.record_performance(
91+
record_name='update-form-with-changes'
92+
):
93+
url = reverse('formidable:form_detail', args=(form_id,))
94+
self.client.put(
95+
url, data=json.dumps(self.form_data_changed),
96+
content_type='application/json'
97+
)
98+
99+
def test_retrieve_form_perf_rec(self):
100+
form_id = self._create_form()
101+
102+
with self.record_performance(record_name='retrieve-form'):
103+
url = reverse('formidable:form_detail', args=(form_id,))
104+
self.client.get(url)
105+
106+
def test_form_validate_perf_rec(self):
107+
form_id = self._create_form()
108+
109+
with self.record_performance(record_name='validate-form'):
110+
url = reverse('formidable:form_validation', args=(form_id,))
111+
self.client.get(url)
112+
113+
def _create_form(self):
114+
session = self.client.session
115+
session['role'] = 'padawan'
116+
session.save()
117+
118+
url = reverse('formidable:form_create')
119+
result = self.client.post(
120+
url, data=self.form_data
121+
)
122+
123+
return result.data.get('id')

0 commit comments

Comments
 (0)