Skip to content

Instantly share code, notes, and snippets.

View piotrkilczuk's full-sized avatar
🐈
I may be slow to respond.

Piotr Kilczuk piotrkilczuk

🐈
I may be slow to respond.
  • Rzeszów, PL
  • 13:54 (UTC -12:00)
View GitHub Profile
@piotrkilczuk
piotrkilczuk / factories_v3.py
Last active May 20, 2021 10:51
factories_v3.py
class PlayerFactory(DjangoModelFactory):
# other declarations removed for brevity
@lazy_attribute_sequence
def government_id(self, n):
dob_part = int(self.dob.strftime('%y%m%d')) * 10000
# lets start at
# last known government_id for a date + 1 if last known government_id for a date is odd
# last known government_id for a date + 2 if last known government_id for a date is even
@piotrkilczuk
piotrkilczuk / factories_call_v2.py
Last active April 10, 2020 23:25
factories_call_v2.py
>>> from championship.factories import *
>>> players = PlayerFactory.build_batch(10)
>>> club = ClubFactory(players=players)
>>> club.players.count()
10
>>> players[0].club == club
True
@piotrkilczuk
piotrkilczuk / factories_v2.py
Last active May 20, 2021 10:52
factories_v2.py
class ClubFactory(DjangoModelFactory):
name = Faker('company')
class Meta:
model = Club
@post_generation
def players(self, create, extracted, **kwargs):
if not create:
return
@piotrkilczuk
piotrkilczuk / factories_call_v1.py
Last active March 31, 2020 21:26
factory-boy-demystified: calling factories v1
>>> from championship.factories import *
>>> club = ClubFactory()
>>> ten_players = PlayerFactory.create_batch(10, club=club)
>>> tournament = TournamentFactory(players=ten_players)
>>> tournament.players.count()
10
>>> ten_players[0].tournaments.count()
1
@piotrkilczuk
piotrkilczuk / factories_v1.py
Last active May 20, 2021 10:52
factory-boy-demystified: populating many-to-many relationships
class ClubFactory(DjangoModelFactory):
name = Faker('company')
class Meta:
model = Club
class PlayerFactory(DjangoModelFactory):
# other declarations removed for brevity
@piotrkilczuk
piotrkilczuk / m2m.py
Created March 31, 2020 09:45
factory-boy-demystified: populating many-to-many relationships
>>> player = Player.objects.first()
>>> tournament = Tournament.objects.last()
>>> tournament in player.tournaments.all()
False
>>> player.tournaments.add(tournament)
>>> tournament in player.tournaments.all()
True
>>> player in tournament.players.all()
True
@piotrkilczuk
piotrkilczuk / generation_strategies.py
Last active May 20, 2021 09:45
factory-boy-demystified: generation strategies
>>> class ClubFactory(DjangoModelFactory):
... name = Faker('company')
...
... class Meta:
... model = Club
...
>>> club = ClubFactory()
>>> club.pk
3
>>> club = ClubFactory.create()
@piotrkilczuk
piotrkilczuk / models.py
Last active May 19, 2020 22:15
factory-boy-demystified models.py
class Club(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
def picture_upload_path(self, filename):
basename = os.path.basename(filename)
return f'player/picture/{self.id}/{basename}'
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
def qux():
try:
fun()
except Exception as exc:
raise RuntimeError('Qux') from exc
def fun():
raise ValueError('Foo bar baz')