Browse Source

Redo create fake threads/posts commands, add first test for fake history creator

rafalp 6 years ago
parent
commit
1d419b12d6

+ 74 - 0
misago/faker/management/commands/createfakeposts.py

@@ -0,0 +1,74 @@
+import random
+import time
+
+from django.contrib.auth import get_user_model
+from django.core.management.base import BaseCommand
+from faker import Factory
+
+from ....categories.models import Category
+from ....core.management.progressbar import show_progress
+from ....threads.models import Thread
+from ...posts import get_fake_hidden_post, get_fake_post, get_fake_unapproved_post
+
+User = get_user_model()
+
+
+class Command(BaseCommand):
+    help = "Creates random posts for dev and testing purposes."
+
+    def add_arguments(self, parser):
+        parser.add_argument(
+            "posts", help="number of posts to create", nargs="?", type=int, default=5
+        )
+
+    def handle(self, *args, **options):
+        items_to_create = options["posts"]
+        fake = Factory.create()
+
+        message = "Creating %s fake posts...\n"
+        self.stdout.write(message % items_to_create)
+
+        created_posts = 0
+        start_time = time.time()
+        show_progress(self, created_posts, items_to_create)
+
+        while created_posts < items_to_create:
+            thread = Thread.objects.order_by("?")[:1].first()
+
+            # 10% chance poster is anonymous
+            if random.randint(0, 100) > 90:
+                poster = None
+            else:
+                poster = User.objects.order_by("?")[:1].last()
+
+            # There's 5% chance post is unapproved
+            if random.randint(0, 100) > 90:
+                get_fake_unapproved_post(fake, thread, poster)
+
+            # There's further 5% chance post is hidden
+            elif random.randint(0, 100) > 95:
+                if random.randint(0, 100) > 90:
+                    hidden_by = None
+                else:
+                    hidden_by = User.objects.order_by("?")[:1].last()
+
+                get_fake_hidden_post(fake, thread, poster, hidden_by)
+
+            # Default, standard post
+            else:
+                get_fake_post(fake, thread, poster)
+
+            thread.synchronize()
+            thread.save()
+
+            created_posts += 1
+            show_progress(self, created_posts, items_to_create, start_time)
+
+        for category in Category.objects.all():
+            category.synchronize()
+            category.save()
+
+        total_time = time.time() - start_time
+        total_humanized = time.strftime("%H:%M:%S", time.gmtime(total_time))
+        message = "\nSuccessfully created %s fake posts in %s"
+        self.stdout.write(message % (created_posts, total_humanized))

+ 36 - 138
misago/faker/management/commands/createfakethreads.py

@@ -3,23 +3,20 @@ import time
 
 
 from django.contrib.auth import get_user_model
 from django.contrib.auth import get_user_model
 from django.core.management.base import BaseCommand
 from django.core.management.base import BaseCommand
-from django.db.transaction import atomic
-from django.utils import timezone
 from faker import Factory
 from faker import Factory
 
 
 from ....categories.models import Category
 from ....categories.models import Category
 from ....core.management.progressbar import show_progress
 from ....core.management.progressbar import show_progress
-from ....threads.checksums import update_post_checksum
-from ....threads.models import Post, Thread
-from ...englishcorpus import EnglishCorpus
-
-PLACEKITTEN_URL = "https://placekitten.com/g/%s/%s"
+from ....threads.models import Thread
+from ...threads import (
+    get_fake_closed_thread,
+    get_fake_hidden_thread,
+    get_fake_thread,
+    get_fake_unapproved_thread,
+)
 
 
 User = get_user_model()
 User = get_user_model()
 
 
-corpus = EnglishCorpus()
-corpus_short = EnglishCorpus(max_length=150)
-
 
 
 class Command(BaseCommand):
 class Command(BaseCommand):
     help = "Creates random threads for dev and testing purposes."
     help = "Creates random threads for dev and testing purposes."
@@ -35,13 +32,12 @@ class Command(BaseCommand):
 
 
     def handle(
     def handle(
         self, *args, **options
         self, *args, **options
-    ):  # pylint: disable=too-many-branches, too-many-locals
+    ):  # pylint: disable=too-many-locals, too-many-branches
         items_to_create = options["threads"]
         items_to_create = options["threads"]
+        fake = Factory.create()
 
 
         categories = list(Category.objects.all_categories())
         categories = list(Category.objects.all_categories())
 
 
-        fake = Factory.create()
-
         message = "Creating %s fake threads...\n"
         message = "Creating %s fake threads...\n"
         self.stdout.write(message % items_to_create)
         self.stdout.write(message % items_to_create)
 
 
@@ -50,117 +46,47 @@ class Command(BaseCommand):
         show_progress(self, created_threads, items_to_create)
         show_progress(self, created_threads, items_to_create)
 
 
         while created_threads < items_to_create:
         while created_threads < items_to_create:
-            with atomic():
-                datetime = timezone.now()
-                category = random.choice(categories)
-                user = User.objects.order_by("?")[:1][0]
-
-                thread_is_unapproved = random.randint(0, 100) > 90
-                thread_is_hidden = random.randint(0, 100) > 90
-                thread_is_closed = random.randint(0, 100) > 90
-
-                thread = Thread(
-                    category=category,
-                    started_on=datetime,
-                    starter_name="-",
-                    starter_slug="-",
-                    last_post_on=datetime,
-                    last_poster_name="-",
-                    last_poster_slug="-",
-                    replies=0,
-                    is_unapproved=thread_is_unapproved,
-                    is_hidden=thread_is_hidden,
-                    is_closed=thread_is_closed,
-                )
-                thread.set_title(corpus_short.random_sentence())
-                thread.save()
-
-                original, parsed = self.fake_post_content()
-
-                post = Post.objects.create(
-                    category=category,
-                    thread=thread,
-                    poster=user,
-                    poster_name=user.username,
-                    original=original,
-                    parsed=parsed,
-                    posted_on=datetime,
-                    updated_on=datetime,
-                )
-                update_post_checksum(post)
-                post.save(update_fields=["checksum"])
-
-                thread.set_first_post(post)
-                thread.set_last_post(post)
-                thread.save()
-
-                user.threads += 1
-                user.posts += 1
-                user.save()
-
-                thread_type = random.randint(0, 100)
-                if thread_type > 98:
-                    thread_replies = random.randint(200, 2500)
-                elif thread_type > 50:
-                    thread_replies = random.randint(5, 30)
-                else:
-                    thread_replies = random.randint(0, 10)
+            category = random.choice(categories)
 
 
-                for _ in range(thread_replies):
-                    datetime = timezone.now()
-                    user = User.objects.order_by("?")[:1][0]
-
-                    original, parsed = self.fake_post_content()
-
-                    is_unapproved = random.randint(0, 100) > 97
-
-                    post = Post.objects.create(
-                        category=category,
-                        thread=thread,
-                        poster=user,
-                        poster_name=user.username,
-                        original=original,
-                        parsed=parsed,
-                        is_unapproved=is_unapproved,
-                        posted_on=datetime,
-                        updated_on=datetime,
-                    )
+            # 10% chance thread poster is anonymous
+            if random.randint(0, 100) > 90:
+                starter = None
+            else:
+                starter = User.objects.order_by("?")[:1].last()
 
 
-                    if not is_unapproved:
-                        is_hidden = random.randint(0, 100) > 97
-                    else:
-                        is_hidden = False
+            # There's 10% chance thread is closed
+            if random.randint(0, 100) > 90:
+                thread = get_fake_closed_thread(fake, category, starter)
 
 
-                    if is_hidden:
-                        post.is_hidden = True
+            # There's further 5% chance thread is hidden
+            elif random.randint(0, 100) > 95:
+                if random.randint(0, 100) > 90:
+                    hidden_by = None
+                else:
+                    hidden_by = User.objects.order_by("?")[:1].last()
 
 
-                        if random.randint(0, 100) < 80:
-                            user = User.objects.order_by("?")[:1][0]
-                            post.hidden_by = user
-                            post.hidden_by_name = user.username
-                            post.hidden_by_slug = user.username
-                        else:
-                            post.hidden_by_name = fake.first_name()
-                            post.hidden_by_slug = post.hidden_by_name.lower()
+                thread = get_fake_hidden_thread(fake, category, starter, hidden_by)
 
 
-                    update_post_checksum(post)
-                    post.save()
+            # And further 5% chance thread is unapproved
+            elif random.randint(0, 100) > 95:
+                thread = get_fake_unapproved_thread(fake, category, starter)
 
 
-                    user.posts += 1
-                    user.save()
+            # Default, standard thread
+            else:
+                thread = get_fake_thread(fake, category, starter)
 
 
-                thread.synchronize()
-                thread.save()
+            thread.synchronize()
+            thread.save()
 
 
-                created_threads += 1
-                show_progress(self, created_threads, items_to_create, start_time)
+            created_threads += 1
+            show_progress(self, created_threads, items_to_create, start_time)
 
 
         pinned_threads = random.randint(0, int(created_threads * 0.025)) or 1
         pinned_threads = random.randint(0, int(created_threads * 0.025)) or 1
         self.stdout.write("\nPinning %s threads..." % pinned_threads)
         self.stdout.write("\nPinning %s threads..." % pinned_threads)
 
 
         for _ in range(0, pinned_threads):
         for _ in range(0, pinned_threads):
             thread = Thread.objects.order_by("?")[:1][0]
             thread = Thread.objects.order_by("?")[:1][0]
-            if random.randint(0, 100) > 75:
+            if random.randint(0, 100) > 90:
                 thread.weight = 2
                 thread.weight = 2
             else:
             else:
                 thread.weight = 1
                 thread.weight = 1
@@ -174,31 +100,3 @@ class Command(BaseCommand):
         total_humanized = time.strftime("%H:%M:%S", time.gmtime(total_time))
         total_humanized = time.strftime("%H:%M:%S", time.gmtime(total_time))
         message = "\nSuccessfully created %s fake threads in %s"
         message = "\nSuccessfully created %s fake threads in %s"
         self.stdout.write(message % (created_threads, total_humanized))
         self.stdout.write(message % (created_threads, total_humanized))
-
-    def fake_post_content(self):
-        raw = []
-        parsed = []
-
-        if random.randint(0, 100) > 80:
-            paragraphs_to_make = random.randint(1, 20)
-        else:
-            paragraphs_to_make = random.randint(1, 5)
-
-        for _ in range(paragraphs_to_make):
-            if random.randint(0, 100) > 95:
-                cat_width = random.randint(1, 16) * random.choice([100, 90, 80])
-                cat_height = random.randint(1, 12) * random.choice([100, 90, 80])
-
-                cat_url = PLACEKITTEN_URL % (cat_width, cat_height)
-
-                raw.append("!(%s)" % cat_url)
-                parsed.append('<p><img src="%s" alt=""/></p>' % cat_url)
-            else:
-                if random.randint(0, 100) > 95:
-                    sentences_to_make = random.randint(1, 20)
-                else:
-                    sentences_to_make = random.randint(1, 7)
-                raw.append(" ".join(corpus.random_sentences(sentences_to_make)))
-                parsed.append("<p>%s</p>" % raw[-1])
-
-        return "\n\n".join(raw), "\n".join(parsed)

+ 9 - 0
misago/faker/tests/test_create_fake_history_command.py

@@ -0,0 +1,9 @@
+from io import StringIO
+
+from django.core.management import call_command
+
+from ..management.commands import createfakehistory
+
+
+def test_management_command_has_no_errors(db):
+    call_command(createfakehistory.Command(), stdout=StringIO())

+ 13 - 0
misago/faker/tests/test_create_fake_posts_command.py

@@ -0,0 +1,13 @@
+from io import StringIO
+
+from django.core.management import call_command
+
+from ...threads.models import Post
+from ..management.commands import createfakeposts
+from ..threads import get_fake_thread
+
+
+def test_management_command_creates_fake_threads(fake, default_category):
+    thread = get_fake_thread(fake, default_category)
+    call_command(createfakeposts.Command(), stdout=StringIO())
+    assert Post.objects.exclude(pk=thread.first_post.pk).exists()

+ 11 - 0
misago/faker/tests/test_create_fake_threads_command.py

@@ -0,0 +1,11 @@
+from io import StringIO
+
+from django.core.management import call_command
+
+from ...threads.models import Thread
+from ..management.commands import createfakethreads
+
+
+def test_management_command_creates_fake_threads(db):
+    call_command(createfakethreads.Command(), stdout=StringIO())
+    assert Thread.objects.exists()