Skip to content

Commit

Permalink
Finish initial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
happy5214 committed Oct 12, 2015
1 parent f67d443 commit 6338855
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
9 changes: 5 additions & 4 deletions competitions/match/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ class MatchConfig(object):

"""Match configuration singleton class."""

created = False

def __init__(self):
"""Constructor."""
if hasattr(self, 'created'):
if MatchConfig.created:
raise RuntimeError
self.created = True
MatchConfig.created = True
self._base_matches = {}
for match in pkg_resources.iter_entry_points(group='competitions.match.base'):
self._base_matches.update({match.name: match.load()})
Expand All @@ -41,8 +43,7 @@ def base_match(self):

@base_match.setter
def base_match(self, match_def):
if match_def in self._base_matches.keys():
self._base_match = match_def
self._base_match = match_def


config = MatchConfig()
36 changes: 35 additions & 1 deletion tests/match_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from . import TestCase, TestMatch

from competitions.match import config
from competitions.match import MatchConfig, config
from competitions.match.default.SimpleMatch import SimpleMatch


Expand All @@ -33,3 +33,37 @@ def test_basic(self):
self.assertEqual(config.base_match, SimpleMatch)
config.base_match = 'competitions.test'
self.assertEqual(config.base_match, TestMatch)
config.base_match = 'competitions.unused'
self.assertIsNone(config.base_match)

def test_singleton(self):
"""Test to ensure the configuration object's singleton status."""
self.assertRaises(RuntimeError, MatchConfig)


class TestSimpleMatch(TestCase):

"""Basic sanity checks for SimpleMatch."""

def test_simulation(self):
"""Test SimpleMatch construction and simulation."""
config.base_match = 'competitions.simple'
team1 = 'Team 1'
team2 = 'Team 2'
for __ in range(1000):
match = config.base_match(team1, team2)
self.assertIsInstance(match, SimpleMatch, 'Wrong class.')
self.assertEqual(match.team1, team1)
self.assertEqual(match.team2, team2)
match.play()
if match.score1 > match.score2:
self.assertEqual(match.winner, team1)
elif match.score2 > match.score1:
self.assertEqual(match.winner, team2)
else:
self.assertIsNone(match.winner)
shortstr = '{}-{}'.format(match.score1, match.score2)
self.assertEqual(match.shortstr(), shortstr)
longstr = '{} {} - {} {}'.format(team1, match.score1, match.score2,
team2)
self.assertEqual(str(match), longstr)

0 comments on commit 6338855

Please sign in to comment.