|
16 | 16 |
|
17 | 17 | from __future__ import annotations |
18 | 18 |
|
| 19 | +import ast |
19 | 20 | import copy |
| 21 | +import subprocess |
20 | 22 | import sys |
21 | 23 | import warnings |
22 | 24 | from typing import Any |
@@ -695,5 +697,33 @@ def test_validate_uri_srv_structure(self): |
695 | 697 | ) |
696 | 698 |
|
697 | 699 |
|
| 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 | + |
698 | 728 | if __name__ == "__main__": |
699 | 729 | unittest.main() |
0 commit comments