Пример #1
0
class OFXTreeTestCase(TestCase):
    def setUp(self):
        self.tree = OFXTree()

    def tearDown(self):
        del self.tree

    def test_parse(self):
        # OFXTree.parse() reads the source, strips the OFX header, feed()s
        # the OFX data to TreeBuilder, and stores the return value from
        # TreeBuilder.close() as its _root
        self.tree._read = MagicMock()
        self.tree._read.return_value = (sentinel.header, sentinel.ofx)

        mockTreeBuilderClass = MagicMock()
        mockTreeBuilderInstance = mockTreeBuilderClass.return_value
        mockTreeBuilderInstance.close.return_value = sentinel.root

        source = "/path/to/file.ofx"
        self.tree.parse(source, parser=mockTreeBuilderInstance)
        self.tree._read.assert_called_once_with(source)
        mockTreeBuilderInstance.feed.assert_called_once_with(sentinel.ofx)
        # FIXME - Fails on Python 3.5 ???
        #  mockTreeBuilderInstance.close.assert_called_once()
        self.assertEqual(self.tree._root, sentinel.root)

    def test_read_filename(self):
        with patch("builtins.open") as fake_open:
            with patch("ofxtools.Parser.parse_header") as fake_parse_header:
                fake_open.return_value = sentinel.file

                fake_header = sentinel.header
                fake_body = sentinel.ofx
                fake_parse_header.return_value = (fake_header, fake_body)

                source = NamedTemporaryFile()
                source.write(b"a bunch of text")
                source.seek(0)

                output = self.tree._read(source.name)
                source.close()
                fake_open.assert_called_once_with(source.name, "rb")
                fake_parse_header.assert_called_once_with(sentinel.file)
                self.assertEqual(output, (fake_header, fake_body))

    def test_read_file(self):
        with patch("ofxtools.Parser.parse_header") as fake_parse_header:
            fake_header = sentinel.header
            fake_body = sentinel.ofx
            fake_parse_header.return_value = (fake_header, fake_body)

            source = NamedTemporaryFile()
            source.write(b"a bunch of text")
            source.seek(0)

            output = self.tree._read(source)
            source.close()
            fake_parse_header.assert_called_once_with(source)
            self.assertEqual(output, (fake_header, fake_body))

    def test_read_not_bytes(self):
        source = NamedTemporaryFile(mode="w+")
        source.write("a bunch of text")
        source.seek(0)

        with self.assertRaises(ValueError):
            self.tree._read(source)

    def test_read_byteslike(self):
        # PR #15
        with patch("ofxtools.Parser.parse_header") as fake_parse_header:
            fake_header = sentinel.header
            fake_body = sentinel.ofx
            fake_parse_header.return_value = (fake_header, fake_body)

            source = BytesIO(b"a bunch of text")
            source.seek(0)

            output = self.tree._read(source)
            source.close()
            fake_parse_header.assert_called_once_with(source)
            self.assertEqual(output, (fake_header, fake_body))

    def test_read_illegal(self):
        source = "a bunch of text"
        with self.assertRaises(FileNotFoundError):
            self.tree._read(source)

    def test_convert(self):
        # Fake the result of OFXTree.parse()
        self.tree._root = Element("FAKE")

        # OFXTree.convert() returns an OFX instance constructed from its root
        with patch("ofxtools.Parser.Aggregate") as MockAggregate:
            ofx = self.tree.convert()
            MockAggregate.from_etree.assert_called_once_with(self.tree._root)
            self.assertEqual(ofx, MockAggregate.from_etree())

    def test_convert_unparsed(self):
        # Calling OFXTree.convert() without first calling OFXTree.parse()
        # raises ValueError
        with self.assertRaises(ValueError):
            self.tree.convert()
Пример #2
0
class OFXTreeTestCase(TestCase):
    """ """
    def setUp(self):
        self.tree = OFXTree()

    def tearDown(self):
        del self.tree

    def test_parse(self):
        # OFXTree.parse() reads the source, strips the OFX header, feed()s
        # the OFX data to TreeBuilder, and stores the return value from
        # TreeBuilder.close() as its _root
        self.tree._read = MagicMock()
        self.tree._read.return_value = ('header', 'OFX payload')

        mockTreeBuilderClass = MagicMock()
        mockTreeBuilderInstance = mockTreeBuilderClass.return_value
        mockTreeBuilderInstance.close.return_value = 'ElementTree.Element'

        source = '/path/to/file.ofx'
        self.tree.parse(source, parser=mockTreeBuilderClass)
        self.tree._read.assert_called_once_with(source)
        mockTreeBuilderInstance.feed.assert_called_once_with('OFX payload')
        mockTreeBuilderInstance.close.assert_called_once()
        self.assertEqual(self.tree._root, 'ElementTree.Element')

    def test_read_filename(self):
        OFXHeader.parse = MagicMock()
        fake_header = MagicMock()
        fake_header.codec = 'utf8'
        OFXHeader.parse.return_value = fake_header

        source = NamedTemporaryFile()
        source.write(b'a bunch of text')
        source.seek(0)

        output = self.tree._read(source.name)
        source.close()
        self.assertEqual(output, (fake_header, 'a bunch of text'))

    def test_read_file(self):
        OFXHeader.parse = MagicMock()
        fake_header = MagicMock()
        fake_header.codec = 'utf8'
        OFXHeader.parse.return_value = fake_header

        source = NamedTemporaryFile()
        source.write(b'a bunch of text')
        source.seek(0)

        output = self.tree._read(source)
        source.close()
        self.assertEqual(output, (fake_header, 'a bunch of text'))

    def test_read_file_binary(self):
        OFXHeader.parse = MagicMock()
        fake_header = MagicMock()
        fake_header.codec = 'utf8'
        OFXHeader.parse.return_value = fake_header

        source = BytesIO('a bunch of text'.encode())

        output = self.tree._read(source)
        source.close()
        self.assertEqual(output, (fake_header, 'a bunch of text'))

    def test_read_illegal(self):
        source = 'a bunch of text'
        with self.assertRaises(ParseError):
            self.tree._read(source)

    def test_convert(self):
        # Fake the result of OFXTree.parse()
        self.tree._root = Element('FAKE')

        # OFXTree.convert() returns an OFX instance constructed from its root
        with patch('ofxtools.Parser.Aggregate') as MockAggregate:
            ofx = self.tree.convert()
            MockAggregate.from_etree.assert_called_once_with(self.tree._root)
            self.assertEqual(ofx, MockAggregate.from_etree())

    def test_convert_unparsed(self):
        # Calling OFXTree.convert() without first calling OFXTree.parse()
        # raises ValueError
        with self.assertRaises(ValueError):
            self.tree.convert()
Пример #3
0
class OFXTreeTestCase(TestCase):
    def setUp(self):
        self.tree = OFXTree()

    def tearDown(self):
        del self.tree

    def test_parse(self):
        # OFXTree.parse() reads the source, strips the OFX header, feed()s
        # the OFX data to TreeBuilder, and stores the return value from
        # TreeBuilder.close() as its _root
        self.tree._read = MagicMock()
        self.tree._read.return_value = (sentinel.header, sentinel.ofx)

        mockTreeBuilderClass = MagicMock()
        mockTreeBuilderInstance = mockTreeBuilderClass.return_value
        mockTreeBuilderInstance.close.return_value = sentinel.root

        source = "/path/to/file.ofx"
        self.tree.parse(source, parser=mockTreeBuilderInstance)
        self.tree._read.assert_called_once_with(source)
        mockTreeBuilderInstance.feed.assert_called_once_with(sentinel.ofx)
        # FIXME - Fails on Python 3.5 ???
        #  mockTreeBuilderInstance.close.assert_called_once()
        self.assertEqual(self.tree._root, sentinel.root)

    def test_read_filename(self):
        with patch("builtins.open") as fake_open:
            with patch("ofxtools.Parser.parse_header") as fake_parse_header:
                fake_open.return_value = sentinel.file

                fake_header = sentinel.header
                fake_body = sentinel.ofx
                fake_parse_header.return_value = (fake_header, fake_body)

                source = NamedTemporaryFile()
                source.write(b"a bunch of text")
                source.seek(0)

                output = self.tree._read(source.name)
                source.close()
                fake_open.assert_called_once_with(source.name, "rb")
                fake_parse_header.assert_called_once_with(sentinel.file)
                self.assertEqual(output, (fake_header, fake_body))

    def test_read_file(self):
        with patch("ofxtools.Parser.parse_header") as fake_parse_header:
            fake_header = sentinel.header
            fake_body = sentinel.ofx
            fake_parse_header.return_value = (fake_header, fake_body)

            source = NamedTemporaryFile()
            source.write(b"a bunch of text")
            source.seek(0)

            output = self.tree._read(source)
            source.close()
            fake_parse_header.assert_called_once_with(source)
            self.assertEqual(output, (fake_header, fake_body))

    def test_read_not_bytes(self):
        source = NamedTemporaryFile(mode="w+")
        source.write("a bunch of text")
        source.seek(0)

        with self.assertRaises(ValueError):
            self.tree._read(source)

    def test_read_byteslike(self):
        # PR #15
        with patch("ofxtools.Parser.parse_header") as fake_parse_header:
            fake_header = sentinel.header
            fake_body = sentinel.ofx
            fake_parse_header.return_value = (fake_header, fake_body)

            source = BytesIO(b"a bunch of text")
            source.seek(0)

            output = self.tree._read(source)
            source.close()
            fake_parse_header.assert_called_once_with(source)
            self.assertEqual(output, (fake_header, fake_body))

    def test_read_illegal(self):
        source = "a bunch of text"
        with self.assertRaises(FileNotFoundError):
            self.tree._read(source)

    def test_convert(self):
        # Fake the result of OFXTree.parse()
        self.tree._root = Element("FAKE")

        # OFXTree.convert() returns an OFX instance constructed from its root
        with patch("ofxtools.Parser.Aggregate") as MockAggregate:
            ofx = self.tree.convert()
            MockAggregate.from_etree.assert_called_once_with(self.tree._root)
            self.assertEqual(ofx, MockAggregate.from_etree())

    def test_convert_unparsed(self):
        # Calling OFXTree.convert() without first calling OFXTree.parse()
        # raises ValueError
        with self.assertRaises(ValueError):
            self.tree.convert()
Пример #4
0
class OFXTreeTestCase(TestCase):
    """ """
    def setUp(self):
        self.tree = OFXTree()

    def tearDown(self):
        del self.tree

    def test_parse(self):
        # OFXTree.parse() reads the source, strips the OFX header, feed()s
        # the OFX data to TreeBuilder, and stores the return value from
        # TreeBuilder.close() as its _root
        self.tree._read = MagicMock()
        self.tree._read.return_value = 'source contents'

        self.tree._stripHeader = MagicMock()
        self.tree._stripHeader.return_value = 'OFX data'

        mockTreeBuilderClass = MagicMock()
        mockTreeBuilderInstance = mockTreeBuilderClass.return_value
        mockTreeBuilderInstance.close.return_value = 'ElementTree.Element'

        source = '/path/to/file.ofx'
        self.tree.parse(source, parser=mockTreeBuilderClass)
        self.tree._read.assert_called_once_with(source)
        self.tree._stripHeader.assert_called_once_with('source contents')
        mockTreeBuilderInstance.feed.assert_called_once_with('OFX data')
        self.assertEqual(self.tree._root, 'ElementTree.Element')

    def test_stripHeader(self):
        # FIXME - can't make unittest.mock.patch work for OFXHeader
        strip = OFXHeader.strip
        OFXHeader.strip = MagicMock()
        OFXHeader.strip.return_value = 'OFX data'
        stripped = self.tree._stripHeader('source contents')
        OFXHeader.strip.assert_called_once_with('source contents')
        self.assertEqual(stripped, 'OFX data')
        OFXHeader.strip = strip
        with self.assertRaises(AttributeError):
            OFXHeader.strip.return_value

    def test_read_filename(self):
        source = NamedTemporaryFile()
        source.write(b'a bunch of text')
        source.seek(0)
        output = self.tree._read(source.name)
        source.close()
        self.assertEqual(output, 'a bunch of text')

    def test_read_file(self):
        source = TemporaryFile()
        source.write(b'a bunch of text')
        source.seek(0)
        output = self.tree._read(source)
        source.close()
        self.assertEqual(output, 'a bunch of text')

    def test_read_file_binary(self):
        source = BytesIO('a bunch of text'.encode())
        output = self.tree._read(source)
        source.close()
        self.assertEqual(output, 'a bunch of text')

    def test_read_string(self):
        source = 'a bunch of text'
        output = self.tree._read(source)
        self.assertEqual(output, 'a bunch of text')

    def test_read_illegal(self):
        source = 23
        with self.assertRaises(ParseError):
            self.tree._read(source)

    def test_convert(self):
        # Fake the result of OFXTree.parse()
        self.tree._root = Element('FAKE')

        # OFXTree.convert() returns an OFX instance constructed from its root
        with patch('ofxtools.Parser.Aggregate') as MockAggregate:
            ofx = self.tree.convert()
            MockAggregate.from_etree.assert_called_once_with(self.tree._root)
            self.assertEqual(ofx, MockAggregate.from_etree())

    def test_convert_unparsed(self):
        # Calling OFXTree.convert() without first calling OFXTree.parse()
        # raises ValueError
        with self.assertRaises(ValueError):
            self.tree.convert()