Passed
Push — main ( 52c0ea...0cab68 )
by Jochen
04:36
created

test_create_ticket_bundles()   B

Complexity

Conditions 2

Size

Total Lines 50
Code Lines 39

Duplication

Lines 50
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 39
nop 11
dl 50
loc 50
rs 8.9439
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
"""
2
:Copyright: 2006-2022 Jochen Kupperschmidt
3
:License: Revised BSD (see `LICENSE` file for details)
4
"""
5
6
from __future__ import annotations
7
from unittest.mock import patch
8
9
from flask import Flask
10
import pytest
11
12
from byceps.events.ticketing import TicketsSold
13
from byceps.services.shop.article.transfer.models import Article
14
from byceps.services.shop.order import action_registry_service
15
from byceps.services.shop.order import log_service as order_log_service
16
from byceps.services.shop.order.transfer.order import Order, Orderer
17
from byceps.services.shop.shop.transfer.models import Shop
18
from byceps.services.shop.storefront.transfer.models import Storefront
19
from byceps.services.ticketing.dbmodels.ticket import Ticket as DbTicket
20
from byceps.services.ticketing import ticket_service, ticket_bundle_service
21
from byceps.services.ticketing.transfer.models import TicketCategory
22
from byceps.services.user.transfer.models import User
23
24
from .helpers import get_tickets_for_order, mark_order_as_paid, place_order
25
26
27
@pytest.fixture
28
def article(make_article, shop: Shop) -> Article:
29
    return make_article(shop.id, total_quantity=8)
30
31
32
@pytest.fixture(scope='module')
33
def ticket_quantity_per_bundle() -> int:
34
    return 5
35
36
37
@pytest.fixture(scope='module')
38
def bundle_quantity() -> int:
39
    return 2
40
41
42
@pytest.fixture
43
def order(
44
    article: Article,
45
    bundle_quantity: int,
46
    storefront: Storefront,
47
    orderer: Orderer,
48
) -> Order:
49
    articles_with_quantity = [(article, bundle_quantity)]
50
    return place_order(storefront.id, orderer, articles_with_quantity)
51
52
53
@pytest.fixture
54
def order_action(
55
    article: Article,
56
    ticket_category: TicketCategory,
57
    ticket_quantity_per_bundle: int,
58
) -> None:
59
    action_registry_service.register_ticket_bundles_creation(
60
        article.item_number, ticket_category.id, ticket_quantity_per_bundle
61
    )
62
63
64 View Code Duplication
@patch('byceps.signals.ticketing.tickets_sold.send')
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
65
def test_create_ticket_bundles(
66
    tickets_sold_signal_send_mock,
67
    admin_app: Flask,
68
    article: Article,
69
    ticket_category: TicketCategory,
70
    ticket_quantity_per_bundle: int,
71
    bundle_quantity: int,
72
    admin_user: User,
73
    orderer_user: User,
74
    orderer: Orderer,
75
    order: Order,
76
    order_action,
77
) -> None:
78
    expected_ticket_total = 10
79
80
    tickets_before_paid = get_tickets_for_order(order)
81
    assert len(tickets_before_paid) == 0
82
83
    shop_order_paid_event = mark_order_as_paid(order.id, admin_user.id)
84
85
    tickets_after_paid = get_tickets_for_order(order)
86
    assert len(tickets_after_paid) == expected_ticket_total
87
88
    for ticket in tickets_after_paid:
89
        assert ticket.owned_by_id == orderer.user_id
90
        assert ticket.used_by_id == orderer.user_id
91
92
    log_entries = order_log_service.get_entries_for_order(order.id)
93
    ticket_bundle_created_log_entries = [
94
        entry
95
        for entry in log_entries
96
        if entry.event_type == 'ticket-bundle-created'
97
    ]
98
    assert len(ticket_bundle_created_log_entries) == bundle_quantity
99
100
    tickets_sold_event = TicketsSold(
101
        occurred_at=shop_order_paid_event.occurred_at,
102
        initiator_id=admin_user.id,
103
        initiator_screen_name=admin_user.screen_name,
104
        party_id=ticket_category.party_id,
105
        owner_id=orderer_user.id,
106
        owner_screen_name=orderer_user.screen_name,
107
        quantity=expected_ticket_total,
108
    )
109
    tickets_sold_signal_send_mock.assert_called_once_with(
110
        None, event=tickets_sold_event
111
    )
112
113
    tear_down_bundles(tickets_after_paid)
114
115
116
# helpers
117
118
119
def tear_down_bundles(tickets: list[DbTicket]) -> None:
120
    bundle_ids = {t.bundle_id for t in tickets}
121
122
    for ticket in tickets:
123
        ticket_service.delete_ticket(ticket.id)
124
125
    for bundle_id in bundle_ids:
126
        ticket_bundle_service.delete_bundle(bundle_id)
127