From b1fc72ccdb7a648f52492bf44581837fbb95b588 Mon Sep 17 00:00:00 2001 From: Wes Date: Wed, 24 Jun 2020 22:58:34 -0400 Subject: [PATCH] Adding unit test example --- tests.py | 158 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 tests.py diff --git a/tests.py b/tests.py new file mode 100644 index 0000000..89956bf --- /dev/null +++ b/tests.py @@ -0,0 +1,158 @@ +''' +Test everything! + +functions must begin with 'test' +''' +import unittest, logging, kf_850, mf_850 +from download_transmissions import load_ftp_credentials +from kf_850 import massageSTM, massageKFI +from mf_850 import getSunburyCust, split_string_by_position, trim_elements + +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker + +engine = create_engine("mssql+pyodbc://jsisbprod", echo=False) +Base = declarative_base(engine) +metadata = Base.metadata + +def loadSession(): + global engine + Session = sessionmaker(bind=engine) + session = Session() + return session + +class Download_Transmissions_Tests(unittest.TestCase): + ''' + Tests relating to download_tranmissions.py + ''' + + ''' + load_ftp_credentials looks up FTP site credentials in a sqlite db that's not committed to the code repo + ''' + def test_load_ftp_credentials(self): + codes = ['wf', 'kf', 'mf', 'mg', 'tsg', 'atx', 'htx', 'acf', 'stm'] + for code in codes: + test = load_ftp_credentials(code) + self.assertEqual(len(test), 3) # Did we get a list back with _exactly_ 3 elements? + self.assertTrue(all(isinstance(n, basestring) for n in test)) # Are each of those elements strings? + +class Kravet_Inbound_850_Tests(unittest.TestCase): + ''' + Tests relating to kf_850.py + ''' + + ''' + Kravet seems to hand key cross references in with a number of varying formats which are incorrect. + I've identified the most common and created massageSTM() to fix them + ''' + def test_massageSTM(self): + #create dummy logger + logger = logging.getLogger('Unit_Testing') + logger.disabled = True + kf_850.logger = logger + + test_input = ['KIRSTEN 73383*2015', 'MULBERRY 73151*COL.825'] + expected_output = ['KIRSTEN*2015', 'MULBERRY*0825'] + for inn, outt in zip(test_input, expected_output): + self.assertEqual(outt, massageSTM(inn)) + + ''' + Right now this simply trims off the first 4 characters of the xref if 'GWF-' is present at the beginning of the string. + ''' + def test_massageKFI(self): + #create dummy logger + logger = logging.getLogger('Unit_Testing') + logger.disabled = True + kf_850.logger = logger + + test_input = ['GWF-3421.516.0'] + expected_output = ['3421.516.0'] + for inn, outt in zip(test_input, expected_output): + self.assertEqual(outt, massageKFI(inn)) + + def test_lookUpSunburyItem(self): + #create dummy logger + logger = logging.getLogger('Unit_Testing') + logger.disabled = True + kf_850.logger = logger + session = loadSession() + + test_input = [ ['BLISS VELVET*2405','3194'], ['DUME*0601','3191'], ['PAISLEY WALK*6103','3190'] ] + expected_output = ['0000041463', '0000006576', '0000032327'] + + for inn, outt in zip(test_input, expected_output): + self.assertEqual(kf_850.lookUpSunburyItem(session, inn[0], inn[1]), outt) + + session.close() + +class Maharam_Inbound_850_Tests(unittest.TestCase): + ''' + Tests relating to mf_850.py + ''' + + addressList = ['45 RASONS CT', '74 HORSEBLOCK ROAD', '175 MUFFIN LANE'] + cityList = ['HAUPPAUGE', 'YAPHANK', 'CUYAHOGA FALLS'] + accountList = [343900, 3439, 343905] + + def test_normal_order(self): + for addr, city, acc in zip(self.addressList, self.cityList, self.accountList): + message = "%s, %s should be account %s" % (addr, city, acc) + self.assertEqual(getSunburyCust('', addr, city), acc, msg=message) + + ''' + If order is custom contract hccb will be something other than an empty string + We should always get account 659000 for contract orders + ''' + def test_contract_order(self): + for addr, city in zip(self.addressList, self.cityList): + self.assertEqual(getSunburyCust('Y', addr, city), 659000, msg='hccb = y but function did not return 659000') + self.assertNotEqual(getSunburyCust('', addr, city), 659000, msg='hccb = \'\' but function returned 659000') + + ''' + Unkown addresses (those not in the lists above) should resolve to 343999 + ''' + def test_unknown_address(self): + self.assertEqual(getSunburyCust('', 'Castle Black', 'The Wall'), 343999, msg="Castle Black, The Wall should be account 343999") + + ''' + + ''' + def test_lookUpSunburyItem(self): + #create dummy logger + logger = logging.getLogger('Unit_Testing') + logger.disabled = True + mf_850.logger = logger + session = loadSession() + + test_input = [ ['464040','004','343904'], ['466166','007','343904'], ['466166','002','3439'] ] + expected_output = ['0000025195', '0000041949', '0000041944'] + + for inn, outt in zip(test_input, expected_output): + self.assertEqual(mf_850.lookUpSunburyItem(session, inn[0], inn[1], inn[2]), outt) + + session.close() + + ''' + trim_elements takes a list as input and returns a list of the same length with any whitespace characters + removed from the beginning/end of each element. + ''' + def test_trim_elements(self): + test_input = [' Pennywise The Dancing Clown ', 'Jon Snow ', 'Paul Atreides\n\r', ' \tBobby B.\t'] + expected_output = ['Pennywise The Dancing Clown', 'Jon Snow', 'Paul Atreides', 'Bobby B.'] + self.assertEqual(trim_elements(test_input), expected_output) + + ''' + Maharam is still transmitting fixed length data to us *yuck* + The function takes 1 string and a list of ints + the string is chopped up into len(ints) elements each element being ints[n] in length + ''' + def test_split_string_by_position(self): + test_string = 'Pennywise The Dancing ClownJon SnowPaul AtreidesBobby B.' + lens = [27, 8, 13, 8] + expected_output = ['Pennywise The Dancing Clown', 'Jon Snow', 'Paul Atreides', 'Bobby B.'] + self.assertEqual(split_string_by_position(test_string, lens), expected_output) + + +if __name__ == '__main__': + unittest.main()