Skip to content

Commit

Permalink
Merge pull request #1365 from anarkiwi/none
Browse files Browse the repository at this point in the history
test for flask handler.
  • Loading branch information
anarkiwi authored Jul 26, 2024
2 parents 4b97350 + 7a90eb5 commit a418a7b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
7 changes: 5 additions & 2 deletions gamutrf/flask_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ def reconf(self):
continue
if not hasattr(new_options, arg):
return f"no such option {arg}", 400
val_type = type(getattr(self.options, arg))
val_type = getattr(self.options, arg)
try:
setattr(new_options, arg, val_type(val))
if val_type is None:
setattr(new_options, arg, val)
else:
setattr(new_options, arg, type(val_type)(val))
except (TypeError, ValueError) as err:
return f"cannot set {arg} = {val}: {err}", 400
results = self.check_options(new_options)
Expand Down
40 changes: 40 additions & 0 deletions tests/test_flask_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/python3
import unittest

from gamutrf.flask_handler import FlaskHandler


class FakeOptions:
def __init__(self, apiport, foo, bar):
self.apiport = apiport
self.foo = foo
self.bar = bar


class FakeRequest:
def __init__(self, args):
self.args = args


class FlaskHandlerTestCase(unittest.TestCase):
def test_flask_handler(self):
def good_check_options(new_options):
return ""

def bad_check_options(new_options):
return "bad options are bad"

banned_args = []
options = FakeOptions(apiport=2048, foo=123, bar=None)
request = FakeRequest(args={"foo": "999", "bar": None, "apiport": 123})
handler = FlaskHandler(options, bad_check_options, banned_args)
handler.request = request
self.assertEqual(("bad options are bad", 400), handler.reconf())
self.assertEqual(handler.options, options)
handler.check_options = good_check_options
self.assertEqual(("reconf", 200), handler.reconf())
self.assertNotEqual(handler.options, options)


if __name__ == "__main__": # pragma: no cover
unittest.main()

0 comments on commit a418a7b

Please sign in to comment.