test_create_fake_history_command.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from io import StringIO
  2. import pytest
  3. from django.contrib.auth import get_user_model
  4. from django.core.management import call_command
  5. from django.utils import timezone
  6. from ...categories.models import Category
  7. from ...threads.models import Thread
  8. from ...users.models import Rank
  9. from ..management.commands import createfakehistory
  10. from ..threads import get_fake_thread
  11. from ..users import get_fake_admin_activated_user, get_fake_inactive_user, get_fake_user
  12. User = get_user_model()
  13. @pytest.fixture
  14. def command(db):
  15. return createfakehistory.Command(stdout=StringIO())
  16. @pytest.fixture
  17. def date():
  18. return timezone.now()
  19. def test_management_command_has_no_errors(db):
  20. call_command(createfakehistory.Command(), max_actions=3, stdout=StringIO())
  21. def test_management_command_creates_fake_user(fake, command, date):
  22. ranks = list(Rank.objects.all())
  23. command.create_fake_user(fake, date, ranks)
  24. assert User.objects.exists()
  25. def test_fake_user_join_date_is_overridden_by_command(fake, command, date):
  26. ranks = list(Rank.objects.all())
  27. command.create_fake_user(fake, date, ranks)
  28. user = User.objects.order_by("-pk").last()
  29. assert user.joined_on == date
  30. def test_fake_user_rank_is_one_from_the_choices(fake, command, date):
  31. ranks = list(Rank.objects.all())
  32. command.create_fake_user(fake, date, ranks)
  33. user = User.objects.order_by("-pk").last()
  34. assert user.rank in ranks
  35. def test_none_is_returned_for_random_user_if_no_users_exist(command, date):
  36. user = command.get_random_user(date)
  37. assert user is None
  38. def test_users_created_after_given_date_are_excluded_from_random_user_pick(
  39. command, date, other_user
  40. ):
  41. other_user.joined_on = timezone.now()
  42. other_user.save()
  43. user = command.get_random_user(date)
  44. assert user is None
  45. def test_inactive_users_are_excluded_from_random_user_pick(fake, command):
  46. get_fake_admin_activated_user(fake)
  47. get_fake_inactive_user(fake)
  48. user = command.get_random_user(timezone.now())
  49. assert user is None
  50. def test_random_user_pick_returns_random_user(fake, command):
  51. valid_choices = [get_fake_user(fake) for _ in range(5)]
  52. user = command.get_random_user(timezone.now())
  53. assert user in valid_choices
  54. def test_management_command_creates_fake_thread(fake, command, date):
  55. categories = list(Category.objects.all_categories())
  56. command.create_fake_thread(fake, date, categories)
  57. assert Thread.objects.exists()
  58. def test_fake_thread_start_date_is_overridden_by_command(fake, command, date):
  59. categories = list(Category.objects.all_categories())
  60. command.create_fake_thread(fake, date, categories)
  61. thread = Thread.objects.last()
  62. assert thread.started_on == date
  63. def test_fake_thread_was_created_in_one_of_valid_categories(fake, command, date):
  64. categories = list(Category.objects.all_categories())
  65. command.create_fake_thread(fake, date, categories)
  66. thread = Thread.objects.last()
  67. assert thread.category in categories
  68. def test_none_is_returned_for_random_thread_if_no_threads_exist(command, date):
  69. thread = command.get_random_thread(date)
  70. assert thread is None
  71. def test_threads_created_after_given_date_are_excluded_from_random_thread_pick(
  72. fake, command, date, default_category
  73. ):
  74. get_fake_thread(fake, default_category)
  75. thread = command.get_random_thread(date)
  76. assert thread is None
  77. def test_random_thread_pick_returns_random_thread(fake, command, default_category):
  78. valid_choices = [get_fake_thread(fake, default_category) for _ in range(5)]
  79. thread = command.get_random_thread(timezone.now())
  80. assert thread in valid_choices