def test_get_close_db(app): with app.app_context(): db = get_db() assert db is get_db() with pytest.raises(sqlite3.ProgrammingError) as e: db.execute('SELECT 1') assert 'closed' in str(e.value)
def app(): db_fd, db_path = tempfile.mkstemp() app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
def edit(id): if request.method == 'POST': maincategory = request.form['maincategory'] subcategory = request.form['subcategory'] urlname = request.form['urlname'] urllocation = request.form['urllocation'] error = None if not maincategory: error = 'Main Category is required.' elif not subcategory: error = 'Sub Category is required.' elif not urlname: error = 'URL Name is required.' elif not urllocation: error = 'URL Location is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'UPDATE links SET maincategory = ? , subcategory = ?, urlname = ?, urllocation = ?' ' WHERE id = ?', (maincategory, subcategory, urlname, urllocation, id)) db.commit() return redirect(url_for('navigation.index')) return render_template('index.html')
def add(): if request.method == 'POST': maincategory = request.form['maincategory'] subcategory = request.form['subcategory'] urlname = request.form['urlname'] urllocation = request.form['urllocation'] error = None if not maincategory: error = 'Main Category is required.' elif not subcategory: error = 'Sub Category is required.' elif not urlname: error = 'URL Name is required.' elif not urllocation: error = 'URL Location is required.' if error is not None: flash(error) else: db = get_db() db.execute( 'INSERT INTO links (maincategory, subcategory, urlname, urllocation)' ' VALUES (?, ?, ?, ?)', (maincategory, subcategory, urlname, urllocation)) db.commit() return redirect(url_for('navigation.index')) return render_template('index.html')
def test_delete(client, app): response = client.post('/1/delete') assert response.headers['Location'] == 'http://localhost/' with app.app_context(): db = get_db() link = db.execute('SELECT * FROM links WHERE id = 1').fetchone() assert link is None
def test_edit(client, app): response = client.post('/1') client.post('/1', data={ 'maincategory': 'test', 'subcategory': 'ebook', 'urlname': 'big1000', 'urllocation': 'ebook.big1000.com' }) with app.app_context(): db = get_db() link = db.execute('SELECT * FROM links WHERE id = 1').fetchone() assert link['maincategory'] == 'test'
def test_add(client, app): response = client.post('/') client.post('/', data={ 'maincategory': 'IBM', 'subcategory': 'Power', 'urlname': 'E980', 'urllocation': 'e980.com' }) with app.app_context(): db = get_db() link = db.execute('SELECT * FROM links WHERE id = 2').fetchone() assert link['maincategory'] == 'IBM'
def index(): db = get_db() links = db.execute('SELECT * FROM links').fetchall() maincg_list = db.execute( 'SELECT distinct maincategory FROM links').fetchall() links_list = [] links.sort(key=itemgetter('maincategory')) for i, j in groupby(links, key=itemgetter('maincategory')): j = list(j) j.sort(key=itemgetter('subcategory')) sub_list = [] for x, y in groupby(j, key=itemgetter('subcategory')): y = list(y) subdict = {'sub_cg': x, 'link': y} sub_list.append(subdict) maindict = {'main_cg': i, 'sub_cg_list': sub_list} links_list.append(maindict) return render_template('index.html', links=links_list, maincg_list=maincg_list)
def delete(id): db = get_db() db.execute('DELETE FROM links WHERE id = ?', (id, )) db.commit() return redirect(url_for('navigation.index'))