def testAll(self): clearRepoData() saveRepoMasterFileToDB( join(getCurrentDir(), 'samples', 'RepoMaster_20210315_20210315193134.xml')) saveRepoTradeFileToDB( join(getCurrentDir(), 'samples', 'RepoTrade_20210315_20210315194216.xml')) saveRepoRerateFileToDB( join(getCurrentDir(), 'samples', 'Repo_ReRate_20210315_20210315211227.xml')) file1 = join(getCurrentDir(), 'samples', 'Repo_PosRecon_20210315_1.csv') file2 = join(getCurrentDir(), 'samples', 'Repo_PosRecon_20210315_2.csv') result = compose( list, partial(map, partial(updateAccruedInterest, getDateFromFilename(file1))), chain.from_iterable, partial(map, partial(enrichPosition, getRepoData())), loadRepoPosition)(file1, file2) self.verifyEnrichedPosition5(result) self.verifyEnrichedPosition6(result)
def getXmlHeaderFile(fileType): if fileType == 'loan_master': return join(getCurrentDir(), 'data', 'repo_master_header.txt') elif fileType == 'transaction': return join(getCurrentDir(), 'data', 'repo_trade_header.txt') elif fileType == 'rerate': return join(getCurrentDir(), 'data', 'repo_rerate_header.txt') else: logger.error('getXmlHeaderFile(): invalid file type {0}'.format(fileType)) raise ValueError
def testLoadRepoPosition(self): file1 = join(getCurrentDir(), 'samples', 'Repo_PosRecon_20210309_1.csv') file2 = join(getCurrentDir(), 'samples', 'Repo_PosRecon_20210309_2.csv') L = list(loadRepoPosition(file1, file2)) self.assertEqual(9, len(L)) self.assertEqual( set([ 'MMRPEA256T', 'MMRPE1257V', 'MMRPEB24DV', 'MMRPEA2560', 'MMRPE925N6', 'MMRPE925MV', 'MMRPE425RN', 'MMRPE12574', 'MMRPE322ZO' ]), set(map(lambda p: p['RepoName'], L)))
def testEnrichPosition(self): """ To complete this test, load information into database first, then enrich repo position with database info. """ clearRepoData() files = [ 'RepoMaster_20210309_20210309165806', 'RepoMaster_20210309_20210309190550', 'RepoMaster_20210309_20210309204006', 'RepoMaster_20210310_20210310225909' ] for file in files: saveRepoMasterFileToDB( join(getCurrentDir(), 'samples', file + '.xml')) files = [ 'RepoTrade_20210309_20210309174113', 'RepoTrade_20210309_20210309190913', 'RepoTrade_20210309_20210309204024', 'RepoTrade_20210310_20210310230717' ] for file in files: saveRepoTradeFileToDB( join(getCurrentDir(), 'samples', file + '.xml')) saveRepoRerateFileToDB( join(getCurrentDir(), 'samples', 'Repo_ReRate_20210309_20210309174543.xml')) file1 = join(getCurrentDir(), 'samples', 'Repo_PosRecon_20210309_1.csv') file2 = join(getCurrentDir(), 'samples', 'Repo_PosRecon_20210309_2.csv') L = compose( list, partial(map, partial(updateAccruedInterest, getDateFromFilename(file1))), chain.from_iterable, partial(map, partial(enrichPosition, getRepoData())), loadRepoPosition)(file1, file2) self.assertEqual(10, len(L)) self.verifyEnrichedPosition1(L) # closed position self.verifyEnrichedPosition2(L) # multi collateral, type OPEN self.verifyEnrichedPosition3(L) # fixed term self.verifyEnrichedPosition4(L) # not started yet
def testGetBloombergReconFiles(self): directory = join(getCurrentDir(), 'samples', 'data') file1, file2 = getBloombergReconFiles(directory) self.assertEqual('Repo_PosRecon_20210309_1.csv', getFilenameWithoutPath(file1)) self.assertEqual('Repo_PosRecon_20210309_2.csv', getFilenameWithoutPath(file2))
def testRepoTradeFromFile(self): inputFile = join(getCurrentDir(), 'samples', 'RepoTrade_20210209.xml') info = list(getRepoTradeFromFile(inputFile)) self.assertEqual(6, len(info)) self.verifyTrade1(info[0]) self.verifyTrade2(info[1]) self.verifyTrade3(info[2])
def testRepoTradeFromFile2(self): inputFile = join(getCurrentDir(), 'samples', 'RepoTrade_20210216.xml') info = list(getRepoTradeFromFile(inputFile)) self.assertEqual(1, len(info)) trade = info[0] self.assertEqual('Repo_InsertUpdate', trade['TransactionType']) self.assertEqual('309790', trade['UserTranId1']) self.assertEqual('2021-02-16T00:00:00', trade['ActualSettleDate']) self.assertEqual('', trade['OpenEnded']) self.assertEqual('CALC', trade['FundStructure']) self.assertEqual('CALC', trade['AccruedInterest'])
def upload(remoteDir, files): """ [String] remote directory, [List] files side effect: upload the files to Geneva SFTP server. Note: this function calls Windows Application WinScp to connect to the SFTP server. Before running this problem, use the WinScp to connect to the server at least once, to make sure the server key is acceptable to the caller. """ logger.debug('upload(): {0}'.format(','.join(files))) def createWinScpScript(files): """ [List] files => [String] WinScp script file Side effect: create a script to be used by WinScp program """ scriptFile = join(getCurrentDir(), 'winscp_scripts', 'run-sftp_{0}.txt'.format(getDatetimeAsString())) with open(scriptFile, 'w') as f: f.write('open sftp://{0}:{1}@{2}\n'.format(getSftpUser(), \ getSftpPassword(), getSftpServer())) f.write('cd {0}\n'.format(remoteDir)) for file in files: f.write('put {0}\n'.format(file)) f.write('exit') return scriptFile # end of createWinScpScript() if len(files) > 0: run([ getWinScpPath(), '/script={0}'.format(createWinScpScript(files)), '/log={0}'.format(join(getCurrentDir(), 'logs', 'winscp_log.log')) ], timeout=getSftpTimeout(), check=True)
def createWinScpScript(files): """ [List] files => [String] WinScp script file Side effect: create a script to be used by WinScp program """ scriptFile = join(getCurrentDir(), 'winscp_scripts', 'run-sftp_{0}.txt'.format(getDatetimeAsString())) with open(scriptFile, 'w') as f: f.write('open sftp://{0}:{1}@{2}\n'.format(getSftpUser(), \ getSftpPassword(), getSftpServer())) f.write('cd {0}\n'.format(remoteDir)) for file in files: f.write('put {0}\n'.format(file)) f.write('exit') return scriptFile
def testCreateDummyFile2(self): inputFile = join(getCurrentDir(), 'samples', 'RepoTrade_sample_mixed.xml') outputFile = createDummyRerateFile(inputFile) self.assertEqual('Dummy_ReRate_RepoTrade_sample_mixed.xml', outputFile) self.verifyFile(join(getCurrentDir(), 'samples', outputFile))
def testCreateDummyFile1(self): inputFile = join(getCurrentDir(), 'samples', 'RepoTrade_20210216.xml') outputFile = createDummyRerateFile(inputFile) self.assertEqual('', outputFile)
def testRepoMasterFromFile(self): inputFile = join(getCurrentDir(), 'samples', 'RepoMaster_20210210.xml') info = list(getRawDataFromXML(inputFile)) self.assertEqual(5, len(info)) self.verifyMaster(info[4])