Fix str and dict usages in unittests for Python3

- use six.text_type instead of unicode for assertion;
- use dict.items() instead of dict.iteritems() for dict iteration;
- use six.text_type to capture exception messages, the 'message' attribute
  is no longer existing;
- dict.keys() returns a dict_keys object under Python3 which cannot be
  reversed, explicitly convert it to a list.

Partial-Bug: #1755413

Change-Id: I5d31397b7bfe08b504ee5b9cd4ff894031f66c2b
Signed-off-by: Zhao Chao <zhaochao1984@gmail.com>
This commit is contained in:
Zhao Chao 2018-03-15 10:10:19 +08:00
parent a5cc40f488
commit 996e134a1c
10 changed files with 48 additions and 29 deletions

View File

@ -20,6 +20,8 @@ from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.debug import sensitive_variables # noqa
import six
from horizon import exceptions
from horizon import forms
from horizon import messages
@ -299,10 +301,10 @@ class LaunchForm(forms.SelfHandlingForm):
else:
fields_to_restore_at_the_end[k] = v
for k, v in datastore_flavor_fields.iteritems():
for k, v in datastore_flavor_fields.items():
self.fields[k] = v
for k in reversed(fields_to_restore_at_the_end.keys()):
for k in reversed(list(fields_to_restore_at_the_end.keys())):
self.fields[k] = fields_to_restore_at_the_end[k]
def _add_attr_to_optional_fields(self, datastore, selection_text):
@ -363,7 +365,8 @@ class LaunchForm(forms.SelfHandlingForm):
except Exception as e:
redirect = reverse("horizon:project:database_clusters:index")
exceptions.handle(request,
_('Unable to launch cluster. %s') % e.message,
_('Unable to launch cluster. %s') %
six.text_type(e),
redirect=redirect)
@ -465,7 +468,8 @@ class ClusterAddInstanceForm(forms.SelfHandlingForm):
except Exception as e:
redirect = reverse("horizon:project:database_clusters:index")
exceptions.handle(request,
_('Unable to grow cluster. %s') % e.message,
_('Unable to grow cluster. %s') %
six.text_type(e),
redirect=redirect)
return True
@ -489,5 +493,5 @@ class ResetPasswordForm(forms.SelfHandlingForm):
except Exception as e:
redirect = reverse("horizon:project:database_clusters:index")
exceptions.handle(request, _('Unable to reset password. %s') %
e.message, redirect=redirect)
six.text_type(e), redirect=redirect)
return True

View File

@ -20,6 +20,8 @@ from django.template.defaultfilters import title # noqa
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ungettext_lazy
import six
from horizon import messages
from horizon import tables
from horizon.templatetags import sizeformat
@ -269,10 +271,10 @@ class ClusterShrinkAction(tables.BatchAction):
except Exception as ex:
LOG.error('Action %(action)s failed with %(ex)s for %(data)s' %
{'action': self._get_action_name(past=True).lower(),
'ex': ex.message,
'ex': six.text_type(ex),
'data': display_str})
msg = _('Unable to remove instances from cluster: %s')
messages.error(request, msg % ex.message)
messages.error(request, msg % six.text_type(ex))
return shortcuts.redirect(self.get_success_url(request))
@ -404,10 +406,10 @@ class ClusterGrowAction(tables.Action):
messages.success(request, msg)
except Exception as ex:
LOG.error('Action grow cluster failed with %(ex)s for %(data)s' %
{'ex': ex.message,
{'ex': six.text_type(ex),
'data': display_str})
msg = _('Unable to grow cluster: %s')
messages.error(request, msg % ex.message)
messages.error(request, msg % six.text_type(ex))
finally:
cluster_manager.delete(cluster_id)

View File

@ -653,7 +653,7 @@ class ClustersTests(test.TestCase):
return filtered_datastore_versions
def _contains_datastore_in_attribute(self, field, datastore):
for key, value in field.widget.attrs.iteritems():
for key, value in field.widget.attrs.items():
if datastore in key:
return True
return False

View File

@ -15,6 +15,8 @@
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
import six
from horizon import exceptions
from horizon import forms
from horizon import messages
@ -107,7 +109,7 @@ class CreateConfigurationForm(forms.SelfHandlingForm):
redirect = reverse("horizon:project:database_configurations:index")
exceptions.handle(request, _('Unable to create configuration '
'group. %s')
% e.message, redirect=redirect)
% six.text_type(e), redirect=redirect)
return True
@ -185,5 +187,5 @@ class AddParameterForm(forms.SelfHandlingForm):
except Exception as e:
redirect = reverse("horizon:project:database_configurations:index")
exceptions.handle(request, _('Unable to add new parameter: %s')
% e.message, redirect=redirect)
% six.text_type(e), redirect=redirect)
return True

View File

@ -20,6 +20,8 @@ from django import shortcuts
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ungettext_lazy
import six
from horizon import forms
from horizon import messages
from horizon import tables
@ -141,7 +143,7 @@ class DiscardChanges(tables.Action):
except Exception as ex:
messages.error(
request,
_('Error resetting parameters: %s') % ex.message)
_('Error resetting parameters: %s') % six.text_type(ex))
return shortcuts.redirect(request.build_absolute_uri())

View File

@ -16,6 +16,8 @@ from django.core.urlresolvers import reverse
from django.forms import ValidationError # noqa
from django.utils.translation import ugettext_lazy as _
import six
from horizon import exceptions
from horizon import forms
from horizon import messages
@ -47,7 +49,7 @@ class CreateDatabaseForm(forms.SelfHandlingForm):
redirect = reverse("horizon:project:databases:detail",
args=(instance,))
exceptions.handle(request, _('Unable to create database. %s') %
e.message, redirect=redirect)
six.text_type(e), redirect=redirect)
return True
@ -88,7 +90,7 @@ class ResizeVolumeForm(forms.SelfHandlingForm):
except Exception as e:
redirect = reverse("horizon:project:databases:index")
exceptions.handle(request, _('Unable to resize volume. %s') %
e.message, redirect=redirect)
six.text_type(e), redirect=redirect)
return True
@ -126,7 +128,7 @@ class ResizeInstanceForm(forms.SelfHandlingForm):
except Exception as e:
redirect = reverse("horizon:project:databases:index")
exceptions.handle(request, _('Unable to resize instance. %s') %
e.message, redirect=redirect)
six.text_type(e), redirect=redirect)
return True
@ -146,7 +148,7 @@ class PromoteToReplicaSourceForm(forms.SelfHandlingForm):
exceptions.handle(
request,
_('Unable to promote replica as the new replica source. "%s"')
% e.message, redirect=redirect)
% six.text_type(e), redirect=redirect)
return True
@ -182,7 +184,7 @@ class CreateUserForm(forms.SelfHandlingForm):
redirect = reverse("horizon:project:databases:detail",
args=(instance,))
exceptions.handle(request, _('Unable to create user. %s') %
e.message, redirect=redirect)
six.text_type(e), redirect=redirect)
return True
def _get_databases(self, data):
@ -231,7 +233,7 @@ class EditUserForm(forms.SelfHandlingForm):
redirect = reverse("horizon:project:databases:detail",
args=(instance,))
exceptions.handle(request, _('Unable to update user. %s') %
e.message, redirect=redirect)
six.text_type(e), redirect=redirect)
return True
def clean(self):
@ -279,5 +281,5 @@ class AttachConfigurationForm(forms.SelfHandlingForm):
redirect = reverse("horizon:project:databases:index")
exceptions.handle(request, _('Unable to attach configuration '
'group. %s')
% e.message, redirect=redirect)
% six.text_type(e), redirect=redirect)
return True

View File

@ -17,6 +17,8 @@ from django import shortcuts
from django.utils.translation import ugettext_lazy as _
from django.views import generic
import six
from horizon import exceptions
from horizon import messages
@ -63,7 +65,8 @@ def get_contents(request, instance_id, filename, publish, lines):
for log_part in log_generator():
data += log_part
except Exception as e:
data = _('Unable to load {0} log\n{1}').format(filename, e.message)
data = _('Unable to load {0} log\n{1}').format(filename,
six.text_type(e))
return data

View File

@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import six.moves.urllib.parse as urlparse
from django.conf import settings
from django.core import urlresolvers
from django.template import defaultfilters as d_filters
@ -22,6 +20,9 @@ from django.utils.translation import pgettext_lazy
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ungettext_lazy
import six
import six.moves.urllib.parse as urlparse
from horizon import exceptions
from horizon import messages
from horizon import tables
@ -510,7 +511,8 @@ class DisableRootAction(tables.Action):
messages.success(request, _("Successfully disabled root access."))
except Exception as e:
messages.warning(request,
_("Cannot disable root access: %s") % e.message)
_("Cannot disable root access: %s") %
six.text_type(e))
class ManageRoot(tables.LinkAction):

View File

@ -15,6 +15,8 @@
from django import template
from django.utils.translation import ugettext_lazy as _
import six
from horizon import exceptions
from horizon import tabs
from oslo_log import log as logging
@ -178,7 +180,7 @@ class LogsTab(tabs.TableTab):
return logs
except Exception as e:
LOG.exception(
_('Unable to retrieve list of logs.\n%s') % e.message)
_('Unable to retrieve list of logs.\n%s') % six.text_type(e))
logs = []
return logs

View File

@ -496,7 +496,7 @@ class DatabaseTests(test.TestCase):
database.id = u'id'
user = self.database_user_roots.first()
api.trove.instance_get(IsA(http.HttpRequest), IsA(unicode))\
api.trove.instance_get(IsA(http.HttpRequest), IsA(six.text_type))\
.AndReturn(database)
api.trove.root_show(IsA(http.HttpRequest), database.id) \
@ -514,7 +514,7 @@ class DatabaseTests(test.TestCase):
def test_show_root_exception(self):
database = self.databases.first()
api.trove.instance_get(IsA(http.HttpRequest), IsA(unicode))\
api.trove.instance_get(IsA(http.HttpRequest), IsA(six.text_type))\
.AndReturn(database)
api.trove.root_show(IsA(http.HttpRequest), u'id') \
@ -1266,7 +1266,7 @@ class DatabaseTests(test.TestCase):
database = self.databases.first()
configuration = self.database_configurations.first()
api.trove.instance_get(IsA(http.HttpRequest), IsA(unicode))\
api.trove.instance_get(IsA(http.HttpRequest), IsA(six.text_type))\
.AndReturn(database)
api.trove.configuration_list(IsA(http.HttpRequest))\
@ -1296,7 +1296,7 @@ class DatabaseTests(test.TestCase):
database = self.databases.first()
configuration = self.database_configurations.first()
api.trove.instance_get(IsA(http.HttpRequest), IsA(unicode))\
api.trove.instance_get(IsA(http.HttpRequest), IsA(six.text_type))\
.AndReturn(database)
api.trove.configuration_list(IsA(http.HttpRequest))\