From 49291353ca649eabdfffbfd506125151f140ab87 Mon Sep 17 00:00:00 2001 From: Sebastiaan Huber Date: Fri, 19 Jun 2020 14:33:34 +0200 Subject: [PATCH] `NodeTranslator`: do not assume `get_export_formats` exists The `get_all_download_formats` will try to call `get_export_formats` on all the known plugin types. However, it is not guaranteed that all plugins implement this method. This was properly considered in the case that `full_type` is not None, however, in the other case the `AttributeError` was not being caught causing the REST API to except. --- aiida/restapi/translator/nodes/node.py | 17 +++++++---------- tests/restapi/test_translator.py | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 tests/restapi/test_translator.py diff --git a/aiida/restapi/translator/nodes/node.py b/aiida/restapi/translator/nodes/node.py index ea75a72adf..58b14b3384 100644 --- a/aiida/restapi/translator/nodes/node.py +++ b/aiida/restapi/translator/nodes/node.py @@ -416,17 +416,14 @@ def get_all_download_formats(full_type=None): for name in get_entry_point_names(entry_point_group): try: node_cls = load_entry_point(entry_point_group, name) - except LoadingEntryPointError: - pass - else: - node_cls.get_export_formats() - try: available_formats = node_cls.get_export_formats() - if available_formats: - full_type = construct_full_type(node_cls.class_node_type, '') - all_formats[full_type] = available_formats - except AttributeError: - pass + except (AttributeError, LoadingEntryPointError): + continue + + if available_formats: + full_type = construct_full_type(node_cls.class_node_type, '') + all_formats[full_type] = available_formats + return all_formats @staticmethod diff --git a/tests/restapi/test_translator.py b/tests/restapi/test_translator.py new file mode 100644 index 0000000000..fae5b7ceaa --- /dev/null +++ b/tests/restapi/test_translator.py @@ -0,0 +1,15 @@ +"""Tests for the `aiida.restapi.translator` module.""" +# pylint: disable=invalid-name +from aiida.restapi.translator.nodes.node import NodeTranslator +from aiida.orm import Data + + +def test_get_all_download_formats(): + """Test the `get_all_download_formats` method.""" + NodeTranslator.get_all_download_formats() + + +def test_get_all_download_formats_missing_get_export_formats(monkeypatch): + """Test `get_all_download_formats` does not except if a `Data` class does not implement `get_export_formats`.""" + monkeypatch.delattr(Data, 'get_export_formats') + NodeTranslator.get_all_download_formats()