# Path: wizzat/sqlhelper.py
# def fetch_results(conn, sql, **bind_params):
#     """
#     Immediately fetches the SQL results into memory
#     Trades memory for the ability to immediately execute another query
#     """
#     global _log_func
#     try:
#         cur = conn.cursor()
#         if _log_func:
#             _log_func(cur, sql, bind_params)
# 
#         cur.execute(sql, bind_params)
#         return cur.fetchall()
#     finally:
#         cur.close()
# 
# Path: wizzat/util.py
# def assert_online():
#     """
#         This method tests the OFFLINE environment variable and throws OfflineError().  This directly
#         hooks into @skip_offline, but can be used in other situations as well.
#     """
#     if not is_online():
#         raise OfflineError()
# 
# class OfflineError(Exception): pass
# 
# def reset_online():
#     """
#         Resets the global offline state
#     """
#     global _offline
#     _offline = os.environ.get('OFFLINE', False) == False
# 
# def set_online(value):
#     """
#         Sets the global offline state
#     """
#     global _offline
#     _offline = value
# 
# def is_online():
#     """
#         This method tests the OFFLINE environment variable and the global offline state.
#     """
#     global _offline
#     return _offline
# 
# Path: wizzat/dateutil.py
# def reset_now():
#     """
#     All future calls to `wizzat.now()` will return the current time as of reset_now()
#     """
#     set_now(None)
#     set_now(now())
# 
# Path: wizzat/textutil.py
# def text_table(header, rows, **options):
#     options = set_strict_defaults(options,
#         row_dicts = False,
#         col_trunc_len = None,
#     )
# 
#     if not header:
#         return '[]'
# 
#     fmt_rows = []
#     field_sizes = [ len(name) for name in header ]
# 
#     for row in rows:
#         if options['row_dicts']:
#             fmt_row = [ format_value(row.get(name, '')) for name in header ]
#         else:
#             if len(row) != len(header):
#                 raise TypeError("Expected row of length {}, got {}".format(
#                     len(header),
#                     len(row)),
#                 )
# 
#             fmt_row = [ format_value(value) for value in row ]
# 
#         _update_sizes(field_sizes, fmt_row)
#         fmt_rows.append(fmt_row)
# 
#     header_sep = _format_hline(field_sizes, '=', '+')
#     row_sep = _format_hline(field_sizes, '-', '+')
# 
#     fp = io.StringIO()
#     fp.write(row_sep)
#     fp.write(_format_row(field_sizes, header))
#     fp.write(header_sep)
# 
#     for row in fmt_rows:
#         fp.write(_format_row(field_sizes, row))
#         fp.write(row_sep)
# 
#     return fp.getvalue()


from builtins import *
from wizzat.sqlhelper import fetch_results
from wizzat.util import assert_online, OfflineError, reset_online, set_online, is_online
from wizzat.dateutil import reset_now
from wizzat.decorators import *
from wizzat.textutil import text_table
import unittest, difflib, functools, os, json
                import pghelper
        expected = json.dumps(obj1, indent=4, sort_keys=True)
        actual = json.dumps(obj2, indent=4, sort_keys=True)

        diff = list(difflib.unified_diff(expected.split('\n'), actual.split('\n'), n=20))
        if diff:
            raise AssertionError("Assert JSON failed:\n{diff}".format(
                diff = '\n'.join(diff),
            ))

    def assertSqlResults(self, conn, sql, *rows):
        """
            Example:

            self.assertSqlResults(self.db_conn, '''
                select *
                from some_table
                order by logdate
            ''',
                [ 'logdate',  'col1',  'col2',  'metadata',  ],
                [ time1,      'abc1',  'def',   1,           ],
                [ time1,      'abc1',  'def',   1,           ],
                [ time2,      'abc2',  'def',   1,           ],
                [ time3,      'abc1',  'def',   1,           ],
            )

        """
        header, rows = rows[0], rows[1:]
        results = fetch_results(conn, sql)

        expected = text_table(header, rows)

# Path: wizzat/sqlhelper.py
# def fetch_results(conn, sql, **bind_params):
#     """
#     Immediately fetches the SQL results into memory
#     Trades memory for the ability to immediately execute another query
#     """
#     global _log_func
#     try:
#         cur = conn.cursor()
#         if _log_func:
#             _log_func(cur, sql, bind_params)
# 
#         cur.execute(sql, bind_params)
#         return cur.fetchall()
#     finally:
#         cur.close()
# 
# Path: wizzat/util.py
# def assert_online():
#     """
#         This method tests the OFFLINE environment variable and throws OfflineError().  This directly
#         hooks into @skip_offline, but can be used in other situations as well.
#     """
#     if not is_online():
#         raise OfflineError()
# 
# class OfflineError(Exception): pass
# 
# def reset_online():
#     """
#         Resets the global offline state
#     """
#     global _offline
#     _offline = os.environ.get('OFFLINE', False) == False
# 
# def set_online(value):
#     """
#         Sets the global offline state
#     """
#     global _offline
#     _offline = value
# 
# def is_online():
#     """
#         This method tests the OFFLINE environment variable and the global offline state.
#     """
#     global _offline
#     return _offline
# 
# Path: wizzat/dateutil.py
# def reset_now():
#     """
#     All future calls to `wizzat.now()` will return the current time as of reset_now()
#     """
#     set_now(None)
#     set_now(now())
# 
# Path: wizzat/textutil.py
# def text_table(header, rows, **options):
#     options = set_strict_defaults(options,
#         row_dicts = False,
#         col_trunc_len = None,
#     )
# 
#     if not header:
#         return '[]'
# 
#     fmt_rows = []
#     field_sizes = [ len(name) for name in header ]
# 
#     for row in rows:
#         if options['row_dicts']:
#             fmt_row = [ format_value(row.get(name, '')) for name in header ]
#         else:
#             if len(row) != len(header):
#                 raise TypeError("Expected row of length {}, got {}".format(
#                     len(header),
#                     len(row)),
#                 )
# 
#             fmt_row = [ format_value(value) for value in row ]
# 
#         _update_sizes(field_sizes, fmt_row)
#         fmt_rows.append(fmt_row)
# 
#     header_sep = _format_hline(field_sizes, '=', '+')
#     row_sep = _format_hline(field_sizes, '-', '+')
# 
#     fp = io.StringIO()
#     fp.write(row_sep)
#     fp.write(_format_row(field_sizes, header))
#     fp.write(header_sep)
# 
#     for row in fmt_rows:
#         fp.write(_format_row(field_sizes, row))
#         fp.write(row_sep)
# 
#     return fp.getvalue()


# Path: wizzat/testutil.py
from builtins import *
from wizzat.sqlhelper import fetch_results
from wizzat.util import assert_online, OfflineError, reset_online, set_online, is_online
from wizzat.dateutil import reset_now
from wizzat.decorators import *
from wizzat.textutil import text_table
import unittest, difflib, functools, os, json
                import pghelper

        expected = json.dumps(obj1, indent=4, sort_keys=True)
        actual = json.dumps(obj2, indent=4, sort_keys=True)

        diff = list(difflib.unified_diff(expected.split('\n'), actual.split('\n'), n=20))
        if diff:
            raise AssertionError("Assert JSON failed:\n{diff}".format(
                diff = '\n'.join(diff),
            ))

    def assertSqlResults(self, conn, sql, *rows):
        """
            Example:

            self.assertSqlResults(self.db_conn, '''
                select *
                from some_table
                order by logdate
            ''',
                [ 'logdate',  'col1',  'col2',  'metadata',  ],
                [ time1,      'abc1',  'def',   1,           ],
                [ time1,      'abc1',  'def',   1,           ],
                [ time2,      'abc2',  'def',   1,           ],
                [ time3,      'abc1',  'def',   1,           ],
            )

        """
        header, rows = rows[0], rows[1:]
        results = fetch_results(conn, sql)

        expected = text_table(header, rows)

        actual   = text_table(header, results, row_dicts = True)