-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
fix and refactor migrations test teardown and setup #76
Conversation
This reverts commit 55a5261.
Move ``_Migration`` and ``_MigrationSpec`` to newly introduced module and rename them to ``MigrationTarget`` and ``MigrationSpec``. Fix type hints of ``MigratorTestCase`` class attributes: ``migrate_from`` and ``migrate_to``.
Codecov Report
@@ Coverage Diff @@
## master #76 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 5 9 +4
Lines 113 189 +76
Branches 13 19 +6
=========================================
+ Hits 113 189 +76
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing work!
This is a very complicated feature, but a very simple and thoughtful implementation.
Really amazing 👍
However, there is some polishing to be done!
django_test_migrations/plan.py
Outdated
targets: List[MigrationTarget], | ||
plan: MigrationPlan, | ||
) -> MigrationPlan: | ||
"""Truncate migrations ``plan`` up to ``targets``.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add some more docs on why do we need this function.
It is not clear from the source code (at least for me).
django_test_migrations/sql.py
Outdated
|
||
_Connection = Union[DefaultConnectionProxy, BaseDatabaseWrapper] | ||
|
||
DJANGO_MIGRATIONS_TABLE_NAME = 'django_migrations' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be Final
?
include_views=False, | ||
) | ||
sql_drop_tables = [ | ||
connection.SchemaEditorClass.sql_delete_table % { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this operation safe in terms of sql-injection? Maybe there are safer methods to join sql vars?
Should we even care?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be safe, because sql_delete_table
is string format defined by database backend (no user input) and its table
argument is table's name (not any users input again), so it should be save.
This implementation is based on https://github.com/django/django/blob/c226c6cb3209122b6732fd501e2994c408dc258e/django/db/backends/base/schema.py#L342
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
django_test_migrations/sql.py
Outdated
database_name: str, | ||
style: Optional[Style] = None, | ||
) -> None: | ||
"""Flush `django_migrations` table.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also explain why do we handle it separately.
django_test_migrations/sql.py
Outdated
|
||
This function is copy of ``connection.ops.execute_sql_flush`` | ||
method from Django's source code: | ||
https://github.com/django/django/blob/227d0c7365cfd0a64d021cb9bdcf77bed2d3f170/django/db/backends/base/operations.py#L401 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's use bit.ly
for this link.
django_test_migrations/sql.py
Outdated
method from Django's source code: | ||
https://github.com/django/django/blob/227d0c7365cfd0a64d021cb9bdcf77bed2d3f170/django/db/backends/base/operations.py#L401 | ||
to make `django-test-migrations` compatible with `Django==1.11`. | ||
``connection.ops.execute_sql_flush()`` was introduced in `Django==2.0`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Django==2.0
should be in double backquotes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surely I will correct it.
To be honest I don't feel the difference between single backquote and double backquotes.
I would be really grateful if you could explain it ;)
tests/test_plan/conftest.py
Outdated
|
||
|
||
@pytest.fixture() | ||
def plan(mocker): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need a mocker
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leftover from some "first attempts" code, thanks for catching!
|
||
|
||
def test_drop_models_table_table_detected(mocker): | ||
"""Ensure `DROP TABLE` statements are executed when any table detected.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be in double backquotes.
|
||
|
||
def test_drop_models_table_no_tables_detected(mocker): | ||
"""Ensure any `DROP TABLE` statement executed when no tables detected.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be in double backquotes.
Add more detailed docstrings. Fix backquotes usage in docstrings. Use bit.ly for long URLs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot! Merging! 🎉
include_views=False, | ||
) | ||
sql_drop_tables = [ | ||
connection.SchemaEditorClass.sql_delete_table % { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Closes #38
Closes #40
This commit implements new migrations test teardown and setup.
Teardown simply call
migrate
command, but on already tested database.Setup is more complicated but more similar to real case of running migrations on production (I described it pros in #38) :
django_migrations
tablemigrate_from
targetsThis part related to custom plan creation is necessary to make API developers' friendly just to not force them to include migrations targets for all models they want to use in test.
Generated plan cause also
.after()
applies migrations more like its done on production (only these migrations that aren't applied yet will be applied - on production all older migrations are already applied), because it is just truncated full (clean) migrations plan.