def test_add(self): repository = InMemoryProductRepository() product = Product(name='The Last of Us Part II', sku='AHJU-49685', cost=10.00, price=220.00, inventory_quantity=150) product_id = repository.add(product) created_product = repository.get_by_id(product_id) self.assertIsInstance(product_id, str) self.assertEqual('1', product_id) self.assertEqual('1', created_product.id) self.assertEqual(product.name, created_product.name) self.assertEqual(product.sku, created_product.sku) self.assertEqual(product.price, created_product.price) self.assertEqual(product.inventory_quantity, created_product.inventory_quantity) self.assertEqual( '2', repository.add( Product(name='The Last of Us Part II', sku='AHJU-496851', cost=10.00, price=220.00, inventory_quantity=150)))
def test_list(self): repository = InMemoryProductRepository() first_product = Product(name='The Last of Us Part II', sku='AHJU-49685', cost=10.00, price=220.00, inventory_quantity=150) second_product = Product(name='God of war', sku='AHJU-49681', cost=20.00, price=220.00, inventory_quantity=80) first_product_id = repository.add(first_product) second_product_id = repository.add(second_product) created_products = repository.list() self.assertIsInstance(created_products, list) self.assertEqual(2, len(created_products)) self.assertEqual(first_product_id, created_products[0].id) self.assertEqual(first_product.name, created_products[0].name) self.assertEqual(first_product.sku, created_products[0].sku) self.assertEqual(first_product.price, created_products[0].price) self.assertEqual(first_product.inventory_quantity, created_products[0].inventory_quantity) self.assertEqual(second_product_id, created_products[1].id) self.assertEqual(second_product.name, created_products[1].name) self.assertEqual(second_product.sku, created_products[1].sku) self.assertEqual(second_product.price, created_products[1].price) self.assertEqual(second_product.inventory_quantity, created_products[1].inventory_quantity)
def test_should_update_infos(self): product = Product(id=1, name='Last of Us Part II', sku='AHJU-4968', cost=2.00, price=100.00, inventory_quantity=100) product.update_infos(name='The Last of Us Part II', cost=10.00, price=220.00, inventory_quantity=150) self.assertEqual(product.name, 'The Last of Us Part II') self.assertEqual(product.cost, 10.00) self.assertEqual(product.price, 220.00) self.assertEqual(product.inventory_quantity, 150)
def __create_product_from_mongo(self, mongo_product: dict) -> Product: return Product(id=str(mongo_product['_id']), name=mongo_product['name'], sku=mongo_product['sku'], cost=mongo_product['cost'], price=mongo_product['price'], inventory_quantity=mongo_product['inventoryQuantity'])
def test_product_initialization_should_have_an_id(self): product = Product(id=1, name='The Last of Us Part II', sku='AHJU-49685', cost=10.00, price=220.00, inventory_quantity=150) self.assertEqual(product.id, 1)
def test_update(self): repository = MongoProductRepository(self.mongo_db) product = Product(name='Last of Us Part II', sku='AHJU-4968', cost=2.00, price=100.00, inventory_quantity=100) product_id = repository.add(product) repository.add( Product(name='Bloodborne', sku='AHJU-1458', cost=50.00, price=200.00, inventory_quantity=70)) product.define_id(product_id) product.update_infos(name='The Last of Us Part II', cost=10.00, price=220.00, inventory_quantity=150) repository.update(product) product = repository.get_by_id(product_id) self.assertEqual(product.id, product_id) self.assertEqual(product.name, 'The Last of Us Part II') self.assertEqual(product.cost, 10.00) self.assertEqual(product.price, 220.00) self.assertEqual(product.inventory_quantity, 150)
def test_update_should_raise_not_found_when_cant_find_product(self): repository = MongoProductRepository(self.mongo_db) product = Product(id='5f566e9c1022bd08188d674b', name='Last of Us Part II', sku='AHJU-4968', cost=2.00, price=100.00, inventory_quantity=100) with self.assertRaises(NotFound): repository.update(product)
def test_product_initialization_should_have_all_fields(self): product = Product(name='The Last of Us Part II', sku='AHJU-49685', cost=10.00, price=220.00, inventory_quantity=150) self.assertEqual(product.name, 'The Last of Us Part II') self.assertEqual(product.sku, 'AHJU-49685') self.assertEqual(product.cost, 10.00) self.assertEqual(product.price, 220.00) self.assertEqual(product.inventory_quantity, 150)
def test_remove(self): repository = InMemoryProductRepository() product = Product(name='The Last of Us Part II', sku='AHJU-49685', cost=10.00, price=220.00, inventory_quantity=150) product_id = repository.add(product) repository.remove(product_id) with self.assertRaises(NotFound): repository.get_by_id(product_id)
def test_list_with_skus(self): repository = MongoProductRepository(self.mongo_db) first_product = Product(name='The Last of Us Part II', sku='AHJU-49685', cost=10.00, price=220.00, inventory_quantity=150) second_product = Product(name='God of war', sku='AHJU-49684', cost=20.00, price=220.00, inventory_quantity=80) third_product = Product(name='Horizon Zero Dawn', sku='AHJU-49610', cost=20.00, price=220.00, inventory_quantity=80) first_product_id = repository.add(first_product) second_product_id = repository.add(second_product) repository.add(third_product) skus_products = repository.list_with_skus(['AHJU-49685', 'AHJU-49684']) self.assertIsInstance(skus_products, list) self.assertEqual(2, len(skus_products)) self.assertEqual(first_product_id, skus_products[0].id) self.assertEqual(first_product.name, skus_products[0].name) self.assertEqual(first_product.sku, skus_products[0].sku) self.assertEqual(first_product.price, skus_products[0].price) self.assertEqual(first_product.inventory_quantity, skus_products[0].inventory_quantity) self.assertEqual(second_product_id, skus_products[1].id) self.assertEqual(second_product.name, skus_products[1].name) self.assertEqual(second_product.sku, skus_products[1].sku) self.assertEqual(second_product.price, skus_products[1].price) self.assertEqual(second_product.inventory_quantity, skus_products[1].inventory_quantity)
def test_add_should_raise_skuExistsError_when_another_product_has_the_same_sku( self): repository = MongoProductRepository(self.mongo_db) product = Product(name='The Last of Us Part II', sku='AHJU-49685', cost=10.00, price=220.00, inventory_quantity=150) repository.add(product) with self.assertRaises(skuExistsError): repository.add(product)
def test_get_by_sku(self): repository = MongoProductRepository(self.mongo_db) product = Product(name='The Last of Us Part II', sku='AHJU-49685', cost=10.00, price=220.00, inventory_quantity=150) product_id = repository.add(product) created_product = repository.get_by_sku('AHJU-49685') self.assertIsInstance(created_product, Product) self.assertEqual(product_id, created_product.id) self.assertEqual(product.name, created_product.name) self.assertEqual(product.sku, created_product.sku) self.assertEqual(product.price, created_product.price) self.assertEqual(product.inventory_quantity, created_product.inventory_quantity)
def test_add_should_raise_skuExistsError_when_another_product_has_the_same_sku( self): repository = InMemoryProductRepository() product = Product(name='The Last of Us Part II', sku='AHJU-49685', cost=10.00, price=220.00, inventory_quantity=150) product_id = repository.add(product) created_product = repository.get_by_id(product_id) self.assertIsInstance(product_id, str) self.assertEqual('1', product_id) self.assertEqual('1', created_product.id) self.assertEqual(product.name, created_product.name) self.assertEqual(product.sku, created_product.sku) self.assertEqual(product.price, created_product.price) self.assertEqual(product.inventory_quantity, created_product.inventory_quantity) with self.assertRaises(skuExistsError): self.assertEqual('2', repository.add(product))
def add(self, product: Product) -> str: product = deepcopy(product) product.define_id(self.__next_id()) self.__raise_if_sku_already_exists(product.sku) self.__products.append(product) return product.id
def create_product(self, product_creation_command: dict) -> Product: product = Product(**product_creation_command) product_id = self.__product_repository.add(product) product.define_id(product_id) return product