to/from_dict methods

This commit is contained in:
James Turk 2012-05-28 14:06:52 -04:00
parent 93ca13a63d
commit 4ea5d98333
3 changed files with 89 additions and 2 deletions

1
TODO
View File

@ -1 +0,0 @@
* actually have belts change hands when title is on the line in a match

View File

@ -49,6 +49,24 @@ class Event(models.Model):
name = models.CharField(max_length=100)
date = models.DateField()
def to_dict(self):
d = {'name': self.name, 'date': self.date,
'matches': [m.to_dict() for m in self.matches.all()]
}
return d
@staticmethod
def from_dict(d):
event = Event.objects.create(name=d['name'], date=d['date'])
for match in d['matches']:
event.add_match(*match['teams'],
winner=match['winner'],
outcome=match['outcome'],
title_at_stake=match['title_at_stake'],
notes=match['notes'])
return event
def add_match(self, *teams, **kwargs):
winner = kwargs.get('winner', None)
outcome = kwargs.get('outcome', '')
@ -91,6 +109,15 @@ class Match(models.Model):
title_at_stake = models.CharField(max_length=50, choices=TITLES, null=True)
notes = models.TextField(blank=True, default='')
def to_dict(self):
d = {'winner': self.winner_id,
'outcome': self.outcome,
'title_at_stake': self.title_at_stake,
'notes': self.notes}
d['teams'] = [[m.id for m in team.members.all()]
for team in self.teams.all()]
return d
def record_win(self, star, outcome):
team = self.teams.get(members__pk=star)
team.victorious = True

View File

@ -1,3 +1,4 @@
import datetime
from django.test import TestCase
from django.contrib.auth.models import User
from .models import Event, League, Team, TeamPoints, Star
@ -32,6 +33,53 @@ class StarTest(TestCase):
self.assertEqual(Star.objects.get(pk='ziggler').title, 'tag')
self.assertEqual(Star.objects.get(pk='swagger').title, 'tag')
class EventTest(TestCase):
maxDiff = None
fixtures = ['testdata']
# add_match isn't explicity tested, should it be?
def test_to_dict(self):
event = Event.objects.create(name='RAW',
date='2012-01-01')
event.add_match('jimross', 'jerrylawler',
winner='jimross', outcome='submission')
event = Event.objects.get()
expected = {'name': 'RAW', 'date': datetime.date(2012,1,1),
'matches': [
{'teams': [[u'jimross'],[u'jerrylawler']],
'winner': u'jimross',
'outcome': u'submission',
'notes': '',
'title_at_stake': None,
}
]
}
self.assertEqual(event.to_dict(), expected)
def test_from_dict(self):
edict = {'name': 'RAW', 'date': datetime.date(2012,1,1),
'matches': [
{'teams': [[u'jimross'],[u'jerrylawler']],
'winner': u'jimross',
'outcome': u'submission',
'notes': '',
'title_at_stake': None,
}
]
}
event = Event.from_dict(edict)
self.assertEqual(event.name, 'RAW')
self.assertEqual(event.date, datetime.date(2012,1,1))
match_one = event.matches.all()[0]
self.assertEqual([[s.id for s in team.members.all()]
for team in match_one.teams.all()],
[[u'jimross'], [u'jerrylawler']])
self.assertEqual(match_one.winner_id, 'jimross')
self.assertEqual(match_one.outcome, 'submission')
self.assertEqual(match_one.notes, '')
self.assertEqual(match_one.title_at_stake, None)
class MatchTest(TestCase):
fixtures = ['testdata']
@ -40,7 +88,7 @@ class MatchTest(TestCase):
self.event = Event.objects.create(name='Wrestlemania 29',
date='2012-04-01')
def test_basics(self):
def test_display(self):
match = self.event.add_match('tripleh', 'undertaker')
self.assertEqual(unicode(match),
'Triple H vs. Undertaker (no contest)')
@ -52,6 +100,19 @@ class MatchTest(TestCase):
outcome='normal')
self.assertEqual(unicode(match), 'CM Punk (c) (v) vs. Rey Mysterio')
def test_to_dict(self):
match = self.event.add_match('jimross', 'jerrylawler', 'michaelcole',
winner='jimross', outcome='submission',
notes='announcer beat down')
expected = {'teams':
[['jimross'],['jerrylawler'],['michaelcole']],
'winner': 'jimross',
'outcome': 'submission',
'title_at_stake': None,
'notes': 'announcer beat down'}
self.assertEqual(match.to_dict(), expected)
def test_do_title_change(self):
# title to punk
match = self.event.add_match('cmpunk', 'reymysterio', winner='cmpunk',