# Path: yawn/utilities/database.py
# def close_on_exception(func):
#     def wrapper(*args, **kwargs):
# def current_time():
# 
# Path: yawn/utilities/database.py
# def close_on_exception(func):
#     """
#     A wrapper to close the database connection if a DB error occurs,
#     so that it will get re-opened on the next use.
# 
#     Squashes the exception and logs it.
#     """
# 
#     @functools.wraps(func)
#     def wrapper(*args, **kwargs):
#         try:
#             func(*args, **kwargs)
#         except OperationalError:
#             logger.error('Database error, closing connection', exc_info=True)
#             db.connection.close()
#             assert db.connection.closed_in_transaction is False, \
#                 'Could not close connection, probably because this wrapper ' \
#                 'was used inside an transaction.atomic() block.'
# 
#     return wrapper


import datetime
import pytest
from django.db import connection
from yawn.utilities import database
from yawn.utilities.database import close_on_exception




@pytest.mark.no_transaction
def test_close_on_exception():

    @close_on_exception
    def example_disconnect():
        with connection.cursor() as cursor:

            # kill the current connection, which will raise a django.db.OperationalError
            cursor.execute('select pg_terminate_backend(pg_backend_pid())')

            # which means we never get here
            cursor.execute('select 1')
            assert False, 'exception was not raised'

    # this will cause a disconnect:
    example_disconnect()

    # but the exception is caught, and on retry the database reconnects:
    with connection.cursor() as cursor:
        cursor.execute('select 1')


def test_current_time():

# Path: yawn/utilities/database.py
# def close_on_exception(func):
#     def wrapper(*args, **kwargs):
# def current_time():
# 
# Path: yawn/utilities/database.py
# def close_on_exception(func):
#     """
#     A wrapper to close the database connection if a DB error occurs,
#     so that it will get re-opened on the next use.
# 
#     Squashes the exception and logs it.
#     """
# 
#     @functools.wraps(func)
#     def wrapper(*args, **kwargs):
#         try:
#             func(*args, **kwargs)
#         except OperationalError:
#             logger.error('Database error, closing connection', exc_info=True)
#             db.connection.close()
#             assert db.connection.closed_in_transaction is False, \
#                 'Could not close connection, probably because this wrapper ' \
#                 'was used inside an transaction.atomic() block.'
# 
#     return wrapper


# Path: yawn/utilities/tests/test_database.py
import datetime
import pytest
from django.db import connection
from yawn.utilities import database
from yawn.utilities.database import close_on_exception





@pytest.mark.no_transaction
def test_close_on_exception():

    @close_on_exception
    def example_disconnect():
        with connection.cursor() as cursor:

            # kill the current connection, which will raise a django.db.OperationalError
            cursor.execute('select pg_terminate_backend(pg_backend_pid())')

            # which means we never get here
            cursor.execute('select 1')
            assert False, 'exception was not raised'

    # this will cause a disconnect:
    example_disconnect()

    # but the exception is caught, and on retry the database reconnects:
    with connection.cursor() as cursor:
        cursor.execute('select 1')


def test_current_time():

    assert isinstance(database.current_time(), datetime.datetime)