Skip to content

Commit 5b962fe

Browse files
authored
Merge pull request #1749 from tisnik/lcore-2264
LCORE-2264: Unit tests for checking the model for a class PostgreSQLDatabaseConfiguration
2 parents d2800b5 + 2970aa6 commit 5b962fe

1 file changed

Lines changed: 102 additions & 4 deletions

File tree

tests/unit/models/config/test_postgresql_database_configuration.py

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ def test_postgresql_database_configuration() -> None:
1717
"""Test the PostgreSQLDatabaseConfiguration model."""
1818
# pylint: disable=no-member
1919
c = PostgreSQLDatabaseConfiguration(
20-
db="db", user="user", password="password"
20+
db="db",
21+
user="user",
22+
password="password",
2123
) # pyright: ignore[reportCallIssue]
24+
25+
# most attributes are set to default values
2226
assert c is not None
2327
assert c.host == "localhost"
2428
assert c.port == 5432
@@ -31,6 +35,52 @@ def test_postgresql_database_configuration() -> None:
3135
assert c.ca_cert_path is None
3236

3337

38+
def test_postgresql_database_configuration_missing_values(subtests: SubTests) -> None:
39+
"""Test the PostgreSQLDatabaseConfiguration model."""
40+
with subtests.test(msg="Missing 'db' attribute"):
41+
with pytest.raises(ValueError, match="Field required"):
42+
PostgreSQLDatabaseConfiguration(
43+
user="user",
44+
password="password",
45+
) # pyright: ignore[reportCallIssue]
46+
47+
with subtests.test(msg="Missing 'user' attribute"):
48+
with pytest.raises(ValueError, match="Field required"):
49+
PostgreSQLDatabaseConfiguration(
50+
db="db",
51+
password="password",
52+
) # pyright: ignore[reportCallIssue]
53+
54+
with subtests.test(msg="Missing 'password' attribute"):
55+
with pytest.raises(ValueError, match="Field required"):
56+
PostgreSQLDatabaseConfiguration(
57+
db="db",
58+
user="user",
59+
) # pyright: ignore[reportCallIssue]
60+
61+
with subtests.test(msg="Missing 'db' and 'user' attributes"):
62+
with pytest.raises(ValueError, match="Field required"):
63+
PostgreSQLDatabaseConfiguration(
64+
password="password",
65+
) # pyright: ignore[reportCallIssue]
66+
67+
with subtests.test(msg="Missing 'id' and 'user' attributes"):
68+
with pytest.raises(ValueError, match="Field required"):
69+
PostgreSQLDatabaseConfiguration(
70+
password="password",
71+
) # pyright: ignore[reportCallIssue]
72+
73+
with subtests.test(msg="Missing 'user' and 'password' attributes"):
74+
with pytest.raises(ValueError, match="Field required"):
75+
PostgreSQLDatabaseConfiguration(
76+
db="db",
77+
) # pyright: ignore[reportCallIssue]
78+
79+
with subtests.test(msg="Missing all required attributes"):
80+
with pytest.raises(ValueError, match="Field required"):
81+
PostgreSQLDatabaseConfiguration() # pyright: ignore[reportCallIssue]
82+
83+
3484
def test_postgresql_database_configuration_namespace_specification() -> None:
3585
"""Test the PostgreSQLDatabaseConfiguration model.
3686
@@ -46,6 +96,8 @@ def test_postgresql_database_configuration_namespace_specification() -> None:
4696
c = PostgreSQLDatabaseConfiguration(
4797
db="db", user="user", password="password", namespace="foo"
4898
) # pyright: ignore[reportCallIssue]
99+
100+
# most attributes are set to default values
49101
assert c is not None
50102
assert c.host == "localhost"
51103
assert c.port == 5432
@@ -70,21 +122,57 @@ def test_postgresql_database_configuration_port_setting(subtests: SubTests) -> N
70122
"""
71123
with subtests.test(msg="Correct port value"):
72124
c = PostgreSQLDatabaseConfiguration(
73-
db="db", user="user", password="password", port=1234
125+
db="db",
126+
user="user",
127+
password="password",
128+
port=1234,
74129
) # pyright: ignore[reportCallIssue]
75130
assert c is not None
76131
assert c.port == 1234
77132

78133
with subtests.test(msg="Negative port value"):
79134
with pytest.raises(ValidationError, match="Input should be greater than 0"):
80135
PostgreSQLDatabaseConfiguration(
81-
db="db", user="user", password="password", port=-1
136+
db="db",
137+
user="user",
138+
password="password",
139+
port=-1,
140+
) # pyright: ignore[reportCallIssue]
141+
142+
with subtests.test(msg="Zero port value"):
143+
with pytest.raises(ValidationError, match="Input should be greater than 0"):
144+
PostgreSQLDatabaseConfiguration(
145+
db="db",
146+
user="user",
147+
password="password",
148+
port=0,
82149
) # pyright: ignore[reportCallIssue]
83150

84151
with subtests.test(msg="Too big port value"):
85152
with pytest.raises(ValueError, match="Port value should be less than 65536"):
86153
PostgreSQLDatabaseConfiguration(
87-
db="db", user="user", password="password", port=100000
154+
db="db",
155+
user="user",
156+
password="password",
157+
port=100000,
158+
) # pyright: ignore[reportCallIssue]
159+
160+
with subtests.test(msg="Non integer port value"):
161+
with pytest.raises(ValueError, match="Input should be a valid integer"):
162+
PostgreSQLDatabaseConfiguration(
163+
db="db",
164+
user="user",
165+
password="password",
166+
port="xyzzy",
167+
) # pyright: ignore[reportCallIssue]
168+
169+
with subtests.test(msg="Null port value"):
170+
with pytest.raises(ValueError, match="Input should be a valid integer"):
171+
PostgreSQLDatabaseConfiguration(
172+
db="db",
173+
user="user",
174+
password="password",
175+
port=None,
88176
) # pyright: ignore[reportCallIssue]
89177

90178

@@ -121,3 +209,13 @@ def test_postgresql_database_configuration_ca_cert_path(subtests: SubTests) -> N
121209
port=1234,
122210
ca_cert_path=Path("not a file"),
123211
) # pyright: ignore[reportCallIssue]
212+
213+
with subtests.test(msg="Path is empty"):
214+
with pytest.raises(ValidationError, match="Path does not point to a file"):
215+
PostgreSQLDatabaseConfiguration(
216+
db="db",
217+
user="user",
218+
password="password",
219+
port=1234,
220+
ca_cert_path=Path(""),
221+
) # pyright: ignore[reportCallIssue]

0 commit comments

Comments
 (0)