from os.path import exists
|
|
import unittest
|
|
|
|
from .context import ptaimport
|
|
from ptaimport import data_importers as imp
|
|
|
|
class TestDataImporters( unittest.TestCase ):
|
|
"""
|
|
Tests the data_importers modules functions
|
|
"""
|
|
|
|
def setUp( self ):
|
|
# nothing provided
|
|
self.config_none = {
|
|
}
|
|
|
|
# name provided, but not import
|
|
self.config_noImport = {
|
|
"name": "test importer"
|
|
}
|
|
|
|
# import provided but no name
|
|
self.config_noName = {
|
|
"import": "mock"
|
|
}
|
|
|
|
# wrong import provided
|
|
self.config_wrong = {
|
|
"name": "test importer",
|
|
"import": "unknown"
|
|
}
|
|
|
|
# correct
|
|
self.config_mock = {
|
|
"name": "test importer",
|
|
"import": "mock"
|
|
}
|
|
|
|
|
|
def test_verify( self ):
|
|
"""
|
|
Tests the data_importers.verify() function
|
|
"""
|
|
(ok, err) = imp.verify( self.config_none )
|
|
self.assertFalse(ok)
|
|
self.assertNotEqual( len(err), 0 )
|
|
|
|
(ok, err) = imp.verify( self.config_noImport )
|
|
self.assertFalse(ok)
|
|
self.assertNotEqual( len(err), 0 )
|
|
|
|
(ok, err) = imp.verify( self.config_noName )
|
|
self.assertFalse(ok)
|
|
self.assertNotEqual( len(err), 0 )
|
|
|
|
(ok, err) = imp.verify( self.config_wrong )
|
|
self.assertFalse(ok)
|
|
self.assertNotEqual( len(err), 0 )
|
|
|
|
(ok, err) = imp.verify( self.config_mock )
|
|
self.assertTrue(ok)
|
|
self.assertEqual( len(err), 0 )
|
|
|
|
|
|
def test_factory( self ):
|
|
"""
|
|
Tests the data_importers.Importer() function
|
|
"""
|
|
self.assertRaises( ValueError, imp.Importer, self.config_none )
|
|
self.assertRaises( ValueError, imp.Importer, self.config_noImport )
|
|
self.assertRaises( ValueError, imp.Importer, self.config_noName )
|
|
self.assertRaises( ValueError, imp.Importer, self.config_wrong )
|
|
imp.Importer( self.config_mock ) # should not raise
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
from ptaimport.data_importers.modo import ModoDataImporter
|
|
|
|
class TestModoDataImporter( unittest.TestCase ):
|
|
|
|
def setUp( self ):
|
|
pass
|
|
|
|
# Note: to activate this test, save a modo invoice (HTML) from your account
|
|
# to the "tests/data/" directory (as "tests/data/modo_invoice.html"), and
|
|
# also create a "modo_invoice_expected.txt" file containing the expected
|
|
# transactions to test against
|
|
@unittest.skipUnless( exists("tests/data/modo_invoice.html")
|
|
and exists("tests/data/modo_invoice_expected.txt"),
|
|
"Missing test data" )
|
|
def test_html_parse( self ):
|
|
modo = ModoDataImporter({
|
|
"name": "test",
|
|
"user_id": 42,
|
|
"password_id": "none"
|
|
})
|
|
|
|
expected_transactions = []
|
|
with open("tests/data/modo_invoice_expected.txt") as f:
|
|
expected_transactions = f.readlines()
|
|
|
|
actual_transactions = []
|
|
with open("tests/data/modo_invoice.html") as f:
|
|
actual_transactions = modo._parse_invoice( f.read() )
|
|
|
|
self.assertEqual( len(expected_transactions), len(actual_transactions) )
|
|
for i in range(0, len(expected_transactions)):
|
|
self.assertEqual(
|
|
expected_transactions[i].strip(),
|
|
actual_transactions[i].strip()
|
|
)
|