Skip to content

Commit

Permalink
fix: make rest.py still load on python2, do not test bad json
Browse files Browse the repository at this point in the history
It's not worth fixing the test to make it work on python2.

But do define the missing JSONDecodeError as it's base class
ValueError on python2.
  • Loading branch information
rouilj committed Dec 18, 2024
1 parent 4039c14 commit fffab9b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 6 additions & 1 deletion roundup/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
from datetime import timedelta
from hashlib import md5

try:
from json import JSONDecodeError
except ImportError:
JSONDecodeError = ValueError

try:
from urllib.parse import urlparse
except ImportError:
Expand Down Expand Up @@ -2747,7 +2752,7 @@ def raise_error_on_constant(x):
parse_constant=raise_error_on_constant)
self.value = [self.FsValue(index, self.json_dict[index])
for index in self.json_dict]
except (json.decoder.JSONDecodeError, ValueError) as e:
except (JSONDecodeError, ValueError) as e:
raise ValueError(e.args[0] + ". JSON is: " + json_string)


Expand Down
10 changes: 9 additions & 1 deletion test/rest_common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import unittest
import shutil
import sys
import errno

from time import sleep
Expand Down Expand Up @@ -63,6 +64,13 @@ def dst(self, dt):
skip_jwt = mark_class(pytest.mark.skip(
reason='Skipping JWT tests: jwt library not available'))

if sys.version_info[0] > 2:
skip_on_py2 = lambda func, *args, **kwargs: func
else:
from .pytest_patcher import mark_class
skip_on_py2 =mark_class(pytest.mark.skip(
reason='Skipping test on Python 2'))

NEEDS_INSTANCE = 1


Expand Down Expand Up @@ -2427,6 +2435,7 @@ def testDispatchBadAccept(self):
json_dict = json.loads(b2s(results))
self.assertIn('Unable to parse Accept Header. Invalid param: foo. Acceptable types: */*, application/json', json_dict['error']['msg'])

@skip_on_py2
def testBadJson(self):
'''Run some JSON we don't accept through the wringer
'''
Expand Down Expand Up @@ -2509,7 +2518,6 @@ def testBadJson(self):

self.assertEqual(json.loads(results), expected)


def testStatsGen(self):
# check stats being returned by put and get ops
# using dispatch which parses the @stats query param
Expand Down

0 comments on commit fffab9b

Please sign in to comment.