Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement FOREST_DISABLE_AUTO_SCHEMA_UPDATE and test for it #138

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 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,22 @@ 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.'
])

@pytest.mark.usefixtures('reset_config_dir_import')
@override_settings(FOREST={'FOREST_DISABLE_AUTO_SCHEMA_UPDATE': True}, 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 +225,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 +276,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_update = get_forest_setting('FOREST_DISABLE_AUTO_SCHEMA_UPDATE', False)
if not disable_auto_schema_update:
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