Sto provando a testare una funzione che crea un grafico di scambio di messaggi dai registri delle chat IRC. Sto avendo problemi a cercare di deridere le dipendenze. La funzione utilizza il modulo util. Le funzioni all'interno di utils ricevono argomenti e restituiscono un output dopo un po 'di elaborazione. Il problema è che queste funzioni di utilità vengono chiamate più volte all'interno di for-loops e if-conditionals. Come posso specificare più resi in base agli argomenti di input? Mi piacerebbe sapere come testare le funzioni in modo simile a questi.
Ho usato le librerie standard di unittest e mock di Python.
Fonte del codice: IRCLogParser
def message_number_graph(log_dict, nicks, nick_same_list,DAY_BY_DAY_ANALYSIS=False):
"""
Creates a directed graph
with each node representing an IRC user
and each directed edge has a weight which
mentions the number messages sent and recieved by that user
in the selected time frame.
Args:
log_dict (dict): with key as dateTime.date object and value as {"data":datalist,"channel_name":channels name}
nicks(list): list of all the nicks
nick_same_list(list): list of lists mentioning nicks which belong to same users
Returns:
message_number_graph (nx graph object)
"""
message_number_day_list = []
conversations=[[0] for i in range(config.MAX_EXPECTED_DIFF_NICKS)]
aggregate_message_number_graph = nx.DiGraph() #graph with multiple directed edges between clients used
G = util.to_graph(nick_same_list)
conn_comp_list = list(connected_components(G))
util.create_connected_nick_list(conn_comp_list)
def msg_no_analysis_helper(rec_list, corrected_nick, nick, conn_comp_list,conversations,today_conversation):
for receiver in rec_list:
if(receiver == nick):
if(corrected_nick != nick):
nick_receiver = ''
nick_receiver = util.get_nick_sen_rec(config.MAX_EXPECTED_DIFF_NICKS, nick, conn_comp_list, nick_receiver)
if DAY_BY_DAY_ANALYSIS:
today_conversation = util.extend_conversation_list(nick_sender, nick_receiver, today_conversation)
else:
conversations = util.extend_conversation_list(nick_sender, nick_receiver, conversations)
def message_no_add_egde(message_graph, conversation):
for index in xrange(config.MAX_EXPECTED_DIFF_NICKS):
if(len(conversation[index]) == 3 and conversation[index][0] >= config.THRESHOLD_MESSAGE_NUMBER_GRAPH):
if len(conversation[index][1]) >= config.MINIMUM_NICK_LENGTH and len(conversation[index][2]) >= config.MINIMUM_NICK_LENGTH:
message_graph.add_edge(util.get_nick_representative(nicks, nick_same_list, conversation[index][1]), util.get_nick_representative(nicks, nick_same_list, conversation[index][2]), weight=conversation[index][0])
return message_graph
for day_content_all_channels in log_dict.values():
for day_content in day_content_all_channels:
day_log = day_content["log_data"]
today_conversation = [[0] for i in range(config.MAX_EXPECTED_DIFF_NICKS)]
for line in day_log:
flag_comma = 0
if(util.check_if_msg_line (line)):
parsed_nick = re.search(r"\<(.*?)\>", line)
corrected_nick = util.correctLastCharCR(parsed_nick.group(0)[1:-1])
nick_sender = ""
nick_receiver = ""
nick_sender = util.get_nick_sen_rec(config.MAX_EXPECTED_DIFF_NICKS, corrected_nick, conn_comp_list, nick_sender)
for nick in nicks:
rec_list = [e.strip() for e in line.split(':')]
util.rec_list_splice(rec_list)
if not rec_list[1]:
break
rec_list = util.correct_last_char_list(rec_list)
msg_no_analysis_helper(rec_list, corrected_nick, nick, conn_comp_list, conversations,today_conversation)
if "," in rec_list[1]:
flag_comma = 1
rec_list_2=[e.strip() for e in rec_list[1].split(',')]
for i in xrange(0,len(rec_list_2)):
if(rec_list_2[i]):
rec_list_2[i] = util.correctLastCharCR(rec_list_2[i])
msg_no_analysis_helper(rec_list_2, corrected_nick, nick, conn_comp_list, conversations, today_conversation)
if(flag_comma == 0):
rec = line[line.find(">")+1:line.find(", ")]
rec = rec[1:]
rec = util.correctLastCharCR(rec)
if(rec == nick):
if(corrected_nick != nick):
nick_receiver = nick_receiver_from_conn_comp(nick, conn_comp_list)
if DAY_BY_DAY_ANALYSIS:
today_message_number_graph = nx.DiGraph()
today_message_number_graph = message_no_add_egde(today_message_number_graph, today_conversation)
year, month, day = util.get_year_month_day(day_content)
message_number_day_list.append([today_message_number_graph, year+'-'+month+'-'+day])
print "\nBuilding graph object with EDGE WEIGHT THRESHOLD:", config.THRESHOLD_MESSAGE_NUMBER_GRAPH
if not DAY_BY_DAY_ANALYSIS:
aggregate_message_number_graph = message_no_add_egde(aggregate_message_number_graph, conversations)
if config.DEBUGGER:
print "========> 30 on " + str(len(conversations)) + " conversations"
print conversations[:30]
if DAY_BY_DAY_ANALYSIS:
return message_number_day_list
else:
return aggregate_message_number_graph