| | 1 | from django.test import TestCase |
|---|
| | 2 | |
|---|
| | 3 | from models import Author, Book, Authorship |
|---|
| | 4 | |
|---|
| | 5 | class ManyToManyWithCustomColumnTestCase(TestCase): |
|---|
| | 6 | def setUp(self): |
|---|
| | 7 | self.a = Author.objects.create(name="J.R.R. Tolkien") |
|---|
| | 8 | self.b1 = Book.objects.create(title="The Hobbit") |
|---|
| | 9 | self.b2 = Book.objects.create(title="The Fellowship of the Ring") |
|---|
| | 10 | self.auth1 = Authorship.objects.create(author=self.a, book=self.b1) |
|---|
| | 11 | self.auth2 = Authorship.objects.create(author=self.a, book=self.b2) |
|---|
| | 12 | |
|---|
| | 13 | def test_query(self): |
|---|
| | 14 | self.assertEqual(list(self.a.book_set.all()), [self.b1, self.b2]) |
|---|
| | 15 | self.assertEqual(list(self.b1.authors.all()), [self.a]) |
|---|
| | 16 | self.assertEqual(list(self.a.authorship_set.all()), [self.auth1, self.auth2]) |
|---|
| | 17 | self.assertEqual(list(self.b1.authorship_set.all()), [self.auth1]) |