def setUp(self): TestSetUp.setUp(self) self.pos_file = test_pos_path + r'/2014-08-01-PositionStatement.csv' self.acc_file = test_acc_path + r'/2014-07-23-AccountStatement.csv' self.pos_data = open(self.pos_file, mode='r').read() self.acc_data = open(self.acc_file, mode='r').read() self.open_csv = OpenCSV(data=self.pos_data)
def test_fillna_dict_with_exists(self): """ Test fill empty or none value in dict """ self.acc_file = test_path + r'/2014-10-31/2014-10-31-AccountStatement.csv' self.acc_data = open(self.acc_file, mode='r').read() self.open_csv = OpenCSV(self.acc_data) self.open_csv.trade_history = list() trade_history_keys = [ '', 'execute_time', 'spread', 'side', 'quantity', 'pos_effect', 'symbol', 'expire_date', 'strike', 'contract', 'price', 'net_price', 'order_type' ] self.open_csv.set_values(start_phrase='Account Trade History', end_phrase=None, start_with=2, end_until=-1, prop_keys=trade_history_keys, prop_name='trade_history') before_fillna = deepcopy(self.open_csv.trade_history) for t in before_fillna: print t['execute_time'] self.open_csv.fillna_dict_with_exists( self.open_csv.trade_history, 'execute_time', ('execute_time', 'spread', 'order_type')) after_fillna = self.open_csv.trade_history after_fillna = map(self.open_csv.del_empty_keys, after_fillna) for key, (tk1, tk2) in enumerate(zip(before_fillna, after_fillna)): print tk1 print tk2 print '' if not tk1['execute_time']: self.assertEqual(tk1['execute_time'], '') self.assertEqual(tk1['spread'], '') self.assertEqual(tk1['order_type'], '') self.assertEqual(tk2['execute_time'], after_fillna[key - 1]['execute_time']) self.assertEqual(tk2['spread'], after_fillna[key - 1]['spread']) self.assertEqual(tk2['order_type'], after_fillna[key - 1]['order_type'])
def test_set_values(self): """ Test open files, get lines section, format data, make dict and finally set values in property """ self.open_csv = OpenCSV(self.acc_data) self.open_csv.trade_history = list() trade_history_keys = [ '', 'execute_time', 'spread', 'side', 'quantity', 'pos_effect', 'symbol', 'expire_date', 'strike', 'contract', 'price', 'net_price', 'order_type' ] start_phrase = 'Account Trade History' end_phrase = None start_add = 2 end_reduce = -1 prop_name = 'trade_history' print 'start phrase: %s, end phrase: %s' % (start_phrase, end_phrase) print 'start add: %d, end reduce: %d' % (start_add, end_reduce) print 'property name: %s' % prop_name self.open_csv.set_values(start_phrase=start_phrase, end_phrase=end_phrase, start_with=start_add, end_until=end_reduce, prop_keys=trade_history_keys, prop_name=prop_name) trade_history = self.open_csv.trade_history lines = self.open_csv.get_lines('Account Trade History') print '' for tk in trade_history: print tk self.assertEqual(type(tk), dict) self.assertEqual(len(tk), 13) self.assertEqual(len(lines), len(trade_history) + 2 + 1) self.assertEqual(type(trade_history), list)
def test_get_lines(self): """ Test get lines section from lines """ self.open_csv = OpenCSV(self.acc_data) phrases = [('Profits and Losses', 'OVERALL TOTALS'), ('Options', ',,,,,,'), ('Equities', 'OVERALL TOTALS'), ('Account Trade History', None), ('Account Order History', None), ('Forex Statements', None), ('Futures Statements', None), ('Cash Balance', 'TOTAL')] for start, end in phrases: lines = self.open_csv.get_lines(start, end) print start, end for line in lines: print line print '-' * 100
def test_fillna_dict(self): """ Test fill empty or none value in dict """ self.open_csv = OpenCSV(self.acc_data) self.open_csv.trade_history = list() trade_history_keys = [ '', 'execute_time', 'spread', 'side', 'quantity', 'pos_effect', 'symbol', 'expire_date', 'strike', 'contract', 'price', 'net_price', 'order_type' ] self.open_csv.set_values(start_phrase='Account Trade History', end_phrase=None, start_with=2, end_until=-1, prop_keys=trade_history_keys, prop_name='trade_history') before_fillna = self.open_csv.trade_history after_fillna = self.open_csv.fillna_dict(self.open_csv.trade_history) after_fillna = map(self.open_csv.del_empty_keys, after_fillna) for tk1, tk2 in zip(before_fillna, after_fillna): print tk1 print tk2 print '' self.assertIn('', tk1.values()) self.assertNotIn('', tk2.values()) for value1, value2 in zip(tk1.values(), tk2.values()): if value1 and (value1 != 'DEBIT' and value1 != 'CREDIT'): self.assertIn(value1, tk2.values()) for key in tk1.keys(): if key: self.assertIn(key, tk2.keys())