def test_ordered_query(): assert Short.query.count() == 0 Short.generate(target="one", delay=0) Short.generate(target="two", delay=0) Short.generate(target="drop", delay=1) Short.generate(target="nope", delay=2) assert Short.query.count() == 4 assert Short.ordered(None).count() == 4 query = Short.query.filter(Short.delay == 0) assert query.count() == 2 assert Short.ordered(None, query=query).count() == 2
def show(page=None, field=None, sort=None): term = request.args.get("q", None) form = ShortFindForm(term=term) if form.validate_on_submit(): target = form.action() if target: return redirect(target) query = ( Short.searched(term, field=field, rev=sort == "desc") if term is not None else Short.ordered(field=field, rev=sort == "desc") ) if not query: abort(404) return render_template( "plus/show.html", title="Show", form=form, elements=query.paginate( page=page, per_page=current_app.config["PAGINATION"] ), )
def test_ordered(): assert Short.query.count() == 0 now = datetime.utcnow() one = Short.generate( target="one", delay=2, created=now - timedelta(seconds=1), ) two = Short.generate( target="two", delay=1, created=now - timedelta(seconds=3), ) thr = Short.generate( target="thr", delay=3, created=now - timedelta(seconds=2), ) assert Short.query.count() == 3 assert Short.ordered("PRIME") is None assert Short.ordered("BANANA") is None assert Short.ordered(None).all() == [one, two, thr] # same as prime assert Short.ordered("prime").all() == [one, two, thr] assert Short.ordered("prime", rev=True).all() == [thr, two, one] assert Short.ordered("delay").all() == [two, one, thr] assert Short.ordered("delay", rev=True).all() == [thr, one, two] assert Short.ordered("target").all() == [one, thr, two] assert Short.ordered("target", rev=True).all() == [two, thr, one] assert Short.ordered("created").all() == [two, thr, one] assert Short.ordered("created", rev=True).all() == [one, thr, two] assert Short.ordered("active").all() == [one, two, thr] assert Short.ordered("active", rev=True).all() == [thr, two, one]