<commit_before>"""
:Copyright: 2006-2020 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

import pytest

from byceps.services.news import (
    channel_service as news_channel_service,
    service as news_service,
)

from tests.helpers import create_brand, create_user

from ...conftest import database_recreated


@pytest.fixture(scope='module')
def app(party_app, db):
    with party_app.app_context():
        with database_recreated(db):
            yield party_app


@pytest.fixture
def editor(make_user):
    yield from make_user('NewsEditor')


@pytest.fixture(scope='module')
def brand(app):
    return create_brand()


@pytest.fixture(scope='module')
def channel(brand):
    channel_id = f'{brand.id}-test'
    url_prefix = 'https://example.com/news/'

    channel = news_channel_service.create_channel(brand.id, channel_id, url_prefix)

    yield channel

    news_channel_service.delete_channel(channel_id)


def test_image_url_with_image(app, channel, editor):
    item = create_item(
        channel.id,
        'with-image',
        editor.id,
        image_url_path='breaking.png',
    )

    assert item.image_url_path == '/data/global/news_channels/acmecon-test/breaking.png'

    tear_down_news_item(item)


def test_image_url_without_image(app, channel, editor):
    item = create_item(channel.id, 'without-image', editor.id)

    assert item.image_url_path is None

    tear_down_news_item(item)


# helpers


def create_item(channel_id, slug, editor_id, *, image_url_path=None):
    title = 'the title'
    body = 'the body'

    item = news_service.create_item(
        channel_id, slug, editor_id, title, body, image_url_path=image_url_path
    )

    # Return aggregated version of item.
    return news_service.find_aggregated_item_by_slug(channel_id, slug)


def tear_down_news_item(item):
    news_service.delete_item(item.id)<commit_msg>Provide news items as fixtures<commit_after>"""
:Copyright: 2006-2020 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

import pytest

from byceps.services.news import (
    channel_service as news_channel_service,
    service as news_service,
)

from tests.helpers import create_brand, create_user

from ...conftest import database_recreated


@pytest.fixture(scope='module')
def app(party_app, db):
    with party_app.app_context():
        with database_recreated(db):
            yield party_app


@pytest.fixture
def editor(make_user):
    yield from make_user('NewsEditor')


@pytest.fixture(scope='module')
def brand(app):
    return create_brand()


@pytest.fixture(scope='module')
def channel(brand):
    channel_id = f'{brand.id}-test'
    url_prefix = 'https://example.com/news/'

    channel = news_channel_service.create_channel(brand.id, channel_id, url_prefix)

    yield channel

    news_channel_service.delete_channel(channel_id)


@pytest.fixture
def news_item_with_image(channel, editor):
    item = create_item(
        channel.id,
        'with-image',
        editor.id,
        image_url_path='breaking.png',
    )

    yield item

    news_service.delete_item(item.id)


@pytest.fixture
def news_item_without_image(channel, editor):
    item = create_item(channel.id, 'without-image', editor.id)

    yield item

    news_service.delete_item(item.id)


def test_image_url_with_image(news_item_with_image):
    assert news_item_with_image.image_url_path == '/data/global/news_channels/acmecon-test/breaking.png'


def test_image_url_without_image(news_item_without_image):
    assert news_item_without_image.image_url_path is None


# helpers


def create_item(channel_id, slug, editor_id, *, image_url_path=None):
    title = 'the title'
    body = 'the body'

    item = news_service.create_item(
        channel_id, slug, editor_id, title, body, image_url_path=image_url_path
    )

    # Return aggregated version of item.
    return news_service.find_aggregated_item_by_slug(channel_id, slug)
{'fraction_non_alphanumeric': 0.10156050140700947, 'fraction_numerical': 0.004093118444614991, 'mean_word_length': 4.131233595800525, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2536966', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1291, 'n_words': 256}