Python 3.5: Fix alarms repository

Fix python3 unit test for alarms reposiotry.

Change-Id: Ibb8cba96f62ac5580cd7b74bf6f2751efa66cd90
This commit is contained in:
Adrian Czarnecki 2018-06-11 10:17:13 +02:00
parent 49decc368e
commit dd99ad56a2
2 changed files with 43 additions and 36 deletions

View File

@ -16,6 +16,8 @@
from datetime import datetime
from time import time
import six
from monasca_api.common.repositories import alarms_repository
from monasca_api.common.repositories import exceptions
from monasca_api.common.repositories.sqla import models
@ -284,7 +286,8 @@ class AlarmsRepository(sql_repository.SQLRepository,
if 'metric_name' in query_parms:
query = query.where(a.c.id.in_(self.get_a_am_query))
parms['b_md_name'] = query_parms['metric_name'].encode('utf8')
parms['b_md_name'] = query_parms['metric_name'].encode('utf8') if six.PY2 else \
query_parms['metric_name']
if 'severity' in query_parms:
severities = query_parms['severity'].split('|')
@ -295,7 +298,7 @@ class AlarmsRepository(sql_repository.SQLRepository,
if 'state' in query_parms:
query = query.where(a.c.state == bindparam('b_state'))
parms['b_state'] = query_parms['state'].encode('utf8')
parms['b_state'] = query_parms['state']
if 'lifecycle_state' in query_parms:
query = (query
@ -311,7 +314,9 @@ class AlarmsRepository(sql_repository.SQLRepository,
query = (query
.where(a.c.state_updated_at >=
bindparam('b_state_updated_at')))
date_str = query_parms['state_updated_start_time'].encode('utf8')
date_str = query_parms['state_updated_start_time'].encode('utf8') if six.PY2 else \
query_parms['state_updated_start_time']
date_param = datetime.strptime(date_str,
'%Y-%m-%dT%H:%M:%S.%fZ')
parms['b_state_updated_at'] = date_param
@ -334,7 +339,8 @@ class AlarmsRepository(sql_repository.SQLRepository,
if metric_dimension and metric_dimension[1]:
if '|' in metric_dimension[1]:
values = metric_dimension[1].encode('utf8').split('|')
values = metric_dimension[1].encode('utf8').split('|') if six.PY2 else \
metric_dimension[1].split('|')
sub_values_cond = []
for j, value in enumerate(values):
sub_md_value = "b_md_value_{}_{}".format(i, j)
@ -363,7 +369,8 @@ class AlarmsRepository(sql_repository.SQLRepository,
sub_query_md.c.dimension_set_id ==
mdd.c.metric_dimension_set_id))
parms[md_name] = metric_dimension[0].encode('utf8')
parms[md_name] = metric_dimension[0].encode('utf8') if six.PY2 else \
metric_dimension[0]
sub_query = (sub_query
.select_from(sub_query_from)

View File

@ -365,72 +365,72 @@ class TestAlarmRepoDB(base.BaseTestCase):
'updated_at': datetime.datetime.now()}]
self.default_ams = [{'alarm_id': '1',
'metric_definition_dimensions_id': '11'},
'metric_definition_dimensions_id': b'11'},
{'alarm_id': '1',
'metric_definition_dimensions_id': '22'},
'metric_definition_dimensions_id': b'22'},
{'alarm_id': '2',
'metric_definition_dimensions_id': '11'},
'metric_definition_dimensions_id': b'11'},
{'alarm_id': '3',
'metric_definition_dimensions_id': '22'},
'metric_definition_dimensions_id': b'22'},
{'alarm_id': '234111',
'metric_definition_dimensions_id': '31'},
'metric_definition_dimensions_id': b'31'},
{'alarm_id': '234111',
'metric_definition_dimensions_id': '32'}]
'metric_definition_dimensions_id': b'32'}]
self.default_mdes = [{'id': '1',
self.default_mdes = [{'id': b'1',
'name': 'cpu.idle_perc',
'tenant_id': 'bob',
'region': 'west'},
{'id': '111',
{'id': b'111',
'name': 'cpu.sys_mem',
'tenant_id': 'bob',
'region': 'west'},
{'id': '112',
{'id': b'112',
'name': 'cpu.idle_perc',
'tenant_id': 'bob',
'region': 'west'}]
self.default_mdds = [{'id': '11',
'metric_definition_id': '1',
'metric_dimension_set_id': '1'},
{'id': '22',
'metric_definition_id': '1',
'metric_dimension_set_id': '2'},
{'id': '31',
'metric_definition_id': '111',
'metric_dimension_set_id': '21'},
{'id': '32',
'metric_definition_id': '112',
'metric_dimension_set_id': '22'}]
self.default_mdds = [{'id': b'11',
'metric_definition_id': b'1',
'metric_dimension_set_id': b'1'},
{'id': b'22',
'metric_definition_id': b'1',
'metric_dimension_set_id': b'2'},
{'id': b'31',
'metric_definition_id': b'111',
'metric_dimension_set_id': b'21'},
{'id': b'32',
'metric_definition_id': b'112',
'metric_dimension_set_id': b'22'}]
self.default_mds = [{'dimension_set_id': '1',
self.default_mds = [{'dimension_set_id': b'1',
'name': 'instance_id',
'value': '123'},
{'dimension_set_id': '1',
{'dimension_set_id': b'1',
'name': 'service',
'value': 'monitoring'},
{'dimension_set_id': '2',
{'dimension_set_id': b'2',
'name': 'flavor_id',
'value': '222'},
{'dimension_set_id': '21',
{'dimension_set_id': b'21',
'name': 'service',
'value': 'monitoring'},
{'dimension_set_id': '22',
{'dimension_set_id': b'22',
'name': 'service',
'value': 'monitoring'},
{'dimension_set_id': '21',
{'dimension_set_id': b'21',
'name': 'hostname',
'value': 'roland'},
{'dimension_set_id': '22',
{'dimension_set_id': b'22',
'name': 'hostname',
'value': 'roland'},
{'dimension_set_id': '21',
{'dimension_set_id': b'21',
'name': 'region',
'value': 'colorado'},
{'dimension_set_id': '22',
{'dimension_set_id': b'22',
'name': 'region',
'value': 'colorado'},
{'dimension_set_id': '22',
{'dimension_set_id': b'22',
'name': 'extra',
'value': 'vivi'}]