parent
bd26c9a38a
commit
8e65f68994
@ -0,0 +1,39 @@ |
||||
import sys |
||||
import os |
||||
import unittest |
||||
|
||||
base_dir = os.path.split( __file__ )[ 0 ] |
||||
|
||||
sys.path.append( "../.." ) # fudge! need this to allow a script to run within a package :-/ |
||||
from asl_cards.parse import PdfParser |
||||
|
||||
# --------------------------------------------------------------------- |
||||
|
||||
class TestCaseBase( unittest.TestCase ) : |
||||
"""Base for all test classes.""" |
||||
|
||||
def _test_pdf_parser( self , fname , expected_cards ) : |
||||
# parse the specified PDF |
||||
fname2 = os.path.join( base_dir , fname ) |
||||
if not os.path.isfile( fname2 ) : |
||||
raise RuntimeError( "Missing data file: {}".format( fname2 ) ) |
||||
pdf_parser = PdfParser( |
||||
#progress = lambda _,msg: print( msg , file=sys.stderr , flush=True ) |
||||
) |
||||
cards = pdf_parser.parse( fname2 , images=False ) |
||||
if False : |
||||
for c in cards : |
||||
print(c) |
||||
# check the results |
||||
if len(cards) != len(expected_cards) : |
||||
raise RuntimeError( "{}: got {} cards, expected {}.".format( fname , len(cards) , len(expected_cards) ) ) |
||||
if len(cards) == 0 : |
||||
return |
||||
# get the attributes we're interested in |
||||
card = expected_cards[0] |
||||
attrs = [ a for a in dir(card) if not a.startswith("_") and not callable(getattr(card,a)) ] |
||||
attrs.remove( "card_image" ) # this is messing things up :-/ |
||||
# compare the extracted cards with the expected results |
||||
for i in range(0,len(cards)) : |
||||
if not all( getattr(cards[i],a) == getattr(expected_cards[i],a) for a in attrs ) : |
||||
raise RuntimeError( "{}: Card mismatch ({}): got {}, expected {}.".format( fname , i , cards[i] , expected_cards[i] ) ) |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,67 @@ |
||||
#!/usr/bin/env python3 |
||||
|
||||
import sys |
||||
import os |
||||
import unittest |
||||
|
||||
from pdfminer.pdfparser import PDFSyntaxError |
||||
|
||||
from _test_case_base import TestCaseBase |
||||
from asl_cards.parse import AslCard |
||||
|
||||
# --------------------------------------------------------------------- |
||||
|
||||
class TestSyntheticData( TestCaseBase ) : |
||||
"""Run tests using the synthetic PDF files. |
||||
|
||||
We test with some generated test files, since the real "ASL Cards" files need to purchased, |
||||
so we can't keep them in source control. |
||||
""" |
||||
|
||||
def _test_pdf_parser( self , fname , expected_cards ) : |
||||
"""Test the PDF parser.""" |
||||
super()._test_pdf_parser( os.path.join("synthetic-data",fname) , expected_cards ) |
||||
|
||||
def test_null_file( self ) : |
||||
# try parsing a zero-byte file |
||||
self.assertRaises( |
||||
PDFSyntaxError , |
||||
self._test_pdf_parser , "null.pdf" , None |
||||
) |
||||
|
||||
def test_empty_file( self ) : |
||||
# try parsing an empty file |
||||
self._test_pdf_parser( "empty.pdf" , [] ) |
||||
|
||||
def test_1card_file( self ) : |
||||
# try parsing a file with 1 card |
||||
self._test_pdf_parser( "1-card.pdf" , [ |
||||
AslCard( page_id=1 , page_pos=0 , card_tag="Vehicle #1" , nationality="Moldovia" , name="Big Tank" ) , |
||||
] ) |
||||
|
||||
def test_2card_file( self ) : |
||||
# try parsing a file with 2 cards |
||||
self._test_pdf_parser( "2-cards.pdf" , [ |
||||
AslCard( page_id=1 , page_pos=0 , card_tag="Vehicle #1" , nationality="Moldovia" , name="Big Tank" ) , |
||||
AslCard( page_id=1 , page_pos=1 , card_tag="Vehicle #2" , nationality="Moldovia" , name="Little Tank" ) , |
||||
] ) |
||||
|
||||
def test_3card_file( self ) : |
||||
# try parsing a file with 3 cards |
||||
self._test_pdf_parser( "3-cards.pdf" , [ |
||||
AslCard( page_id=1 , page_pos=0 , card_tag="Vehicle #1" , nationality="Moldovia" , name="Big Tank" ) , |
||||
AslCard( page_id=1 , page_pos=1 , card_tag="Vehicle #2" , nationality="Moldovia" , name="Little Tank" ) , |
||||
AslCard( page_id=2 , page_pos=0 , card_tag="Ordnance #1" , nationality="Moldovia" , name="Big Gun" ) , |
||||
] ) |
||||
|
||||
def test_bad_spacing( self ) : |
||||
# try parsing cards with bad spacing |
||||
self._test_pdf_parser( "bad-spacing.pdf" , [ |
||||
AslCard( page_id=1 , page_pos=0 , card_tag="Vehicle #1" , nationality="" , name="" ) , |
||||
AslCard( page_id=1 , page_pos=1 , card_tag="Vehicle #2" , nationality="Moldovia" , name="" ) , |
||||
] ) |
||||
|
||||
# --------------------------------------------------------------------- |
||||
|
||||
if __name__ == "__main__" : |
||||
unittest.main() |
Loading…
Reference in new issue