# 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 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 .helpers.movie_data import Actor, Director, Movie
    actor = None
    i = 0
    while not actor or len(actor.movies) < 2:
        actor = actors[i]
        i += 1

    assert len(backend.filter(Actor, {'movies': {'$all' : actor.movies}}))
    assert actor in backend.filter(Actor, {'movies': {'$all' : actor.movies}})


def test_invalid_query(backend, small_test_data):

    with pytest.raises(BaseException):
        backend.filter(Actor, {'$in' : Movie.cast})


def test_non_indexed_delete(backend, small_test_data):

    (movies, actors, directors) = small_test_data

    for movie in movies:
        if movie.get('director'):
            director = movie.director
            for directed_movie in backend.filter(Movie,{'director' : director}):
                backend.update(directed_movie,unset_fields = ['director'])
                backend.commit()
            backend.delete(director)

    backend.commit()


# 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 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/test_querying.py
import pytest
from .helpers.movie_data import Actor, Director, Movie

    actor = None
    i = 0
    while not actor or len(actor.movies) < 2:
        actor = actors[i]
        i += 1

    assert len(backend.filter(Actor, {'movies': {'$all' : actor.movies}}))
    assert actor in backend.filter(Actor, {'movies': {'$all' : actor.movies}})


def test_invalid_query(backend, small_test_data):

    with pytest.raises(BaseException):
        backend.filter(Actor, {'$in' : Movie.cast})


def test_non_indexed_delete(backend, small_test_data):

    (movies, actors, directors) = small_test_data

    for movie in movies:
        if movie.get('director'):
            director = movie.director
            for directed_movie in backend.filter(Movie,{'director' : director}):
                backend.update(directed_movie,unset_fields = ['director'])
                backend.commit()
            backend.delete(director)

    backend.commit()


    directors = backend.filter(Director,{})