# -*- encoding: utf8 -*-
from datetime import *
from decimal import Decimal
#from fastdec import mpd as Decimal
from cPickle import dumps, loads

#from sqlalchemy.dialects.postgresql.base import ARRAY

from stresstest import *

# ---
test_types = False
test_methods = True
test_pickle = False
test_orm = False
# ---
verbose = True

def values_results(raw_results):
    return [tuple(r.values()) for r in raw_results]

def getitem_str_results(raw_results):
    return [
        (r['id'],
         r['field0'], r['field1'], r['field2'], r['field3'], r['field4'],
         r['field5'], r['field6'], r['field7'], r['field8'], r['field9'])
         for r in raw_results]

def getitem_fallback_results(raw_results):
    return [
        (r['ID'],
         r['FIELD0'], r['FIELD1'], r['FIELD2'], r['FIELD3'], r['FIELD4'],
         r['FIELD5'], r['FIELD6'], r['FIELD7'], r['FIELD8'], r['FIELD9'])
         for r in raw_results]

def getitem_int_results(raw_results):
    return [
        (r[0],
         r[1], r[2], r[3], r[4], r[5],
         r[6], r[7], r[8], r[9], r[10])
         for r in raw_results]

def getitem_long_results(raw_results):
    return [
        (r[0L],
         r[1L], r[2L], r[3L], r[4L], r[5L],
         r[6L], r[7L], r[8L], r[9L], r[10L])
         for r in raw_results]

def getitem_obj_results(raw_results):
    c = test_table.c
    fid, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9 = (
        c.id, c.field0, c.field1, c.field2, c.field3, c.field4,
        c.field5, c.field6, c.field7, c.field8, c.field9)
    return [
        (r[fid],
         r[f0], r[f1], r[f2], r[f3], r[f4],
         r[f5], r[f6], r[f7], r[f8], r[f9])
         for r in raw_results]

def slice_results(raw_results):
    return [row[0:6] + row[6:11] for row in raw_results]

# ---------- #
# Test types #
# ---------- #

# Array
#def genarrayvalue(rnum, fnum):
#    return [fnum, fnum + 1, fnum + 2]
#arraytest = (ARRAY(Integer), genarrayvalue,
#             dict(num_fields=100, num_records=1000,
#                  engineurl='postgresql:///test'))

# Boolean
def genbooleanvalue(rnum, fnum):
    if rnum % 4:
        return bool(fnum % 2)
    else:
        return None
booleantest = (Boolean, genbooleanvalue, dict(num_records=100000))

# Datetime
def gendatetimevalue(rnum, fnum):
    return (rnum % 4) and datetime(2005, 3, 3) or None
datetimetest = (DateTime, gendatetimevalue, dict(num_records=10000))

# Decimal
def gendecimalvalue(rnum, fnum):
    if rnum % 4:
        return Decimal(str(0.25 * fnum))
    else:
        return None
decimaltest = (Numeric(10, 2), gendecimalvalue, dict(num_records=10000))

# Interval

# no microseconds because Postgres does not seem to support it
from_epoch = timedelta(14643, 70235)
def genintervalvalue(rnum, fnum):
    return from_epoch
intervaltest = (Interval, genintervalvalue,
                dict(num_fields=2, num_records=100000))

# PickleType
def genpicklevalue(rnum, fnum):
    return (rnum % 4) and {'str': "value%d" % fnum, 'int': rnum} or None
pickletypetest = (PickleType, genpicklevalue,
                  dict(num_fields=1, num_records=100000))

# TypeDecorator
class MyIntType(TypeDecorator):
    impl = Integer

    def process_bind_param(self, value, dialect):
        return value * 10

    def process_result_value(self, value, dialect):
        return value / 10

    def copy(self):
        return MyIntType()

def genmyintvalue(rnum, fnum):
    return rnum + fnum
typedecoratortest = (MyIntType, genmyintvalue,
                     dict(num_records=100000))

# Unicode
def genunicodevalue(rnum, fnum):
    return (rnum % 4) and (u"value%d" % fnum) or None
unicodetest = (Unicode(20, assert_unicode=False), genunicodevalue,
               dict(num_records=100000))
#               dict(engineurl='mysql:///test', freshdata=False))

# do the tests
if test_types:
    tests = [booleantest, datetimetest, decimaltest, intervaltest,
             pickletypetest, typedecoratortest, unicodetest]