Exemple #1
0
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
def products_main():
    '''This is the main /products routes.'''
    required_fields = ['name', 'description', 'start_bid']
    if request.method == 'POST':
        input_dict = request.get_json(force=True)
        _log.debug('Product POST request received with body %s', input_dict)
        if all(field in input_dict for field in required_fields):
            name = input_dict['name']
            description = input_dict['description']
            start = input_dict['start_bid']
            new_product = Product(name, description, start)
            create_product(new_product)
            return jsonify(new_product.to_dict()), 201
        else:
            return request.json, 400
    elif request.method == 'GET':
        _log.debug('GET request for products')
        _log.debug(db.read_all_products())
        return jsonify(db.read_all_products()), 200
    else:
        pass
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)