rafalp 6 лет назад
Родитель
Сommit
214d5502f9

+ 4 - 7
misago/faker/management/commands/createfakehistory.py

@@ -10,7 +10,7 @@ from faker import Factory
 from ....categories.models import Category
 from ....core.pgutils import chunk_queryset
 from ....threads.checksums import update_post_checksum
-from ....threads.models import Post, Thread
+from ....threads.models import Thread
 from ....users.models import Rank
 from ...posts import get_fake_hidden_post, get_fake_post, get_fake_unapproved_post
 from ...threads import (
@@ -42,7 +42,7 @@ class Command(BaseCommand):
             default=5,
         )
 
-    def handle(self, *args, **options):
+    def handle(self, *args, **options):  # pylint: disable=too-many-locals
         history_length = options["length"]
         fake = Factory.create()
 
@@ -210,9 +210,6 @@ class Command(BaseCommand):
 
         self.write_event(date, "%s followed %s" % (user_a, user_b))
 
-    def get_random_post(self, data):
-        return Post.objects.filter(posted_on__lt=date).order_by("?").first()
-
     def get_random_thread(self, date):
         return (
             Thread.objects.filter(started_on__lt=date)
@@ -263,9 +260,9 @@ class Command(BaseCommand):
         self.stdout.write(message % (Category.objects.count(), total_humanized))
 
 
-def get_random_date_variations(date, min, max):
+def get_random_date_variations(date, min_date, max_date):
     variations = []
-    for _ in range(random.randint(min, max)):
+    for _ in range(random.randint(min_date, max_date)):
         random_offset = timedelta(minutes=random.randint(1, 1200))
         variations.append(date - random_offset)
     return sorted(variations)

+ 1 - 1
misago/faker/posts.py

@@ -20,7 +20,7 @@ def get_fake_post(fake, thread, poster=None):
         category=thread.category,
         thread=thread,
         poster=poster,
-        poster_name=poster or get_fake_username(fake),
+        poster_name=poster.username if poster else get_fake_username(fake),
         original=original,
         parsed=parsed,
         posted_on=posted_on,

+ 3 - 5
misago/faker/tests/test_englishcorpus.py

@@ -1,11 +1,9 @@
-from django.test import TestCase
-
 from ..englishcorpus import EnglishCorpus
 
 
 def test_corpus_has_length():
     corpus = EnglishCorpus()
-    assert len(corpus) > 0
+    assert corpus
 
 
 def test_corpus_can_be_shuffled():
@@ -15,12 +13,12 @@ def test_corpus_can_be_shuffled():
 
 def test_corpus_can_be_limited_to_phrases_shorter_than_specified():
     corpus = EnglishCorpus(max_length=100)
-    assert len(corpus) > 0
+    assert corpus
 
 
 def test_corpus_can_be_limited_to_phrases_longer_than_specified():
     corpus = EnglishCorpus(min_length=100)
-    assert len(corpus) > 0
+    assert corpus
 
 
 def test_corpus_produces_random_sequence():

+ 55 - 0
misago/faker/tests/test_fake_threads.py

@@ -0,0 +1,55 @@
+from ..threads import (
+    get_fake_closed_thread,
+    get_fake_hidden_thread,
+    get_fake_thread,
+    get_fake_unapproved_thread,
+)
+
+
+def test_fake_thread_can_be_created(fake, default_category):
+    assert get_fake_thread(fake, default_category)
+
+
+def test_fake_thread_is_created_with_opening_post(fake, default_category):
+    thread = get_fake_thread(fake, default_category)
+    assert thread.first_post
+
+
+def test_fake_thread_is_created_with_guest_starter(fake, default_category):
+    thread = get_fake_thread(fake, default_category)
+    assert thread.first_post.poster is None
+
+
+def test_fake_thread_is_created_with_specified_starter(fake, default_category, user):
+    thread = get_fake_thread(fake, default_category, user)
+    assert thread.first_post.poster == user
+    assert thread.first_post.poster_name == user.username
+
+
+def test_fake_thread_is_created_in_specified_category(fake, default_category):
+    thread = get_fake_thread(fake, default_category)
+    assert thread.category == default_category
+    assert thread.first_post.category == default_category
+
+
+def test_fake_closed_thread_can_be_created(fake, default_category):
+    thread = get_fake_closed_thread(fake, default_category)
+    assert thread.is_closed
+
+
+def test_fake_hidden_thread_can_be_created(fake, default_category):
+    thread = get_fake_hidden_thread(fake, default_category)
+    assert thread.is_hidden
+    assert thread.first_post.is_hidden
+
+
+def test_fake_unapproved_thread_can_be_created(fake, default_category):
+    thread = get_fake_unapproved_thread(fake, default_category)
+    assert thread.is_unapproved
+    assert thread.first_post.is_unapproved
+
+
+def test_different_fake_thread_title_is_used_every_time(fake, default_category):
+    thread_a = get_fake_thread(fake, default_category)
+    thread_b = get_fake_thread(fake, default_category)
+    assert thread_a.title != thread_b.title

+ 13 - 1
misago/faker/tests/test_fake_users.py

@@ -4,6 +4,7 @@ from ..users import (
     PASSWORD,
     get_fake_admin_activated_user,
     get_fake_banned_user,
+    get_fake_deleted_user,
     get_fake_inactive_user,
     get_fake_user,
     get_fake_username,
@@ -24,6 +25,12 @@ def test_fake_user_is_created_with_test_avatars(db, fake):
     assert user.avatars
 
 
+def test_new_fake_user_avatars_are_created_for_every_new_user(db, fake):
+    user_a = get_fake_user(fake)
+    user_b = get_fake_user(fake)
+    assert user_a.avatars != user_b.avatars
+
+
 def test_fake_user_is_created_with_explicit_rank(db, fake):
     rank = Rank.objects.create(name="Test Rank")
     user = get_fake_user(fake, rank)
@@ -45,10 +52,15 @@ def test_admin_activated_fake_user_can_be_created(db, fake):
     assert user.requires_activation
 
 
+def test_deleted_fake_user_can_be_created(db, fake):
+    user = get_fake_deleted_user(fake)
+    assert not user.is_active
+
+
 def test_fake_username_can_be_created(fake):
     assert get_fake_username(fake)
 
 
-def test_different_fake_username_is_created_every_time(fake):
+def test_different_fake_username_is_used_every_time(fake):
     fake_usernames = [get_fake_username(fake) for i in range(5)]
     assert len(fake_usernames) == len(set(fake_usernames))

+ 13 - 11
misago/faker/threads.py

@@ -7,35 +7,37 @@ from .posts import get_fake_hidden_post, get_fake_post, get_fake_unapproved_post
 corpus_short = EnglishCorpus(max_length=150)
 
 
-def get_fake_thread(fake, category, starter):
-    thread = create_fake_thread(fake, category, starter)
+def get_fake_thread(fake, category, starter=None):
+    thread = create_fake_thread(fake, category)
     thread.first_post = get_fake_post(fake, thread, starter)
     thread.save(update_fields=["first_post"])
     return thread
 
 
-def get_fake_closed_thread(fake, category, starter):
-    thread = get_fake_thread(fake, category, starter)
+def get_fake_closed_thread(fake, category, starter=None):
+    thread = get_fake_thread(fake, category)
     thread.is_closed = True
     thread.save(update_fields=["is_closed"])
     return thread
 
 
-def get_fake_hidden_thread(fake, category, starter, hidden_by=None):
-    thread = create_fake_thread(fake, category, starter)
+def get_fake_hidden_thread(fake, category, starter=None, hidden_by=None):
+    thread = create_fake_thread(fake, category)
     thread.first_post = get_fake_hidden_post(fake, thread, starter, hidden_by)
-    thread.save(update_fields=["first_post"])
+    thread.is_hidden = True
+    thread.save(update_fields=["first_post", "is_hidden"])
     return thread
 
 
-def get_fake_unapproved_thread(fake, category, starter):
-    thread = create_fake_thread(fake, category, starter)
+def get_fake_unapproved_thread(fake, category, starter=None):
+    thread = create_fake_thread(fake, category)
     thread.first_post = get_fake_unapproved_post(fake, thread, starter)
-    thread.save(update_fields=["first_post"])
+    thread.is_unapproved = True
+    thread.save(update_fields=["first_post", "is_unapproved"])
     return thread
 
 
-def create_fake_thread(fake, category, starter):
+def create_fake_thread(fake, category):
     started_on = timezone.now()
     thread = Thread(
         category=category,