Skip to content

Commit

Permalink
feat: implement FOREST_DISABLE_AUTO_SCHEMA_UPDATE and test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalomolbar committed Jul 19, 2023
1 parent 021b9c4 commit c6014c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
18 changes: 14 additions & 4 deletions django_forest/tests/utils/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def tearDown(self):
@pytest.mark.usefixtures('reset_config_dir_import')
def test_handle_schema_file_no_file(self):
with self.assertLogs() as cm:
self.assertRaises(Exception, Schema.handle_schema_file())
Schema.handle_schema_file()
self.assertIsNone(Schema.schema_data)
self.assertEqual(cm.output, [
'ERROR:django_forest.utils.schema:The .forestadmin-schema.json file does not exist.',
Expand All @@ -180,13 +180,23 @@ def test_handle_schema_file_production(self):
@pytest.mark.usefixtures('invalid_forestadmin_schema')
def test_handle_schema_file_invalid_json_production(self):
with self.assertLogs() as cm:
self.assertRaises(Exception, Schema.handle_schema_file())
Schema.handle_schema_file()
self.assertIsNone(Schema.schema_data)
self.assertEqual(cm.output, [
'ERROR:django_forest.utils.schema:The content of .forestadmin-schema.json file is not a correct JSON.',
'ERROR:django_forest.utils.schema:The schema cannot be synchronized with Forest Admin servers.'
])

@override_settings(FOREST={'FOREST_DISABLE_AUTO_SCHEMA_UPDATE': True})
@pytest.mark.usefixtures('reset_config_dir_import')
@override_settings(DEBUG=True)
def test_handle_schema_file_debug_disable_update(self):
Schema.handle_schema_file()
self.assertIsNotNone(Schema.schema_data)
with self.assertRaises(FileNotFoundError) as cm:
open(file_path, 'r')


@pytest.mark.usefixtures('reset_config_dir_import')
@override_settings(DEBUG=True)
def test_handle_schema_file_debug(self):
Expand Down Expand Up @@ -216,7 +226,7 @@ def test_send_apimap_disable_apply(self, mocked_get_serialized_schema):

@override_settings(FOREST={'FOREST_DISABLE_AUTO_SCHEMA_APPLY': 'foo'})
def test_send_apimap_server_error(self):
self.assertRaises(Exception, Schema.send_apimap())
Exception, Schema.send_apimap()

@override_settings(DEBUG=True)
@mock.patch('requests.post', return_value=mocked_requests({'key1': 'value1'}, 200))
Expand Down Expand Up @@ -267,7 +277,7 @@ def test_send_apimap_warning(self, mocked_requests_post):
def test_send_apimap_zero(self, mocked_requests_post):
Schema.schema_data = test_question_schema_data
with self.assertLogs() as cm:
self.assertRaises(Exception, Schema.send_apimap())
Schema.send_apimap()
self.assertEqual(cm.records[0].message,
'Cannot send the apimap to Forest. Are you online?')
self.assertEqual(cm.records[0].levelname, 'WARNING')
Expand Down
6 changes: 4 additions & 2 deletions django_forest/utils/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ def handle_schema_file(cls):
for index, collection in enumerate(cls.schema_data['collections']):
cls.schema_data['collections'][index] = cls.get_serialized_collection(collection)

with open(file_path, 'w') as f:
f.write(json.dumps(cls.schema_data, indent=2))
disable_auto_schema_apply = get_forest_setting('FOREST_DISABLE_AUTO_SCHEMA_UPDATE', False)
if not disable_auto_schema_apply:
with open(file_path, 'w') as f:
f.write(json.dumps(cls.schema_data, indent=2))
else:
cls.handle_schema_file_production(file_path)

Expand Down

0 comments on commit c6014c9

Please sign in to comment.