This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> from championship.factories import * | |
>>> players = PlayerFactory.build_batch(10) | |
>>> club = ClubFactory(players=players) | |
>>> club.players.count() | |
10 | |
>>> players[0].club == club | |
True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ClubFactory(DjangoModelFactory): | |
name = Faker('company') | |
class Meta: | |
model = Club | |
@post_generation | |
def players(self, create, extracted, **kwargs): | |
if not create: | |
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ClubFactory(DjangoModelFactory): | |
name = Faker('company') | |
class Meta: | |
model = Club | |
class PlayerFactory(DjangoModelFactory): | |
# other declarations removed for brevity | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> class ClubFactory(DjangoModelFactory): | |
... name = Faker('company') | |
... | |
... class Meta: | |
... model = Club | |
... | |
>>> club = ClubFactory() | |
>>> club.pk | |
3 | |
>>> club = ClubFactory.create() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def qux(): | |
try: | |
fun() | |
except Exception as exc: | |
raise RuntimeError('Qux') from exc | |
def fun(): | |
raise ValueError('Foo bar baz') | |