1
|
|
|
# -*- coding: utf-8 - |
2
|
|
|
|
3
|
|
|
"""Tests of the component module. |
4
|
|
|
|
5
|
|
|
SPDX-License-Identifier: MIT |
6
|
|
|
""" |
7
|
|
|
|
8
|
|
|
from configparser import NoSectionError |
9
|
|
|
from unittest.mock import MagicMock |
10
|
|
|
import getpass |
11
|
|
|
import os |
12
|
|
|
|
13
|
|
|
import keyring |
14
|
|
|
import pytest |
15
|
|
|
|
16
|
|
|
from oemof.db import connect |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
def test_url_with_keyring(): |
20
|
|
|
os.chdir(os.path.dirname(__file__)) |
21
|
|
|
config_fn = "connect1.ini" |
22
|
|
|
keyring.get_password = MagicMock(return_value="super_secure_pw") |
23
|
|
|
keyring.set_password = MagicMock() |
24
|
|
|
es = "postgresql+psycopg2://my_name:[email protected]:815/my_db" |
25
|
|
|
assert es == connect.url(section="db", config_file=config_fn) |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def test_url_without_keyring_pw_typed_in(): |
29
|
|
|
config_fn = "connect1.ini" |
30
|
|
|
keyring.get_password = MagicMock(return_value=None) |
31
|
|
|
getpass.getpass = MagicMock(return_value="type_in_pw") |
32
|
|
|
es = "postgresql+psycopg2://my_name:[email protected]:815/my_db" |
33
|
|
|
assert es == connect.url(section="db", config_file=config_fn) |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def test_url_without_keyring_pw_from_ini(): |
37
|
|
|
config_fn = os.path.join(os.getcwd(), "connect2.ini") |
38
|
|
|
keyring.get_password = MagicMock(return_value=None) |
39
|
|
|
es = "postgresql+psycopg2://my_name:[email protected]:815/my_db" |
40
|
|
|
assert es == connect.url(section="db", config_file=config_fn) |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def test_url_missing_section_in_ini(): |
44
|
|
|
keyring.get_password = MagicMock( |
45
|
|
|
side_effect=NoSectionError(section="dsfa")) |
46
|
|
|
msg = "No section: 'There is no section db in your config file." |
47
|
|
|
with pytest.raises(NoSectionError, match=msg): |
48
|
|
|
connect.url(section="db") |
49
|
|
|
|