Im trying to implement some unit tests into my project but am getting errors. what am I doing wrong. the errors im getting are "File "C:\Users\phily\AppData\Local\Programs\Python\Python37\lib\site-packages\django\conf__init__.py", line 42, in _setup % (desc, ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings." Any help would be much apprecited
Transaction model below
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)
Sales model below
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)
Test below
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)
Aucun commentaire:
Enregistrer un commentaire