diff options
author | Frederick Muriuki Muriithi | 2024-11-21 11:39:09 -0600 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-11-21 11:46:27 -0600 |
commit | eb99148991a5869cf10c241aeff6c47708986a3a (patch) | |
tree | fc97a78e1f7217327018bfb2ca431f8e70caba2a /tests/unit | |
parent | efc6523f330205dd57be9fb9af58abea489d3ccb (diff) | |
download | gn-libs-eb99148991a5869cf10c241aeff6c47708986a3a.tar.gz |
Add tests for gn_libs.mysqldb module
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/test_mysqldb.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/tests/unit/test_mysqldb.py b/tests/unit/test_mysqldb.py new file mode 100644 index 0000000..d880e6c --- /dev/null +++ b/tests/unit/test_mysqldb.py @@ -0,0 +1,83 @@ +"""Tests for function in ....mysqldb module""" +"""module contains test for db_utils""" +from unittest import mock + +import pytest + +from gn_libs.mysqldb import parse_db_url, database_connection + +@pytest.mark.unit_test +@mock.patch("gn_libs.mysqldb.mdb") +@mock.patch("gn_libs.mysqldb.parse_db_url") +def test_database_connection(mock_db_parser, mock_sql): + """test for creating database connection""" + mock_db_parser.return_value = { + "host": "localhost", + "user": "guest", + "password": "4321", + "database": "users", + "port": 3306 + } + + mock_sql.Error = Exception + with database_connection("mysql://guest:4321@localhost/users") as _conn: + mock_sql.connect.assert_called_with( + db="users", user="guest", passwd="4321", host="localhost", + port=3306) + + +@pytest.mark.unit_test +@pytest.mark.parametrize( + "sql_uri,expected", + (("mysql://theuser:passwd@thehost:3306/thedb", + { + "host": "thehost", + "port": 3306, + "user": "theuser", + "password": "passwd", + "database": "thedb" + }), + (("mysql://auser:passwd@somehost:3307/thedb?" + "unix_socket=/run/mysqld/mysqld.sock&connect_timeout=30"), + { + "host": "somehost", + "port": 3307, + "user": "auser", + "password": "passwd", + "database": "thedb", + "unix_socket": "/run/mysqld/mysqld.sock", + "connect_timeout": 30 + }), + ("mysql://guest:4321@localhost/users", + { + "host": "localhost", + "port": 3306, + "user": "guest", + "password": "4321", + "database": "users" + }), + ("mysql://localhost/users", + { + "host": "localhost", + "port": 3306, + "user": None, + "password": None, + "database": "users" + }))) +def test_parse_db_url(sql_uri, expected): + """Test that valid URIs are passed into valid connection dicts""" + assert parse_db_url(sql_uri) == expected + + +@pytest.mark.unit_test +@pytest.mark.parametrize( + "sql_uri,invalidopt", + (("mysql://localhost/users?socket=/run/mysqld/mysqld.sock", "socket"), + ("mysql://localhost/users?connect_timeout=30¬avalidoption=value", + "notavalidoption"))) +def test_parse_db_url_with_invalid_options(sql_uri, invalidopt): + """Test that invalid options cause the function to raise an exception.""" + with pytest.raises(AssertionError) as exc_info: + parse_db_url(sql_uri) + + assert exc_info.value.args[0] == f"Invalid database connection option ({invalidopt}) provided." |