|
| 1 | +import unittest |
1 | 2 | from datetime import datetime
|
2 | 3 | from unittest import skipUnless
|
3 | 4 | from unittest.mock import Mock, patch
|
|
14 | 15 | Document,
|
15 | 16 | Place,
|
16 | 17 | Poll,
|
| 18 | + PollChildBookWithManyToMany, |
| 19 | + PollChildRestaurantWithManyToMany, |
17 | 20 | PollWithAlternativeManager,
|
18 | 21 | PollWithExcludeFields,
|
19 | 22 | PollWithHistoricalSessionAttr,
|
| 23 | + PollWithManyToMany, |
| 24 | + PollWithManyToManyCustomHistoryID, |
| 25 | + PollWithManyToManyWithIPAddress, |
| 26 | + PollWithSelfManyToMany, |
| 27 | + PollWithSeveralManyToMany, |
20 | 28 | PollWithUniqueQuestion,
|
21 | 29 | Street,
|
22 | 30 | )
|
23 | 31 | from simple_history.utils import (
|
24 | 32 | bulk_create_with_history,
|
25 | 33 | bulk_update_with_history,
|
26 | 34 | get_history_manager_for_model,
|
| 35 | + get_history_model_for_model, |
| 36 | + get_m2m_field_name, |
| 37 | + get_m2m_reverse_field_name, |
27 | 38 | update_change_reason,
|
28 | 39 | )
|
29 | 40 |
|
30 | 41 | User = get_user_model()
|
31 | 42 |
|
32 | 43 |
|
| 44 | +class GetM2MFieldNamesTestCase(unittest.TestCase): |
| 45 | + def test__get_m2m_field_name__returns_expected_value(self): |
| 46 | + def field_names(model): |
| 47 | + history_model = get_history_model_for_model(model) |
| 48 | + # Sort the fields, to prevent flaky tests |
| 49 | + fields = sorted(history_model._history_m2m_fields, key=lambda f: f.name) |
| 50 | + return [get_m2m_field_name(field) for field in fields] |
| 51 | + |
| 52 | + self.assertListEqual(field_names(PollWithManyToMany), ["pollwithmanytomany"]) |
| 53 | + self.assertListEqual( |
| 54 | + field_names(PollWithManyToManyCustomHistoryID), |
| 55 | + ["pollwithmanytomanycustomhistoryid"], |
| 56 | + ) |
| 57 | + self.assertListEqual( |
| 58 | + field_names(PollWithManyToManyWithIPAddress), |
| 59 | + ["pollwithmanytomanywithipaddress"], |
| 60 | + ) |
| 61 | + self.assertListEqual( |
| 62 | + field_names(PollWithSeveralManyToMany), ["pollwithseveralmanytomany"] * 3 |
| 63 | + ) |
| 64 | + self.assertListEqual( |
| 65 | + field_names(PollChildBookWithManyToMany), |
| 66 | + ["pollchildbookwithmanytomany"] * 2, |
| 67 | + ) |
| 68 | + self.assertListEqual( |
| 69 | + field_names(PollChildRestaurantWithManyToMany), |
| 70 | + ["pollchildrestaurantwithmanytomany"] * 2, |
| 71 | + ) |
| 72 | + self.assertListEqual( |
| 73 | + field_names(PollWithSelfManyToMany), ["from_pollwithselfmanytomany"] |
| 74 | + ) |
| 75 | + |
| 76 | + def test__get_m2m_reverse_field_name__returns_expected_value(self): |
| 77 | + def field_names(model): |
| 78 | + history_model = get_history_model_for_model(model) |
| 79 | + # Sort the fields, to prevent flaky tests |
| 80 | + fields = sorted(history_model._history_m2m_fields, key=lambda f: f.name) |
| 81 | + return [get_m2m_reverse_field_name(field) for field in fields] |
| 82 | + |
| 83 | + self.assertListEqual(field_names(PollWithManyToMany), ["place"]) |
| 84 | + self.assertListEqual(field_names(PollWithManyToManyCustomHistoryID), ["place"]) |
| 85 | + self.assertListEqual(field_names(PollWithManyToManyWithIPAddress), ["place"]) |
| 86 | + self.assertListEqual( |
| 87 | + field_names(PollWithSeveralManyToMany), ["book", "place", "restaurant"] |
| 88 | + ) |
| 89 | + self.assertListEqual( |
| 90 | + field_names(PollChildBookWithManyToMany), ["book", "place"] |
| 91 | + ) |
| 92 | + self.assertListEqual( |
| 93 | + field_names(PollChildRestaurantWithManyToMany), ["place", "restaurant"] |
| 94 | + ) |
| 95 | + self.assertListEqual( |
| 96 | + field_names(PollWithSelfManyToMany), ["to_pollwithselfmanytomany"] |
| 97 | + ) |
| 98 | + |
| 99 | + |
33 | 100 | class BulkCreateWithHistoryTestCase(TestCase):
|
34 | 101 | def setUp(self):
|
35 | 102 | self.data = [
|
|
0 commit comments