mirror of
https://github.com/ansible/awx.git
synced 2026-05-24 09:07:45 -02:30
fix unit tests and make delete last unattach more generic
This commit is contained in:
@@ -450,6 +450,11 @@ class SubListCreateAttachDetachAPIView(SubListCreateAPIView):
|
|||||||
else:
|
else:
|
||||||
return self.attach(request, *args, **kwargs)
|
return self.attach(request, *args, **kwargs)
|
||||||
|
|
||||||
|
'''
|
||||||
|
Models for which you want the last instance to be deleted from the database
|
||||||
|
when the last disassociate is called should inherit from this class. Further,
|
||||||
|
the model should implement is_detached()
|
||||||
|
'''
|
||||||
class DeleteLastUnattachLabelMixin(object):
|
class DeleteLastUnattachLabelMixin(object):
|
||||||
def unattach(self, request, *args, **kwargs):
|
def unattach(self, request, *args, **kwargs):
|
||||||
(sub_id, res) = super(DeleteLastUnattachLabelMixin, self).unattach_validate(request)
|
(sub_id, res) = super(DeleteLastUnattachLabelMixin, self).unattach_validate(request)
|
||||||
@@ -458,10 +463,10 @@ class DeleteLastUnattachLabelMixin(object):
|
|||||||
|
|
||||||
res = super(DeleteLastUnattachLabelMixin, self).unattach_by_id(request, sub_id)
|
res = super(DeleteLastUnattachLabelMixin, self).unattach_by_id(request, sub_id)
|
||||||
|
|
||||||
label = Label.objects.get(id=sub_id)
|
obj = self.model.objects.get(id=sub_id)
|
||||||
|
|
||||||
if label.is_detached():
|
if obj.is_detached():
|
||||||
label.delete()
|
obj.delete()
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|||||||
@@ -123,28 +123,30 @@ class TestSubListCreateAttachDetachAPIView:
|
|||||||
view.unattach_by_id.assert_not_called()
|
view.unattach_by_id.assert_not_called()
|
||||||
|
|
||||||
class TestDeleteLastUnattachLabelMixin:
|
class TestDeleteLastUnattachLabelMixin:
|
||||||
@mock.patch('awx.api.generics.super')
|
@mock.patch('__builtin__.super')
|
||||||
def test_unattach_ok(self, super, mocker):
|
def test_unattach_ok(self, super, mocker):
|
||||||
mock_request = mocker.MagicMock()
|
mock_request = mocker.MagicMock()
|
||||||
mock_sub_id = mocker.MagicMock()
|
mock_sub_id = mocker.MagicMock()
|
||||||
super.return_value = super
|
super.return_value = super
|
||||||
super.unattach_validate = mocker.MagicMock(return_value=(mock_sub_id, None))
|
super.unattach_validate = mocker.MagicMock(return_value=(mock_sub_id, None))
|
||||||
super.unattach_by_id = mocker.MagicMock()
|
super.unattach_by_id = mocker.MagicMock()
|
||||||
mock_label = mocker.patch('awx.api.generics.Label')
|
|
||||||
mock_label.objects.get.return_value = mock_label
|
mock_model = mocker.MagicMock()
|
||||||
mock_label.is_detached.return_value = True
|
mock_model.objects.get.return_value = mock_model
|
||||||
|
mock_model.is_detached.return_value = True
|
||||||
|
|
||||||
view = DeleteLastUnattachLabelMixin()
|
view = DeleteLastUnattachLabelMixin()
|
||||||
|
view.model = mock_model
|
||||||
|
|
||||||
view.unattach(mock_request, None, None)
|
view.unattach(mock_request, None, None)
|
||||||
|
|
||||||
super.unattach_validate.assert_called_with(mock_request)
|
super.unattach_validate.assert_called_with(mock_request)
|
||||||
super.unattach_by_id.assert_called_with(mock_request, mock_sub_id)
|
super.unattach_by_id.assert_called_with(mock_request, mock_sub_id)
|
||||||
mock_label.is_detached.assert_called_with()
|
mock_model.is_detached.assert_called_with()
|
||||||
mock_label.objects.get.assert_called_with(id=mock_sub_id)
|
mock_model.objects.get.assert_called_with(id=mock_sub_id)
|
||||||
mock_label.delete.assert_called_with()
|
mock_model.delete.assert_called_with()
|
||||||
|
|
||||||
@mock.patch('awx.api.generics.super')
|
@mock.patch('__builtin__.super')
|
||||||
def test_unattach_fail(self, super, mocker):
|
def test_unattach_fail(self, super, mocker):
|
||||||
mock_request = mocker.MagicMock()
|
mock_request = mocker.MagicMock()
|
||||||
mock_response = mocker.MagicMock()
|
mock_response = mocker.MagicMock()
|
||||||
|
|||||||
Reference in New Issue
Block a user