-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools: add
generate_fixtures
command
- Loading branch information
Showing
8 changed files
with
22,363 additions
and
102,256 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ docs/build | |
### pykeg | ||
build | ||
dist | ||
test-db.sqlite |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
fixtures: export KEGBOT_DATABASE_URL = sqlite:///test-db.sqlite | ||
fixtures: export KEGBOT_BASE_URL = http://fixture.example.com/ | ||
fixtures: export KEGBOT_ENV = test | ||
fixtures: | ||
rm -f test-db.sqlite | ||
bin/kegbot generate_fixtures | ||
rm -f test-db.sqlite | ||
|
||
.PHONY: fixtures |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import datetime | ||
import itertools | ||
import logging | ||
|
||
from django.conf import settings | ||
from django.core.management import call_command | ||
from django.core.management.base import BaseCommand | ||
from freezegun import freeze_time | ||
|
||
from pykeg.core import defaults, models | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
FAKE_TIME = "2022-03-04" | ||
FIXTURE_PASSWORD = "!unusable-fixture-user-password" | ||
FIXTURE_OUTPUT_FILENAME = "testdata/demo-site.json" | ||
|
||
|
||
class Command(BaseCommand): | ||
f"""Generates the file {FIXTURE_OUTPUT_FILENAME} with fake data.""" | ||
|
||
@freeze_time("2022-03-04") | ||
def handle(self, *args, **options): | ||
if settings.KEGBOT_ENV != "test": | ||
raise ValueError("Can only run this command in test mode!") | ||
call_command("migrate") | ||
self.create_objects() | ||
call_command( | ||
"dumpdata", | ||
"core", | ||
format="json", | ||
indent=2, | ||
output="testdata/demo-site.json", | ||
) | ||
|
||
def create_objects(self): | ||
defaults.set_defaults(set_is_setup=True, create_controller=True) | ||
user = models.User.objects.create( | ||
username="admin", email="[email protected]", is_superuser=True, is_staff=True | ||
) | ||
guest_user = models.User.objects.get(username="guest") | ||
|
||
# If we call `.set_unusable_password()`, we'll get a new random (unusable) | ||
# password every time this script is run. The password for the fake user | ||
# isn't important either way in tests, so just crib an unusable password here. | ||
# (It's not documented, but Django should treat a prefix of "!" as unusable.) | ||
user.password = FIXTURE_PASSWORD | ||
user.save() | ||
guest_user.password = FIXTURE_PASSWORD | ||
guest_user.save() | ||
|
||
producer1 = models.BeverageProducer.objects.create(name="Test Producer 1") | ||
producer2 = models.BeverageProducer.objects.create(name="Test Producer 2") | ||
beverage1 = models.Beverage.objects.create(name="Test Beverage 1", producer=producer1) | ||
beverage2 = models.Beverage.objects.create(name="Test Beverage 2", producer=producer2) | ||
|
||
keg1 = models.Keg.start_keg("kegboard.flow0", beverage1) | ||
keg2 = models.Keg.start_keg("kegboard.flow1", beverage2) | ||
|
||
users = {"alice": None, "bob": None, "carol": None, "mallory": None} | ||
for username in users.keys(): | ||
user = models.User.create_new_user(username=username, email=f"{username}@example.com") | ||
user.password = FIXTURE_PASSWORD | ||
user.activation_key = "" | ||
user.save() | ||
users[username] = user | ||
|
||
delay_generator = itertools.cycle([1, 2, 4, 3, 16, 1440]) | ||
tick_generator = itertools.cycle([120, 200, 300, 500, 25, 500]) | ||
meter_generator = itertools.cycle(["kegboard.flow0", "kegboard.flow0", "kegboard.flow1"]) | ||
user_generator = itertools.cycle(users.values()) | ||
with freeze_time(FAKE_TIME) as frozen_datetime: | ||
for _ in range(50): | ||
user = next(user_generator) | ||
delay = next(delay_generator) | ||
ticks = next(tick_generator) | ||
meter = next(meter_generator) | ||
models.Drink.record_drink(meter, ticks, username=user.username) | ||
frozen_datetime.tick(delta=datetime.timedelta(minutes=delay)) |
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
Oops, something went wrong.