aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2020-09-14 17:07:22 +0200
committerFlorian Fischer <florian.fl.fischer@fau.de>2020-09-14 17:07:22 +0200
commitac224fe92b075e45fee9a59f6be20be4eb5c9694 (patch)
treec510d6b94ba8545c52d152a5d019fd777f474d18
parent2ad7fbc35c13093c3d13c796ea4fe4f03592d3db (diff)
downloadgeldschieberbot-ac224fe92b075e45fee9a59f6be20be4eb5c9694.tar.gz
geldschieberbot-ac224fe92b075e45fee9a59f6be20be4eb5c9694.zip
add list-changes command
-rw-r--r--geldschieberbot.py58
-rwxr-xr-xtest.py79
2 files changed, 137 insertions, 0 deletions
diff --git a/geldschieberbot.py b/geldschieberbot.py
index a2c5186..6f55696 100644
--- a/geldschieberbot.py
+++ b/geldschieberbot.py
@@ -191,6 +191,7 @@ cars pay car-name amount - pay a bill for the specified car
tanken amount [person] [car] [info] - calculate fuel costs, service charge and add them to the person's and car's balance respectively
fuck - rewind last change
+list-changes [n] [first] - list the last n changes starting at first
weekly name cmd - repeat cmd each week
monthly name cmd - repeat cmd each month
@@ -626,6 +627,63 @@ cmds["fuck"] = fuck
cmds["rewind"] = fuck
cmds["undo"] = fuck
+def list_changes(sender, args, msg):
+ if not sender in num2name:
+ return None, "you must register first"
+
+ sender_name = num2name[sender]
+
+ changes_to_list = 5
+ if len(args) >= 2:
+ try:
+ changes_to_list = int(args[1])
+ except ValueError:
+ return None, 'the amount of changes to list must be a number'
+
+
+ nchanges = len(changes[sender_name])
+ if nchanges == 0:
+ return "Nothing to list", None
+
+ first_to_list = nchanges - changes_to_list
+ if first_to_list < 0:
+ first_to_list = 0
+
+ if len(args) == 3:
+ try:
+ first_to_list = int(args[2]) - 1
+ except ValueError:
+ return None, 'the first change to list must be a number'
+
+ if first_to_list > nchanges:
+ return None, 'the first change to list is bigger than there are changes'
+
+ msg = ""
+ for i, change in enumerate(changes[sender_name]):
+ if i < first_to_list:
+ continue
+
+ if i >= first_to_list + changes_to_list:
+ # i is used to track how many changes we listed
+ # This change will not be listed so decrement i before extiting the loop.
+ i -= 1
+ break
+
+ msg += f'Change {i + 1}:\n'
+ msg += f'{" ".join(change[0])}\n'
+ for sender, recipient, amount in change[1:]:
+ msg += "{} {} {} {}\n".format(sender,
+ ("->" if amount < 0 else "<-"),
+ to_euro(abs(amount)),
+ recipient)
+
+ # prepend message header because we want to know how much changes we actually listed
+ msg = f'Changes from {sender_name} {first_to_list + 1}-{i + 1}\n' + msg
+
+ return msg, None
+
+cmds["list-changes"] = list_changes
+
def schedule(sender, args, msg):
if not sender in num2name:
return None, "you must register first"
diff --git a/test.py b/test.py
index 56f6949..b26fc89 100755
--- a/test.py
+++ b/test.py
@@ -708,6 +708,85 @@ New Balance: bob -> 5.00 charlie
#TODO: tanken, transfer, cars pay
+class TestListChangesCmd(unittest.TestCase):
+
+ def setUp(self):
+ reset_state("test/state.json_3users")
+
+ def test_list_changes_unregistered(self):
+ res = run_bot(self, "+4971576357", "!list-changes")
+ self.assertEqual(res.stdout, 'ERROR: you must register first')
+
+ def test_list_changes_single_change(self):
+ run_bot(self, num[alice], f"!schieb {bob} 10")
+ res = run_bot(self, num[alice], "!list-changes")
+ msg = \
+"""Changes from alice 1-1
+Change 1:
+!schieb bob 10
+alice <- 10.00 bob
+"""
+ self.assertEqual(res.stdout, msg)
+
+ def test_list_changes_single_change_invalid_amount(self):
+ run_bot(self, num[alice], f"!schieb {bob} 10")
+ res = run_bot(self, num[alice], "!list-changes foo")
+ msg = \
+"""ERROR: the amount of changes to list must be a number"""
+ self.assertEqual(res.stdout, msg)
+
+ def test_list_changes_single_change_invalid_offset(self):
+ run_bot(self, num[alice], f"!schieb {bob} 10")
+ res = run_bot(self, num[alice], "!list-changes 1 foo")
+ msg = \
+"""ERROR: the first change to list must be a number"""
+ self.assertEqual(res.stdout, msg)
+
+ res = run_bot(self, num[alice], "!list-changes 1 20")
+ msg = \
+"""ERROR: the first change to list is bigger than there are changes"""
+ self.assertEqual(res.stdout, msg)
+
+ def test_list_changes_two_changes(self):
+ run_bot(self, num[alice], f"!schieb {bob} 10")
+ run_bot(self, num[alice], f"!zieh {charlie} 5")
+ res = run_bot(self, num[alice], "!list-changes")
+ msg = \
+"""Changes from alice 1-2
+Change 1:
+!schieb bob 10
+alice <- 10.00 bob
+Change 2:
+!zieh charlie 5
+alice -> 5.00 charlie
+"""
+ self.assertEqual(res.stdout, msg)
+
+ def test_list_changes_one_of_two(self):
+ run_bot(self, num[alice], f"!schieb {bob} 10")
+ run_bot(self, num[alice], f"!zieh {charlie} 5")
+ res = run_bot(self, num[alice], "!list-changes 1")
+ msg = \
+"""Changes from alice 2-2
+Change 2:
+!zieh charlie 5
+alice -> 5.00 charlie
+"""
+ self.assertEqual(res.stdout, msg)
+
+ def test_list_changes_only_second(self):
+ run_bot(self, num[alice], f"!schieb {bob} 10")
+ run_bot(self, num[alice], f"!zieh {charlie} 5")
+ run_bot(self, num[alice], f"!split 9 {bob} {charlie}")
+ res = run_bot(self, num[alice], "!list-changes 1 2")
+ msg = \
+"""Changes from alice 2-2
+Change 2:
+!zieh charlie 5
+alice -> 5.00 charlie
+"""
+ self.assertEqual(res.stdout, msg)
+
class TestFuckCmd(unittest.TestCase):
def setUp(self):