Skip to content

Commit

Permalink
Start working on test optimisation to speed things up a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtom5152 committed Jul 28, 2016
1 parent a69541a commit c8f52ea
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 91 deletions.
66 changes: 37 additions & 29 deletions src/xSACdb/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,34 @@ class BaseTest(TestCase):

fixtures = settings.TEST_FIXTURES

def setUp(self):
self.setUp_user()
@classmethod
def setUpTestData(cls):
cls.setUp_user()

# Hooks to allow precise setUp ordering
if hasattr(self, 'setUp_base'):
if hasattr(cls, 'setUp_base'):
# For running this prior to test setup and request prefetch
self.setUp_base()
if hasattr(self, 'setUp_test'):
cls.setUp_base()
if hasattr(cls, 'setUp_test'):
# Individual tests setup, keep flat
self.setUp_test()
cls.setUp_test()

def setUp_base(self):
@classmethod
def setUp_base(cls):
pass

def setUp_user(self):
@classmethod
def setUp_user(cls):
U = get_user_model()
user = U.objects.create_user(
email=self.EMAIL,
password=self.PASSWORD,
first_name=self.FIRST_NAME,
last_name=self.LAST_NAME,
email=cls.EMAIL,
password=cls.PASSWORD,
first_name=cls.FIRST_NAME,
last_name=cls.LAST_NAME,
)
user.save()
user.memberprofile.new_notify = False
user.memberprofile.date_of_birth = self.get_past_date()
user.memberprofile.date_of_birth = cls.get_past_date()
user.memberprofile.gender = random.choice(('m', 'f'))
user.memberprofile.address = "addr line 1\naddr line 2"
user.memberprofile.postcode = "UHU ioIJ"
Expand All @@ -55,21 +58,25 @@ def setUp_user(self):
user.memberprofile.next_of_kin_relation = testdata.get_name(1)
user.memberprofile.next_of_kin_phone = "4684564564654564"
user.memberprofile.save()
self.user = user
self.mp = user.memberprofile
cls.user = user
cls.mp = user.memberprofile

def get_random_date(self):
@staticmethod
def get_random_date():
return datetime.date.fromtimestamp(random.randrange(-2284101485, 2284101485))

def get_future_date(self):
@staticmethod
def get_future_date():
dt = testdata.get_future_datetime()
return datetime.date(dt.year, dt.month, dt.day)

def get_past_date(self):
@staticmethod
def get_past_date():
dt = testdata.get_past_datetime()
return datetime.date(max(1900, dt.year), dt.month, dt.day)

def create_a_user(self):
@staticmethod
def create_a_user():
"""Make a random user, return them"""
U = get_user_model()
user = U.objects.create_user(
Expand All @@ -88,8 +95,7 @@ def login(self, c):

def get_client(self):
"""Return a logged in and ready to go client"""
c = Client()
return self.login(c)
return self.login(self.client)


class BaseAsGroupTest(BaseTest):
Expand All @@ -106,15 +112,17 @@ def set_groups(self):


class AsGroupMixin(object):
def setUp_base(self):
super(AsGroupMixin, self).setUp_base()
self.set_groups()

def set_groups(self):
for group in self.GROUPS:
@classmethod
def setUp_base(cls):
super(AsGroupMixin, cls).setUp_base()
cls.set_groups()

@classmethod
def set_groups(cls):
for group in cls.GROUPS:
g = Group.objects.get(pk=group)
self.user.groups.add(g)
self.user.save()
cls.user.groups.add(g)
cls.user.save()


class ViewTestMixin(object):
Expand Down
42 changes: 25 additions & 17 deletions src/xsd_members/tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,26 @@ class MemberDetailTest(ViewTestMixin, AsGroupMixin, BaseTest):
url_name = 'xsd_members:MemberDetail'
view = MemberDetail

def setUp_test(self):
self.test_user = self.create_a_user()
self.url_kwargs = {'pk': self.test_user.memberprofile.pk}
@classmethod
def setUp_test(cls):
cls.test_user = cls.create_a_user()
cls.url_kwargs = {'pk': cls.test_user.memberprofile.pk}

def test_member_detail(self):
r = self.response
self.assertContains(r, self.test_user.first_name)#, html=True)
self.assertContains(r, self.test_user.last_name)#


class MemberEditTest(ViewTestMixin, AsGroupMixin, BaseTest):
GROUPS = [GROUP_MEMBERS]
url_name = 'xsd_members:MemberEdit'
view = MemberEdit

def setUp_test(self):
self.test_user = self.create_a_user()
self.url_kwargs = {'pk': self.test_user.memberprofile.pk}
@classmethod
def setUp_test(cls):
cls.test_user = cls.create_a_user()
cls.url_kwargs = {'pk': cls.test_user.memberprofile.pk}

def test_member_detail(self):
r = self.response
Expand All @@ -72,9 +75,10 @@ class MemberDeleteTest(ViewTestMixin, AsGroupMixin, BaseTest):
url_name = 'xsd_members:MemberDelete'
view = MemberDelete

def setUp_test(self):
self.test_user = self.create_a_user()
self.url_kwargs = {'pk': self.test_user.memberprofile.pk}
@classmethod
def setUp_test(cls):
cls.test_user = cls.create_a_user()
cls.url_kwargs = {'pk': cls.test_user.memberprofile.pk}

def test_member_delete(self):
r = self.response
Expand All @@ -89,8 +93,9 @@ class MemberListTest(ViewTestMixin, AsGroupMixin, BaseTest):
url_name = 'xsd_members:MemberList'
view = MemberList

def setUp_test(self):
self.test_user = self.create_a_user()
@classmethod
def setUp_test(cls):
cls.test_user = cls.create_a_user()

def test_member_in_list(self):
r = self.response
Expand All @@ -103,8 +108,9 @@ class NewMembersTest(ViewTestMixin, AsGroupMixin, BaseTest):
url_name = 'xsd_members:NewMembers'
view = NewMembers

def setUp_test(self):
self.test_user = self.create_a_user()
@classmethod
def setUp_test(cls):
cls.test_user = cls.create_a_user()

def test_member_in_list(self):
r = self.response
Expand All @@ -124,8 +130,9 @@ class MembersExpiredFormsListTest(ViewTestMixin, AsGroupMixin, BaseTest):
url_name = 'xsd_members:MembersExpiredFormsList'
view = MembersExpiredFormsList

def setUp_test(self):
self.test_user = self.create_a_user()
@classmethod
def setUp_test(cls):
cls.test_user = cls.create_a_user()

def test_member_in_list(self):
r = self.response
Expand Down Expand Up @@ -155,8 +162,9 @@ class TokenInputAPITest(ViewTestMixin, AsGroupMixin, BaseTest):
url_name = 'xsd_members:tokeninput-json'
view = BulkAddForms

def setUp_test(self):
self.test_user = self.create_a_user()
@classmethod
def setUp_test(cls):
cls.test_user = cls.create_a_user()

def test_template_used(self):
# Does not use template, disable
Expand Down
71 changes: 37 additions & 34 deletions src/xsd_training/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,50 @@ class BaseTraineeTest(BaseTest):


class BaseInstructorTest(BaseTraineeTest):
def setUp_base(self):
self.mp.set_qualification(Qualification.objects.get(code="OWI"))
self.mp.save()
super(BaseInstructorTest, self).setUp_base()
@classmethod
def setUp_base(cls):
cls.mp.set_qualification(Qualification.objects.get(code="OWI"))
cls.mp.save()
super(BaseInstructorTest, cls).setUp_base()


class BaseTrainingTest(AsGroupMixin, BaseTest):
GROUPS = [3]


class TrainingTestToolsMixin(object):
def setUp_base(self):
self.trainingTestToolsSetUp()
super(TrainingTestToolsMixin, self).setUp_base()

def trainingTestToolsSetUp(self):
self.OD = Qualification.objects.get(code="OD")
self.SD = Qualification.objects.get(code="SD")
self.DL = Qualification.objects.get(code="DL")
self.AD = Qualification.objects.get(code="AD")
self.FC = Qualification.objects.get(code="FC")

self.PERSONAL_QUALS = [self.OD, self.SD, self.DL, self.AD, self.FC]

self.ADI = Qualification.objects.get(code="ADI")
self.PI = Qualification.objects.get(code="PI")
self.THI = Qualification.objects.get(code="THI")
self.AOWI = Qualification.objects.get(code="AOWI")
self.OWI = Qualification.objects.get(code="OWI")
self.AI = Qualification.objects.get(code="AI")
self.NI = Qualification.objects.get(code="NI")

self.INSTRUCTOR_QUALS = [self.ADI, self.PI, self.THI, self.AOWI, self.OWI,
self.AI, self.NI]

self.OO1 = Lesson.objects.get(code="OO1")
self.OO2 = Lesson.objects.get(code="OO2")
self.SO1 = Lesson.objects.get(code="SO1")

self.BOAT_HANDLING = SDC.objects.get(title="Boat Handling")
self.WRECK_APPRECIATION = SDC.objects.get(title="Wreck Appreciation")
@classmethod
def setUp_base(cls):
cls.trainingTestToolsSetUp()
super(TrainingTestToolsMixin, cls).setUp_base()

@classmethod
def trainingTestToolsSetUp(cls):
cls.OD = Qualification.objects.get(code="OD")
cls.SD = Qualification.objects.get(code="SD")
cls.DL = Qualification.objects.get(code="DL")
cls.AD = Qualification.objects.get(code="AD")
cls.FC = Qualification.objects.get(code="FC")

cls.PERSONAL_QUALS = [cls.OD, cls.SD, cls.DL, cls.AD, cls.FC]

cls.ADI = Qualification.objects.get(code="ADI")
cls.PI = Qualification.objects.get(code="PI")
cls.THI = Qualification.objects.get(code="THI")
cls.AOWI = Qualification.objects.get(code="AOWI")
cls.OWI = Qualification.objects.get(code="OWI")
cls.AI = Qualification.objects.get(code="AI")
cls.NI = Qualification.objects.get(code="NI")

cls.INSTRUCTOR_QUALS = [cls.ADI, cls.PI, cls.THI, cls.AOWI, cls.OWI,
cls.AI, cls.NI]

cls.OO1 = Lesson.objects.get(code="OO1")
cls.OO2 = Lesson.objects.get(code="OO2")
cls.SO1 = Lesson.objects.get(code="SO1")

cls.BOAT_HANDLING = SDC.objects.get(title="Boat Handling")
cls.WRECK_APPRECIATION = SDC.objects.get(title="Wreck Appreciation")

def get_trainee(self, training_for=None):
if not training_for: training_for = self.OD
Expand Down
25 changes: 14 additions & 11 deletions src/xsd_training/tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ class TrainingLessonsViewTest(TrainingTestToolsMixin, ViewTestMixin, BaseTrainee
url_name = 'xsd_training:training-lessons'
template_name = 'lessons.html'

def setUp_test(self):
self.mp.training_for = self.OD
self.mp.save()
@classmethod
def setUp_test(cls):
cls.mp.training_for = cls.OD
cls.mp.save()

def test_content(self):
r = self.response
Expand Down Expand Up @@ -217,10 +218,11 @@ class TraineeGroupUpdate(ViewTestMixin, BaseTrainingTest):
url_name = 'xsd_training:TraineeGroupUpdate'
view = TraineeGroupUpdate

def setUp_test(self):
self.TG_NAME = "TESTGROUP"
tg = TraineeGroup.objects.create(name=self.TG_NAME)
self.url_kwargs = {'pk': tg.pk}
@classmethod
def setUp_test(cls):
cls.TG_NAME = "TESTGROUP"
tg = TraineeGroup.objects.create(name=cls.TG_NAME)
cls.url_kwargs = {'pk': tg.pk}

def test_content(self):
r = self.response
Expand All @@ -230,10 +232,11 @@ class TraineeGroupDelete(ViewTestMixin, BaseTrainingTest):
url_name = 'xsd_training:TraineeGroupDelete'
view = TraineeGroupDelete

def setUp_test(self):
self.TG_NAME = testdata.get_str(64)
tg = TraineeGroup.objects.create(name=self.TG_NAME)
self.url_kwargs = {'pk': tg.pk}
@classmethod
def setUp_test(cls):
cls.TG_NAME = testdata.get_str(64)
tg = TraineeGroup.objects.create(name=cls.TG_NAME)
cls.url_kwargs = {'pk': tg.pk}

def test_delete(self):
# TODO export this as DeleteViewTestMixin
Expand Down

0 comments on commit c8f52ea

Please sign in to comment.