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_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_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 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_validate_tickets(db):
    tickets = TicketSubFactory.create_batch(3, event=EventFactoryBasic())
    db.session.commit()

    assert validate_tickets([str(ticket.id) for ticket in tickets]) == tickets