-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix and improve error handling for missing or invalid configuration file
- Loading branch information
Showing
8 changed files
with
151 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
# This workflow will install Python dependencies | ||
# and run unit tests for given OSes | ||
|
||
name: Unit tests | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
build: | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
include: | ||
- os: 'ubuntu-latest' | ||
python-version: '3.7' | ||
rf-version: '3.2.2' | ||
- os: 'windows' | ||
python-version: '3.8' | ||
rf-version: '4.1.3' | ||
- os: 'ubuntu-latest' | ||
python-version: '3.9' | ||
rf-version: '5.0.1' | ||
- os: 'ubuntu-latest' | ||
python-version: '3.10' | ||
rf-version: '6.1.1' | ||
- os: 'ubuntu-latest' | ||
python-version: '3.11' | ||
rf-version: '6.1.1' | ||
- os: 'ubuntu-latest' | ||
python-version: '3.12' | ||
rf-version: '7.0a1' | ||
runs-on: ${{ matrix.os }} | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 2 | ||
|
||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install robotframework==${{ matrix.rf-version }} coverage pytest | ||
pip install . | ||
- name: Run unit tests with coverage | ||
run: | ||
coverage run -m pytest | ||
|
||
- name: Codecov | ||
uses: codecov/codecov-action@v3 | ||
with: | ||
name: ${{ matrix.python-version }}-${{ matrix.os }}-${{ matrix.rf-version }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import re | ||
from pathlib import Path | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
|
||
from DatabaseLibrary.connection_manager import ConnectionManager | ||
|
||
TEST_DATA = Path(__file__).parent / "test_data" | ||
|
||
|
||
class TestConnectWithConfigFile: | ||
def test_connect_with_empty_config(self): | ||
conn_manager = ConnectionManager() | ||
config_path = str(TEST_DATA / "empty.cfg") | ||
with pytest.raises(ValueError, match=re.escape("Configuration file does not have [default] section.")): | ||
conn_manager.connect_to_database("my_client", dbConfigFile=config_path) | ||
|
||
def test_connect_no_params_no_config(self): | ||
conn_manager = ConnectionManager() | ||
with pytest.raises(ValueError, match="Required 'dbName' parameter was not provided in keyword arguments."): | ||
conn_manager.connect_to_database("my_client") | ||
|
||
def test_connect_missing_option(self): | ||
conn_manager = ConnectionManager() | ||
config_path = str(TEST_DATA / "no_option.cfg") | ||
with pytest.raises( | ||
ValueError, | ||
match="Required 'dbPassword' parameter missing in both keyword arguments and configuration file.", | ||
): | ||
conn_manager.connect_to_database("my_client", dbConfigFile=config_path) | ||
|
||
def test_aliased_section(self): | ||
conn_manager = ConnectionManager() | ||
config_path = str(TEST_DATA / "alias.cfg") | ||
with patch("importlib.import_module", new=MagicMock()) as client: | ||
conn_manager.connect_to_database( | ||
"my_client", | ||
dbUsername="name", | ||
dbPassword="password", | ||
dbHost="host", | ||
dbPort=0, | ||
dbConfigFile=config_path, | ||
alias="alias2", | ||
) | ||
client.return_value.connect.assert_called_with( | ||
database="example", user="name", password="password", host="host", port=0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[alias2] | ||
dbName = example |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[default] | ||
dbName = example | ||
dbUsername = example |