Skip to content

Commit f26360d

Browse files
authored
PYTHON-5727 Add tests for __main__ blocks (#2936)
1 parent a0ba73f commit f26360d

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

test/test_uri_parser.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
from __future__ import annotations
1818

19+
import ast
1920
import copy
21+
import subprocess
2022
import sys
2123
import warnings
2224
from typing import Any
@@ -695,5 +697,33 @@ def test_validate_uri_srv_structure(self):
695697
)
696698

697699

700+
class TestMainBlock(unittest.TestCase):
701+
def test_valid_uri_prints_parsed_dict(self):
702+
# Run uri_parser.py as a script; a valid URI is pretty-printed.
703+
result = subprocess.run(
704+
[sys.executable, "-m", "pymongo.uri_parser", "mongodb://localhost:27017/mydb"],
705+
capture_output=True,
706+
text=True,
707+
timeout=15,
708+
)
709+
self.assertEqual(0, result.returncode)
710+
# The output is a valid Python literal; parse it and assert on values
711+
# rather than the formatting of the pretty-printed text.
712+
parsed = ast.literal_eval(result.stdout)
713+
self.assertEqual("mydb", parsed["database"])
714+
self.assertEqual([("localhost", 27017)], parsed["nodelist"])
715+
716+
def test_invalid_uri_prints_error(self):
717+
# An invalid URI is caught and its message printed, still exiting 0.
718+
result = subprocess.run(
719+
[sys.executable, "-m", "pymongo.uri_parser", "not-a-valid-uri"],
720+
capture_output=True,
721+
text=True,
722+
timeout=15,
723+
)
724+
self.assertEqual(0, result.returncode)
725+
self.assertIn("Invalid URI scheme", result.stdout)
726+
727+
698728
if __name__ == "__main__":
699729
unittest.main()

0 commit comments

Comments
 (0)