Skip to content

Commit

Permalink
added function to create Wincase class form json;
Browse files Browse the repository at this point in the history
added function to get opposite of wincase
  • Loading branch information
MKashevsky committed Nov 28, 2018
1 parent 4338e9a commit ecee326
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
55 changes: 55 additions & 0 deletions scorum/graphenebase/betting/wincase.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def __init__(self, wincase_type):
self.name = "::".join(self.get_name(wincase_type).rsplit('_', 1))
self.id = self.get_id(self.name)

def __eq__(self, other):
return str(self) == str(other)


class ResultHomeYes(GrapheneObject):
pass
Expand Down Expand Up @@ -193,3 +196,55 @@ def __init__(self, threshold=0):
self.data = OrderedDict([("threshold", Int16(threshold))])

super(TotalGoalsAwayUnder, self).__init__(self.data)


WINCASES_MAP = {
"result_home::yes": ResultHomeYes,
"result_home::no": ResultHomeNo,
"result_draw::yes": ResultDrawYes,
"result_draw::no": ResultDrawNo,
"result_away::yes": ResultAwayYes,
"result_away::no": ResultAwayNo,
"round_home::yes": RoundHomeYes,
"round_home::no": RoundHomeNo,
"handicap::over": HandicapOver,
"handicap::under": HandicapUnder,
"correct_score_home::yes": CorrectScoreHomeYes,
"correct_score_home::no": CorrectScoreHomeNo,
"correct_score_draw::yes": CorrectScoreDrawYes,
"correct_score_draw::no": CorrectScoreDrawNo,
"correct_score_away::yes": CorrectScoreAwayYes,
"correct_score_away::no": CorrectScoreAwayNo,
"correct_score::yes": CorrectScoreYes,
"correct_score::no": CorrectScoreNo,
"goal_home::yes": GoalHomeYes,
"goal_home::no": GoalHomeNo,
"goal_both::yes": GoalBothYes,
"goal_both::no": GoalBothNo,
"goal_away::yes": GoalAwayYes,
"goal_away::no": GoalAwayNo,
"total::over": TotalOver,
"total::under": TotalUnder,
"total_goals_home::over": TotalGoalsHomeOver,
"total_goals_home::under": TotalGoalsHomeUnder,
"total_goals_away::over": TotalGoalsAwayOver,
"total_goals_away::under": TotalGoalsAwayUnder
}


def create_obj_from_json(wc):
"""
:param list wc: Wincase name and type parameters, example ["handicap::over", {"threashold": 1000}]
:return: Wincase class object
"""
if len(wc) != 2:
raise ValueError("Unexpected json value was given.")
name, kwargs = wc
return Wincase(WINCASES_MAP[name](**kwargs))


def opposite(name: str):
idx = WINCASES.index(name)
if idx % 2:
return WINCASES[idx - 1]
return WINCASES[idx + 1]
20 changes: 20 additions & 0 deletions tests/graphenebase/test_betting.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import json

from binascii import hexlify

Expand Down Expand Up @@ -187,3 +188,22 @@ def test_serialize_cancel_pending_bets_to_hex():
op = ops.cancel_pending_bets([uuid], "admin")
signed_ops = SignedTransaction.cast_operations_to_array_of_opklass([op])
assert hexlify(bytes(signed_ops.data[0])) == result_bin


@pytest.mark.parametrize('wc', [
wincase.HandicapOver(-1000),
wincase.CorrectScoreNo(3, 4),
wincase.ResultDrawYes(),
wincase.TotalGoalsAwayOver(500)
])
def test_get_wincase_obj_from_json(wc):
assert Wincase(wc) == wincase.create_obj_from_json(json.loads(str(Wincase(wc))))


@pytest.mark.parametrize('name, expected', [
("result_home::yes", "result_home::no"),
("total_goals_away::under", "total_goals_away::over"),
("correct_score_draw::no", "correct_score_draw::yes")
])
def test_get_wincase_opposite(name, expected):
assert expected == wincase.opposite(name)

0 comments on commit ecee326

Please sign in to comment.