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_search_result(self): location = Location(name="Test Location", organization=self.organization) location.save() product = Product(name="Test Product", organization=self.organization) product.save() response = self.client.get("/{}/search?q=Test".format( self.organization.slug)) self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200) self.assertIn("Test Location", response.content.decode()) self.assertIn("Test Product", response.content.decode()) self.assertNotIn("*****@*****.**", response.content.decode()) # Test case insensitity response = self.client.get("/{}/search?q=test".format( self.organization.slug)) self.assertEqual(response.status_code, 200) self.assertIn("Test Location", response.content.decode()) self.assertIn("Test Product", response.content.decode()) self.assertNotIn("*****@*****.**", response.content.decode()) # Test finding a user response = self.client.get("/{}/search?q=lennon".format( self.organization.slug)) self.assertEqual(response.status_code, 200) self.assertNotIn("Test Location", response.content.decode()) self.assertNotIn("Test Product", response.content.decode()) self.assertIn("*****@*****.**", response.content.decode())
def test_mutations(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) mutation = Mutation( product=product, location=location, amount=3.0, organization=self.organization, ) mutation.save() inventory = Inventory.objects.get() self.assertEqual(inventory.amount, 3.0)
def test_create_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() self.assertEqual(inventory.amount, 0)
def test_mutation_zero_amount(self): location = Location(name="Test Location", organization=self.organization) location.save() product = Product(name="Test Product", organization=self.organization) product.save() mutation = Mutation( product=product, location=location, amount=0.0, organization=self.organization, ).save() self.assertEqual(mutation, None)
def test_inventory_count(self): location = Location(name="Test Location", organization=self.organization) location.save() location_2 = Location(name="Test Location 2", organization=self.organization) location_2.save() product = Product(name="Test Product", organization=self.organization) product.save() inventory = Inventory( location=location, product=product, amount=1.0, organization=self.organization, ) inventory.save() inventory = Inventory( location=location_2, product=product, amount=1.0, organization=self.organization, ) inventory.save() self.assertEqual(product.inventory.count(), 2) self.assertEqual(product.inventory_count, 2)
def test_inventory_should_only_be_seen_by_the_organization(self): # Create a location for the first organization location = Location(name="Test Location", organization=self.organization) location.save() # Create a location for the first organization product = Product(name="Test Product", organization=self.organization) product.save() inventory = Inventory( location=location, product=product, organization=self.organization, amount=10, ) inventory.save() # The inventory should be available for the first organization with the first user logged in response = self.client.get("/{}/inventory/location".format( self.organization.slug)) self.assertEqual(response.status_code, 200) self.assertIn("Test Location", response.content.decode()) response = self.client.get("/{}/inventory/product".format( self.organization.slug)) self.assertEqual(response.status_code, 200) self.assertIn("Test Product", response.content.decode()) self.client.login(email="*****@*****.**", password="******") # The second user with a different organization should not be able to see it response = self.client.get("/{}/inventory/location".format( self.organization_2.slug)) self.assertEqual(response.status_code, 200) self.assertNotIn("Test Location", response.content.decode()) response = self.client.get("/{}/inventory/product".format( self.organization_2.slug)) self.assertEqual(response.status_code, 200) self.assertNotIn("Test Product", response.content.decode()) # Even when forcing the url to be the same as organization 1 response = self.client.get("/{}/inventory/location".format( self.organization.slug)) self.assertIn("Page not found", response.content.decode()) response = self.client.get("/{}/inventory/product".format( self.organization.slug)) self.assertIn("Page not found", response.content.decode())
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)
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)
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 setUp(self): super(TestBaseWithInventory, self).setUp() self.product = Product(name="Test Product", organization=self.organization) self.product.save() self.location = Location(name="Test Location", organization=self.organization) self.location.save() self.inventory = Inventory( organization=self.organization, product=self.product, location=self.location, amount=100, ) self.inventory.save()
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)
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_location_should_only_be_seen_by_the_organization(self): # Create a location for the first organization location = Location(name="Test Location", organization=self.organization) location.save() # The location should be available for the first organization with the first user logged in response = self.client.get("/{}/location/{}".format( self.organization.slug, location.id)) self.assertEqual(response.status_code, 200) self.client.login(email="*****@*****.**", password="******") # The second user with a different organization should not be able to see it response = self.client.get("/{}/location/{}".format( self.organization_2.slug, location.id)) self.assertIn("Page not found", response.content.decode()) # Even when forcing the url to be the same as organization 1 response = self.client.get("/{}/location/{}".format( self.organization.slug, location.id)) self.assertIn("Page not found", response.content.decode())
def test_delete_location(self): location = Location(name="Test Location", organization=self.organization) location.save() self.assertEqual(Location.objects.count(), 1) location.delete() self.assertEqual(Location.objects.count(), 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])
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)
def test_create_location(self): location = Location(name="Test Location", organization=self.organization) location.save() self.assertEqual(Location.objects.count(), 1) self.assertEqual(Location.objects.get().name, "Test Location")