Files
kasu/src/mahjong_ranking/tests.py
Xeniac d62f549a30 Squashed commit of the following:
commit bb5081a78b
Author: Xeniac <xeniac@posteo.at>
Date:   Thu Nov 23 22:02:40 2017 +0100

    Added a setting where the exported excel files should be stored.
    Added a option to send the exported excel as mail attachment.

commit 854fd38740
Author: Xeniac <xeniac@posteo.at>
Date:   Thu Nov 23 22:01:38 2017 +0100

    Fixed: enumerate the Seasonrankings starting with 1
    Fixed: Logging error when a value changed from/to None

commit 6de1ecb102
Author: Christian Berg <xeniac@posteo.at>
Date:   Thu Nov 23 14:15:36 2017 +0100

    add a latest method to query the latest x events

commit bf12060c3b
Author: Christian Berg <xeniac@posteo.at>
Date:   Thu Nov 23 14:15:12 2017 +0100

    add a latest method to query the latest x events

commit 5ad628f33a
Author: Christian Berg <xeniac@posteo.at>
Date:   Mon Nov 20 07:47:47 2017 +0100

    Changed PlayerDanScore to only list non-legacy hanchans

commit 36272c60d6
Author: Christian Berg <xeniac@posteo.at>
Date:   Mon Nov 20 07:42:44 2017 +0100

    fixed import of MIN_HANCHANS_FOR_LADDER that moved to settings

commit c428f6ed1f
Author: Christian Berg <xeniac@posteo.at>
Date:   Mon Nov 20 07:41:04 2017 +0100

    Updated docstrings for new since and until kwargs

commit 9276e97c36
Author: Christian Berg <xeniac@posteo.at>
Date:   Mon Nov 20 07:33:54 2017 +0100

    added a since parameter to the hanchan queries to return only hanchans since the give date and time

commit fd244f10e8
Author: Christian Berg <xeniac@posteo.at>
Date:   Sun Nov 19 16:55:10 2017 +0100

    new command: resetdanranking YYYY-MM-DD, sets every dan player to 1st dan with zero dan_points at the given date.

commit 0a45cf1fd8
Author: Christian Berg <xeniac@posteo.at>
Date:   Sun Nov 19 16:14:59 2017 +0100

    added new fields to KyuDanRanking that allow to pick up the calculation from the last state of the KyuDanRanking.
    last_hanchan_date: it contains the start of the latest hanchan content for this players ranking.
    wins_in_row: to save the currents wins in a row

    Added option to calcuclate rankings until a given datetime.
2017-11-23 22:26:22 +01:00

104 lines
3.7 KiB
Python

"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
import random
from django.test import TestCase
from mahjong_ranking.models import Hanchan, KyuDanRanking
class KyuDanTest(TestCase):
"""
Unittest to check if the hanchan calculation works against the rulebook.
"""
equal_attrs = ('dan', 'dan_points', 'kyu', 'kyu_points', 'good_hanchans',
'won_hanchans', 'hanchan_count')
fixtures = [
'test_membership.json',
'test_events.json',
'test_hanchans.json',
'test_event_rankings.json',
'test_kyu_dan_rankings.json'
]
def _test_recalc(self):
"""
Test if a Kyu/Dan Ranking recalculation gives the same result as stored.
:return:
"""
for ranking in KyuDanRanking.objects.all():
original = {a: getattr(ranking, a) for a in self.equal_attrs}
ranking.calculate()
for attr in self.equal_attrs:
self.assertEqual(
original[attr],
getattr(ranking, attr),
"Recalculation was different! user: {user}, attribute: "
"{attr}, original: {original}, recalc: {recalc}".format(
user=ranking.user, attr=attr, original=original[attr],
recalc=getattr(ranking, attr))
)
def test_partial_recalc(self):
"""
Test if partial recalclulation gives the same results.
:return:
"""
for ranking in KyuDanRanking.objects.all():
original = {a: getattr(ranking, a) for a in self.equal_attrs}
confirmed_hanchans = Hanchan.objects.confirmed(
user=ranking.user,
since=ranking.legacy_date
)
if not confirmed_hanchans.count():
continue
rnd = random.randrange(confirmed_hanchans.count())
since = confirmed_hanchans[rnd].start
ranking.calculate(since=since)
for attr in self.equal_attrs:
self.assertEqual(
original[attr],
getattr(ranking, attr),
"partial Recalculation was different! user: {user}, "
"attribute: {attr}, original: {original}, recalc: "
"{recalc}".format(
user=ranking.user, attr=attr, original=original[attr],
recalc=getattr(ranking, attr))
)
def test_points_sum(self):
"""
Test if the sum of the kyu / dan points equals the value in the Ranking.
:return: None
"""
for ranking in KyuDanRanking.objects.all():
dan_kyu = 'dan_points' if ranking.dan else 'kyu_points'
points = {
'dan_points': ranking.legacy_dan_points or 0,
'kyu_points': ranking.legacy_kyu_points or 0
}
confirmed_hanchans = Hanchan.objects.confirmed(
user=ranking.user,
since=ranking.legacy_date
)
for hanchan in confirmed_hanchans:
hanchan.get_playerdata(ranking.user)
points[dan_kyu] += getattr(hanchan, dan_kyu) or 0
self.assertEqual(
points[dan_kyu],
getattr(ranking, dan_kyu),
"{dan_kyu} for {player} won't compute! ranking: {ranking}, sum: {sum}".format(
dan_kyu=dan_kyu, player=ranking.user,
ranking=getattr(ranking, dan_kyu), sum=points[dan_kyu])
)