Skip to content

Commit

Permalink
Merge branch 'tests_for_moves' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
shuttle1987 committed Mar 12, 2016
2 parents befd132 + 6b1cb3e commit efc9430
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 21 deletions.
8 changes: 5 additions & 3 deletions apps/footbagmoves/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ def clean(self):
if not any(cleaned_data and not cleaned_data.get('DELETE', False)
for cleaned_data in self.cleaned_data):
raise forms.ValidationError('At least one item is required.')
components_entered = set()
sequences_entered = set()
for component in self.cleaned_data:
seq_number = component.get('sequence_number', False)
if seq_number and seq_number in components_entered:
if seq_number is False:
raise forms.ValidationError('A sequence number was not provided, each component must be given a sequence number in the move')
elif seq_number in sequences_entered:
raise forms.ValidationError('A sequence number was repeated, sequence numbers must be unique')
else:
components_entered.add(seq_number)
sequences_entered.add(seq_number)


class MoveEditForm(forms.ModelForm):
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from django.test import TestCase

from apps.footbagmoves.video_api_helpers import extract_yt_id
from apps.footbagmoves.models import Component, YOUTUBE_VIDEO_TYPE
from apps.footbagmoves.forms import VideoEntryForm
from apps.footbagmoves.models import YOUTUBE_VIDEO_TYPE

class YoutubeIDExtraction(TestCase):
"""Test functionality that extracts the youtube ID from a youtube URL"""
Expand Down Expand Up @@ -42,19 +41,3 @@ def test_valid_youtube_video(self):
}
form = VideoEntryForm(data=form_data)
self.assertTrue(form.is_valid())

class ComponentCreationTest(TestCase):
"""Test that we can successfully create a component"""
def test_creating_component_and_saving_to_db(self):
"""Test that we can create a component and save it to the database"""
comp1 = Component()
comp1.name = "Toe stall"
comp1.save()

all_components_in_db = Component.objects.all()
self.assertEquals(len(all_components_in_db), 1)
only_component_in_db = all_components_in_db[0]
self.assertEquals(only_component_in_db, comp1)

#Test the component saved it's name properly in the DB
self.assertEquals(only_component_in_db.name, "Toe stall")
124 changes: 124 additions & 0 deletions apps/footbagmoves/tests/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
from django.test import TestCase

from apps.footbagmoves.models import Component, Move, MoveComponentSequence

class ComponentCreationTest(TestCase):
"""Test that we can successfully create a component"""
def test_creating_component_and_saving_to_db(self):
"""Test that we can create a component and save it to the database"""
comp1 = Component()
comp1.name = "Toe stall"
comp1.save()

all_components_in_db = Component.objects.all()
self.assertEquals(len(all_components_in_db), 1)
only_component_in_db = all_components_in_db[0]
self.assertEquals(only_component_in_db, comp1)

#Test the component saved it's name properly in the DB
self.assertEquals(only_component_in_db.name, "Toe stall")

class MoveCreationTest(TestCase):
"""Tests for creating footbag moves"""

def setUp(self):
"""Set up components for use wiht Move tests"""
self.component_toe_kick = Component(name="Toe kick")
self.component_toe_kick.save()

def test_creating_move(self):
"""Test we can create a move and save it"""
move_toe_kick = Move(name="Toe kick")
move_toe_kick.save()
component_sequence = MoveComponentSequence(
sequence_number=0,
component=self.component_toe_kick,
move=move_toe_kick
)
component_sequence.save()

all_moves_in_db = Move.objects.all()
self.assertEquals(len(all_moves_in_db), 1)
only_move_in_db = all_moves_in_db[0]
self.assertEquals(only_move_in_db, move_toe_kick)
#Test the component saved it's name properly in the DB
self.assertEquals(only_move_in_db.name, "Toe kick")

def duplicate_component_should_fail_validation(self):
"""
Test that a move with more than one component with the same sequence
number should fail validation
"""
pass


from apps.footbagmoves.edit_views import ComponentSequenceFormset
class MoveComponentSequences(TestCase):
"""Tests for the move component sequences"""

def setUp(self):
self.component_toe_kick = Component(name="Toe kick")
self.component_toe_kick.save()
self.test_move = Move(name='test move')
self.test_move.save()

def form_data(self, move, component0_id, component0_seq_number, component1_id, component1_seq_number):
data = {
'form-TOTAL_FORMS': 2,
'form-INITIAL_FORMS': 0,
'form-MIN_NUM_FORMS': 0,
'form-MAM_NUM_FORMS': 15,
'form-0-sequence_number': component0_seq_number,
'form-0-component': component0_id,
'form-1-sequence_number': component1_seq_number,
'form-1-component': component1_id,
}
return ComponentSequenceFormset(
data,
instance = move,
prefix = 'form'
)

def test_valid_data(self):
"""Test that a valid move passes validation"""
form = self.form_data(
self.test_move,
self.component_toe_kick.id,
0,
self.component_toe_kick.id,
1
)
self.assertTrue(form.is_valid())

def test_duplicated_sequence(self):
"""Test that a move with duplicated sequence fails validation"""
form = self.form_data(
self.test_move,
self.component_toe_kick.id,
0,
self.component_toe_kick.id,
0
)
self.assertFalse(form.is_valid())

def test_invalid_sequence_number(self):
"""Test that a move with invalid sequence fails validation"""
form = self.form_data(
self.test_move,
self.component_toe_kick.id,
-1,
self.component_toe_kick.id,
0
)
self.assertFalse(form.is_valid())

def test_missing_sequence_number(self):
"""Test that a move with missing sequence fails validation"""
form = self.form_data(
self.test_move,
self.component_toe_kick.id,
"",
self.component_toe_kick.id,
1
)
self.assertFalse(form.is_valid())

0 comments on commit efc9430

Please sign in to comment.