def save(self, auction: Auction) -> None: raw_auction = { "title": auction.title, "starting_price": auction.starting_price.amount, "current_price": auction.current_price.amount, "ends_at": auction.ends_at, "ended": auction._ended, } update_result = self._conn.execute( auctions.update(values=raw_auction, whereclause=auctions.c.id == auction.id)) assert update_result.rowcount == 1 # no support for creating for bid in auction.bids: if bid.id: continue result = self._conn.execute( bids.insert( values={ "auction_id": auction.id, "amount": bid.amount.amount, "bidder_id": bid.bidder_id })) (bid.id, ) = result.inserted_primary_key if auction.withdrawn_bids_ids: self._conn.execute( bids.delete( whereclause=bids.c.id.in_(auction.withdrawn_bids_ids))) for event in auction.domain_events: self._event_bus.post(event) auction.clear_events()
def test_saves_auction_changes( connection: Connection, another_bidder_id: int, bid_model: RowProxy, auction_model_with_a_bid: RowProxy, ends_at: datetime, event_bus_mock: Mock, ) -> None: new_bid_price = get_dollars(bid_model.amount * 2) auction = Auction( id=auction_model_with_a_bid.id, title=auction_model_with_a_bid.title, starting_price=get_dollars(auction_model_with_a_bid.starting_price), ends_at=ends_at, bids=[ Bid(bid_model.id, bid_model.bidder_id, get_dollars(bid_model.amount)), Bid(None, another_bidder_id, new_bid_price), ], ended=True, ) SqlAlchemyAuctionsRepo(connection, event_bus_mock).save(auction) assert connection.execute(select([func.count() ]).select_from(bids)).scalar() == 2 proxy = connection.execute( select([ auctions ]).where(auctions.c.id == auction_model_with_a_bid.id)).first() assert proxy.current_price == new_bid_price.amount assert proxy.ended
def auction_with_a_winner(input_dto: WithdrawingBidsInputDto) -> Auction: losing_bid = Bid(id=4, bidder_id=2, amount=get_dollars('5.50')) winning_bid = Bid(id=2, bidder_id=1, amount=get_dollars('6.00')) return Auction(id=2, title='does not matter', initial_price=get_dollars('5.00'), bids=[winning_bid, losing_bid])
def test_removes_withdrawn_bids( auction_model_with_a_bid: AuctionModel) -> None: bid_model = auction_model_with_a_bid.bid_set.first() auction = Auction(id=auction_model_with_a_bid.id, title=auction_model_with_a_bid.title, initial_price=get_dollars( auction_model_with_a_bid.initial_price), bids=[ Bid(bid_model.id, bid_model.bidder_id, get_dollars(bid_model.amount)), ]) auction.withdrawn_bids_ids = [bid_model.id] DjangoORMAuctionsRepository().save(auction) assert auction_model_with_a_bid.bid_set.count() == 0
def execute(self, input_dto: BeginningAuctionInputDto) -> None: if input_dto.ends_at < datetime.now(tz=input_dto.ends_at.tzinfo): raise AuctionEndingInThePast auction = Auction.create(input_dto.auction_id, input_dto.title, input_dto.starting_price, input_dto.ends_at) self.auctions_repo.save(auction)
def test_removes_withdrawn_bids(connection: Connection, bid_model: RowProxy, auction_model_with_a_bid: dict, ends_at: datetime, event_bus_mock: Mock) -> None: auction = Auction( id=auction_model_with_a_bid.id, title=auction_model_with_a_bid.title, starting_price=get_dollars(auction_model_with_a_bid.starting_price), ends_at=ends_at, bids=[ Bid(bid_model.id, bid_model.bidder_id, get_dollars(bid_model.amount)) ], ended=False, ) auction.withdraw_bids([bid_model.id]) SqlAlchemyAuctionsRepo(connection, event_bus_mock).save(auction) assert connection.execute(select([func.count() ]).select_from(bids)).scalar() == 0
def get(self, auction_id: int) -> Auction: from auctions.models import Auction as AuctionModel auction_model = AuctionModel.objects.prefetch_related('bid_set').get(id=auction_id) return Auction( id=auction_model.id, title=auction_model.title, initial_price=get_dollars(auction_model.initial_price), bids=[ Bid(id=bid_model.id, bidder_id=bid_model.bidder_id, amount=get_dollars(bid_model.amount)) for bid_model in auction_model.bid_set.all() ] )
def _row_to_entity(self, auction_proxy: RowProxy, bids_proxies: List[RowProxy]) -> Auction: auction_bids = [ Bid(bid.id, bid.bidder_id, get_dollars(bid.amount)) for bid in bids_proxies ] return Auction( auction_proxy.id, auction_proxy.title, get_dollars(auction_proxy.starting_price), auction_bids, auction_proxy.ends_at.replace(tzinfo=pytz.UTC), auction_proxy.ended, )
def test_saves_auction_changes(auction_model_with_a_bid: AuctionModel) -> None: bid_model = auction_model_with_a_bid.bid_set.first() auction = Auction(id=auction_model_with_a_bid.id, title=auction_model_with_a_bid.title, initial_price=get_dollars( auction_model_with_a_bid.initial_price), bids=[ Bid(bid_model.id, bid_model.bidder_id, get_dollars(bid_model.amount)), Bid(None, bid_model.bidder_id, get_dollars(bid_model.amount)) ]) DjangoORMAuctionsRepository().save(auction) assert auction_model_with_a_bid.bid_set.count() == 2
def save(self, auction: Auction) -> None: for event in auction.domain_events: self._event_bus.post(event) auction.clear_events() self._data[auction.id] = auction
def create_auction(bids: typing.List[Bid] = None) -> Auction: if not bids: bids = [] return Auction(id=1, title='', initial_price=get_dollars('10'), bids=bids)
def auction(exemplary_auction_id: int) -> Auction: return Auction(id=exemplary_auction_id, title='irrelevant', initial_price=get_dollars('2.00'), bids=[])