在我的模型中,我有以下方法:
def _bags_remaining(self):
current_set = SortingRecords.objects.values().filter(~Q(id=self.id), tag=self.tag)
sorted = [SortingRecords['bags_sorted'] for SortingRecords in current_set if
SortingRecords['date'] <= self.date]
remaining = self.tag.pieces - sum(sorted) - self.bags_sorted
return remaining
bags_remaining = property(_bags_remaining)
它旨在查找目前已在与记录关联的标签下分类的行李数量,并从总行李中扣除该数量(以及在此记录下分类的金额)。
效果很好!适当的金额成功传递给模板。
然而,我很沮丧它放弃了我的单元测试。
======================================================================
ERROR: test_sorting_records_bags_remaining_calculation (AlmondKing.InventoryLogs.tests.test_views.test_purchase_details.DetailsPageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Projects\AlmondKing\AlmondKing\InventoryLogs\tests\test_views\test_purchase_details.py", line 155, in test_sorting_records_bags_remaining_calculation
self.assertEqual(self.sortrecord1.bags_remaining, 79)
File "C:\Projects\AlmondKing\AlmondKing\InventoryLogs\models.py", line 115, in _bags_remaining
sorted = [SortingRecords['bags_sorted'] for SortingRecords in current_set if
File "C:\Projects\AlmondKing\AlmondKing\InventoryLogs\models.py", line 116, in <listcomp>
SortingRecords['date'] <= self.date]
TypeError: unorderable types: datetime.date() <= str()
----------------------------------------------------------------------
Ran 22 tests in 0.203s
FAILED (errors=2)
Destroying test database for alias 'default'...
似乎是将我的日期对象解释为字符串。它绘制的模型是DateField。如果我在其上调用类型,则将其报告为:
这里是它所在的模型:
class SortingRecords(models.Model):
tag = models.ForeignKey(Purchase, related_name='sorting_record')
date = models.DateField()
bags_sorted = models.IntegerField()
turnout = models.IntegerField()
objects = models.Manager()
def __str__(self):
return "%s [%s]" % (self.date, self.tag.tag)
这是我正在进行的测试。
# Sorting Records should calculate bags remaining for each entry.
def test_sorting_records_bags_remaining_calculation(self):
self.assertEqual(self.sortrecord1.bags_remaining, 79)
self.assertEqual(self.sortrecord2.bags_remaining, 39)
self.assertEqual(self.sortrecord3.bags_remaining, 9)
同样,它在现实生活中有效,但在运行测试时失败了。有什么想法吗?
编辑添加详细信息:
采用的数据库是Postgres。
这是我的测试setUpTestData():
class DetailsPageTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.product1 = ProductGroup.objects.create(
product_name="Almonds"
)
cls.variety1 = Variety.objects.create(
product_group = cls.product1,
variety_name = "non pareil",
husked = False,
finished = False,
)
cls.supplier1 = Supplier.objects.create(
company_name = "Acme",
company_location = "Acme Acres",
contact_info = "Call me!"
)
cls.shipment1 = Purchase.objects.create(
tag=9,
shipment_id=9999,
supplier_id = cls.supplier1,
purchase_date='2015-01-09',
purchase_price=9.99,
product_name=cls.variety1,
pieces=99,
kgs=999,
crackout_estimate=99.9
)
cls.shipment2 = Purchase.objects.create(
tag=8,
shipment_id=8888,
supplier_id=cls.supplier1,
purchase_date='2015-01-08',
purchase_price=8.88,
product_name=cls.variety1,
pieces=88,
kgs=888,
crackout_estimate=88.8
)
cls.shipment3 = Purchase.objects.create(
tag=7,
shipment_id=7777,
supplier_id=cls.supplier1,
purchase_date='2014-01-07',
purchase_price=7.77,
product_name=cls.variety1,
pieces=77,
kgs=777,
crackout_estimate=77.7
)
cls.sortrecord1 = SortingRecords.objects.create(
tag=cls.shipment1,
date="2015-02-05",
bags_sorted=20,
turnout=199,
)
cls.sortrecord2 = SortingRecords.objects.create(
tag=cls.shipment1,
date="2015-02-07",
bags_sorted=40,
turnout=399,
)
cls.sortrecord3 = SortingRecords.objects.create(
tag=cls.shipment1,
date='2015-02-09',
bags_sorted=30,
turnout=299,
)
答案 0 :(得分:0)
感谢@bruno desthuilliers我发现了这个问题。
我的setUpTestData()
方法用字符串而不是datetime对象填充字段。将它们转换为适当的输入后,它可以正常工作:
purchase_date=datetime.date(2015,1,9)