def test_valid_add_query_join(
        self,
        flask_test_app_db,
        input_field,
    ):
        table = get_entity_object_from_name("Investigation")

        test_utility = DatabaseFilterUtilities()
        test_utility.extract_filter_fields(input_field)

        expected_query = ReadQuery(table)
        if test_utility.related_related_field:
            expected_table = get_entity_object_from_name(
                test_utility.related_field)

            included_table = get_entity_object_from_name(test_utility.field)
            expected_query.base_query = expected_query.base_query.join(
                included_table, ).join(expected_table)
        elif test_utility.related_field:
            expected_table = get_entity_object_from_name(test_utility.field)

            expected_query = ReadQuery(table)
            expected_query.base_query = expected_query.base_query.join(
                expected_table)
        else:
            expected_table = table

        with ReadQuery(table) as test_query:
            test_utility.add_query_join(test_query)

        # Check the JOIN has been applied
        assert str(test_query.base_query) == str(expected_query.base_query)
    def test_valid_get_field(self, flask_test_app_db):
        table = get_entity_object_from_name("Investigation")

        test_utility = DatabaseFilterUtilities()
        field = test_utility._get_field(table, "name")

        assert field == table.name
Exemplo n.º 3
0
    def add_query_join(self, query):
        """
        Adds any required JOINs to the query if any related fields have been used in the
        filter

        :param query: The query to have filters applied to
        :type query: :class:`datagateway_api.common.database.helpers.[QUERY]`
        """

        if self.related_related_field:
            included_table = get_entity_object_from_name(self.field)
            included_included_table = get_entity_object_from_name(
                self.related_field)
            query.base_query = query.base_query.join(included_table).join(
                included_included_table, )
        elif self.related_field:
            included_table = get_entity_object_from_name(self.field)
            query.base_query = query.base_query.join(included_table)
    def test_valid_get_entity_model_for_filter(self, input_field):
        table = get_entity_object_from_name("Investigation")

        test_utility = DatabaseFilterUtilities()
        test_utility.extract_filter_fields(input_field)

        if test_utility.related_related_field:
            expected_table = get_entity_object_from_name(
                test_utility.related_field)
        elif test_utility.related_field:
            expected_table = get_entity_object_from_name(test_utility.field)
        else:
            expected_table = table

        with ReadQuery(table) as test_query:
            output_field = test_utility.get_entity_model_for_filter(test_query)

        # Check the output is correct
        field_name_to_fetch = input_field.split(".")[-1]
        assert output_field == getattr(expected_table, field_name_to_fetch)
Exemplo n.º 5
0
    def get_entity_model_for_filter(self, query):
        """
        Fetches the appropriate entity model based on the contents of the instance
        variables of this class

        :param query: The query to have filters applied to
        :type query: :class:`datagateway_api.common.database.helpers.[QUERY]`
        :return: Entity model of the field (usually the field relating to the endpoint
            the request is coming from)
        """
        if self.related_related_field:
            included_included_table = get_entity_object_from_name(
                self.related_field)
            field = self._get_field(included_included_table,
                                    self.related_related_field)
        elif self.related_field:
            included_table = get_entity_object_from_name(self.field)
            field = self._get_field(included_table, self.related_field)
        else:
            # No related fields
            field = self._get_field(query.table, self.field)

        return field
    def test_invalid_get_field(self, flask_test_app_db):
        table = get_entity_object_from_name("Investigation")

        test_utility = DatabaseFilterUtilities()
        with pytest.raises(FilterError):
            test_utility._get_field(table, "unknown")
 def test_invalid_get_entity_object_from_name(self):
     with pytest.raises(ApiError):
         get_entity_object_from_name("Application1234s")
Exemplo n.º 8
0
def create_entity_models():
    """
    Creates a schema dict for each endpoint

    :return: dict of endpoint names to model
    """
    endpoint_models = {}

    for endpoint in endpoints:
        params = {}
        required = []
        endpoint_table = get_entity_object_from_name(endpoints[endpoint])
        endpoint_inspection = inspect(endpoint_table)
        for column in endpoint_inspection.columns:
            # Needed to ensure camelCase field names are used, rather than SNAKE_CASE
            attribute_field_name = endpoint_inspection.get_property_by_column(
                column,
            ).key
            python_type = (
                column.type.impl.python_type
                if hasattr(column.type, "impl")
                else column.type.python_type
            )

            param = type_conversion(python_type)
            if column.name == "ID":
                param["readOnly"] = True
            if column.doc:
                param["description"] = column.doc
            if not column.nullable:
                required.append(attribute_field_name)
            params[attribute_field_name] = param

        for (
            relationship_name,
            relationship_class,
        ) in endpoint_inspection.relationships.items():
            if (
                relationship_class.direction.name == "MANYTOONE"
                or relationship_class.direction.name == "ONETOONE"
            ):
                params[relationship_name] = {
                    "$ref": "#/components/schemas/"
                    f"{relationship_name.strip('_').upper()}",
                }
            if (
                relationship_class.direction.name == "MANYTOMANY"
                or relationship_class.direction.name == "ONETOMANY"
            ):
                entity_underscore_strip = relationship_name.strip("_")
                # Checking for plurals on a related ICAT entity
                if entity_underscore_strip[-1] == "s":
                    pascal_case = (
                        entity_underscore_strip[0].upper() + entity_underscore_strip[1:]
                    )
                    entity_reference_name = endpoints[pascal_case].upper()
                else:
                    entity_reference_name = relationship_name.strip("_").upper()

                params[relationship_name] = {
                    "type": "array",
                    "items": {"$ref": f"#/components/schemas/{entity_reference_name}"},
                }
        endpoint_models[endpoint_table.__name__] = {
            "properties": params,
            "required": required,
        }

    return endpoint_models
    def test_valid_get_entity_object_from_name(self, entity_name,
                                               expected_object_type):
        database_entity = get_entity_object_from_name(entity_name)

        assert type(database_entity) == expected_object_type
Exemplo n.º 10
0
 def count_with_filters(self, session_id, entity_type, filters, **kwargs):
     table = get_entity_object_from_name(entity_type)
     return get_filtered_row_count(table, filters)
Exemplo n.º 11
0
 def get_with_id(self, session_id, entity_type, id_, **kwargs):
     table = get_entity_object_from_name(entity_type)
     return get_row_by_id(table, id_).to_dict()
Exemplo n.º 12
0
 def update(self, session_id, entity_type, data, **kwargs):
     table = get_entity_object_from_name(entity_type)
     return patch_entities(table, data)
Exemplo n.º 13
0
 def create(self, session_id, entity_type, data, **kwargs):
     table = get_entity_object_from_name(entity_type)
     return create_rows_from_json(table, data)
Exemplo n.º 14
0
 def get_with_filters(self, session_id, entity_type, filters, **kwargs):
     table = get_entity_object_from_name(entity_type)
     return get_rows_by_filter(table, filters)
Exemplo n.º 15
0
 def update_with_id(self, session_id, entity_type, id_, data, **kwargs):
     table = get_entity_object_from_name(entity_type)
     return update_row_from_id(table, id_, data)
Exemplo n.º 16
0
 def delete_with_id(self, session_id, entity_type, id_, **kwargs):
     table = get_entity_object_from_name(entity_type)
     return delete_row_by_id(table, id_)