def test_should_update_infos(self): kit_products = [ KitProduct(product_sku='FASD-498', quantity=2, discount_percentage=10.5), KitProduct(product_sku='FASD-1489', quantity=1, discount_percentage=10.5) ] kit = Kit(name='Sony Gaming Pack', sku='FASD-789', kit_products=kit_products) updated_kit_products = [ KitProduct(product_sku='FASD-4918', quantity=7, discount_percentage=20.5), KitProduct(product_sku='FASD-14289', quantity=7, discount_percentage=32.5) ] kit.update_infos(name='Sony Gaming Pack I', kit_products=updated_kit_products) self.assertEqual(kit.name, 'Sony Gaming Pack I') self.assertEqual(kit.kit_products[0], updated_kit_products[0]) self.assertEqual(kit.kit_products[1], updated_kit_products[1])
def test_kit_initialization(self): kit_products = [ KitProduct(product_sku='FASD-498', quantity=2, discount_percentage=10.5), KitProduct(product_sku='FASD-1489', quantity=1, discount_percentage=10.5) ] kit = Kit(name='Sony Gaming Pack', sku='FASD-789', kit_products=kit_products) self.assertEqual(kit.name, 'Sony Gaming Pack') self.assertEqual(kit.sku, 'FASD-789') self.assertEqual(kit.kit_products, kit_products)
def test_list(self): repository = MongoKitRepository(self.mongo_db) first_kit_products = [ KitProduct(product_sku='FASD-498', quantity=2, discount_percentage=10.5), KitProduct(product_sku='FASD-14891', quantity=1, discount_percentage=10.5) ] first_kit = Kit(name='Sony Gaming Pack', sku='FASD-789', kit_products=first_kit_products) second_kit_products = [ KitProduct(product_sku='FASD-498', quantity=9, discount_percentage=10.5), KitProduct(product_sku='FASD-1479', quantity=1, discount_percentage=10.5) ] second_kit = Kit(name='Sony Gaming Pack II', sku='FASD-7894', kit_products=second_kit_products) first_kit_id = repository.add(first_kit) second_kit_id = repository.add(second_kit) created_kits = repository.list() self.assertIsInstance(created_kits, list) self.assertEqual(2, len(created_kits)) self.assertEqual(first_kit_id, created_kits[0].id) self.assertEqual(first_kit.sku, created_kits[0].sku) self.assertEqual(first_kit.name, created_kits[0].name) self.assertEqual(first_kit.kit_products[0], created_kits[0].kit_products[0]) self.assertEqual(first_kit.kit_products[1], created_kits[0].kit_products[1]) self.assertEqual(second_kit_id, created_kits[1].id) self.assertEqual(second_kit.sku, created_kits[1].sku) self.assertEqual(second_kit.name, created_kits[1].name) self.assertEqual(second_kit.kit_products[0], created_kits[1].kit_products[0]) self.assertEqual(second_kit.kit_products[1], created_kits[1].kit_products[1])
def test_product_kit_initialization(self): kit_product = KitProduct(product_sku='FASD-498', quantity=2, discount_percentage=10.5) self.assertEqual(kit_product.product_sku, 'FASD-498') self.assertEqual(kit_product.quantity, 2) self.assertEqual(kit_product.discount_percentage, 10.5)
def test_update_kit(self): kit_update_command = { 'sku': 'FASF-123', 'name': 'Sony Pack I', 'kit_products': [{ 'product_sku': 'AHJU-49685', 'quantity': 1, 'discount_percentage': 10 }, { 'product_sku': 'AHJU-49621', 'quantity': 2, 'discount_percentage': 15 }] } kit_mock = mock.MagicMock() product_repository_mock = mock.MagicMock() kit_repository_mock = mock.MagicMock() kit_repository_mock.get_by_id.return_value = kit_mock service = KitsService(kit_repository_mock, product_repository_mock) updated_kit = service.update_kit(1, kit_update_command) self.assertEqual(updated_kit, kit_mock) kit_repository_mock.get_by_id.assert_called_with(1) kit_products = [ KitProduct(**kit_product) for kit_product in kit_update_command.pop('kit_products') ] kit_mock.update_infos.assert_called_with(**kit_update_command, kit_products=kit_products) kit_repository_mock.update.assert_called_with(kit_mock)
def test_update_should_raise_not_found_when_cant_find_product(self): repository = MongoKitRepository(self.mongo_db) kit_products = [ KitProduct(product_sku='FASD-498', quantity=2, discount_percentage=10.5), KitProduct(product_sku='FASD-1489', quantity=1, discount_percentage=10.5) ] kit = Kit(id='5f566e9c1022bd08188d674b', name='Sony Gaming Pack', sku='FASD-789', kit_products=kit_products) with self.assertRaises(NotFound): repository.update(kit)
def test_remove(self): repository = MongoKitRepository(self.mongo_db) kit_products = [ KitProduct(product_sku='FASD-498', quantity=2, discount_percentage=10.5), KitProduct(product_sku='FASD-1489', quantity=1, discount_percentage=10.5) ] kit = Kit(name='Sony Gaming Pack', sku='FASD-789', kit_products=kit_products) kit_id = repository.add(kit) repository.remove(kit_id) with self.assertRaises(NotFound): repository.get_by_id(kit_id)
def test_add_should_raise_skuExistsError_when_another_kit_has_the_same_sku( self): repository = MongoKitRepository(self.mongo_db) kit_products = [ KitProduct(product_sku='FASD-498', quantity=2, discount_percentage=10.5), KitProduct(product_sku='FASD-1489', quantity=1, discount_percentage=10.5) ] kit = Kit(name='Sony Gaming Pack', sku='FASD-789', kit_products=kit_products) repository.add(kit) with self.assertRaises(skuExistsError): repository.add(kit)
def create_kit(self, kit_creation_command: dict) -> Kit: kit_products = [] for kit_product_dict in kit_creation_command.pop('kit_products'): self.__product_repository.get_by_sku( kit_product_dict['product_sku']) kit_products.append(KitProduct(**kit_product_dict)) kit = Kit(**kit_creation_command, kit_products=kit_products) kit_id = self.__kit_repository.add(kit) kit.define_id(kit_id) return kit
def __create_kit_from_mongo(kit_mongo: dict) -> Kit: kit_products = [ KitProduct( product_sku=kit_product_mongo['productSku'], quantity=kit_product_mongo['quantity'], discount_percentage=kit_product_mongo['discountPercentage']) for kit_product_mongo in kit_mongo.pop('kitProducts') ] return Kit(id=str(kit_mongo['_id']), name=kit_mongo['name'], sku=kit_mongo['sku'], kit_products=kit_products)
def update_kit(self, kit_id: str, kit_update_command: dict) -> Kit: kit_update_command = deepcopy(kit_update_command) kit = self.__kit_repository.get_by_id(kit_id) kit_products = [] for kit_product_dict in kit_update_command.pop('kit_products'): self.__product_repository.get_by_sku( kit_product_dict['product_sku']) kit_products.append(KitProduct(**kit_product_dict)) kit.update_infos(**kit_update_command, kit_products=kit_products) self.__kit_repository.update(kit) return kit
def test_add(self): repository = MongoKitRepository(self.mongo_db) kit_products = [ KitProduct(product_sku='FASD-498', quantity=2, discount_percentage=10.5), KitProduct(product_sku='FASD-1489', quantity=1, discount_percentage=10.5) ] kit = Kit(name='Sony Gaming Pack', sku='FASD-789', kit_products=kit_products) kit_id = repository.add(kit) created_kit = repository.get_by_id(kit_id) self.assertIsInstance(kit_id, str) self.assertEqual(kit_id, created_kit.id) self.assertEqual(kit.sku, created_kit.sku) self.assertEqual(kit.name, created_kit.name) self.assertEqual(kit.kit_products[0], created_kit.kit_products[0]) self.assertEqual(kit.kit_products[1], created_kit.kit_products[1])
def test_update(self): repository = MongoKitRepository(self.mongo_db) kit_products = [ KitProduct(product_sku='FASD-498', quantity=2, discount_percentage=10.5), KitProduct(product_sku='FASD-1489', quantity=1, discount_percentage=10.5) ] kit = Kit(name='Sony Gaming Pack', sku='FASD-789', kit_products=kit_products) kit_id = repository.add(kit) kit.define_id(kit_id) repository.add( Kit(name='Sony Gaming Pack II', sku='FASD-7894', kit_products=[ KitProduct(product_sku='FASD-4988', quantity=9, discount_percentage=10.5), KitProduct(product_sku='FASD-1489', quantity=1, discount_percentage=10.5) ])) kit.update_infos(name='Sony Gaming Pack I', kit_products=[ KitProduct(product_sku='FASD-498', quantity=7, discount_percentage=80.5), KitProduct(product_sku='FASD-1429', quantity=5, discount_percentage=72.5) ]) repository.update(kit) kit = repository.get_by_id(kit_id) self.assertEqual(kit.name, 'Sony Gaming Pack I') self.assertEqual(kit.kit_products[0], kit.kit_products[0]) self.assertEqual(kit.kit_products[1], kit.kit_products[1])