-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚚(celery) move and rename task files and task functions
Relocating and renaming tasks files `deletion_tasks` and `emailing_tasks` to `tasks.deletion` and `tasks.emailing`. Renaming `delete_user` utility function to `delete_user_from_db`. Renaming `deletion_task` task function to `delete_user`. Renaming `send_email` task function to `warn_user`.
- Loading branch information
Showing
8 changed files
with
96 additions
and
96 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
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 @@ | ||
# noqa: D104 |
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
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 |
---|---|---|
|
@@ -8,11 +8,11 @@ | |
from sqlalchemy import select | ||
from sqlalchemy.exc import SQLAlchemyError | ||
|
||
from mork.celery.deletion_tasks import ( | ||
from mork.celery.tasks.deletion import ( | ||
delete_email_status, | ||
delete_inactive_users, | ||
delete_user, | ||
deletion_task, | ||
delete_user_from_db, | ||
) | ||
from mork.edx import crud | ||
from mork.edx.factories.auth import EdxAuthUserFactory | ||
|
@@ -42,19 +42,19 @@ def test_delete_inactive_users(edx_db, monkeypatch): | |
email="[email protected]", | ||
) | ||
|
||
monkeypatch.setattr("mork.celery.deletion_tasks.OpenEdxDB", lambda *args: edx_db) | ||
monkeypatch.setattr("mork.celery.tasks.deletion.OpenEdxDB", lambda *args: edx_db) | ||
|
||
mock_group = Mock() | ||
monkeypatch.setattr("mork.celery.deletion_tasks.group", mock_group) | ||
mock_deletion_task = Mock() | ||
monkeypatch.setattr("mork.celery.deletion_tasks.deletion_task", mock_deletion_task) | ||
monkeypatch.setattr("mork.celery.tasks.deletion.group", mock_group) | ||
mock_delete_user = Mock() | ||
monkeypatch.setattr("mork.celery.tasks.deletion.delete_user", mock_delete_user) | ||
|
||
delete_inactive_users() | ||
|
||
mock_group.assert_called_once_with( | ||
[ | ||
mock_deletion_task.s(email="[email protected]"), | ||
mock_deletion_task.s(email="[email protected]"), | ||
mock_delete_user.s(email="[email protected]"), | ||
mock_delete_user.s(email="[email protected]"), | ||
] | ||
) | ||
|
||
|
@@ -71,70 +71,74 @@ def test_delete_inactive_users_with_batch_size(edx_db, monkeypatch): | |
email="[email protected]", | ||
) | ||
|
||
monkeypatch.setattr("mork.celery.deletion_tasks.OpenEdxDB", lambda *args: edx_db) | ||
monkeypatch.setattr("mork.celery.tasks.deletion.OpenEdxDB", lambda *args: edx_db) | ||
|
||
mock_group = Mock() | ||
monkeypatch.setattr("mork.celery.deletion_tasks.group", mock_group) | ||
mock_deletion_task = Mock() | ||
monkeypatch.setattr("mork.celery.deletion_tasks.deletion_task", mock_deletion_task) | ||
monkeypatch.setattr("mork.celery.tasks.deletion.group", mock_group) | ||
mock_delete_user = Mock() | ||
monkeypatch.setattr("mork.celery.tasks.deletion.delete_user", mock_delete_user) | ||
|
||
# Set batch size to 1 | ||
monkeypatch.setattr("mork.celery.deletion_tasks.settings.EDX_QUERY_BATCH_SIZE", 1) | ||
monkeypatch.setattr("mork.celery.tasks.deletion.settings.EDX_QUERY_BATCH_SIZE", 1) | ||
|
||
delete_inactive_users() | ||
|
||
mock_group.assert_has_calls( | ||
[ | ||
call( | ||
[ | ||
mock_deletion_task.s(email="[email protected]"), | ||
mock_delete_user.s(email="[email protected]"), | ||
] | ||
), | ||
call().delay(), | ||
call( | ||
[ | ||
mock_deletion_task.s(email="[email protected]"), | ||
mock_delete_user.s(email="[email protected]"), | ||
] | ||
), | ||
call().delay(), | ||
] | ||
) | ||
|
||
|
||
def test_deletion_task(monkeypatch): | ||
"""Test the `deletion_task` function.""" | ||
mock_delete_user = Mock() | ||
monkeypatch.setattr("mork.celery.deletion_tasks.delete_user", mock_delete_user) | ||
def test_delete_user(monkeypatch): | ||
"""Test the `delete_user` function.""" | ||
mock_delete_user_from_db = Mock() | ||
monkeypatch.setattr( | ||
"mork.celery.tasks.deletion.delete_user_from_db", mock_delete_user_from_db | ||
) | ||
mock_delete_email_status = Mock() | ||
monkeypatch.setattr( | ||
"mork.celery.deletion_tasks.delete_email_status", mock_delete_email_status | ||
"mork.celery.tasks.deletion.delete_email_status", mock_delete_email_status | ||
) | ||
email = "[email protected]" | ||
deletion_task(email) | ||
delete_user(email) | ||
|
||
mock_delete_user.assert_called_once_with(email) | ||
mock_delete_user_from_db.assert_called_once_with(email) | ||
mock_delete_email_status.assert_called_once_with(email) | ||
|
||
|
||
def test_deletion_task_delete_failure(monkeypatch): | ||
"""Test the `deletion_task` function with a delete failure.""" | ||
def test_delete_user_failure(monkeypatch): | ||
"""Test the `delete_user` function with a delete failure.""" | ||
|
||
def mock_delete(*args): | ||
def mock_delete_user_from_db(*args): | ||
raise UserDeleteError("An error occurred") | ||
|
||
monkeypatch.setattr("mork.celery.deletion_tasks.delete_user", mock_delete) | ||
monkeypatch.setattr( | ||
"mork.celery.tasks.deletion.delete_user_from_db", mock_delete_user_from_db | ||
) | ||
|
||
with pytest.raises(UserDeleteError, match="An error occurred"): | ||
deletion_task("[email protected]") | ||
delete_user("[email protected]") | ||
|
||
|
||
def test_delete_user(edx_db, monkeypatch): | ||
"""Test the `delete_user` function.""" | ||
def test_delete_user_from_db(edx_db, monkeypatch): | ||
"""Test the `delete_user_from_db` function.""" | ||
EdxAuthUserFactory._meta.sqlalchemy_session = edx_db.session | ||
EdxAuthUserFactory.create(email="[email protected]") | ||
EdxAuthUserFactory.create(email="[email protected]") | ||
|
||
monkeypatch.setattr("mork.celery.deletion_tasks.OpenEdxDB", lambda *args: edx_db) | ||
monkeypatch.setattr("mork.celery.tasks.deletion.OpenEdxDB", lambda *args: edx_db) | ||
|
||
assert crud.get_user( | ||
edx_db.session, | ||
|
@@ -145,7 +149,7 @@ def test_delete_user(edx_db, monkeypatch): | |
email="[email protected]", | ||
) | ||
|
||
delete_user(email="[email protected]") | ||
delete_user_from_db(email="[email protected]") | ||
|
||
assert not crud.get_user( | ||
edx_db.session, | ||
|
@@ -157,19 +161,19 @@ def test_delete_user(edx_db, monkeypatch): | |
) | ||
|
||
|
||
def test_delete_user_with_failure(edx_db, monkeypatch): | ||
"""Test the `delete_user` function with a commit failure.""" | ||
def test_delete_user_from_db_with_failure(edx_db, monkeypatch): | ||
"""Test the `delete_user_from_db` function with a commit failure.""" | ||
EdxAuthUserFactory._meta.sqlalchemy_session = edx_db.session | ||
EdxAuthUserFactory.create(email="[email protected]") | ||
|
||
def mock_session_commit(): | ||
raise SQLAlchemyError("An error occurred") | ||
|
||
edx_db.session.commit = mock_session_commit | ||
monkeypatch.setattr("mork.celery.deletion_tasks.OpenEdxDB", lambda *args: edx_db) | ||
monkeypatch.setattr("mork.celery.tasks.deletion.OpenEdxDB", lambda *args: edx_db) | ||
|
||
with pytest.raises(UserDeleteError, match="Failed to delete user."): | ||
delete_user(email="[email protected]") | ||
delete_user_from_db(email="[email protected]") | ||
|
||
|
||
def test_delete_email_status(db_session, monkeypatch): | ||
|
@@ -179,7 +183,7 @@ class MockMorkDB: | |
session = db_session | ||
|
||
EmailStatusFactory._meta.sqlalchemy_session = db_session | ||
monkeypatch.setattr("mork.celery.deletion_tasks.MorkDB", MockMorkDB) | ||
monkeypatch.setattr("mork.celery.tasks.deletion.MorkDB", MockMorkDB) | ||
|
||
email = "[email protected]" | ||
EmailStatusFactory.create(email=email) | ||
|
@@ -203,7 +207,7 @@ class MockMorkDB: | |
session = db_session | ||
|
||
EmailStatusFactory._meta.sqlalchemy_session = db_session | ||
monkeypatch.setattr("mork.celery.deletion_tasks.MorkDB", MockMorkDB) | ||
monkeypatch.setattr("mork.celery.tasks.deletion.MorkDB", MockMorkDB) | ||
|
||
email = "[email protected]" | ||
|
||
|
@@ -212,7 +216,7 @@ class MockMorkDB: | |
delete_email_status(email) | ||
|
||
assert ( | ||
"mork.celery.deletion_tasks", | ||
"mork.celery.tasks.deletion", | ||
logging.WARNING, | ||
"Mork DB - No user found with email='[email protected]' for deletion", | ||
) in caplog.record_tuples | ||
|
@@ -230,7 +234,7 @@ class MockMorkDB: | |
session = db_session | ||
|
||
EmailStatusFactory._meta.sqlalchemy_session = db_session | ||
monkeypatch.setattr("mork.celery.deletion_tasks.MorkDB", MockMorkDB) | ||
monkeypatch.setattr("mork.celery.tasks.deletion.MorkDB", MockMorkDB) | ||
|
||
email = "[email protected]" | ||
EmailStatusFactory.create(email=email) | ||
|
@@ -240,7 +244,7 @@ class MockMorkDB: | |
delete_email_status(email) | ||
|
||
assert ( | ||
"mork.celery.deletion_tasks", | ||
"mork.celery.tasks.deletion", | ||
logging.ERROR, | ||
"Mork DB - Failed to delete user with email='[email protected]':" | ||
" An error occurred", | ||
|
Oops, something went wrong.