In my models, I have the following method:
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)
It is designed to find the amount of bags that have been sorted so far under the tag associated with the record, and deduct that amount (along with the amount sorted under this record) from the total bags.
It works great! The appropriate amounts are successfully passed to the templates.
However, I was dismayed that it threw off my unit tests.
======================================================================
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'...
It seems to be interpreting my date object as a string. The model it draws from is a DateField. If I call type on it it reports it as:
Here's the model where it is housed:
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)
This is the test I am running.
# 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)
Again, it works in real life, but fails while running the test. Any ideas why?
EDIT TO ADD DETAILS:
Database employed is Postgres.
Here is my test 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,
)
Thanks to @bruno desthuilliers I observed the issue.
My
setUpTestData()
method was populating the fields with strings instead of datetime objects. It works properly after converting them to the appropriate input: