diff options
-rw-r--r-- | gn_libs/mysqldb.py | 7 | ||||
-rw-r--r-- | tests/unit/test_mysqldb.py | 4 |
2 files changed, 9 insertions, 2 deletions
diff --git a/gn_libs/mysqldb.py b/gn_libs/mysqldb.py index c738d96..d620b11 100644 --- a/gn_libs/mysqldb.py +++ b/gn_libs/mysqldb.py @@ -24,6 +24,11 @@ def __parse_boolean__(val: str) -> bool: return val.strip().lower() in true_vals +def __non_negative_int__(val: str) -> int: + """Convert a value to a non-negative int.""" + _val = int(val) + assert (val >= 0), f"Expected a non-negative value. Got {_val}" + return _val def __parse_db_opts__(opts: str) -> dict: """Parse database options into their appropriate values. @@ -36,7 +41,7 @@ def __parse_db_opts__(opts: str) -> dict: "local_infile", "autocommit", "binary_prefix") conversion_fns: dict[str, Callable] = { **{opt: str for opt in allowed_opts}, - "connect_timeout": int, + "connect_timeout": __non_negative_int__, "compress": __parse_boolean__, "use_unicode": __parse_boolean__, # "cursorclass": __load_cursor_class__ diff --git a/tests/unit/test_mysqldb.py b/tests/unit/test_mysqldb.py index b75f1e0..153f322 100644 --- a/tests/unit/test_mysqldb.py +++ b/tests/unit/test_mysqldb.py @@ -86,7 +86,9 @@ def test_parse_db_url_with_invalid_options(sql_uri, invalidopt): @pytest.mark.parametrize( "sql_uri", (("mysql://auser:passwd@somehost:3307/thedb?use_unicode=fire"), - ("mysql://auser:passwd@somehost:3307/thedb?use_unicode=3"))) + ("mysql://auser:passwd@somehost:3307/thedb?use_unicode=3"), + ("mysql://auser:passwd@somehost:3307/thedb?connect_timeout=-30"), + ("mysql://auser:passwd@somehost:3307/thedb?connect_timeout=santa"))) def test_parse_db_url_with_invalid_options_values(sql_uri): """Test parsing with invalid options' values.""" with pytest.raises(InvalidOptionValue) as iov: |