#NEWLINE# @ patch ( 'portstat.psutil.net_connections' ) #NEWLINE# def test_get_port_stats( self , net_connections_mock ) : #NEWLINE# #INDENT# ports = [ Mock ( ) for _ in range ( 5 ) ] #NEWLINE# ports [ 0 ] . laddr = ( None , 5222 ) #NEWLINE# ports [ 0 ] . status = 'ok' #NEWLINE# ports [ 1 ] . laddr = ports [ 2 ] . laddr = ports [ 3 ] . laddr = ( None , 8888 ) #NEWLINE# ports [ 1 ] . status = 'ok' #NEWLINE# ports [ 2 ] . status = 'OK' #NEWLINE# ports [ 3 ] . status = 'bad' #NEWLINE# ports [ 4 ] . laddr = ( None , 9999 ) #NEWLINE# net_connections_mock . return_value = ports #NEWLINE# cnts = get_port_stats ( 5222 ) #NEWLINE# self . assertEqual ( net_connections_mock . call_count , 1 ) #NEWLINE# self . assertEqual ( cnts , { #NEWLINE# #INDENT# 'ok' : 1 , #NEWLINE# #UNINDENT# } ) #NEWLINE# cnts = get_port_stats ( 8888 ) #NEWLINE# self . assertEqual ( net_connections_mock . call_count , 2 ) #NEWLINE# self . assertEqual ( net_connections_mock , { #NEWLINE# #INDENT# 'ok' : 2 , #NEWLINE# 'bad' : 1 , #NEWLINE# #UNINDENT# } )