Passed
Push — main ( 854eb5...57d628 )
by Jochen
05:03
created

test_order_state()   A

Complexity

Conditions 1

Size

Total Lines 25
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nop 4
dl 0
loc 25
rs 9.352
c 0
b 0
f 0
1
"""
2
:Copyright: 2006-2021 Jochen Kupperschmidt
3
:License: Revised BSD (see `LICENSE` file for details)
4
"""
5
6
from datetime import datetime
7
from decimal import Decimal
8
9
import pytest
10
11
from byceps.services.shop.order.models.orderer import Orderer
12
from byceps.services.shop.order import service as order_service
13
from byceps.services.shop.order.transfer.models import (
14
    Order,
15
    OrderNumber,
16
    OrderState,
17
    PaymentState,
18
)
19
from byceps.services.shop.shop.transfer.models import ShopID
20
21
22
SHIPPING_REQUIRED = True
23
SHIPPED = True
24
25
26
@pytest.mark.parametrize(
27
    'payment_state, shipping_required, shipped, expected',
28
    [
29
        (PaymentState.open,                 not SHIPPING_REQUIRED,  not SHIPPED, OrderState.open    ),
30
        (PaymentState.open,                     SHIPPING_REQUIRED,  not SHIPPED, OrderState.open    ),
31
        (PaymentState.open,                     SHIPPING_REQUIRED,      SHIPPED, OrderState.open    ),
32
        (PaymentState.canceled_before_paid, not SHIPPING_REQUIRED,  not SHIPPED, OrderState.canceled),
33
        (PaymentState.canceled_before_paid,     SHIPPING_REQUIRED,  not SHIPPED, OrderState.canceled),
34
        (PaymentState.canceled_before_paid,     SHIPPING_REQUIRED,      SHIPPED, OrderState.canceled),
35
        (PaymentState.canceled_after_paid,  not SHIPPING_REQUIRED,  not SHIPPED, OrderState.canceled),
36
        (PaymentState.canceled_after_paid,      SHIPPING_REQUIRED,  not SHIPPED, OrderState.canceled),
37
        (PaymentState.canceled_after_paid,      SHIPPING_REQUIRED,      SHIPPED, OrderState.canceled),
38
        (PaymentState.paid,                 not SHIPPING_REQUIRED,  not SHIPPED, OrderState.complete),
39
        (PaymentState.paid,                     SHIPPING_REQUIRED,  not SHIPPED, OrderState.open    ),
40
        (PaymentState.paid,                     SHIPPING_REQUIRED,      SHIPPED, OrderState.complete),
41
    ],
42
)
43
def test_order_state(
44
    payment_state: PaymentState,
45
    shipping_required: bool,
46
    shipped: bool,
47
    expected: OrderState,
48
):
49
    order = create_order(payment_state, shipping_required, shipped)
50
    assert order.state == expected
51
52
53
# helpers
54
55
56
def create_order(
57
    payment_state: PaymentState, shipping_required: bool, shipped: bool
58
) -> Order:
59
    shop_id = ShopID('shop-123')
60
    order_number = OrderNumber('ORDER-42')
61
    orderer = create_orderer()
62
    created_at = datetime.utcnow()
63
64
    order = order_service._build_order(
65
        shop_id, order_number, orderer, created_at
66
    )
67
    order.payment_state = payment_state
68
    order.shipping_required = shipping_required
69
    order.shipped_at = created_at if shipped else None
70
71
    return order_service._order_to_transfer_object(order)
72
73
74
def create_orderer() -> Orderer:
75
    return Orderer(
76
        user_id='937a2112-62b5-4824-b5c0-430396b94591',
77
        first_names='Burkhardt',
78
        last_name='Playhardt',
79
        country='Country',
80
        zip_code='55555',
81
        city='City',
82
        street='Street',
83
    )
84