# Path: protector/parser/subparsers/resolution.py
# class ResolutionParser(object):
#     """
#     Calculate the data resolution from a query, which is
#     the time interval between two data points.
#     The resolution will always be converted to seconds.
#     """
# 
#     # Regex pattern to extract the value and unit of a group by time() statement
#     # E.g. group by time(10s) => { 'value': 10, 'unit': s }
#     GROUP_BY_TIME_PATTERN = re.compile(r'\s*(?:by)?\s*time\((\d+)([wdhmsu]|ms)\).*')
# 
#     def __init__(self):
#         pass
# 
#     def parse(self, group_by_stmt):
#         """
#         Extract the data resolution of a query in seconds
#         E.g. "group by time(99s)" => 99
# 
#         :param group_by_stmt: A raw InfluxDB group by statement
#         """
#         if not group_by_stmt:
#             return Resolution.MAX_RESOLUTION
# 
#         m = self.GROUP_BY_TIME_PATTERN.match(group_by_stmt)
#         if not m:
#             return None
# 
#         value = int(m.group(1))
#         unit = m.group(2)
#         resolution = self.convert_to_seconds(value, unit)
# 
#         # We can't have a higher resolution than the max resolution
#         return max(resolution, Resolution.MAX_RESOLUTION)
# 
#     @staticmethod
#     def convert_to_seconds(value, unit):
#         if unit not in TimeExpression.UNIT_CONVERSION.keys():
#             # Invalid query
#             return None
#         conversion = TimeExpression.UNIT_CONVERSION[unit]
#         return value * conversion
# 
# Path: protector/influxdb/resolution.py
# class Resolution(object):
#     # Many metrics collectors like collectd send metrics every 10s
#     # so we use this as an estimate for the maximum data resolution
#     MAX_RESOLUTION = 10


import unittest
from protector.parser.subparsers.resolution import ResolutionParser
from protector.influxdb.resolution import Resolution



class TestParseResolution(unittest.TestCase):
    def setUp(self):
        self.resolution_parser = ResolutionParser()

    def test_invalid_resolutions(self):
        self.assertIsNone(self.resolution_parser.parse('asdf'), None)
        self.assertIsNone(self.resolution_parser.parse('time(-1s)'), None)
        self.assertEqual(self.resolution_parser.parse(None), Resolution.MAX_RESOLUTION)

# Path: protector/parser/subparsers/resolution.py
# class ResolutionParser(object):
#     """
#     Calculate the data resolution from a query, which is
#     the time interval between two data points.
#     The resolution will always be converted to seconds.
#     """
# 
#     # Regex pattern to extract the value and unit of a group by time() statement
#     # E.g. group by time(10s) => { 'value': 10, 'unit': s }
#     GROUP_BY_TIME_PATTERN = re.compile(r'\s*(?:by)?\s*time\((\d+)([wdhmsu]|ms)\).*')
# 
#     def __init__(self):
#         pass
# 
#     def parse(self, group_by_stmt):
#         """
#         Extract the data resolution of a query in seconds
#         E.g. "group by time(99s)" => 99
# 
#         :param group_by_stmt: A raw InfluxDB group by statement
#         """
#         if not group_by_stmt:
#             return Resolution.MAX_RESOLUTION
# 
#         m = self.GROUP_BY_TIME_PATTERN.match(group_by_stmt)
#         if not m:
#             return None
# 
#         value = int(m.group(1))
#         unit = m.group(2)
#         resolution = self.convert_to_seconds(value, unit)
# 
#         # We can't have a higher resolution than the max resolution
#         return max(resolution, Resolution.MAX_RESOLUTION)
# 
#     @staticmethod
#     def convert_to_seconds(value, unit):
#         if unit not in TimeExpression.UNIT_CONVERSION.keys():
#             # Invalid query
#             return None
#         conversion = TimeExpression.UNIT_CONVERSION[unit]
#         return value * conversion
# 
# Path: protector/influxdb/resolution.py
# class Resolution(object):
#     # Many metrics collectors like collectd send metrics every 10s
#     # so we use this as an estimate for the maximum data resolution
#     MAX_RESOLUTION = 10


# Path: protector/tests/parser_test/subparser_test/test_resolution_parser.py
import unittest
from protector.parser.subparsers.resolution import ResolutionParser
from protector.influxdb.resolution import Resolution




class TestParseResolution(unittest.TestCase):
    def setUp(self):
        self.resolution_parser = ResolutionParser()

    def test_invalid_resolutions(self):
        self.assertIsNone(self.resolution_parser.parse('asdf'), None)
        self.assertIsNone(self.resolution_parser.parse('time(-1s)'), None)
        self.assertEqual(self.resolution_parser.parse(None), Resolution.MAX_RESOLUTION)

        self.assertEqual(self.resolution_parser.parse(''), Resolution.MAX_RESOLUTION)