Merge pull request #1248 from SchrodingersGat/overdue-unit-test

Adds unit testing for order overdue status
This commit is contained in:
Oliver 2021-01-20 08:38:44 +11:00 committed by GitHub
commit 71522fa608
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 9 deletions

View File

@ -225,22 +225,19 @@ class Build(MPTTModel):
blank=True, help_text=_('Extra build notes')
)
@property
def is_overdue(self):
"""
Returns true if this build is "overdue":
- Not completed
- Target date is "in the past"
Makes use of the OVERDUE_FILTER to avoid code duplication
"""
# Cannot be deemed overdue if target_date is not set
if self.target_date is None:
return False
today = datetime.now().date()
return self.active and self.target_date < today
query = Build.objects.filter(pk=self.pk)
query = query.filter(Build.OVERDUE_FILTER)
return query.exists()
@property
def active(self):
"""

View File

@ -10,6 +10,7 @@ from rest_framework.test import APITestCase
from rest_framework import status
import json
from datetime import datetime, timedelta
from .models import Build
from stock.models import StockItem
@ -70,6 +71,24 @@ class BuildTestSimple(TestCase):
self.assertEqual(b2.status, BuildStatus.COMPLETE)
def test_overdue(self):
"""
Test overdue status functionality
"""
today = datetime.now().date()
build = Build.objects.get(pk=1)
self.assertFalse(build.is_overdue)
build.target_date = today - timedelta(days=1)
build.save()
self.assertTrue(build.is_overdue)
build.target_date = today + timedelta(days=80)
build.save()
self.assertFalse(build.is_overdue)
def test_is_active(self):
b1 = Build.objects.get(pk=1)
b2 = Build.objects.get(pk=2)

View File

@ -272,6 +272,7 @@ class PurchaseOrder(Order):
self.complete_date = datetime.now().date()
self.save()
@property
def is_overdue(self):
"""
Returns True if this PurchaseOrder is "overdue"

View File

@ -5,6 +5,8 @@ from django.test import TestCase
from django.core.exceptions import ValidationError
from django.db.utils import IntegrityError
from datetime import datetime, timedelta
from company.models import Company
from stock.models import StockItem
from order.models import SalesOrder, SalesOrderLineItem, SalesOrderAllocation
@ -40,6 +42,26 @@ class SalesOrderTest(TestCase):
# Create a line item
self.line = SalesOrderLineItem.objects.create(quantity=50, order=self.order, part=self.part)
def test_overdue(self):
"""
Tests for overdue functionality
"""
today = datetime.now().date()
# By default, order is *not* overdue as the target date is not set
self.assertFalse(self.order.is_overdue)
# Set target date in the past
self.order.target_date = today - timedelta(days=5)
self.order.save()
self.assertTrue(self.order.is_overdue)
# Set target date in the future
self.order.target_date = today + timedelta(days=5)
self.order.save()
self.assertFalse(self.order.is_overdue)
def test_empty_order(self):
self.assertEqual(self.line.quantity, 50)
self.assertEqual(self.line.allocated_quantity(), 0)

View File

@ -1,3 +1,7 @@
# -*- coding: utf-8 -*-
from datetime import datetime, timedelta
from django.test import TestCase
import django.core.exceptions as django_exceptions
@ -37,6 +41,24 @@ class OrderTest(TestCase):
self.assertEqual(str(line), "100 x ACME0001 from ACME (for PO0001 - ACME)")
def test_overdue(self):
"""
Test overdue status functionality
"""
today = datetime.now().date()
order = PurchaseOrder.objects.get(pk=1)
self.assertFalse(order.is_overdue)
order.target_date = today - timedelta(days=5)
order.save()
self.assertTrue(order.is_overdue)
order.target_date = today + timedelta(days=1)
order.save()
self.assertFalse(order.is_overdue)
def test_increment(self):
next_ref = PurchaseOrder.getNextOrderNumber()