def test_ticket_with_different_discount_code(db): ticket = TicketSubFactory() discount = DiscountCodeTicketSubFactory(tickets=[]) db.session.commit() with pytest.raises(UnprocessableEntityError, match='Invalid Discount Code'): calculate_order_amount([{'id': ticket.id}], discount.id)
def test_create_order(client, db, jwt): discount_code = DiscountCodeTicketSubFactory(type='percent', value=10.0, tickets=[]) tickets_dict = _create_taxed_tickets(db, tax_included=False, discount_code=discount_code) db.session.commit() response = client.post( '/v1/orders/create-order', content_type='application/json', headers=jwt, data=json.dumps({ 'tickets': tickets_dict, 'discount-code': str(discount_code.id) }), ) assert TicketHolder.query.count() == 12 assert response.status_code == 200 order_dict = json.loads(response.data) order = Order.query.get(order_dict['data']['id']) assert order_dict['data']['attributes']['amount'] == 4745.81 assert order.amount == 4745.81 assert len(order.ticket_holders) == 12 assert order.discount_code == discount_code
def test_request_error(client, db): ticket = TicketSubFactory() discount = DiscountCodeTicketSubFactory(tickets=[]) db.session.commit() response = client.post( '/v1/orders/calculate-amount', content_type='application/json', data=json.dumps({ 'tickets': [{ 'id': ticket.id }], 'discount-code': discount.id }), ) assert response.status_code == 422 assert json.loads(response.data) == { "errors": [{ "status": 422, "source": { "pointer": "discount_code_id" }, "title": "Unprocessable Entity", "detail": "Invalid Discount Code", }], "jsonapi": { "version": "1.0" }, }
def test_request_calculate_order_amount(client, db): discount_code = DiscountCodeTicketSubFactory(type='percent', value=10.0, tickets=[]) tickets_dict = _create_taxed_tickets(db, tax_included=False, discount_code=discount_code) db.session.commit() response = client.post( '/v1/orders/calculate-amount', content_type='application/json', data=json.dumps({ 'tickets': tickets_dict, 'discount-code': str(discount_code.id) }), ) assert response.status_code == 200 amount_data = json.loads(response.data) assert amount_data['sub_total'] == 4021.87 assert amount_data['total'] == 4745.81 assert amount_data['tax']['included'] is False _assert_tax_data_discount(amount_data) assert amount_data['tax']['amount'] == 723.94
def _create_discount_code(db, **kwargs): tickets = TicketSubFactory.create_batch(3, event=EventFactoryBasic(), **kwargs) discount = DiscountCodeTicketSubFactory(tickets_number=5, tickets=tickets) db.session.commit() return discount, [{'id': ticket.id} for ticket in tickets]
def test_ticket_with_deleted_discount_code(db): ticket = TicketSubFactory() discount = DiscountCodeTicketSubFactory(deleted_at=datetime.now(), tickets=[ticket]) db.session.commit() with pytest.raises(ObjectNotFound): calculate_order_amount([{'id': ticket.id}], discount.id)
def test_tax_included_with_discount(db): discount_code = DiscountCodeTicketSubFactory(type='percent', value=10.0, tickets=[]) tickets_dict = _create_taxed_tickets(db, discount_code=discount_code) amount_data = calculate_order_amount(tickets_dict, discount_code) assert amount_data['sub_total'] == 4021.87 assert amount_data['total'] == 4021.87 assert amount_data['tax_included'] is True _assert_tax_data_discount(amount_data)
def test_no_amount_with_discount(db): ticket = TicketSubFactory(price=100.0) discount_code = DiscountCodeTicketSubFactory( type='percent', value=10.0, tickets=[ticket] ) db.session.commit() amount_data = calculate_order_amount([], discount_code.id) assert amount_data['total'] == 0.0 assert amount_data['tax'] is None assert amount_data['discount'] == 0.0 assert amount_data['tickets'] == []
def test_multiple_tickets_discount(db): ticket_a = TicketSubFactory(price=50.0) ticket_b = TicketSubFactory(price=495.8, event=ticket_a.event) ticket_c = TicketSubFactory(price=321.3, event=ticket_a.event) # Deliberately add price to free ticket ticket_d = TicketSubFactory(price=500.0, event=ticket_a.event, type='free') discount = DiscountCodeTicketSubFactory(type='percent', value=50.0, tickets=[ticket_a, ticket_b]) DiscountCodeTicketSubFactory(type='amount', value=100.0, tickets=[ticket_c]) db.session.commit() tickets_dict = _create_ticket_dict( [ticket_a, ticket_b, ticket_c, ticket_d], [2, 3, 1, 2]) amount_data = calculate_order_amount(tickets_dict, discount.id) assert amount_data['total'] == 1115.0 assert amount_data['discount'] == 793.7 assert amount_data['tickets'][0]['quantity'] == 2 assert amount_data['tickets'][0]['price'] == 50.0 assert amount_data['tickets'][0]['sub_total'] == 50.0 assert amount_data['tickets'][0]['discount']['total'] == 50.0 assert amount_data['tickets'][0]['discount']['amount'] == 25.0 assert amount_data['tickets'][0]['discount']['percent'] == 50.0 assert amount_data['tickets'][1]['quantity'] == 3 assert amount_data['tickets'][1]['price'] == 495.8 assert amount_data['tickets'][1]['sub_total'] == 743.7 assert amount_data['tickets'][1]['discount']['total'] == 743.7 assert amount_data['tickets'][1]['discount']['amount'] == 247.9 assert amount_data['tickets'][1]['discount']['percent'] == 50.0 assert amount_data['tickets'][2]['quantity'] == 1 assert amount_data['tickets'][2]['price'] == 321.3 assert amount_data['tickets'][2]['sub_total'] == 321.3 assert amount_data['tickets'][2]['discount'] is None
def test_discount_code_amount_type(db): ticket = TicketSubFactory(price=100.0) discount_code = DiscountCodeTicketSubFactory( type='amount', value=50.0, tickets=[ticket] ) db.session.commit() amount_data = calculate_order_amount([{'id': ticket.id}], discount_code.id) assert amount_data['total'] == 50.0 assert amount_data['discount'] == 50.0 ticket_dict = amount_data['tickets'][0] assert ticket_dict['sub_total'] == 50.0 assert ticket_dict['discount']['total'] == 50.0 assert ticket_dict['discount']['amount'] == 50.0 assert ticket_dict['discount']['percent'] == 50.0 assert ticket_dict['discount']['code'] == discount_code.code
def test_discount_code(db): ticket = TicketSubFactory(price=100.0) discount_code = DiscountCodeTicketSubFactory( type='percent', value=10.0, tickets=[ticket] ) db.session.commit() amount_data = calculate_order_amount([{'id': ticket.id}], discount_code.id) assert amount_data['total'] == 90.0 assert amount_data['tax'] is None assert amount_data['discount'] == 10.0 ticket_dict = amount_data['tickets'][0] assert ticket_dict['id'] == ticket.id assert ticket_dict['name'] == ticket.name assert ticket_dict['price'] == ticket.price assert ticket_dict['quantity'] == 1 assert ticket_dict['ticket_fee'] == 0.0 assert ticket_dict['sub_total'] == 90.0 assert ticket_dict['discount']['total'] == 10.0 assert ticket_dict['discount']['amount'] == 10.0 assert ticket_dict['discount']['percent'] == 10.0 assert ticket_dict['discount']['code'] == discount_code.code
def test_validate_discount_code_require_tickets_or_holders(db): with pytest.raises(ValueError): DiscountCodeTicketSubFactory(tickets=[]).validate()