from os.path import abspath, dirname, join

from django.test import TestCase
from unittest import skipIf

from sopn_parsing.helpers.text_helpers import clean_text
from sopn_parsing.tests import should_skip_pdf_tests

try:
    from sopn_parsing.helpers.pdf_helpers import SOPNDocument
except ImportError:
    pass


class TestSOPNHelpers(TestCase):
    def test_clean_text(self):
        text = "\n C andidates (Namés)"
        self.assertEqual(clean_text(text), "candidates")

    @skipIf(should_skip_pdf_tests(), "Required PDF libs not installed")
    def test_sopn_document(self):
        example_doc_path = abspath(
            join(dirname(__file__), "data/sopn-berkeley-vale.pdf")
        )

        # TODO: pass in a list of documents from get_all_documents_with_source()
        doc = SOPNDocument(open(example_doc_path, "rb"))
        self.assertSetEqual(
            doc.document_heading,
            {
                # Header
                "the",
                "statement",
                "of",
                "persons",
                "nominated",
                "for",
                "stroud",
                "district",
                "berkeley",
                "vale",
                "council",
                "on",
                "thursday",
                "february",
                "28",
                "2019",
                # table headers
                "candidate",
                "name",
                "description",
                "proposer",
                "reason",
                "why",
                "no",
                "longer",
                "(if",
                "any)",
                # candidates
                "simpson",
                "jane",
                "eleanor",
                "liz",
                "ashton",
                "lindsey",
                "simpson",
                "labour",
                "green",
                "party",
                # More words here?
                "election",
                "a",
                "councillor",
                "following",
                "is",
                "as",
            },
        )

        self.assertEqual(len(doc.pages), 1)
        self.assertEqual(
            doc.get_pages_by_ward_name("berkeley")[0].page_number, 1
        )

    @skipIf(should_skip_pdf_tests(), "Required PDF libs not installed")
    def test_multipage_doc(self):

        example_doc_path = abspath(
            join(dirname(__file__), "data/NI-Assembly-Election-2016.pdf")
        )
        # TODO: pass in a list of documents from get_all_documents_with_source()
        doc = SOPNDocument(open(example_doc_path, "rb"))
        self.assertEqual(len(doc.pages), 9)
        na_wards = doc.get_pages_by_ward_name("north antrim")
        self.assertEqual(len(na_wards), 5)
        self.assertEqual(na_wards[0].page_number, 5)

        doc = SOPNDocument(open(example_doc_path, "rb"))
        ulster_wards = doc.get_pages_by_ward_name("ulster")
        self.assertEqual(len(ulster_wards), 4)
        self.assertEqual(ulster_wards[0].page_number, 1)

@@ -156,16 +156,93 @@
 an_text%0A
+from sopn_parsing.helpers.extract_pages import get_all_documents_with_source%0A
 from sop
@@ -766,25 +766,24 @@
       )%0A
-%0A
         
 # TODO: 
@@ -778,79 +778,104 @@
     
-# TODO: pass in a list of documents from get_all_documents_with_source(
+all_documents_with_source = get_all_documents_with_source(%0A            example_doc_path%0A        
 )%0A  
@@ -891,32 +891,45 @@
  = SOPNDocument(
+%0A            
 open(example_doc
@@ -932,32 +932,68 @@
 _doc_path, %22rb%22)
+, all_documents_with_source%0A        
 )%0A        self.a
@@ -2324,113 +2324,8 @@
 , 1)
-%0A        self.assertEqual(%0A            doc.get_pages_by_ward_name(%22berkeley%22)%5B0%5D.page_number, 1%0A        )
 %0A%0A  
@@ -2428,17 +2428,16 @@
 (self):%0A
-%0A
         
@@ -2548,24 +2548,25 @@
       )%0A
+%0A
         
 # TODO: 
@@ -2561,79 +2561,104 @@
     
-# TODO: pass in a list of documents from get_all_documents_with_source(
+all_documents_with_source = get_all_documents_with_source(%0A            example_doc_path%0A        
 )%0A  
@@ -2674,32 +2674,45 @@
  = SOPNDocument(
+%0A            
 open(example_doc
@@ -2727,157 +2727,88 @@
 rb%22)
-)%0A        self.assertEqual(len(doc.pages), 9)%0A        na_wards = doc.get_pages_by_ward_name(%22north antrim%22)%0A        self.assertEqual(len(na_wards), 5
+, all_documents_with_source%0A        )%0A        import pdb%0A%0A        pdb.set_trace(
 )%0A  
@@ -2834,34 +2834,25 @@
 ual(
-na_wards%5B0%5D.page_number, 5
+len(doc.pages), 9
 )%0A%0A 
@@ -2862,217 +2862,148 @@
     
-doc = SOPNDocument(open(example_doc_path, %22rb%22))%0A        ulster_wards = doc.get_pages_by_ward_name(%22ulster%22)%0A        self.assertEqual(len(ulster_wards), 4)%0A        self.assertEqual(ulster_wards%5B0%5D.page_number, 1
+for doc_info in doc.match_all_pages():%0A            self.assertEqual(doc_info%5B0%5D, doc.pages%5B0%5D)%0A            self.assertEqual(doc_info%5B1%5D, %221,2%22
 )%0A

Add list of documents to sopn tests