aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2021-04-18 19:43:45 +0200
committerFlorian Fischer <florian.fl.fischer@fau.de>2021-04-18 19:43:45 +0200
commit90d130393faecc48810dfe6daac0e54f101ce713 (patch)
treecadd293bc35902c555c3bf4c5532551f67d8c37f
parent478ff36d8e7eccfe19106d0c08a8c0db4ba493bd (diff)
downloadgeldschieberbot-90d130393faecc48810dfe6daac0e54f101ce713.tar.gz
geldschieberbot-90d130393faecc48810dfe6daac0e54f101ce713.zip
[cars] add remove command
-rw-r--r--geldschieberbot.py20
-rwxr-xr-xtest.py18
2 files changed, 37 insertions, 1 deletions
diff --git a/geldschieberbot.py b/geldschieberbot.py
index 27f87ae..875ab30 100644
--- a/geldschieberbot.py
+++ b/geldschieberbot.py
@@ -168,6 +168,12 @@ def add_to_balance(name):
balance[name] = nb
+def remove_from_balance(name):
+ del balance[name]
+ for m in balance:
+ del balance[m][name]
+
+
def create_help():
return """
Usage: send a message starting with '!' followed by a command
@@ -191,6 +197,7 @@ cars [cmd] - interact with the available cars
cars [list | ls] - list available cars and their service charge
cars add car-name service-charge - add new car
cars new car-name service-charge - add new car
+cars remove car-name - remove new car
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
@@ -482,6 +489,19 @@ def cars(sender, args, msg):
available_cars[car] = service_charge
add_to_balance(car)
return f'added "{car}" as an available car', None
+
+ # remove car
+ if args[1] in ["rm", "remove"]:
+ if len(args) < 3:
+ return None, f'not in form "{args[0]} {args[1]} car-name"'
+
+ car = args[2]
+ if car not in available_cars:
+ return None, f'A car with the name "{car}" does not exists'
+
+ del available_cars[car]
+ remove_from_balance(car)
+ return f'removed "{car}" from the available cars', None
# pay bill
if args[1] in ["pay"]:
diff --git a/test.py b/test.py
index 86ca3db..3f74559 100755
--- a/test.py
+++ b/test.py
@@ -406,7 +406,7 @@ alice:
self.assertEqual(res.stdout, msg)
-class TestCarsAddCmd(unittest.TestCase):
+class TestCarsAdd(unittest.TestCase):
def setUp(self):
reset_state("test/state.json_3users")
@@ -471,6 +471,22 @@ foo:
self.assertEqual(res.stdout, o)
+class TestCarsRemove(unittest.TestCase):
+ def setUp(self):
+ reset_state("test/state.json_2cars")
+
+ def test_schieb_car(self):
+ i = "!cars remove foo"
+ res = run_bot(self, num[alice], i)
+ o = 'removed "foo" from the available cars'
+ self.assertEqual(res.stdout, o)
+
+ i = "!sum foo"
+ res = run_bot(self, num[alice], i)
+ o = 'ERROR: name "foo" not registered'
+ self.assertEqual(res.stdout, o)
+
+
class TestCarPayCmd(unittest.TestCase):
def setUp(self):
reset_state("test/state.json_2cars")