tests.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. from __future__ import unicode_literals
  2. from operator import attrgetter
  3. from django.db import connection
  4. from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
  5. from django.test import override_settings
  6. from .models import Country, Restaurant, Pizzeria, State, TwoFields
  7. class BulkCreateTests(TestCase):
  8. def setUp(self):
  9. self.data = [
  10. Country(name="United States of America", iso_two_letter="US"),
  11. Country(name="The Netherlands", iso_two_letter="NL"),
  12. Country(name="Germany", iso_two_letter="DE"),
  13. Country(name="Czech Republic", iso_two_letter="CZ")
  14. ]
  15. def test_simple(self):
  16. created = Country.objects.bulk_create(self.data)
  17. self.assertEqual(len(created), 4)
  18. self.assertQuerysetEqual(Country.objects.order_by("-name"), [
  19. "United States of America", "The Netherlands", "Germany", "Czech Republic"
  20. ], attrgetter("name"))
  21. created = Country.objects.bulk_create([])
  22. self.assertEqual(created, [])
  23. self.assertEqual(Country.objects.count(), 4)
  24. @skipUnlessDBFeature('has_bulk_insert')
  25. def test_efficiency(self):
  26. with self.assertNumQueries(1):
  27. Country.objects.bulk_create(self.data)
  28. def test_inheritance(self):
  29. Restaurant.objects.bulk_create([
  30. Restaurant(name="Nicholas's")
  31. ])
  32. self.assertQuerysetEqual(Restaurant.objects.all(), [
  33. "Nicholas's",
  34. ], attrgetter("name"))
  35. with self.assertRaises(ValueError):
  36. Pizzeria.objects.bulk_create([
  37. Pizzeria(name="The Art of Pizza")
  38. ])
  39. self.assertQuerysetEqual(Pizzeria.objects.all(), [])
  40. self.assertQuerysetEqual(Restaurant.objects.all(), [
  41. "Nicholas's",
  42. ], attrgetter("name"))
  43. def test_non_auto_increment_pk(self):
  44. State.objects.bulk_create([
  45. State(two_letter_code=s)
  46. for s in ["IL", "NY", "CA", "ME"]
  47. ])
  48. self.assertQuerysetEqual(State.objects.order_by("two_letter_code"), [
  49. "CA", "IL", "ME", "NY",
  50. ], attrgetter("two_letter_code"))
  51. @skipUnlessDBFeature('has_bulk_insert')
  52. def test_non_auto_increment_pk_efficiency(self):
  53. with self.assertNumQueries(1):
  54. State.objects.bulk_create([
  55. State(two_letter_code=s)
  56. for s in ["IL", "NY", "CA", "ME"]
  57. ])
  58. self.assertQuerysetEqual(State.objects.order_by("two_letter_code"), [
  59. "CA", "IL", "ME", "NY",
  60. ], attrgetter("two_letter_code"))
  61. @skipIfDBFeature('allows_auto_pk_0')
  62. def test_zero_as_autoval(self):
  63. """
  64. Zero as id for AutoField should raise exception in MySQL, because MySQL
  65. does not allow zero for automatic primary key.
  66. """
  67. valid_country = Country(name='Germany', iso_two_letter='DE')
  68. invalid_country = Country(id=0, name='Poland', iso_two_letter='PL')
  69. with self.assertRaises(ValueError):
  70. Country.objects.bulk_create([valid_country, invalid_country])
  71. def test_batch_same_vals(self):
  72. # Sqlite had a problem where all the same-valued models were
  73. # collapsed to one insert.
  74. Restaurant.objects.bulk_create([
  75. Restaurant(name='foo') for i in range(0, 2)
  76. ])
  77. self.assertEqual(Restaurant.objects.count(), 2)
  78. def test_large_batch(self):
  79. with override_settings(DEBUG=True):
  80. connection.queries_log.clear()
  81. TwoFields.objects.bulk_create([
  82. TwoFields(f1=i, f2=i + 1) for i in range(0, 1001)
  83. ])
  84. self.assertEqual(TwoFields.objects.count(), 1001)
  85. self.assertEqual(
  86. TwoFields.objects.filter(f1__gte=450, f1__lte=550).count(),
  87. 101)
  88. self.assertEqual(TwoFields.objects.filter(f2__gte=901).count(), 101)
  89. @skipUnlessDBFeature('has_bulk_insert')
  90. def test_large_single_field_batch(self):
  91. # SQLite had a problem with more than 500 UNIONed selects in single
  92. # query.
  93. Restaurant.objects.bulk_create([
  94. Restaurant() for i in range(0, 501)
  95. ])
  96. @skipUnlessDBFeature('has_bulk_insert')
  97. def test_large_batch_efficiency(self):
  98. with override_settings(DEBUG=True):
  99. connection.queries_log.clear()
  100. TwoFields.objects.bulk_create([
  101. TwoFields(f1=i, f2=i + 1) for i in range(0, 1001)
  102. ])
  103. self.assertTrue(len(connection.queries) < 10)
  104. def test_large_batch_mixed(self):
  105. """
  106. Test inserting a large batch with objects having primary key set
  107. mixed together with objects without PK set.
  108. """
  109. with override_settings(DEBUG=True):
  110. connection.queries_log.clear()
  111. TwoFields.objects.bulk_create([
  112. TwoFields(id=i if i % 2 == 0 else None, f1=i, f2=i + 1)
  113. for i in range(100000, 101000)])
  114. self.assertEqual(TwoFields.objects.count(), 1000)
  115. # We can't assume much about the ID's created, except that the above
  116. # created IDs must exist.
  117. id_range = range(100000, 101000, 2)
  118. self.assertEqual(TwoFields.objects.filter(id__in=id_range).count(), 500)
  119. self.assertEqual(TwoFields.objects.exclude(id__in=id_range).count(), 500)
  120. @skipUnlessDBFeature('has_bulk_insert')
  121. def test_large_batch_mixed_efficiency(self):
  122. """
  123. Test inserting a large batch with objects having primary key set
  124. mixed together with objects without PK set.
  125. """
  126. with override_settings(DEBUG=True):
  127. connection.queries_log.clear()
  128. TwoFields.objects.bulk_create([
  129. TwoFields(id=i if i % 2 == 0 else None, f1=i, f2=i + 1)
  130. for i in range(100000, 101000)])
  131. self.assertTrue(len(connection.queries) < 10)
  132. def test_explicit_batch_size(self):
  133. objs = [TwoFields(f1=i, f2=i) for i in range(0, 4)]
  134. TwoFields.objects.bulk_create(objs, 2)
  135. self.assertEqual(TwoFields.objects.count(), len(objs))
  136. TwoFields.objects.all().delete()
  137. TwoFields.objects.bulk_create(objs, len(objs))
  138. self.assertEqual(TwoFields.objects.count(), len(objs))
  139. @skipUnlessDBFeature('has_bulk_insert')
  140. def test_explicit_batch_size_efficiency(self):
  141. objs = [TwoFields(f1=i, f2=i) for i in range(0, 100)]
  142. with self.assertNumQueries(2):
  143. TwoFields.objects.bulk_create(objs, 50)
  144. TwoFields.objects.all().delete()
  145. with self.assertNumQueries(1):
  146. TwoFields.objects.bulk_create(objs, len(objs))