# Copyright (c) 2010, Mats Kindahl, Charles Bell, and Lars Thalmann
# All rights reserved.
#
# Use of this source code is goverened by a BSD licence that can be
# found in the LICENCE file.

"""Test of the binary log reader.
"""

import sys
import os.path
_HERE = os.path.dirname(os.path.abspath(__file__))
_ROOTPATH = os.path.split(_HERE)[0]
sys.path.append(_ROOTPATH)

import glob
import time
import unittest
import struct

import mysql.replicant.binary_log as binlog

from itertools import izip, imap

def _data_file(fname):
    return os.path.join(_HERE, 'data', fname)

def _timestamp(string):
    return int(time.mktime(time.strptime(string, "%Y-%m-%d %H:%M:%S")))

_STUBS = {
    'context-bin.000001': [
        { 'type_code': binlog.FORMAT_DESCRIPTION_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-23 15:20:07'),
          'size': 106-4,
          'pos': 4,
          'end_pos': 106,
          'flags': 0,
          },
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-25 00:38:48'),
          'size': 182-106,
          'pos': 106,
          'end_pos': 182,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-25 00:38:49'),
          'size': 294-182,
          'pos': 182,
          'end_pos': 294,
          'flags': 16,
          },
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-25 00:39:18'),
          'size': 382-294,
          'pos': 294,
          'end_pos': 382,
          'flags': 16,
          },
        { 'type_code': binlog.INTVAR_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-25 00:39:31'),
          'size': 410-382,
          'pos': 382,
          'end_pos': 410,
          'flags': 0,
          },
        { 'type_code': binlog.USER_VAR_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-25 00:39:31'),
          'size': 454-410,
          'pos': 410,
          'end_pos': 454,
          'flags': 0,
          },
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-25 00:39:31'),
          'size': 547-454,
          'pos': 454,
          'end_pos': 547,
          'flags': 16,
          },
        { 'type_code': binlog.ROTATE_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-25 00:39:54'),
          'size': 592-547,
          'pos': 547,
          'end_pos': 592,
          'flags': 0,
          },
        ],
    'mysqld1-bin.000005': [
        { 'type_code': binlog.FORMAT_DESCRIPTION_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 11:27:02'),
          'size': 106-4,
          'pos': 4,
          'end_pos': 106,
          'flags': 0,
          },
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 11:27:03'),
          'size': 186-106,
          'pos': 106,
          'end_pos': 186,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 11:27:03'),
          'size': 290-186,
          'pos': 186,
          'end_pos': 290,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 11:27:03'),
          'size': 394-290,
          'pos': 290,
          'end_pos': 394,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 11:27:03'),
          'size': 474-394,
          'pos': 394,
          'end_pos': 474,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 11:27:03'),
          'size': 578-474,
          'pos': 474,
          'end_pos': 578,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 11:27:03'),
          'size': 682-578,
          'pos': 578,
          'end_pos': 682,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 11:27:03'),
          'size': 768-682,
          'pos': 682,
          'end_pos': 768,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 11:27:03'),
          'size': 854-768,
          'pos': 768,
          'end_pos': 854,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 14:55:09'),
          'size': 934-854,
          'pos': 854,
          'end_pos': 934,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 14:55:09'),
          'size': 1038-934,
          'pos': 934,
          'end_pos': 1038,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 14:55:09'),
          'size': 1142-1038,
          'pos': 1038,
          'end_pos': 1142,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 14:55:09'),
          'size': 1222-1142,
          'pos': 1142,
          'end_pos': 1222,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 14:55:09'),
          'size': 1326-1222,
          'pos': 1222,
          'end_pos': 1326,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 14:55:09'),
          'size': 1430-1326,
          'pos': 1326,
          'end_pos': 1430,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 14:55:09'),
          'size': 1516-1430,
          'pos': 1430,
          'end_pos': 1516,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 14:55:09'),
          'size': 1602-1516,
          'pos': 1516,
          'end_pos': 1602,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 14:56:31'),
          'size': 1682-1602,
          'pos': 1602,
          'end_pos': 1682,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 14:56:31'),
          'size': 1786-1682,
          'pos': 1682,
          'end_pos': 1786,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 14:56:31'),
          'size': 1890-1786,
          'pos': 1786,
          'end_pos': 1890,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 14:56:31'),
          'size': 1970-1890,
          'pos': 1890,
          'end_pos': 1970,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 14:56:31'),
          'size': 2074-1970,
          'pos': 1970,
          'end_pos': 2074,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 14:56:31'),
          'size': 2178-2074,
          'pos': 2074,
          'end_pos': 2178,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 14:56:31'),
          'size': 2264-2178,
          'pos': 2178,
          'end_pos': 2264,
          'flags': 16,
          }, 
        { 'type_code': binlog.QUERY_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-16 14:56:31'),
          'size': 2350-2264,
          'pos': 2264,
          'end_pos': 2350,
          'flags': 16,
          }, 
        { 'type_code': binlog.STOP_EVENT,
          'server_id': 1,
          'when': _timestamp('2011-08-19 21:44:29'),
          'size': 2369-2350,
          'pos': 2350,
          'end_pos': 2369,
          'flags': 0,
          }, 
        ],
}

_DECODED = {
    'context-bin.000001': [
        { 'type_name': 'FormatDescription',
          'binlog_version': 4,
          'server_version': '5.1.41-3ubuntu12.10-log',
          'created': _timestamp('2011-08-23 15:20:07'),
            },
        { 'type_name': 'Query',
          'database': 'test',
          'query': 'drop table t1',
          'thread_id': 82,
          'exec_time': 0,
          'error_code': 0,
          'sql_mode': 0,
          },
        {
            'type_name': 'Query',
            'database': 'test',
            'query': 'create table t1(a int primary key auto_increment)',
            'thread_id': 82,
            'exec_time': 0,
            'error_code': 0,
            },
        {
            'type_name': 'Query',
            'database': 'test',
            'query': 'alter table t1 add b text',
            'thread_id': 82,
            'exec_time': 0,
            'error_code': 0,
            },
        {
            'type_name': 'Intvar',
            'variable': binlog.IntvarEvent.INSERT_ID,
            'value': 1,
            },
        {
            'type_name': 'Uservar',
            'variable': 'foo',
            'value': 42,
            },
        {
            'type_name': 'Query',
            'database': 'test',
            'query': 'insert into t1(b) values(@foo)',
            'thread_id': 82,
            'exec_time': 0,
            'error_code': 0,
            'sql_mode': 0,
            },
        {
            'type_name': 'Rotate',
            'next_pos': 4,
            'next_file': 'mysqld1-bin.000002',
            }
        ],