From d4eff5aa329add2e71518f9750fb612f02b233f4 Mon Sep 17 00:00:00 2001 From: Eliot Berriot Date: Fri, 4 Jan 2019 14:36:08 +0100 Subject: [PATCH] =?UTF-8?q?Less=20DB=C2=A0queries=20in=20test=20factories?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/funkwhale_api/factories.py | 11 ++++++ api/funkwhale_api/favorites/factories.py | 4 +-- api/funkwhale_api/federation/factories.py | 42 ++++++++--------------- api/funkwhale_api/federation/models.py | 9 +++++ api/funkwhale_api/history/factories.py | 4 +-- api/funkwhale_api/music/factories.py | 25 +++++++------- api/funkwhale_api/playlists/factories.py | 6 ++-- api/funkwhale_api/radios/factories.py | 8 ++--- api/funkwhale_api/users/factories.py | 6 ++-- 9 files changed, 61 insertions(+), 54 deletions(-) diff --git a/api/funkwhale_api/factories.py b/api/funkwhale_api/factories.py index 602037a06..5db75fd58 100644 --- a/api/funkwhale_api/factories.py +++ b/api/funkwhale_api/factories.py @@ -28,3 +28,14 @@ def ManyToManyFromList(field_name): field.add(*extracted) return inner + + +class NoUpdateOnCreate: + """ + Factory boy calls save after the initial create. In most case, this + is not needed, so we disable this behaviour + """ + + @classmethod + def _after_postgeneration(cls, instance, create, results=None): + return diff --git a/api/funkwhale_api/favorites/factories.py b/api/funkwhale_api/favorites/factories.py index d96ef1c15..fcc2f8204 100644 --- a/api/funkwhale_api/favorites/factories.py +++ b/api/funkwhale_api/favorites/factories.py @@ -1,12 +1,12 @@ import factory -from funkwhale_api.factories import registry +from funkwhale_api.factories import registry, NoUpdateOnCreate from funkwhale_api.music.factories import TrackFactory from funkwhale_api.users.factories import UserFactory @registry.register -class TrackFavorite(factory.django.DjangoModelFactory): +class TrackFavorite(NoUpdateOnCreate, factory.django.DjangoModelFactory): track = factory.SubFactory(TrackFactory) user = factory.SubFactory(UserFactory) diff --git a/api/funkwhale_api/federation/factories.py b/api/funkwhale_api/federation/factories.py index cbe0bee85..331a5f5d6 100644 --- a/api/funkwhale_api/federation/factories.py +++ b/api/funkwhale_api/federation/factories.py @@ -7,7 +7,7 @@ from django.conf import settings from django.utils import timezone from django.utils.http import http_date -from funkwhale_api.factories import registry +from funkwhale_api.factories import registry, NoUpdateOnCreate from funkwhale_api.users import factories as user_factories from . import keys, models @@ -67,7 +67,7 @@ def create_user(actor): @registry.register -class Domain(factory.django.DjangoModelFactory): +class Domain(NoUpdateOnCreate, factory.django.DjangoModelFactory): name = factory.Faker("domain_name") class Meta: @@ -76,7 +76,7 @@ class Domain(factory.django.DjangoModelFactory): @registry.register -class ActorFactory(factory.DjangoModelFactory): +class ActorFactory(NoUpdateOnCreate, factory.DjangoModelFactory): public_key = None private_key = None preferred_username = factory.Faker("user_name") @@ -100,6 +100,7 @@ class ActorFactory(factory.DjangoModelFactory): o.domain.name, o.preferred_username ) ) + keys = factory.LazyFunction(keys.get_key_pair) class Meta: model = models.Actor @@ -125,19 +126,9 @@ class ActorFactory(factory.DjangoModelFactory): else: self.user = UserFactory(actor=self, **kwargs) - @factory.post_generation - def keys(self, create, extracted, **kwargs): - if not create: - # Simple build, do nothing. - return - if not extracted: - private, public = keys.get_key_pair() - self.private_key = private.decode("utf-8") - self.public_key = public.decode("utf-8") - @registry.register -class FollowFactory(factory.DjangoModelFactory): +class FollowFactory(NoUpdateOnCreate, factory.DjangoModelFactory): target = factory.SubFactory(ActorFactory) actor = factory.SubFactory(ActorFactory) @@ -149,28 +140,23 @@ class FollowFactory(factory.DjangoModelFactory): @registry.register -class MusicLibraryFactory(factory.django.DjangoModelFactory): +class MusicLibraryFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): actor = factory.SubFactory(ActorFactory) privacy_level = "me" name = factory.Faker("sentence") description = factory.Faker("sentence") uploads_count = 0 fid = factory.Faker("federation_url") + followers_url = factory.LazyAttribute( + lambda o: o.fid + "/followers" if o.fid else None + ) class Meta: model = "music.Library" - @factory.post_generation - def followers_url(self, create, extracted, **kwargs): - if not create: - # Simple build, do nothing. - return - - self.followers_url = extracted or self.fid + "/followers" - @registry.register -class LibraryScan(factory.django.DjangoModelFactory): +class LibraryScan(NoUpdateOnCreate, factory.django.DjangoModelFactory): library = factory.SubFactory(MusicLibraryFactory) actor = factory.SubFactory(ActorFactory) total_files = factory.LazyAttribute(lambda o: o.library.uploads_count) @@ -180,7 +166,7 @@ class LibraryScan(factory.django.DjangoModelFactory): @registry.register -class ActivityFactory(factory.django.DjangoModelFactory): +class ActivityFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): actor = factory.SubFactory(ActorFactory) url = factory.Faker("federation_url") payload = factory.LazyFunction(lambda: {"type": "Create"}) @@ -190,7 +176,7 @@ class ActivityFactory(factory.django.DjangoModelFactory): @registry.register -class InboxItemFactory(factory.django.DjangoModelFactory): +class InboxItemFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): actor = factory.SubFactory(ActorFactory, local=True) activity = factory.SubFactory(ActivityFactory) type = "to" @@ -200,7 +186,7 @@ class InboxItemFactory(factory.django.DjangoModelFactory): @registry.register -class DeliveryFactory(factory.django.DjangoModelFactory): +class DeliveryFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): activity = factory.SubFactory(ActivityFactory) inbox_url = factory.Faker("url") @@ -209,7 +195,7 @@ class DeliveryFactory(factory.django.DjangoModelFactory): @registry.register -class LibraryFollowFactory(factory.DjangoModelFactory): +class LibraryFollowFactory(NoUpdateOnCreate, factory.DjangoModelFactory): target = factory.SubFactory(MusicLibraryFactory) actor = factory.SubFactory(ActorFactory) diff --git a/api/funkwhale_api/federation/models.py b/api/funkwhale_api/federation/models.py index 35b15b667..2fdeaaa76 100644 --- a/api/funkwhale_api/federation/models.py +++ b/api/funkwhale_api/federation/models.py @@ -238,6 +238,15 @@ class Actor(models.Model): ) return data + @property + def keys(self): + return self.private_key, self.public_key + + @keys.setter + def keys(self, v): + self.private_key = v[0].decode("utf-8") + self.public_key = v[1].decode("utf-8") + class InboxItem(models.Model): """ diff --git a/api/funkwhale_api/history/factories.py b/api/funkwhale_api/history/factories.py index 0524eff19..59a701063 100644 --- a/api/funkwhale_api/history/factories.py +++ b/api/funkwhale_api/history/factories.py @@ -1,12 +1,12 @@ import factory -from funkwhale_api.factories import registry +from funkwhale_api.factories import registry, NoUpdateOnCreate from funkwhale_api.music import factories from funkwhale_api.users.factories import UserFactory @registry.register -class ListeningFactory(factory.django.DjangoModelFactory): +class ListeningFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): user = factory.SubFactory(UserFactory) track = factory.SubFactory(factories.TrackFactory) diff --git a/api/funkwhale_api/music/factories.py b/api/funkwhale_api/music/factories.py index 8a7e84a92..cd2a91ccb 100644 --- a/api/funkwhale_api/music/factories.py +++ b/api/funkwhale_api/music/factories.py @@ -2,7 +2,8 @@ import os import factory -from funkwhale_api.factories import ManyToManyFromList, registry +from funkwhale_api.factories import ManyToManyFromList, registry, NoUpdateOnCreate + from funkwhale_api.federation import factories as federation_factories from funkwhale_api.music import licenses from funkwhale_api.users import factories as users_factories @@ -39,7 +40,7 @@ def deduce_from_conf(field): @registry.register -class LicenseFactory(factory.django.DjangoModelFactory): +class LicenseFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): code = "cc-by-4.0" url = deduce_from_conf("url") commercial = deduce_from_conf("commercial") @@ -54,7 +55,7 @@ class LicenseFactory(factory.django.DjangoModelFactory): @registry.register -class ArtistFactory(factory.django.DjangoModelFactory): +class ArtistFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): name = factory.Faker("name") mbid = factory.Faker("uuid4") fid = factory.Faker("federation_url") @@ -65,7 +66,7 @@ class ArtistFactory(factory.django.DjangoModelFactory): @registry.register -class AlbumFactory(factory.django.DjangoModelFactory): +class AlbumFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): title = factory.Faker("sentence", nb_words=3) mbid = factory.Faker("uuid4") release_date = factory.Faker("date_object") @@ -80,7 +81,7 @@ class AlbumFactory(factory.django.DjangoModelFactory): @registry.register -class TrackFactory(factory.django.DjangoModelFactory): +class TrackFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): fid = factory.Faker("federation_url") title = factory.Faker("sentence", nb_words=3) mbid = factory.Faker("uuid4") @@ -104,7 +105,7 @@ class TrackFactory(factory.django.DjangoModelFactory): @registry.register -class UploadFactory(factory.django.DjangoModelFactory): +class UploadFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): fid = factory.Faker("federation_url") track = factory.SubFactory(TrackFactory) library = factory.SubFactory(federation_factories.MusicLibraryFactory) @@ -128,7 +129,7 @@ class UploadFactory(factory.django.DjangoModelFactory): @registry.register -class UploadVersionFactory(factory.django.DjangoModelFactory): +class UploadVersionFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): upload = factory.SubFactory(UploadFactory, bitrate=200000) bitrate = factory.SelfAttribute("upload.bitrate") mimetype = "audio/mpeg" @@ -140,7 +141,7 @@ class UploadVersionFactory(factory.django.DjangoModelFactory): @registry.register -class WorkFactory(factory.django.DjangoModelFactory): +class WorkFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): mbid = factory.Faker("uuid4") language = "eng" nature = "song" @@ -151,7 +152,7 @@ class WorkFactory(factory.django.DjangoModelFactory): @registry.register -class LyricsFactory(factory.django.DjangoModelFactory): +class LyricsFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): work = factory.SubFactory(WorkFactory) url = factory.Faker("url") content = factory.Faker("paragraphs", nb=4) @@ -161,7 +162,7 @@ class LyricsFactory(factory.django.DjangoModelFactory): @registry.register -class TagFactory(factory.django.DjangoModelFactory): +class TagFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): name = factory.SelfAttribute("slug") slug = factory.Faker("slug") @@ -172,7 +173,7 @@ class TagFactory(factory.django.DjangoModelFactory): # XXX To remove -class ImportBatchFactory(factory.django.DjangoModelFactory): +class ImportBatchFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): submitted_by = factory.SubFactory(users_factories.UserFactory) class Meta: @@ -180,7 +181,7 @@ class ImportBatchFactory(factory.django.DjangoModelFactory): @registry.register -class ImportJobFactory(factory.django.DjangoModelFactory): +class ImportJobFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): batch = factory.SubFactory(ImportBatchFactory) source = factory.Faker("url") mbid = factory.Faker("uuid4") diff --git a/api/funkwhale_api/playlists/factories.py b/api/funkwhale_api/playlists/factories.py index ff031945a..acfde10cd 100644 --- a/api/funkwhale_api/playlists/factories.py +++ b/api/funkwhale_api/playlists/factories.py @@ -1,12 +1,12 @@ import factory -from funkwhale_api.factories import registry +from funkwhale_api.factories import registry, NoUpdateOnCreate from funkwhale_api.music.factories import TrackFactory from funkwhale_api.users.factories import UserFactory @registry.register -class PlaylistFactory(factory.django.DjangoModelFactory): +class PlaylistFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): name = factory.Faker("name") user = factory.SubFactory(UserFactory) @@ -15,7 +15,7 @@ class PlaylistFactory(factory.django.DjangoModelFactory): @registry.register -class PlaylistTrackFactory(factory.django.DjangoModelFactory): +class PlaylistTrackFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): playlist = factory.SubFactory(PlaylistFactory) track = factory.SubFactory(TrackFactory) diff --git a/api/funkwhale_api/radios/factories.py b/api/funkwhale_api/radios/factories.py index a83c53737..2ba66483d 100644 --- a/api/funkwhale_api/radios/factories.py +++ b/api/funkwhale_api/radios/factories.py @@ -1,11 +1,11 @@ import factory -from funkwhale_api.factories import registry +from funkwhale_api.factories import registry, NoUpdateOnCreate from funkwhale_api.users.factories import UserFactory @registry.register -class RadioFactory(factory.django.DjangoModelFactory): +class RadioFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): name = factory.Faker("name") description = factory.Faker("paragraphs") user = factory.SubFactory(UserFactory) @@ -16,7 +16,7 @@ class RadioFactory(factory.django.DjangoModelFactory): @registry.register -class RadioSessionFactory(factory.django.DjangoModelFactory): +class RadioSessionFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): user = factory.SubFactory(UserFactory) class Meta: @@ -24,7 +24,7 @@ class RadioSessionFactory(factory.django.DjangoModelFactory): @registry.register(name="radios.CustomRadioSession") -class CustomRadioSessionFactory(factory.django.DjangoModelFactory): +class CustomRadioSessionFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): user = factory.SubFactory(UserFactory) radio_type = "custom" custom_radio = factory.SubFactory( diff --git a/api/funkwhale_api/users/factories.py b/api/funkwhale_api/users/factories.py index 9bef1da1c..a5b113cf4 100644 --- a/api/funkwhale_api/users/factories.py +++ b/api/funkwhale_api/users/factories.py @@ -2,13 +2,13 @@ import factory from django.contrib.auth.models import Permission from django.utils import timezone -from funkwhale_api.factories import ManyToManyFromList, registry +from funkwhale_api.factories import ManyToManyFromList, registry, NoUpdateOnCreate from . import models @registry.register -class GroupFactory(factory.django.DjangoModelFactory): +class GroupFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): name = factory.Sequence(lambda n: "group-{0}".format(n)) class Meta: @@ -32,7 +32,7 @@ class GroupFactory(factory.django.DjangoModelFactory): @registry.register -class InvitationFactory(factory.django.DjangoModelFactory): +class InvitationFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory): owner = factory.LazyFunction(lambda: UserFactory()) class Meta: