|
| 1 | +from django.db import IntegrityError |
| 2 | +from django.test import TestCase, TransactionTestCase |
| 3 | +from django.utils.timezone import now |
| 4 | +from mock import Mock, patch |
| 5 | + |
| 6 | +from simple_history.exceptions import NotHistoricalModelError |
| 7 | +from simple_history.tests.models import ( |
| 8 | + Document, |
| 9 | + Place, |
| 10 | + Poll, |
| 11 | + PollWithExcludeFields |
| 12 | +) |
| 13 | +from simple_history.utils import bulk_create_with_history |
| 14 | + |
| 15 | + |
| 16 | +class BulkCreateWithHistoryTestCase(TestCase): |
| 17 | + def setUp(self): |
| 18 | + self.data = [ |
| 19 | + Poll(id=1, question='Question 1', pub_date=now()), |
| 20 | + Poll(id=2, question='Question 2', pub_date=now()), |
| 21 | + Poll(id=3, question='Question 3', pub_date=now()), |
| 22 | + Poll(id=4, question='Question 4', pub_date=now()), |
| 23 | + Poll(id=5, question='Question 5', pub_date=now()), |
| 24 | + ] |
| 25 | + |
| 26 | + def test_bulk_create_history(self): |
| 27 | + bulk_create_with_history(self.data, Poll) |
| 28 | + |
| 29 | + self.assertEqual(Poll.objects.count(), 5) |
| 30 | + self.assertEqual(Poll.history.count(), 5) |
| 31 | + |
| 32 | + def test_bulk_create_history_num_queries_is_two(self): |
| 33 | + with self.assertNumQueries(2): |
| 34 | + bulk_create_with_history(self.data, Poll) |
| 35 | + |
| 36 | + def test_bulk_create_history_on_model_without_history_raises_error(self): |
| 37 | + self.data = [ |
| 38 | + Place(id=1, name='Place 1'), |
| 39 | + Place(id=2, name='Place 2'), |
| 40 | + Place(id=3, name='Place 3'), |
| 41 | + ] |
| 42 | + with self.assertRaises(NotHistoricalModelError): |
| 43 | + bulk_create_with_history(self.data, Place) |
| 44 | + |
| 45 | + def test_num_queries_when_batch_size_is_less_than_total(self): |
| 46 | + with self.assertNumQueries(6): |
| 47 | + bulk_create_with_history(self.data, Poll, batch_size=2) |
| 48 | + |
| 49 | + def test_bulk_create_history_with_batch_size(self): |
| 50 | + bulk_create_with_history(self.data, Poll, batch_size=2) |
| 51 | + |
| 52 | + self.assertEqual(Poll.objects.count(), 5) |
| 53 | + self.assertEqual(Poll.history.count(), 5) |
| 54 | + |
| 55 | + def test_bulk_create_works_with_excluded_fields(self): |
| 56 | + bulk_create_with_history(self.data, PollWithExcludeFields) |
| 57 | + |
| 58 | + self.assertEqual(Poll.objects.count(), 0) |
| 59 | + self.assertEqual(Poll.history.count(), 0) |
| 60 | + |
| 61 | + self.assertEqual(PollWithExcludeFields.objects.count(), 5) |
| 62 | + self.assertEqual(PollWithExcludeFields.history.count(), 5) |
| 63 | + |
| 64 | + |
| 65 | +class BulkCreateWithHistoryTransactionTestCase(TransactionTestCase): |
| 66 | + def setUp(self): |
| 67 | + self.data = [ |
| 68 | + Poll(id=1, question='Question 1', pub_date=now()), |
| 69 | + Poll(id=2, question='Question 2', pub_date=now()), |
| 70 | + Poll(id=3, question='Question 3', pub_date=now()), |
| 71 | + Poll(id=4, question='Question 4', pub_date=now()), |
| 72 | + Poll(id=5, question='Question 5', pub_date=now()), |
| 73 | + ] |
| 74 | + |
| 75 | + @patch('simple_history.manager.HistoryManager.bulk_history_create', |
| 76 | + Mock(side_effect=Exception)) |
| 77 | + def test_transaction_rolls_back_if_bulk_history_create_fails(self): |
| 78 | + with self.assertRaises(Exception): |
| 79 | + bulk_create_with_history(self.data, Poll) |
| 80 | + |
| 81 | + self.assertEqual(Poll.objects.count(), 0) |
| 82 | + self.assertEqual(Poll.history.count(), 0) |
| 83 | + |
| 84 | + def test_bulk_create_history_on_objects_that_already_exist(self): |
| 85 | + Poll.objects.bulk_create(self.data) |
| 86 | + |
| 87 | + with self.assertRaises(IntegrityError): |
| 88 | + bulk_create_with_history(self.data, Poll) |
| 89 | + |
| 90 | + self.assertEqual(Poll.objects.count(), 5) |
| 91 | + self.assertEqual(Poll.history.count(), 0) |
| 92 | + |
| 93 | + def test_bulk_create_history_rolls_back_when_last_exists(self): |
| 94 | + Poll.objects.create(id=5, question='Question 5', pub_date=now()) |
| 95 | + |
| 96 | + self.assertEqual(Poll.objects.count(), 1) |
| 97 | + self.assertEqual(Poll.history.count(), 1) |
| 98 | + |
| 99 | + with self.assertRaises(IntegrityError): |
| 100 | + bulk_create_with_history(self.data, Poll, batch_size=1) |
| 101 | + |
| 102 | + self.assertEqual(Poll.objects.count(), 1) |
| 103 | + self.assertEqual(Poll.history.count(), 1) |
| 104 | + |
| 105 | + def test_bulk_create_fails_with_wrong_model(self): |
| 106 | + with self.assertRaises(AttributeError): |
| 107 | + bulk_create_with_history(self.data, Document) |
| 108 | + |
| 109 | + self.assertEqual(Poll.objects.count(), 0) |
| 110 | + self.assertEqual(Poll.history.count(), 0) |
0 commit comments