Example #1
0
    def test_model_raises_when_missing_correct_slug_attribute(self):
        # ARRANGE
        model = mock.Mock(slug="Not an InstrumentedAttribute")

        # ACT & ASSERT
        with pytest.raises(expected_exception=exceptions.UnableToGenerateSlug
                           ) as exception:
            utils.unique_slugify(model=model, text="My blog")
        assert str(exception.value) == "Model does not have the slug attribute"
Example #2
0
    def test_recursion_error_raises_exception(self):
        # ARRANGE
        model = self._setup_valid_model()

        # ACT & ASSERT
        with pytest.raises(expected_exception=exceptions.UnableToGenerateSlug
                           ) as exception:
            utils.unique_slugify(model=model, text="My blog")
        assert str(exception.value) == "Unable to generate unique slug"
Example #3
0
def create_category(title: str, description: str) -> models.Category:
    """
    Create a Category in the database.

    Raises:
        - `UnableToCreate` if Category cannot be created.

    Returns:
        - A new Category.
    """
    try:
        slug = utils.unique_slugify(model=models.Category,
                                    text=title,
                                    max_length=200)
    except exceptions.UnableToGenerateSlug:
        raise exceptions.UnableToCreate("Unable to create category.")
    try:
        category = models.Category.new(
            title=title,
            slug=slug,
            description=description,
        )
    except Exception:
        # TODO: Publish an event
        raise exceptions.UnableToCreate("Unable to create category.")

    # TODO: Publish an event

    return category
Example #4
0
 def on_model_change(self, form, model, is_created):
     if is_created:
         # Required as we're querying the database
         # https://docs.sqlalchemy.org/en/13/orm/session_api.html#sqlalchemy.orm.session.Session.no_autoflush
         with db.session.no_autoflush:
             slug = utils.unique_slugify(model=blog_models.Blog,
                                         text=model.title,
                                         max_length=200)
         model.slug = slug
Example #5
0
    def test_unique_slug_generated_with_max_length(self):
        # ARRANGE
        model = self._setup_valid_model()
        model.query.filter_by.return_value.first.return_value = None

        # ACT
        slug = utils.unique_slugify(model=model, text="My blog", max_length=4)

        # ASSERT
        assert slug == "my-b"
Example #6
0
    def test_unique_slug_generated_with_uuid(self, mock_uuid4):
        # ARRANGE
        model = self._setup_valid_model()
        model.query.filter_by.return_value.first.side_effect = [
            mock.Mock(),
            None,
        ]
        mock_uuid4.return_value = mock.Mock(
            hex="a12c6af1-144d-4f88-b467-e06e3f3e3d47")

        # ACT
        slug = utils.unique_slugify(model=model, text="My blog")

        # ASSERT
        assert slug == "a12-my-blog"
Example #7
0
def create_blog(
    title: str,
    body: str,
    author: account_models.Account,
    description: Optional[str] = None,
    categories: Optional[List[category_models.Category]] = None,
    published: bool = False,
    comment: bool = False,
) -> blog_models.Blog:
    """
    Create a Blog in the database.

    Raises:
        - `UnableToCreate` if Blog cannot be created.
    Returns:
        - A new Blog.
    """
    # TODO: This is a race condition (slug is unique when called, but possibly
    # not when new() is called). We need to lock the blogs table to ensure that
    # the slug is actually unique.
    try:
        slug = utils.unique_slugify(model=blog_models.Blog,
                                    text=title,
                                    max_length=200)
    except exceptions.UnableToGenerateSlug:
        # TODO: Publish an event
        raise exceptions.UnableToCreate("Unable to create blog.")
    try:
        blog = blog_models.Blog.new(
            title=title,
            slug=slug,
            body=body,
            author=author,
            description=description,
            categories=categories,
            published=published,
            comment=comment,
        )
    except Exception:
        # TODO: Publish an event
        raise exceptions.UnableToCreate("Unable to create blog.")

    # TODO: Publish an event

    return blog