示例#1
0
def populate():
    start_mappers()

    _articles = [
        {
            'title': 'Artigo 1',
            'description': 'Descrição memorável',
            'content': 'Conteúdo top',
            'tags': [],
        },
        {
            'title': 'Artigo 2',
            'description': 'Descrição memorável',
            'content': 'Conteúdo top',
            'tags': [],
        },
        {
            'title': 'Artigo 3',
            'description': 'Descrição memorável',
            'content': 'Conteúdo top',
        }
    ]

    session = sessionmaker(bind=create_engine(config.get_postgres_uri()))()

    for _article in _articles:
        session.add(
            article.Article(
                title=_article['title'],
                description=_article['description'],
                content=_article['content'],
                tags=[]
            ))

        session.commit()
def add_article(title: str,
                description: str,
                content: str,
                category_id: int,
                uow: unit_of_work.AbstractUnitOfWork,
                tags: list = None) -> str:
    with uow:

        category = category_service.get_category(category_id)

        if not category:
            raise exceptions.CategoryNotFound('Category not found.')

        if title_is_duplicate(title, uow):
            raise exceptions.DuplicateTitle('Can\'t create article. Title '
                                            'duplicate.')

        _tags = tag_service.get_valid_tags_by_name(tags, uow) or []

        _article = article.Article(title, description, content, _tags,
                                   category)

        uow.articles.add(_article)
        uow.commit()

    return title
示例#3
0
def test_can_create_article_with_friendly_url():
    _article = article.Article(
        title="Um artigo precisa ter uma url amigável?",
        description="Post de boas vindas",
        content="Este é o dama rowahutu!",
        category=category.Category.GUIDE,
        tags=tag_factory(),
    )

    assert _article.url == 'um-artigo-precisa-ter-uma-url-amigável'
示例#4
0
def test_remove_special_characters_returns_clean_string():
    _article = article.Article(
        title="Um artigo ĩwẽ precisa ter uma url amigável?",
        description="Post de boas vindas",
        content="Este é o dama rowahutu!",
        category=category.Category.GUIDE,
        tags=tag_factory(),
    )

    assert _article._remove_special_characters() == \
           'Um artigo ĩwẽ precisa ter uma url amigável '
示例#5
0
def test_can_create_short_article_with_timestamp():
    _article = article.Article(
        title="Bem-vindos",
        description="Post de boas vindas",
        content="Este é o dama rowahutu!",
        category=category.Category.GUIDE,
        tags=tag_factory(),

    )

    assert _article.created_on
示例#6
0
def test_orm_saves_date_of_article(session):

    _article = article.Article(title='An article',
                               description='A great description',
                               content='This is a useful article',
                               tags=tag_factory(),
                               category=category.Category.GUIDE)

    session.add(_article)
    session.commit()

    assert session.query(article.Article).first().created_on
def test_returns_false_when_passing_article_to_exclude_from_comparison():
    uow = FakeUnitOfWork()

    article_1 = article.Article(
        title='An article',
        description='A great description',
        content='This is a useful article',
        category=category.Category.CULTURE,
        tags=[tag.Tag('verbos'), tag.Tag('substantivos')])

    article_2 = article.Article(
        title='Another article',
        description='A great description',
        content='This is a useful article',
        category=category.Category.CULTURE,
        tags=[tag.Tag('verbos'), tag.Tag('substantivos')])

    uow.articles.add(article_1)
    uow.articles.add(article_2)
    uow.commit()

    assert article_service.title_is_duplicate(
        title='An article', article_to_update=article_1, uow=uow) is False
def test_returns_true_when_title_is_duplicate():
    uow = FakeUnitOfWork()

    article_1 = article.Article(
        title='An article',
        description='A great description',
        content='This is a useful article',
        category=category.Category.CULTURE,
        tags=[tag.Tag('verbos'), tag.Tag('substantivos')])

    article_2 = article.Article(
        title='Another article',
        description='A great description',
        content='This is a useful article',
        category=category.Category.CULTURE,
        tags=[tag.Tag('verbos'), tag.Tag('substantivos')])

    uow.articles.add(article_1)
    uow.articles.add(article_2)
    uow.commit()

    assert article_service.title_is_duplicate(title='An article', uow=uow)
    assert article_service.title_is_duplicate(title='Another article', uow=uow)
    def test_can_add_an_article(self, session):
        _article = article.Article(
            title='An article',
            description='A great description',
            content='This is a useful article',
            tags=[tag.Tag('verbos')],
            category=category.Category.GUIDE
        )

        repo = repository.SqlAlchemyRepositoryArticles(session)

        repo.add(_article)
        session.commit()

        assert session.query(article.Article).first() == _article
示例#10
0
    def test_gets_article_by_url(self, session):
        _article = article.Article(
            title='An article',
            description='A great description',
            content='This is a useful article',
            tags=[tag.Tag('verbos')],
            category=category.Category.GUIDE
        )

        repo = repository.SqlAlchemyRepositoryArticles(session)

        repo.add(_article)
        session.commit()

        assert _article == repo.get(value='an-article')
示例#11
0
def test_orm_deletes_a_tag_used_in_article(session):

    _article = article.Article(title='An article',
                               description='A great description',
                               content='This is a useful article',
                               tags=tag_factory(),
                               category=category.Category.GUIDE)

    session.add(_article)
    session.commit()

    _tag = session.query(tag.Tag).first()

    session.delete(_tag)
    session.commit()

    assert len(session.query(article.Article).first().tags) == 3
示例#12
0
def test_uow_can_add_an_article(session_factory):

    _article = article.Article(title='An article',
                               description='A great description',
                               content='This is a useful article',
                               tags=[tag.Tag('verbos')],
                               category=category.Category.GUIDE)

    uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory)

    with uow:
        uow.articles.add(_article)
        uow.commit()

    session = session_factory()

    retrieved_article = session.query(article.Article).first()

    assert retrieved_article.title == 'An article'