Here is a good example: http://stackoverflow.com/questions/5761617/pyramid-authorization-for-stored-items/5761901#5761901
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 & |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |