Skip to content

Commit

Permalink
get_data_fields with integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
bengolder committed Mar 12, 2016
1 parent 992ba3a commit 01ddaec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
18 changes: 11 additions & 7 deletions src/pdftk_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ def _clean_up_tmp_files(self):
path = self._tmp_files.pop()
os.remove(path)

def _get_file_contents(self, path, decode=False):
def _get_file_contents(self, path, decode=False, encoding=None):
"""given a file path, return the contents of the file
if decode is True, the contents will be decoded using the default
encoding
"""
bytestring = open(path, 'rb').read()
if decode:
return bytestring.decode(self.encoding)
return bytestring.decode(encoding or self.encoding)
return bytestring

def get_fdf(self, fp):
Expand All @@ -68,14 +68,18 @@ def get_fdf(self, fp):
tmp_outfile = self._write_tmp_file()
self.run_command([fp, 'generate_fdf',
'output', tmp_outfile])
return self._get_file_contents(
contents = self._get_file_contents(
tmp_outfile, decode=True)

def get_xfdf(self, fp):
return None
return contents

def get_data_fields(self, fp):
return None
fp = self._coerce_to_file_path(fp)
tmp_outfile = self._write_tmp_file()
self.run_command([fp, 'dump_data_fields_utf8',
'output', tmp_outfile])
contents = self._get_file_contents(
tmp_outfile, decode=True, encoding='utf-8')
return contents

def parse_fdf_fields(self, fdf_str):
yield None
Expand Down
10 changes: 9 additions & 1 deletion tests/integration/test_pdftk.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def setUp(self):
self.tmp_dir = 'data'
self.sample_form_path = 'data/sample_pdfs/sample_form.pdf'
self.fdf_str_sample = '%FDF-1.2\n%âãÏÓ\n1 0 obj \n<<\n/FDF \n<<\n/Fields [\n<<\n/V /Yes\n/T (Language 2 Check Box)\n>> \n<<\n/V ()\n/T (Address 2 Text Box)\n>> \n<<\n/V /Off\n/T (Language 3 Check Box)\n>> \n<<\n/V ()\n/T (City Text Box)\n>> \n<<\n/V /Off\n/T (Language 1 Check Box)\n>> \n<<\n/V /Off\n/T (Driving License Check Box)\n>> \n<<\n/V ()\n/T (Given Name Text Box)\n>> \n<<\n/V /Off\n/T (Language 5 Check Box)\n>> \n<<\n/V ()\n/T (House nr Text Box)\n>> \n<<\n/V (150)\n/T (Height Formatted Field)\n>> \n<<\n/V ()\n/T (Family Name Text Box)\n>> \n<<\n/V ()\n/T (Address 1 Text Box)\n>> \n<<\n/V /Off\n/T (Language 4 Check Box)\n>> \n<<\n/V ()\n/T (Postcode Text Box)\n>>]\n>>\n>>\nendobj \ntrailer\n\n<<\n/Root 1 0 R\n>>\n%%EOF\n'
self.data_fields_str_sample = '---\nFieldType: Text\nFieldName: Given Name Text Box\nFieldNameAlt: First name\nFieldFlags: 0\nFieldValue: \nFieldJustification: Left\nFieldMaxLength: 40\n---\nFieldType: Text\nFieldName: Family Name Text Box\nFieldNameAlt: Last name\nFieldFlags: 0\nFieldValue: \nFieldJustification: Left\nFieldMaxLength: 40\n---\nFieldType: Text\nFieldName: Address 1 Text Box\nFieldFlags: 0\nFieldValue: \nFieldJustification: Left\nFieldMaxLength: 40\n---\nFieldType: Text\nFieldName: House nr Text Box\nFieldNameAlt: House and floor\nFieldFlags: 0\nFieldValue: \nFieldJustification: Left\nFieldMaxLength: 20\n---\nFieldType: Text\nFieldName: Address 2 Text Box\nFieldFlags: 0\nFieldValue: \nFieldJustification: Left\nFieldMaxLength: 40\n---\nFieldType: Text\nFieldName: Postcode Text Box\nFieldFlags: 0\nFieldValue: \nFieldJustification: Left\nFieldMaxLength: 20\n---\nFieldType: Text\nFieldName: City Text Box\nFieldFlags: 0\nFieldValue: \nFieldJustification: Left\nFieldMaxLength: 40\n---\nFieldType: Text\nFieldName: Height Formatted Field\nFieldNameAlt: Value from 40 to 250 cm\nFieldFlags: 0\nFieldValue: 150\nFieldValueDefault: 150\nFieldJustification: Left\nFieldMaxLength: 20\n---\nFieldType: Button\nFieldName: Driving License Check Box\nFieldNameAlt: Car driving license\nFieldFlags: 0\nFieldValue: Off\nFieldJustification: Left\nFieldStateOption: Off\nFieldStateOption: Yes\n---\nFieldType: Button\nFieldName: Language 1 Check Box\nFieldFlags: 0\nFieldValue: Off\nFieldJustification: Left\nFieldStateOption: Off\nFieldStateOption: Yes\n---\nFieldType: Button\nFieldName: Language 2 Check Box\nFieldFlags: 0\nFieldValue: Yes\nFieldJustification: Left\nFieldStateOption: Off\nFieldStateOption: Yes\n---\nFieldType: Button\nFieldName: Language 3 Check Box\nFieldFlags: 0\nFieldValue: Off\nFieldJustification: Left\nFieldStateOption: Off\nFieldStateOption: Yes\n---\nFieldType: Button\nFieldName: Language 4 Check Box\nFieldFlags: 0\nFieldValue: Off\nFieldJustification: Left\nFieldStateOption: Off\nFieldStateOption: Yes\n---\nFieldType: Button\nFieldName: Language 5 Check Box\nFieldFlags: 0\nFieldValue: Off\nFieldJustification: Left\nFieldStateOption: Off\nFieldStateOption: Yes\n'

def test_pdftk_errors(self):
pdftk = PDFTKWrapper()
Expand All @@ -32,11 +33,18 @@ def test_pdftk_errors(self):
with self.assertRaises(PdftkError):
pdftk.run_command(args)

def test_generate_fdf(self):
def test_get_fdf(self):
pdftk = PDFTKWrapper()
results = pdftk.get_fdf(self.sample_form_path)
self.assertEqual(results, self.fdf_str_sample)

def test_get_data_fields(self):
pdftk = PDFTKWrapper()
results = pdftk.get_data_fields(self.sample_form_path)
self.assertEqual(results, self.data_fields_str_sample)






10 changes: 0 additions & 10 deletions tests/unit/test_pdftk.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ def test_get_fdf(self):
contents_getter.assert_called_once_with(
'tmp_path.fdf', decode=True)

def test_get_xfdf(self):
pdftk = PDFTKWrapper()
fp = 'something.pdf'
results = pdftk.get_xfdf(fp)

def test_get_data_fields(self):
pdftk = PDFTKWrapper()
fp = 'something.pdf'
results = pdftk.get_data_fields(fp)

def test_parse_fdf_fields(self):
pdftk = PDFTKWrapper()
fdf_sample = """
Expand Down

0 comments on commit 01ddaec

Please sign in to comment.