def test_dict(mock_session): item = Item() item.name = 'x' item.catagory_id = 2 item.description = 'z' assert item.dict == { 'id': None, 'catagory_id': 2, 'name': 'x', 'description': 'z' }
def store_item(): """ Store the new requested item. """ # Get the category and check if it's exist or fail to 404 page if not category = Category.find_or_fail(request.form['category_id']) # Create new item item = Item() item.title = request.form['title'] item.description = request.form['description'] # Associate the new item with the category item.category().associate(category) # Associate the new item to the current user item.user().associate(current_user) # The below is for validate the unique slug, if it # was exists before, then the ORM will not fall to # the ModelNotFound exception and the code will # continue flashing the error message to the user. # Otherwise, if the slug is not exists, then the # request is completely valid and the ORM will # going to fall to the ModelNotFound exception # then save the new Item try: # Get the item and check if it's exist or fail to 404 page if not Item.where('slug', item.slug).first_or_fail() flash('The title seems similar to another' ' title, please choose another one.') return render_template('items/add.html', categories=Category.all(), current_category=category, item=item) except ModelNotFound: # Save the item item.save() return redirect( url_for('view_item', category_slug=category.slug, item_slug=item.slug))
def test_update_item_with_description_updates_description(mock_session): item = Item() item.description = 'x' item.update(description='y') assert item.description == 'y'