Added get prize testcases and fixed prize calculation
This commit is contained in:
parent
9a84532042
commit
46f0c3a88b
111
billard/tests.py
111
billard/tests.py
@ -1,3 +1,112 @@
|
|||||||
|
from datetime import datetime, time
|
||||||
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
# Create your tests here.
|
from billard.utils import calculate_prize_for, get_prize_for
|
||||||
|
|
||||||
|
|
||||||
|
class PrizeCalculationTestCase(TestCase):
|
||||||
|
def calculate_prize_for(self, start, end, pph, expected):
|
||||||
|
self.assertEqual(calculate_prize_for(start, end, pph), expected)
|
||||||
|
|
||||||
|
def test_cpf_1(self):
|
||||||
|
start = datetime(2017, 1, 1, 6)
|
||||||
|
end = datetime(2017, 1, 1, 7)
|
||||||
|
pph = 10
|
||||||
|
self.calculate_prize_for(start, end, pph, pph)
|
||||||
|
|
||||||
|
def test_cpf_2(self):
|
||||||
|
start = datetime(2017, 1, 1, 6)
|
||||||
|
end = datetime(2017, 1, 1, 6, 30)
|
||||||
|
pph = 10
|
||||||
|
expected = 5
|
||||||
|
self.calculate_prize_for(start, end, pph, expected)
|
||||||
|
|
||||||
|
def test_cpf_3(self):
|
||||||
|
start = datetime(2017, 1, 1, 6)
|
||||||
|
end = datetime(2017, 1, 1, 6, 15)
|
||||||
|
pph = 10
|
||||||
|
expected = 2.5
|
||||||
|
self.calculate_prize_for(start, end, pph, expected)
|
||||||
|
|
||||||
|
def test_cpf_4(self):
|
||||||
|
start = datetime(2017, 1, 1, 6)
|
||||||
|
end = datetime(2017, 1, 1, 6, 1)
|
||||||
|
pph = 60
|
||||||
|
expected = 1
|
||||||
|
self.calculate_prize_for(start, end, pph, expected)
|
||||||
|
|
||||||
|
|
||||||
|
class PrizeGetTestCase(TestCase):
|
||||||
|
def get_prize_for(self, start, end, pph=0, hh_start=None, hh_end=None, pphh=0, expected=(0,0,0)):
|
||||||
|
self.assertEqual(get_prize_for(start, end, pph, hh_start, hh_end, pphh), expected)
|
||||||
|
|
||||||
|
def test_gpf_1(self):
|
||||||
|
start = datetime(2017, 1, 1, 6)
|
||||||
|
end = datetime(2017, 1, 1, 7)
|
||||||
|
hh_start = None
|
||||||
|
hh_end = None
|
||||||
|
pph = 10
|
||||||
|
pphh = 5
|
||||||
|
expected = (10, 10, 0)
|
||||||
|
self.get_prize_for(start, end, pph, hh_start, hh_end, pphh, expected)
|
||||||
|
|
||||||
|
def test_gpf_2(self):
|
||||||
|
start = datetime(2017, 1, 1, 6)
|
||||||
|
end = datetime(2017, 1, 1, 8)
|
||||||
|
hh_start = time(7)
|
||||||
|
hh_end = time(8)
|
||||||
|
pph = 10
|
||||||
|
pphh = 5
|
||||||
|
expected = (15, 10, 5)
|
||||||
|
self.get_prize_for(start, end, pph, hh_start, hh_end, pphh, expected)
|
||||||
|
|
||||||
|
def test_gpf_3(self):
|
||||||
|
start = datetime(2017, 1, 1, 6)
|
||||||
|
end = datetime(2017, 1, 1, 9)
|
||||||
|
hh_start = time(7)
|
||||||
|
hh_end = time(8)
|
||||||
|
pph = 10
|
||||||
|
pphh = 5
|
||||||
|
expected = (25, 20, 5)
|
||||||
|
self.get_prize_for(start, end, pph, hh_start, hh_end, pphh, expected)
|
||||||
|
|
||||||
|
def test_gpf_4(self):
|
||||||
|
start = datetime(2017, 1, 1, 6)
|
||||||
|
end = datetime(2017, 1, 2, 9)
|
||||||
|
hh_start = time(7)
|
||||||
|
hh_end = time(8)
|
||||||
|
pph = 10
|
||||||
|
pphh = 5
|
||||||
|
expected = (260, 250, 10)
|
||||||
|
self.get_prize_for(start, end, pph, hh_start, hh_end, pphh, expected)
|
||||||
|
|
||||||
|
def test_gpf_5(self):
|
||||||
|
start = datetime(2017, 1, 1, 7, 30)
|
||||||
|
end = datetime(2017, 1, 1, 8)
|
||||||
|
hh_start = time(7)
|
||||||
|
hh_end = time(8)
|
||||||
|
pph = 10
|
||||||
|
pphh = 5
|
||||||
|
expected = (2.5, 0, 2.5)
|
||||||
|
self.get_prize_for(start, end, pph, hh_start, hh_end, pphh, expected)
|
||||||
|
|
||||||
|
def test_gpf_6(self):
|
||||||
|
start = datetime(2017, 1, 1, 7, 30)
|
||||||
|
end = datetime(2017, 1, 1, 8, 30)
|
||||||
|
hh_start = time(7)
|
||||||
|
hh_end = time(8)
|
||||||
|
pph = 10
|
||||||
|
pphh = 5
|
||||||
|
expected = (7.5, 5, 2.5)
|
||||||
|
self.get_prize_for(start, end, pph, hh_start, hh_end, pphh, expected)
|
||||||
|
|
||||||
|
def test_gpf_7(self):
|
||||||
|
start = datetime(2017, 1, 1, 6, 30)
|
||||||
|
end = datetime(2017, 1, 1, 7, 30)
|
||||||
|
hh_start = time(7)
|
||||||
|
hh_end = time(8)
|
||||||
|
pph = 10
|
||||||
|
pphh = 5
|
||||||
|
expected = (7.5, 5, 2.5)
|
||||||
|
self.get_prize_for(start, end, pph, hh_start, hh_end, pphh, expected)
|
@ -18,7 +18,7 @@ def get_prize_for(start, end, pph=0, hh_start=None, hh_end=None, pphh=0):
|
|||||||
prize = 0
|
prize = 0
|
||||||
prize_normal = 0
|
prize_normal = 0
|
||||||
prize_hh = 0
|
prize_hh = 0
|
||||||
if hh_start is not None and hh_end is not None and pphh is not None:
|
if hh_start and hh_end and pphh:
|
||||||
d = start.date()
|
d = start.date()
|
||||||
t = start.time()
|
t = start.time()
|
||||||
end_date = end.date()
|
end_date = end.date()
|
||||||
@ -31,35 +31,39 @@ def get_prize_for(start, end, pph=0, hh_start=None, hh_end=None, pphh=0):
|
|||||||
prize_normal += p
|
prize_normal += p
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
p += calculate_prize_for(start=t, end=hh_start, pph=pph)
|
p = calculate_prize_for(start=t, end=hh_start, pph=pph)
|
||||||
prize += p
|
prize += p
|
||||||
prize_normal += p
|
prize_normal += p
|
||||||
t = hh_start
|
t = hh_start
|
||||||
|
continue
|
||||||
elif hh_start <= t < hh_end:
|
elif hh_start <= t < hh_end:
|
||||||
if end_time < hh_end and d == end_date:
|
if end_time < hh_end and d == end_date:
|
||||||
p += calculate_prize_for(start=t, end=end_time, pph=pphh)
|
p = calculate_prize_for(start=t, end=end_time, pph=pphh)
|
||||||
prize += p
|
prize += p
|
||||||
prize_hh += p
|
prize_hh += p
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
p += calculate_prize_for(start=t, end=hh_end, pph=pphh)
|
p = calculate_prize_for(start=t, end=hh_end, pph=pphh)
|
||||||
prize += p
|
prize += p
|
||||||
prize_hh += p
|
prize_hh += p
|
||||||
t = hh_end
|
t = hh_end
|
||||||
|
continue
|
||||||
else:
|
else:
|
||||||
if d == end_date:
|
if d == end_date:
|
||||||
p += calculate_prize_for(start=t, end=end_time, pph=pph)
|
p = calculate_prize_for(start=t, end=end_time, pph=pph)
|
||||||
prize += p
|
prize += p
|
||||||
prize_normal += p
|
prize_normal += p
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
p += calculate_prize_for(start=t, end=datetime.strptime('23:59:59', '%H:%M:%S').time(), pph=pph)
|
p = calculate_prize_for(start=t, end=datetime.strptime('23:59:59', '%H:%M:%S').time(), pph=pph)
|
||||||
prize += p
|
prize += p
|
||||||
prize_normal += p
|
prize_normal += p
|
||||||
t = datetime.strptime('00:00:00', '%H:%M:%S').time()
|
t = datetime.strptime('00:00:00', '%H:%M:%S').time()
|
||||||
d = (datetime.combine(d, t) + timedelta(days=1)).date()
|
d = (datetime.combine(d, t) + timedelta(days=1)).date()
|
||||||
|
continue
|
||||||
else:
|
else:
|
||||||
prize = calculate_prize_for(start=start, end=end, pph=pph)
|
prize = calculate_prize_for(start=start, end=end, pph=pph)
|
||||||
|
prize_normal = prize
|
||||||
return prize, prize_normal, prize_hh
|
return prize, prize_normal, prize_hh
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user