# Path: tests/core/external.py
# class BaseDocument (seeker.Indexable):
#     base_field = dsl.String()
# 
# Path: tests/core/mappings.py
# class DjangoBookDocument (seeker.ModelIndex):
#     class Meta:
# class DerivedDocument (BaseDocument):
#     def queryset(cls):
# 
# Path: tests/core/models.py
# class Book (models.Model):
#     title = models.CharField(max_length=200)
#     authors = models.ManyToManyField(Author, related_name='books', blank=True)
#     category = models.ForeignKey(Category, related_name='books', null=True, blank=True)
#     date_published = models.DateField(default=datetime.date.today)
#     pages = models.IntegerField(default=0)
#     in_print = models.BooleanField(default=True)
# 
#     def __str__(self):
#         return self.title
# 
# class Category (models.Model):
#     name = models.CharField(max_length=100)
# 
#     def __str__(self):
#         return self.name


from django.core.management import call_command
from django.test import TestCase
from .external import BaseDocument
from .mappings import BookDocument, DerivedDocument, DjangoBookDocument
from .models import Book, Category
import seeker
        # All the documents (so far) in these tests are in the "core" app.
        self.assertEqual(set(seeker.app_documents['core']), set(seeker.documents))

    def test_query(self):
        results = BookDocument.search().query('query_string', query='django').execute()
        self.assertEqual(set(int(r.meta.id) for r in results), set([2]))
        results = BookDocument.search().query('term', title='herd').execute()
        self.assertEqual(set(int(r.meta.id) for r in results), set([1]))
        self.assertIsInstance(results[0], BookDocument)
        # Test multi-model seeker.search
        results = seeker.search(models=(Book,)).query('term', title='herd').execute()
        self.assertEqual(set(int(r.meta.id) for r in results), set([1]))
        self.assertIsInstance(results[0], BookDocument)

    def test_filter(self):
        results = BookDocument.search().filter('term', **{'authors.raw': 'Alexa Watson'}).execute()
        self.assertEqual(set(r.title for r in results), set(['Herding Cats', 'Law School Sucks']))
        results = BookDocument.search().filter('term', in_print=False).execute()
        self.assertEqual(results.hits.total, 1)
        self.assertEqual(results[0].meta.id, '3')

    def test_facets(self):
        facet = seeker.TermsFacet('category.raw')
        search = facet.apply(BookDocument.search())
        data = facet.data(search.execute())
        self.assertIn('buckets', data)
        self.assertEqual(data['buckets'], [{'key': 'Non-Fiction', 'doc_count': 2}, {'key': 'Fiction', 'doc_count': 1}])

    def test_filtered_queryset(self):
        self.assertEqual(DjangoBookDocument.search().count(), 1)

# Path: tests/core/external.py
# class BaseDocument (seeker.Indexable):
#     base_field = dsl.String()
# 
# Path: tests/core/mappings.py
# class DjangoBookDocument (seeker.ModelIndex):
#     class Meta:
# class DerivedDocument (BaseDocument):
#     def queryset(cls):
# 
# Path: tests/core/models.py
# class Book (models.Model):
#     title = models.CharField(max_length=200)
#     authors = models.ManyToManyField(Author, related_name='books', blank=True)
#     category = models.ForeignKey(Category, related_name='books', null=True, blank=True)
#     date_published = models.DateField(default=datetime.date.today)
#     pages = models.IntegerField(default=0)
#     in_print = models.BooleanField(default=True)
# 
#     def __str__(self):
#         return self.title
# 
# class Category (models.Model):
#     name = models.CharField(max_length=100)
# 
#     def __str__(self):
#         return self.name


# Path: tests/core/tests.py
from django.core.management import call_command
from django.test import TestCase
from .external import BaseDocument
from .mappings import BookDocument, DerivedDocument, DjangoBookDocument
from .models import Book, Category
import seeker

        # All the documents (so far) in these tests are in the "core" app.
        self.assertEqual(set(seeker.app_documents['core']), set(seeker.documents))

    def test_query(self):
        results = BookDocument.search().query('query_string', query='django').execute()
        self.assertEqual(set(int(r.meta.id) for r in results), set([2]))
        results = BookDocument.search().query('term', title='herd').execute()
        self.assertEqual(set(int(r.meta.id) for r in results), set([1]))
        self.assertIsInstance(results[0], BookDocument)
        # Test multi-model seeker.search
        results = seeker.search(models=(Book,)).query('term', title='herd').execute()
        self.assertEqual(set(int(r.meta.id) for r in results), set([1]))
        self.assertIsInstance(results[0], BookDocument)

    def test_filter(self):
        results = BookDocument.search().filter('term', **{'authors.raw': 'Alexa Watson'}).execute()
        self.assertEqual(set(r.title for r in results), set(['Herding Cats', 'Law School Sucks']))
        results = BookDocument.search().filter('term', in_print=False).execute()
        self.assertEqual(results.hits.total, 1)
        self.assertEqual(results[0].meta.id, '3')

    def test_facets(self):
        facet = seeker.TermsFacet('category.raw')
        search = facet.apply(BookDocument.search())
        data = facet.data(search.execute())
        self.assertIn('buckets', data)
        self.assertEqual(data['buckets'], [{'key': 'Non-Fiction', 'doc_count': 2}, {'key': 'Fiction', 'doc_count': 1}])

    def test_filtered_queryset(self):
        self.assertEqual(DjangoBookDocument.search().count(), 1)

        all_books = set(r.meta.id for r in BookDocument.search().execute())