Skip to content

Instantly share code, notes, and snippets.

def get_count(q):
count_q = q.statement.with_only_columns([func.count()]).order_by(None)
count = q.session.execute(count_q).scalar()
return count
q = session.query(TestModel).filter(...).order_by(...)
# Slow: SELECT COUNT(*) FROM (SELECT ... FROM TestModel WHERE ...) ...
print q.count()
@rob-b
rob-b / doc.rst
Last active August 29, 2015 14:14 — forked from hadrien/doc.rst

Pytest's session fixture scope is really handy, but there are some things which need to execute only once per actual test session. For example, one of my session fixtures sets up DB tables at the start of the test session and tears them down at the end. (I use PostgreSQL's nested transactions to keep from having to drop and recreate tables between each individual test.)

@pytest.fixture(scope='session')
def dbtables(request, sqlengine):
    Base.metadata.create_all(sqlengine)

    def teardown():
        Base.metadata.drop_all(sqlengine)
@rob-b
rob-b / authentication_helpers.rb
Created November 10, 2012 17:53 — forked from mattsnyder/authentication_helpers.rb
Easy faking of user authentication in RSpec when using Devise
module AuthenticationHelpers
# Example usage: login mock_user(Editor)
def login(user)
request.env['warden'] = mock(Warden,
:authenticate => user,
:authenticate! => user)
end
def mock_user(type, stubs={})
mock_model(type, stubs).as_null_object
@rob-b
rob-b / post-checkout
Last active October 8, 2015 03:48 — forked from codysoyland/post-checkout
place in .git/hooks/post-checkout to delete empty directories and pyc files
#! /bin/sh
echo "Purging pyc files and empty directories..."
# Start from the repository root.
cd ./$(git rev-parse --show-cdup)
# Delete .pyc files and empty directories.
find . -name "*.pyc" -delete > /dev/null 2>&1 &
find . -type d -empty -delete > /dev/null 2>&1 &
@rob-b
rob-b / gist:3249309
Created August 3, 2012 16:35 — forked from alfredo/gist:3205967
Mock a Django Model
# Based on https://github.com/dcramer/mock-django
import mock
from django.utils.unittest import TestCase
from project.app.models import Category
class ModelMock(mock.MagicMock):
@rob-b
rob-b / mkappenginevenv.sh
Created October 12, 2011 20:31 — forked from rmyers/mkappenginevenv.sh
Setup virtual env for appengine
#!/bin/bash
#
# Build a virtual environment suitable for running appengine.
# This uses virtualenvwrapper to make the virtual environment
# and modifies the postactivate/postdeactivate scripts to make
# the appengine code happy.
#
# Usage:
# $ curl -s https://raw.github.com/gist/1282442 | bash
#
@rob-b
rob-b / gist:1254947
Created September 30, 2011 20:53 — forked from munhitsu/gist:1034876
python 2.7 install on OSX (10.6.7) using brew (pip, easy_install, virtualenv, virtualenvwrapper)
#NOTE: .pydistutils.cfg seems to be not compatible with brew install python
#areas I needed to clean before installation
#clean up ~/Library/Python
#clean up .local
brew install python --framework
easy_install pip
pip install virtualenv
pip install virtualenvwrapper
mkdir $HOME/.virtualenvs