我正在尝试将测试实施到Django应用程序中,但出现错误。我究竟做错了什么?

时间:2019-04-27 18:25:45

标签: django testing

我正在尝试在我的项目中实施一些单元测试,但出现错误。我究竟做错了什么。我得到的错误是“文件” django.core.exceptions.ImproperlyConfigured:请求的设置为INSTALLED_APPS,但未配置设置。您必须先定义环境变量DJANGO_SETTINGS_MODULE或在访问设置之前调用settings.configure()。”非常感谢任何帮助

下面的交易模型

class Transaction(models.Model):
    currency = models.CharField(max_length=20)
    amount = models.IntegerField()
    total_price = models.DecimalField(max_digits=8, decimal_places=2)
    date_purchased = models.DateTimeField()
    note = models.TextField(default="")
    owner = models.ForeignKey(User, on_delete=models.CASCADE)
    amount_per_coin = models.DecimalField(max_digits=8, decimal_places=2, editable=False)


    def save(self, *args, **kwargs):
        self.amount_per_coin = self.total_price / self.amount
        super(Transaction, self).save(*args, **kwargs)

    def __str__(self):
        return str(self.pk)+','+self.currency + ', '+str(self.amount)

    def get_absolute_url(self):
        return reverse('transaction-detail', kwargs={'pk': self.pk})

    @property
    def coins_remaining(self):
        return self.amount - sum(self.sales.all().values_list('amount_sold', flat=True))

    @property
    def coin_value(self):
        return get_currency_price(self.currency)

    @property
    def total_value(self):
        value = self.coin_value * self.amount
        return round(value, 2)

    @property
    def profit_loss(self):
        value = float(self.total_value) - float(self.total_price)
        return round(value, 2)

    @property
    def profit_loss_percent(self):
        value = ((float(self.total_value) - float(self.total_price))/self.total_value)*100
        return round(value, 1)

    def get_absolute_profit_loss(self):
        return abs(self.profit_loss)

    def get_absolute_profit_loss_percent(self):
        return abs(self.profit_loss_percent)

下面的销售模式

class Sale(models.Model):
    amount_sold = models.IntegerField()
    total_price_sold = models.DecimalField(max_digits=8, decimal_places=2)
    date_sold = models.DateTimeField(default=timezone.now)
    note = models.TextField(default="")
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name="sales")
    amount_per_coin_sold = models.DecimalField(max_digits=8, decimal_places=2, editable=False)

    def __str__(self):
        return str(self.pk)+','+str(self.amount_sold) + ', '+self.note

    def save(self, *args, **kwargs):
        self.amount_per_coin_sold = self.total_price_sold / self.amount_sold
        super(Sale, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse('sale-detail', kwargs={'pk': self.pk})

    @property
    def profit_loss(self):
        return (self.amount_per_coin_sold - self.transaction.amount_per_coin) * self.amount_sold

    @property
    def profit_loss_percent(self):
        value = ((self.total_price_sold - (self.transaction.amount_per_coin * self.amount_sold))/self.total_price_sold) * 100
        return round(value, 1)

    def get_absolute_profit_loss(self):
        return abs(self.profit_loss)

    def get_absolute_profit_loss_percent(self):
        return abs(self.profit_loss_percent)

在下面测试

from django.test import TestCase
from .models import Transaction
import datetime
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cryptopredict.settings")


class TransactionTestCase(TestCase):
    def setUp(self):
        Transaction.objects.create(currency="BTC", amount=2, total_price=5000, date_purchased=datetime.datetime.now(),
                                   note="note", owner=1)
        Transaction.objects.create(currency="BTC", amount=2, total_price=6000, date_purchased=datetime.datetime.now(),
                                   note="note", owner=1)

    def test_animals_can_speak(self):
        """Should return price for single coin"""
        t1 = Transaction.objects.get(total_price=5000)
        t2 = Transaction.objects.get(total_price=6000)
        self.assertEqual(t1.amount_per_coin, 2500)
        self.assertEqual(t2.amount_per_coin, 2000)

0 个答案:

没有答案