# Path: tests/test_querysets.py
# class SampleModel(Model):
# 
#     timestamp = DateTimeField()
#     materialized_date = DateField(materialized='toDate(timestamp)')
#     num = Int32Field()
#     color = Enum8Field(Color)
#     num_squared = Int32Field(alias='num*num')
# 
#     engine = MergeTree('materialized_date', ('materialized_date',))


import unittest
import pytz
import logging
from .base_test_with_data import *
from .test_querysets import SampleModel
from datetime import date, datetime, tzinfo, timedelta
from ipaddress import IPv4Address, IPv6Address
from decimal import Decimal
from infi.clickhouse_orm.database import ServerError
from infi.clickhouse_orm.utils import NO_VALUE
from infi.clickhouse_orm.funcs import F
        from uuid import UUID
            result = list(qs)
            logging.info('\t==> %s', result[0].value if result else '<empty>')
            if expected_value != NO_VALUE:
                self.assertEqual(result[0].value, expected_value)
            return result[0].value if result else None
        except ServerError as e:
            if 'Unknown function' in e.message:
                logging.warning(e.message)
                return # ignore functions that don't exist in the used ClickHouse version
            raise

    def test_func_to_sql(self):
        # No args
        self.assertEqual(F('func').to_sql(), 'func()')
        # String args
        self.assertEqual(F('func', "Wendy's", u"Wendy's").to_sql(), "func('Wendy\\'s', 'Wendy\\'s')")
        # Numeric args
        self.assertEqual(F('func', 1, 1.1, Decimal('3.3')).to_sql(), "func(1, 1.1, 3.3)")
        # Date args
        self.assertEqual(F('func', date(2018, 12, 31)).to_sql(), "func(toDate('2018-12-31'))")
        # Datetime args
        self.assertEqual(F('func', datetime(2018, 12, 31)).to_sql(), "func(toDateTime('1546214400'))")
        # Boolean args
        self.assertEqual(F('func', True, False).to_sql(), "func(1, 0)")
        # Timezone args
        self.assertEqual(F('func', pytz.utc).to_sql(), "func('UTC')")
        self.assertEqual(F('func', pytz.timezone('Europe/Athens')).to_sql(), "func('Europe/Athens')")
        # Null args
        self.assertEqual(F('func', None).to_sql(), "func(NULL)")
        # Fields as args

# Path: tests/test_querysets.py
# class SampleModel(Model):
# 
#     timestamp = DateTimeField()
#     materialized_date = DateField(materialized='toDate(timestamp)')
#     num = Int32Field()
#     color = Enum8Field(Color)
#     num_squared = Int32Field(alias='num*num')
# 
#     engine = MergeTree('materialized_date', ('materialized_date',))


# Path: tests/test_funcs.py
import unittest
import pytz
import logging
from .base_test_with_data import *
from .test_querysets import SampleModel
from datetime import date, datetime, tzinfo, timedelta
from ipaddress import IPv4Address, IPv6Address
from decimal import Decimal
from infi.clickhouse_orm.database import ServerError
from infi.clickhouse_orm.utils import NO_VALUE
from infi.clickhouse_orm.funcs import F
        from uuid import UUID

            result = list(qs)
            logging.info('\t==> %s', result[0].value if result else '<empty>')
            if expected_value != NO_VALUE:
                self.assertEqual(result[0].value, expected_value)
            return result[0].value if result else None
        except ServerError as e:
            if 'Unknown function' in e.message:
                logging.warning(e.message)
                return # ignore functions that don't exist in the used ClickHouse version
            raise

    def test_func_to_sql(self):
        # No args
        self.assertEqual(F('func').to_sql(), 'func()')
        # String args
        self.assertEqual(F('func', "Wendy's", u"Wendy's").to_sql(), "func('Wendy\\'s', 'Wendy\\'s')")
        # Numeric args
        self.assertEqual(F('func', 1, 1.1, Decimal('3.3')).to_sql(), "func(1, 1.1, 3.3)")
        # Date args
        self.assertEqual(F('func', date(2018, 12, 31)).to_sql(), "func(toDate('2018-12-31'))")
        # Datetime args
        self.assertEqual(F('func', datetime(2018, 12, 31)).to_sql(), "func(toDateTime('1546214400'))")
        # Boolean args
        self.assertEqual(F('func', True, False).to_sql(), "func(1, 0)")
        # Timezone args
        self.assertEqual(F('func', pytz.utc).to_sql(), "func('UTC')")
        self.assertEqual(F('func', pytz.timezone('Europe/Athens')).to_sql(), "func('Europe/Athens')")
        # Null args
        self.assertEqual(F('func', None).to_sql(), "func(NULL)")
        # Fields as args

        self.assertEqual(F('func', SampleModel.color).to_sql(), "func(`color`)")