# 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 load_test_asset(fname):
#     """
#     Load a test file from assets directory, returning contents as str.
#     """
#     path = os.path.dirname(os.path.realpath(__file__))
#     fname = os.path.join(path, 'assets', fname)
#     with open(fname, 'r') as f:
#         return f.read()
# 
# 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()


import pytest
import axonchisel.metrics.foundation.chrono.timerange as timerange
import axonchisel.metrics.foundation.metricdef.metricdef as metricdef
import axonchisel.metrics.foundation.metricdef.filters as filters
import axonchisel.metrics.foundation.metricdef.mdefl as mdefl
import axonchisel.metrics.io.emfetch.plugins.emf_http as emf_http
import logging
from .util import dt, load_test_asset, log_config
    # Setup / Teardown
    #

    def setup_method(self, method):
        self.extinfo = {
            'api_url': 'http://localhost/apidemo/kpi_reduce/',
            'api_key': 'TestKey',
            # 'table_prefix': 'my_',
        }
        # Parse MDefL MetSet:
        self.yaml_metset1 = load_test_asset('metset-http.yml')
        self.parser1 = mdefl.MetSetParser()
        self.metset1 = self.parser1.parse_ystr_metset(self.yaml_metset1)

    #
    # Tests
    #

    def test_maybe_real(self):
        mdef1 = self.metset1.get_metric_by_id('rev_new_sales')
        emfetch1 = emf_http.EMFetcher_http(mdef1, extinfo=self.extinfo)
        if MOCK_REQUESTS:
            self._mock_requests(emfetch1, '{ "body": { "result": 12345 } }')
        emfetch1.plugin_create()
        for x in range(1):
            tmrange = timerange.TimeRange(
                inc_begin=dt('2013-02-01'), exc_end=dt('2013-02-02'))
            dpoint1 = emfetch1.fetch(tmrange)
            print dpoint1
            tmrange = timerange.TimeRange(

# 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 load_test_asset(fname):
#     """
#     Load a test file from assets directory, returning contents as str.
#     """
#     path = os.path.dirname(os.path.realpath(__file__))
#     fname = os.path.join(path, 'assets', fname)
#     with open(fname, 'r') as f:
#         return f.read()
# 
# 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()


# Path: tests/test_emfetch_http.py
import pytest
import axonchisel.metrics.foundation.chrono.timerange as timerange
import axonchisel.metrics.foundation.metricdef.metricdef as metricdef
import axonchisel.metrics.foundation.metricdef.filters as filters
import axonchisel.metrics.foundation.metricdef.mdefl as mdefl
import axonchisel.metrics.io.emfetch.plugins.emf_http as emf_http
import logging
from .util import dt, load_test_asset, log_config

    # Setup / Teardown
    #

    def setup_method(self, method):
        self.extinfo = {
            'api_url': 'http://localhost/apidemo/kpi_reduce/',
            'api_key': 'TestKey',
            # 'table_prefix': 'my_',
        }
        # Parse MDefL MetSet:
        self.yaml_metset1 = load_test_asset('metset-http.yml')
        self.parser1 = mdefl.MetSetParser()
        self.metset1 = self.parser1.parse_ystr_metset(self.yaml_metset1)

    #
    # Tests
    #

    def test_maybe_real(self):
        mdef1 = self.metset1.get_metric_by_id('rev_new_sales')
        emfetch1 = emf_http.EMFetcher_http(mdef1, extinfo=self.extinfo)
        if MOCK_REQUESTS:
            self._mock_requests(emfetch1, '{ "body": { "result": 12345 } }')
        emfetch1.plugin_create()
        for x in range(1):
            tmrange = timerange.TimeRange(
                inc_begin=dt('2013-02-01'), exc_end=dt('2013-02-02'))
            dpoint1 = emfetch1.fetch(tmrange)
            print dpoint1
            tmrange = timerange.TimeRange(

                inc_begin=dt('2013-02-02'), exc_end=dt('2013-02-03'))