from unittest.mock import patch
from django.test import TestCase
from derrida.books.models import Reference, Instance
from derrida.books.search_indexes import ReferenceIndex


class TestReferenceIndex(TestCase):
    fixtures = ['test_references.json']

    def setUp(self):
        '''None of the Instacefixtures have slugs, so generate them'''
        for instance in Instance.objects.all():
            instance.slug = instance.generate_safe_slug()
            instance.save()

        self.refindex = ReferenceIndex()

    def test_prepare_instance_slug(self):

        # get a ReferenceIndex object
        refindex = self.refindex
        # get a reference
        reference = Reference.objects.first()
        # not a book section (none in test set are)
        # should return the slug of its instance
        slug = refindex.prepare_instance_slug(reference)
        assert slug == reference.instance.slug

        # create a work as a 'collected in'
        ecrit = Instance.objects.get(slug__icontains='lecriture-et-la')
        debat = Instance.objects.get(slug__icontains='le-debat-sur')
        # make ecrit a 'section' of debat
        ecrit.collected_in = debat
        ecrit.save()
        # get a reference from ecrit
        reference = Reference.objects.filter(instance=ecrit).first()
        # should return the slug for debat not ecrit
        slug = refindex.prepare_instance_slug(reference)
        assert slug == debat.slug

    def test_prepare_book_page_sort(self):

        refindex = self.refindex
        # get all references by pk
        references = Reference.objects.all().order_by('pk')
        expected_book_pages = [
            0, 0, 0, 0, 62, 171, 0, 87, 87, 44, 126, 148, 355, -495, 23, 256,
            0, 38, 43, 0
        ]
        i = 0
        # check that each one's page number is convered to an integer
        # as expected
        for reference in references:
            integered = refindex.prepare_book_page_sort(reference)
            assert expected_book_pages[i] == integered
            i += 1
        # check a few variant patterns
        reference = Reference.objects.first()
        # page includes back reference
        reference.book_page = '(452a)47p'
        # should be just 47
        assert refindex.prepare_book_page_sort(reference) == 47
        # page is a chapter or something we simply can't guess at
        reference.book_page = '___(chapter VI)'
        # should return  0 since there's no way to sort definitively
        assert refindex.prepare_book_page_sort(reference) == 0

@@ -1476,33 +1476,32 @@
 age_sort(self):%0A
-%0A
         refindex
@@ -1509,32 +1509,33 @@
 = self.refindex%0A
+%0A
         # get al
@@ -1532,15 +1532,14 @@
   # 
-get all
+sample
  ref
@@ -1548,184 +1548,121 @@
 ence
-s
  b
-y pk%0A        references = Reference.objects.all().order_by('pk')%0A        expected_book_pages = %5B%0A            0, 0, 0
+ook pages values with expected sort value%0A        page_values = %5B(''
 , 0
+)
 , 
-62, 171, 0, 87, 87, 44, 126, 148, 355, -495, 23, 256
+('62p', 62), ('44s', 44), ('v', -495)
 ,%0A  
@@ -1675,456 +1675,183 @@
     
-0
+('38'
 , 38
+)
 , 
-43, 0%0A        %5D%0A        i = 0%0A        # check that each one's page number is convered to an integer%0A        # as expected%0A        for reference in references:%0A            integered = refindex.prepare_book_page_sort(reference)%0A            assert expected_book_pages%5Bi%5D == integered%0A            i += 1%0A        # check a few variant patterns%0A        reference = Reference.objects.first()%0A        # page includes back reference%0A        r
+('(452a)47p', 47), ('22-23', 22), ('L', -450),%0A            ('___(chapter VI)', 0)%5D%0A%0A        for input_val, expected_val in page_values:%0A            ref = R
 eference
 .boo
@@ -1838,33 +1838,33 @@
  ref = Reference
-.
+(
 book_page = '(45
@@ -1860,59 +1860,32 @@
 page
- = '(452a)47p'%0A        # should be just 47%0A
+=input_val)%0A       
      
-   
 asse
@@ -1926,264 +1926,23 @@
 (ref
-erence) == 47%0A        # page is a chapter or something we simply can't guess at%0A        reference.book_page = '___(chapter VI)'%0A        # should return  0 since there's no way to sort definitively%0A        assert refindex.prepare_book_page_sort(reference) == 0
+) == expected_val%0A
 %0A

Refactor test for reference book page sort prep for readability