-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
English string validation to error code validation
- Loading branch information
Showing
4 changed files
with
131 additions
and
24 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,64 @@ | ||
import pytest | ||
from unittest.mock import MagicMock, patch | ||
from awx.main.tasks.system import update_inventory_computed_fields | ||
from awx.main.models import Inventory | ||
from django.db import DatabaseError | ||
|
||
|
||
@pytest.fixture | ||
def mock_logger(): | ||
with patch("awx.main.tasks.system.logger") as logger: | ||
yield logger | ||
|
||
|
||
@pytest.fixture | ||
def mock_inventory(): | ||
return MagicMock(spec=Inventory) | ||
|
||
|
||
def test_update_inventory_computed_fields_existing_inventory(mock_logger, mock_inventory): | ||
# Mocking the Inventory.objects.filter method to return a non-empty queryset | ||
with patch("awx.main.tasks.system.Inventory.objects.filter") as mock_filter: | ||
mock_filter.return_value.exists.return_value = True | ||
mock_filter.return_value.__getitem__.return_value = mock_inventory | ||
|
||
# Mocking the update_computed_fields method | ||
with patch.object(mock_inventory, "update_computed_fields") as mock_update_computed_fields: | ||
update_inventory_computed_fields(1) | ||
|
||
# Assertions | ||
mock_filter.assert_called_once_with(id=1) | ||
mock_update_computed_fields.assert_called_once() | ||
|
||
# You can add more assertions based on your specific requirements | ||
|
||
|
||
def test_update_inventory_computed_fields_missing_inventory(mock_logger): | ||
# Mocking the Inventory.objects.filter method to return an empty queryset | ||
with patch("awx.main.tasks.system.Inventory.objects.filter") as mock_filter: | ||
mock_filter.return_value.exists.return_value = False | ||
|
||
update_inventory_computed_fields(1) | ||
|
||
# Assertions | ||
mock_filter.assert_called_once_with(id=1) | ||
mock_logger.error.assert_called_once_with("Update Inventory Computed Fields failed due to missing inventory: 1") | ||
|
||
|
||
def test_update_inventory_computed_fields_database_error_nosqlstate(mock_logger, mock_inventory): | ||
# Mocking the Inventory.objects.filter method to return a non-empty queryset | ||
with patch("awx.main.tasks.system.Inventory.objects.filter") as mock_filter: | ||
mock_filter.return_value.exists.return_value = True | ||
mock_filter.return_value.__getitem__.return_value = mock_inventory | ||
|
||
# Mocking the update_computed_fields method | ||
with patch.object(mock_inventory, "update_computed_fields") as mock_update_computed_fields: | ||
# Simulating the update_computed_fields method to explicitly raise a DatabaseError | ||
mock_update_computed_fields.side_effect = DatabaseError("Some error") | ||
|
||
update_inventory_computed_fields(1) | ||
|
||
# Assertions | ||
mock_filter.assert_called_once_with(id=1) | ||
mock_update_computed_fields.assert_called_once() | ||
mock_inventory.update_computed_fields.assert_called_once() |
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