Exemplo n.º 1
0
def test_infer_bids():
    raw_event = '13|5|BID|toaster_1|12.50'
    event = Event.decode(raw_event)
    assert isinstance(event, Bid)
    assert event.timestamp == 13
    assert event.user_id == '5'
    assert event.item == 'toaster_1'
    assert event.bid_amount == 12.50
Exemplo n.º 2
0
def test_infer_listing():
    raw_event = '10|1|SELL|toaster_1|10.00|20'
    event = Event.decode(raw_event)
    assert isinstance(event, Listing)
    assert event.timestamp == 10
    assert event.user_id == '1'
    assert event.item == 'toaster_1'
    assert event.reserve_price == 10
    assert event.end_time == 20
Exemplo n.º 3
0
def process(raw_events: List) -> Generator:
    auctions = {}
    errors = []

    for raw_event in raw_events:

        event = Event.decode(raw_event)

        if event is None:
            errors.append({'error': raw_event})

        if isinstance(event, Listing):
            auction = Auction(item=event.item,
                              end_time=event.end_time,
                              reserve_price=event.reserve_price,
                              bids=[])
            auctions[event.item] = auction
            continue

        # Route bid to correct auction
        if isinstance(event, Bid):
            if event.item in auctions:
                auctions[event.item].bid(event)
                continue

        # Broadcast HeartBeat to all auctions
        if isinstance(event, HeartBeat):
            for _, auction in auctions.items():
                auction.update(event)
                continue

    for auction in auctions.values():
        yield str(auction)

    if any(errors):
        yield errors
Exemplo n.º 4
0
def test_garbage_does_not_create_event():
    raw_event = 'adsfasdf'
    event = Event.decode(raw_event)
    assert event is None
Exemplo n.º 5
0
def test_infer_heartbeat():
    raw_event = '10'
    event = Event.decode(raw_event)
    assert isinstance(event, HeartBeat)
    assert event.timestamp == 10