Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore some model fields #20

Open
jpic opened this issue Jul 8, 2019 · 0 comments
Open

Ignore some model fields #20

jpic opened this issue Jul 8, 2019 · 0 comments
Assignees

Comments

@jpic
Copy link
Member

jpic commented Jul 8, 2019

When some fields have random data such as secret tokens, they will always arrive in the dumped fixture file because dbdiff relies on Django's dumpdata/loaddata code through call_command().

dbdiff has Fixture.exclude to declare fields that must be ignored from the diff test and that works. But when running with FIXTURE_REWRITE=1, new random values will always arrive in the fixture file, generating un-necessary git diffs, making it painful to commit changes.

For example (to use in dbdiff test) with this model field:

import secrets
class YourModel(models.Model):
    token = models.CharField(
        default=secrets.token_urlsafe,
        max_length=255,
    )

Consider executing with something like:

$ FIXTURE_REWRITE=1 py.test -x -s -vv --strict -r fEsxXw src

All tests pass ok because the env var causes full rewrites of test fixtures files with new dumps. It's now up to the developer to use something like git add -p or git checkout -p to choose which changes to add to the commit.

But the problem with random fields is that it will generate un-necessary changes such as:

$ git diff
diff --git a/src/institution/tests/test_mrsrequest_iframe.json b/src/institution/tests/test_mrsrequest_iframe.json
index d1d9994f..d043d1bc 100644
--- a/src/institution/tests/test_mrsrequest_iframe.json
+++ b/src/institution/tests/test_mrsrequest_iframe.json
@@ -57,7 +57,7 @@
         "status_user": null,
         "suspended": false,
         "taxi_cost": "147.42",
-        "token": "QU3JiKVz-UryoY9a5shJuru2NBt9SQJctyRoHZFEyjc"
+        "token": "o80pqFurRYiiQrnwpLr2MToWuvYpKVpuBEP-_wA0KMA"
     },

To complete this issue, ensure fields in Fixture.exclude (existing feature) are not dumped at all in the dump. You can either monkey-patch the models before call_command(dumpdata) and re-add the field after, either implement your own dumpdata implementation.

@ugkarthik ugkarthik self-assigned this Jul 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants