def test_list_with_product(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) third_kit_products = [ KitProduct(product_sku='FASD-49809', quantity=9, discount_percentage=10.5), KitProduct(product_sku='FASD-147099', quantity=1, discount_percentage=10.5) ] third_kit = Kit(name='Sony Gaming Pack III', sku='FASD-78990', kit_products=third_kit_products) first_kit_id = repository.add(first_kit) second_kit_id = repository.add(second_kit) third_kit_id = repository.add(third_kit) created_kits = repository.list_with_product('FASD-498') 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_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 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 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_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 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])
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 add(self, kit: Kit) -> str: kit = deepcopy(kit) kit.define_id(self.__next_id()) self.__raise_if_sku_already_exists(kit.sku) self.__kits.append(kit) return kit.id