|
40 | 40 | Library,
|
41 | 41 | MultiOneToOne,
|
42 | 42 | Person,
|
| 43 | + Place, |
43 | 44 | Poll,
|
44 | 45 | PollInfo,
|
45 | 46 | PollWithExcludeFields,
|
| 47 | + PollWithExcludedFKField, |
46 | 48 | Province,
|
47 | 49 | Restaurant,
|
48 | 50 | SelfFK,
|
@@ -946,3 +948,51 @@ def test_custom_table_name_from_register(self):
|
946 | 948 | self.get_table_name(ContactRegister.history),
|
947 | 949 | 'contacts_register_history',
|
948 | 950 | )
|
| 951 | + |
| 952 | + |
| 953 | +class ExcludeFieldsTest(TestCase): |
| 954 | + def test_restore_pollwithexclude(self): |
| 955 | + poll = PollWithExcludeFields.objects.create(question="what's up?", |
| 956 | + pub_date=today) |
| 957 | + historical = poll.history.order_by('pk')[0] |
| 958 | + with self.assertRaises(AttributeError): |
| 959 | + historical.pub_date |
| 960 | + original = historical.instance |
| 961 | + self.assertEqual(original.pub_date, poll.pub_date) |
| 962 | + |
| 963 | + |
| 964 | +class ExcludeForeignKeyTest(TestCase): |
| 965 | + def setUp(self): |
| 966 | + self.poll = PollWithExcludedFKField.objects.create( |
| 967 | + question="Is it?", pub_date=today, |
| 968 | + place=Place.objects.create(name="Somewhere") |
| 969 | + ) |
| 970 | + |
| 971 | + def get_first_historical(self): |
| 972 | + """ |
| 973 | + Retrieve the idx'th HistoricalPoll, ordered by time. |
| 974 | + """ |
| 975 | + return self.poll.history.order_by('history_date')[0] |
| 976 | + |
| 977 | + def test_instance_fk_value(self): |
| 978 | + historical = self.get_first_historical() |
| 979 | + original = historical.instance |
| 980 | + self.assertEqual(original.place, self.poll.place) |
| 981 | + |
| 982 | + def test_history_lacks_fk(self): |
| 983 | + historical = self.get_first_historical() |
| 984 | + with self.assertRaises(AttributeError): |
| 985 | + historical.place |
| 986 | + |
| 987 | + def test_nb_queries(self): |
| 988 | + with self.assertNumQueries(2): |
| 989 | + historical = self.get_first_historical() |
| 990 | + historical.instance |
| 991 | + |
| 992 | + def test_changed_value_lost(self): |
| 993 | + new_place = Place.objects.create(name="More precise") |
| 994 | + self.poll.place = new_place |
| 995 | + self.poll.save() |
| 996 | + historical = self.get_first_historical() |
| 997 | + instance = historical.instance |
| 998 | + self.assertEqual(instance.place, new_place) |
0 commit comments