# Path: tests/conftest.py
# def _sql_backend(request, engine, **kwargs):
# 
#     meta = MetaData(engine)
#     meta.reflect()
#     meta.drop_all()
#     # we enable foreign key checks for SQLITE
#     if str(engine.url).startswith('sqlite://'):
#         engine.connect().execute('pragma foreign_keys=ON')
# 
#     if not 'ondelete' in kwargs:
#         kwargs['ondelete'] = 'CASCADE'
#     backend = SqlBackend(engine=engine, **kwargs)
#     backend.init_schema()
#     backend.create_schema()
# 
#     def finalizer():
#         backend.rollback()
#         del backend.connection
#         print("Dropping schema...")
#         # we disable foreign key checks for SQLITE (as dropping tables with circular foreign keys won't work otherwise...)
#         if str(engine.url).startswith('sqlite://'):
#             engine.connect().execute('pragma foreign_keys=OFF')
#         meta = MetaData(engine)
#         meta.reflect()
#         meta.drop_all()
#         print("Done...")
# 
#     request.addfinalizer(finalizer)
# 
#     return backend
# 
# def get_sql_engine():
#     url = os.environ.get('BLITZDB_SQLALCHEMY_URL', memory_url)
#     engine = create_engine(url, echo=False)
#     # we make sure foreign keys are enforced...
#     return engine
# 
# Path: tests/helpers/movie_data.py
# class Actor(Document):
# 
#     name = CharField(indexed = True)
#     gross_income_m = FloatField(indexed = True)
#     salary_amount = FloatField(indexed = True,key = 'salary.amount')
#     salary_currency = CharField(indexed = True,key = 'salary.currency')
#     appearances = IntegerField(indexed = True)
#     birth_year = IntegerField(indexed = True)
#     favorite_food = ManyToManyField('Food')
#     is_funny = BooleanField(indexed = True)
#     movies = ManyToManyField('Movie',backref = 'actors')
# 
# class Director(Document):
# 
#     """
#     Warning: There is a circular foreign key relationship between
#     Director and Movie, hence trying to save a pair of those objects
#     that point to each other will yield an exception for e.g.
#     the Postgres backend.
#     """
# 
#     name = CharField(indexed = True)
#     favorite_actor = ForeignKeyField('Actor')
#     best_movie = ForeignKeyField('Movie',unique=True,backref = 'best_of_director')
# 
# class Food(Document):
# 
#     name = CharField(indexed = True)
# 
# class Movie(Document):
# 
#     title = CharField(nullable = True,indexed = True)
#     director = ForeignKeyField(related = 'Director',nullable = True,backref = 'movies')
#     cast = ManyToManyField(related = 'Actor')
#     year = IntegerField(indexed = True)
#     best_actor = ForeignKeyField('Actor',backref = 'best_movies')
# 
#     class Meta(Document.Meta):
# 
#         dbref_includes = ['title','year']


import pytest
from ..conftest import _sql_backend, get_sql_engine
from ..helpers.movie_data import Actor, Director, Food, Movie



@pytest.fixture
def backend(request):
    engine = get_sql_engine()

# Path: tests/conftest.py
# def _sql_backend(request, engine, **kwargs):
# 
#     meta = MetaData(engine)
#     meta.reflect()
#     meta.drop_all()
#     # we enable foreign key checks for SQLITE
#     if str(engine.url).startswith('sqlite://'):
#         engine.connect().execute('pragma foreign_keys=ON')
# 
#     if not 'ondelete' in kwargs:
#         kwargs['ondelete'] = 'CASCADE'
#     backend = SqlBackend(engine=engine, **kwargs)
#     backend.init_schema()
#     backend.create_schema()
# 
#     def finalizer():
#         backend.rollback()
#         del backend.connection
#         print("Dropping schema...")
#         # we disable foreign key checks for SQLITE (as dropping tables with circular foreign keys won't work otherwise...)
#         if str(engine.url).startswith('sqlite://'):
#             engine.connect().execute('pragma foreign_keys=OFF')
#         meta = MetaData(engine)
#         meta.reflect()
#         meta.drop_all()
#         print("Done...")
# 
#     request.addfinalizer(finalizer)
# 
#     return backend
# 
# def get_sql_engine():
#     url = os.environ.get('BLITZDB_SQLALCHEMY_URL', memory_url)
#     engine = create_engine(url, echo=False)
#     # we make sure foreign keys are enforced...
#     return engine
# 
# Path: tests/helpers/movie_data.py
# class Actor(Document):
# 
#     name = CharField(indexed = True)
#     gross_income_m = FloatField(indexed = True)
#     salary_amount = FloatField(indexed = True,key = 'salary.amount')
#     salary_currency = CharField(indexed = True,key = 'salary.currency')
#     appearances = IntegerField(indexed = True)
#     birth_year = IntegerField(indexed = True)
#     favorite_food = ManyToManyField('Food')
#     is_funny = BooleanField(indexed = True)
#     movies = ManyToManyField('Movie',backref = 'actors')
# 
# class Director(Document):
# 
#     """
#     Warning: There is a circular foreign key relationship between
#     Director and Movie, hence trying to save a pair of those objects
#     that point to each other will yield an exception for e.g.
#     the Postgres backend.
#     """
# 
#     name = CharField(indexed = True)
#     favorite_actor = ForeignKeyField('Actor')
#     best_movie = ForeignKeyField('Movie',unique=True,backref = 'best_of_director')
# 
# class Food(Document):
# 
#     name = CharField(indexed = True)
# 
# class Movie(Document):
# 
#     title = CharField(nullable = True,indexed = True)
#     director = ForeignKeyField(related = 'Director',nullable = True,backref = 'movies')
#     cast = ManyToManyField(related = 'Actor')
#     year = IntegerField(indexed = True)
#     best_actor = ForeignKeyField('Actor',backref = 'best_movies')
# 
#     class Meta(Document.Meta):
# 
#         dbref_includes = ['title','year']


# Path: tests/sql/conftest.py
import pytest
from ..conftest import _sql_backend, get_sql_engine
from ..helpers.movie_data import Actor, Director, Food, Movie




@pytest.fixture
def backend(request):
    engine = get_sql_engine()

    backend = _sql_backend(request, engine)