mirror of
https://github.com/ansible/awx.git
synced 2026-03-15 07:57:29 -02:30
convert py2 -> py3
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import mock
|
||||
from unittest import mock
|
||||
import pytest
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Python
|
||||
import pytest
|
||||
import mock
|
||||
from mock import PropertyMock
|
||||
from unittest import mock
|
||||
from unittest.mock import PropertyMock
|
||||
|
||||
# AWX
|
||||
from awx.api.serializers import (
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
# Python
|
||||
from collections import namedtuple
|
||||
import pytest
|
||||
import mock
|
||||
from unittest import mock
|
||||
import json
|
||||
|
||||
from six.moves import xrange
|
||||
|
||||
# AWX
|
||||
from awx.api.serializers import (
|
||||
JobDetailSerializer,
|
||||
@@ -47,12 +45,12 @@ def job(mocker, job_template, project_update):
|
||||
|
||||
@pytest.fixture
|
||||
def labels(mocker):
|
||||
return [Label(id=x, name='label-%d' % x) for x in xrange(0, 25)]
|
||||
return [Label(id=x, name='label-%d' % x) for x in range(0, 25)]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def jobs(mocker):
|
||||
return [Job(id=x, name='job-%d' % x) for x in xrange(0, 25)]
|
||||
return [Job(id=x, name='job-%d' % x) for x in range(0, 25)]
|
||||
|
||||
|
||||
@mock.patch('awx.api.serializers.UnifiedJobTemplateSerializer.get_related', lambda x,y: {})
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
# Python
|
||||
import pytest
|
||||
import mock
|
||||
|
||||
from six.moves import xrange
|
||||
from unittest import mock
|
||||
|
||||
# AWX
|
||||
from awx.api.serializers import (
|
||||
@@ -41,7 +39,7 @@ def job(mocker, job_template):
|
||||
|
||||
@pytest.fixture
|
||||
def jobs(mocker):
|
||||
return [Job(id=x, name='job-%d' % x) for x in xrange(0, 25)]
|
||||
return [Job(id=x, name='job-%d' % x) for x in range(0, 25)]
|
||||
|
||||
|
||||
@mock.patch('awx.api.serializers.UnifiedJobTemplateSerializer.get_related', lambda x,y: {})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Python
|
||||
import pytest
|
||||
import mock
|
||||
from unittest import mock
|
||||
|
||||
# AWX
|
||||
from awx.api.serializers import (
|
||||
|
||||
@@ -66,7 +66,7 @@ def test_invalid_field():
|
||||
field_lookup = FieldLookupBackend()
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
field_lookup.value_to_python(WorkflowJobTemplate, invalid_field, 'foo')
|
||||
assert 'is not an allowed field name. Must be ascii encodable.' in excinfo.value.message
|
||||
assert 'is not an allowed field name. Must be ascii encodable.' in str(excinfo.value)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('lookup_suffix', ['', 'contains', 'startswith', 'in'])
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
# Python
|
||||
import pytest
|
||||
import mock
|
||||
from unittest import mock
|
||||
|
||||
# DRF
|
||||
from rest_framework import status
|
||||
@@ -12,7 +12,6 @@ from rest_framework.exceptions import PermissionDenied
|
||||
from awx.api.generics import (
|
||||
ParentMixin,
|
||||
SubListCreateAttachDetachAPIView, SubListAttachDetachAPIView,
|
||||
DeleteLastUnattachLabelMixin,
|
||||
ResourceAccessList,
|
||||
ListAPIView
|
||||
)
|
||||
@@ -30,13 +29,6 @@ def get_object_or_400(mocker):
|
||||
return mocker.patch('awx.api.generics.get_object_or_400')
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_response_new(mocker):
|
||||
m = mocker.patch('awx.api.generics.Response.__new__')
|
||||
m.return_value = m
|
||||
return m
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_organization():
|
||||
return Organization(pk=4, name="Unsaved Org")
|
||||
@@ -76,33 +68,26 @@ class TestSubListCreateAttachDetachAPIView:
|
||||
|
||||
assert type(res) is Response
|
||||
|
||||
def test_attach_create_and_associate(self, mocker, get_object_or_400, parent_relationship_factory, mock_response_new):
|
||||
def test_attach_create_and_associate(self, mocker, get_object_or_400, parent_relationship_factory):
|
||||
(serializer, mock_parent_relationship) = parent_relationship_factory(SubListCreateAttachDetachAPIView, 'wife')
|
||||
create_return_value = mocker.MagicMock(status_code=status.HTTP_201_CREATED)
|
||||
serializer.create = mocker.Mock(return_value=create_return_value)
|
||||
|
||||
mock_request = mocker.MagicMock(data=dict())
|
||||
ret = serializer.attach(mock_request, None, None)
|
||||
serializer.attach(mock_request, None, None)
|
||||
|
||||
assert ret == mock_response_new
|
||||
serializer.create.assert_called_with(mock_request, None, None)
|
||||
mock_parent_relationship.wife.add.assert_called_with(get_object_or_400.return_value)
|
||||
mock_response_new.assert_called_with(
|
||||
Response, create_return_value.data, status=status.HTTP_201_CREATED,
|
||||
headers={'Location': create_return_value['Location']}
|
||||
)
|
||||
|
||||
def test_attach_associate_only(self, mocker, get_object_or_400, parent_relationship_factory, mock_response_new):
|
||||
def test_attach_associate_only(self, mocker, get_object_or_400, parent_relationship_factory):
|
||||
(serializer, mock_parent_relationship) = parent_relationship_factory(SubListCreateAttachDetachAPIView, 'wife')
|
||||
serializer.create = mocker.Mock(return_value=mocker.MagicMock())
|
||||
|
||||
mock_request = mocker.MagicMock(data=dict(id=1))
|
||||
ret = serializer.attach(mock_request, None, None)
|
||||
serializer.attach(mock_request, None, None)
|
||||
|
||||
assert ret == mock_response_new
|
||||
serializer.create.assert_not_called()
|
||||
mock_parent_relationship.wife.add.assert_called_with(get_object_or_400.return_value)
|
||||
mock_response_new.assert_called_with(Response, status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
def test_unattach_validate_ok(self, mocker):
|
||||
mock_request = mocker.MagicMock(data=dict(id=1))
|
||||
@@ -183,44 +168,6 @@ def test_attach_detatch_only(mocker):
|
||||
assert 'field is missing' in resp.data['msg']
|
||||
|
||||
|
||||
class TestDeleteLastUnattachLabelMixin:
|
||||
@mock.patch('__builtin__.super')
|
||||
def test_unattach_ok(self, super, mocker):
|
||||
mock_request = mocker.MagicMock()
|
||||
mock_sub_id = mocker.MagicMock()
|
||||
super.return_value = super
|
||||
super.unattach_validate = mocker.MagicMock(return_value=(mock_sub_id, None))
|
||||
super.unattach_by_id = mocker.MagicMock()
|
||||
|
||||
mock_model = mocker.MagicMock()
|
||||
mock_model.objects.get.return_value = mock_model
|
||||
mock_model.is_detached.return_value = True
|
||||
|
||||
view = DeleteLastUnattachLabelMixin()
|
||||
view.model = mock_model
|
||||
|
||||
view.unattach(mock_request, None, None)
|
||||
|
||||
super.unattach_validate.assert_called_with(mock_request)
|
||||
super.unattach_by_id.assert_called_with(mock_request, mock_sub_id)
|
||||
mock_model.is_detached.assert_called_with()
|
||||
mock_model.objects.get.assert_called_with(id=mock_sub_id)
|
||||
mock_model.delete.assert_called_with()
|
||||
|
||||
@mock.patch('__builtin__.super')
|
||||
def test_unattach_fail(self, super, mocker):
|
||||
mock_request = mocker.MagicMock()
|
||||
mock_response = mocker.MagicMock()
|
||||
super.return_value = super
|
||||
super.unattach_validate = mocker.MagicMock(return_value=(None, mock_response))
|
||||
view = DeleteLastUnattachLabelMixin()
|
||||
|
||||
res = view.unattach(mock_request, None, None)
|
||||
|
||||
super.unattach_validate.assert_called_with(mock_request)
|
||||
assert mock_response == res
|
||||
|
||||
|
||||
class TestParentMixin:
|
||||
def test_get_parent_object(self, mocker, get_object_or_404):
|
||||
parent_mixin = ParentMixin()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import pytest
|
||||
import StringIO
|
||||
from io import StringIO
|
||||
|
||||
# AWX
|
||||
from awx.api.parsers import JSONParser
|
||||
@@ -16,14 +16,14 @@ from rest_framework.exceptions import ParseError
|
||||
]
|
||||
)
|
||||
def test_jsonparser_valid_input(input_, output):
|
||||
input_stream = StringIO.StringIO(input_)
|
||||
input_stream = StringIO(input_)
|
||||
assert JSONParser().parse(input_stream) == output
|
||||
input_stream.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('invalid_input', ['1', '"foobar"', '3.14', '{"foo": "bar",}'])
|
||||
def test_json_parser_invalid_input(invalid_input):
|
||||
input_stream = StringIO.StringIO(invalid_input)
|
||||
input_stream = StringIO(invalid_input)
|
||||
with pytest.raises(ParseError):
|
||||
JSONParser().parse(input_stream)
|
||||
input_stream.close()
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
|
||||
import mock
|
||||
import pytest
|
||||
import requests
|
||||
from copy import deepcopy
|
||||
from unittest import mock
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
@@ -24,13 +23,6 @@ from awx.main.views import handle_error
|
||||
from rest_framework.test import APIRequestFactory
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_response_new(mocker):
|
||||
m = mocker.patch('awx.api.views.Response.__new__')
|
||||
m.return_value = m
|
||||
return m
|
||||
|
||||
|
||||
def test_handle_error():
|
||||
# Assure that templating of error does not raise errors
|
||||
request = APIRequestFactory().get('/fooooo/')
|
||||
@@ -38,7 +30,7 @@ def test_handle_error():
|
||||
|
||||
|
||||
class TestApiRootView:
|
||||
def test_get_endpoints(self, mocker, mock_response_new):
|
||||
def test_get_endpoints(self, mocker):
|
||||
endpoints = [
|
||||
'ping',
|
||||
'config',
|
||||
@@ -72,11 +64,9 @@ class TestApiRootView:
|
||||
]
|
||||
view = ApiVersionRootView()
|
||||
ret = view.get(mocker.MagicMock())
|
||||
|
||||
assert ret == mock_response_new
|
||||
data_arg = mock_response_new.mock_calls[0][1][1]
|
||||
assert ret.status_code == 200
|
||||
for endpoint in endpoints:
|
||||
assert endpoint in data_arg
|
||||
assert endpoint in ret.data
|
||||
|
||||
|
||||
class TestJobTemplateLabelList:
|
||||
|
||||
Reference in New Issue
Block a user