test_middleware.py (1522B)
1 import json 2 import logging 3 4 import unittest 5 6 from gas_proxy.web3 import GasMiddleware 7 8 logging.basicConfig(level=logging.DEBUG) 9 10 11 class Mocket: 12 13 14 def __init__(self): 15 self.last_id = None 16 self.response = None 17 self.active = True 18 19 20 def toggle(self): 21 self.active = not self.active 22 23 24 def send(self, d): 25 if not self.active: 26 raise ConnectionError('socket deactivated') 27 o = json.loads(d) 28 self.last_id = o['id'] 29 r = { 30 "jsonrpc": "2.0", 31 "id": self.last_id, 32 "result": "0x1324", 33 } 34 self.response = json.dumps(r) 35 36 def recv(self, c=0): 37 return self.response 38 39 40 class MiddlewareTest(unittest.TestCase): 41 42 43 def setUp(self): 44 self.socket = Mocket() 45 46 def socket_constructor(): 47 return self.socket 48 49 GasMiddleware.socket_constructor = socket_constructor 50 51 52 def tearDown(self): 53 pass 54 55 56 def test_middleware(self): 57 GasMiddleware.last_gas = "0x2a" 58 middleware = GasMiddleware(None, None) 59 60 self.socket.toggle() 61 r = middleware.__call__("eth_gasPrice", []) 62 self.assertEqual(r['result'], "0x2a") 63 64 self.socket.toggle() 65 r = middleware.__call__("eth_gasPrice", []) 66 self.assertEqual(r['result'], "0x1324") 67 68 self.socket.toggle() 69 r = middleware.__call__("eth_gasPrice", []) 70 self.assertEqual(r['result'], "0x1324") 71 72 if __name__ == '__main__': 73 unittest.main()