Unverified Commit 4f00c56c authored by Thom Wiggers's avatar Thom Wiggers 📐
Browse files

Speed up tests

Now we're using `setUpTestData` more, not running some slow code,
using MD5 hashes for speed, and using bulk_create.

Also removes some dead code.
parent 7dcddb24
...@@ -11,14 +11,19 @@ from members.models import Member ...@@ -11,14 +11,19 @@ from members.models import Member
class CommitteeMembersTest(TestCase): class CommitteeMembersTest(TestCase):
fixtures = ['members.json', 'committees.json'] fixtures = ['members.json', 'committees.json']
def setUp(self): @classmethod
# Don't use setUpTestData because delete() will cause problems def setUpTestData(cls):
self.testcie = Committee.objects.get(pk=1) cls.testcie = Committee.objects.get(pk=1)
self.testuser = Member.objects.get(pk=1) cls.testuser = Member.objects.get(pk=1)
self.m = CommitteeMembership(committee=self.testcie, cls.m = CommitteeMembership.objects.create(
member=self.testuser, committee=cls.testcie,
member=cls.testuser,
chair=False) chair=False)
self.m.save()
def setUp(self):
self.testcie.refresh_from_db()
self.testuser.refresh_from_db()
self.m.refresh_from_db()
def test_unique(self): def test_unique(self):
with self.assertRaises(IntegrityError): with self.assertRaises(IntegrityError):
...@@ -86,12 +91,16 @@ class CommitteeMembersTest(TestCase): ...@@ -86,12 +91,16 @@ class CommitteeMembersTest(TestCase):
class CommitteeMembersChairTest(TestCase): class CommitteeMembersChairTest(TestCase):
fixtures = ['members.json', 'committees.json'] fixtures = ['members.json', 'committees.json']
@classmethod
def setUpTestData(cls):
cls.testcie = Committee.objects.get(pk=1)
cls.testuser = Member.objects.get(pk=1)
cls.testuser2 = Member.objects.get(pk=2)
def setUp(self): def setUp(self):
self.testcie = Committee.objects.get(pk=1)
self.testuser = Member.objects.get(pk=1)
self.testuser2 = Member.objects.get(pk=2)
self.m1 = CommitteeMembership(committee=self.testcie, self.m1 = CommitteeMembership(committee=self.testcie,
since=timezone.now().date().replace( since=timezone.now().date().replace(
day=1,
year=1900), year=1900),
member=self.testuser, member=self.testuser,
chair=True) chair=True)
...@@ -106,7 +115,7 @@ class CommitteeMembersChairTest(TestCase): ...@@ -106,7 +115,7 @@ class CommitteeMembersChairTest(TestCase):
m.full_clean() m.full_clean()
def test_inactive_chair(self): def test_inactive_chair(self):
self.m1.until = timezone.now().date().replace(year=1900) self.m1.until = timezone.now().date().replace(day=1, year=1900)
self.m1.save() self.m1.save()
m = CommitteeMembership(committee=self.testcie, m = CommitteeMembership(committee=self.testcie,
......
# Create your tests here.
...@@ -13,8 +13,9 @@ class EventTest(TestCase): ...@@ -13,8 +13,9 @@ class EventTest(TestCase):
fixtures = ['members.json'] fixtures = ['members.json']
def setUp(self): @classmethod
self.event = Event.objects.create( def setUpTestData(cls):
cls.event = Event.objects.create(
title_nl='testevene', title_nl='testevene',
title_en='testevent', title_en='testevent',
description_en='desc', description_en='desc',
...@@ -26,11 +27,16 @@ class EventTest(TestCase): ...@@ -26,11 +27,16 @@ class EventTest(TestCase):
map_location='test map location', map_location='test map location',
price=0.00, price=0.00,
fine=0.00) fine=0.00)
self.member = Member.objects.all()[0] cls.member = Member.objects.all()[0]
def test_end_after_start(self): def setUp(self):
self.event.refresh_from_db()
self.member.refresh_from_db()
def test_clean_works(self):
self.event.clean() self.event.clean()
def test_end_after_start(self):
self.event.start, self.event.end = self.event.end, self.event.start self.event.start, self.event.end = self.event.end, self.event.start
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
self.event.clean() self.event.clean()
...@@ -146,8 +152,7 @@ class EventTest(TestCase): ...@@ -146,8 +152,7 @@ class EventTest(TestCase):
def test_not_reached_participants_limit(self): def test_not_reached_participants_limit(self):
self.event.max_participants = 1 self.event.max_participants = 1
r1 = Registration.objects.create(event=self.event, member=self.member) Registration.objects.create(event=self.event, member=self.member)
r1.save()
self.assertTrue(self.event.reached_participants_limit()) self.assertTrue(self.event.reached_participants_limit())
...@@ -156,8 +161,9 @@ class RegistrationTest(TestCase): ...@@ -156,8 +161,9 @@ class RegistrationTest(TestCase):
fixtures = ['members.json'] fixtures = ['members.json']
def setUp(self): @classmethod
self.event = Event.objects.create( def setUpTestData(cls):
cls.event = Event.objects.create(
title_nl='testevene', title_nl='testevene',
title_en='testevent', title_en='testevent',
description_en='desc', description_en='desc',
...@@ -169,37 +175,38 @@ class RegistrationTest(TestCase): ...@@ -169,37 +175,38 @@ class RegistrationTest(TestCase):
map_location='test map location', map_location='test map location',
price=0.00, price=0.00,
fine=0.00) fine=0.00)
self.member = Member.objects.all()[0] cls.member = Member.objects.all()[0]
cls.r1 = Registration.objects.create(event=cls.event,
member=cls.member)
def setUp(self):
self.r1.refresh_from_db()
def test_is_late_registration(self): def test_is_late_registration(self):
r1 = Registration.objects.create(event=self.event, member=self.member) self.assertFalse(self.r1.is_late_cancellation())
self.assertFalse(r1.is_late_cancellation())
r1.date_cancelled = timezone.now() self.r1.date_cancelled = timezone.now()
self.assertFalse(r1.is_late_cancellation()) self.assertFalse(self.r1.is_late_cancellation())
self.event.cancel_deadline = (timezone.now() + self.event.cancel_deadline = (timezone.now() +
datetime.timedelta(hours=1)) datetime.timedelta(hours=1))
self.assertFalse(r1.is_late_cancellation()) self.assertFalse(self.r1.is_late_cancellation())
self.event.cancel_deadline = (timezone.now() - self.event.cancel_deadline = (timezone.now() -
datetime.timedelta(hours=1)) datetime.timedelta(hours=1))
self.assertTrue(r1.is_late_cancellation()) self.assertTrue(self.r1.is_late_cancellation())
def test_queue_position(self): def test_queue_position(self):
r1 = Registration.objects.create(event=self.event, member=self.member) self.assertEqual(self.r1.queue_position(), 0)
r1.save()
self.assertEqual(r1.queue_position(), 0)
self.event.max_participants = 0 self.event.max_participants = 0
self.assertEqual(r1.queue_position(), 1) self.assertEqual(self.r1.queue_position(), 1)
self.event.max_participants = 1 self.event.max_participants = 1
self.assertEqual(r1.queue_position(), 0) self.assertEqual(self.r1.queue_position(), 0)
def test_registration_either_name_or_member(self): def test_registration_either_name_or_member(self):
r1 = Registration.objects.create(event=self.event, member=self.member) self.r1.clean()
r1.clean()
r2 = Registration.objects.create(event=self.event, name='test name') r2 = Registration.objects.create(event=self.event, name='test name')
r2.clean() r2.clean()
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
......
...@@ -12,8 +12,9 @@ class RegistrationTest(TestCase): ...@@ -12,8 +12,9 @@ class RegistrationTest(TestCase):
fixtures = ['members.json'] fixtures = ['members.json']
def setUp(self): @classmethod
self.event = Event.objects.create( def setUpTestData(cls):
cls.event = Event.objects.create(
pk=1, pk=1,
title_nl='testevene', title_nl='testevene',
title_en='testevent', title_en='testevent',
...@@ -27,8 +28,9 @@ class RegistrationTest(TestCase): ...@@ -27,8 +28,9 @@ class RegistrationTest(TestCase):
map_location='test map location', map_location='test map location',
price=0.00, price=0.00,
fine=0.00) fine=0.00)
self.event.save() cls.member = Member.objects.filter(user__last_name="Wiggers").first()
self.member = Member.objects.filter(user__last_name="Wiggers").first()
def setUp(self):
self.client = Client() self.client = Client()
self.client.force_login(self.member.user) self.client.force_login(self.member.user)
...@@ -90,8 +92,7 @@ class RegistrationTest(TestCase): ...@@ -90,8 +92,7 @@ class RegistrationTest(TestCase):
self.event.cancel_deadline = (timezone.now() + self.event.cancel_deadline = (timezone.now() +
datetime.timedelta(hours=1)) datetime.timedelta(hours=1))
self.event.save() self.event.save()
r1 = Registration.objects.create(event=self.event, member=self.member) Registration.objects.create(event=self.event, member=self.member)
r1.save()
response = self.client.post('/events/1/registration/cancel/', response = self.client.post('/events/1/registration/cancel/',
follow=True) follow=True)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
...@@ -113,7 +114,6 @@ class RegistrationTest(TestCase): ...@@ -113,7 +114,6 @@ class RegistrationTest(TestCase):
name_en="test bool", name_en="test bool",
name_nl="test bool", name_nl="test bool",
required=False) required=False)
field1.save()
field2 = RegistrationInformationField.objects.create( field2 = RegistrationInformationField.objects.create(
pk=2, pk=2,
...@@ -122,7 +122,6 @@ class RegistrationTest(TestCase): ...@@ -122,7 +122,6 @@ class RegistrationTest(TestCase):
name_en="test int", name_en="test int",
name_nl="test int", name_nl="test int",
required=False) required=False)
field2.save()
field3 = RegistrationInformationField.objects.create( field3 = RegistrationInformationField.objects.create(
pk=3, pk=3,
...@@ -131,7 +130,6 @@ class RegistrationTest(TestCase): ...@@ -131,7 +130,6 @@ class RegistrationTest(TestCase):
name_en="test text", name_en="test text",
name_nl="test text", name_nl="test text",
required=False) required=False)
field3.save()
response = self.client.post('/events/1/registration/register/', response = self.client.post('/events/1/registration/register/',
{'info_field_1': True, {'info_field_1': True,
...@@ -155,13 +153,12 @@ class RegistrationTest(TestCase): ...@@ -155,13 +153,12 @@ class RegistrationTest(TestCase):
datetime.timedelta(hours=1)) datetime.timedelta(hours=1))
self.event.save() self.event.save()
field = RegistrationInformationField.objects.create( RegistrationInformationField.objects.create(
event=self.event, event=self.event,
type=RegistrationInformationField.TEXT_FIELD, type=RegistrationInformationField.TEXT_FIELD,
name_en="test", name_en="test",
name_nl="test", name_nl="test",
required=True) required=True)
field.save()
response = self.client.post('/events/1/registration/register/', response = self.client.post('/events/1/registration/register/',
follow=True) follow=True)
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
"model": "auth.user", "model": "auth.user",
"pk": 1, "pk": 1,
"fields": { "fields": {
"password": "pbkdf2_sha256$30000$HIt9lBUpgkYG$T2ofXIOlAhsqfMUqzl3Vl9vyaDq50d1JJJNEYeZ9/OM=", "password": "md5$wdVCeynupRVW$de1e35e14409c561c1b30a8d64d423d3",
"last_login": "2016-07-07T11:37:43Z", "last_login": "2016-07-07T11:37:43Z",
"is_superuser": true, "is_superuser": true,
"username": "thom", "username": "thom",
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
"model": "auth.user", "model": "auth.user",
"pk": 2, "pk": 2,
"fields": { "fields": {
"password": "pbkdf2_sha256$30000$80KR811he3aB$W11Exs1wY0tXw9kLsyunh1dzvRDcn1a+Hc9m1lTirFY=", "password": "md5$wdVCeynupRVW$de1e35e14409c561c1b30a8d64d423d3",
"last_login": "2016-07-07T12:01:02.638Z", "last_login": "2016-07-07T12:01:02.638Z",
"is_superuser": false, "is_superuser": false,
"username": "testuser", "username": "testuser",
......
from datetime import date, datetime, timedelta from datetime import date, datetime, timedelta
from django.contrib.auth.models import User from django.contrib.auth import get_user_model
from django.test import TestCase from django.test import TestCase
from django.utils import timezone from django.utils import timezone
...@@ -60,32 +60,26 @@ class MemberBirthdayTest(TestCase): ...@@ -60,32 +60,26 @@ class MemberBirthdayTest(TestCase):
class StatisticsTest(TestCase): class StatisticsTest(TestCase):
def setUp(self):
@classmethod
def setUpTestData(cls):
# Add 10 members with default membership # Add 10 members with default membership
for i in range(10): users = [get_user_model()(id=i, username=i) for i in range(10)]
u = User(username=i) get_user_model().objects.bulk_create(users)
u.save() memberships = [Membership(user_id=i, type="member")
membership = Membership(user=u, type="member") for i in range(10)]
membership.save() Membership.objects.bulk_create(memberships)
m = Member(user=u) members = [Member(user_id=i) for i in range(10)]
m.save() Member.objects.bulk_create(members)
def sum_members(self, members, type=None): def sum_members(self, members, type=None):
s = 0
for i in members:
if type is None: if type is None:
for j in i.values(): return sum(sum(i.values()) for i in members)
s = s + j
else: else:
s = s + i[type] return sum(map(lambda x: x[type], members))
return s
def sum_member_types(self, members): def sum_member_types(self, members):
s = 0 return sum(members.values())
for i in members.values():
s = s + i
return s
def test_gen_stats_year_no_members(self): def test_gen_stats_year_no_members(self):
member_types = ["member", "supporter", "honorary"] member_types = ["member", "supporter", "honorary"]
......
...@@ -22,34 +22,36 @@ def load_tests(loader, tests, ignore): ...@@ -22,34 +22,36 @@ def load_tests(loader, tests, ignore):
class NewslettersTest(TestCase): class NewslettersTest(TestCase):
def setUp(self): @classmethod
self.user = User.objects.create_user(username='jacob', def setUpTestData(cls):
cls.user = User.objects.create_user(username='jacob',
email='jacob@test.com', email='jacob@test.com',
password='top_secret', password='top_secret',
is_staff=True) is_staff=True)
self.user.user_permissions.set( cls.user.user_permissions.set(
Permission.objects.filter(content_type__app_label="newsletters") Permission.objects.filter(content_type__app_label="newsletters")
) )
self.user.backend = 'django.contrib.auth.backends.ModelBackend' cls.user.backend = 'django.contrib.auth.backends.ModelBackend'
self.user.save() cls.user.save()
self.client.force_login(self.user)
self.testletter_sent = Newsletter.objects.create( cls.testletter_sent = Newsletter.objects.create(
title_nl='testletter', title_nl='testletter',
title_en='testletter', title_en='testletter',
description_nl='testdesc', description_nl='testdesc',
description_en='testdesc', description_en='testdesc',
sent=True) sent=True)
self.testletter_concept = Newsletter.objects.create( cls.testletter_concept = Newsletter.objects.create(
title_nl='testletter', title_nl='testletter',
title_en='testletter', title_en='testletter',
description_nl='testdesc', description_nl='testdesc',
description_en='testdesc', description_en='testdesc',
sent=False) sent=False)
def setUp(self):
self.client.force_login(self.user)
def test_sent_change_redirect(self): def test_sent_change_redirect(self):
response = self.client.get(reverse( response = self.client.get(reverse(
'admin:newsletters_newsletter_change', args=[ 'admin:newsletters_newsletter_change', args=[
......
...@@ -25,19 +25,20 @@ class RaaSTestCase(SimpleTestCase): ...@@ -25,19 +25,20 @@ class RaaSTestCase(SimpleTestCase):
class AppApiTestCase(TestCase): class AppApiTestCase(TestCase):
"""Tests event registrations""" """Tests event registrations"""
def setUp(self): @classmethod
self.user = get_user_model().objects.create_user( def setUpTestData(cls):
cls.user = get_user_model().objects.create_user(
username='testuser', username='testuser',
first_name='first', first_name='first',
last_name='last_name', last_name='last_name',
email='foo@bar.com', email='foo@bar.com',
password='top secret') password='top secret')
self.member = Member.objects.create( cls.member = Member.objects.create(
user=self.user, user=cls.user,
birthday=datetime(1993, 3, 2) birthday=datetime(1993, 3, 2)
) )
self.token = Token.create_token(self.user) cls.token = Token.create_token(cls.user)
def test_GET_denied(self): def test_GET_denied(self):
response = self.client.get('/api/login') response = self.client.get('/api/login')
......
...@@ -151,6 +151,12 @@ AUTH_PASSWORD_VALIDATORS = [ ...@@ -151,6 +151,12 @@ AUTH_PASSWORD_VALIDATORS = [
}, },
] ]
# allow to use md5 in tests
PASSWORD_HASHERS = (
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
'django.contrib.auth.hashers.MD5PasswordHasher',
)
AUTHENTICATION_BACKENDS = [ AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend', 'django.contrib.auth.backends.ModelBackend',
'activemembers.backends.CommitteeBackend', 'activemembers.backends.CommitteeBackend',
......
...@@ -4,6 +4,10 @@ Settings for CI testing ...@@ -4,6 +4,10 @@ Settings for CI testing
This file is loaded by __init__.py if GITLAB_CI is set in the environment This file is loaded by __init__.py if GITLAB_CI is set in the environment
""" """
import logging
from .settings import INSTALLED_APPS, MIDDLEWARE
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.postgresql', 'ENGINE': 'django.db.backends.postgresql',
...@@ -14,3 +18,26 @@ DATABASES = { ...@@ -14,3 +18,26 @@ DATABASES = {
'PORT': 5432, 'PORT': 5432,
}, },
} }
# This won't help anyway
DEBUG = False
logging.disable(logging.CRITICAL)
# Fasters hashing
PASSWORD_HASHERS = (
'django.contrib.auth.hashers.MD5PasswordHasher',
)
# Strip unneeded apps
[INSTALLED_APPS.remove(x) for x in (
'corsheaders',
)]
# Strip unneeded middlewares
[MIDDLEWARE.remove(x) for x in (
'corsheaders.middleware.CorsMiddleware',
'django.middleware.http.ConditionalGetMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)]
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment