Skip to content

Commit fd91cfb

Browse files
committed
add test coverage for new validation;
1 parent ff8153c commit fd91cfb

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

graphql_api/data/base_data.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ def append_uniq(size):
5151
return str(size) + uniq
5252

5353

54+
def json_serialised(obj):
55+
"""A simple wrapper to facilitate testing."""
56+
return json.dumps(obj)
57+
58+
5459
def replace_enums(kwargs: Dict) -> Dict:
5560
"""Replace any Enum members with their values.
5661
@@ -317,14 +322,15 @@ def _write_object(self, object_id, object_type, body):
317322
# We've been caught out by Schema classes that are not json-serialisable (the ENUM issue).
318323
# This should make that more obvious if it happens again.
319324
try:
320-
json.dumps(body)
321-
except TypeError as err:
322-
logging.error(type(err))
323-
logging.error(
325+
json_serialised(body)
326+
except Exception as exc:
327+
# logging.error(repr(exc))
328+
msg = (
324329
"This object cannot be persisted to a PynamoDB.Model,"
325330
" check that all enums and types are json serialisable!"
326331
)
327-
raise err
332+
logging.error(msg)
333+
raise graphql.GraphQLError(f'{__name__}._write_object() method failed with exception. %s' % msg)
328334

329335
toshi_object = self._model(object_id=str(object_id), object_type=body['clazz_name'], object_content=body)
330336

graphql_api/tests/test_table_schema_fix_252.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from unittest import mock
77

88
import boto3
9+
import pytest
910
from graphene.test import Client
1011
from moto import mock_dynamodb, mock_s3
1112
from pynamodb.connection.base import Connection # for mocking
@@ -40,6 +41,17 @@
4041
}
4142
}'''
4243

44+
INPUT_VARIABLES = {
45+
'headers': ['series', 'series_name', 'X', 'Y'],
46+
'object_id': 'SW52ZXJzaW9uU29sdXRpb246MTAxOTY2',
47+
'rows': [['0', 'foo', '1.0', '2.0'], ['1', 'bar', '1.5', '2.5']],
48+
'column_types': ['integer', 'string', 'double', 'double'],
49+
'table_name': 'Inversion Solution MFD table',
50+
'created': '2025-08-06T23:32:58.526823Z',
51+
'table_type': 'MFD_CURVES',
52+
'dimensions': [],
53+
}
54+
4355

4456
class IncrId:
4557
next_id = -1
@@ -49,6 +61,11 @@ def get_next_id(self, *args):
4961
return str(self.next_id) + 'RANDM'
5062

5163

64+
def json_dumps_mock(obj):
65+
raise RuntimeError(str(obj))
66+
return None
67+
68+
5269
@mock.patch('graphql_api.data.BaseDynamoDBData._write_object', lambda self, object_id, object_type, body: None)
5370
class TestFailingMutation(unittest.TestCase):
5471
"""
@@ -69,17 +86,8 @@ def test_create_252_exmple_table(self):
6986
# {'message': 'Object of type EnumMeta is not JSON serializable', 'locations': [{'line': 2, 'column': 3}], 'path': ['create_table']}
7087

7188
print(CREATE_TABLE)
72-
input_variables = {
73-
'headers': ['series', 'series_name', 'X', 'Y'],
74-
'object_id': 'SW52ZXJzaW9uU29sdXRpb246MTAxOTY2',
75-
'rows': [['0', 'foo', '1.0', '2.0'], ['1', 'bar', '1.5', '2.5']],
76-
'column_types': ['integer', 'string', 'double', 'double'],
77-
'table_name': 'Inversion Solution MFD table',
78-
'created': '2025-08-06T23:32:58.526823Z',
79-
'table_type': 'MFD_CURVES',
80-
'dimensions': [],
81-
}
82-
result = self.client.execute(CREATE_TABLE, variable_values=input_variables)
89+
90+
result = self.client.execute(CREATE_TABLE, variable_values=INPUT_VARIABLES)
8391
print(result)
8492
assert result['data']['create_table']['table']['id'] == 'VGFibGU6MFJBTkRN'
8593

@@ -97,7 +105,6 @@ def setUp(self):
97105
self._bucket = self._s3_conn.Bucket(S3_BUCKET_NAME)
98106
self._connection = Connection(region=REGION)
99107

100-
# ToshiThingObject.create_table()
101108
ToshiTableObject.create_table()
102109
ToshiIdentity.create_table()
103110

@@ -106,16 +113,14 @@ def setUp(self):
106113
def test_create_one_table(self):
107114

108115
print(CREATE_TABLE)
109-
input_variables = {
110-
'headers': ['series', 'series_name', 'X', 'Y'],
111-
'object_id': 'SW52ZXJzaW9uU29sdXRpb246MTAxOTY2',
112-
'rows': [['0', 'foo', '1.0', '2.0'], ['1', 'bar', '1.5', '2.5']],
113-
'column_types': ['integer', 'string', 'double', 'double'],
114-
'table_name': 'Inversion Solution MFD table',
115-
'created': '2025-08-06T23:32:58.526823Z',
116-
'table_type': 'MFD_CURVES',
117-
'dimensions': [],
118-
}
119-
result = self.client.execute(CREATE_TABLE, variable_values=input_variables)
116+
117+
result = self.client.execute(CREATE_TABLE, variable_values=INPUT_VARIABLES)
120118
print(result)
121119
assert result['data']['create_table']['table']['id'] == 'VGFibGU6MTAwMDAw'
120+
121+
@mock.patch('graphql_api.data.base_data.json_serialised', json_dumps_mock)
122+
def test_new_logging_exception_handing(self):
123+
print(CREATE_TABLE)
124+
result = self.client.execute(CREATE_TABLE, variable_values=INPUT_VARIABLES)
125+
print(result['errors'])
126+
assert result['errors'][0]['message'].find("This object cannot be persisted to a PynamoDB.Model") != -1

0 commit comments

Comments
 (0)