def test_delete_product_with_inventory(self):
        product = Product(name="Test Product", organization=self.organization)
        product.save()

        self.assertEqual(Product.objects.count(), 1)

        location = Location(name="Test Location", organization=self.organization)
        location.save()

        self.assertEqual(Location.objects.count(), 1)

        inventory = Inventory(
            location=location, product=product, organization=self.organization
        )
        inventory.amount = 1.0
        inventory.save()

        self.assertEqual(inventory.amount, 1.0)

        # Deleting it should fail since we have inventory
        with pytest.raises(Exception) as deletion_error:
            product.delete()

        self.assertEqual(
            str(deletion_error.value),
            "Unable to delete this product, we currently have it in inventory",
        )

        # Removing inventory should make the product deletable
        inventory.add(-1)

        product.delete()

        self.assertEqual(Product.objects.count(), 0)
    def test_delete_location_with_inventory(self):
        location = Location(name="Test Location", organization=self.organization)
        location.save()

        self.assertEqual(Location.objects.count(), 1)

        product = Product(name="Test Product", organization=self.organization)
        product.save()

        self.assertEqual(Product.objects.count(), 1)

        inventory = Inventory(
            location=location, product=product, organization=self.organization
        )
        inventory.amount = 1.0
        inventory.save()

        self.assertEqual(inventory.amount, 1.0)

        # Deleting it should fail since we have inventory
        with pytest.raises(Exception) as deletion_error:
            location.delete()

        self.assertEqual(
            str(deletion_error.value),
            "We cannot delete a location if it still has inventory.",
        )

        # Removing inventory should make the location deletable
        inventory.add(-1)

        location.delete()

        self.assertEqual(Location.objects.count(), 0)
Пример #3
0
    def test_shortcut_move(self):
        location = Location(name="Test Location",
                            organization=self.organization)
        location.save()

        location_2 = Location(name="Test Location",
                              organization=self.organization)
        location_2.save()

        product = Product(name="Test Product", organization=self.organization)
        product.save()

        inventory = Inventory(location=location,
                              product=product,
                              organization=self.organization)
        inventory.save()

        inventory.add(10)
        inventory.refresh_from_db()

        self.assertEqual(inventory.amount, 10.0)

        response = self.client.get("/{}/shortcuts/move".format(
            self.organization.slug))
        self.assertEqual(response.status_code, 200)

        response = self.client.post(
            "/{}/shortcuts/move".format(self.organization.slug),
            {
                "amount": 5.0,
                "product": product.id,
                "location_from": location.id,
                "location_to": location_2.id,
            },
            follow=True,
        )

        self.assertEqual(response.status_code, 200)

        messages = list(response.context["messages"])
        self.assertEqual(len(messages), 1)
        self.assertEqual(str(messages[0]), "5.0 Test Product moved!")

        inventory.refresh_from_db()
        self.assertEqual(inventory.amount, 5.0)

        location_2_inventory = Inventory.objects.get(product=product,
                                                     location=location_2)
        self.assertEqual(location_2_inventory.amount, 5.0)
Пример #4
0
    def test_shortcut_sale(self):
        location = Location(name="Test Location",
                            organization=self.organization)
        location.save()

        product = Product(name="Test Product", organization=self.organization)
        product.save()

        inventory = Inventory(location=location,
                              product=product,
                              organization=self.organization)
        inventory.save()

        inventory.add(10)
        inventory.refresh_from_db()

        self.assertEqual(inventory.amount, 10.0)

        response = self.client.get("/{}/shortcuts/sales".format(
            self.organization.slug))
        self.assertEqual(response.status_code, 200)

        response = self.client.post(
            "/{}/shortcuts/sales".format(self.organization.slug),
            {
                "amount": 1.0,
                "product": product.id,
                "location": location.id,
                "desc": "Testing",
            },
            follow=True,
        )

        self.assertEqual(response.status_code, 200)

        messages = list(response.context["messages"])
        self.assertEqual(len(messages), 1)
        self.assertEqual(str(messages[0]), "Sold 1.0 Test Product!")

        mutation = Mutation.objects.filter(amount=-1.0)[0]
        self.assertEqual(
            mutation.desc, "Sold {} {} - {}".format(1.0, product.name,
                                                    "Testing"))

        inventory.refresh_from_db()
        self.assertEqual(inventory.amount, 9.0)
Пример #5
0
    def test_shortcut_sale_without_inventory(self):
        location = Location(name="Test Location",
                            organization=self.organization)
        location.save()

        product = Product(name="Test Product", organization=self.organization)
        product.save()

        inventory = Inventory(location=location,
                              product=product,
                              organization=self.organization)
        inventory.save()

        inventory.add(1)
        inventory.refresh_from_db()

        self.assertEqual(inventory.amount, 1.0)

        response = self.client.get("/{}/shortcuts/sales".format(
            self.organization.slug))
        self.assertEqual(response.status_code, 200)

        response = self.client.post(
            "/{}/shortcuts/sales".format(self.organization.slug),
            {
                "amount": 10.0,
                "product": product.id,
                "location": location.id,
                "desc": "",
            },
            follow=True,
        )

        self.assertEqual(response.status_code, 200)

        messages = list(response.context["messages"])
        self.assertEqual(len(messages), 1)
        self.assertEqual(
            str(messages[0]),
            "* Insufficient inventory of 10.0 Test Product at Test Location",
        )

        inventory.refresh_from_db()

        self.assertEqual(inventory.amount, 1.0)
    def test_inventory_remove(self):
        location = Location(name="Test Location", organization=self.organization)
        location.save()

        product = Product(name="Test Product", organization=self.organization)
        product.save()

        inventory = Inventory(
            location=location, product=product, organization=self.organization
        )
        inventory.save()

        self.assertEqual(inventory.amount, 0.0)

        inventory.add(1)
        inventory.refresh_from_db()

        self.assertEqual(inventory.amount, 1.0)

        inventory.add(-1)
        inventory.refresh_from_db()

        self.assertEqual(inventory.amount, 0.0)
    def test_inventory_add(self):
        location = Location(name="Test Location", organization=self.organization)
        location.save()

        product = Product(name="Test Product", organization=self.organization)
        product.save()

        inventory = Inventory(
            location=location, product=product, organization=self.organization
        )
        inventory.save()

        self.assertEqual(inventory.amount, 0.0)

        inventory.add(1)
        inventory.refresh_from_db()

        self.assertEqual(inventory.amount, 1.0)

        self.assertEqual(len([location]), product.available_locations.count())
        self.assertEqual(location, product.available_locations[0])

        self.assertEqual(len([product]), location.available_products.count())
        self.assertEqual(product, location.available_products[0])
Пример #8
0
    def test_shortcut_sale_reservation(self):
        location = Location(name="Test Location",
                            organization=self.organization)
        location.save()

        product = Product(name="Test Product", organization=self.organization)
        product.save()

        inventory = Inventory(location=location,
                              product=product,
                              organization=self.organization)
        inventory.save()

        inventory.add(10)
        inventory.refresh_from_db()

        self.assertEqual(inventory.amount, 10.0)

        response = self.client.get("/{}/shortcuts/sales".format(
            self.organization.slug))
        self.assertEqual(response.status_code, 200)

        response = self.client.post(
            "/{}/shortcuts/sales".format(self.organization.slug),
            {
                "reserved": "on",
                "amount": 1.0,
                "product": product.id,
                "location": location.id,
                "desc": "Reservation for Joost",
            },
            follow=True,
        )

        self.assertEqual(response.status_code, 200)

        messages = list(response.context["messages"])
        self.assertEqual(len(messages), 1)
        self.assertEqual(str(messages[0]), "Reserved 1.0 Test Product!")

        mutation = Mutation.objects.filter(amount=-1.0)[0]
        self.assertEqual(
            mutation.desc,
            "Reserved {} {} - {}".format(1.0, product.name,
                                         "Reservation for Joost"),
        )

        # It should not subtract from inventory just yet...
        inventory.refresh_from_db()
        self.assertEqual(inventory.amount, 10.0)

        response = self.client.get(
            "/{}/reservation/{}?action=confirm".format(self.organization.slug,
                                                       mutation.id),
            follow=True,
        )
        self.assertEqual(response.status_code, 200)
        messages = list(response.context["messages"])
        self.assertEqual(len(messages), 1)
        self.assertEqual(str(messages[0]), "Reservation confirmed!")

        # It should be subtracted from inventory now..
        inventory.refresh_from_db()
        self.assertEqual(inventory.amount, 9.0)

        # And it should have aremove operation
        mutation.refresh_from_db()
        self.assertEqual(mutation.operation, "remove")

        # Create another reservation
        response = self.client.post(
            "/{}/shortcuts/sales".format(self.organization.slug),
            {
                "reserved": "on",
                "amount": 1.0,
                "product": product.id,
                "location": location.id,
                "desc": "Reservation for Joost to cancel",
            },
            follow=True,
        )

        self.assertEqual(response.status_code, 200)

        # It should not subtract from inventory just yet...
        inventory.refresh_from_db()
        self.assertEqual(inventory.amount, 9.0)

        response = self.client.get(
            "/{}/reservation/{}?action=cancel".format(self.organization.slug,
                                                      mutation.id),
            follow=True,
        )
        messages = list(response.context["messages"])
        self.assertEqual(len(messages), 1)
        self.assertEqual(str(messages[0]), "Reservation cancelled!")

        # The mutation should be deleted... and no inventory removed
        inventory.refresh_from_db()
        self.assertEqual(inventory.amount, 9.0)