Skip to content

Commit

Permalink
#150: Rewrite JSON validity tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Dec 3, 2020
1 parent cf89d7e commit b97b19b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
27 changes: 0 additions & 27 deletions test/db/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,11 @@
from datagateway_api.common.helpers import (
get_filters_from_query_string,
get_session_id_from_auth_header,
is_valid_json,
queries_records,
)
from test.test_base import FlaskAppTest


class TestIsValidJSON(TestCase):
def test_array(self):
self.assertTrue(is_valid_json("[]"))

def test_null(self):
self.assertTrue("null")

def test_valid_json(self):
self.assertTrue(is_valid_json('{"test":1}'))

def test_single_quoted_json(self):
self.assertFalse(is_valid_json("{'test':1}"))

def test_none(self):
self.assertFalse(is_valid_json(None))

def test_int(self):
self.assertFalse(is_valid_json(1))

def test_dict(self):
self.assertFalse(is_valid_json({"test": 1}))

def test_list(self):
self.assertFalse(is_valid_json([]))


class TestRequiresSessionID(FlaskAppTest):
def setUp(self):
super().setUp()
Expand Down
34 changes: 34 additions & 0 deletions test/test_is_valid_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest

from datagateway_api.common.helpers import is_valid_json


class TestIsValidJSON:
@pytest.mark.parametrize(
"input_json",
[
pytest.param("[]", id="empty array"),
pytest.param("null", id="null"),
pytest.param('{"test":1}', id="key-value pair"),
pytest.param('{"test":{"inner_key":"inner_value"}}', id="nested json"),
],
)
def test_valid_json_input(self, input_json):
valid_json = is_valid_json(input_json)

assert valid_json

@pytest.mark.parametrize(
"invalid_json",
[
pytest.param("{'test':1}", id="single quotes"),
pytest.param(None, id="none"),
pytest.param(1, id="integer"),
pytest.param({"test": 1}, id="dictionary"),
pytest.param([], id="empty list"),
],
)
def test_invalid_json_input(self, invalid_json):
valid_json = is_valid_json(invalid_json)

assert not valid_json

0 comments on commit b97b19b

Please sign in to comment.