def test_auto_start(self): try: import requests # import urllib2 except ImportError: raise # import urllib.request as urllib2 with patch("uiautomator.JsonRPCMethod") as JsonRPCMethod: # returns = [urllib2.URLError("error"), "ok"] returns = [requests.exceptions.ConnectionError("error"), "ok"] def side_effect(): result = returns.pop(0) if isinstance(result, Exception): raise result return result JsonRPCMethod.return_value.side_effect = side_effect server = AutomatorServer() server.start = MagicMock() server.stop = MagicMock() self.assertEqual("ok", server.jsonrpc.any_method()) server.start.assert_called_once_with(timeout=30) with patch("uiautomator.JsonRPCMethod") as JsonRPCMethod: returns = [JsonRPCError(-32000 - 1, "error msg"), "ok"] def side_effect(): result = returns.pop(0) if isinstance(result, Exception): raise result return result JsonRPCMethod.return_value.side_effect = side_effect server = AutomatorServer() server.start = MagicMock() server.stop = MagicMock() self.assertEqual("ok", server.jsonrpc.any_method()) server.start.assert_called_once_with() with patch("uiautomator.JsonRPCMethod") as JsonRPCMethod: JsonRPCMethod.return_value.side_effect = JsonRPCError( -32000 - 2, "error msg") server = AutomatorServer() server.start = MagicMock() server.stop = MagicMock() with self.assertRaises(JsonRPCError): server.jsonrpc.any_method()
def testJsonRPCError(self): e = JsonRPCError(200, "error") self.assertEqual(200, e.code) self.assertTrue(len(str(e)) > 0) e = JsonRPCError("200", "error") self.assertEqual(200, e.code)