
from unittest.mock import patch

import pytest

from byceps.services.user_message import service as user_message_service

from tests.conftest import database_recreated
from tests.helpers import app_context, create_brand, create_email_config, \
    create_party, create_site, create_user


def test_recipient_formatting(application, params):
    screen_name, email_address, expected = params

    create_email_config()

    brand = create_brand()
    party = create_party(brand.id)

    site = create_site(party.id)

    user = create_user(screen_name, email_address=email_address)

    message = user_message_service.create_message(user.id, user.id, '', '',
                                                  site.id)

    assert message.recipients == [expected]


@pytest.fixture(params=[
    ('Alice', 'alice@example.com', 'Alice <alice@example.com>'),
    ('<AngleInvestor>', 'angleinvestor@example.com', '"<AngleInvestor>" <angleinvestor@example.com>'),
    ('-=]YOLO[=-', 'yolo@example.com', '"-=]YOLO[=-" <yolo@example.com>'),
])
def params(request):
    yield request.param


@pytest.fixture
def application(db):
    with app_context():
        with database_recreated(db):
            yield


from unittest.mock import patch

import pytest

from byceps.services.user_message import service as user_message_service

from tests.conftest import database_recreated
from tests.helpers import app_context, create_brand, create_email_config, \
    create_party, create_site, create_user


def test_recipient_formatting(site, params):
    screen_name, email_address, expected = params

    user = create_user(screen_name, email_address=email_address)

    message = user_message_service.create_message(user.id, user.id, '', '',
                                                  site.id)

    assert message.recipients == [expected]


@pytest.fixture(params=[
    ('Alice', 'alice@example.com', 'Alice <alice@example.com>'),
    ('<AngleInvestor>', 'angleinvestor@example.com', '"<AngleInvestor>" <angleinvestor@example.com>'),
    ('-=]YOLO[=-', 'yolo@example.com', '"-=]YOLO[=-" <yolo@example.com>'),
])
def params(request):
    yield request.param


@pytest.fixture(scope='module')
def site(db):
    with app_context():
        with database_recreated(db):
            create_email_config()

            brand = create_brand()
            party = create_party(brand.id)

            site = create_site(party.id)

            yield site

Speed up user message address formatting test
Speed up user message address formatting test

The common set-up is moved to the fixture, then the fixture's scope is
widened so that it is used for all test cases in the module, avoiding
duplicate work.

  
  from unittest.mock import patch
  
  import pytest
  
  from byceps.services.user_message import service as user_message_service
  
  from tests.conftest import database_recreated
  from tests.helpers import app_context, create_brand, create_email_config, \
      create_party, create_site, create_user
  
  
- def test_recipient_formatting(application, params):
+ def test_recipient_formatting(site, params):
      screen_name, email_address, expected = params
- 
-     create_email_config()
- 
-     brand = create_brand()
-     party = create_party(brand.id)
- 
-     site = create_site(party.id)
  
      user = create_user(screen_name, email_address=email_address)
  
      message = user_message_service.create_message(user.id, user.id, '', '',
                                                    site.id)
  
      assert message.recipients == [expected]
  
  
  @pytest.fixture(params=[
      ('Alice', 'alice@example.com', 'Alice <alice@example.com>'),
      ('<AngleInvestor>', 'angleinvestor@example.com', '"<AngleInvestor>" <angleinvestor@example.com>'),
      ('-=]YOLO[=-', 'yolo@example.com', '"-=]YOLO[=-" <yolo@example.com>'),
  ])
  def params(request):
      yield request.param
  
  
- @pytest.fixture
- def application(db):
+ @pytest.fixture(scope='module')
+ def site(db):
      with app_context():
          with database_recreated(db):
-             yield
+             create_email_config()
  
+             brand = create_brand()
+             party = create_party(brand.id)
+ 
+             site = create_site(party.id)
+ 
+             yield site
+ 
Speed up user message address formatting test
## Code Before:

from unittest.mock import patch

import pytest

from byceps.services.user_message import service as user_message_service

from tests.conftest import database_recreated
from tests.helpers import app_context, create_brand, create_email_config, \
    create_party, create_site, create_user


def test_recipient_formatting(application, params):
    screen_name, email_address, expected = params

    create_email_config()

    brand = create_brand()
    party = create_party(brand.id)

    site = create_site(party.id)

    user = create_user(screen_name, email_address=email_address)

    message = user_message_service.create_message(user.id, user.id, '', '',
                                                  site.id)

    assert message.recipients == [expected]


@pytest.fixture(params=[
    ('Alice', 'alice@example.com', 'Alice <alice@example.com>'),
    ('<AngleInvestor>', 'angleinvestor@example.com', '"<AngleInvestor>" <angleinvestor@example.com>'),
    ('-=]YOLO[=-', 'yolo@example.com', '"-=]YOLO[=-" <yolo@example.com>'),
])
def params(request):
    yield request.param


@pytest.fixture
def application(db):
    with app_context():
        with database_recreated(db):
            yield

## Instruction:
Speed up user message address formatting test
## Code After:

from unittest.mock import patch

import pytest

from byceps.services.user_message import service as user_message_service

from tests.conftest import database_recreated
from tests.helpers import app_context, create_brand, create_email_config, \
    create_party, create_site, create_user


def test_recipient_formatting(site, params):
    screen_name, email_address, expected = params

    user = create_user(screen_name, email_address=email_address)

    message = user_message_service.create_message(user.id, user.id, '', '',
                                                  site.id)

    assert message.recipients == [expected]


@pytest.fixture(params=[
    ('Alice', 'alice@example.com', 'Alice <alice@example.com>'),
    ('<AngleInvestor>', 'angleinvestor@example.com', '"<AngleInvestor>" <angleinvestor@example.com>'),
    ('-=]YOLO[=-', 'yolo@example.com', '"-=]YOLO[=-" <yolo@example.com>'),
])
def params(request):
    yield request.param


@pytest.fixture(scope='module')
def site(db):
    with app_context():
        with database_recreated(db):
            create_email_config()

            brand = create_brand()
            party = create_party(brand.id)

            site = create_site(party.id)

            yield site
