Skip to content

Commit

Permalink
Add tests for different configuration styles
Browse files Browse the repository at this point in the history
  • Loading branch information
danpalmer committed Apr 11, 2021
1 parent d856067 commit 638eefc
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 39 deletions.
36 changes: 27 additions & 9 deletions src/devdata/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django.conf import settings as django_settings
from django.utils.module_loading import import_string

from .utils import get_all_models, to_app_model_label

DEFAULT_FIELD_ANONYMISERS = {}
DEFAULT_MODEL_ANONYMISERS = {}
DEFAULT_FAKER_LOCALES = ["en_US"]
Expand All @@ -15,15 +17,31 @@ def strategies(self):

ret = {}

for model, strategies in model_strategies.items():
ret[model] = []
for strategy in strategies:
try:
klass_path, kwargs = strategy
klass = import_string(klass_path)
ret[model].append(klass(**kwargs))
except (ValueError, TypeError, IndexError):
ret[model].append(strategy)
for model in get_all_models():
if model._meta.abstract:
continue

app_model_label = to_app_model_label(model)

ret[app_model_label] = []
strategies = model_strategies.get(app_model_label)

if strategies is None:
default_strategy = getattr(
django_settings,
"DEVDATA_DEFAULT_STRATEGY",
None,
)
if default_strategy is not None:
ret[app_model_label] = [default_strategy]
else:
for strategy in strategies:
try:
klass_path, kwargs = strategy
klass = import_string(klass_path)
ret[app_model_label].append(klass(**kwargs))
except (ValueError, TypeError, IndexError):
ret[app_model_label].append(strategy)

return ret

Expand Down
4 changes: 2 additions & 2 deletions tests/test_infrastructure/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

ALL_TEST_STRATEGIES = (
("admin.LogEntry", "default"),
("auth.Permission", "default"),
("auth.Permission", "replaced"),
("auth.Group_permissions", "default"),
("auth.Group", "default"),
("auth.User_groups", "default"),
("auth.User_user_permissions", "default"),
("contenttypes.ContentType", "default"),
("contenttypes.ContentType", "replaced"),
("sessions.Session", "default"),
("polls.Question", "default"),
("polls.Choice", "default"),
Expand Down
65 changes: 37 additions & 28 deletions tests/testsite/testsite/settings/devdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,49 @@
DEVDATA_FIELD_ANONYMISERS = {}
DEVDATA_MODEL_ANONYMISERS = {}

DEVDATA_DEFAULT_STRATEGY = QuerySetStrategy(name="default")

DEVDATA_FAKER_LOCALES = ["en_GB", "de"]

DEVDATA_STRATEGIES = {
"admin.LogEntry": [
("devdata.strategies.QuerySetStrategy", {"name": "default"}),
],
"auth.Permission": [
("devdata.strategies.QuerySetStrategy", {"name": "default"}),
],
"auth.Group_permissions": [
("devdata.strategies.QuerySetStrategy", {"name": "default"}),
],
"auth.Group": [
("devdata.strategies.QuerySetStrategy", {"name": "default"}),
],
"auth.User_groups": [
("devdata.strategies.QuerySetStrategy", {"name": "default"}),
],
"auth.User_user_permissions": [
("devdata.strategies.QuerySetStrategy", {"name": "default"}),
],
###
# Important: If updating behaviour here, remember to update
# `test_infrastructure.base.ALL_TEST_STRATEGIES` which defines which
# exported files are created for tests.
###
# Admin/Groups system is left to the default, we don't use these for
# tests.
# admin.LogEntry
# auth.Group_permissions
# auth.Group
# auth.User_groups
# auth.User_user_permissions
###
# Content Types can be slightly tricky, we need to be careful to replace any
# default Django provided entries with our own that have the correct PKs.
# Same goes for Permission.
"contenttypes.ContentType": [
("devdata.strategies.QuerySetStrategy", {"name": "default"}),
],
"sessions.Session": [
("devdata.strategies.QuerySetStrategy", {"name": "default"}),
],
# Polls is a very basic import/export example.
"polls.Question": [
QuerySetStrategy(name="default"),
(
"devdata.strategies.DeleteFirstQuerySetStrategy",
{"name": "replaced"},
),
],
"polls.Choice": [
QuerySetStrategy(name="default"),
"auth.Permission": [
(
"devdata.strategies.DeleteFirstQuerySetStrategy",
{"name": "replaced"},
),
],
###
# Do not export sessions, there's little benefit to these, there may be lots
# of them, and they pose a security risk if not correctly anonymised.
"sessions.Session": [],
###
# Polls is a very basic import/export example. We leave these to the default
# strategy.
# polls.Question
# polls.Choice
###
# Photofeed is used to demonstrate restricted exports and anonymising of
# user data. In particular, we customise the users exported to restrict the
# photos and likes exported.
Expand Down

0 comments on commit 638eefc

Please sign in to comment.