# Path: monasca_api/common/repositories/model/sub_alarm_definition.py
# class SubAlarmDefinition(object):
#     def __init__(self, row=None, sub_expr=None):
#     def _init_dimensions(self, dimensions_str):
#     def expression(self):
#     def __hash__(self):
#     def __repr__(self):
#     def __eq__(self, other):
#     def same_key_fields(self, other):
# 
# Path: monasca_api/common/repositories/sqla/models.py
# def get_all_metadata():
# def create_a_model(metadata=None):
# def create_sadd_model(metadata=None):
# def create_aa_model(metadata=None):
# def create_md_model(metadata=None):
# def create_mde_model(metadata=None):
# def create_nm_model(metadata=None):
# def create_nmt_model(metadata=None):
# def create_mdd_model(metadata=None):
# def create_am_model(metadata=None):
# def create_ad_model(metadata=None):
# def create_sa_model(metadata=None):
# def create_sad_model(metadata=None):
#     def __init__(self, columns, separator=',', order_by=None):
# def _group_concat_oracle(element, compiler_, **kw):
# def _group_concat_postgresql(element, compiler_, **kw):
# def _group_concat_sybase(element, compiler_, **kw):
# def _group_concat_mysql(element, compiler_, **kw):
# def _group_concat_default(element, compiler_, **kw):
#     def __init__(self, column, fields):
# def _field_sort_mysql(element, compiler_, **kw):
# def _field_sort_general(element, compiler_, **kw):
# class group_concat(expression.ColumnElement):
# class field_sort(expression.ColumnElement):
# 
# Path: monasca_api/expression_parser/alarm_expr_parser.py
# _DETERMINISTIC_ASSIGNMENT_LEN = 3
# _DETERMINISTIC_ASSIGNMENT_SHORT_LEN = 1
# _DETERMINISTIC_ASSIGNMENT_VALUE_INDEX = 2
# _DEFAULT_PERIOD = 60
# _DEFAULT_PERIODS = 1
# COMMA = pyparsing.Suppress(pyparsing.Literal(","))
# LPAREN = pyparsing.Suppress(pyparsing.Literal("("))
# RPAREN = pyparsing.Suppress(pyparsing.Literal(")"))
# EQUAL = pyparsing.Literal("=")
# LBRACE = pyparsing.Suppress(pyparsing.Literal("{"))
# RBRACE = pyparsing.Suppress(pyparsing.Literal("}"))
# MINUS = pyparsing.Literal('-')
# AND = pyparsing.CaselessLiteral("and") | pyparsing.CaselessLiteral("&&")
# OR = pyparsing.CaselessLiteral("or") | pyparsing.CaselessLiteral("||")
# class SubExpr(object):
# class BinaryOp(object):
# class AndSubExpr(BinaryOp):
# class OrSubExpr(BinaryOp):
# class AlarmExprParser(object):
#     def __init__(self, tokens):
#     def fmtd_sub_expr_str(self):
#     def dimensions_str(self):
#     def operands_list(self):
#     def func(self):
#     def normalized_func(self):
#     def metric_name(self):
#     def normalized_metric_name(self):
#     def dimensions(self):
#     def dimensions_as_list(self):
#     def operator(self):
#     def threshold(self):
#     def period(self):
#     def periods(self):
#     def deterministic(self):
#     def normalized_operator(self):
#     def id(self):
#     def id(self, id):
#     def __init__(self, tokens):
#     def operands_list(self):
# def periodValidation(instr, loc, tokens):
# def periodsValidation(instr, loc, tokens):
#     def __init__(self, expr):
#     def sub_expr_list(self):
# def main():


import datetime
import fixtures
from oslo_config import cfg
from oslo_db.sqlalchemy.engines import create_engine
from sqlalchemy import delete, MetaData, insert, bindparam, select, func
from monasca_api.common.repositories import exceptions
from monasca_api.common.repositories.model import sub_alarm_definition
from monasca_api.common.repositories.sqla import models
from monasca_api.expression_parser import alarm_expr_parser
from monasca_api.tests import base
        from monasca_api.common.repositories.sqla import alarm_definitions_repository as adr
        from monasca_api.common.repositories import exceptions
    def test_should_patch_expression_periods_change(self):
        self.run_patch_test(
            expression=ALARM_DEF_123_FIELDS['expression'].replace(
                ' 10', ' 10 times 2'))

    def test_patch_fails_change_match_by(self):
        self.assertRaises(
            exceptions.InvalidUpdateException,
            self.run_patch_test,
            match_by=u'device')

    def test_patch_fails_change_metric_name(self):
        self.assertRaises(exceptions.InvalidUpdateException, self.run_patch_test,
                          expression=ALARM_DEF_123_FIELDS['expression'].replace('hpcs.compute',
                                                                                'new_metric_name'))

    def test_patch_fails_change_metric_dimensions(self):
        self.assertRaises(exceptions.InvalidUpdateException, self.run_patch_test,
                          expression=ALARM_DEF_123_FIELDS['expression'].replace('image_id=888',
                                                                                'image_id=42'))

    def test_patch_fails_change_num_sub_expressions(self):
        self.assertRaises(exceptions.InvalidUpdateException, self.run_patch_test,
                          expression=ALARM_DEF_123_FIELDS['expression']
                          .replace(' 10', ' 10 and MAX(cpu.idle_perc) < 10'))

    def run_patch_test(self, name=None, expression=None, description=None, actions_enabled=None,
                       alarm_actions=None, ok_actions=None, undetermined_actions=None,
                       match_by=None, severity=None):
        if expression:

# Path: monasca_api/common/repositories/model/sub_alarm_definition.py
# class SubAlarmDefinition(object):
#     def __init__(self, row=None, sub_expr=None):
#     def _init_dimensions(self, dimensions_str):
#     def expression(self):
#     def __hash__(self):
#     def __repr__(self):
#     def __eq__(self, other):
#     def same_key_fields(self, other):
# 
# Path: monasca_api/common/repositories/sqla/models.py
# def get_all_metadata():
# def create_a_model(metadata=None):
# def create_sadd_model(metadata=None):
# def create_aa_model(metadata=None):
# def create_md_model(metadata=None):
# def create_mde_model(metadata=None):
# def create_nm_model(metadata=None):
# def create_nmt_model(metadata=None):
# def create_mdd_model(metadata=None):
# def create_am_model(metadata=None):
# def create_ad_model(metadata=None):
# def create_sa_model(metadata=None):
# def create_sad_model(metadata=None):
#     def __init__(self, columns, separator=',', order_by=None):
# def _group_concat_oracle(element, compiler_, **kw):
# def _group_concat_postgresql(element, compiler_, **kw):
# def _group_concat_sybase(element, compiler_, **kw):
# def _group_concat_mysql(element, compiler_, **kw):
# def _group_concat_default(element, compiler_, **kw):
#     def __init__(self, column, fields):
# def _field_sort_mysql(element, compiler_, **kw):
# def _field_sort_general(element, compiler_, **kw):
# class group_concat(expression.ColumnElement):
# class field_sort(expression.ColumnElement):
# 
# Path: monasca_api/expression_parser/alarm_expr_parser.py
# _DETERMINISTIC_ASSIGNMENT_LEN = 3
# _DETERMINISTIC_ASSIGNMENT_SHORT_LEN = 1
# _DETERMINISTIC_ASSIGNMENT_VALUE_INDEX = 2
# _DEFAULT_PERIOD = 60
# _DEFAULT_PERIODS = 1
# COMMA = pyparsing.Suppress(pyparsing.Literal(","))
# LPAREN = pyparsing.Suppress(pyparsing.Literal("("))
# RPAREN = pyparsing.Suppress(pyparsing.Literal(")"))
# EQUAL = pyparsing.Literal("=")
# LBRACE = pyparsing.Suppress(pyparsing.Literal("{"))
# RBRACE = pyparsing.Suppress(pyparsing.Literal("}"))
# MINUS = pyparsing.Literal('-')
# AND = pyparsing.CaselessLiteral("and") | pyparsing.CaselessLiteral("&&")
# OR = pyparsing.CaselessLiteral("or") | pyparsing.CaselessLiteral("||")
# class SubExpr(object):
# class BinaryOp(object):
# class AndSubExpr(BinaryOp):
# class OrSubExpr(BinaryOp):
# class AlarmExprParser(object):
#     def __init__(self, tokens):
#     def fmtd_sub_expr_str(self):
#     def dimensions_str(self):
#     def operands_list(self):
#     def func(self):
#     def normalized_func(self):
#     def metric_name(self):
#     def normalized_metric_name(self):
#     def dimensions(self):
#     def dimensions_as_list(self):
#     def operator(self):
#     def threshold(self):
#     def period(self):
#     def periods(self):
#     def deterministic(self):
#     def normalized_operator(self):
#     def id(self):
#     def id(self, id):
#     def __init__(self, tokens):
#     def operands_list(self):
# def periodValidation(instr, loc, tokens):
# def periodsValidation(instr, loc, tokens):
#     def __init__(self, expr):
#     def sub_expr_list(self):
# def main():


# Path: monasca_api/tests/test_ad_repository.py
import datetime
import fixtures
from oslo_config import cfg
from oslo_db.sqlalchemy.engines import create_engine
from sqlalchemy import delete, MetaData, insert, bindparam, select, func
from monasca_api.common.repositories import exceptions
from monasca_api.common.repositories.model import sub_alarm_definition
from monasca_api.common.repositories.sqla import models
from monasca_api.expression_parser import alarm_expr_parser
from monasca_api.tests import base
        from monasca_api.common.repositories.sqla import alarm_definitions_repository as adr
        from monasca_api.common.repositories import exceptions

    def test_should_patch_expression_periods_change(self):
        self.run_patch_test(
            expression=ALARM_DEF_123_FIELDS['expression'].replace(
                ' 10', ' 10 times 2'))

    def test_patch_fails_change_match_by(self):
        self.assertRaises(
            exceptions.InvalidUpdateException,
            self.run_patch_test,
            match_by=u'device')

    def test_patch_fails_change_metric_name(self):
        self.assertRaises(exceptions.InvalidUpdateException, self.run_patch_test,
                          expression=ALARM_DEF_123_FIELDS['expression'].replace('hpcs.compute',
                                                                                'new_metric_name'))

    def test_patch_fails_change_metric_dimensions(self):
        self.assertRaises(exceptions.InvalidUpdateException, self.run_patch_test,
                          expression=ALARM_DEF_123_FIELDS['expression'].replace('image_id=888',
                                                                                'image_id=42'))

    def test_patch_fails_change_num_sub_expressions(self):
        self.assertRaises(exceptions.InvalidUpdateException, self.run_patch_test,
                          expression=ALARM_DEF_123_FIELDS['expression']
                          .replace(' 10', ' 10 and MAX(cpu.idle_perc) < 10'))

    def run_patch_test(self, name=None, expression=None, description=None, actions_enabled=None,
                       alarm_actions=None, ok_actions=None, undetermined_actions=None,
                       match_by=None, severity=None):
        if expression:

            sub_expr_list = (alarm_expr_parser.AlarmExprParser(expression).sub_expr_list)