summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2005-07-29 16:20:39 +0000
committerAdrian Holovaty <adrian@holovaty.com>2005-07-29 16:20:39 +0000
commit85d2b12bdff63a5c4b269b290904dd499f92a40f (patch)
tree808de93e0341665d05822d3e99561203a11b74f7
parentd4a9a4f83a4e4d94b7357666ee99eec0d3ef5486 (diff)
Added two more model test modules -- get_latest and lookup
git-svn-id: http://code.djangoproject.com/svn/django/trunk@339 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--tests/testapp/models/__init__.py2
-rw-r--r--tests/testapp/models/get_latest.py44
-rw-r--r--tests/testapp/models/lookup.py67
3 files changed, 112 insertions, 1 deletions
diff --git a/tests/testapp/models/__init__.py b/tests/testapp/models/__init__.py
index 640028864c..08a7671fb3 100644
--- a/tests/testapp/models/__init__.py
+++ b/tests/testapp/models/__init__.py
@@ -1 +1 @@
-__all__ = ['basic', 'repr', 'custom_methods', 'many_to_one', 'many_to_many', 'ordering']
+__all__ = ['basic', 'repr', 'custom_methods', 'many_to_one', 'many_to_many', 'ordering', 'lookup', 'get_latest']
diff --git a/tests/testapp/models/get_latest.py b/tests/testapp/models/get_latest.py
new file mode 100644
index 0000000000..2bd5a6660b
--- /dev/null
+++ b/tests/testapp/models/get_latest.py
@@ -0,0 +1,44 @@
+"""
+8. get_latest_by
+
+Models can have a ``get_latest_by`` attribute, which should be set to the name
+of a DateField or DateTimeField. If ``get_latest_by`` exists, the model's
+module will get a ``get_latest()`` function, which will return the latest
+object in the database according to that field. "Latest" means "having the
+date farthest into the future."
+"""
+
+from django.core import meta
+
+class Article(meta.Model):
+ fields = (
+ meta.CharField('headline', maxlength=100),
+ meta.DateTimeField('pub_date'),
+ )
+ get_latest_by = 'pub_date'
+
+ def __repr__(self):
+ return self.headline
+
+API_TESTS = """
+# Because no Articles exist yet, get_latest() raises ArticleDoesNotExist.
+>>> articles.get_latest()
+Traceback (most recent call last):
+ ...
+ArticleDoesNotExist: Article does not exist for {'order_by': ('-pub_date',), 'limit': 1}
+
+# Create a couple of Articles.
+>>> from datetime import datetime
+>>> a1 = articles.Article(id=None, headline='Article 1', pub_date=datetime(2005, 7, 26))
+>>> a1.save()
+>>> a2 = articles.Article(id=None, headline='Article 2', pub_date=datetime(2005, 7, 27))
+>>> a2.save()
+>>> a3 = articles.Article(id=None, headline='Article 3', pub_date=datetime(2005, 7, 27))
+>>> a3.save()
+>>> a4 = articles.Article(id=None, headline='Article 4', pub_date=datetime(2005, 7, 28))
+>>> a4.save()
+
+# Get the latest Article.
+>>> articles.get_latest()
+Article 4
+"""
diff --git a/tests/testapp/models/lookup.py b/tests/testapp/models/lookup.py
new file mode 100644
index 0000000000..8eb0032036
--- /dev/null
+++ b/tests/testapp/models/lookup.py
@@ -0,0 +1,67 @@
+"""
+7. The lookup API
+
+"""
+
+from django.core import meta
+
+class Article(meta.Model):
+ fields = (
+ meta.CharField('headline', maxlength=100),
+ meta.DateTimeField('pub_date'),
+ )
+ ordering = ('-pub_date', 'headline')
+
+ def __repr__(self):
+ return self.headline
+
+API_TESTS = """
+# Create a couple of Articles.
+>>> from datetime import datetime
+>>> a1 = articles.Article(id=None, headline='Article 1', pub_date=datetime(2005, 7, 26))
+>>> a1.save()
+>>> a2 = articles.Article(id=None, headline='Article 2', pub_date=datetime(2005, 7, 27))
+>>> a2.save()
+>>> a3 = articles.Article(id=None, headline='Article 3', pub_date=datetime(2005, 7, 27))
+>>> a3.save()
+>>> a4 = articles.Article(id=None, headline='Article 4', pub_date=datetime(2005, 7, 28))
+>>> a4.save()
+
+# get_iterator() is just like get_list(), but it's a generator.
+>>> for a in articles.get_iterator():
+... print a.headline
+Article 4
+Article 2
+Article 3
+Article 1
+
+# get_iterator() takes the same lookup arguments as get_list().
+>>> for a in articles.get_iterator(headline__endswith='4'):
+... print a.headline
+Article 4
+
+# get_count() returns the number of objects matching search criteria.
+>>> articles.get_count()
+4L
+>>> articles.get_count(pub_date__exact=datetime(2005, 7, 27))
+2L
+>>> articles.get_count(headline__startswith='Blah blah')
+0L
+
+# get_in_bulk() takes a list of IDs and returns a dictionary mapping IDs
+# to objects.
+>>> articles.get_in_bulk([1, 2])
+{1: Article 1, 2: Article 2}
+>>> articles.get_in_bulk([3])
+{3: Article 3}
+>>> articles.get_in_bulk([1000])
+{}
+
+# Every DateField and DateTimeField creates get_next_by_FOO() and
+# get_previous_by_FOO() methods.
+>>> a3.get_next_by_pub_date()
+Article 4
+>>> a2.get_previous_by_pub_date()
+Article 1
+
+"""