factory_boy, 5 ans après

Presenter Notes

En 2011

Fonctionnalités :

  • Déclarer une factory
  • Choisir build ou create
  • Auto-découverte du modèle de base, parfois

Presenter Notes

En 2011

Déclarations :

  • LazyAttribute(lambda o: o.x + o.y)
  • Sequence(lambda n: int(n))
  • LazyAttributeSequence(lambda o, n: o.x * int(n))

Presenter Notes

En 2011

ORMs :

  • Django
  • Python pur

Presenter Notes

En 2011 : Exemple

class UserFactory(factory.Factory):
    FACTORY_FOR = auth_models.User

    first_name = factory.Sequence(lambda n: "John-%s" % n)
    last_name = factory.Sequence(lambda n: "Doe-%s" % n)
    username = factory.LazyAttribute(
        lambda o: '%s.%s' % (
            o.first_name.lower(),
            o.last_name.lower(),
        ))

Presenter Notes

5 ans plus tard

Presenter Notes

5 ans plus tard

3 mainteneurs :

  • Raphaël Barrois
  • Jeff Widman
  • Remco Gerlich

Presenter Notes

5 ans plus tard

Nouvelles fonctionnalités clés :

  • Héritage
  • Renommage de champs
  • Déclarations post-génération (set_password)
  • Paramétrage du compteur de séquence
  • Création par lots (UserFactory.create_batch(10))
  • Détection des déclarations cycliques

Presenter Notes

5 ans plus tard

Déclarations :

  • LazyFunction(timezone.now)
  • SelfAttribute('.start_date')
  • Iterator(["Ringo", "John", "Paul", "George"])
  • ForeignKey directe (SubFactory)
  • ForeignKey inverse et ManyToMany (RelatedFactory)

Presenter Notes

5 ans plus tard

Aléatoire :

  • Faker (Faker('username'))
  • fuzzy (FuzzyText, FuzzyChoice, ...)

Presenter Notes

5 ans plus tard

Paramétrage :

  • Params
  • Traits
class StationFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = stations_models.Station

    class Params:
        nb_parks = 4
        maintenance = factory.Trait(
            state='maintenance',
            maintenance_log=factory.RelatedFactory(
                    MaintenanceLog, 'station'),
        )

Presenter Notes

5 ans plus tard

ORMs :

  • Django
  • SQLAlchemy
  • Mogo
  • Mongoengine

Presenter Notes

5 ans plus tard

Pour Django :

  • get_or_create
  • FileField
  • ImageField
  • multi-DB
  • mute_signals

Presenter Notes

Et après ?

Presenter Notes

Et après ?

  • Support de plus d'ORMs
  • Auto-déclaration de factories à partir du modèle
  • Nouveaux schémas
  • Notion de "contexte d'appel" (debug, efficacité du get_or_create, transactions, ...)

Presenter Notes

Merci !

Presenter Notes