Completed
Push — main ( de2ccb...e8e40e )
by Jochen
56:38 queued 53:08
created

test_create_ticket_with_existing_code()   A

Complexity

Conditions 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nop 4
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
"""
2
:Copyright: 2006-2020 Jochen Kupperschmidt
3
:License: Modified BSD, see LICENSE for details.
4
"""
5
6
from unittest.mock import patch
7
8
from pytest import raises
9
10
from byceps.services.ticketing import (
11
    event_service,
12
    ticket_creation_service,
13
    ticket_service,
14
)
15
16
17
def test_create_ticket(admin_app, category, ticket_owner):
18
    ticket = ticket_creation_service.create_ticket(category.id, ticket_owner.id)
19
20
    assert_created_ticket(ticket, category.id, ticket_owner.id)
21
22
    # Clean up.
23
    ticket_service.delete_ticket(ticket.id)
24
25
26
@patch('byceps.services.ticketing.ticket_creation_service._generate_ticket_code')
27
def test_create_ticket_with_existing_code(generate_ticket_code_mock, admin_app, category, ticket_owner):
28
    generate_ticket_code_mock.return_value = 'TAKEN'
29
30
    existing_ticket = ticket_creation_service.create_ticket(category.id, ticket_owner.id)
31
    assert existing_ticket.code == 'TAKEN'
32
33
    with raises(ticket_creation_service.TicketCreationFailed):
34
        ticket_creation_service.create_ticket(category.id, ticket_owner.id)
35
36
    # Clean up.
37
    ticket_service.delete_ticket(existing_ticket.id)
38
39
40
def test_create_tickets(admin_app, category, ticket_owner):
41
    quantity = 3
42
    tickets = ticket_creation_service.create_tickets(
43
        category.id, ticket_owner.id, quantity
44
    )
45
46
    for ticket in tickets:
47
        assert_created_ticket(ticket, category.id, ticket_owner.id)
48
49
    # Clean up.
50
    for ticket in tickets:
51
        ticket_service.delete_ticket(ticket.id)
52
53
54
def assert_created_ticket(ticket, expected_category_id, expected_owner_id):
55
    assert ticket is not None
56
    assert ticket.created_at is not None
57
    assert ticket.code is not None
58
    assert ticket.bundle_id is None
59
    assert ticket.category_id == expected_category_id
60
    assert ticket.owned_by_id == expected_owner_id
61
    assert ticket.seat_managed_by_id is None
62
    assert ticket.user_managed_by_id is None
63
    assert ticket.occupied_seat_id is None
64
    assert ticket.used_by_id is None
65
    assert not ticket.revoked
66
    assert not ticket.user_checked_in
67
68
    events = event_service.get_events_for_ticket(ticket.id)
69
    assert len(events) == 0
70