Skip to content

Commit

Permalink
Fix TypeError raised when comparing nullable property (#4279)
Browse files Browse the repository at this point in the history
* Fix `TypeError` raised when comparing nullable property

* Fix failing lint tests
  • Loading branch information
ajharry69 committed Apr 22, 2024
1 parent e19b0dc commit f1d9ecd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/oscar/apps/order/abstract_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ def is_allocation_consumption_possible(self, quantity):
if self.allocation_cancelled:
return False

return quantity <= self.num_allocated
return quantity <= (self.num_allocated or 0)

def consume_allocation(self, quantity):
if not self.can_track_allocations:
Expand Down
2 changes: 1 addition & 1 deletion src/oscar/apps/partner/abstract_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def is_allocation_consumption_possible(self, quantity):
"""
Test if a proposed stock consumption is permitted
"""
return quantity <= min(self.num_allocated, self.num_in_stock)
return quantity <= min(self.num_allocated or 0, self.num_in_stock)

def consume_allocation(self, quantity):
"""
Expand Down
34 changes: 34 additions & 0 deletions tests/integration/order/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,40 @@ def setUp(self):
def tearDown(self):
ShippingEventType.objects.all().delete()

def test_is_allocation_consumption_possible_when_num_allocated_is_greater_than_quantity(
self,
):
self.line.num_allocated = 2

actual = self.line.is_allocation_consumption_possible(1)

self.assertTrue(actual)

def test_is_allocation_consumption_possible_when_num_allocated_is_lower_than_quantity(
self,
):
self.line.num_allocated = 0

actual = self.line.is_allocation_consumption_possible(1)

self.assertFalse(actual)

def test_is_allocation_consumption_possible_when_num_allocated_is_equal_to_quantity(
self,
):
self.line.num_allocated = 1

actual = self.line.is_allocation_consumption_possible(1)

self.assertTrue(actual)

def test_is_allocation_consumption_possible_when_num_allocated_is_null(self):
self.line.num_allocated = None

actual = self.line.is_allocation_consumption_possible(1)

self.assertFalse(actual)

def event(self, event_type, quantity=None):
"""
Creates a shipping event for the test line
Expand Down
34 changes: 34 additions & 0 deletions tests/integration/partner/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,40 @@ def setUp(self):
self.product, price=D("10.00"), num_in_stock=10
)

def test_is_allocation_consumption_possible_when_num_allocated_is_greater_than_quantity(
self,
):
self.stockrecord.num_allocated = 2

actual = self.stockrecord.is_allocation_consumption_possible(1)

self.assertTrue(actual)

def test_is_allocation_consumption_possible_when_num_allocated_is_lower_than_quantity(
self,
):
self.stockrecord.num_allocated = 0

actual = self.stockrecord.is_allocation_consumption_possible(1)

self.assertFalse(actual)

def test_is_allocation_consumption_possible_when_num_allocated_is_equal_to_quantity(
self,
):
self.stockrecord.num_allocated = 1

actual = self.stockrecord.is_allocation_consumption_possible(1)

self.assertTrue(actual)

def test_is_allocation_consumption_possible_when_num_allocated_is_null(self):
self.stockrecord.num_allocated = None

actual = self.stockrecord.is_allocation_consumption_possible(1)

self.assertFalse(actual)

def test_get_price_excl_tax_returns_correct_value(self):
self.assertEqual(D("10.00"), self.stockrecord.price)

Expand Down

0 comments on commit f1d9ecd

Please sign in to comment.