# Path: graphite_beacon/utils.py
# def parse_rule(rule):
#     tokens = _tokenize_rule(rule)
#     level, initial_expr, exprs = _parse_rule(tokens)
# 
#     result = {'level': level, 'raw': rule, 'exprs': [_parse_expr(initial_expr)]}
# 
#     for logical_operator, expr in exprs:
#         result['exprs'].extend([logical_operator, _parse_expr(expr)])
# 
#     return result
# 
# Path: graphite_beacon/utils.py
# IDENTITY = lambda x: x
# 
# def convert_from_format(num, unit=None):
#     if not unit:
#         return float(num)
#     return float(num) * CONVERT_HASH.get(unit, 1)
# 
# def convert_to_format(value, frmt=None):
#     value = float(value)
#     units = CONVERT.get(frmt, [])
#     for name, size in units:
#         if size < value:
#             break
#     else:
#         return value
# 
#     value /= size  # pylint: disable=undefined-loop-variable
#     value = ("%.1f" % value).rstrip('0').rstrip('.')
#     return "{}{}".format(value, name)  # pylint: disable=undefined-loop-variable


import operator as op
import pytest
from funcparserlib.lexer import LexerError
from graphite_beacon.utils import parse_rule as parse_rule
from graphite_beacon.utils import (IDENTITY, convert_from_format,
                                   convert_to_format)
    assert convert_from_format('456.8', 'Kbps') == 456800

    assert convert_to_format(789, 'short') == 789
    assert convert_to_format(456789, 'short') == '456.8K'
    assert convert_from_format('456.8', 'K') == 456800
    assert convert_to_format(45678912, 'short') == '45.7Mil'
    assert convert_from_format('45.7', 'Mil') == 45700000
    assert convert_to_format(4567891245, 'short') == '4.6Bil'
    assert convert_from_format('4.6', 'Bil') == 4600000000

    assert convert_to_format(789, 's') == "13.2m"
    assert convert_from_format('13.2', 'm') == 792
    assert convert_to_format(789456, 's') == "1.3w"
    assert convert_from_format('1.3', 'w') == 786240
    assert convert_to_format(789456234, 's') == "25y"

    assert convert_to_format(79456234, 'ms') == "22.1h"
    assert convert_to_format(34, 'ms') == "34ms"


def test_parse_rule():
    with pytest.raises(LexerError):
        assert parse_rule('invalid')

    assert parse_rule('normal: == 0') == {
        'level': 'normal', 'raw': 'normal: == 0',
        'exprs': [{'op': op.eq, 'value': 0, 'mod': IDENTITY}]}

    assert parse_rule('critical: < 30MB') == {
        'level': 'critical', 'raw': 'critical: < 30MB',

# Path: graphite_beacon/utils.py
# def parse_rule(rule):
#     tokens = _tokenize_rule(rule)
#     level, initial_expr, exprs = _parse_rule(tokens)
# 
#     result = {'level': level, 'raw': rule, 'exprs': [_parse_expr(initial_expr)]}
# 
#     for logical_operator, expr in exprs:
#         result['exprs'].extend([logical_operator, _parse_expr(expr)])
# 
#     return result
# 
# Path: graphite_beacon/utils.py
# IDENTITY = lambda x: x
# 
# def convert_from_format(num, unit=None):
#     if not unit:
#         return float(num)
#     return float(num) * CONVERT_HASH.get(unit, 1)
# 
# def convert_to_format(value, frmt=None):
#     value = float(value)
#     units = CONVERT.get(frmt, [])
#     for name, size in units:
#         if size < value:
#             break
#     else:
#         return value
# 
#     value /= size  # pylint: disable=undefined-loop-variable
#     value = ("%.1f" % value).rstrip('0').rstrip('.')
#     return "{}{}".format(value, name)  # pylint: disable=undefined-loop-variable


# Path: tests/unit/utils_test.py
import operator as op
import pytest
from funcparserlib.lexer import LexerError
from graphite_beacon.utils import parse_rule as parse_rule
from graphite_beacon.utils import (IDENTITY, convert_from_format,
                                   convert_to_format)

    assert convert_from_format('456.8', 'Kbps') == 456800

    assert convert_to_format(789, 'short') == 789
    assert convert_to_format(456789, 'short') == '456.8K'
    assert convert_from_format('456.8', 'K') == 456800
    assert convert_to_format(45678912, 'short') == '45.7Mil'
    assert convert_from_format('45.7', 'Mil') == 45700000
    assert convert_to_format(4567891245, 'short') == '4.6Bil'
    assert convert_from_format('4.6', 'Bil') == 4600000000

    assert convert_to_format(789, 's') == "13.2m"
    assert convert_from_format('13.2', 'm') == 792
    assert convert_to_format(789456, 's') == "1.3w"
    assert convert_from_format('1.3', 'w') == 786240
    assert convert_to_format(789456234, 's') == "25y"

    assert convert_to_format(79456234, 'ms') == "22.1h"
    assert convert_to_format(34, 'ms') == "34ms"


def test_parse_rule():
    with pytest.raises(LexerError):
        assert parse_rule('invalid')

    assert parse_rule('normal: == 0') == {
        'level': 'normal', 'raw': 'normal: == 0',
        'exprs': [{'op': op.eq, 'value': 0, 'mod': IDENTITY}]}

    assert parse_rule('critical: < 30MB') == {
        'level': 'critical', 'raw': 'critical: < 30MB',

        'exprs': [{'op': op.lt, 'value': 31457280, 'mod': IDENTITY}]}