daily pastebin goal
52%
SHARE
TWEET

Untitled

a guest Feb 16th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. """
  4. filename: descriptor_2_1.py
  5. description: python descriptor
  6. License: GPL V2
  7. Author: 天使de眼睛
  8.  
  9. 除特别声明,所有代码均是 python3.6 在 iOS 环境下编写测试。
  10.  
  11. 运行代码需要 pytest
  12.     pip install pytest
  13. """
  14. from ipaddress import ip_address
  15.  
  16. import pytest
  17.  
  18.  
  19. class Server:
  20.     def __init__(self, name, ip, rack):
  21.         self._name = None
  22.         self._ip = None
  23.         self.name = name
  24.         self.ip = ip
  25.        
  26.         self._rack = None
  27.         self.rack = rack
  28.        
  29.  
  30.     @property
  31.     def name(self):
  32.         return self._name
  33.  
  34.     @name.setter
  35.     def name(self, value):
  36.         if not isinstance(value, str):
  37.             raise TypeError(f'Expected {str}')
  38.         self._name = value.upper()
  39.  
  40.     @property
  41.     def ip(self):
  42.         return self._ip
  43.  
  44.     @ip.setter
  45.     def ip(self, value):
  46.         if not isinstance(value, str):
  47.             raise TypeError(f'Expected {str}')
  48.         self._ip = ip_address(value)
  49.  
  50.     @property
  51.     def rack(self):
  52.         return self._rack
  53.        
  54.     @rack.setter
  55.     def rack(self, value):
  56.         if not isinstance(value, int):
  57.             raise TypeError(f'Expected {int}')
  58.         self._rack = value
  59.  
  60. class Protocol:
  61.     def __init__(self, protocol, source, destination, port):
  62.         self._protocol = None
  63.         self._source = None
  64.         self._destination = None
  65.         self._port = None
  66.         self.protocol = protocol
  67.         self.source = source
  68.         self.destination = destination
  69.         self.port = port
  70.  
  71.     @property
  72.     def protocol(self):
  73.         return self._protocol
  74.  
  75.     @protocol.setter
  76.     def protocol(self, value):
  77.         if not isinstance(value, str):
  78.             raise TypeError(f'Expected {str}')
  79.         self._protocol = value.upper()
  80.  
  81.     @property
  82.     def source(self):
  83.         return self._source
  84.  
  85.     @source.setter
  86.     def source(self, value):
  87.         if not isinstance(value, str):
  88.             raise TypeError(f'Expected {str}')
  89.         self._source = ip_address(value)
  90.  
  91.     @property
  92.     def destination(self):
  93.         return self._destination
  94.  
  95.     @destination.setter
  96.     def destination(self, value):
  97.         if not isinstance(value, str):
  98.             raise TypeError(f'Expected {str}')
  99.         self._destination = ip_address(value)
  100.  
  101.     @property
  102.     def port(self):
  103.         return self._port
  104.        
  105.     @port.setter
  106.     def port(self, value):
  107.         if not isinstance(value, int):
  108.             raise TypeError(f'Expected {int}')
  109.         if value < 1:
  110.             raise ValueError('port must >= 1')
  111.         if value > 65535:
  112.             raise ValueError('port must <= 65535')
  113.         self._port = value
  114.  
  115. class TestClasses:
  116.     def test_Server(self):
  117.         server = Server('myserver1', '192.168.1.1', 5)
  118.         assert server.name == 'MYSERVER1'
  119.         assert server.ip == ip_address('192.168.1.1')
  120.         with pytest.raises(TypeError) as error:
  121.             server.name = 1234
  122.         assert str(error.value) == "Expected <class 'str'>"
  123.         with pytest.raises(TypeError) as error:
  124.             server.ip = 1234
  125.         assert str(error.value) == "Expected <class 'str'>"
  126.         assert server.rack == 5
  127.         with pytest.raises(TypeError) as error:
  128.             server.rack = 'up'
  129.         assert str(error.value) == "Expected <class 'int'>"
  130.        
  131.  
  132.     def test_Protocol(self):
  133.         logger = Protocol('tcp', '192.168.1.1', '10.1.1.1', 8080)
  134.         assert logger.protocol == 'TCP'
  135.         assert logger.source == ip_address('192.168.1.1')
  136.         assert logger.destination == ip_address('10.1.1.1')
  137.         with pytest.raises(TypeError) as error:
  138.             logger.protocol = ['tcp']
  139.         assert str(error.value) == "Expected <class 'str'>"
  140.         with pytest.raises(TypeError) as error:
  141.             logger.source = ['tcp']
  142.         assert str(error.value) == "Expected <class 'str'>"
  143.         assert logger.port == 8080
  144.         with pytest.raises(ValueError) as error:
  145.             logger.port = 0
  146.         assert str(error.value) == "port must >= 1"
  147.         with pytest.raises(ValueError) as error:
  148.             logger.port = 65536
  149.         assert str(error.value) == "port must <= 65535"
  150.  
  151.  
  152. if __name__ == '__main__':
  153.     import sys
  154.  
  155.     pytest.main(sys.argv + ['-v'])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top