def test_get_seat_ids_where_ids_are_missing(self): ticket_order_one = TicketOrder('a', seats=[ Seat('A1'), Seat('A2'), Seat(None), ]) ticket_order_two = TicketOrder('b', seats=[ Seat('B1'), Seat(None), Seat('B3'), ]) order = Order(1, ticket_orders=[ticket_order_one, ticket_order_two]) seat_ids = order.get_seat_ids() assert seat_ids == ['A1', 'A2', 'B1', 'B3']
def test_get_event_ids(self): event_one = Event(id_='abc123') event_two = Event(id_='def456') bundle = Bundle('tests', orders=[ Order(item=1, event=event_one), Order(item=2, event=event_two), ]) events = bundle.get_event_ids() assert events == {'abc123', 'def456'}
def test_get_orders(self): #state order_one = Order(1) order_two = Order(2) order_three = Order(3) order_four = Order(4) bundle_one = Bundle('tests', orders=[order_one, order_three]) bundle_two = Bundle('tests_two', orders=[order_two, order_four]) trolley = Trolley(bundles=[bundle_one, bundle_two]) # results orders = trolley.get_orders() assert orders == [order_one, order_three, order_two, order_four]
def test_unique_seat_text(self): ticket_order_one = TicketOrder('a', seats=[ Seat('A1', seat_text='Hell bad'), Seat('A2'), Seat('A3'), ]) ticket_order_two = TicketOrder('b', seats=[ Seat('B1', seat_text='Hell good'), Seat('B2'), Seat('B3'), ]) order = Order(1, ticket_orders=[ticket_order_one, ticket_order_two]) seat_text = order.unique_seat_text() assert 'Hell bad' in seat_text assert 'Hell good' in seat_text
def test_get_seats(self): ticket_order_one = TicketOrder('a', seats=[ Seat('A1'), Seat('A2'), Seat('A3'), ]) ticket_order_two = TicketOrder('b', seats=[ Seat('B1'), Seat('B2'), Seat('B3'), ]) order = Order(1, ticket_orders=[ticket_order_one, ticket_order_two]) seats = order.get_seats() assert [seat.id for seat in seats] == [ 'A1', 'A2', 'A3', 'B1', 'B2', 'B3', ]
def test_from_api_data_with_actual_commission(self): data = { "gross_commission": { "amount_excluding_vat": 18.75, "amount_including_vat": 22.5, "commission_currency_code": "gbp" }, "item_number": 1, "user_commission": { "amount_excluding_vat": 12.5, "amount_including_vat": 15, "commission_currency_code": "gbp" } } order = Order.from_api_data(data) assert type(order.user_commission) is Commission assert order.user_commission.including_vat == 15 assert order.user_commission.excluding_vat == 12.5 assert order.user_commission.currency_code == "gbp" assert type(order.gross_commission) is Commission assert order.gross_commission.including_vat == 22.5 assert order.gross_commission.excluding_vat == 18.75 assert order.gross_commission.currency_code == "gbp"
def from_api_data(cls, data): """Creates a new Trolley object from API data from ticketswitch. Args: data (dict): the part of the response from a ticketswitch API call that concerns a trolley. Returns: :class:`Trolley <pyticketswitch.trolley.Trolley>`: a new :class:`Trolley <pyticketswitch.trolley.Trolley>` object populated with the data from the api. """ raw_contents = data.get('trolley_contents', {}) if not raw_contents: raw_contents = data.get('reserved_trolley', {}) if not raw_contents: raw_contents = data.get('trolley_token_contents', {}) raw_bundles = raw_contents.get('bundle', []) bundles = [Bundle.from_api_data(bundle) for bundle in raw_bundles] raw_discarded_orders = data.get('discarded_orders', []) discarded_orders = [ Order.from_api_data(order) for order in raw_discarded_orders ] kwargs = { 'token': data.get('trolley_token'), 'bundles': bundles, 'discarded_orders': discarded_orders, 'transaction_uuid': raw_contents.get('transaction_uuid'), 'transaction_id': raw_contents.get('transaction_id'), 'order_count': data.get('trolley_order_count'), 'input_contained_unavailable_order': data.get('input_contained_unavailable_order', False), } minutes = data.get('minutes_left_on_reserve') if minutes is not None: kwargs.update(minutes_left=float(minutes)) purchase_result = raw_contents.get('purchase_result') if purchase_result: kwargs.update( purchase_result=PurchaseResult.from_api_data(purchase_result)) return cls(**kwargs)
def from_api_data(cls, data): """Creates a new Trolley object from API data from ticketswitch. Args: data (dict): the part of the response from a ticketswitch API call that concerns a trolley. Returns: :class:`Trolley <pyticketswitch.trolley.Trolley>`: a new :class:`Trolley <pyticketswitch.trolley.Trolley>` object populated with the data from the api. """ raw_contents = data.get('trolley_contents', {}) if not raw_contents: raw_contents = data.get('reserved_trolley', {}) if not raw_contents: raw_contents = data.get('trolley_token_contents', {}) raw_bundles = raw_contents.get('bundle', []) bundles = [ Bundle.from_api_data(bundle) for bundle in raw_bundles ] raw_discarded_orders = data.get('discarded_orders', []) discarded_orders = [ Order.from_api_data(order) for order in raw_discarded_orders ] kwargs = { 'token': data.get('trolley_token'), 'bundles': bundles, 'discarded_orders': discarded_orders, 'transaction_uuid': raw_contents.get('transaction_uuid'), 'transaction_id': raw_contents.get('transaction_id'), 'order_count': data.get('trolley_order_count'), 'input_contained_unavailable_order': data.get( 'input_contained_unavailable_order', False), } minutes = data.get('minutes_left_on_reserve') if minutes is not None: kwargs.update(minutes_left=float(minutes)) purchase_result = raw_contents.get('purchase_result') if purchase_result: kwargs.update( purchase_result=PurchaseResult.from_api_data(purchase_result)) return cls(**kwargs)
def from_api_data(cls, data): """Creates a new Bundle object from API data from ticketswitch. Args: data (dict): the part of the response from a ticketswitch API call that concerns a bundle. Returns: :class:`Bundle <pyticketswitch.bundle.Bundle>`: a new :class:`Bundle <pyticketswitch.bundle.Bundle>` object populated with the data from the api. """ kwargs = { 'source_code': data.get('bundle_source_code'), 'description': data.get('bundle_source_desc'), 'currency_code': data.get('currency_code'), 'terms_and_conditions': data.get('source_t_and_c'), } raw_orders = data.get('order') if raw_orders: orders = [Order.from_api_data(order) for order in raw_orders] kwargs.update(orders=orders) # Below we are explicital checking for not None because we want to # differentiate between situtations where a value is 0 and a value is # missing from the response. total_seatprice = data.get('bundle_total_seatprice') if total_seatprice is not None: kwargs.update(total_seatprice=total_seatprice) total_surcharge = data.get('bundle_total_surcharge') if total_surcharge is not None: kwargs.update(total_surcharge=total_surcharge) total_send_cost = data.get('bundle_total_send_cost') if total_send_cost is not None: kwargs.update(total_send_cost=total_send_cost) total = data.get('bundle_total_cost') if total is not None: kwargs.update(total=total) raw_debitor = data.get('debitor') if raw_debitor: debitor = Debitor.from_api_data(raw_debitor) kwargs.update(debitor=debitor) raw_purchase_result = data.get('purchase_result') if raw_purchase_result: purchase_result = PurchaseResult.from_api_data(raw_purchase_result) kwargs.update(purchase_result=purchase_result) return cls(**kwargs)
def test_from_api_data_without_commission(self): data = { 'item_number': 1, "send_method": { "send_code": "COBO", }, } order = Order.from_api_data(data) assert order.gross_commission is None assert order.user_commission is None
def test_from_api_data_with_send_method(self): data = { 'item_number': 1, "send_method": { "send_code": "COBO", }, } order = Order.from_api_data(data) assert order.send_method.code == 'COBO'
def from_api_data(cls, data): kwargs = { "cancelled_item_numbers": data.get("cancelled_item_numbers", []), "trolley": Trolley.from_api_data(data), } raw_must_also_cancel = data.get("must_also_cancel") if raw_must_also_cancel: must_also_cancel = [ Order.from_api_data(order) for order in raw_must_also_cancel ] kwargs.update(must_also_cancel=must_also_cancel) return cls(**kwargs)
def test_get_event_ids(self): event_one = Event(id_='abc123') event_two = Event(id_='def456') event_three = Event(id_='ghi789') event_four = Event(id_='abc123') bundle_one = Bundle('tests', orders=[ Order(item=1, event=event_one), Order(item=2, event=event_two), ]) bundle_two = Bundle('tests_two', orders=[ Order(item=3, event=event_three), Order(item=4, event=event_four), ]) trolley = Trolley(bundles=[bundle_one, bundle_two]) events = trolley.get_event_ids() assert events == {'abc123', 'def456', 'ghi789'}
def test_is_purchased(self): pr = PurchaseResult(success=True) bundle_orders = [Order(item=1, event=Event(id_='TEST'))] bundle = Bundle( 'tests', orders=bundle_orders, purchase_result=pr, ) no_pr_bundle = Bundle( 'tests', orders=bundle_orders, ) assert bundle.is_purchased() assert not no_pr_bundle.is_purchased()
def test_from_api_data_with_decimal(self): data = { "backend_purchase_reference": 'GHI098', "event": { "event_id": "6IF", }, "item_number": 1, "performance": { "perf_id": "6IF-A7N", }, "price_band_code": "C/pool", "price_band_desc": "Band C", "seat_request_status": "got_all", "ticket_orders": { "ticket_order": [ {"discount_code": "ADULT"}, {"discount_code": "CHILD"}, ] }, "ticket_type_code": "CIRCLE", "ticket_type_desc": "Upper circle", "total_no_of_seats": 3, "total_sale_seatprice": 51, "total_sale_surcharge": Decimal('5.40'), "requested_seat_ids": [ 'ABC123', 'DEF456', ], 'send_method': { 'send_code': 'POST', 'send_cost': Decimal('3.5'), 'send_desc': 'Post (UK & Ireland only)', 'send_type': 'post', 'permitted_countries': { 'country': [ { 'country_code': 'ie', 'country_desc': 'Ireland' }, { 'country_code': 'uk', 'country_desc': 'United Kingdom' } ] } }, 'predicted_gross_commission': { 'amount_including_vat': Decimal('4.0'), 'amount_excluding_vat': Decimal('4.5'), 'commission_currency_code': 'gbp', }, 'predicted_user_commission': { 'amount_including_vat': Decimal('2.0'), 'amount_excluding_vat': Decimal('2.1'), 'commission_currency_code': 'gbp', }, } order = Order.from_api_data(data) assert order.item == 1 assert order.price_band_code == 'C/pool' assert order.price_band_description == 'Band C' assert order.ticket_type_code == 'CIRCLE' assert order.ticket_type_description == 'Upper circle' assert order.number_of_seats == 3 assert order.total_seatprice == 51 assert order.total_surcharge == Decimal('5.40') assert len(order.ticket_orders) == 2 assert len(order.requested_seat_ids) == 2 assert order.total_including_send_cost() == (51 + Decimal('5.40') + Decimal('3.5')) assert isinstance(order.gross_commission, Commission) assert order.gross_commission.including_vat == Decimal('4.0') assert order.gross_commission.excluding_vat == Decimal('4.5') assert order.gross_commission.currency_code == 'gbp' assert isinstance(order.user_commission, Commission) assert order.user_commission.including_vat == Decimal('2.0') assert order.user_commission.excluding_vat == Decimal('2.1') assert order.user_commission.currency_code == 'gbp'
def test_repr(self): order = Order(1, ticket_orders=[]) assert repr(order) == '<Order 1>'
def test_get_seats_with_no_ticket_orders(self): order = Order(1, ticket_orders=[]) assert order.get_seats() == []
def test_from_api_data(self): data = { "backend_purchase_reference": 'GHI098', "backend_cancellation_reference": 'GHI098-cancelled', "external_management_url": 'www.example.com/some-more-decisions', "cancellation_status": "possible", "cancellation_comment": "", "event": { "event_id": "6IF", }, "item_number": 1, "performance": { "perf_id": "6IF-A7N", }, "price_band_code": "C/pool", "price_band_desc": "Band C", "seat_request_status": "got_all", "ticket_orders": { "ticket_order": [ {"discount_code": "ADULT"}, {"discount_code": "CHILD"}, ] }, "ticket_type_code": "CIRCLE", "ticket_type_desc": "Upper circle", "total_no_of_seats": 3, "total_sale_seatprice": 51, "total_sale_surcharge": 5.40, "requested_seat_ids": [ 'ABC123', 'DEF456', ], 'send_method': { 'send_code': 'POST', 'send_cost': 3.5, 'send_desc': 'Post (UK & Ireland only)', 'send_type': 'post', 'permitted_countries': { 'country': [ { 'country_code': 'ie', 'country_desc': 'Ireland' }, { 'country_code': 'uk', 'country_desc': 'United Kingdom' } ] } }, 'predicted_gross_commission': { 'amount_including_vat': 4.0, 'amount_excluding_vat': 4.5, 'commission_currency_code': 'gbp', }, 'predicted_user_commission': { 'amount_including_vat': 2.0, 'amount_excluding_vat': 2.1, 'commission_currency_code': 'gbp', }, } order = Order.from_api_data(data) assert order.item == 1 assert order.price_band_code == 'C/pool' assert order.price_band_description == 'Band C' assert order.ticket_type_code == 'CIRCLE' assert order.ticket_type_description == 'Upper circle' assert order.number_of_seats == 3 assert order.total_seatprice == 51 assert order.total_surcharge == 5.40 assert order.seat_request_status == "got_all" assert order.backend_purchase_reference == 'GHI098' assert order.backend_cancellation_reference == 'GHI098-cancelled' assert order.cancellation_status == "possible" assert order.cancellation_comment == "" assert order.external_management_url == 'www.example.com/some-more-decisions' assert isinstance(order.event, Event) assert order.event.id == '6IF' assert isinstance(order.performance, Performance) assert order.performance.id == '6IF-A7N' assert len(order.ticket_orders) == 2 assert order.ticket_orders[0].code == 'ADULT' assert order.ticket_orders[1].code == 'CHILD' assert len(order.requested_seat_ids) == 2 assert order.requested_seat_ids[0] == 'ABC123' assert order.requested_seat_ids[1] == 'DEF456' assert order.total_including_send_cost() == (51 + 5.40 + 3.5) assert isinstance(order.gross_commission, Commission) assert order.gross_commission.including_vat == 4.0 assert order.gross_commission.excluding_vat == 4.5 assert order.gross_commission.currency_code == 'gbp' assert isinstance(order.user_commission, Commission) assert order.user_commission.including_vat == 2.0 assert order.user_commission.excluding_vat == 2.1 assert order.user_commission.currency_code == 'gbp'