forked from zacharyvoase/django-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for the basic Statement API zacharyvoase#4
- Loading branch information
Scott Walton
committed
May 8, 2013
1 parent
89d4810
commit 47639c1
Showing
6 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
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
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 |
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
Empty file.
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
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)' |
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
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) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Create your views here. |