Browse Source

Add some tests to createfakehistory command

rafalp 6 years ago
parent
commit
09bfcd3d01

+ 10 - 6
misago/faker/management/commands/createfakehistory.py

@@ -41,9 +41,17 @@ class Command(BaseCommand):
             type=int,
             type=int,
             default=5,
             default=5,
         )
         )
+        parser.add_argument(
+            "max_actions",
+            help="number of items generate for a single day",
+            nargs="?",
+            type=int,
+            default=50,
+        )
 
 
     def handle(self, *args, **options):  # pylint: disable=too-many-locals
     def handle(self, *args, **options):  # pylint: disable=too-many-locals
         history_length = options["length"]
         history_length = options["length"]
+        max_actions = options["max_actions"]
         fake = Factory.create()
         fake = Factory.create()
 
 
         categories = list(Category.objects.all_categories())
         categories = list(Category.objects.all_categories())
@@ -59,7 +67,7 @@ class Command(BaseCommand):
         start_timestamp = timezone.now()
         start_timestamp = timezone.now()
         for days_ago in reversed(range(history_length)):
         for days_ago in reversed(range(history_length)):
             date = start_timestamp - timedelta(days=days_ago)
             date = start_timestamp - timedelta(days=days_ago)
-            for date_variation in get_random_date_variations(date, 0, 50):
+            for date_variation in get_random_date_variations(date, 0, max_actions):
                 action = random.randint(0, 100)
                 action = random.randint(0, 100)
                 if action >= 80:
                 if action >= 80:
                     self.create_fake_user(fake, date_variation, ranks)
                     self.create_fake_user(fake, date_variation, ranks)
@@ -86,10 +94,6 @@ class Command(BaseCommand):
             user.audittrail_set.all().delete()
             user.audittrail_set.all().delete()
 
 
     def create_fake_user(self, fake, date, ranks):
     def create_fake_user(self, fake, date, ranks):
-        # There's 40% chance user has registered on this day
-        if random.randint(1, 100) > 25:
-            return
-
         # Pick random rank for next user
         # Pick random rank for next user
         rank = random.choice(ranks)
         rank = random.choice(ranks)
 
 
@@ -249,7 +253,7 @@ class Command(BaseCommand):
         self.stdout.write("\nSynchronizing categories...")
         self.stdout.write("\nSynchronizing categories...")
         start_time = time.time()
         start_time = time.time()
 
 
-        for category in Category.objects.all():
+        for category in Category.objects.all_categories():
             category.synchronize()
             category.synchronize()
             category.save()
             category.save()
 
 

+ 109 - 1
misago/faker/tests/test_create_fake_history_command.py

@@ -1,9 +1,117 @@
 from io import StringIO
 from io import StringIO
 
 
+import pytest
+from django.contrib.auth import get_user_model
 from django.core.management import call_command
 from django.core.management import call_command
+from django.utils import timezone
 
 
+from ...categories.models import Category
+from ...threads.models import Thread
+from ...users.models import Rank
 from ..management.commands import createfakehistory
 from ..management.commands import createfakehistory
+from ..threads import get_fake_thread
+from ..users import get_fake_admin_activated_user, get_fake_inactive_user, get_fake_user
+
+User = get_user_model()
+
+
+@pytest.fixture
+def command(db):
+    return createfakehistory.Command(stdout=StringIO())
+
+
+@pytest.fixture
+def date():
+    return timezone.now()
 
 
 
 
 def test_management_command_has_no_errors(db):
 def test_management_command_has_no_errors(db):
-    call_command(createfakehistory.Command(), stdout=StringIO())
+    call_command(createfakehistory.Command(), max_actions=3, stdout=StringIO())
+
+
+def test_management_command_creates_fake_user(fake, command, date):
+    ranks = list(Rank.objects.all())
+    command.create_fake_user(fake, date, ranks)
+    assert User.objects.exists()
+
+
+def test_fake_user_join_date_is_overridden_by_command(fake, command, date):
+    ranks = list(Rank.objects.all())
+    command.create_fake_user(fake, date, ranks)
+    user = User.objects.order_by("-pk").last()
+    assert user.joined_on == date
+
+
+def test_fake_user_rank_is_one_from_the_choices(fake, command, date):
+    ranks = list(Rank.objects.all())
+    command.create_fake_user(fake, date, ranks)
+    user = User.objects.order_by("-pk").last()
+    assert user.rank in ranks
+
+
+def test_none_is_returned_for_random_user_if_no_users_exist(command, date):
+    user = command.get_random_user(date)
+    assert user is None
+
+
+def test_users_created_after_given_date_are_excluded_from_random_user_pick(
+    command, date, other_user
+):
+    other_user.joined_on = timezone.now()
+    other_user.save()
+
+    user = command.get_random_user(date)
+    assert user is None
+
+
+def test_inactive_users_are_excluded_from_random_user_pick(fake, command):
+    get_fake_admin_activated_user(fake)
+    get_fake_inactive_user(fake)
+
+    user = command.get_random_user(timezone.now())
+    assert user is None
+
+
+def test_random_user_pick_returns_random_user(fake, command):
+    valid_choices = [get_fake_user(fake) for _ in range(5)]
+    user = command.get_random_user(timezone.now())
+    assert user in valid_choices
+
+
+def test_management_command_creates_fake_thread(fake, command, date):
+    categories = list(Category.objects.all_categories())
+    command.create_fake_thread(fake, date, categories)
+    assert Thread.objects.exists()
+
+
+def test_fake_thread_start_date_is_overridden_by_command(fake, command, date):
+    categories = list(Category.objects.all_categories())
+    command.create_fake_thread(fake, date, categories)
+    thread = Thread.objects.last()
+    assert thread.started_on == date
+
+
+def test_fake_thread_was_created_in_one_of_valid_categories(fake, command, date):
+    categories = list(Category.objects.all_categories())
+    command.create_fake_thread(fake, date, categories)
+    thread = Thread.objects.last()
+    assert thread.category in categories
+
+
+def test_none_is_returned_for_random_thread_if_no_threads_exist(command, date):
+    thread = command.get_random_thread(date)
+    assert thread is None
+
+
+def test_threads_created_after_given_date_are_excluded_from_random_thread_pick(
+    fake, command, date, default_category
+):
+    get_fake_thread(fake, default_category)
+    thread = command.get_random_thread(date)
+    assert thread is None
+
+
+def test_random_thread_pick_returns_random_thread(fake, command, default_category):
+    valid_choices = [get_fake_thread(fake, default_category) for _ in range(5)]
+    thread = command.get_random_thread(timezone.now())
+    assert thread in valid_choices