-
Notifications
You must be signed in to change notification settings - Fork 0
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
[#37] Explicitly handle transactions in the runner #38
Open
swrichards
wants to merge
1
commit into
main
Choose a base branch
from
runner-managed-db-transactions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,105 @@ | ||
from unittest import mock | ||
|
||
from django.contrib.auth.models import User | ||
|
||
import pytest | ||
|
||
from django_setup_configuration.configuration import BaseConfigurationStep | ||
from django_setup_configuration.models import ConfigurationModel | ||
from tests.conftest import TestStep | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
def side_effect_test_func(): | ||
pass | ||
|
||
|
||
class TransactionTestConfigurationModel(ConfigurationModel): | ||
|
||
username: str | ||
|
||
|
||
class TransactionTestConfigurationStep( | ||
BaseConfigurationStep[TransactionTestConfigurationModel] | ||
): | ||
|
||
config_model = TransactionTestConfigurationModel | ||
enable_setting = "transaction_test_configuration_enabled" | ||
namespace = "transaction_test_configuration" | ||
verbose_name = "Transaction Test Configuration" | ||
|
||
def execute(self, model) -> None: | ||
User.objects.create_user( | ||
username=model.username, | ||
password="secret", | ||
) | ||
|
||
side_effect_test_func() | ||
|
||
|
||
@pytest.fixture() | ||
def valid_config_object(test_step_valid_config): | ||
return { | ||
"transaction_test_configuration_enabled": True, | ||
"transaction_test_configuration": {"username": "alice"}, | ||
} | test_step_valid_config | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not familiar with this, isn't |
||
|
||
|
||
def test_runner_rolls_back_all_on_failing_step( | ||
runner_factory, valid_config_object, step_execute_mock | ||
): | ||
runner = runner_factory( | ||
steps=[TransactionTestConfigurationStep, TestStep], | ||
object_source=valid_config_object, | ||
) | ||
exc = Exception() | ||
step_execute_mock.side_effect = exc | ||
|
||
user_configuration_step_result, test_step_result = runner.execute_all() | ||
|
||
# Initial run is rolled back, so no objects created | ||
assert test_step_result.has_run | ||
assert test_step_result.run_exception is exc | ||
|
||
assert user_configuration_step_result.has_run | ||
assert user_configuration_step_result.run_exception is None | ||
assert User.objects.count() == 0 | ||
|
||
# Subsequent run does not raise, so the objects are created | ||
step_execute_mock.side_effect = None | ||
|
||
user_configuration_step_result, test_step_result = runner.execute_all() | ||
|
||
assert test_step_result.has_run | ||
assert test_step_result.run_exception is None | ||
|
||
assert user_configuration_step_result.has_run | ||
assert user_configuration_step_result.run_exception is None | ||
assert User.objects.count() == 1 | ||
|
||
|
||
def test_runner_rolls_back_on_executing_single_step( | ||
runner_factory, valid_config_object | ||
): | ||
runner = runner_factory( | ||
steps=[TransactionTestConfigurationStep, TestStep], | ||
object_source=valid_config_object, | ||
) | ||
with mock.patch("tests.test_transactions.side_effect_test_func") as m: | ||
exc = Exception() | ||
m.side_effect = exc | ||
|
||
user_configuration_step_result = runner._execute_step( | ||
runner.configured_steps[0] | ||
) | ||
|
||
assert user_configuration_step_result.has_run | ||
assert user_configuration_step_result.run_exception is exc | ||
assert User.objects.count() == 0 | ||
|
||
user_configuration_step_result = runner._execute_step(runner.configured_steps[0]) | ||
|
||
assert user_configuration_step_result.has_run | ||
assert user_configuration_step_result.run_exception is None | ||
assert User.objects.count() == 1 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If I try this locally (for instance with Open Notificaties and have an incorrect service identifier) and add a breakpoint at line 227, the command output is:
And it seems that line 227 is never reached, probably because the entire thing crashes due to the
CommandError
which isn't caught. Is this a mistake in the implementation of the step innotifications_api_common
or should that be handled here?