def test_end_auction(client: TestClient, superuser_token_headers: dict, db: Session): auction = create_random_auction(db) amount = auction.reserve + random_float() bidder = create_random_user(db) english = EnglishAuction() ending_date = (datetime.now() + timedelta(days=1)).isoformat() english.start(db, db_obj=auction, ending_date=ending_date) english.bid(db, db_obj=auction, amount=amount, bidder_id=bidder.id) response = client.put( f"{settings.API_PREFIX}/v1/auctions/{auction.id}/end", headers=superuser_token_headers) assert response.status_code == 200 content = response.json() db.refresh(auction) assert auction.state == AuctionState.ENDED assert content["id"] assert content["owner_id"] assert content["starting_amount"] == auction.starting_amount assert content["reserve"] == auction.reserve assert content["bid_cap"] == auction.bid_cap assert content["product_id"] == auction.product_id assert content["state"] == AuctionState.ENDED
def test_start(db: Session): auction = create_random_auction(db) assert auction.state == AuctionState.CREATED english = EnglishAuction() ending_date = (datetime.now() + timedelta(days=1)).isoformat() english.start(db, db_obj=auction, ending_date=ending_date) auction = auction_repo.get(db, id=auction.id) assert auction.state == AuctionState.ONGOING
def test_buy_it_now(db: Session): auction = create_random_auction(db) english = EnglishAuction() ending_date = (datetime.now() + timedelta(days=1)).isoformat() english.start(db, db_obj=auction, ending_date=ending_date) assert auction.state == AuctionState.ONGOING buyer = create_random_user(db) english.buy_it_now(db, db_obj=auction, buyer_id=buyer.id) db_obj = auction_repo.get(db, id=auction.id) assert db_obj.state == AuctionState.ENDED assert db_obj.current_bid_amount == db_obj.bid_cap assert db_obj.final_winner_id == buyer.id
def test_buy_it_now(client: TestClient, superuser_token_headers: dict, db: Session): auction = create_random_auction(db) english = EnglishAuction() english.start(db, db_obj=auction) response = client.post( f"{settings.API_PREFIX}/v1/auctions/{auction.id}/buy_it_now", headers=superuser_token_headers) assert response.status_code == 200 content = response.json() db.refresh(auction) assert auction.final_winner assert auction.state == AuctionState.ENDED
def test_bid(db: Session): auction = create_random_auction(db) english = EnglishAuction() ending_date = (datetime.now() + timedelta(days=1)).isoformat() english.start(db, db_obj=auction, ending_date=ending_date) assert auction.state == AuctionState.ONGOING amount = auction.starting_amount + 1 bidder = create_random_user(db) english.bid(db, db_obj=auction, amount=amount, bidder_id=bidder.id) db_obj = auction_repo.get(db, id=auction.id) assert db_obj.current_bid_amount == amount + 1.25 assert db_obj.last_bid_at assert db_obj.winning_bid.amount == amount assert db_obj.bids
def test_end_when_reserve_not_met(db: Session): auction = create_random_auction(db) english = EnglishAuction() ending_date = (datetime.now() + timedelta(days=1)).isoformat() english.start(db, db_obj=auction, ending_date=ending_date) assert auction.state == AuctionState.ONGOING amount = auction.current_bid_amount + 2 bidder = create_random_user(db) english.bid(db, db_obj=auction, amount=amount, bidder_id=bidder.id) db_obj = auction_repo.get(db, id=auction.id) assert db_obj.winning_bid english.end(db, db_obj=db_obj) assert db_obj.state == AuctionState.CANCLED assert not db_obj.final_winner_id
def start_auction(*, id: int, starting_date: datetime = Body(None), db: Session = Depends(get_db)): auction = auction_repo.get(db, id=id) if not auction: raise AUCTION_NOT_FOUND_EXCEPTION english = EnglishAuction() return english.start(db, db_obj=auction, starting_date=starting_date)
def test_bid_auction(client: TestClient, superuser_token_headers: dict, db: Session): auction = create_random_auction(db) amount = auction.reserve + random_float() english = EnglishAuction() english.start(db, db_obj=auction) data = amount response = client.post( f"{settings.API_PREFIX}/v1/auctions/{auction.id}/bid", headers=superuser_token_headers, json=data) assert response.status_code == 200 content = response.json() db.refresh(auction) assert auction.winning_bid assert auction.last_bid_at
def start_auction(*, id: int, starting_date: datetime = Body(None), ending_date: datetime = Body(...), db: Session = Depends(get_db), current_user: User = Depends(get_current_active_user)): auction = auction_repo.get(db, id=id) if not auction: raise AUCTION_NOT_FOUND_EXCEPTION if auction.state != AuctionState.CREATED: if auction.state == AuctionState.ONGOING: raise AUCTION_ONGOING_EXCEPTION elif auction.state == AuctionState.ENDED: raise AUCTION_ENDED_EXCEPTION elif auction.state == AuctionState.CANCLED: raise AUCTION_CANCELED_EXCEPTION english = EnglishAuction() return english.start(db, db_obj=auction, starting_date=starting_date, ending_date=ending_date)