~homeworkprod/byceps

49c3e9aff230ba6f9d1fee0bd0c9a0d5cba5a659 — Jochen Kupperschmidt 1 year, 5 months ago a721a8f
Delete shop number sequences via sequence ID
M byceps/services/shop/sequence/service.py => byceps/services/shop/sequence/service.py +17 -15
@@ 16,7 16,7 @@ from ..order.transfer.models import OrderNumber
from ..shop.transfer.models import ShopID

from .models import NumberSequence as DbNumberSequence
from .transfer.models import NumberSequence, Purpose
from .transfer.models import NumberSequence, NumberSequenceID, Purpose


def create_sequence(


@@ 25,43 25,45 @@ def create_sequence(
    prefix: str,
    *,
    value: Optional[int] = None,
) -> None:
) -> NumberSequenceID:
    """Create a sequence for that shop and purpose."""
    sequence = DbNumberSequence(shop_id, purpose, prefix, value=value)

    db.session.add(sequence)
    db.session.commit()

    return sequence.id


def create_article_number_sequence(
    shop_id: ShopID, prefix: str, *, value: Optional[int] = None
) -> None:
    create_sequence(shop_id, Purpose.article, prefix, value=value)
) -> NumberSequenceID:
    return create_sequence(shop_id, Purpose.article, prefix, value=value)


def create_order_number_sequence(
    shop_id: ShopID, prefix: str, *, value: Optional[int] = None
) -> None:
    create_sequence(shop_id, Purpose.order, prefix, value=value)
) -> NumberSequenceID:
    return create_sequence(shop_id, Purpose.order, prefix, value=value)


def delete_sequence(shop_id: ShopID, purpose: Purpose) -> None:
    """Delete sequence for that shop and purpose."""
def delete_sequence(sequence_id: NumberSequenceID) -> None:
    """Delete the sequence."""
    db.session.query(DbNumberSequence) \
        .filter_by(shop_id=shop_id, _purpose=purpose.name) \
        .filter_by(id=sequence_id) \
        .delete()

    db.session.commit()


def delete_article_number_sequence(shop_id: ShopID) -> None:
    """Delete article sequence for that shop."""
    delete_sequence(shop_id, Purpose.article)
def delete_article_number_sequence(sequence_id: NumberSequenceID) -> None:
    """Delete the article sequence."""
    delete_sequence(sequence_id)


def delete_order_number_sequence(shop_id: ShopID) -> None:
    """Delete order sequence for that shop."""
    delete_sequence(shop_id, Purpose.order)
def delete_order_number_sequence(sequence_id: NumberSequenceID) -> None:
    """Delete the order sequence."""
    delete_sequence(sequence_id)


class NumberGenerationFailed(Exception):

M tests/integration/blueprints/admin/shop/order/test_order_export.py => tests/integration/blueprints/admin/shop/order/test_order_export.py +4 -2
@@ 124,9 124,11 @@ def orderer(make_user):

@pytest.fixture
def order_number_sequence(shop) -> None:
    sequence_service.create_order_number_sequence(shop.id, 'LR-08-B', value=26)
    sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'LR-08-B', value=26
    )
    yield
    sequence_service.delete_order_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(sequence_id)


@pytest.fixture

M tests/integration/blueprints/admin/shop/order/test_views.py => tests/integration/blueprints/admin/shop/order/test_views.py +4 -2
@@ 37,9 37,11 @@ from tests.integration.services.shop.helpers import (

@pytest.fixture(scope='module')
def order_number_sequence(shop) -> None:
    sequence_service.create_order_number_sequence(shop.id, 'order-')
    sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'order-'
    )
    yield
    sequence_service.delete_order_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(sequence_id)


@pytest.fixture(scope='module')

M tests/integration/blueprints/site/shop/order/test_views.py => tests/integration/blueprints/site/shop/order/test_views.py +4 -2
@@ 38,7 38,9 @@ COMMON_FORM_DATA = {
@pytest.fixture
def shop(email_config, admin_user):
    shop = create_shop('shop-1')
    sequence_service.create_order_number_sequence(shop.id, 'AEC-01-B', value=4)
    order_number_sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'AEC-01-B', value=4
    )
    snippet_id = create_shop_fragment(
        shop.id, admin_user.id, 'payment_instructions', 'Send all ur moneyz!'
    )


@@ 46,7 48,7 @@ def shop(email_config, admin_user):
    yield shop

    snippet_service.delete_snippet(snippet_id)
    sequence_service.delete_order_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(order_number_sequence_id)
    shop_service.delete_shop(shop.id)



M tests/integration/blueprints/site/shop/orders/test_views.py => tests/integration/blueprints/site/shop/orders/test_views.py +4 -2
@@ 45,13 45,15 @@ def site2(shop2):
@pytest.fixture
def shop1(admin_app, email_config, admin_user):
    shop = create_shop('shop-1')
    sequence_service.create_order_number_sequence(shop.id, 'LF-02-B')
    order_number_sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'LF-02-B'
    )
    snippet_id = create_payment_instructions_snippet(shop.id, admin_user.id)

    yield shop

    snippet_service.delete_snippet(snippet_id)
    sequence_service.delete_order_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(order_number_sequence_id)
    shop_service.delete_shop(shop.id)



M tests/integration/services/shop/conftest.py => tests/integration/services/shop/conftest.py +4 -2
@@ 36,6 36,8 @@ def empty_cart() -> Cart:

@pytest.fixture
def order_number_sequence(shop) -> None:
    sequence_service.create_order_number_sequence(shop.id, 'order-')
    sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'order-'
    )
    yield
    sequence_service.delete_order_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(sequence_id)

M tests/integration/services/shop/order/email/test_email_on_order_canceled.py => tests/integration/services/shop/order/email/test_email_on_order_canceled.py +4 -2
@@ 32,7 32,9 @@ def customer(admin_app):

@pytest.fixture
def order(shop, customer, order_admin):
    sequence_service.create_order_number_sequence(shop.id, 'AC-14-B', value=16)
    order_number_sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'AC-14-B', value=16
    )

    email_footer_snippet_id = create_email_footer_snippet(
        shop.id, order_admin.id


@@ 46,7 48,7 @@ def order(shop, customer, order_admin):

    snippet_service.delete_snippet(email_footer_snippet_id)
    order_service.delete_order(order.id)
    sequence_service.delete_order_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(order_number_sequence_id)


@patch('byceps.email.send')

M tests/integration/services/shop/order/email/test_email_on_order_paid.py => tests/integration/services/shop/order/email/test_email_on_order_paid.py +4 -2
@@ 33,7 33,9 @@ def customer(admin_app):

@pytest.fixture
def order(shop, customer, order_admin):
    sequence_service.create_order_number_sequence(shop.id, 'AC-14-B', value=21)
    order_number_sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'AC-14-B', value=21
    )

    email_footer_snippet_id = create_email_footer_snippet(
        shop.id, order_admin.id


@@ 47,7 49,7 @@ def order(shop, customer, order_admin):

    snippet_service.delete_snippet(email_footer_snippet_id)
    order_service.delete_order(order.id)
    sequence_service.delete_order_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(order_number_sequence_id)


@patch('byceps.email.send')

M tests/integration/services/shop/order/email/test_email_on_order_placed.py => tests/integration/services/shop/order/email/test_email_on_order_placed.py +4 -2
@@ 69,7 69,9 @@ def article2(shop):

@pytest.fixture
def order(shop, article1, article2, customer, order_admin):
    sequence_service.create_order_number_sequence(shop.id, 'AC-14-B', value=252)
    order_number_sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'AC-14-B', value=252
    )

    email_payment_instructions_snippet_id = create_email_payment_instructions_snippet(
        shop.id, order_admin.id


@@ 94,7 96,7 @@ def order(shop, article1, article2, customer, order_admin):
    snippet_service.delete_snippet(email_payment_instructions_snippet_id)
    snippet_service.delete_snippet(email_footer_snippet_id)
    order_service.delete_order(order.id)
    sequence_service.delete_order_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(order_number_sequence_id)


@patch('byceps.email.send')

M tests/integration/services/shop/order/test_ordered_articles_service.py => tests/integration/services/shop/order/test_ordered_articles_service.py +4 -2
@@ 33,7 33,9 @@ def test_count_ordered_articles(admin_app, shop, article, orderer):
        PaymentState.canceled_after_paid: 6,
    }

    sequence_service.create_order_number_sequence(shop.id, 'ABC-01-B')
    order_number_sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'ABC-01-B'
    )

    order_ids = set()
    for article_quantity, payment_state in [


@@ 63,7 65,7 @@ def test_count_ordered_articles(admin_app, shop, article, orderer):
    for order_id in order_ids:
        order_service.delete_order(order_id)

    sequence_service.delete_order_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(order_number_sequence_id)


# helpers

M tests/integration/services/shop/order/test_service_orders_placed_by_user.py => tests/integration/services/shop/order/test_service_orders_placed_by_user.py +8 -4
@@ 20,22 20,26 @@ from tests.integration.services.shop.helpers import create_shop
@pytest.fixture
def shop1(email_config):
    shop = create_shop('first-nice-shop')
    sequence_service.create_order_number_sequence(shop.id, 'LF-02-B')
    order_number_sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'LF-02-B'
    )

    yield shop

    sequence_service.delete_order_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(order_number_sequence_id)
    shop_service.delete_shop(shop.id)


@pytest.fixture
def shop2(email_config):
    shop = create_shop('second-nice-shop')
    sequence_service.create_order_number_sequence(shop.id, 'LF-03-B')
    order_number_sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'LF-03-B'
    )

    yield shop

    sequence_service.delete_order_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(order_number_sequence_id)
    shop_service.delete_shop(shop.id)



M tests/integration/services/shop/sequence/test_service_sequence_number_generation.py => tests/integration/services/shop/sequence/test_service_sequence_number_generation.py +12 -12
@@ 26,20 26,21 @@ def shop2(email_config):
def test_generate_article_number_default(admin_app, shop1):
    shop = shop1

    sequence_service.create_article_number_sequence(shop.id, 'AEC-01-A')
    article_number_sequence_id = sequence_service.create_article_number_sequence(
        shop.id, 'AEC-01-A'
    )

    actual = sequence_service.generate_article_number(shop.id)

    assert actual == 'AEC-01-A00001'

    sequence_service.delete_article_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(shop.id)
    sequence_service.delete_article_number_sequence(article_number_sequence_id)


def test_generate_article_number_custom(admin_app, shop2):
    shop = shop2

    sequence_service.create_article_number_sequence(
    article_number_sequence_id = sequence_service.create_article_number_sequence(
        shop.id, 'XYZ-09-A', value=41,
    )



@@ 47,21 48,21 @@ def test_generate_article_number_custom(admin_app, shop2):

    assert actual == 'XYZ-09-A00042'

    sequence_service.delete_article_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(shop.id)
    sequence_service.delete_article_number_sequence(article_number_sequence_id)


def test_generate_order_number_default(admin_app, shop1):
    shop = shop1

    sequence_service.create_order_number_sequence(shop.id, 'AEC-01-B')
    order_number_sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'AEC-01-B'
    )

    actual = sequence_service.generate_order_number(shop.id)

    assert actual == 'AEC-01-B00001'

    sequence_service.delete_article_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(order_number_sequence_id)


def test_generate_order_number_custom(admin_app, shop2):


@@ 69,7 70,7 @@ def test_generate_order_number_custom(admin_app, shop2):

    last_assigned_order_sequence_number = 206

    sequence_service.create_order_number_sequence(
    order_number_sequence_id = sequence_service.create_order_number_sequence(
        shop.id, 'LOL-03-B', value=last_assigned_order_sequence_number
    )



@@ 77,5 78,4 @@ def test_generate_order_number_custom(admin_app, shop2):

    assert actual == 'LOL-03-B00207'

    sequence_service.delete_article_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(shop.id)
    sequence_service.delete_order_number_sequence(order_number_sequence_id)