Skip to content

Commit

Permalink
Add tests and file comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
Manu committed May 20, 2018
1 parent 086a1e0 commit d89c4de
Show file tree
Hide file tree
Showing 21 changed files with 309 additions and 124 deletions.
2 changes: 0 additions & 2 deletions invoice2data/input/pdftotext.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ def to_text(path):
import shutil import shutil
from distutils import spawn #py2 compat from distutils import spawn #py2 compat




if spawn.find_executable("pdftotext"): #shutil.which('pdftotext'): if spawn.find_executable("pdftotext"): #shutil.which('pdftotext'):
out, err = subprocess.Popen( out, err = subprocess.Popen(
["pdftotext", '-layout', '-enc', 'UTF-8', path, '-'], ["pdftotext", '-layout', '-enc', 'UTF-8', path, '-'],
Expand Down
10 changes: 10 additions & 0 deletions invoice2data/test/common.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,10 @@
import os
import pkg_resources

def get_sample_files(extension):
compare_files = []
for path, subdirs, files in os.walk(pkg_resources.resource_filename(__name__, 'compare')):
for file in files:
if file.endswith(extension):
compare_files.append(os.path.join(path, file))
return compare_files
31 changes: 31 additions & 0 deletions invoice2data/test/compare/AmazonWebServices.json
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"amount": 4.11,
"amount_untaxed": 4.11,
"currency": "USD",
"date": "03/08/2014",
"desc": "Invoice 42183017 from Amazon Web Services",
"invoice_number": "42183017",
"issuer": "Amazon Web Services",
"lines": [
{
"description": "AWS Data Transfer",
"price_unit": "0.01"
},
{
"description": "Amazon Elastic Compute Cloud",
"price_unit": "1.87"
},
{
"description": "Amazon Glacier",
"price_unit": "2.22"
},
{
"description": "Amazon Simple Storage Service",
"price_unit": "0.01"
}
],
"partner_name": "Amazon Web Services, Inc.",
"partner_website": "aws.amazon.com"
}
]
File renamed without changes.
Binary file added invoice2data/test/compare/AmazonWebServices.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions invoice2data/test/compare/FlipkartInvoice.json
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"amount": 319.0,
"currency": "INR",
"date": "20/10/2015",
"desc": "Invoice #BLR_WFLD20151000982590 from Flipkart",
"invoice_number": "#BLR_WFLD20151000982590",
"issuer": "Flipkart",
"order_id": "OD304175096047380001"
}
]
File renamed without changes.
Binary file added invoice2data/test/compare/FlipkartInvoice.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions invoice2data/test/compare/QualityHosting.json
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,56 @@
[
{
"amount": 34.73,
"amount_untaxed": 34.73,
"currency": "EUR",
"date": "07/05/2014",
"desc": "Invoice 30064443 from QualityHosting AG",
"invoice_number": "30064443",
"issuer": "QualityHosting AG",
"lines": [
{
"desc": "Small Business StandardExchange 2010\nGrundgeb\u00fchr pro Einheit\nDienst: OUDJQ_strukan\n01.05.14-31.05.14",
"pos": "1",
"price": 3.89,
"qty": 1.0
},
{
"desc": "Small Business QualityExchange 2010\nGrundgeb\u00fchr pro Einheit\nDienst: OUDJQ_schneider\n01.05.14-31.05.14",
"pos": "2",
"price": 5.39,
"qty": 1.0
},
{
"desc": "Small Business QualityExchange 2010\nGrundgeb\u00fchr pro Einheit\nDienst: OUDJQ_minar\n01.05.14-31.05.14",
"pos": "3",
"price": 5.39,
"qty": 1.0
},
{
"desc": "Small Business QualityExchange 2010\nGrundgeb\u00fchr pro Einheit\nDienst: OUDJQ_mayr\n01.05.14-31.05.14",
"pos": "4",
"price": 5.39,
"qty": 1.0
},
{
"desc": "Small Business QualityExchange 2010\nGrundgeb\u00fchr pro Einheit\nDienst: OUDJQ_jenewein\n01.05.14-31.05.14",
"pos": "5",
"price": 5.39,
"qty": 1.0
},
{
"desc": "Small Business QualityExchange 2010\nGrundgeb\u00fchr pro Einheit\nDienst: OUDJQ_jauernik\n01.05.14-31.05.14\nQualityHosting AG - Uferweg 40-42 - D-63571 Gelnhausen\niViveLabs Ltd.\n93B Sai Yu Chung\nYuen Long, N.T.\nHong Kong\nPos. Menge Beschreibung Rabatt % VK-Preis Zeilenbetrag\nOhne Ohne MwSt.\nMwSt.",
"pos": "6",
"price": 5.39,
"qty": 1.0
},
{
"desc": "Small Business StandardExchange 2010\nGrundgeb\u00fchr pro Einheit\nDienst: OUDJQ_office\n01.05.14-31.05.14\n",
"pos": "7",
"price": 3.89,
"qty": 1.0
}
],
"vat": "DE 232 446 240"
}
]
13 changes: 13 additions & 0 deletions invoice2data/test/compare/free_fiber.json
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"amount": 29.99,
"amount_untaxed": 24.99,
"currency": "EUR",
"date": "02/07/2015",
"date_due": "2015-07-05 00:00:00",
"desc": "Invoice 562044387 from Free",
"invoice_number": "562044387",
"issuer": "Free",
"vat": "FR60421938861"
}
]
File renamed without changes.
5 changes: 0 additions & 5 deletions invoice2data/test/compare/invoices-output.csv

This file was deleted.

24 changes: 0 additions & 24 deletions invoice2data/test/compare/invoices-output.json

This file was deleted.

27 changes: 0 additions & 27 deletions invoice2data/test/compare/invoices-output.xml

This file was deleted.

10 changes: 10 additions & 0 deletions invoice2data/test/compare/oyo.json
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"amount": 1939.0,
"currency": "INR",
"date": "31/12/2017",
"desc": "Invoice IBZY2087 from OYO",
"invoice_number": "IBZY2087",
"issuer": "OYO"
}
]
File renamed without changes.
Binary file added invoice2data/test/compare/oyo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
143 changes: 77 additions & 66 deletions invoice2data/test/test_cli.py
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,8 @@
import os import os
import glob import glob
import filecmp import filecmp
import json
import shutil


try: try:
from StringIO import StringIO from StringIO import StringIO
Expand All @@ -12,100 +14,109 @@
from invoice2data.main import * from invoice2data.main import *
from invoice2data.extract.loader import read_templates from invoice2data.extract.loader import read_templates


from .common import *


class TestCLI(unittest.TestCase): class TestCLI(unittest.TestCase):
def setUp(self): def setUp(self):
self.templates = read_templates() self.templates = read_templates()
self.parser = create_parser() self.parser = create_parser()


def _get_test_file_path(self): def compare_json_content(self, test_file, json_file):
out_files = [None]*4 with open(test_file) as json_test_file, open(json_file) as json_json_file:
for path, subdirs, files in os.walk(pkg_resources.resource_filename(__name__, 'pdfs')): jdatatest = json.load(json_test_file)
for file in files: jdatajson = json.load(json_json_file)
if file == "2014-05-07 Invoice 30064443 from QualityHosting.pdf": # logger.info(jdatajson)
out_files[0] = os.path.join(path, file) # logger.info(jdatatest)
elif file == "2014-08-03 SALES Amazon Web Services aws.amazon.coUS.pdf": if jdatajson == jdatatest:
out_files[1] = os.path.join(path, file) logger.info("True")
elif file == "2015-01-29 PAYPAL ENVATO MKPL EN 4029357733 AU.pdf": return True
out_files[2] = os.path.join(path, file) else:
elif file == "2015-07-02-invoice_free_fiber.pdf": logger.info("False")
out_files[3] = os.path.join(path, file) return False
return out_files

def _get_test_file_content():
pass


def test_input(self): def test_input(self):
args = self.parser.parse_args(['--input-reader', 'pdftotext'] + self._get_test_file_path()) args = self.parser.parse_args(['--input-reader', 'pdftotext'] + get_sample_files('.pdf'))
main(args) main(args)


def test_output_name(self): def test_output_name(self):
test_file = 'inv_test_8asd89f78a9df.csv' test_file = 'inv_test_8asd89f78a9df.csv'
args = self.parser.parse_args(['--output-name', test_file, '--output-format', 'csv'] + self._get_test_file_path()) args = self.parser.parse_args(['--output-name', test_file, '--output-format', 'csv']
+ get_sample_files('.pdf'))
main(args) main(args)
self.assertTrue(os.path.exists(test_file)) self.assertTrue(os.path.exists(test_file))
os.remove(test_file) os.remove(test_file)


def test_debug(self): def test_debug(self):
args = self.parser.parse_args(['--debug'] + self._get_test_file_path()) args = self.parser.parse_args(['--debug'] + get_sample_files('.pdf'))
main(args) main(args)


# TODO: move result comparison to own test module. # TODO: move result comparison to own test module.
# TODO: parse output files instaed of comparing them byte-by-byte. # TODO: parse output files instaed of comparing them byte-by-byte.


def test_content_csv(self): def test_content_json(self):
for path, subdirs, files in os.walk(pkg_resources.resource_filename(__name__, 'compare')): pdf_files = get_sample_files('.pdf')
for file in files: json_files = get_sample_files('.json')
if file.endswith(".csv"): test_files = 'test_compare.json'
cmp_file = os.path.join(path, file) for pfile in pdf_files:

for jfile in json_files:
test_files = 'inv_test.csv' if pfile[:-4] == jfile[:-5]:
args = self.parser.parse_args(['--output-name', test_files, '--output-format', 'csv'] + self._get_test_file_path()) args = self.parser.parse_args(
['--output-name', test_files, '--output-format', 'json', pfile])
main(args)
compare_verified = self.compare_json_content(test_files, jfile)
print(compare_verified)
if not compare_verified:
self.assertTrue(False)
os.remove(test_files)
self.assertTrue(True)

def test_copy(self):
# folder = pkg_resources.resource_filename(__name__, 'pdfs')
directory = os.path.dirname("invoice2data/test/copy_test/pdf/")
os.makedirs(directory)
args = self.parser.parse_args(['--copy', 'invoice2data/test/copy_test/pdf'] + get_sample_files('.pdf'))
main(args) main(args)
# self.assertTrue(filecmp.cmp(test_files, cmp_file, shallow=False)) i = 0
os.remove(test_files) for path, subdirs, files in os.walk(pkg_resources.resource_filename(__name__, 'copy_test/pdf')):

def test_content_xml(self):
for path, subdirs, files in os.walk(pkg_resources.resource_filename(__name__, 'compare')):
for file in files: for file in files:
if file.endswith(".xml"): if file.endswith(".pdf"):
cmp_file = os.path.join(path, file) i += 1

shutil.rmtree('invoice2data/test/copy_test/', ignore_errors=True)
self.assertEqual(i, len(get_sample_files('.json')))
'''
if i != len(self._get_test_file_json_path()):
print(i)
self.assertTrue(True)
else:
print(i)
self.assertTrue(False, "Number of files not equal")
'''


test_files = 'inv_test.xml' # def test_template(self):
args = self.parser.parse_args(['--output-name', test_files, '--output-format', 'xml'] + self._get_test_file_path()) # directory = os.path.dirname("invoice2data/test/temp_test/")
main(args) # os.makedirs(directory)
# self.assertTrue(filecmp.cmp(test_files, cmp_file, shallow=False)) # args = self.parser.parse_args(['--template-folder', 'ACME-templates', self._get_test_file_path()])
os.remove(test_files) # main(args)
# shutil.rmtree('invoice2data/test/temp_test/', ignore_errors=True)
# self.assertTrue(args.template_folder)


def test_content_json(self): def test_exclude_template(self):
for path, subdirs, files in os.walk(pkg_resources.resource_filename(__name__, 'compare')): for path, subdirs, files in os.walk(pkg_resources.resource_filename(__name__, 'compare')):
for file in files: for file in files:
if file.endswith(".json"): if file.endswith("oyo.pdf"):
cmp_file = os.path.join(path, file) my_file = os.path.join(path, file)

directory = os.path.dirname("invoice2data/test/temp_test/")
test_files = 'inv_test.json' os.makedirs(directory)
args = self.parser.parse_args(['--output-name', test_files, '--output-format', 'json'] + self._get_test_file_path()) shutil.copy('invoice2data/extract/templates/com/com.oyo.invoice.yml', 'invoice2data/test/temp_test/')
args = self.parser.parse_args(['--exclude-built-in-templates',
'--template-folder',
directory,
my_file])
main(args) main(args)
# self.assertTrue(filecmp.cmp(test_files, cmp_file, shallow=False)) shutil.rmtree('invoice2data/test/temp_test/')
os.remove(test_files)

# def test_copy(self):
# parser = create_parser()
# folder = pkg_resources.resource_filename(__name__, 'pdfs')
# args = parser.parse_args(['--copy', '/invoice2data/test/', self._get_test_file_path()])
# self.assertTrue(args.copy)

# def test_template(self):
# parser = create_parser()
# folder = pkg_resources.resource_filename(__name__, 'pdfs')
# args = parser.parse_args(['--template-folder', 'ACME-templates', self._get_test_file_path()])
# self.assertTrue(args.template_folder)


# def test_exclude_template(self):
# parser = create_parser()
# folder = pkg_resources.resource_filename(__name__, 'pdfs')
# args = parser.parse_args(['--exclude-built-in-templates', '--template-folder', 'ACME-templates', self._get_test_file_path()])
# self.assertTrue(args.exclude_built_in_templates)



if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Loading

0 comments on commit d89c4de

Please sign in to comment.