예제 #1
0
def test_bucket_change_config():
    """
    Tests InvestmentBucket.change_config()
    """
    cfg_str = namedtuple("cfg_str", ["id", "quantity"])
    user1 = User.objects.create(username='******', password="******")
    stock1 = Stock(name="Name1X", ticker="TKRC")
    stock1.save()
    DailyStockQuote(date="2016-06-10", value=100.0, stock=stock1).save()
    bucket = InvestmentBucket(name="Bucket1",
                              public=True,
                              owner=user1.profile,
                              available=10)
    bucket.save()
    cfg1 = InvestmentStockConfiguration(
        quantity=1,
        stock=stock1,
        bucket=bucket,
        start="2016-06-06",
        end="2016-06-08",
    )
    cfg2 = InvestmentStockConfiguration(
        quantity=1,
        stock=stock1,
        bucket=bucket,
        start="2016-06-08",
    )
    cfg1.save()
    cfg2.save()
    with pytest.raises(Exception):
        bucket.change_config([cfg_str(id=stock1.id, quantity=2)])
    bucket.available = 1000
    bucket.change_config([cfg_str(id=stock1.id, quantity=2)])
    bucket.refresh_from_db()
    assert bucket.available == 900
    assert bucket.stocks.filter(end=None).values('stock_id').annotate(
        sum_q=Sum('quantity')).get()['sum_q'] == 2
예제 #2
0
def test_bucket_sell_all():
    """
    Tests InvestmentBucket._sell_all()
    """
    user1 = User.objects.create(username='******', password="******")
    stock1 = Stock(name="Name1X", ticker="TKRC")
    stock1.save()
    DailyStockQuote(date="2016-06-10", value=100.0, stock=stock1).save()
    bucket = InvestmentBucket(name="Bucket1",
                              public=True,
                              owner=user1.profile,
                              available=10)
    bucket.save()
    cfg1 = InvestmentStockConfiguration(
        quantity=1,
        stock=stock1,
        bucket=bucket,
        start="2016-06-06",
        end="2016-06-08",
    )
    cfg2 = InvestmentStockConfiguration(
        quantity=1,
        stock=stock1,
        bucket=bucket,
        start="2016-06-08",
    )
    cfg1.save()
    cfg2.save()
    # pylint: disable=protected-access
    bucket._sell_all()
    # pylint: enable=protected-access
    bucket.refresh_from_db()
    cfg1.refresh_from_db()
    cfg2.refresh_from_db()
    assert bucket.available == 110
    assert cfg1.end == datetime.date(2016, 6, 8)
    assert cfg2.end is not None