Класс тестирования Django models.py содержит ошибку
Я тестирую файл models.py, который содержит два класса, один из которых Farm, а другой Batch
и Batch имеет внешний ключ, связанный с farm
При тестировании партии я проверил все остальные столбцы, но не уверен, как я должен проверить столбец внешнего ключа класса партии
models.py файл lopoks как
и класс, который я сейчас изучаю, это Batch
и тестовый файл пакета выглядит как
from django.test import TestCase
from django.db.utils import IntegrityError
from farm_management.models import Farm, Device, BatchYield, Batch
class TestBatch(TestCase):
def setUp(self):
self.batch1 = Batch.objects.create(commodity_id="2")
self.batch2 = Batch.objects.create(commodity_variety_id="4")
self.batch3 = Batch.objects.create(acerage="90")
self.batch4 = Batch.objects.create(batch_health="100")
self.batch5 = Batch.objects.create(stage="germination")
self.batch6 = Batch.objects.create(batch_status="running")
self.batch7 = Batch.objects.create(updated_at="2021-11-26 09:27:18.464511")
self.batch8 = Batch.objects.create(created_at="2021-11-26 08:50:26.618932")
self.batch9 = Batch.objects.create(updated_by_id="1224")
self.batch10 = Batch.objects.create(created_by_id="1224")
self.batch11 = Batch.objects.create(commodity_name="Apple")
self.batch12 = Batch.objects.create(start_date="2021-11-26 14:20:14.000000")
def test_batch(self):
self.assertEqual(self.batch1.commodity_id, "2")
self.assertEqual(self.batch2.commodity_variety_id, "4")
self.assertEqual(self.batch3.acerage, "90")
self.assertEqual(self.batch4.batch_health, "100")
self.assertEqual(self.batch5.stage, "germination")
self.assertEqual(self.batch6.batch_status, "running")
self.assertEqual(self.batch7.updated_at, "2021-11-26 09:27:18.464511")
self.assertEqual(self.batch8.created_at, "2021-11-26 08:50:26.618932")
self.assertEqual(self.batch9.updated_by_id, "1224")
self.assertEqual(self.batch10.created_by_id, "1224")
self.assertEqual(self.batch11.commodity_name, "Apple")
self.assertEqual(self.batch12.start_date, "2021-11-26 14:20:14.000000")
Но этот код тестирования выдает ошибку
File "/home/admin123/igrow-api/app/farm_management/test/models/batch.py", line 10, in raise IntegrityError(*tuple(e.args))
django.db.utils.IntegrityError: (1048, "Column 'acerage' cannot be null")
Ran 1 test in 0.418s
FAILED (errors=1)
Destroying test database for alias 'default'...
также я тестирую класс BatchYield из файла models.py и код тестирования для BatchYield выглядит следующим образом
from django.test import TestCase
from django.db.utils import IntegrityError
from farm_management.models import Farm, Device, BatchYield, Batch
class TestBatchYield(TestCase):
def setUp(self):
self.batchyield1 = BatchYield.objects.create(
updated_at="2021-11-26 08:53:18.115406",
created_at="",
#updated_by_id=" ",
#created_by_id="",
expected_production="2000",
grade_a_produce="600",
grade_b_produce="1400",
grade_c_rejection="0",
expected_delivery_date="2020-01-31 00:00:00.000000",
#batch_id="20",
end_date="2020-01-31 00:00:00.000000",
grade_a_sell_price="0",
grade_b_sell_price="0",
expected_grade_a_produce="1200",
expected_grade_b_produce="800",
is_active="0"
)
self.batcid1 = Batch.objects.create(batch_id="20")
def test_batchyield(self):
self.assertEqual(self.batchyield1.updated_at, "2021-11-26 08:53:18.115406")
self.assertEqual(self.batchyield1.created_at, "")
#self.assertEqual(self.batchyield1.updated_by_id, "")
#self.assertEqual(self.batchyield1.created_by_id, "")
self.assertEqual(self.batchyield1.expected_production, "2000")
self.assertEqual(self.batchyield1.grade_a_produce, "600")
self.assertEqual(self.batchyield1.grade_b_produce, "1400")
self.assertEqual(self.batchyield1.grade_c_rejection, "0")
self.assertEqual(self.batchyield1.expected_delivery_date, "2020-01-31 00:00:00.000000")
#self.assertEqual(self.batchyield1.batch_id, "20")
self.assertEqual(self.batchyield1.end_date, "2020-01-31 00:00:00.000000")
self.assertEqual(self.batchyield1.grade_a_sell_price, "0")
self.assertEqual(self.batchyield1.grade_b_sell_price, "0")
self.assertEqual(self.batchyield1.expected_grade_a_produce, "1200")
self.assertEqual(self.batchyield1.expected_grade_b_produce, "800")
self.assertEqual(self.batchyield1.is_active, "0")
self.assertEqual(self.batcid1.batch_id, "20")
который также показывает ту же самую ошибку
django.db.utils.IntegrityError: (1048, "Column 'batch_id' cannot be null")
Ran 1 test in 0.005s
FAILED (errors=1)
Destroying test database for alias 'default'...