Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added initial testing framework

  • Loading branch information...
commit 89eece26a1e3e0d43893b840e11d3fbcd5750a96 1 parent 851ae91
@jordanm jordanm authored
View
23 django_hstore/query.py
@@ -1,6 +1,7 @@
from django.db.models.manager import Manager
from django.db.models.query import QuerySet
+from django.db.models.sql.constants import SINGLE
from django.db.models.sql.datastructures import EmptyResultSet
from django.db.models.sql.query import Query
from django.db.models.sql.where import EmptyShortCircuit, WhereNode
@@ -39,6 +40,24 @@ def __init__(self, model):
class HStoreQuerySet(QuerySet):
def __init__(self, model=None, query=None, using=None):
- query = query or HStoreQuery(model)
- super(HStoreQuerySet, self).__init__(model=model, query=query, using=using)
+ super(HStoreQuerySet, self).__init__(model=model, query=(query or HStoreQuery(model)), using=using)
+
+ def peek(self, attr, key):
+ query = self.query.clone()
+ query.add_extra({'_': "%s -> %%s" % attr}, [key], None, None, None, None)
+ query.default_cols = False
+ query.clear_select_fields()
+ result = query.get_compiler(self.db).execute_sql(SINGLE)
+ return (result[0] if result else None)
+
+ def pull(self, attr, keys):
+ """Pulls the specified keys from the specified hstore attribute."""
+
+ query = self.query.clone()
+ clause = 'slice(%s, ARRAY[%s])' % (attr, ','.join("'%s'" % key for key in keys))
+ query.add_extra({'_v': clause}, None, None, None, None, None)
+ query.default_cols = False
+ query.clear_select_fields()
+ result = query.get_compiler(self.db).execute_sql(SINGLE)
+ return (result[0] if result else None)
View
12 runtests
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+DB_NAME=${DB_NAME:-django_hstore_test}
+DB_USER=${DB_USER:-jrm}
+HSTORE_SQL=${HSTORE_SQL:-/usr/share/postgresql/contrib/hstore.sql}
+
+dropdb -U "${DB_USER}" "${DB_NAME}"
+createdb -U "${DB_USER}" "${DB_NAME}"
+psql -U "${DB_USER}" "${DB_NAME}" -f "${HSTORE_SQL}" -q
+
+DB_NAME=$DB_NAME DB_USER=$DB_USER nosetests "$@"
+
View
19 tests/__init__.py
@@ -0,0 +1,19 @@
+
+from os import environ
+
+from django.conf import settings
+from django.core.management import call_command
+
+settings.configure(
+ DATABASES = {
+ 'default': {
+ 'ENGINE': 'django_hstore.postgresql_psycopg2',
+ 'NAME': environ['DB_NAME'],
+ 'USER': environ['DB_USER'],
+ }
+ },
+ INSTALLED_APPS = ['tests.app']
+)
+
+call_command('syncdb')
+
View
0  tests/app/__init__.py
No changes.
View
19 tests/app/models.py
@@ -0,0 +1,19 @@
+
+from django.db import models
+from django_hstore import hstore
+
+class Ref(models.Model):
+ name = models.CharField(max_length=32)
+
+ def __unicode__(self):
+ return self.name
+
+class Bag(models.Model):
+ name = models.CharField(max_length=32)
+ data = hstore.DictionaryField()
+ refs = hstore.ReferencesField()
+ objects = hstore.Manager()
+
+ def __unicode__(self):
+ return self.name
+
View
16 tests/tests.py
@@ -0,0 +1,16 @@
+
+import unittest
+from .app.models import Bag, Ref
+
+class TestCase(unittest.TestCase):
+ def setUp(self):
+ Bag.objects.all().delete()
+ Ref.objects.all().delete()
+
+class TestDictionaryField(TestCase):
+ def test_null_instantiation(self):
+ bag = Bag.objects.create(name='bag')
+ self.assertTrue(isinstance(bag.data, dict))
+ self.assertEquals(bag.data, {})
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.