Prj701 7 ~ Sales Order XML Parse

September 19, 2018
PRJ701 Python XML FTP Odoo Sales Open source

Project 702 ~ Sales Order XML Parse

One of the stages in the project is to create a module which provides the functionality of automatically generating a sales order based on the values from an XML file retrived from an FTP server. Before I create a module I am going to create a test-driven example using the standard library ftplib which provides an FTP client, containing the functions (nlist and retrbinary) that are necessary. I am going to iterate over the files in the FTP server using nlst, and then download the file with retrbinary.

class FTPI(object):
    def __init__(self, ftpconn=None):
        if not os.path.isdir('ftp-files'):
            os.mkdir('ftp-files')
        self.filespath = 'ftp-files'
        self.ftpconn = ftpconn

    # Downloads (all) files from the ftp server and saves them to 'ftp-files'
    def download_files(self):
        for i, f in enumerate(self.ftpconn.nlst()):
            fhandler = open('%s/%s' % (self.filespath, f), 'wb')
            print('Fetching %s: %s' % (i, f))
            self.ftpconn.retrbinary('RETR %s' % (f), fhandler.write)
            fhandler.close()

Now to write the unittest ~ first one that fails

import unittest
import ftpi
import ftplib

class ftpi_testcase(unittest.TestCase):
 
    def test_ftpi_download_files_successful(self):
        server = ftplib.FTP('REDACTED')
        server.login('MISPELT USERNAME', 'REDACTED')
        ftpconn = ftpi.FTPI(server)
        self.assertNotEqual(ftpconn.download_files(), Exception)

if __name__ == '__main__':
    unittest.main()
======================================================================
ERROR: test_ftpi_download_files_successful (__main__.ftpi_testcase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 11, in test_ftpi_download_files_successful
    server.login('MISPELT USERNAME', 'REDACTED')
  File "/usr/lib/python3.7/ftplib.py", line 420, in login
    resp = self.sendcmd('PASS ' + passwd)
  File "/usr/lib/python3.7/ftplib.py", line 273, in sendcmd
    return self.getresp()
  File "/usr/lib/python3.7/ftplib.py", line 246, in getresp
    raise error_perm(resp)
ftplib.error_perm: 530 Invalid userid/password

----------------------------------------------------------------------
Ran 1 test in 0.157s

FAILED (errors=1)

One that passes ~

import unittest
import ftpi
import ftplib

class ftpi_testcase(unittest.TestCase):
 
    def test_ftpi_download_files_successful(self):
        server = ftplib.FTP('REDACTED')
        server.login('CORRECT USERNAME', 'REDACTED')
        ftpconn = ftpi.FTPI(server)
        self.assertNotEqual(ftpconn.download_files(), Exception)

if __name__ == '__main__':
    unittest.main()
----------------------------------------------------------------------
Ran 1 test in 11.231s

OK

Not entirely sure if that is how assertNotEqual is supposed to be used but seems to work.

For the refactor part I am going merge the functions with an Odoo module.

Web701 22

June 15, 2019
Web701 Serverless OpenFaas Docker CLI Python Digital Ocean

Web701 21

May 21, 2019
Web701 VirtualBox Serverless OpenFaas Docker CLI Python

Web701 20

May 20, 2019
Web701 Python Django Heroku Web Hosting
comments powered by Disqus