Skip to content

Commit

Permalink
Added tests for the basic Statement API zacharyvoase#4
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Walton committed May 8, 2013
1 parent 89d4810 commit 47639c1
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions django_postgres/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from django_postgres.view import View
from django_postgres.function import Statement
from django_postgres.bitstrings import BitStringField, BitStringExpression as B, Bits
14 changes: 13 additions & 1 deletion django_postgres/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,19 @@ def prepare(self, args=None):
"""Prepare the statement for filtering by executing it with the
arguments passed.
"""
statement_name = self.model._meta.db_table
statement_name, statement_args = self.model._meta.db_table.split(u'(')
statement_args = u'(' + statement_args

model_name = self.model.__name__
app_label = self.model._meta.app_label
module = self.model.__module__

execute_statement = u'EXECUTE {name}({args})'.format(
name=statement_name,
args=(', '.join(args) if args else ''))

model = _create_model(
model_name, execute_statement, None, app_label, module)

def get_queryset(self):
"""No methods that depend on this can be called until the statement has
Expand Down
Empty file.
15 changes: 15 additions & 0 deletions tests/test_project/functiontest/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.db import models
import django_postgres


class UserTypeCounter(django_postgres.Statement):
"""A simple class that tests the prepared statement. Can be called with
either True or False as arguments
"""
sql = """SELECT COUNT(*) AS my_count FROM auth_user WHERE
is_superuser = $1;"""

my_count = models.IntegerField()

class Meta:
db_table = 'user_type (BOOLEAN)'
35 changes: 35 additions & 0 deletions tests/test_project/functiontest/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from django.contrib import auth
from django.core import exceptions
from django.test import TestCase

import models


class StatementTestCase(TestCase):

def test_get_counter(self):
"""Must run prepare on the manager to prepare the statement to be
executed.
"""
foo_user = auth.models.User.objects.create(
username='foo', is_superuser=True)
foo_user.set_password('blah')
foo_user.save()

foo_superuser = models.UserTypeCounter.objects.prepare(
True)

self.assertEqual(foo_superuser.get().my_count, 1)

def test_unprepared(self):
"""Cannot execute the statement unless you explicitly prepare it first
"""
foo_user = auth.models.User.objects.create(
username='foo', is_superuser=True)
foo_user.set_password('blah')
foo_user.save()

self.assertRaises(
exceptions.ObjectDoesNotExist,
models.UserTypeCounter.objects.filter,
pk=1)
1 change: 1 addition & 0 deletions tests/test_project/functiontest/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your views here.

0 comments on commit 47639c1

Please sign in to comment.