Fix Python 3 compatibility in idlutils

In python 2 dict.values() would return a list while in python 3
it returns a dict_values object. The function row_by_record
tries to fetch directly the first element of the list, which
will work on python 2 but will fail on python 3 with the error
"'dict_values' object does not support indexing".

This patch addresses this issue by converting the return value
of dict.values() to list which will return the same value on
python 2 and python 3.

Change-Id: I8e3b28a84e9e5ccaeaf2d79e4547f9941472651b
This commit is contained in:
Alin Balutoiu 2017-03-22 16:51:51 +02:00
parent 71d96a8306
commit 34714b28ff
2 changed files with 13 additions and 1 deletions

View File

@ -84,7 +84,7 @@ def row_by_record(idl_, table, record):
if rl.table is None:
raise ValueError(_("Table %s can only be queried by UUID") % table)
if rl.column is None:
return t.rows.values()[0]
return next(iter(t.rows.values()))
row = row_by_value(idl_, rl.table, rl.column, record)
if rl.uuid_column:
rows = getattr(row, rl.uuid_column)

View File

@ -142,3 +142,15 @@ class TestIdlUtils(base.BaseTestCase):
def test_db_replace_record_cmd(self):
obj = MockCommand("test")
self.assertEqual("test", idlutils.db_replace_record(obj))
def test_row_by_record(self):
FAKE_RECORD = 'fake_record'
mock_idl_ = mock.MagicMock()
mock_table = mock.MagicMock(
rows={mock.sentinel.row: mock.sentinel.row_value})
mock_idl_.tables = {mock.sentinel.table_name: mock_table}
res = idlutils.row_by_record(mock_idl_,
mock.sentinel.table_name,
FAKE_RECORD)
self.assertEqual(mock.sentinel.row_value, res)