-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from jrsmth/44_scroll_duplicate_fix
[#44] Prevent duplicate records for the same streak
- Loading branch information
Showing
7 changed files
with
202 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
from datetime import datetime | ||
|
||
from src.app.model.group.group import Group | ||
from src.app.model.group.player import Player | ||
from src.app.model.group.record import Record | ||
|
||
|
||
class GroupSpec: | ||
group_name = 'group_name' | ||
|
||
def should_not_update_scroll_if_new_streak_lower_than_the_lowest_record_streak(self): | ||
""" Should not update scroll if new streak lower than the lowest record streak """ | ||
# Given | ||
players = [Player('Adam', 4, '1', 1000), Player('Hayden', 3, '2', 1000), Player('James', 2, '3', 1000)] | ||
scroll = [Record('Adam', 4, '1', 'today'), Record('Hayden', 3, '2', 'today'), Record('James', 2, '3', 'today')] | ||
subject = Group(self.group_name, players, Player('Adam', 4, '1', 1000), scroll) | ||
|
||
test = Player('Maciej', 1, '4', 1000) | ||
|
||
# When | ||
subject.update_scroll(test) | ||
|
||
# Then : scroll is left untouched | ||
assert subject.scroll is scroll | ||
|
||
def should_update_scroll_if_new_streak_lower_than_the_lowest_record_streak_but_scroll_has_capacity(self): | ||
""" Should update scroll if new streak lower than the lowest record streak but scroll has capacity""" | ||
# Given | ||
players = [Player('Adam', 4, '1', 1000), Player('Hayden', 3, '2', 1000)] | ||
scroll = [Record('Adam', 4, '1', 'today'), Record('Hayden', 3, '2', 'today')] | ||
subject = Group(self.group_name, players, Player('Adam', 4, '1', 1000), scroll) | ||
|
||
test = Player('Maciej', 2, '4', 1000) | ||
# Note :: streaks are defined as being >= 2 | ||
|
||
# When | ||
subject.update_scroll(test) | ||
|
||
# Then : test is added to the scroll | ||
assert subject.scroll == [Record('Adam', 4, '1', 'today'), Record('Hayden', 3, '2', 'today'), test.get_record()] | ||
|
||
def should_update_existing_record_if_player_streak_is_active_on_the_scroll(self): | ||
""" Should update existing record if worthy player streak is active on the scroll """ | ||
# Given | ||
players = [Player('Adam', 4, '1', 1000), Player('Hayden', 3, '2', 1000), Player('James', 2, '3', 1000)] | ||
scroll = [Record('Adam', 4, '1', 'today'), Record('Hayden', 3, '2', 'today'), Record('James', 2, '3', 'today')] | ||
subject = Group(self.group_name, players, Player('Adam', 4, '1', 1000), scroll) | ||
|
||
test = Player('Adam', 5, '1', 1005) | ||
|
||
# When | ||
subject.update_scroll(test) | ||
|
||
# Then : 'Adam' Record with streak id '1' should be updated | ||
updated_record = [x for x in subject.scroll if x.streak_id == test.streak_id][0] | ||
assert updated_record.streak is 5 | ||
|
||
def should_add_new_record_if_worthy_streak_is_not_active_on_the_scroll_and_scroll_below_capacity(self): | ||
""" Should add new record if worthy streak is not active on the scroll and scroll below capacity """ | ||
# Given | ||
players = [Player('Adam', 4, '1', 1000), Player('Hayden', 3, '2', 1000)] | ||
scroll = [Record('Adam', 4, '1', 'today'), Record('Hayden', 3, '2', 'today')] | ||
subject = Group(self.group_name, players, Player('Adam', 4, '1', 1000), scroll) | ||
|
||
test = Player('Adam', 5, '3', 1005) | ||
|
||
# When | ||
subject.update_scroll(test) | ||
|
||
# Then : 'Adam' Record w/streak id '1' should be preserved; second 'Adam' Record added w/streak id '2' | ||
# Note : also expect scroll to be sorted upon update | ||
expected_record = Record('Adam', 5, '3', datetime.today().strftime('%d/%m/%Y')) | ||
expected_scroll = [expected_record, Record('Adam', 4, '1', 'today'), Record('Hayden', 3, '2', 'today')] | ||
difference = [x for x in subject.scroll if x not in expected_scroll] | ||
assert len(difference) is 0 | ||
|
||
def should_replace_an_old_record_if_worthy_streak_is_not_active_on_the_scroll_and_scroll_at_capacity(self): | ||
""" Should replace an old record if worthy streak is not active on the scroll and scroll at capacity """ | ||
# Given | ||
players = [Player('Adam', 4, '1', 1000), Player('Hayden', 3, '2', 1000), Player('James', 2, '3', 1000)] | ||
scroll = [Record('Adam', 4, '1', 'today'), Record('Hayden', 3, '2', 'today'), Record('James', 2, '3', 'today')] | ||
subject = Group(self.group_name, players, Player('Adam', 4, '1', 1000), scroll) | ||
|
||
test = Player('Adam', 5, '4', 1005) | ||
|
||
# When | ||
subject.update_scroll(test) | ||
|
||
# Then : 'Adam' Record w/streak id '1' should be preserved; second 'Adam' Record added w/streak id '2' | ||
# Note : also expect scroll to be sorted upon update | ||
expected_record = Record('Adam', 5, '4', datetime.today().strftime('%d/%m/%Y')) | ||
expected_scroll = [expected_record, Record('Adam', 4, '1', 'today'), Record('Hayden', 3, '2', 'today')] | ||
difference = [x for x in subject.scroll if x not in expected_scroll] | ||
assert len(difference) is 0 | ||
|
||
def should_dethrone_king_by_crowning_next_highest_player(self): | ||
""" Should dethrone king by crowning next highest player """ | ||
# Given | ||
players = [Player('Adam', 4, '1', 1000), Player('Hayden', 3, '2', 1000), Player('James', 2, '3', 1000)] | ||
subject = Group(self.group_name, players, Player('Adam', 4, '1', 1000), []) | ||
|
||
test = Player('Adam', 0, '1', 1000) | ||
|
||
# When | ||
subject.update_player(test) | ||
subject.dethrone() | ||
|
||
# Then : King is Hayden | ||
assert subject.king == Player('Hayden', 3, '2', 1000) | ||
|
||
def should_reset_streak_id_when_player_has_streak_of_zero(self): | ||
""" Should reset streak id when player has streak of zero """ | ||
# Given | ||
players = [Player('Adam', 4, '1', 1000)] | ||
subject = Group(self.group_name, players, Player('Adam', 4, '1', 1000), []) | ||
|
||
test = Player('Adam', 0, '1', 1000) | ||
|
||
# When | ||
subject.update_player(test) | ||
|
||
# Then : Adam should have new streak id | ||
assert [x for x in subject.players if x.name == 'Adam'][0].streak_id != '1' | ||
|
||
def should_not_reset_streak_id_when_player_has_non_zero_streak(self): | ||
""" Should not reset streak id when player has non-zero streak """ | ||
# Given | ||
players = [Player('Adam', 4, '1', 1000)] | ||
subject = Group(self.group_name, players, Player('Adam', 4, '1', 1000), []) | ||
|
||
test = Player('Adam', 5, '1', 1000) | ||
|
||
# When | ||
subject.update_player(test) | ||
|
||
# Then : Adam should have new streak id | ||
assert [x for x in subject.players if x.name == 'Adam'][0].streak_id == '1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
from dataclasses import dataclass | ||
|
||
from datetime import datetime | ||
from src.app.model.base import Base | ||
from src.app.model.group.record import Record | ||
|
||
|
||
@dataclass | ||
class Player(Base): | ||
""" Tracked player information """ | ||
name: str | ||
streak: int | ||
streak_id: str | ||
score: int | ||
|
||
@classmethod | ||
def from_dict(cls, dic): | ||
return cls( | ||
name=dic["name"], | ||
streak=dic["streak"], | ||
streak_id=dic["streak_id"], | ||
score=dic["score"] | ||
) | ||
|
||
def get_record(self): | ||
""" Convert a player into a record """ | ||
return Record(self.name, self.streak, self.streak_id, datetime.today().strftime('%d/%m/%Y')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ coverage-badge | |
pytest-cov | ||
genbadge | ||
defusedxml | ||
shortuuid |