Skip to content

Commit 9aed62e

Browse files
committed
[tests] Add test for write_service_to_text() as well as for the issue #46 ('%' in passwords)
1 parent 4b44400 commit 9aed62e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

test/data/service_base.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ dbname=db_3
1818
port=3333
1919
user=user_3
2020
password=pwd_3
21+
22+
[service_4]
23+
host=my-project-db-65b82f87-f737-4d69-8cf4-1dd7cd8.withoutclouds.com
24+
dbname=db_4 # My comment
25+
port=4444
26+
user=user_4
27+
password=pwd_%4 # Test for issue #46
28+
sslkey=/home/s.key

test/test_lib.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
55
Usage from the repo root folder:
66
7+
export PGSERVICEPARSER_SRC_DIR=$pwd
78
python -m unittest test.test_lib
89
910
For a specific test:
1011
12+
export PGSERVICEPARSER_SRC_DIR=$pwd
1113
python -m unittest test.test_lib.TestLib.test_remove_service
1214
1315
"""
@@ -25,6 +27,7 @@
2527
service_names,
2628
write_service,
2729
write_service_setting,
30+
write_service_to_text,
2831
)
2932
from pgserviceparser.exceptions import ServiceFileNotFound, ServiceNotFound
3033

@@ -49,7 +52,7 @@ def test_full_config(self):
4952
self.assertRaises(ServiceFileNotFound, full_config, "non_existing_file")
5053

5154
def test_service_names(self):
52-
self.assertEqual(service_names(), ["service_1", "service_2", "service_3"])
55+
self.assertEqual(service_names(), ["service_1", "service_2", "service_3", "service_4"])
5356

5457
def test_service_config(self):
5558
self.assertRaises(ServiceNotFound, service_config, "non_existing_service")
@@ -109,6 +112,13 @@ def test_write_service(self):
109112
self.assertIsInstance(new_srv, dict)
110113
self.assertIn("service_4", service_names())
111114

115+
def test_write_service_to_text(self):
116+
config_4 = service_config("service_4")
117+
res = write_service_to_text("service_4", config_4)
118+
expected = "[service_4]\nhost=my-project-db-65b82f87-f737-4d69-8cf4-1dd7cd8.withoutclouds.com\ndbname=db_4 # My comment\nport=4444\nuser=user_4\npassword=pwd_%4 # Test for issue #46\nsslkey=/home/s.key"
119+
120+
self.assertEqual(res, expected)
121+
112122
def test_create_pgservice_file(self):
113123
# Create a new service file
114124
new_service_file_path: Path = PGSERVICEPARSER_SRC_PATH / "test" / "data" / "notexisting" / "pgservice.conf"

0 commit comments

Comments
 (0)