def test_move_output_to_customer_launch_proper_signals(self): self.s_output_sender = None self.s_output_product = None @receiver(output_changed) def output_listener(sender, product, **kwargs): self.s_output_sender = sender self.s_output_product = product self.s_customer_sender = None self.s_customer_product = None @receiver(customer_changed) def customer_listener(sender, product, **kwargs): self.s_customer_sender = sender self.s_customer_product = product quantity = 20 price = Money(3, bazaar_settings.DEFAULT_CURRENCY) move(self.output, self.customer, self.product, quantity, price) output_stock = Stock.objects.get(location=self.output, product=self.product) customer_stock = Stock.objects.get(location=self.customer, product=self.product) self.assertEqual(self.s_output_sender, output_stock) self.assertEqual(self.s_customer_sender, customer_stock) self.assertEqual(self.s_output_product, self.product) self.assertEqual(self.s_customer_product, self.product) self.assertEqual(self.s_output_sender.quantity, -quantity) self.assertEqual(self.s_customer_sender.quantity, quantity) self.assertEqual(self.s_customer_sender.unit_price, price)
def test_move_supplier_to_storage_launch_proper_signals(self): self.s_supplier_sender = None self.s_supplier_product = None @receiver(supplier_changed) def supplier_listener(sender, product, **kwargs): self.s_supplier_sender = sender self.s_supplier_product = product self.s_storage_sender = None self.s_storage_product = None @receiver(storage_changed) def storage_listener(sender, product, **kwargs): self.s_storage_sender = sender self.s_storage_product = product quantity = 20 price = Money(3, bazaar_settings.DEFAULT_CURRENCY) move(self.supplier, self.storage, self.product, quantity, price) supplier_stock = Stock.objects.get(location=self.supplier, product=self.product) storage_stock = Stock.objects.get(location=self.storage, product=self.product) self.assertEqual(self.s_supplier_sender, supplier_stock) self.assertEqual(self.s_storage_sender, storage_stock) self.assertEqual(self.s_supplier_product, self.product) self.assertEqual(self.s_storage_product, self.product) self.assertEqual(self.s_supplier_sender.quantity, -quantity) self.assertEqual(self.s_storage_sender.quantity, quantity) self.assertEqual(self.s_storage_sender.unit_price, price)
def test_movement_converts_currency(self): move(self.supplier, self.storage, self.product, 10, Money(1.0, "USD")) movement = Movement.objects.get(from_location=self.supplier, to_location=self.storage) self.assertEqual(movement.unit_price, Money(0.74, "EUR")) self.assertEqual(movement.original_unit_price, Money(1.0, "USD"))
def test_movement_is_created(self): self.assertFalse(Movement.objects.filter( from_location=self.supplier, to_location=self.storage).exists()) move(self.supplier, self.storage, self.product, 10, 1.0) self.assertTrue(Movement.objects.filter( from_location=self.supplier, to_location=self.storage).exists())
def test_stocks_are_created_on_move(self): self.assertFalse(Stock.objects.filter(location=self.supplier, product=self.product).exists()) self.assertFalse(Stock.objects.filter(location=self.storage, product=self.product).exists()) move(self.supplier, self.storage, self.product, 10, 1.0) self.assertTrue(Stock.objects.filter(location=self.supplier, product=self.product).exists()) self.assertTrue(Stock.objects.filter(location=self.storage, product=self.product).exists())
def test_movement_is_created(self): self.assertFalse( Movement.objects.filter(from_location=self.supplier, to_location=self.storage).exists()) move(self.supplier, self.storage, self.product, 10, 1.0) self.assertTrue( Movement.objects.filter(from_location=self.supplier, to_location=self.storage).exists())
def test_cost_sort(self): """ Test that sort by cost works correctly """ product2 = f.ProductFactory(name='product2', price=1, description='the best you can have!') api.move(get_lost_and_found(), get_storage(), product2, 1, 5000) self.client.login(username=self.user.username, password='******') response = self.client.get("/products/?&order_by=purchase_price") self.assertEqual(response.status_code, status.HTTP_200_OK) products = response.context_data['product_list'] self.assertEqual(products.count(), 2) self.assertEqual(products[0].name, 'product1')
def test_stocks_quantity_updated_on_move(self): move(self.supplier, self.storage, self.product, 10, 1.0) move(self.supplier, self.storage, self.product, 10, 1.0) move(self.storage, self.output, self.product, 10, 1.0) move(self.output, self.storage, self.product, 5, 1.0) move(self.storage, self.supplier, self.product, 2, 1.0) supplier_stock = Stock.objects.get(location=self.supplier, product=self.product) storage_stock = Stock.objects.get(location=self.storage, product=self.product) output_stock = Stock.objects.get(location=self.output, product=self.product) self.assertEqual(supplier_stock.quantity, -18) self.assertEqual(storage_stock.quantity, 13) self.assertEqual(output_stock.quantity, 5)
def test_stocks_are_created_on_move(self): self.assertFalse( Stock.objects.filter(location=self.supplier, product=self.product).exists()) self.assertFalse( Stock.objects.filter(location=self.storage, product=self.product).exists()) move(self.supplier, self.storage, self.product, 10, 1.0) self.assertTrue( Stock.objects.filter(location=self.supplier, product=self.product).exists()) self.assertTrue( Stock.objects.filter(location=self.storage, product=self.product).exists())
def test_move_raise_error_with_invalid_quantity(self): with self.assertRaises(MovementException): move(self.supplier, self.storage, self.product, -1, 1.0)
def test_move_raise_error_with_invalid_data(self): with self.assertRaises(MovementException): move(None, None, self.product, 10, 1.0)
def test_stocks_price_updated_on_move(self): move(self.supplier, self.storage, self.product, 10, 1.0) move(self.supplier, self.storage, self.product, 10, 0.5) move(self.supplier, self.storage, self.product, 20, 0.4) move(self.storage, self.output, self.product, 1, 2.5) move(self.storage, self.output, self.product, 2, 2.0) move(self.storage, self.output, self.product, 5, 1.5) supplier_stock = Stock.objects.get(location=self.supplier, product=self.product) storage_stock = Stock.objects.get(location=self.storage, product=self.product) output_stock = Stock.objects.get(location=self.output, product=self.product) self.assertEqual(supplier_stock.unit_price, Money(0, "EUR")) self.assertEqual(storage_stock.unit_price, Money(0.575, "EUR")) self.assertEqual(output_stock.unit_price, Money(1.75, "EUR"))
def setUp(self): self.time_ago = timezone.now() self.user = get_user_model().objects.create_user(username='******', email='*****@*****.**', password='******') self.lost_and_found = f.LocationFactory(name='lost and found', slug='lost_and_found', type=4) self.storage = f.LocationFactory(name='storage', slug='storage', type=1) self.output = f.LocationFactory(name='output', slug='output', type=2) self.customer = f.LocationFactory(name='customer', slug='customer', type=3) self.product1 = f.ProductFactory(name='product1', price=2, description='cheap!') self.product2 = f.ProductFactory(name='product2', price=20, description='not cheap!') self.product3 = f.ProductFactory(name='product3', price=200, description='really expensive!') # Move products to the warehouse api.move(self.lost_and_found, self.storage, self.product1, 1, 3) api.move(self.lost_and_found, self.storage, self.product2, 10, 30) api.move(self.lost_and_found, self.storage, self.product3, 100, 300) api.move(self.storage, self.output, self.product1, 1, 5) api.move(self.storage, self.output, self.product2, 1, 50) api.move(self.output, self.storage, self.product2, 1, 50) api.move(self.storage, self.output, self.product3, 5, 500) api.move(self.output, self.customer, self.product3, 2, 500)