def create_product(new_product: Product): '''Create a Product in the database''' new_product.set_id(_get_product_id_counter()) try: products.insert_one(new_product.to_dict()) op_success = new_product except pymongo.errors.DuplicateKeyError as err: _log.error(err) op_success = None _log.info('Added %s product. ID: %s.', new_product.get_name(), new_product.get_id()) return op_success
class ProductTestSuite(unittest.TestCase): '''Test suite for Product class''' product = None def setUp(self): self.product = Product('name', 'description', 0) def tearDown(self): self.product = None @classmethod def setUpClass(cls): cls.product = Product() @classmethod def tearDownClass(cls): cls.product = None def test_get_id(self): '''Test for the get_id function.''' self.assertEqual(self.product._id, self.product.get_id()) def test_get_name(self): '''Test for the get_name function.''' self.assertEqual(self.product.name, self.product.get_name()) def test_get_description(self): '''Test for the get_description function.''' self.assertEqual(self.product.description, self.product.get_description()) def test_get_start_bid(self): '''Test for the get_start_bid function.''' self.assertEqual(self.product.start_bid, self.product.get_start_bid()) def test_get_status(self): '''Test for the get_status function.''' self.assertEqual(self.product.status, self.product.get_status()) def test_set_id(self): '''Test for the set_id function.''' self.product.set_id(10) self.assertEqual(10, self.product._id) def test_set_name(self): '''Test for the set_name function.''' self.product.set_name("named") self.assertEqual(self.product.name, "named") def test_set_description(self): '''Test for the set_description function.''' self.product.set_description("desc") self.assertEqual(self.product.description, "desc") def test_set_start_bid(self): '''Test for the start_bid function.''' self.product.set_start_bid(1) self.assertEqual(self.product.start_bid, 1) def test_set_status(self): '''Test for the set_status function.''' self.product.set_status("Approved") self.assertEqual(self.product.status, "Approved") def test_to_dict(self): '''Test for the t0_dict function.''' self.product = Product("name", "descript", 1) self.assertIs(type(self.product.to_dict()), dict) def test_from_dict(self): '''Test for the from_dict function.''' test_dict = {'name': 'name', 'description': 'desc', 'start_bid': 1} self.product = Product().from_dict(test_dict) self.assertIs(type(self.product), Product)