annotate test/test_anypy.py @ 7766:c65e0a725c88

fix: replace eval with ast.literal_eval; ruff linting strings.py: When reading csv export files, use the safer literal_eval to evaluate the fields. Issue pointed out by ruff linter. Other Cleanup: sort imports replace if, else, if, else with if, elif, else use isinstance with tuples rather than 'isinstance() or isinstance()' test_anypy.py: Added tests for tuples, and booleans. Also added exception handling for malformed strings, booleans, tuples.
author John Rouillard <rouilj@ieee.org>
date Sat, 02 Mar 2024 04:01:22 -0500
parents e4db9d0b85c7
children ebb03b8cee4d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6532
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
1 """Random tests for anypy modules"""
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
2
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
3
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
4 import unittest
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
5 from roundup.anypy.strings import repr_export, eval_import
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
6
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
7 import sys
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
8 _py3 = sys.version_info[0] > 2
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
9
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
10 class StringsTest(unittest.TestCase):
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
11
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
12 def test_import_params(self):
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
13 """ issue2551170 - handle long int in history/journal
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
14 params tuple
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
15 """
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
16 # python2 export with id as number
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
17 val = eval_import("('issue', 2345L, 'status')")
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
18 self.assertSequenceEqual(val, ('issue', 2345, 'status'))
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
19
7766
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
20 # eval a tuple e.g. date representation
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
21 val = eval_import("(2022, 9, 6, 3, 58, 4.776, 0, 0, 0)")
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
22 self.assertSequenceEqual(val, (2022, 9, 6, 3, 58, 4.776, 0, 0, 0))
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
23
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
24 # eval a boolean
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
25 val = eval_import("False")
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
26 self.assertEqual(val, False)
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
27 val = eval_import("True")
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
28 self.assertEqual(val, True)
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
29
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
30 # check syntax error
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
31 for testcase in ['true', '(2004, 10, 20', "2000, 10, 22)",
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
32 "test'", '"test']:
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
33 with self.assertRaises(ValueError) as m:
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
34 val = eval_import(testcase)
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
35 print(m.exception)
c65e0a725c88 fix: replace eval with ast.literal_eval; ruff linting
John Rouillard <rouilj@ieee.org>
parents: 6532
diff changeset
36
6532
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
37 # python3 export with id as number
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
38 val = eval_import("('issue', 2345, 'status')")
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
39 self.assertSequenceEqual(val, ('issue', 2345, 'status'))
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
40
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
41 # python2 or python3 export with id as string
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
42 val = eval_import("('issue', '2345', 'status')")
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
43 self.assertSequenceEqual(val, ('issue', '2345', 'status'))
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
44
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
45 def test_export_params(self):
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
46 """ issue2551170 - handle long int in history/journal
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
47 params tuple
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
48 """
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
49 # python2 export with id as number
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
50 if _py3:
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
51 val = repr_export(('issue', 2345, 'status'))
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
52 self.assertEqual(val, "('issue', 2345, 'status')")
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
53 else:
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
54 val = repr_export(('issue', long(2345), 'status'))
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
55 self.assertEqual(val, "('issue', 2345L, 'status')")
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
56
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
57 # python2 or python3 export with id as string
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
58 val = repr_export(('issue', '2345', 'status'))
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
59 self.assertEqual(val, "('issue', '2345', 'status')")
e4db9d0b85c7 test for issue2551170 process python 2 long under python3
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
60

Roundup Issue Tracker: http://roundup-tracker.org/