def test_multiple_tickets(db): ticket = TicketSubFactory(price=50) ticket2 = TicketSubFactory(price=12.5, event=ticket.event) ticket3 = TicketSubFactory(price=233.15, event=ticket.event) db.session.commit() tickets = _create_ticket_dict([ticket, ticket2, ticket3], [3, 1, 2]) amount_data = calculate_order_amount(tickets) assert amount_data['total'] == 628.8 assert amount_data['tax_included'] is None assert amount_data['tax'] == 0.0 assert amount_data['discount'] == 0.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'] == 3 assert ticket_dict['ticket_fee'] == 0.0 assert ticket_dict['sub_total'] == 150.0 assert amount_data['tickets'][-1]['id'] == ticket3.id assert amount_data['tickets'][-1]['price'] == ticket3.price assert amount_data['tickets'][-1]['quantity'] == 2 assert amount_data['tickets'][-1]['sub_total'] == 466.3
def test_reject_deleted_tickets(db): event = EventFactoryBasic() tickets = TicketSubFactory.create_batch(3, event=event) tickets.append(TicketSubFactory(deleted_at=datetime.now(), event=event)) db.session.commit() with pytest.raises(ObjectNotFound, match=r'Tickets not found for IDs: .*'): validate_tickets([ticket.id for ticket in tickets])
def test_multiple_tickets_different_event(db): ticket1 = TicketSubFactory() ticket2 = TicketSubFactory() db.session.commit() ticket_dict = _create_ticket_dict([ticket1, ticket2], [1, 2]) with pytest.raises(UnprocessableEntityError, match=r".*All tickets must belong to same event.*"): calculate_order_amount(ticket_dict)
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_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_edit_attendee_ticket(db, client, jwt): attendee = AttendeeOrderTicketSubFactory() ticket = TicketSubFactory(event=attendee.event) db.session.commit() attendee_ticket = attendee.ticket data = json.dumps({ 'data': { 'type': 'attendee', 'id': str(attendee.id), "relationships": { "ticket": { "data": { "id": str(ticket.id), "type": "ticket" } } }, } }) client.patch( f'/v1/attendees/{attendee.id}', content_type='application/vnd.api+json', headers=jwt, data=data, ) db.session.refresh(attendee) # Ticket should not be updated assert attendee.ticket.id != ticket.id assert attendee.ticket.id == attendee_ticket.id
def test_match_discount_quantity(db): """Method to test the quantity calculation of discount code""" ticket = TicketSubFactory() discount_code = DiscountCodeTicketFactory(tickets_number=5) discount_code.tickets.append(ticket) order_without_discount = OrderFactory(status='completed') db.session.commit() # Attendees associated with the order without discount code should not be counted AttendeeFactoryBase.create_batch( 10, order_id=order_without_discount.id, ticket_id=ticket.id, event_id=ticket.event_id, ) assert discount_code.is_available(ticket_holders=[1]) is True order_with_discount = OrderFactory(status='completed', discount_code_id=discount_code.id) db.session.commit() # Attendees associated with the order with discount code should be counted AttendeeFactoryBase.create_batch(5, order_id=order_with_discount.id, ticket_id=ticket.id, event_id=ticket.event_id) assert discount_code.is_available(ticket_holders=[1]) is False assert discount_code.confirmed_attendees_count == 5
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 _create_donation_tickets(db): event = EventFactoryBasic() tickets = _create_tickets([10, 20], event=event) tickets.append( TicketSubFactory(type='donation', max_price=20, min_price=10, event=event) ) db.session.commit() return _create_ticket_dict(tickets[::-1], [2, 1, 3])
def test_reject_tickets_of_different_events(db): tickets = TicketSubFactory.create_batch(3) db.session.commit() with pytest.raises( UnprocessableEntityError, match=r'All tickets must belong to same event. Found: .*', ): validate_tickets([ticket.id for ticket in tickets])
def test_validate_discount_code_accept_mix_deleted_tickets(db): discount, tickets = _create_discount_code(db) deleted_tickets = TicketSubFactory.create_batch(2, deleted_at=datetime.now()) discount.tickets += deleted_tickets db.session.commit() assert (discount.validate(tickets=tickets + [{ 'id': ticket.id } for ticket in deleted_tickets]) == discount)
def _create_taxed_tickets(db, tax_included=True, discount_code=None): tax = TaxSubFactory(name='GST', rate=18.0, is_tax_included_in_price=tax_included) tickets = _create_tickets([123.5, 456.3], event=tax.event) tickets += [ TicketSubFactory( type='donation', event=tax.event, min_price=500.0, max_price=1000.0 ), TicketSubFactory(type='free', price=435.0, event=tax.event), ] if discount_code: discount_code.tickets = [tickets[1]] + tickets[2:] db.session.commit() tickets_dict = _create_ticket_dict(tickets, [2, 4, 3, 3]) # Set price of donation ticket tickets_dict[-2]['price'] = 789.7 return tickets_dict
def test_validate_discount_code_reject_deleted_tickets(db): discount, _ = _create_discount_code(db) deleted_tickets = TicketSubFactory.create_batch(2, deleted_at=datetime.now()) discount.tickets += deleted_tickets db.session.commit() with pytest.raises(UnprocessableEntityError, match='Invalid Discount Code'): discount.validate(tickets=[{ 'id': ticket.id } for ticket in deleted_tickets])
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_single_ticket(db): ticket = TicketSubFactory(price=10) db.session.commit() amount_data = calculate_order_amount([{'id': ticket.id}]) assert amount_data['total'] == 10.0 assert amount_data['tax'] is None assert amount_data['discount'] == 0.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'] == 10.0
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_ticket_sales_end(db, client, admin_jwt): ticket = TicketSubFactory() db.session.commit() data = json.dumps({ 'data': { 'type': 'ticket', 'id': str(ticket.id), "attributes": { "sales-ends-at": "2199-10-01T1:00:00+00:00" }, } }) response = client.patch( f'/v1/tickets/{ticket.id}', content_type='application/vnd.api+json', headers=admin_jwt, data=data, ) assert json.loads(response.data) == { 'errors': [{ 'detail': "End of ticket sales date of 'example' cannot be after " 'end of event date', 'source': { 'sales_ends_at': '/data/attributes/sales-ends-at' }, 'status': 422, 'title': 'Unprocessable Entity', }], 'jsonapi': { 'version': '1.0' }, } assert response.status_code == 422
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_tickets(db): tickets = TicketSubFactory.create_batch(3, event=EventFactoryBasic()) db.session.commit() assert validate_tickets([str(ticket.id) for ticket in tickets]) == tickets
def _create_tickets(prices, **kwargs): return [TicketSubFactory(price=price, **kwargs) for price in prices]
def test_ticket_of_deleted_event(db): ticket = TicketSubFactory(event__deleted_at=datetime.now()) db.session.commit() with pytest.raises(ObjectNotFound): calculate_order_amount([{'id': ticket.id}])