def test_combinations(self): # Test that shipment.verbose_status returns the expected strings # Shortcut names for statuses PROG = Shipment.STATUS_IN_PROGRESS READ = Shipment.STATUS_READY TRAN = Shipment.STATUS_IN_TRANSIT RCVD = Shipment.STATUS_RECEIVED ODUE = Shipment.STATUS_OVERDUE # test data is a 3-tuple: # * shipment status # * list of package statuses, one per package for that shipment (can be empty) # * expected status string. test_data = [ (PROG, [], "In progress"), (PROG, [PROG], "In progress"), # This next one can't happen - setting pkg status sets shipment status ahead # (PROG, [TRAN], "In progress"), (READ, [], "Ready for pickup"), (READ, [PROG], "Ready for pickup"), (READ, [READ], "Ready for pickup"), (TRAN, [], "In transit"), (TRAN, [TRAN, PROG], "In transit (50%)"), (TRAN, [TRAN, TRAN], "In transit"), (TRAN, [TRAN, RCVD], "In transit (50%)"), (RCVD, [], "Received"), (RCVD, [TRAN, RCVD], "Received (50%)"), (RCVD, [RCVD, RCVD], "Received"), (ODUE, [], "Overdue"), ] # We'll run all the tests without stopping, but set `failed` if any fail # and then fail the test at the end. That'll give us more fine-grained # results without having to write N separate tests. failed = False for shipment_status, package_statuses, expected_status in test_data: shipment = ShipmentFactory(status=shipment_status) for pkg_status in package_statuses: PackageFactory(shipment=shipment, status=pkg_status) shipment_from_view = ShipmentDBView.objects.get(pk=shipment.pk) shipment_status_str = status_as_string(shipment_status) got_status = shipment_from_view.get_verbose_status() package_statuses = ','.join( [status_as_string(stat) for stat in package_statuses]) errmsg = "Test failed: with shipment status %s and package statuses %s, expected %s " \ "but got %s" % (shipment_status_str, package_statuses, expected_status, got_status) if not expected_status == got_status: failed = True print(errmsg) shipment.packages.all().delete() shipment.delete() if failed: self.fail("Test failed; see previous messages for details")
def test_combinations(self): # Test that shipment.verbose_status returns the expected strings # Shortcut names for statuses PROG = Shipment.STATUS_IN_PROGRESS READ = Shipment.STATUS_READY TRAN = Shipment.STATUS_IN_TRANSIT RCVD = Shipment.STATUS_RECEIVED ODUE = Shipment.STATUS_OVERDUE # test data is a 3-tuple: # * shipment status # * list of package statuses, one per package for that shipment (can be empty) # * expected status string. test_data = [ (PROG, [], "In progress"), (PROG, [PROG], "In progress"), # This next one can't happen - setting pkg status sets shipment status ahead # (PROG, [TRAN], "In progress"), (READ, [], "Ready for pickup"), (READ, [PROG], "Ready for pickup"), (READ, [READ], "Ready for pickup"), (TRAN, [], "In transit"), (TRAN, [TRAN, PROG], "In transit (50%)"), (TRAN, [TRAN, TRAN], "In transit"), (TRAN, [TRAN, RCVD], "In transit (50%)"), (RCVD, [], "Received"), (RCVD, [TRAN, RCVD], "Received (50%)"), (RCVD, [RCVD, RCVD], "Received"), (ODUE, [], "Overdue"), ] # We'll run all the tests without stopping, but set `failed` if any fail # and then fail the test at the end. That'll give us more fine-grained # results without having to write N separate tests. failed = False for shipment_status, package_statuses, expected_status in test_data: shipment = ShipmentFactory(status=shipment_status) for pkg_status in package_statuses: PackageFactory(shipment=shipment, status=pkg_status) shipment_from_view = ShipmentDBView.objects.get(pk=shipment.pk) shipment_status_str = status_as_string(shipment_status) got_status = shipment_from_view.get_verbose_status() package_statuses = ','.join([status_as_string(stat) for stat in package_statuses]) errmsg = "Test failed: with shipment status %s and package statuses %s, expected %s " \ "but got %s" % (shipment_status_str, package_statuses, expected_status, got_status) if not expected_status == got_status: failed = True print(errmsg) shipment.packages.all().delete() shipment.delete() if failed: self.fail("Test failed; see previous messages for details")
def setUp(self): self.email = '*****@*****.**' self.password = '******' self.user = get_user_model().objects.create_superuser( password=self.password, email=self.email) assert self.client.login(email=self.email, password=self.password) self.description = 'Yet Another _shipment_ DESCRIPTION' self.shipment = ShipmentFactory(description=self.description, partner=self.user)
def setUpClass(cls): super(ReportTestMixin, cls).setUpClass() bootstrap_permissions() cls.partner1 = PartnerFactory() cls.partner2 = PartnerFactory() cls.partner3 = PartnerFactory() cls.donor1 = DonorFactory() cls.donor2 = DonorFactory() cls.donor3 = DonorFactory() cls.category1 = ItemCategoryFactory() cls.category2 = ItemCategoryFactory() cls.category3 = ItemCategoryFactory() cls.shipment1 = ShipmentFactory(partner=cls.partner1, shipment_date=cls.day_before, status=Shipment.STATUS_IN_TRANSIT) cls.package1 = PackageFactory(shipment=cls.shipment1, status=Shipment.STATUS_IN_TRANSIT) cls.item1 = PackageItemFactory(package=cls.package1, donor=cls.donor1, item_category=cls.category1) cls.shipment2 = ShipmentFactory(partner=cls.partner2, shipment_date=cls.important_date, status=Shipment.STATUS_RECEIVED) cls.package2 = PackageFactory(shipment=cls.shipment2, status=Shipment.STATUS_RECEIVED) cls.item2 = PackageItemFactory(package=cls.package2, donor=cls.donor2, item_category=cls.category2) cls.shipment3 = ShipmentFactory(partner=cls.partner3, shipment_date=cls.day_after, status=Shipment.STATUS_CANCELED) cls.package3 = PackageFactory(shipment=cls.shipment3, status=Shipment.STATUS_CANCELED) cls.item3 = PackageItemFactory(package=cls.package3, donor=cls.donor3, item_category=cls.category3)
class TestShipment(TestCase): model = Shipment def setUp(self): super(TestShipment, self).setUp() self.shipment = ShipmentFactory() def test_set_date_in_transit(self): self.assertIsNone(self.shipment.date_in_transit) self.shipment.status = Shipment.STATUS_IN_TRANSIT self.shipment.save() shipment = Shipment.objects.get(pk=self.shipment.pk) self.assertEqual(now().date(), shipment.date_in_transit) def test_set_date_picked_up(self): self.assertIsNone(self.shipment.date_picked_up) self.shipment.status = Shipment.STATUS_PICKED_UP self.shipment.save() shipment = Shipment.objects.get(pk=self.shipment.pk) self.assertEqual(now().date(), shipment.date_picked_up)
def test_pkg_edit_specify_shipment(self): # Creating a new instance, specify shipment shipment = ShipmentFactory() data = { 'name': 'New name', 'description': 'New description', } form = PackageEditForm(data=data, shipment=shipment) pkg = form.save() self.assertEqual(shipment, pkg.shipment) self.assertEqual(data['name'], pkg.name) self.assertEqual(data['description'], pkg.description)
def test_package_price(self): shipment = ShipmentFactory() package = PackageFactory(shipment=shipment) item1 = PackageItemFactory(package=package, price_usd=Decimal('1.23'), quantity=2) item2 = PackageItemFactory(package=package, price_usd=Decimal('9.11'), quantity=3) expected_price = quantize_usd(Decimal(item1.quantity * item1.price_usd + item2.quantity * item2.price_usd)) pkg = PackageDBView.objects.get(pk=package.pk) price = pkg.price_usd self.assertEqual(expected_price, price)
def test_partner_filter(self): rsp = self.ajax_get(self.url + "?partner=%d" % self.partner1.pk) qs = rsp.context['queryset'] self.assertEqual(1, len(qs)) self.assertEqual(self.day_before.month, qs[0]['month'].month) self.assertEqual(1, qs[0]['pk__count']) last_month = self.day_before - timedelta(days=30) ShipmentFactory(partner=self.partner1, shipment_date=last_month, status=Shipment.STATUS_IN_TRANSIT) rsp = self.ajax_get(self.url + "?partner=%d" % self.partner1.pk) qs = rsp.context['queryset'] # 2 records self.assertEqual(2, len(qs)) self.assertEqual(last_month.month, qs[1]['month'].month) self.assertEqual(1, qs[1]['pk__count'])
def test_get_queryset(self): view = self.report_class() qs = view.get_queryset() # all shipped within same month self.assertEqual(1, len(qs)) self.assertEqual(self.day_before.month, qs[0]['month'].month) self.assertEqual(3, qs[0]['pk__count']) last_month = self.day_before - timedelta(days=30) ShipmentFactory(partner=self.partner1, shipment_date=last_month, status=Shipment.STATUS_IN_TRANSIT) qs = view.get_queryset() # 2 records self.assertEqual(2, len(qs)) self.assertEqual(last_month.month, qs[1]['month'].month) self.assertEqual(1, qs[1]['pk__count'])
def test_shipment_price(self): shipment = ShipmentFactory() package1 = PackageFactory(shipment=shipment) PackageItemFactory(package=package1, price_usd=Decimal('1.23'), quantity=2) PackageItemFactory(package=package1, price_usd=Decimal('9.11'), quantity=3) package2 = PackageFactory(shipment=shipment) PackageItemFactory(package=package2, price_usd=Decimal('3.14'), quantity=13) pkgs = PackageDBView.objects.filter(pk__in=[package1.pk, package2.pk]) price_sum = sum([pkg.price_usd for pkg in pkgs]) expected_price = quantize_usd(price_sum) ship = ShipmentDBView.objects.get(pk=shipment.pk) price = ship.price_usd self.assertEqual(expected_price, price)
def test_edit(self): shipment_date = (timezone.now() + timedelta(days=2)).date() shipment = ShipmentFactory( description='First', shipment_date=shipment_date, date_expected=shipment_date + timedelta(days=3), ) data = { 'description': 'New description', 'shipment_date': shipment_date, 'estimated_delivery': 1, 'store_release': 'foo', 'partner': self.partner.id } form = ShipmentEditForm(instance=shipment, data=data) self.assertTrue(form.is_valid()) result = form.save() self.assertEqual(shipment.pk, result.pk) self.assertEqual('New description', result.description) self.assertEqual(shipment_date + timedelta(days=1), result.date_expected)
def test_finalize(self): shipment = ShipmentFactory() pkg2 = PackageFactory(shipment=shipment, name='pkg2') pkg1 = PackageFactory(shipment=shipment, name='pkg1') self.assertEqual(pkg1.status, Shipment.STATUS_IN_PROGRESS) shipment.finalize() pkg1 = Package.objects.get(pk=pkg1.pk) pkg2 = Package.objects.get(pk=pkg2.pk) self.assertEqual(pkg1.status, Shipment.STATUS_READY) self.assertEqual(pkg2.number_in_shipment, 1) # in order of creation self.assertEqual(pkg1.number_in_shipment, 2) shipment = Shipment.objects.get(pk=shipment.pk) self.assertEqual(shipment.status, Shipment.STATUS_READY) shipment.reopen() pkg1 = Package.objects.get(pk=pkg1.pk) self.assertEqual(pkg1.status, Shipment.STATUS_IN_PROGRESS)
def test_status(self): """ Shipment "status" from get_status() is complicated. """ shipment = ShipmentFactory( date_expected=now().date() - timedelta(days=2), ) package = PackageFactory.build( shipment=shipment, status=Shipment.STATUS_IN_PROGRESS, date_received=now().date() - timedelta(days=1), date_in_transit=now().date(), date_picked_up=now().date(), ) # These three are just returned as-is for st in (Shipment.STATUS_CANCELED, Shipment.STATUS_LOST, Shipment.STATUS_IN_PROGRESS): package.status = st self.assertEqual(package.get_status(), st) # Any other status is ignored and it looks at the dates package.status = -99 # We have a date received, so shipment is received self.assertEqual(package.get_status(), Shipment.STATUS_RECEIVED) package.date_received = None # No date received, and date_expected is in the past self.assertEqual(package.get_status(), Shipment.STATUS_OVERDUE) package.shipment.date_expected = now().date() + timedelta(days=1) package.shipment.save() # No date received, date-expected in future self.assertEqual(package.get_status(), Shipment.STATUS_IN_TRANSIT) package.date_in_transit = None # Not in transit yet self.assertEqual(package.get_status(), Shipment.STATUS_PICKED_UP) package.date_picked_up = None # Not picked up yet (no idea what the difference is) self.assertEqual(package.get_status(), Shipment.STATUS_READY)
def test_has_shipped(self): shipment = ShipmentFactory(status=Shipment.STATUS_IN_PROGRESS) self.assertFalse(shipment.has_shipped()) shipment.status = Shipment.STATUS_READY self.assertFalse(shipment.has_shipped()) shipment.status = Shipment.STATUS_PICKED_UP shipment.date_picked_up = now() self.assertTrue(shipment.has_shipped()) shipment.status = Shipment.STATUS_IN_TRANSIT shipment.date_in_transit = now() self.assertTrue(shipment.has_shipped()) shipment.status = Shipment.STATUS_LOST self.assertTrue(shipment.has_shipped()) shipment.status = Shipment.STATUS_RECEIVED self.assertTrue(shipment.has_shipped())
def setUp(self): super(TestShipmentDBView, self).setUp() self.shipment = ShipmentFactory()
class TestShipmentDBView(TestCase): # model = ShipmentDBView def setUp(self): super(TestShipmentDBView, self).setUp() self.shipment = ShipmentFactory() def test_shipment_db_view_no_packages(self): shipment = ShipmentDBView.objects.get(pk=self.shipment.pk) self.assertEqual(shipment.description, self.shipment.description) # No packages self.assertEqual(0, shipment.num_packages) self.assertEqual(0, shipment.num_items) self.assertEqual(0, shipment.num_received_items) self.assertEqual(Decimal('0'), shipment.price_usd) self.assertEqual(Decimal('0'), shipment.price_local) def test_shipment_db_view_one_package_no_items(self): # 1 package, no items PackageFactory(shipment=self.shipment) shipment = ShipmentDBView.objects.get(pk=self.shipment.pk) self.assertEqual(1, shipment.packages.count()) self.assertEqual(1, shipment.num_packages) self.assertEqual(0, shipment.num_items) self.assertEqual(0, shipment.num_received_items) self.assertEqual(Decimal('0'), shipment.price_usd) self.assertEqual(Decimal('0'), shipment.price_local) def test_shipment_db_view_one_package_with_items(self): # 1 package, with items package = PackageFactory(shipment=self.shipment) PackageItemFactory(package=package, quantity=1, price_usd=Decimal('1.00'), price_local=Decimal('0.001')) PackageItemFactory(package=package, quantity=2, price_usd=Decimal('2.00'), price_local=Decimal('0.002')) shipment = ShipmentDBView.objects.get(pk=self.shipment.pk) self.assertEqual(1, shipment.packages.count()) self.assertEqual(1, shipment.num_packages) self.assertEqual(3, shipment.num_items) self.assertEqual(0, shipment.num_received_items) self.assertEqual(Decimal('5.0'), shipment.price_usd) self.assertEqual(Decimal('0.005'), shipment.price_local) def test_shipment_db_view_two_packages_no_items(self): # 2 packages, no items PackageFactory(shipment=self.shipment) PackageFactory(shipment=self.shipment) shipment = ShipmentDBView.objects.get(pk=self.shipment.pk) self.assertEqual(2, shipment.packages.count()) self.assertEqual(2, shipment.num_packages) self.assertEqual(0, shipment.num_items) self.assertEqual(0, shipment.num_received_items) self.assertEqual(Decimal('0'), shipment.price_usd) self.assertEqual(Decimal('0'), shipment.price_local) def test_shipment_db_view_two_packages_with_items(self): # 2 packages, with items, one of them received self.shipment.date_received = now() self.shipment.status = Shipment.STATUS_RECEIVED self.shipment.save() package1 = PackageFactory(shipment=self.shipment) package2 = PackageFactory(shipment=self.shipment, status=Shipment.STATUS_RECEIVED) PackageItemFactory(package=package1, quantity=1, price_usd=Decimal('1.00'), price_local=Decimal('0.001')) PackageItemFactory(package=package2, quantity=2, price_usd=Decimal('2.00'), price_local=Decimal('0.002')) PackageItemFactory(package=package2, quantity=3, price_usd=Decimal('3.00'), price_local=Decimal('0.003')) shipment = ShipmentDBView.objects.get(pk=self.shipment.pk) self.assertEqual(2, shipment.packages.count()) self.assertEqual(2, shipment.num_packages) self.assertEqual(6, shipment.num_items) self.assertEqual(5, shipment.num_received_items) self.assertEqual(Decimal('14.0'), shipment.price_usd) self.assertEqual(Decimal('0.014'), shipment.price_local) def test_verbose_status_received_percentage(self): self.shipment.date_received = now() self.shipment.status = Shipment.STATUS_RECEIVED self.shipment.save() self.assertEqual(self.shipment.status, Shipment.STATUS_RECEIVED) shipment = ShipmentDBView.objects.get(pk=self.shipment.pk) # No packages: should not show any percent (they're all "received") self.assertEqual(0, shipment.num_packages) self.assertEqual('Received', shipment.get_verbose_status()) # Add one, unreceived package PackageFactory(shipment=self.shipment, status=Shipment.STATUS_IN_TRANSIT) shipment = ShipmentDBView.objects.get(pk=self.shipment.pk) # Should still show 0% self.assertEqual(1, shipment.num_packages) self.assertEqual('Received (0%)', shipment.get_verbose_status()) # Add a second package, this one received PackageFactory(shipment=self.shipment, status=Shipment.STATUS_RECEIVED) shipment = ShipmentDBView.objects.get(pk=self.shipment.pk) # Now should show 50% self.assertEqual(2, shipment.num_packages) self.assertEqual('Received (50%)', shipment.get_verbose_status()) # Set all packages to received self.shipment.packages.update(status=Shipment.STATUS_RECEIVED) shipment = ShipmentDBView.objects.get(pk=self.shipment.pk) # Now should show no percentage, just the status self.assertEqual(2, shipment.num_packages) self.assertEqual('Received', shipment.get_verbose_status())
def setUp(self): super(ShipmentDeleteViewTest, self).setUp() self.shipment = ShipmentFactory() self.package = PackageFactory(shipment=self.shipment) self.item = PackageItemFactory(package=self.package) self.url = reverse('delete_shipment', kwargs={'pk': self.shipment.pk})
def item_for_partner(self, user): return PackageFactory(shipment=ShipmentFactory(partner=user))
def item_for_partner(self, user): return ShipmentFactory(partner=user)