Exemplo n.º 1
0
 def setUp(self):
     """ Runs before each test """
     # service.init_db()
     db.drop_all()    # clean up the last tests
     db.create_all()  # create new tables
     Product(pid=1,pname="Athens Table", pdesc='Stupid Table', pcat="Table", pprice=20, pcond="Boxed",pinv=2, prev="", prat=5).save()
     Product(pid=2,pname="Rome Chair", pdesc='Stupid Chair', pcat="Chair", pprice=40, pcond="Boxed", pinv=2, prev="",prat=8).save()
     self.app = service.app.test_client()
Exemplo n.º 2
0
 def test_find_by_pricerange(self):
     """ Find a Product by PriceRange """
     Product(1, "Couch", "White couch", "Furniture", 200, "Boxed", 50, " ",
             8).save()
     Product(2, "Table", "Oak table", "Furniture", 150, "Boxed", 100, " ",
             7).save()
     products = Product.search_by_price(160, 210)
     self.assertEqual(products[0].id, 1)
     self.assertEqual(products[0].category, "Furniture")
     self.assertEqual(products[0].name, "Couch")
     self.assertEqual(products[0].description, "White couch")
     self.assertEqual(products[0].price, 200)
     self.assertEqual(products[0].condition, "Boxed")
     self.assertEqual(products[0].inventory, 50)
     self.assertEqual(products[0].rating, 8)
Exemplo n.º 3
0
def dbcreate():
    Product.logger.info(Product.query.with_entities(Product.id).all())
    if ((Product.query.with_entities(Product.id).all()) != []):
        return
    Product(1, 'Desk', 'Wooden study desk', 'Furniture', 50, 'Boxed', 500,
            "Great product", 8).save()
    Product(2, 'Chair', 'Mesh office chair', 'Furniture', 30, 'Boxed', 1500,
            "Nothing great", 9).save()
    Product(3, 'Tshirt', 'Black printed tshirt', 'Clothing', 20, 'Boxed', 900,
            "So so", 8).save()
    Product(4, 'Trousers', 'Brown linen trouser', 'Clothing', 35, 'Boxed', 300,
            "Awesome!", 10).save()
    Product(5, 'Mattress', 'Memory foam mattress', 'Furniture', 100, 'Boxed',
            2000, "Still checking", 7).save()
    Product.logger.info(Product.query.with_entities(Product.id).all())
Exemplo n.º 4
0
 def test_find_by_category(self):
     """ Find Products by Category """
     Product(1, "Couch", "White couch", "Furniture", 200, "Boxed", 50, " ",
             8).save()
     Product(2, "Table", "Oak table", "Home", 150, "Boxed", 100, " ",
             7).save()
     products = Product.find_by_category("Furniture")
     self.assertEqual(products[0].id, 1)
     self.assertEqual(products[0].category, "Furniture")
     self.assertEqual(products[0].name, "Couch")
     self.assertEqual(products[0].description, "White couch")
     self.assertEqual(products[0].price, 200)
     self.assertEqual(products[0].condition, "Boxed")
     self.assertEqual(products[0].inventory, 50)
     self.assertEqual(products[0].rating, 8)
Exemplo n.º 5
0
def create_product():
    print "creating Products..."
    products = [('Chair', 'chair.jpg', '50%-50%', 100.45, 15, 1000, 3, 1000, 1,
                 3),
                ('Round Table', 'round_table.jpg', '', 200.12, 25, 2000, 2,
                 1000, 2, 4),
                ('Dust Bin (No.9038)', 'dustbin.jpg', '', 200.67, 25, 2000, 2,
                 1000, 3, 2),
                ('Mesh Cover (No.9003)', 'round_mesh_cover.jpg', '', 200.12,
                 25, 2000, 2, 1000, 4, 1)]
    for p in products:
        product = Product()
        product.name = p[0]
        product.photo = p[1]
        product.multi_colors_ratio = p[2]
        product.weight = p[3]
        product.time_to_build = p[4]
        product.selling_price = p[5]
        product.num_employee_required = p[6]
        product.raw_material_weight_per_bag = p[7]
        machine = Machine.query.get(p[8])
        product.machine_id = machine.id
        color = Color.query.get(p[9])
        product.colors = [color]
        db.session.add(product)
Exemplo n.º 6
0
 def post(self):
     """
     Creates a Product
     This endpoint will create a Product based the data in the body that is posted
     """
     try:
         check_content_type('application/json')
         product = Product(1, "", "", "", 0, "", 0, "", 0)
         # product = Product()
         # app.logger.info((api.payload))
         product.deserialize(api.payload)
         product.save()
         message = product.serialize()
         location_url = api.url_for(ProductCollection,
                                    item_id=product.id,
                                    _external=True)
         # return make_response(jsonify(message), status.HTTP_201_CREATED,
         #                      {
         #                          'Location': location_url
         #                      })
         return product.serialize(), status.HTTP_201_CREATED, {
             'Location': location_url
         }
     except ValidationError:
         return request_validation_error('Invalid Data')
Exemplo n.º 7
0
 def test_find_by_id(self):
     """ Find a Product by ID """
     Product(1, "Couch", "White couch", "Furniture", 200, "Boxed", 50, " ",
             8).save()
     table = Product(2, "Table", "Oak table", "Home", 150, "Boxed", 100,
                     " ", 7)
     table.save()
     product = Product.find_by_id(table.id)
     self.assertIsNot(product, None)
     self.assertEqual(product.id, table.id)
     self.assertEqual(product.name, "Table")
     self.assertEqual(product.category, "Home")
     self.assertEqual(product.description, "Oak table")
     self.assertEqual(product.price, 150)
     self.assertEqual(product.condition, "Boxed")
     self.assertEqual(product.inventory, 100)
     self.assertEqual(product.rating, 7)
Exemplo n.º 8
0
 def test_delete_a_product(self):
     """ Delete a Product """
     product = Product(1, "Couch", "White couch", "Furniture", 200, "Boxed",
                       50, " ", 8)
     product.save()
     self.assertEqual(len(Product.all()), 1)
     # delete the product and make sure it isn't in the database
     product.delete()
     self.assertEqual(len(Product.all()), 0)
Exemplo n.º 9
0
 def test_sort_by_date(self):
     """ Sort Products by Date """
     Product(1, "Couch", "White couch", "Furniture", 200, "Boxed", 50, " ",
             8).save()
     table = Product(2, "Table", "Oak table", "Home", 150, "Boxed", 100,
                     " ", 7)
     table.save()
     table.price = 200
     table.update()
     product = list(Product.sort_by_date())[0]
     self.assertIsNot(product, None)
     self.assertEqual(product.id, table.id)
     self.assertEqual(product.name, "Table")
     self.assertEqual(product.category, "Home")
     self.assertEqual(product.description, "Oak table")
     self.assertEqual(product.price, 200)
     self.assertEqual(product.condition, "Boxed")
     self.assertEqual(product.inventory, 100)
     self.assertEqual(product.rating, 7)
Exemplo n.º 10
0
 def test_create_a_product(self):
     """ Create a product and assert that it exists """
     product = Product(1, "Couch", "White couch", "Furniture", 200, "Boxed",
                       50, " ", 8)
     self.assertTrue(product is not None)
     self.assertEqual(product.id, 1)
     self.assertEqual(product.name, "Couch")
     self.assertEqual(product.category, "Furniture")
     self.assertEqual(product.description, "White couch")
     self.assertEqual(product.price, 200)
     self.assertEqual(product.condition, "Boxed")
     self.assertEqual(product.inventory, 50)
     self.assertEqual(product.rating, 8)
Exemplo n.º 11
0
 def test_add_a_product(self):
     """ Create a product and add it to the database """
     products = Product.all()
     self.assertEqual(products, [])
     product = Product(1, "Couch", "White couch", "Furniture", 200, "Boxed",
                       50, " ", 8)
     self.assertTrue(product is not None)
     self.assertEqual(product.id, 1)
     product.save()
     # Assert that it was assigned an id and shows up in the database
     self.assertEqual(product.id, 1)
     products = Product.all()
     self.assertEqual(len(products), 1)
Exemplo n.º 12
0
def gen_sample_data():
    for i in range(10):
        user = User(username=f'user{i}', password='******')

        for j in range(10):
            name = ''.join(
                random.choices(string.ascii_uppercase + string.digits, k=10))
            Product(name=name, designer=user)

        db.session.add(user)
        db.session.commit()
        echo(f'{user} added.', fg='blue')
    echo('sample data generated.', fg='green')
Exemplo n.º 13
0
def add_product():
    name = request.json['product']
    product_category = request.json['category']

    if product_exists_name(name):
        return jsonify({'error': 'this product already exists in database'})
    elif not category_exists_id(product_category):
        return jsonify({'error': 'there is no such category'})
    else:
        product = Product(name, product_category)
        db.session.add(product)
        db.session.commit()
        return jsonify({'response': {'added': product.serialized}})
Exemplo n.º 14
0
 def test_deserialize_bad_data_missing_arg(self):
     """ Test deserialization of missing arg """
     data = {
         "id": 1,
         "name": "Couch",
         "category": "Furniture",
         "price": 200,
         "condition": "Boxed",
         "inventory": 50,
         "rating": 8,
         "review": " "
     }
     product = Product(1, "", "", "", 0, "", 0, "", 0)
     self.assertRaises(ValidationError, product.deserialize, data)
Exemplo n.º 15
0
def create(name: str, price: int) -> Product:
    """Create product

    Args:
        name: name of product
        price: price of product

    Returns:
        Product object
    """
    p = Product(name, price)
    db.session.add(p)
    db.session.commit()
    return p
Exemplo n.º 16
0
def create_products():
    """
    Creates a Pet
    This endpoint will create a Pet based the data in the body that is posted
    """
    check_content_type('application/json')
    product = Product(1, "", "", "", 0, "", 0, "", 0)
    product.deserialize(request.get_json())
    product.save()
    message = product.serialize()
    location_url = url_for('list_products_by_id',
                           item_id=product.id,
                           _external=True)
    return make_response(jsonify(message), status.HTTP_201_CREATED,
                         {'Location': location_url})
Exemplo n.º 17
0
 def test_update_a_product_rating(self):
     """ Update a Product Rating"""
     product = Product(1, "Couch", "White couch", "Furniture", 200, "Boxed",
                       50, " ", 8)
     product.save()
     self.assertEqual(product.id, 1)
     # Change it and save it
     product.rating = 10
     product.update()
     self.assertEqual(product.id, 1)
     # Fetch it back and make sure the id hasn't changed
     # but the data did change
     products = Product.all()
     self.assertEqual(len(products), 1)
     self.assertEqual(products[0].rating, 10)
Exemplo n.º 18
0
def product_add():
    """
    represents add product processing url
    """

    name = request.form.get("name")
    price = request.form.get("price")
    picture = request.files.get("picture")
    category = request.form.get("category")

    try:
        new_product = Product(name, price, picture)
        new_product.set_object_category(category)
        new_product.save()
    except Exception as Error:
        flash("error", Error.__str__())
    else:
        flash("success", "sukses tambah produk")

    return redirect(url_for("dashboard.product"))
Exemplo n.º 19
0
 def test_serialize_a_product(self):
     """ Test serialization of a Product """
     product = Product(1, "Couch", "White couch", "Furniture", 200, "Boxed",
                       50, " ", 8)
     data = product.serialize()
     self.assertNotEqual(data, None)
     self.assertIn('id', data)
     self.assertEqual(data['id'], 1)
     self.assertIn('name', data)
     self.assertEqual(data['name'], "Couch")
     self.assertIn('category', data)
     self.assertEqual(data['category'], "Furniture")
     self.assertIn('description', data)
     self.assertEqual(data['description'], "White couch")
     self.assertIn('price', data)
     self.assertEqual(data['price'], 200)
     self.assertIn('condition', data)
     self.assertEqual(data['condition'], "Boxed")
     self.assertIn('inventory', data)
     self.assertEqual(data['inventory'], 50)
     self.assertIn('rating', data)
     self.assertEqual(data['rating'], 8)
Exemplo n.º 20
0
 def test_deserialize_a_product(self):
     """ Test deserialization of a Product """
     data = {
         "id": 1,
         "name": "Couch",
         "description": "White couch",
         "category": "Furniture",
         "price": 200,
         "condition": "Boxed",
         "inventory": 50,
         "rating": 8,
         "review": " "
     }
     product = Product(1, "", "", "", 0, "", 0, "", 0)
     product.deserialize(data)
     self.assertIsNot(product, None)
     self.assertEqual(product.id, 1)
     self.assertEqual(product.name, "Couch")
     self.assertEqual(product.category, "Furniture")
     self.assertEqual(product.description, "White couch")
     self.assertEqual(product.price, 200)
     self.assertEqual(product.condition, "Boxed")
     self.assertEqual(product.inventory, 50)
     self.assertEqual(product.rating, 8)
Exemplo n.º 21
0
 def test_deserialize_bad_data(self):
     """ Test deserialization of bad data """
     data = "this is not a dictionary"
     product = Product(1, "", "", "", 0, "", 0, "", 0)
     self.assertRaises(ValidationError, product.deserialize, data)