def test_a_3_003_pc_delete(self): pc1 = ProductCategory.query().get(1) pc1.delete() pcs = ProductCategory.query().all() self.assertEqual(len(pcs), 15) print("Test a_3_3: pc delete")
def test_a_3_002_pc_update(self): pc1 = ProductCategory.query().get(1) #pc1.name = "modified" pc1.update(name="modified") pc_1 = ProductCategory.query().get(1) self.assertEqual(pc_1.name, "modified") print("Test a_3_2: pc update")
def test_a_3_001_pc_insert(self): db_drop_and_create_all() populate_tables() pc1 = ProductCategory(product_id=3, category_id=7) pc1.insert() pcs = ProductCategory.query().all() self.assertEqual(len(pcs), 16) print("Test a_3_1: pc insert")
def test_a_2_010_category_delete_relationships(self): products_before = len(Product.query().all()) categories_before = len(Category.query().all()) pc_before = len(ProductCategory.query().all()) # deleting the product category_to_del = Category.query().get(1) category_to_del.delete() self.assertEqual(len(Product.query().all()), products_before) self.assertEqual(len(Category.query().all()), categories_before - 1) self.assertEqual(len(ProductCategory.query().all()), pc_before - 3) print("Test a_2_10: category delete relationships")
def test_a_1_010_product_delete_relationships(self): #measuring lengths beofre actions #populate_tables() products_before = len(Product.query().all()) categories_before = len(Category.query().all()) pc_before = len(ProductCategory.query().all()) # deleting the product prod_to_del = Product.query().get(1) prod_to_del.delete() self.assertEqual(len(Product.query().all()), products_before - 1) self.assertEqual(len(Category.query().all()), categories_before) self.assertEqual(len(ProductCategory.query().all()), pc_before - 5) print("Test a_1_10: product delete relationships")
def test_a_3_005_pc_values(self): pc = ProductCategory.query().get(1) self.assertEqual(pc.id, 1) self.assertEqual(pc.product_id, 1) self.assertEqual(pc.category_id, 1) #self.assertEqual(pc.parent,None) #self.assertEqual(str(pc.children), # '[{"id": 2, "name": "Camera", "parent_id": 1}]') #print(pc.product.simple()) self.assertEqual( pc.product.simple(), { 'code': 789456611, 'id': 1, 'name': 'Cheese', 'price': 50.4, 'quantity': 7.89 }) #print(pc.category.simple()) self.assertEqual(pc.category.simple(), { 'id': 1, 'name': 'Electronics', 'parent_id': None }) print("Test a_3_5: pc values")
def change_category(self, request): ''' Change the category of a product ''' # retrieve the requested product product = Product.get_by_urlsafe_key(request.product_key) if not product: message = 'No product with the key "%s" exists.' % request.product_key raise endpoints.NotFoundException(message) # retrieve the user user = User.get_by_urlsafe_key(request.user_key) if not user: message = 'No user with the key "%s" exists.' % request.user_key raise endpoints.NotFoundException(message) # retrieve the category category = ProductCategory.get_by_urlsafe_key(request.category_key) if not category: message = 'No category with the key "%s" exists.' % request.category_key raise endpoints.NotFoundException(message) # remove the product from the old category old_category = product.get_category() old_mapping = ProductCategoryMapping.load(product, old_category) if old_mapping: old_mapping.key.delete() # set the product under the given category ProductCategoryMapping.load(product, category, user) # register the user as editor of the product ProductEditor.add_or_update(product, user) return message_types.VoidMessage()
def delete_products_categories(request, id): try: pc = ProductCategoryExists(id=id) except Exception as e: return jsoinify_error(e) pc = ProductCategory.query().get(pc.id).delete() return {"suucess": True, "result": "Product Category deleted successfully"}
def get_products_categories(request, id): try: pc_id = ProductCategoryExists(id=id) except Exception as e: return jsoinify_error(e) pc = ProductCategory.query().get(pc_id.id) return {"suucess": True, "product_category": pc.deep()}
def update_product(self, request): ''' Update a product ''' product = Product.get_by_urlsafe_key(request.productKey) if not product: message = 'No product with the key "%s" exists.' % request.productKey raise endpoints.NotFoundException(message) user = User.get_by_urlsafe_key(request.userKey) if not user: message = 'No user with the key "%s" exists.' % request.userKey raise endpoints.NotFoundException(message) category = None if request.category: category = ProductCategory.get_by_urlsafe_key(request.category) price = None if request.price: price = request.price currency = CurrencyUnit.get_by_urlsafe_key(price.currencyKey) location = None if price.location: location = PhysicalLocation.load(lat=price.location.lat, lon=price.location.lon) price = Price.create(user=user, value=price.value, currency=currency, location=location) manufacturer = None if request.manufacturer: manufacturer = Manufacturer.load(name=request.manufacturer, user=user) specs = [] if request.specs: for spec in request.specs: specification = Specification.get_by_urlsafe_key(spec.key) unit = Unit.get_by_urlsafe_key(spec.unit) specs.append([specification, spec.value, unit]) product.update(user=user, name=request.name, category=category, manufacturer=manufacturer, price=price, specs=specs) return message_types.VoidMessage()
def test_a_3_007_pc_delete_wrong(self): pcs = ProductCategory.query().all() old_records_number = len(pcs) try: #This code will not be executed #There is no pc with the number 0 pc1 = ProductCategory.query().get(0) pc1.delete() self.assertEqual(True, False) except Exception as e: self.assertEqual( str(e), "'NoneType' " + "object has no attribute 'delete'") #print(str(e)) pcs = ProductCategory.query().all() new_records_number = len(pcs) self.assertEqual(old_records_number, new_records_number) print("Test a_3_7: pc delete mistake, non-existent" + "pc id")
def category_id_validation(cls, value, values): validate_model_id_pydantic(Category, value) # Make sure that these values do not already exist if "product_id" in values: result = ProductCategory.query().filter( ProductCategory.category_id == value, ProductCategory.product_id == values["product_id"]).all() if len(result) != 0: raise ValueError("this product and this" + " category are already linked") return value
def get_list(self, request): ''' Returns a list of all the product categories supported by aWare ''' categories = ProductCategory.query().fetch() collection = [] for category in categories: msg = CategoryMessage() msg.key = category.key.urlsafe() msg.name = category.name collection.append(msg) return CategoriesCollection(items=collection)
def test_a_3_006_pc_insert_wrong(self): pcs = ProductCategory.query().all() old_records_number = len(pcs) try: #This code will not be executed #There are missing required parameters pc = ProductCategory() pc.insert() self.assertEqual(True, False) except Exception as e: self.assertEqual(str(e), "True != False") pcs = ProductCategory.query().all() new_records_number = len(pcs) self.assertEqual(old_records_number, new_records_number) print("Test a_3_6: pc insert with missing" + "required parameters")
def test_a_3_011_pc_deep(self): #measuring lengths beofre actions pc = ProductCategory.query().get(5) #print(pc.deep()) self.assertEqual( pc.deep(), { 'category': { 'id': 5, 'name': 'Cars', 'parent_id': None }, 'category_id': 5, 'id': 5, 'product': { 'code': 789456611, 'id': 1, 'name': 'Cheese', 'price': 50.4, 'quantity': 7.89 }, 'product_id': 1 }) print("Test a_3_11: pc deep")
def insert_data(table_name): if table_name == 'users': user = User.create(username='******', age=30, password='******', email='*****@*****.**') user = User.create(username='******', age=35, password='******', email='*****@*****.**') user = User.create(username='******', age=60, password='******', email='*****@*****.**') user = User.create(username='******', age=68, password='******', email='*****@*****.**') user = User.create(username='******', age=23, password='******', email='*****@*****.**') user.save() elif table_name == 'stores': store = Store.create(user_id=1, name='La esquina', address='Fuentes del Molino #10') store = Store.create(user_id=1, name='Don Simon', address='Fuentes del Molino #12') store = Store.create(user_id=1, name='Carmelita', address='Fuentes del Molino #13') store = Store.create(user_id=4, name='Wallis', address='Fuentes del Molino #14') store = Store.create(user_id=5, name='Don perse', address='Fuentes del Molino #16') store.save() elif table_name == 'products': product = Product.create(store_id=1, name='Pan', description='Pan Integral', price=5.5, stock=10) product = Product.create(store_id=1, name='Leche', description='Baja en grasas', price=15.5, stock=24) product = Product.create(store_id=1, name='Jamon', description='Pavo', price=45.5, stock=10) product = Product.create(store_id=2, name='Soda', description='Dieta', price=10.5, stock=10) product = Product.create(store_id=2, name='Fritura', description='Churros', price=9.5, stock=10) product = Product.create(store_id=2, name='Salsa', description='Habanero', price=11.5, stock=10) product.save() elif table_name == 'categories': category = Category.create(name='Liquidos', description='liquidos') category = Category.create(name='Embutidos', description='embutidos') category = Category.create(name='Snacks', description='snacks') category = Category.create(name='Aderezos', description='aderezos') category = Category.create(name='Carnes', description='carnes') category.save() elif table_name == 'product_categories': ProductCategory.create(category_id=1, product_id=2) ProductCategory.create(category_id=1, product_id=4) ProductCategory.create(category_id=2, product_id=3) ProductCategory.create(category_id=3, product_id=5) ProductCategory.create(category_id=4, product_id=6) ProductCategory.create(category_id=5, product_id=3)
def post_products_categories(request, product_category: ProductCategoryPost): product_category = ProductCategory(**product_category.dict()) product_category.insert() return {"suucess": True, "product_category": product_category.deep()}
def get_products_categories(request): all_pcs = ProductCategory.query().order_by("id").all() all_pcs = [category.simple() for category in all_pcs] return {"suucess": True, "categories": all_pcs}
def test_a_3_008_pc_simple(self): pc = ProductCategory.query().get(1).simple() #print(pc) self.assertEqual(pc, {'category_id': 1, 'id': 1, 'product_id': 1}) print("Test a_3_8: pc simple")
def test_a_3_004_populate(self): populate_tables() pcs = ProductCategory.query().all() self.assertEqual(len(pcs), 15) print("Test a_3_4: Populate Tables")
def test_a_3_009_pc_relationship_order(self): pc = ProductCategory.query().get(1) #pc.parent=None #pc = ProductCategory.query().get(4) print("Test a_3_9:pc relationship")