Skip to content

Commit

Permalink
Remove test method duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
tonnydourado committed Nov 26, 2016
1 parent 8185e15 commit 535e2b8
Showing 1 changed file with 40 additions and 117 deletions.
157 changes: 40 additions & 117 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,8 @@
from ofxparse.ofxparse import OfxFile, OfxPreprocessedFile, OfxParserException, soup_maker


class TestOfxPreprocessedFile(TestCase):

def testPreprocess(self):
fh = six.BytesIO(six.b("""OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE
<OFX><DTASOF><![CDATA[></tricky]]><LEAVE ALONE><VAL.UE>a<VAL_UE>b<TE_ST></TE_ST><TE.ST></TE.ST><INVBAL><BALLIST><BAL><NAME>Net<DTASOF>2222</BAL><BAL><NAME>Gross<DTASOF>3333</BAL></BALLIST></INVBAL></OFX>
"""))
expect = """OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE
<OFX><DTASOF><![CDATA[></tricky]]><LEAVE ALONE></DTASOF><VAL.UE>a</VAL.UE><VAL_UE>b</VAL_UE><TE_ST></TE_ST><TE.ST></TE.ST><INVBAL><BALLIST><BAL><NAME>Net</NAME><DTASOF>2222</DTASOF></BAL><BAL><NAME>Gross</NAME><DTASOF>3333</DTASOF></BAL></BALLIST></INVBAL></OFX>
"""
ofx_file = OfxPreprocessedFile(fh)
data = ofx_file.fh.read()
self.assertEqual(data, expect)
class TestOfxFile(TestCase):
OfxFileCls = OfxFile

def testHeaders(self):
expect = {"OFXHEADER": six.u("100"),
Expand All @@ -60,6 +32,27 @@ def testHeaders(self):
ofx = OfxParser.parse(f)
self.assertEqual(expect, ofx.headers)

def testTextFileHandler(self):
with open_file("bank_medium.ofx") as fh:
with open_file("bank_medium.ofx", mode="r") as fh_str:
ofx_file = self.OfxFileCls(fh)
headers = ofx_file.headers
data = ofx_file.fh.read()

self.assertTrue(type(data) is six.text_type)
for key, value in six.iteritems(headers):
self.assertTrue(type(key) is six.text_type)
self.assertTrue(type(value) is not six.binary_type)

ofx_file = self.OfxFileCls(fh_str)
headers = ofx_file.headers
data = ofx_file.fh.read()

self.assertTrue(type(data) is six.text_type)
for key, value in six.iteritems(headers):
self.assertTrue(type(key) is six.text_type)
self.assertTrue(type(value) is not six.binary_type)

def testUTF8(self):
fh = six.BytesIO(six.b("""OFXHEADER:100
DATA:OFXSGML
Expand All @@ -71,7 +64,7 @@ def testUTF8(self):
NEWFILEUID:NONE
"""))
ofx_file = OfxPreprocessedFile(fh)
ofx_file = self.OfxFileCls(fh)
headers = ofx_file.headers
data = ofx_file.fh.read()

Expand All @@ -91,7 +84,7 @@ def testCP1252(self):
OLDFILEUID:NONE
NEWFILEUID:NONE
"""))
ofx_file = OfxPreprocessedFile(fh)
ofx_file = self.OfxFileCls(fh)
headers = ofx_file.headers
result = ofx_file.fh.read()

Expand All @@ -111,7 +104,7 @@ def testUTF8Japanese(self):
OLDFILEUID:NONE
NEWFILEUID:NONE
"""))
ofx_file = OfxPreprocessedFile(fh)
ofx_file = self.OfxFileCls(fh)
headers = ofx_file.headers
result = ofx_file.fh.read()

Expand All @@ -122,111 +115,41 @@ def testUTF8Japanese(self):

def testBrokenLineEndings(self):
fh = six.BytesIO(six.b("OFXHEADER:100\rDATA:OFXSGML\r"))
ofx_file = OfxPreprocessedFile(fh)
ofx_file = self.OfxFileCls(fh)
self.assertEqual(len(ofx_file.headers.keys()), 2)


class TestOfxFile(TestCase):
def testHeaders(self):
expect = {"OFXHEADER": six.u("100"),
"DATA": six.u("OFXSGML"),
"VERSION": six.u("102"),
"SECURITY": None,
"ENCODING": six.u("USASCII"),
"CHARSET": six.u("1252"),
"COMPRESSION": None,
"OLDFILEUID": None,
"NEWFILEUID": None,
}
with open_file('bank_medium.ofx') as f:
ofx = OfxParser.parse(f)
self.assertEqual(expect, ofx.headers)
class TestOfxPreprocessedFile(TestOfxFile):
OfxFileCls = OfxPreprocessedFile

def testTextFileHandler(self):
with open_file("bank_medium.ofx") as fh:
with open_file("bank_medium.ofx", mode="r") as fh_str:
ofx_file = OfxFile(fh)
headers = ofx_file.headers
data = ofx_file.fh.read()

self.assertTrue(type(data) is six.text_type)
for key, value in six.iteritems(headers):
self.assertTrue(type(key) is six.text_type)
self.assertTrue(type(value) is not six.binary_type)

ofx_file = OfxFile(fh_str)
headers = ofx_file.headers
data = ofx_file.fh.read()

self.assertTrue(type(data) is six.text_type)
for key, value in six.iteritems(headers):
self.assertTrue(type(key) is six.text_type)
self.assertTrue(type(value) is not six.binary_type)

def testUTF8(self):
def testPreprocess(self):
fh = six.BytesIO(six.b("""OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:UNICODE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE
<OFX><DTASOF><![CDATA[></tricky]]><LEAVE ALONE><VAL.UE>a<VAL_UE>b<TE_ST></TE_ST><TE.ST></TE.ST><INVBAL><BALLIST><BAL><NAME>Net<DTASOF>2222</BAL><BAL><NAME>Gross<DTASOF>3333</BAL></BALLIST></INVBAL></OFX>
"""))
ofx_file = OfxFile(fh)
headers = ofx_file.headers
data = ofx_file.fh.read()

self.assertTrue(type(data) is six.text_type)
for key, value in six.iteritems(headers):
self.assertTrue(type(key) is six.text_type)
self.assertTrue(type(value) is not six.binary_type)

def testCP1252(self):
fh = six.BytesIO(six.b("""OFXHEADER:100
expect = """OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET: 1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE
"""))
ofx_file = OfxFile(fh)
headers = ofx_file.headers
result = ofx_file.fh.read()

self.assertTrue(type(result) is six.text_type)
for key, value in six.iteritems(headers):
self.assertTrue(type(key) is six.text_type)
self.assertTrue(type(value) is not six.binary_type)

def testUTF8Japanese(self):
fh = six.BytesIO(six.b("""OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:UTF-8
CHARSET:CSUNICODE
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE
"""))
ofx_file = OfxFile(fh)
headers = ofx_file.headers
result = ofx_file.fh.read()

self.assertTrue(type(result) is six.text_type)
for key, value in six.iteritems(headers):
self.assertTrue(type(key) is six.text_type)
self.assertTrue(type(value) is not six.binary_type)
def testBrokenLineEndings(self):
fh = six.BytesIO(six.b("OFXHEADER:100\rDATA:OFXSGML\r"))
ofx_file = OfxFile(fh)
self.assertEqual(len(ofx_file.headers.keys()), 2)
<OFX><DTASOF><![CDATA[></tricky]]><LEAVE ALONE></DTASOF><VAL.UE>a</VAL.UE><VAL_UE>b</VAL_UE><TE_ST></TE_ST><TE.ST></TE.ST><INVBAL><BALLIST><BAL><NAME>Net</NAME><DTASOF>2222</DTASOF></BAL><BAL><NAME>Gross</NAME><DTASOF>3333</DTASOF></BAL></BALLIST></INVBAL></OFX>
"""
ofx_file = OfxPreprocessedFile(fh)
data = ofx_file.fh.read()
self.assertEqual(data, expect)


class TestParse(TestCase):
Expand Down

0 comments on commit 535e2b8

Please sign in to comment.