test_create_fake_history_command.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from datetime import timedelta
  2. from io import StringIO
  3. import pytest
  4. from django.contrib.auth import get_user_model
  5. from django.core.management import call_command
  6. from django.utils import timezone
  7. from ...categories.models import Category
  8. from ...threads.models import Post, Thread
  9. from ...users.models import Rank
  10. from ..management.commands import createfakehistory
  11. from ..threads import get_fake_thread
  12. from ..users import get_fake_admin_activated_user, get_fake_inactive_user, get_fake_user
  13. User = get_user_model()
  14. @pytest.fixture
  15. def command(db):
  16. return createfakehistory.Command(stdout=StringIO())
  17. @pytest.fixture
  18. def date():
  19. return timezone.now()
  20. def test_management_command_has_no_errors(db):
  21. call_command(createfakehistory.Command(), max_actions=3, stdout=StringIO())
  22. def test_management_command_creates_fake_user(fake, command, date):
  23. ranks = list(Rank.objects.all())
  24. command.create_fake_user(fake, date, ranks)
  25. assert User.objects.exists()
  26. def test_fake_user_join_date_is_overridden_by_command(fake, command, date):
  27. ranks = list(Rank.objects.all())
  28. command.create_fake_user(fake, date, ranks)
  29. user = User.objects.order_by("-pk").last()
  30. assert user.joined_on == date
  31. def test_fake_user_rank_is_one_from_the_choices(fake, command, date):
  32. ranks = list(Rank.objects.all())
  33. command.create_fake_user(fake, date, ranks)
  34. user = User.objects.order_by("-pk").last()
  35. assert user.rank in ranks
  36. def test_none_is_returned_for_random_user_if_no_users_exist(command, date):
  37. user = command.get_random_user(date)
  38. assert user is None
  39. def test_users_created_after_given_date_are_excluded_from_random_user_pick(
  40. command, date, other_user
  41. ):
  42. other_user.joined_on = timezone.now()
  43. other_user.save()
  44. user = command.get_random_user(date)
  45. assert user is None
  46. def test_inactive_users_are_excluded_from_random_user_pick(fake, command):
  47. get_fake_admin_activated_user(fake)
  48. get_fake_inactive_user(fake)
  49. user = command.get_random_user(timezone.now())
  50. assert user is None
  51. def test_random_user_pick_returns_random_user(fake, command):
  52. valid_choices = [get_fake_user(fake) for _ in range(5)]
  53. user = command.get_random_user(timezone.now())
  54. assert user in valid_choices
  55. def test_management_command_creates_fake_thread(fake, command, date):
  56. categories = list(Category.objects.all_categories())
  57. command.create_fake_thread(fake, date, categories)
  58. assert Thread.objects.exists()
  59. def test_fake_thread_start_date_is_overridden_by_command(fake, command, date):
  60. categories = list(Category.objects.all_categories())
  61. command.create_fake_thread(fake, date, categories)
  62. thread = Thread.objects.last()
  63. assert thread.started_on == date
  64. def test_fake_thread_was_created_in_one_of_valid_categories(fake, command, date):
  65. categories = list(Category.objects.all_categories())
  66. command.create_fake_thread(fake, date, categories)
  67. thread = Thread.objects.last()
  68. assert thread.category in categories
  69. def test_none_is_returned_for_random_thread_if_no_threads_exist(command, date):
  70. thread = command.get_random_thread(date)
  71. assert thread is None
  72. def test_threads_created_after_given_date_are_excluded_from_random_thread_pick(
  73. fake, command, date, default_category
  74. ):
  75. get_fake_thread(fake, default_category)
  76. thread = command.get_random_thread(date)
  77. assert thread is None
  78. def test_random_thread_pick_returns_random_thread(fake, command, default_category):
  79. valid_choices = [get_fake_thread(fake, default_category) for _ in range(5)]
  80. thread = command.get_random_thread(timezone.now())
  81. assert thread in valid_choices
  82. def test_management_command_creates_fake_post(fake, command, default_category):
  83. thread = get_fake_thread(fake, default_category)
  84. command.create_fake_post(fake, timezone.now())
  85. assert thread.post_set.count() == 2
  86. def test_fake_post_creation_date_is_overridden_by_command(
  87. fake, command, date, default_category
  88. ):
  89. thread = get_fake_thread(fake, default_category)
  90. thread.started_on -= timedelta(days=1)
  91. thread.save()
  92. command.create_fake_post(fake, date)
  93. post = thread.post_set.last()
  94. assert post.posted_on == date
  95. def test_fake_post_is_not_created_if_no_threads_exist(fake, command, date):
  96. command.create_fake_post(fake, date)
  97. assert not Post.objects.exists()
  98. def test_management_command_synchronizes_threads(fake, command, date, default_category):
  99. command.create_fake_thread(fake, date, [default_category])
  100. command.synchronize_threads()
  101. def test_management_command_synchronizes_categories(
  102. fake, command, date, default_category
  103. ):
  104. command.create_fake_thread(fake, date, [default_category])
  105. command.synchronize_threads()
  106. command.synchronize_categories()