summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_uuid.py
diff options
context:
space:
mode:
authorachhina <aman.s.chhina@gmail.com>2023-01-22 06:59:31 (GMT)
committerGitHub <noreply@github.com>2023-01-22 06:59:31 (GMT)
commit95f5b05a8cad61b296807c14e50896075c4dc1de (patch)
tree165d16ec14680f38e0a8631603190125351de2b0 /Lib/test/test_uuid.py
parentc1c5882359a2899b74c1685a0d4e61d6e232161f (diff)
downloadcpython-95f5b05a8cad61b296807c14e50896075c4dc1de.zip
cpython-95f5b05a8cad61b296807c14e50896075c4dc1de.tar.gz
cpython-95f5b05a8cad61b296807c14e50896075c4dc1de.tar.bz2
GH-88597: Added command line interface to UUID module. (#99463)
The `uuid` module now supports command line usage. ```python ❯ ./python.exe -m uuid 5f2d57b1-90e8-417c-ba5d-69b9b6f74289 ❯ ./python.exe -m uuid -h usage: uuid.py [-h] [-u {uuid1,uuid3,uuid4,uuid5}] [-ns NAMESPACE] [-n NAME] ... ```
Diffstat (limited to 'Lib/test/test_uuid.py')
-rwxr-xr-xLib/test/test_uuid.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py
index 411eec0..61ae256 100755
--- a/Lib/test/test_uuid.py
+++ b/Lib/test/test_uuid.py
@@ -675,6 +675,64 @@ class BaseTestUUID:
weak = weakref.ref(strong)
self.assertIs(strong, weak())
+ @mock.patch.object(sys, "argv", ["", "-u", "uuid3", "-ns", "NAMESPACE_DNS"])
+ def test_cli_namespace_required_for_uuid3(self):
+ with self.assertRaises(SystemExit) as cm:
+ self.uuid.main()
+
+ # Check that exception code is the same as argparse.ArgumentParser.error
+ self.assertEqual(cm.exception.code, 2)
+
+ @mock.patch.object(sys, "argv", ["", "-u", "uuid3", "-n", "python.org"])
+ def test_cli_name_required_for_uuid3(self):
+ with self.assertRaises(SystemExit) as cm:
+ self.uuid.main()
+
+ # Check that exception code is the same as argparse.ArgumentParser.error
+ self.assertEqual(cm.exception.code, 2)
+
+ @mock.patch.object(sys, "argv", [""])
+ def test_cli_uuid4_outputted_with_no_args(self):
+ stdout = io.StringIO()
+ with contextlib.redirect_stdout(stdout):
+ self.uuid.main()
+
+ output = stdout.getvalue().strip()
+ uuid_output = self.uuid.UUID(output)
+
+ # Output uuid should be in the format of uuid4
+ self.assertEqual(output, str(uuid_output))
+ self.assertEqual(uuid_output.version, 4)
+
+ @mock.patch.object(sys, "argv",
+ ["", "-u", "uuid3", "-ns", "NAMESPACE_DNS", "-n", "python.org"])
+ def test_cli_uuid3_ouputted_with_valid_namespace_and_name(self):
+ stdout = io.StringIO()
+ with contextlib.redirect_stdout(stdout):
+ self.uuid.main()
+
+ output = stdout.getvalue().strip()
+ uuid_output = self.uuid.UUID(output)
+
+ # Output should be in the form of uuid5
+ self.assertEqual(output, str(uuid_output))
+ self.assertEqual(uuid_output.version, 3)
+
+ @mock.patch.object(sys, "argv",
+ ["", "-u", "uuid5", "-ns", "NAMESPACE_DNS", "-n", "python.org"])
+ def test_cli_uuid5_ouputted_with_valid_namespace_and_name(self):
+ stdout = io.StringIO()
+ with contextlib.redirect_stdout(stdout):
+ self.uuid.main()
+
+ output = stdout.getvalue().strip()
+ uuid_output = self.uuid.UUID(output)
+
+ # Output should be in the form of uuid5
+ self.assertEqual(output, str(uuid_output))
+ self.assertEqual(uuid_output.version, 5)
+
+
class TestUUIDWithoutExtModule(BaseTestUUID, unittest.TestCase):
uuid = py_uuid