123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- from __future__ import unicode_literals
- from django.db import transaction
- from django.test import TestCase
- from django.utils import six
- from .models import Article, Publication
- class ManyToManyTests(TestCase):
- def setUp(self):
-
- self.p1 = Publication.objects.create(id=None, title='The Python Journal')
- self.p2 = Publication.objects.create(id=None, title='Science News')
- self.p3 = Publication.objects.create(id=None, title='Science Weekly')
- self.p4 = Publication.objects.create(title='Highlights for Children')
- self.a1 = Article.objects.create(id=None, headline='Django lets you build Web apps easily')
- self.a1.publications.add(self.p1)
- self.a2 = Article.objects.create(id=None, headline='NASA uses Python')
- self.a2.publications.add(self.p1, self.p2, self.p3, self.p4)
- self.a3 = Article.objects.create(headline='NASA finds intelligent life on Earth')
- self.a3.publications.add(self.p2)
- self.a4 = Article.objects.create(headline='Oxygen-free diet works wonders')
- self.a4.publications.add(self.p2)
- def test_add(self):
-
- a5 = Article(id=None, headline='Django lets you reate Web apps easily')
-
- self.assertRaises(ValueError, getattr, a5, 'publications')
-
- a5.save()
-
- a5.publications.add(self.p1)
- self.assertQuerysetEqual(a5.publications.all(),
- ['<Publication: The Python Journal>'])
-
- a6 = Article(id=None, headline='ESA uses Python')
- a6.save()
- a6.publications.add(self.p1, self.p2)
- a6.publications.add(self.p3)
-
- a6.publications.add(self.p3)
- self.assertQuerysetEqual(a6.publications.all(),
- [
- '<Publication: Science News>',
- '<Publication: Science Weekly>',
- '<Publication: The Python Journal>',
- ])
-
- with six.assertRaisesRegex(self, TypeError, "'Publication' instance expected, got <Article.*"):
- with transaction.atomic():
- a6.publications.add(a5)
-
- a6.publications.create(title='Highlights for Adults')
- self.assertQuerysetEqual(a6.publications.all(),
- [
- '<Publication: Highlights for Adults>',
- '<Publication: Science News>',
- '<Publication: Science Weekly>',
- '<Publication: The Python Journal>',
- ])
- def test_reverse_add(self):
-
- a5 = Article(headline='NASA finds intelligent life on Mars')
- a5.save()
- self.p2.article_set.add(a5)
- self.assertQuerysetEqual(self.p2.article_set.all(),
- [
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: NASA finds intelligent life on Mars>',
- '<Article: NASA uses Python>',
- '<Article: Oxygen-free diet works wonders>',
- ])
- self.assertQuerysetEqual(a5.publications.all(),
- ['<Publication: Science News>'])
-
- self.p2.article_set.create(headline='Carbon-free diet works wonders')
- self.assertQuerysetEqual(
- self.p2.article_set.all(),
- [
- '<Article: Carbon-free diet works wonders>',
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: NASA finds intelligent life on Mars>',
- '<Article: NASA uses Python>',
- '<Article: Oxygen-free diet works wonders>',
- ])
- a6 = self.p2.article_set.all()[3]
- self.assertQuerysetEqual(a6.publications.all(),
- [
- '<Publication: Highlights for Children>',
- '<Publication: Science News>',
- '<Publication: Science Weekly>',
- '<Publication: The Python Journal>',
- ])
- def test_related_sets(self):
-
- self.assertQuerysetEqual(self.a1.publications.all(),
- ['<Publication: The Python Journal>'])
- self.assertQuerysetEqual(self.a2.publications.all(),
- [
- '<Publication: Highlights for Children>',
- '<Publication: Science News>',
- '<Publication: Science Weekly>',
- '<Publication: The Python Journal>',
- ])
-
- self.assertQuerysetEqual(self.p2.article_set.all(),
- [
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: NASA uses Python>',
- '<Article: Oxygen-free diet works wonders>',
- ])
- self.assertQuerysetEqual(self.p1.article_set.all(),
- [
- '<Article: Django lets you build Web apps easily>',
- '<Article: NASA uses Python>',
- ])
- self.assertQuerysetEqual(Publication.objects.get(id=self.p4.id).article_set.all(),
- ['<Article: NASA uses Python>'])
- def test_selects(self):
-
- self.assertQuerysetEqual(
- Article.objects.filter(publications__id__exact=self.p1.id),
- [
- '<Article: Django lets you build Web apps easily>',
- '<Article: NASA uses Python>',
- ])
- self.assertQuerysetEqual(
- Article.objects.filter(publications__pk=self.p1.id),
- [
- '<Article: Django lets you build Web apps easily>',
- '<Article: NASA uses Python>',
- ])
- self.assertQuerysetEqual(
- Article.objects.filter(publications=self.p1.id),
- [
- '<Article: Django lets you build Web apps easily>',
- '<Article: NASA uses Python>',
- ])
- self.assertQuerysetEqual(
- Article.objects.filter(publications=self.p1),
- [
- '<Article: Django lets you build Web apps easily>',
- '<Article: NASA uses Python>',
- ])
- self.assertQuerysetEqual(
- Article.objects.filter(publications__title__startswith="Science"),
- [
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: NASA uses Python>',
- '<Article: NASA uses Python>',
- '<Article: Oxygen-free diet works wonders>',
- ])
- self.assertQuerysetEqual(
- Article.objects.filter(publications__title__startswith="Science").distinct(),
- [
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: NASA uses Python>',
- '<Article: Oxygen-free diet works wonders>',
- ])
-
- self.assertEqual(Article.objects.filter(publications__title__startswith="Science").count(), 4)
- self.assertEqual(Article.objects.filter(publications__title__startswith="Science").distinct().count(), 3)
- self.assertQuerysetEqual(
- Article.objects.filter(publications__in=[self.p1.id, self.p2.id]).distinct(),
- [
- '<Article: Django lets you build Web apps easily>',
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: NASA uses Python>',
- '<Article: Oxygen-free diet works wonders>',
- ])
- self.assertQuerysetEqual(
- Article.objects.filter(publications__in=[self.p1.id, self.p2]).distinct(),
- [
- '<Article: Django lets you build Web apps easily>',
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: NASA uses Python>',
- '<Article: Oxygen-free diet works wonders>',
- ])
- self.assertQuerysetEqual(
- Article.objects.filter(publications__in=[self.p1, self.p2]).distinct(),
- [
- '<Article: Django lets you build Web apps easily>',
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: NASA uses Python>',
- '<Article: Oxygen-free diet works wonders>',
- ])
-
-
- self.assertQuerysetEqual(Article.objects.exclude(publications=self.p2),
- ['<Article: Django lets you build Web apps easily>'])
- def test_reverse_selects(self):
-
-
- self.assertQuerysetEqual(Publication.objects.filter(id__exact=self.p1.id),
- ['<Publication: The Python Journal>'])
- self.assertQuerysetEqual(Publication.objects.filter(pk=self.p1.id),
- ['<Publication: The Python Journal>'])
- self.assertQuerysetEqual(
- Publication.objects.filter(article__headline__startswith="NASA"),
- [
- '<Publication: Highlights for Children>',
- '<Publication: Science News>',
- '<Publication: Science News>',
- '<Publication: Science Weekly>',
- '<Publication: The Python Journal>',
- ])
- self.assertQuerysetEqual(Publication.objects.filter(article__id__exact=self.a1.id),
- ['<Publication: The Python Journal>'])
- self.assertQuerysetEqual(Publication.objects.filter(article__pk=self.a1.id),
- ['<Publication: The Python Journal>'])
- self.assertQuerysetEqual(Publication.objects.filter(article=self.a1.id),
- ['<Publication: The Python Journal>'])
- self.assertQuerysetEqual(Publication.objects.filter(article=self.a1),
- ['<Publication: The Python Journal>'])
- self.assertQuerysetEqual(
- Publication.objects.filter(article__in=[self.a1.id, self.a2.id]).distinct(),
- [
- '<Publication: Highlights for Children>',
- '<Publication: Science News>',
- '<Publication: Science Weekly>',
- '<Publication: The Python Journal>',
- ])
- self.assertQuerysetEqual(
- Publication.objects.filter(article__in=[self.a1.id, self.a2]).distinct(),
- [
- '<Publication: Highlights for Children>',
- '<Publication: Science News>',
- '<Publication: Science Weekly>',
- '<Publication: The Python Journal>',
- ])
- self.assertQuerysetEqual(
- Publication.objects.filter(article__in=[self.a1, self.a2]).distinct(),
- [
- '<Publication: Highlights for Children>',
- '<Publication: Science News>',
- '<Publication: Science Weekly>',
- '<Publication: The Python Journal>',
- ])
- def test_delete(self):
-
- self.p1.delete()
- self.assertQuerysetEqual(Publication.objects.all(),
- [
- '<Publication: Highlights for Children>',
- '<Publication: Science News>',
- '<Publication: Science Weekly>',
- ])
- self.assertQuerysetEqual(self.a1.publications.all(), [])
-
- self.a2.delete()
- self.assertQuerysetEqual(Article.objects.all(),
- [
- '<Article: Django lets you build Web apps easily>',
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: Oxygen-free diet works wonders>',
- ])
- self.assertQuerysetEqual(self.p2.article_set.all(),
- [
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: Oxygen-free diet works wonders>',
- ])
- def test_bulk_delete(self):
-
- Publication.objects.filter(title__startswith='Science').delete()
- self.assertQuerysetEqual(Publication.objects.all(),
- [
- '<Publication: Highlights for Children>',
- '<Publication: The Python Journal>',
- ])
- self.assertQuerysetEqual(Article.objects.all(),
- [
- '<Article: Django lets you build Web apps easily>',
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: NASA uses Python>',
- '<Article: Oxygen-free diet works wonders>',
- ])
- self.assertQuerysetEqual(self.a2.publications.all(),
- [
- '<Publication: Highlights for Children>',
- '<Publication: The Python Journal>',
- ])
-
- q = Article.objects.filter(headline__startswith='Django')
- self.assertQuerysetEqual(q, ['<Article: Django lets you build Web apps easily>'])
- q.delete()
-
-
- self.assertQuerysetEqual(q, [])
- self.assertQuerysetEqual(self.p1.article_set.all(),
- ['<Article: NASA uses Python>'])
- def test_remove(self):
-
- self.assertQuerysetEqual(self.p2.article_set.all(),
- [
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: NASA uses Python>',
- '<Article: Oxygen-free diet works wonders>',
- ])
- self.a4.publications.remove(self.p2)
- self.assertQuerysetEqual(self.p2.article_set.all(),
- [
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: NASA uses Python>',
- ])
- self.assertQuerysetEqual(self.a4.publications.all(), [])
-
- self.p2.article_set.remove(self.a3)
- self.assertQuerysetEqual(self.p2.article_set.all(),
- [
- '<Article: NASA uses Python>',
- ])
- self.assertQuerysetEqual(self.a3.publications.all(), [])
- def test_set(self):
- self.p2.article_set.set([self.a4, self.a3])
- self.assertQuerysetEqual(self.p2.article_set.all(),
- [
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: Oxygen-free diet works wonders>',
- ])
- self.assertQuerysetEqual(self.a4.publications.all(),
- ['<Publication: Science News>'])
- self.a4.publications.set([self.p3.id])
- self.assertQuerysetEqual(self.p2.article_set.all(),
- ['<Article: NASA finds intelligent life on Earth>'])
- self.assertQuerysetEqual(self.a4.publications.all(),
- ['<Publication: Science Weekly>'])
- self.p2.article_set.set([])
- self.assertQuerysetEqual(self.p2.article_set.all(), [])
- self.a4.publications.set([])
- self.assertQuerysetEqual(self.a4.publications.all(), [])
- self.p2.article_set.set([self.a4, self.a3], clear=True)
- self.assertQuerysetEqual(self.p2.article_set.all(),
- [
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: Oxygen-free diet works wonders>',
- ])
- self.assertQuerysetEqual(self.a4.publications.all(),
- ['<Publication: Science News>'])
- self.a4.publications.set([self.p3.id], clear=True)
- self.assertQuerysetEqual(self.p2.article_set.all(),
- ['<Article: NASA finds intelligent life on Earth>'])
- self.assertQuerysetEqual(self.a4.publications.all(),
- ['<Publication: Science Weekly>'])
- self.p2.article_set.set([], clear=True)
- self.assertQuerysetEqual(self.p2.article_set.all(), [])
- self.a4.publications.set([], clear=True)
- self.assertQuerysetEqual(self.a4.publications.all(), [])
- def test_assign(self):
-
- self.p2.article_set = [self.a4, self.a3]
- self.assertQuerysetEqual(self.p2.article_set.all(),
- [
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: Oxygen-free diet works wonders>',
- ])
- self.assertQuerysetEqual(self.a4.publications.all(),
- ['<Publication: Science News>'])
- self.a4.publications = [self.p3.id]
- self.assertQuerysetEqual(self.p2.article_set.all(),
- ['<Article: NASA finds intelligent life on Earth>'])
- self.assertQuerysetEqual(self.a4.publications.all(),
- ['<Publication: Science Weekly>'])
-
- self.p2.article_set = []
- self.assertQuerysetEqual(self.p2.article_set.all(), [])
- self.a4.publications = []
- self.assertQuerysetEqual(self.a4.publications.all(), [])
- def test_assign_ids(self):
-
- self.p2.article_set = [self.a4.id, self.a3.id]
- self.assertQuerysetEqual(self.p2.article_set.all(),
- [
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: Oxygen-free diet works wonders>',
- ])
- self.assertQuerysetEqual(self.a4.publications.all(),
- ['<Publication: Science News>'])
- self.a4.publications = [self.p3.id]
- self.assertQuerysetEqual(self.p2.article_set.all(),
- ['<Article: NASA finds intelligent life on Earth>'])
- self.assertQuerysetEqual(self.a4.publications.all(),
- ['<Publication: Science Weekly>'])
- def test_forward_assign_with_queryset(self):
-
-
-
- self.a1.publications = [self.p1, self.p2]
- qs = self.a1.publications.filter(title='The Python Journal')
- self.a1.publications = qs
- self.assertEqual(1, self.a1.publications.count())
- self.assertEqual(1, qs.count())
- def test_reverse_assign_with_queryset(self):
-
-
-
- self.p1.article_set = [self.a1, self.a2]
- qs = self.p1.article_set.filter(headline='Django lets you build Web apps easily')
- self.p1.article_set = qs
- self.assertEqual(1, self.p1.article_set.count())
- self.assertEqual(1, qs.count())
- def test_clear(self):
-
- self.p2.article_set.clear()
- self.assertQuerysetEqual(self.p2.article_set.all(), [])
- self.assertQuerysetEqual(self.a4.publications.all(), [])
-
- self.p2.article_set.add(self.a3, self.a4)
- self.assertQuerysetEqual(self.p2.article_set.all(),
- [
- '<Article: NASA finds intelligent life on Earth>',
- '<Article: Oxygen-free diet works wonders>',
- ])
- self.assertQuerysetEqual(self.a4.publications.all(),
- [
- '<Publication: Science News>',
- ])
- self.a4.publications.clear()
- self.assertQuerysetEqual(self.a4.publications.all(), [])
- self.assertQuerysetEqual(self.p2.article_set.all(),
- ['<Article: NASA finds intelligent life on Earth>'])
|