# Path: tests/util.py
# def dt(dtstr):  # (parse datetime str)
#     """
#     Return datetime parsed from str of various accepted formats, eg:
#       - '2014-02-14 16:30:45 001234'
#       - '2014-02-14 16:30:45'
#       - '2014-02-14 16:30'
#       - '2014-02-14'
#     Raise ValueError if not recognizable.
#     """
#     FORMATS = (
#         '%Y-%m-%d %H:%M:%S %f',
#         '%Y-%m-%d %H:%M:%S',
#         '%Y-%m-%d %H:%M',
#         '%Y-%m-%d',
#     )
#     for fmt in FORMATS:
#         try:
#             dt1 = datetime.strptime(dtstr, fmt)
#             return dt1
#         except ValueError as e:
#             pass
#     raise ValueError("time data '{dtstr}' does not match any format"
#         .format(dtstr=dtstr))
# 
# def log_config(level=logging.DEBUG):
#     """
#     Configure logging.
#     Hackily colors log output if 'colorlog' is installed.
#     See: https://pypi.python.org/pypi/colorlog/
#     """
# 
#     format  = ("%(asctime)s %(levelname)-8s [%(name)s]  %(message)s")
#     cformat = ("%(bg_black)s%(asctime)s "+
#         "%(log_color)s%(levelname)-8s [%(name)-50s] %(reset)s "+
#         "%(log_color)s%(bold)s%(message)s")
# 
#     def color_if_possible():
#         try:
#             from colorlog import ColoredFormatter
#         except ImportError:
#             return  # optional lib not available, so don't color
#         formatter = ColoredFormatter(cformat)
#         logging.getLogger().handlers[0].formatter = formatter
# 
#     logging.basicConfig(level=level, format=format)
#     color_if_possible()
# 
# def load_metset(asset):
#     """Helper to load test asset and parse as MetSet, returning."""
#     yaml_metset1 = load_test_asset(asset)
#     parser1 = mdefl.MetSetParser()
#     return parser1.parse_ystr_metset(yaml_metset1)
# 
# def load_query(asset):
#     """Helper to load test asset and parse as Query, returning."""
#     yaml_query1 = load_test_asset(asset)
#     parser1 = mql.QueryParser()
#     return parser1.parse_ystr_query(yaml_query1)


import pytest
import axonchisel.metrics.foundation.chrono.framespec as framespec
import axonchisel.metrics.foundation.metricdef.mdefl as mdefl
import axonchisel.metrics.foundation.query.query as query
import axonchisel.metrics.foundation.query.qdata as qdata
import axonchisel.metrics.foundation.query.qtimeframe as qtimeframe
import axonchisel.metrics.foundation.query.qformat as qformat
import axonchisel.metrics.foundation.query.qghosts as qghosts
import axonchisel.metrics.foundation.query.queryset as queryset
import axonchisel.metrics.foundation.query.mql as mql
import axonchisel.metrics.run.mqengine.mqengine as mqengine
import logging
from .util import dt, log_config, load_metset, load_query
    #
    # Tests
    #

    def test_query_1(self):
        mds = self.mqe1.query( self.query1 )

    def test_extinfo(self):
        emfetch_extinfo = { 'foo': 'bar' }
        mqe2 = mqengine.MQEngine( self.metset1, emfetch_extinfo )
        mds = mqe2.query( self.query1 )

    def test_nodiv(self):
        assert self.query1.qdata.get_qmetric(0).div_metric_id is not None
        self.query1.qdata.get_qmetric(0).div_metric_id = None
        mds = self.mqe1.query( self.query1 )

    def test_reframe_dt(self):
        self.query1.qtimeframe.tmfrspec.reframe_dt = dt('2013-08-15')
        mds = self.mqe1.query( self.query1 )

    def test_str(self):
        mds = self.mqe1.query( self.query1 )
        str(self.mqe1)
        str(self.mqe1._state)

    @pytest.mark.skipif("not TEST_EXTRA_HTTP")
    def test_real_http_backend(self):
        metset1 = load_metset('mqe-metset2.yml')
        query1  = load_query('mqe-query2.yml')

# Path: tests/util.py
# def dt(dtstr):  # (parse datetime str)
#     """
#     Return datetime parsed from str of various accepted formats, eg:
#       - '2014-02-14 16:30:45 001234'
#       - '2014-02-14 16:30:45'
#       - '2014-02-14 16:30'
#       - '2014-02-14'
#     Raise ValueError if not recognizable.
#     """
#     FORMATS = (
#         '%Y-%m-%d %H:%M:%S %f',
#         '%Y-%m-%d %H:%M:%S',
#         '%Y-%m-%d %H:%M',
#         '%Y-%m-%d',
#     )
#     for fmt in FORMATS:
#         try:
#             dt1 = datetime.strptime(dtstr, fmt)
#             return dt1
#         except ValueError as e:
#             pass
#     raise ValueError("time data '{dtstr}' does not match any format"
#         .format(dtstr=dtstr))
# 
# def log_config(level=logging.DEBUG):
#     """
#     Configure logging.
#     Hackily colors log output if 'colorlog' is installed.
#     See: https://pypi.python.org/pypi/colorlog/
#     """
# 
#     format  = ("%(asctime)s %(levelname)-8s [%(name)s]  %(message)s")
#     cformat = ("%(bg_black)s%(asctime)s "+
#         "%(log_color)s%(levelname)-8s [%(name)-50s] %(reset)s "+
#         "%(log_color)s%(bold)s%(message)s")
# 
#     def color_if_possible():
#         try:
#             from colorlog import ColoredFormatter
#         except ImportError:
#             return  # optional lib not available, so don't color
#         formatter = ColoredFormatter(cformat)
#         logging.getLogger().handlers[0].formatter = formatter
# 
#     logging.basicConfig(level=level, format=format)
#     color_if_possible()
# 
# def load_metset(asset):
#     """Helper to load test asset and parse as MetSet, returning."""
#     yaml_metset1 = load_test_asset(asset)
#     parser1 = mdefl.MetSetParser()
#     return parser1.parse_ystr_metset(yaml_metset1)
# 
# def load_query(asset):
#     """Helper to load test asset and parse as Query, returning."""
#     yaml_query1 = load_test_asset(asset)
#     parser1 = mql.QueryParser()
#     return parser1.parse_ystr_query(yaml_query1)


# Path: tests/test_mqengine.py
import pytest
import axonchisel.metrics.foundation.chrono.framespec as framespec
import axonchisel.metrics.foundation.metricdef.mdefl as mdefl
import axonchisel.metrics.foundation.query.query as query
import axonchisel.metrics.foundation.query.qdata as qdata
import axonchisel.metrics.foundation.query.qtimeframe as qtimeframe
import axonchisel.metrics.foundation.query.qformat as qformat
import axonchisel.metrics.foundation.query.qghosts as qghosts
import axonchisel.metrics.foundation.query.queryset as queryset
import axonchisel.metrics.foundation.query.mql as mql
import axonchisel.metrics.run.mqengine.mqengine as mqengine
import logging
from .util import dt, log_config, load_metset, load_query

    #
    # Tests
    #

    def test_query_1(self):
        mds = self.mqe1.query( self.query1 )

    def test_extinfo(self):
        emfetch_extinfo = { 'foo': 'bar' }
        mqe2 = mqengine.MQEngine( self.metset1, emfetch_extinfo )
        mds = mqe2.query( self.query1 )

    def test_nodiv(self):
        assert self.query1.qdata.get_qmetric(0).div_metric_id is not None
        self.query1.qdata.get_qmetric(0).div_metric_id = None
        mds = self.mqe1.query( self.query1 )

    def test_reframe_dt(self):
        self.query1.qtimeframe.tmfrspec.reframe_dt = dt('2013-08-15')
        mds = self.mqe1.query( self.query1 )

    def test_str(self):
        mds = self.mqe1.query( self.query1 )
        str(self.mqe1)
        str(self.mqe1._state)

    @pytest.mark.skipif("not TEST_EXTRA_HTTP")
    def test_real_http_backend(self):
        metset1 = load_metset('mqe-metset2.yml')
        query1  = load_query('mqe-query2.yml')

        query1.qtimeframe.tmfrspec.reframe_dt = dt('2013-08-15')