mirror of
https://github.com/ansible/awx.git
synced 2026-03-22 11:25:08 -02:30
Merge pull request #4011 from AlanCoding/sys_aud_test_issue
Remove in-place operations on Models in unit tests
This commit is contained in:
@@ -37,7 +37,7 @@ class Label(CommonModelNameNotUnique):
|
|||||||
return \
|
return \
|
||||||
Label.objects.filter(
|
Label.objects.filter(
|
||||||
organization=None,
|
organization=None,
|
||||||
jobtemplate_labels__isnull=True
|
unifiedjobtemplate_labels__isnull=True
|
||||||
)
|
)
|
||||||
|
|
||||||
def is_detached(self):
|
def is_detached(self):
|
||||||
|
|||||||
@@ -28,12 +28,12 @@ class TestCustomInventoryScriptSerializer(object):
|
|||||||
(False, False, False, None)))
|
(False, False, False, None)))
|
||||||
def test_to_representation_orphan(self, superuser, sysaudit, admin_role, value):
|
def test_to_representation_orphan(self, superuser, sysaudit, admin_role, value):
|
||||||
with mock.patch.object(CustomInventoryScriptSerializer, 'get_summary_fields', return_value={}):
|
with mock.patch.object(CustomInventoryScriptSerializer, 'get_summary_fields', return_value={}):
|
||||||
User.add_to_class('is_system_auditor', sysaudit)
|
with mock.patch.object(User, 'is_system_auditor', return_value=sysaudit):
|
||||||
user = User(username="root", is_superuser=superuser)
|
user = User(username="root", is_superuser=superuser)
|
||||||
roles = [user] if admin_role else []
|
roles = [user] if admin_role else []
|
||||||
|
|
||||||
with mock.patch('awx.main.models.CustomInventoryScript.admin_role', new_callable=PropertyMock, return_value=roles):
|
with mock.patch('awx.main.models.CustomInventoryScript.admin_role', new_callable=PropertyMock, return_value=roles):
|
||||||
cis = CustomInventoryScript(pk=1, script='#!/python')
|
cis = CustomInventoryScript(pk=1, script=value)
|
||||||
serializer = CustomInventoryScriptSerializer()
|
serializer = CustomInventoryScriptSerializer()
|
||||||
|
|
||||||
factory = APIRequestFactory()
|
factory = APIRequestFactory()
|
||||||
|
|||||||
@@ -1,65 +1,66 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
import mock
|
||||||
|
|
||||||
from awx.main.models.label import Label
|
from awx.main.models.label import Label
|
||||||
from awx.main.models.unified_jobs import UnifiedJobTemplate, UnifiedJob
|
from awx.main.models.unified_jobs import UnifiedJobTemplate, UnifiedJob
|
||||||
|
|
||||||
|
|
||||||
def test_get_orphaned_labels(mocker):
|
mock_query_set = mock.MagicMock()
|
||||||
mock_query_set = mocker.MagicMock()
|
|
||||||
Label.objects.filter = mocker.MagicMock(return_value=mock_query_set)
|
|
||||||
|
|
||||||
ret = Label.get_orphaned_labels()
|
mock_objects = mock.MagicMock(filter=mock.MagicMock(return_value=mock_query_set))
|
||||||
|
|
||||||
assert mock_query_set == ret
|
@mock.patch('awx.main.models.label.Label.objects', mock_objects)
|
||||||
Label.objects.filter.assert_called_with(organization=None, jobtemplate_labels__isnull=True)
|
class TestLabelFilterMocked:
|
||||||
|
|
||||||
def test_is_detached(mocker):
|
def test_get_orphaned_labels(self, mocker):
|
||||||
mock_query_set = mocker.MagicMock()
|
ret = Label.get_orphaned_labels()
|
||||||
Label.objects.filter = mocker.MagicMock(return_value=mock_query_set)
|
|
||||||
mock_query_set.count.return_value = 1
|
|
||||||
|
|
||||||
label = Label(id=37)
|
assert mock_query_set == ret
|
||||||
ret = label.is_detached()
|
Label.objects.filter.assert_called_with(organization=None, unifiedjobtemplate_labels__isnull=True)
|
||||||
|
|
||||||
assert ret is True
|
def test_is_detached(self, mocker):
|
||||||
Label.objects.filter.assert_called_with(id=37, unifiedjob_labels__isnull=True, unifiedjobtemplate_labels__isnull=True)
|
mock_query_set.count.return_value = 1
|
||||||
mock_query_set.count.assert_called_with()
|
|
||||||
|
|
||||||
def test_is_detached_not(mocker):
|
label = Label(id=37)
|
||||||
mock_query_set = mocker.MagicMock()
|
ret = label.is_detached()
|
||||||
Label.objects.filter = mocker.MagicMock(return_value=mock_query_set)
|
|
||||||
mock_query_set.count.return_value = 0
|
|
||||||
|
|
||||||
label = Label(id=37)
|
assert ret is True
|
||||||
ret = label.is_detached()
|
Label.objects.filter.assert_called_with(id=37, unifiedjob_labels__isnull=True, unifiedjobtemplate_labels__isnull=True)
|
||||||
|
mock_query_set.count.assert_called_with()
|
||||||
|
|
||||||
assert ret is False
|
def test_is_detached_not(self, mocker):
|
||||||
Label.objects.filter.assert_called_with(id=37, unifiedjob_labels__isnull=True, unifiedjobtemplate_labels__isnull=True)
|
mock_query_set.count.return_value = 0
|
||||||
mock_query_set.count.assert_called_with()
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("jt_count,j_count,expected", [
|
label = Label(id=37)
|
||||||
(1, 0, True),
|
ret = label.is_detached()
|
||||||
(0, 1, True),
|
|
||||||
(1, 1, False),
|
|
||||||
])
|
|
||||||
def test_is_candidate_for_detach(mocker, jt_count, j_count, expected):
|
|
||||||
mock_job_qs = mocker.MagicMock()
|
|
||||||
mock_job_qs.count = mocker.MagicMock(return_value=j_count)
|
|
||||||
UnifiedJob.objects = mocker.MagicMock()
|
|
||||||
UnifiedJob.objects.filter = mocker.MagicMock(return_value=mock_job_qs)
|
|
||||||
|
|
||||||
mock_jt_qs = mocker.MagicMock()
|
assert ret is False
|
||||||
mock_jt_qs.count = mocker.MagicMock(return_value=jt_count)
|
Label.objects.filter.assert_called_with(id=37, unifiedjob_labels__isnull=True, unifiedjobtemplate_labels__isnull=True)
|
||||||
UnifiedJobTemplate.objects = mocker.MagicMock()
|
mock_query_set.count.assert_called_with()
|
||||||
UnifiedJobTemplate.objects.filter = mocker.MagicMock(return_value=mock_jt_qs)
|
|
||||||
|
|
||||||
label = Label(id=37)
|
@pytest.mark.parametrize("jt_count,j_count,expected", [
|
||||||
ret = label.is_candidate_for_detach()
|
(1, 0, True),
|
||||||
|
(0, 1, True),
|
||||||
|
(1, 1, False),
|
||||||
|
])
|
||||||
|
def test_is_candidate_for_detach(self, mocker, jt_count, j_count, expected):
|
||||||
|
mock_job_qs = mocker.MagicMock()
|
||||||
|
mock_job_qs.count = mocker.MagicMock(return_value=j_count)
|
||||||
|
UnifiedJob.objects = mocker.MagicMock()
|
||||||
|
UnifiedJob.objects.filter = mocker.MagicMock(return_value=mock_job_qs)
|
||||||
|
|
||||||
UnifiedJob.objects.filter.assert_called_with(labels__in=[label.id])
|
mock_jt_qs = mocker.MagicMock()
|
||||||
UnifiedJobTemplate.objects.filter.assert_called_with(labels__in=[label.id])
|
mock_jt_qs.count = mocker.MagicMock(return_value=jt_count)
|
||||||
mock_job_qs.count.assert_called_with()
|
UnifiedJobTemplate.objects = mocker.MagicMock()
|
||||||
mock_jt_qs.count.assert_called_with()
|
UnifiedJobTemplate.objects.filter = mocker.MagicMock(return_value=mock_jt_qs)
|
||||||
|
|
||||||
assert ret is expected
|
label = Label(id=37)
|
||||||
|
ret = label.is_candidate_for_detach()
|
||||||
|
|
||||||
|
UnifiedJob.objects.filter.assert_called_with(labels__in=[label.id])
|
||||||
|
UnifiedJobTemplate.objects.filter.assert_called_with(labels__in=[label.id])
|
||||||
|
mock_job_qs.count.assert_called_with()
|
||||||
|
mock_jt_qs.count.assert_called_with()
|
||||||
|
|
||||||
|
assert ret is expected
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ def test_send_notifications_list(mocker):
|
|||||||
mock_job = mocker.MagicMock(spec=UnifiedJob)
|
mock_job = mocker.MagicMock(spec=UnifiedJob)
|
||||||
patches.append(mocker.patch('awx.main.models.UnifiedJob.objects.get', return_value=mock_job))
|
patches.append(mocker.patch('awx.main.models.UnifiedJob.objects.get', return_value=mock_job))
|
||||||
|
|
||||||
mock_notification = mocker.MagicMock(spec=Notification, subject="test", body={ 'hello': 'world' })
|
mock_notification = mocker.MagicMock(spec=Notification, subject="test", body={'hello': 'world'})
|
||||||
patches.append(mocker.patch('awx.main.models.Notification.objects.get', return_value=mock_notification))
|
patches.append(mocker.patch('awx.main.models.Notification.objects.get', return_value=mock_notification))
|
||||||
|
|
||||||
with apply_patches(patches):
|
with apply_patches(patches):
|
||||||
|
|||||||
Reference in New Issue
Block a user