class AnswerFactory(factory.django.DjangoModelFactory): class Meta: model = Answer user = factory.SubFactory(UserFactory) target = factory.SubFactory(FragmentFactory) content = factory.LazyFunction(lambda: generate_random_string(length=512))
class AvatarFactory(factory.django.DjangoModelFactory): # For special purposes: testing, etc. class Meta: model = Avatar django_get_or_create = [ 'user', ] user = factory.SubFactory(UserFactory) profile_image = factory.LazyFunction(lambda: SimpleUploadedFile( name=f'{ generate_random_string(length=16) }.jpg', content=b'\x00' * random.randint(1, 65536), content_type='image/jpeg')) display_name = factory.LazyFunction( lambda: generate_random_string(length=16)) @factory.post_generation def introduce_message(self, create, extracted, **kwargs): if not create: return if extracted: self.introduce_message = extracted else: self.introduce_message = generate_random_string(length=64)
class UserFactory(factory.django.DjangoModelFactory): # Avatar is created with user by signal class Meta: model = User username = factory.LazyFunction(lambda: generate_random_string(length=16))
class ChatFactory(factory.django.DjangoModelFactory): class Meta: model = Chat user = factory.SubFactory(UserFactory) room = factory.SubFactory(RoomFactory) content = factory.LazyFunction(lambda: generate_random_string(length=64))
def introduce_message(self, create, extracted, **kwargs): if not create: return if extracted: self.introduce_message = extracted else: self.introduce_message = generate_random_string(length=64)
class NotificationFactory(factory.django.DjangoModelFactory): class Meta: model = Notification @factory.post_generation def users(self, create, extracted, **kwargs): if not create: return if isinstance(extracted, User): self.users.add(extracted) elif isinstance(extracted, list): self.users.set(extracted) else: for _ in range(random.randint(1, 10)): self.users.add(UserFactory()) title = factory.LazyFunction(lambda: generate_random_string(length=32)) body = factory.LazyFunction(lambda: generate_random_string(length=96))
class FragmentFactory(factory.django.DjangoModelFactory): class Meta: model = Fragment user = factory.SubFactory(UserFactory) title = factory.LazyFunction(lambda: generate_random_string(length=32)) content = factory.LazyFunction(lambda: generate_random_string(length=1024)) @factory.post_generation def tags(self, create, extracted, **kwargs): if not create: return if isinstance(extracted, Tag): self.tags.add(extracted) elif isinstance(extracted, list): self.tags.set(extracted) else: for _ in range(random.randint(1, 10)): self.tags.add(TagFactory())
def get_data(self, permission, action): return { 'title': 'TEST', 'content': 'TEST', 'tags': [ generate_random_string(length=32) for _ in range(random.randint(1, 6)) ] }
class CommentFactory(factory.django.DjangoModelFactory): class Meta: model = Comment user = factory.SubFactory(UserFactory) target = factory.LazyFunction(lambda: random.choice( get_factories_for_model( Commentable, search_modules=['core.factories'], abstract=True))()) parent = factory.LazyAttribute(lambda o: random.choice( [None, *Comment.objects.filter(target=o.target)]) if random.random() > 0.5 else None) content = factory.LazyFunction(lambda: generate_random_string(length=128))
async def test_chat_websocket_behavior_send_invalid_message_receive_error( self): # two communicator in a room room = RoomFactory() sender = self.get_communicator(room=room.pk) await sender.connect() receiver = self.get_communicator(room=room.pk) await receiver.connect() # send message await sender.send_json_to( data={'content': generate_random_string(length=65536)}) # sender will receive error message response = await sender.receive_json_from(timeout=1) assert 'error' in response # others will get nothing assert await receiver.receive_nothing(timeout=1) # teardown await sender.disconnect() await receiver.disconnect()
class TagFactory(factory.django.DjangoModelFactory): class Meta: model = Tag name = factory.LazyFunction(lambda: generate_random_string(length=16)) is_official = factory.LazyFunction(lambda: random.choice([True, False]))
class RoomFactory(factory.django.DjangoModelFactory): class Meta: model = Room name = factory.LazyFunction(lambda: generate_random_string(length=8)) user = factory.SubFactory(UserFactory)
def get_data(self, permission, action): return {'name': generate_random_string(length=32), 'is_official': True}