diff options
| author | Jason Pellerin <jpellerin@gmail.com> | 2006-07-03 14:23:39 +0000 |
|---|---|---|
| committer | Jason Pellerin <jpellerin@gmail.com> | 2006-07-03 14:23:39 +0000 |
| commit | 1c6199dc8778bc35e55d9c081ca4110448b18f0d (patch) | |
| tree | 6cda46d196c1eaa144ba08288ffe6e77a6a505dd /tests | |
| parent | 4190c9e16f59a944625829015b53bf023ee44520 (diff) | |
[multi-db] Merge trunk to [3257]
git-svn-id: http://code.djangoproject.com/svn/django/branches/multiple-db-support@3258 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/modeltests/many_to_many/models.py | 23 | ||||
| -rw-r--r-- | tests/modeltests/many_to_one/models.py | 36 | ||||
| -rw-r--r-- | tests/modeltests/one_to_one/models.py | 24 | ||||
| -rw-r--r-- | tests/modeltests/serializers/models.py | 27 |
4 files changed, 105 insertions, 5 deletions
diff --git a/tests/modeltests/many_to_many/models.py b/tests/modeltests/many_to_many/models.py index e80afece46..0e989a0fbe 100644 --- a/tests/modeltests/many_to_many/models.py +++ b/tests/modeltests/many_to_many/models.py @@ -75,6 +75,10 @@ API_TESTS = """ [<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>] >>> Article.objects.filter(publications__pk=1) [<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>] +>>> Article.objects.filter(publications=1) +[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>] +>>> Article.objects.filter(publications=p1) +[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>] >>> Article.objects.filter(publications__title__startswith="Science") [<Article: NASA uses Python>, <Article: NASA uses Python>] @@ -89,6 +93,13 @@ API_TESTS = """ >>> Article.objects.filter(publications__title__startswith="Science").distinct().count() 1 +>>> Article.objects.filter(publications__in=[1,2]).distinct() +[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>] +>>> Article.objects.filter(publications__in=[1,p2]).distinct() +[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>] +>>> Article.objects.filter(publications__in=[p1,p2]).distinct() +[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>] + # Reverse m2m queries are supported (i.e., starting at the table that doesn't # have a ManyToManyField). >>> Publication.objects.filter(id__exact=1) @@ -101,9 +112,19 @@ API_TESTS = """ >>> Publication.objects.filter(article__id__exact=1) [<Publication: The Python Journal>] - >>> Publication.objects.filter(article__pk=1) [<Publication: The Python Journal>] +>>> Publication.objects.filter(article=1) +[<Publication: The Python Journal>] +>>> Publication.objects.filter(article=a1) +[<Publication: The Python Journal>] + +>>> Publication.objects.filter(article__in=[1,2]).distinct() +[<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>] +>>> Publication.objects.filter(article__in=[1,a2]).distinct() +[<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>] +>>> Publication.objects.filter(article__in=[a1,a2]).distinct() +[<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>] # If we delete a Publication, its Articles won't be able to access it. >>> p1.delete() diff --git a/tests/modeltests/many_to_one/models.py b/tests/modeltests/many_to_one/models.py index a830ffbdc2..d202975128 100644 --- a/tests/modeltests/many_to_one/models.py +++ b/tests/modeltests/many_to_one/models.py @@ -136,6 +136,10 @@ False >>> Article.objects.filter(reporter__first_name__exact='John') [<Article: John's second story>, <Article: This is a test>] +# Check that implied __exact also works +>>> Article.objects.filter(reporter__first_name='John') +[<Article: John's second story>, <Article: This is a test>] + # Query twice over the related field. >>> Article.objects.filter(reporter__first_name__exact='John', reporter__last_name__exact='Smith') [<Article: John's second story>, <Article: This is a test>] @@ -151,10 +155,20 @@ False [<Article: John's second story>, <Article: This is a test>] # Find all Articles for the Reporter whose ID is 1. +# Use direct ID check, pk check, and object comparison >>> Article.objects.filter(reporter__id__exact=1) [<Article: John's second story>, <Article: This is a test>] >>> Article.objects.filter(reporter__pk=1) [<Article: John's second story>, <Article: This is a test>] +>>> Article.objects.filter(reporter=1) +[<Article: John's second story>, <Article: This is a test>] +>>> Article.objects.filter(reporter=r) +[<Article: John's second story>, <Article: This is a test>] + +>>> Article.objects.filter(reporter__in=[1,2]).distinct() +[<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>] +>>> Article.objects.filter(reporter__in=[r,r2]).distinct() +[<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>] # You need two underscores between "reporter" and "id" -- not one. >>> Article.objects.filter(reporter_id__exact=1) @@ -168,10 +182,6 @@ Traceback (most recent call last): ... TypeError: Cannot resolve keyword 'reporter_id' into field -# "pk" shortcut syntax works in a related context, too. ->>> Article.objects.filter(reporter__pk=1) -[<Article: John's second story>, <Article: This is a test>] - # You can also instantiate an Article by passing # the Reporter's ID instead of a Reporter object. >>> a3 = Article(id=None, headline="This is a test", pub_date=datetime(2005, 7, 27), reporter_id=r.id) @@ -200,6 +210,18 @@ TypeError: Cannot resolve keyword 'reporter_id' into field [<Reporter: John Smith>] >>> Reporter.objects.filter(article__pk=1) [<Reporter: John Smith>] +>>> Reporter.objects.filter(article=1) +[<Reporter: John Smith>] +>>> Reporter.objects.filter(article=a) +[<Reporter: John Smith>] + +>>> Reporter.objects.filter(article__in=[1,4]).distinct() +[<Reporter: John Smith>] +>>> Reporter.objects.filter(article__in=[1,a3]).distinct() +[<Reporter: John Smith>] +>>> Reporter.objects.filter(article__in=[a,a3]).distinct() +[<Reporter: John Smith>] + >>> Reporter.objects.filter(article__headline__startswith='This') [<Reporter: John Smith>, <Reporter: John Smith>, <Reporter: John Smith>] >>> Reporter.objects.filter(article__headline__startswith='This').distinct() @@ -216,6 +238,12 @@ TypeError: Cannot resolve keyword 'reporter_id' into field [<Reporter: John Smith>, <Reporter: John Smith>, <Reporter: John Smith>, <Reporter: John Smith>] >>> Reporter.objects.filter(article__reporter__first_name__startswith='John').distinct() [<Reporter: John Smith>] +>>> Reporter.objects.filter(article__reporter__exact=r).distinct() +[<Reporter: John Smith>] + +# Check that implied __exact also works +>>> Reporter.objects.filter(article__reporter=r).distinct() +[<Reporter: John Smith>] # If you delete a reporter, his articles will be deleted. >>> Article.objects.all() diff --git a/tests/modeltests/one_to_one/models.py b/tests/modeltests/one_to_one/models.py index 27c16b5a34..f95556c08d 100644 --- a/tests/modeltests/one_to_one/models.py +++ b/tests/modeltests/one_to_one/models.py @@ -94,6 +94,12 @@ DoesNotExist: Restaurant matching query does not exist. <Restaurant: Demon Dogs the restaurant> >>> Restaurant.objects.get(place__exact=1) <Restaurant: Demon Dogs the restaurant> +>>> Restaurant.objects.get(place__exact=p1) +<Restaurant: Demon Dogs the restaurant> +>>> Restaurant.objects.get(place=1) +<Restaurant: Demon Dogs the restaurant> +>>> Restaurant.objects.get(place=p1) +<Restaurant: Demon Dogs the restaurant> >>> Restaurant.objects.get(place__pk=1) <Restaurant: Demon Dogs the restaurant> >>> Restaurant.objects.get(place__name__startswith="Demon") @@ -105,8 +111,18 @@ DoesNotExist: Restaurant matching query does not exist. <Place: Demon Dogs the place> >>> Place.objects.get(restaurant__place__exact=1) <Place: Demon Dogs the place> +>>> Place.objects.get(restaurant__place__exact=p1) +<Place: Demon Dogs the place> >>> Place.objects.get(restaurant__pk=1) <Place: Demon Dogs the place> +>>> Place.objects.get(restaurant=1) +<Place: Demon Dogs the place> +>>> Place.objects.get(restaurant=r) +<Place: Demon Dogs the place> +>>> Place.objects.get(restaurant__exact=1) +<Place: Demon Dogs the place> +>>> Place.objects.get(restaurant__exact=r) +<Place: Demon Dogs the place> # Add a Waiter to the Restaurant. >>> w = r.waiter_set.create(name='Joe') @@ -115,14 +131,22 @@ DoesNotExist: Restaurant matching query does not exist. <Waiter: Joe the waiter at Demon Dogs the restaurant> # Query the waiters +>>> Waiter.objects.filter(restaurant__place__pk=1) +[<Waiter: Joe the waiter at Demon Dogs the restaurant>] >>> Waiter.objects.filter(restaurant__place__exact=1) [<Waiter: Joe the waiter at Demon Dogs the restaurant>] +>>> Waiter.objects.filter(restaurant__place__exact=p1) +[<Waiter: Joe the waiter at Demon Dogs the restaurant>] >>> Waiter.objects.filter(restaurant__pk=1) [<Waiter: Joe the waiter at Demon Dogs the restaurant>] >>> Waiter.objects.filter(id__exact=1) [<Waiter: Joe the waiter at Demon Dogs the restaurant>] >>> Waiter.objects.filter(pk=1) [<Waiter: Joe the waiter at Demon Dogs the restaurant>] +>>> Waiter.objects.filter(restaurant=1) +[<Waiter: Joe the waiter at Demon Dogs the restaurant>] +>>> Waiter.objects.filter(restaurant=r) +[<Waiter: Joe the waiter at Demon Dogs the restaurant>] # Delete the restaurant; the waiter should also be removed >>> r = Restaurant.objects.get(pk=1) diff --git a/tests/modeltests/serializers/models.py b/tests/modeltests/serializers/models.py index 8c9483beba..ccf565c365 100644 --- a/tests/modeltests/serializers/models.py +++ b/tests/modeltests/serializers/models.py @@ -91,4 +91,31 @@ API_TESTS = """ >>> Article.objects.all() [<Article: Poker has no place on television>, <Article: Time to reform copyright>] +# Django also ships with a built-in JSON serializers +>>> json = serializers.serialize("json", Category.objects.filter(pk=2)) +>>> json +'[{"pk": "2", "model": "serializers.category", "fields": {"name": "Music"}}]' + +# You can easily create new objects by deserializing data with an empty PK +# (It's easier to demo this with JSON...) +>>> new_author_json = '[{"pk": null, "model": "serializers.author", "fields": {"name": "Bill"}}]' +>>> for obj in serializers.deserialize("json", new_author_json): +... obj.save() +>>> Author.objects.all() +[<Author: Bill>, <Author: Jane>, <Author: Joe>] + +# All the serializers work the same +>>> json = serializers.serialize("json", Article.objects.all()) +>>> for obj in serializers.deserialize("json", json): +... print obj +<DeserializedObject: Poker has no place on television> +<DeserializedObject: Time to reform copyright> + +>>> json = json.replace("Poker has no place on television", "Just kidding; I love TV poker") +>>> for obj in serializers.deserialize("json", json): +... obj.save() + +>>> Article.objects.all() +[<Article: Just kidding; I love TV poker>, <Article: Time to reform copyright>] + """ |
