File: test_performance.py

package info (click to toggle)
python-django-zeal 2.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 284 kB
  • sloc: python: 1,387; makefile: 25; sh: 9
file content (64 lines) | stat: -rw-r--r-- 2,285 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pytest
from djangoproject.social.models import Post, Profile, User
from zeal import zeal_context, zeal_ignore

from .factories import PostFactory, ProfileFactory, UserFactory

pytestmark = [pytest.mark.nozeal, pytest.mark.django_db]


def test_performance(benchmark):
    users = UserFactory.create_batch(10)

    # everyone follows everyone
    user_following_relations = []
    for user in users:
        for followee in users:
            if user == followee:
                continue
            user_following_relations.append(
                User.following.through(
                    from_user_id=user.id, to_user_id=followee.id
                )
            )
    User.following.through.objects.bulk_create(user_following_relations)

    # give everyone a profile
    for user in users:
        ProfileFactory(user=user)

    # everyone has 10 posts
    for user in users:
        PostFactory.create_batch(10, author=user)

    @benchmark
    def _run_benchmark():
        with (
            zeal_context(),
            zeal_ignore(),
        ):
            # Test forward & reverse many-to-one relationships (Post -> User, User -> Posts)
            posts = Post.objects.all()
            for post in posts:
                _ = post.author.username  # forward many-to-one
                _ = list(post.author.posts.all())  # reverse many-to-one

            # Test forward & reverse one-to-one relationships (Profile -> User, User -> Profile)
            profiles = Profile.objects.all()
            for profile in profiles:
                _ = profile.user.username  # forward one-to-one
                _ = profile.user.profile.display_name  # reverse one-to-one

            # Test forward & reverse many-to-many relationships
            users = User.objects.all()
            for user in users:
                _ = list(user.following.all())  # forward many-to-many
                _ = list(user.followers.all())  # reverse many-to-many
                _ = list(
                    user.blocked.all()
                )  # many-to-many without related_name

                # Test chained relationships
                for follower in user.followers.all():
                    _ = follower.profile.display_name
                    _ = list(follower.posts.all())