# 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()


import pytest
import yaml
import axonchisel.metrics.foundation.query.mql as mql
from .util import dt, load_test_asset
    #

    def test_parse_simple(self):
        query2 = self.parser1.parse_ystr_query(self.yaml_query1)
        query2.validate()
        query3 = self.parser1.get_query()
        assert query2 == query3
        query3.validate()

    def test_str(self):
        str(self.parser1)

    def test_parse_qdata(self):
        q = self.parser1.parse_ystr_query(self.yaml_query1)
        assert q.qdata.count_qmetrics() == 1
        qmetric1 = q.qdata.get_qmetric(0)
        assert qmetric1.metric_id == 'num_new_paid_accounts'
        assert qmetric1.div_metric_id == 'num_total_paid_accounts'
        assert qmetric1.goal == 0.2
        assert qmetric1.goal_mode == 'CONSTANT'

    def test_parse_qtimeframe(self):
        q = self.parser1.parse_ystr_query(self.yaml_query1)
        tmfrspec1 = q.qtimeframe.tmfrspec
        assert tmfrspec1.range_unit == 'MONTH'
        assert tmfrspec1.range_val == 3
        assert tmfrspec1.gran_unit == 'DAY'
        assert tmfrspec1.smooth_unit == 'DAY'
        assert tmfrspec1.smooth_val == 30
        assert tmfrspec1.mode == 'CURRENT'

# 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()


# Path: tests/test_mql.py
import pytest
import yaml
import axonchisel.metrics.foundation.query.mql as mql
from .util import dt, load_test_asset

    #

    def test_parse_simple(self):
        query2 = self.parser1.parse_ystr_query(self.yaml_query1)
        query2.validate()
        query3 = self.parser1.get_query()
        assert query2 == query3
        query3.validate()

    def test_str(self):
        str(self.parser1)

    def test_parse_qdata(self):
        q = self.parser1.parse_ystr_query(self.yaml_query1)
        assert q.qdata.count_qmetrics() == 1
        qmetric1 = q.qdata.get_qmetric(0)
        assert qmetric1.metric_id == 'num_new_paid_accounts'
        assert qmetric1.div_metric_id == 'num_total_paid_accounts'
        assert qmetric1.goal == 0.2
        assert qmetric1.goal_mode == 'CONSTANT'

    def test_parse_qtimeframe(self):
        q = self.parser1.parse_ystr_query(self.yaml_query1)
        tmfrspec1 = q.qtimeframe.tmfrspec
        assert tmfrspec1.range_unit == 'MONTH'
        assert tmfrspec1.range_val == 3
        assert tmfrspec1.gran_unit == 'DAY'
        assert tmfrspec1.smooth_unit == 'DAY'
        assert tmfrspec1.smooth_val == 30
        assert tmfrspec1.mode == 'CURRENT'

        assert tmfrspec1.reframe_dt == dt('2014-11-01')