コード例 #1
0
def test_camel_to_snake_case():
    """ Should convert Class Names to Database Table names. """
    test_strings = {
        'test': 'test',
        'Test': 'test',
        'TEST': 'test',
        # https://stackoverflow.com/a/1176023
        'CamelCase': 'camel_case',
        'CamelCamelCase': 'camel_camel_case',
        'Camel2Camel2Case': 'camel2_camel2_case',
        'getHTTPResponseCode': 'get_http_response_code',
        'get2HTTPResponseCode': 'get2_http_response_code',
        'HTTPResponseCode': 'http_response_code',
        'HTTPResponseCodeXYZ': 'http_response_code_xyz',
        # https://gist.github.com/jaytaylor/3660565
        'snakesOnAPlane': 'snakes_on_a_plane',
        'SnakesOnAPlane': 'snakes_on_a_plane',
        'snakes_on_a_plane': 'snakes_on_a_plane',
        'IPhoneHysteria': 'i_phone_hysteria',
        'iPhoneHysteria': 'i_phone_hysteria',
        # These strings aren't camel case so it should do something weird:
        '_Test': '__test',
        '_test_Method': '_test__method',
        '__test__Method': '__test___method',
        '__CamelCase': '___camel_case',
        '_Camel_Case': '__camel__case',
        # This one might want a fix some day, but it is also bad camel case:
        'getHTTPresponseCode': 'get_htt_presponse_code'
    }

    for camel, snake in test_strings.items():
        assert utilities.camel_to_delimiter_separated(camel) == snake
コード例 #2
0
    def __tablename__(cls):  # pylint: disable=no-self-argument
        """
        Convert the TitleCase class names to lower underscore names for the database table name.
        :return str: name for table
        """
        if cls._cached_tablename is None:
            cls._cached_tablename = utilities.camel_to_delimiter_separated(cls.__name__)  # pylint: disable=no-member

        return cls._cached_tablename
コード例 #3
0
    def __init__(self, meta, *args, **kwargs):
        """
        Forces strict=True.
        Forces type_ to kebab-case of self.opts.model.__name__ for JSONAPI recommendation.
        Sets up default self_url and kwargs if model option is set.
        Sets up default self_url_many if model and listable are truthy.
        """
        # Does Resource support a read list endpoint? Default False. Needed for Swagger.
        self.listable = getattr(meta, 'listable', False)

        # TODO: ROB 20170726 Check status of github.com/marshmallow-code/marshmallow/issues/377
        # Force strict by default until ticket is resolved.
        meta.strict = True

        # When the base Schema below is first initialized there won't be a model element.
        model = getattr(meta, 'model', None)
        if model:
            # Automatically set the JSONAPI type (marshmallow-jsonapi) based on model name.
            # JSONAPI recommends kebab-case for naming: http://jsonapi.org/recommendations/#naming
            type_ = utilities.camel_to_delimiter_separated(model.__name__,
                                                           glue='-')
            meta.type_ = type_

            # Self URLs are always based on the JSONAPI type and the model id
            meta.self_url = f'/{type_}/{{id}}'
            meta.self_url_kwargs = {'id': '<id>'}
            # Always include the many url for resource creation at least
            meta.self_url_many = f'/{type_}'

        # Use our custom ModelConverter to turn SQLAlchemy relations into JSONAPI Relationships.
        meta.model_converter = ModelConverter

        def dasherize(text):
            """
            Convert snake_case field names to jsonapi kebab-case.
            :param str text: field name
            :return str: kebab-case name
            """
            return text.replace('_', '-')

        meta.inflect = dasherize

        super().__init__(meta, *args, **kwargs)
コード例 #4
0
    def _add_relationship_kwargs(self, kwargs, prop):
        """ Customize the kwargs for Relationship field based on prop. """
        # super()._add_relationship_kwargs(kwargs, prop)

        # All Schema names should be based on Model name.
        kwargs['schema'] = prop.mapper.class_.__name__ + 'Schema'
        # If the relation uses a list then the Relationship is many
        kwargs['many'] = prop.uselist
        # JSONAPI type is calculated from Model name to kebab-case.
        kwargs['type_'] = utilities.camel_to_delimiter_separated(
            prop.mapper.class_.__name__, glue='-')
        # Attribute of the model for this relationship.
        kwargs['attribute'] = prop.key
        # self.schema_cls is not an instance of the class so we need to use the options directly.
        # Name of the relationship on parent model to use for constructing relationship URLs.
        kwargs['relationship_name'] = self.schema_cls.opts.inflect(prop.key)
        # The relationship's URLs will be calculated from the parent schema's self URL.
        kwargs['parent_self_url'] = self.schema_cls.opts.self_url
        # Relationship URLs will use the same kwargs as the parent schema.
        kwargs['self_url_kwargs'] = self.schema_cls.opts.self_url_kwargs
        # Store the model of the schema_cls so the relationship knows everything necessary for the
        # endpoint.
        kwargs['parent_model'] = self.schema_cls.opts.model
コード例 #5
0
def test_camel_to_kebab_case():
    """ camel_to_delimiter_separated also takes a glue parameter to allow for kebab-case. """
    assert utilities.camel_to_delimiter_separated('Camel2Camel2Case', glue='-') == \
        'camel2-camel2-case'