Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import unittest 

2try: 

3 import netifaces 

4except ImportError: 

5 netifaces = None 

6from unittest import mock 

7from pi3bar.plugins.net import NetIFace 

8 

9 

10class NetIfaceTestCase(unittest.TestCase): 

11 def test_default_values(self): 

12 plugin = NetIFace('eth0') 

13 

14 self.assertEqual('eth0', plugin.instance) 

15 self.assertEqual('eth0', plugin.interface) 

16 self.assertEqual('eth0', plugin.short_text) 

17 self.assertEqual(5, plugin.ticks) 

18 self.assertIsNone(plugin.color) 

19 self.assertIsNone(plugin.background) 

20 

21 def test_interface_required(self): 

22 with self.assertRaises(TypeError): 

23 NetIFace() 

24 

25 @mock.patch.object(NetIFace, 'error') 

26 def test_not_installed(self, mock_error): 

27 plugin = NetIFace('eth0') 

28 

29 from pi3bar.plugins import net 

30 net.netifaces = None 

31 

32 plugin.cycle() 

33 

34 self.assertEqual('not installed', plugin.full_text) 

35 self.assertIsNone(plugin.color) 

36 self.assertEqual('#ff0000', plugin.background) 

37 mock_error.assert_called_once_with('netifaces is not installed') 

38 

39 

40@mock.patch('pi3bar.plugins.net.netifaces') 

41class NetIFaceCycleTestCase(unittest.TestCase): 

42 @unittest.skipIf(netifaces is None, 'netifaces is not installed.') 

43 def test(self, mock_netifaces): 

44 plugin = NetIFace('eth0') 

45 

46 mock_netifaces.AF_INET = 'AF_INET' 

47 mock_netifaces.interfaces.return_value = ['eth0', 'wlan0'] 

48 mock_netifaces.ifaddresses.return_value = {'AF_INET': [{'addr': '192.168.0.42'}]} 

49 plugin.cycle() 

50 

51 self.assertEqual('eth0 192.168.0.42', plugin.full_text) 

52 

53 self.assertEqual(plugin.connected_color, plugin.color) 

54 self.assertEqual(plugin.connected_background, plugin.background) 

55 self.assertEqual('#00ff00', plugin.color) 

56 self.assertIsNone(plugin.background) 

57 

58 @unittest.skipIf(netifaces is None, 'netifaces is not installed.') 

59 @mock.patch.object(NetIFace, 'error') 

60 def test_unkown_interface(self, mock_error, mock_netifaces): 

61 plugin = NetIFace('eth1') 

62 

63 mock_netifaces.interfaces.return_value = ['eth0', 'wlan0'] 

64 plugin.cycle() 

65 

66 self.assertEqual('eth1 not found', plugin.full_text) 

67 self.assertIsNone(plugin.color) 

68 self.assertEqual('#ff0000', plugin.background) 

69 mock_error.assert_called_once_with('\'eth1\' not found in [\'eth0\', \'wlan0\']') 

70 

71 @unittest.skipIf(netifaces is None, 'netifaces is not installed.') 

72 def test_no_AF_INET(self, mock_netifaces): 

73 plugin = NetIFace('eth0') 

74 

75 mock_netifaces.interfaces.return_value = ['eth0', 'wlan0'] 

76 mock_netifaces.ifaddresses.return_value = {} 

77 plugin.cycle() 

78 self.assertEqual(plugin.disconnected_color, plugin.color) 

79 self.assertEqual(plugin.disconnected_background, plugin.background) 

80 self.assertIsNone(plugin.color) 

81 self.assertIsNone(plugin.background) 

82 self.assertEqual('eth0', plugin.full_text) 

83