def test_lot_sold(self):
        """
        """       
        
        context = decimal.Context(prec=20, rounding=decimal.ROUND_HALF_DOWN)
        decimal.setcontext(context)
    
        category = MarketCategory.objects.all()[0]
        
        now = datetime.datetime.now()
        now_plus_10 = now + datetime.timedelta(seconds=5)
        
        auction = AuctionSession(shop=self.shop, title="Auction Session Nr 1", description="-- no desc --", start=now, end=now_plus_10)
        auction.save()
        
        lot = Lot(shop = self.shop,
                  title = "Coin From Rusia 1901 (PCGS 60)",
                  description = "rare coin",
                  category = category,
                  date_time = now,
                  weight = "5",
                  session=auction, 
                  starting_bid=decimal.Decimal("10.00"), 
                  reserve=decimal.Decimal("0.00"))
        lot.save()
        
        success = self.client.login(username='******', password='******')
        self.assertEqual(success, True, "login failed")
        
        bidder = User.objects.filter(username="******").get()
        cart = Cart(shop=self.shop, bidder=bidder)
        cart.save()
        
        #Check that lot is still active...
        self.assertEqual(lot.is_active(), True , "Failed: The lot should be active!")
        self.assertEqual(lot.bidhistory_set.all().count(), 0 , "Failed: The lot should not have any bid yet!")
        
        my_bid = decimal.Decimal("19.00")
        response = self.client.get(reverse("bidding_view_lot", args=[lot.id]), {'amount': my_bid}, HTTP_HOST=self.HTTP_HOST)
        self.assertEqual(response.status_code, 302, "Failed when trying to bid a valid amount $19.00. This value should be allowed...")    
        
        lot = Lot.objects.get(id=lot.id)
        
        self.assertNotEqual(lot.bidhistory_set.all().count(), 0 , "Failed: The lot should have at least one bid!")
        logging.info("waiting to auction session finish...")
        
        while not lot.session.finished():
            time.sleep(1)
            
        logging.info("Running cron...")
        cron.minute_update()
        
        lot = Lot.objects.get(id=lot.id)

        self.assertEqual(lot.reserve_has_been_met(), True , "Failed, reserved has not been met!")
        self.assertEqual(lot.bid_actual.bid_amount, my_bid , "Failed: The bid actual is wrong, is %s but should be %s" % (lot.bid_actual.bid_amount, my_bid))
        self.assertEqual(lot.bid_actual.bidder.username, "test" , "Failed, wrong bidder won!")
        self.assertEqual(lot.is_active(), False, "Failed: The lot state is wrong, should be SOLD but it is %s" % lot.state)
        self.assertEqual(lot.is_didnt_sell(), False, "Failed: The lot state is wrong, should be SOLD but it is %s" % lot.state)
        self.assertEqual(lot.is_sold(), True, "Failed: The lot state is wrong, should be SOLD but it is %s" % lot.state)
    def test_lot_didnt_sell(self):
        """
        Check that a lot get state DIDN'T SELL when there no bidding...
        """       
        
        context = decimal.Context(prec=20, rounding=decimal.ROUND_HALF_DOWN)
        decimal.setcontext(context)
    
        category = MarketCategory.objects.all()[0]
        
        now = datetime.datetime.now()
        now_plus_10 = now + datetime.timedelta(seconds=5)
        
        auction = AuctionSession(shop=self.shop, title="Auction Session Nr 2", description="-- no desc --", start=now, end=now_plus_10)
        auction.save()
        
        lot = Lot(shop = self.shop,
                  title = "Coin From Argentina 1890 (PCGS 60)",
                  description = "rare coin",
                  category = category,
                  date_time = now,
                  weight = "5",
                  session=auction, 
                  starting_bid=decimal.Decimal("100.00"), 
                  reserve=decimal.Decimal("0.00"))
        lot.save()
        
        success = self.client.login(username='******', password='******')
        self.assertEqual(success, True, "login failed")
        
        #Check that lot is still active...
        self.assertEqual(lot.is_active(), True , "Failed: The lot should be active!")
        self.assertEqual(lot.bidhistory_set.all().count(), 0 , "Failed: The lot should not have any bid yet!")
        
        my_bid = decimal.Decimal("90.00")
        response = self.client.get(reverse("bidding_view_lot", args=[lot.id]), {'amount': my_bid}, HTTP_HOST=self.HTTP_HOST)
        self.assertEqual(response.status_code, 200, "Failed: this bid is not valid...")    
        
        self.assertEqual(lot.bidhistory_set.all().count(), 0 , "Failed: The lot should not have any bid yet!")
        
        logging.info("waiting to auction session finish...")
        
        while not lot.session.finished():
            time.sleep(1)
        
        logging.info("Running cron...")
        cron.minute_update()
        
        lot = Lot.objects.get(id=lot.id)

        self.assertEqual(lot.reserve_has_been_met(), False , "Failed, the reserved price should not be reached!")
        self.assertEqual(lot.bid_actual, None , "Failed: There were no bids! ")
        self.assertEqual(lot.is_active(), False, "Failed: The lot could not be active, the lot finished and there were no bids!")
        self.assertEqual(lot.is_sold(), False, "Failed: The lot could not be sold, there were no bids!")
        self.assertEqual(lot.is_didnt_sell(), True, "Failed: The lot wasn't sell!")
    def test_lot_still_active(self):
        """
        Check that nothing happend to those lots that aren't finished yet when cron is executed
        """ 
        context = decimal.Context(prec=20, rounding=decimal.ROUND_HALF_DOWN)
        decimal.setcontext(context)
    
        category = MarketCategory.objects.all()[0]
        
        now = datetime.datetime.now()
        now_plus_10 = now + datetime.timedelta(seconds=5)
        
        auction = AuctionSession(shop=self.shop, title="Auction Session Nr 4", description="-- no desc --", start=now, end=now_plus_10)
        auction.save()
        
        lot = Lot(shop=self.shop,
                  title="Coin From USA 1905 (PCGS 50)",
                  description="rare coin",
                  category=category,
                  date_time=now,
                  weight="5",
                  session=auction, 
                  starting_bid=decimal.Decimal("100.00"), 
                  reserve=decimal.Decimal("300.00"))
        lot.save()
        
        success = self.client.login(username='******', password='******')
        self.assertEqual(success, True, "login failed")
        
        my_bid = decimal.Decimal("120.00")
        response = self.client.get(reverse("bidding_view_lot", args=[lot.id]), {'amount': my_bid}, HTTP_HOST=self.HTTP_HOST)
        self.assertEqual(response.status_code, 302, "Failed when trying to bid a valid amount %s. This value should be allowed..." % my_bid)    
        
        lot = Lot.objects.get(id=lot.id)
        self.assertEqual(lot.bidhistory_set.all().count(), 1 , "Failed: The lot should not have any bid yet!")
        
        logging.info("don't wait for auction session to finish...")

        #Check that lot is still active...
        lot = Lot.objects.get(id=lot.id)
        self.assertEqual(lot.is_active(), True , "Failed: The lot should be active!")

        logging.info("Running cron...")
        
        cron.minute_update()
        
        lot = Lot.objects.get(id=lot.id)

        self.assertEqual(lot.is_active(), True , "Failed: The lot should be active!")
        self.assertEqual(lot.bid_actual.bid_amount, my_bid, "Failed: The bid actual is wrong, is %s but should be %s" % (lot.bid_actual.bid_amount, my_bid))
        self.assertEqual(lot.bid_actual.bidder.username, "test" , "Failed, wrong bidder won!")
        self.assertEqual(lot.is_sold(), False, "Failed: The lot state is wrong, should be ACTIVE but it is %s" % lot.state)
    def test_lot_didnt_sell2(self):
        """
        Check that a lot get state DIDN'T SELL when there are no biddings that reach the reserve price
        """       
        context = decimal.Context(prec=20, rounding=decimal.ROUND_HALF_DOWN)
        decimal.setcontext(context)
        
        category = MarketCategory.objects.all()[0]
        
        now = datetime.datetime.now()
        now_plus_10 = now + datetime.timedelta(seconds=5)
        
        auction = AuctionSession(shop=self.shop, title="Auction Session Nr 3", description="-- no desc --", start=now, end=now_plus_10)
        auction.save()
        
        lot = Lot(shop=self.shop,
                  title = "Coin From Brasil 1900 (PCGS 60)",
                  description = "rare coin",
                  category = category,
                  date_time = now,
                  weight="5",
                  session=auction, 
                  starting_bid=decimal.Decimal("100.00"), 
                  reserve=decimal.Decimal("300.00"))
        lot.save()
        
        success = self.client.login(username='******', password='******')
        self.assertEqual(success, True, "login failed")
        
        #Check that lot is still active...
        self.assertEqual(lot.is_active(), True , "Failed: The lot should be active!")
        self.assertEqual(lot.bidhistory_set.all().count(), 0 , "Failed: The lot should not have any bid yet!")
        
        #1) Trying To BID wrong
        my_bid = decimal.Decimal("90.00")
        response = self.client.get(reverse("bidding_view_lot", args=[lot.id]), {'amount': my_bid}, HTTP_HOST=self.HTTP_HOST)
        self.assertEqual(response.status_code, 200, "Failed: this bid is not valid...")    
        self.assertEqual(lot.bidhistory_set.all().count(), 0 , "Failed: The lot should not have any bid yet!")
        
        #2) First valid bid, but don't reach the reserve price
        my_bid = decimal.Decimal("120.00")
        response = self.client.get(reverse("bidding_view_lot", args=[lot.id]), {'amount': my_bid}, HTTP_HOST=self.HTTP_HOST)
        self.assertEqual(response.status_code, 302, "Failed when trying to bid a valid amount %s. This value should be allowed..." % my_bid)    
        
        lot = Lot.objects.get(id=lot.id)
        self.assertEqual(lot.bidhistory_set.all().count(), 1 , "Failed: The lot should have 1 bid!")
        
        #3) Second valid bid, but neither reach the reserve price
        my_bid = decimal.Decimal("290.00")
        response = self.client.get(reverse("bidding_view_lot", args=[lot.id]), {'amount': my_bid}, HTTP_HOST=self.HTTP_HOST)
        self.assertEqual(response.status_code, 302, "Failed when trying to bid a valid amount %s. This value should be allowed..." % my_bid)    
        
        lot = Lot.objects.get(id=lot.id)
        self.assertEqual(lot.bidhistory_set.all().count(), 2 , "Failed: The lot should have 2 bids!")
        
        logging.info("waiting to auction session finish...")
        while not lot.session.finished():
            time.sleep(1)
            
        logging.info("Running cron...")
        cron.minute_update()
        
        lot = Lot.objects.get(id=lot.id)

        self.assertEqual(lot.reserve_has_been_met(), False , "Failed, the reserved price should not be reached!")
        self.assertEqual(lot.bid_actual.bid_amount, my_bid, "Failed: The bid actual is wrong, is %s but should be %s" % (lot.bid_actual.bid_amount, my_bid))
        self.assertEqual(lot.bid_actual.bidder.username, "test" , "Failed, wrong bidder won!")
        self.assertEqual(lot.is_sold(), False, "Failed: The lot state is wrong, should be DIDN'T SELL but it is %s" % lot.state)
        self.assertEqual(lot.is_didnt_sell(), True, "Failed: The lot state is wrong, should be DIDN'T SELL but it is %s" % lot.state)