def test_no_pass_files_json(self, mock_requests): with self.assertRaises(Exception) as ctx: post_uri("1.2.3.4", json={"some": "data"}, files=self.file_mock) self.assertTrue( "Cannot pass json and data/files at the same time" in str(ctx.exception) )
def test_pass_files_data(self, mock_requests): post_uri("1.2.3.4", files=self.file_mock, data={"some": "data"}) mock_requests.assert_called_with( "http://1.2.3.4/", timeout=2, data={"some": "data"}, files=self.file_mock, json=None, )
def test_ip_port(self, mock_requests): post_uri("1.2.3.4:5000", "api/version") mock_requests.assert_called_with( "http://1.2.3.4:5000/api/version", timeout=2, data=None, files=None, json=None, )
def modify_current_job(self, action): if action not in ("cancel", "start", "toggle"): raise PrinterDriverException("Action %s is not allowed" % (action, )) request = None if self.client.connected: body = {"command": action} if action == "toggle": body = {"command": "pause", "action": "toggle"} request = post_uri(self.ip, endpoint="/api/job", json=body) if not request: self.client.connected = False # TODO improve return value return bool(request is not None and request.status_code == 204)
def test_no_success(self, mock_requests): def mock_call(uri, **kwargs): raise requests.exceptions.ConnectionError("mocked") mock_requests.side_effect = mock_call request = post_uri("1.2.3.4", "/api/version") self.assertEqual(request, None) self.assertEqual(mock_requests.call_count, 1) mock_requests.assert_has_calls( [ mock.call( "http://1.2.3.4/api/version", timeout=2, data=None, files=None, json=None, ) ] )
def upload_and_start_job(self, gcode_disk_path, path=None): request = None if self.client.connected: status = self.status() if status["state"] != "Operational": raise PrinterDriverException( "Printer is printing, cannot start another print") request = post_uri( self.ip, endpoint="/api/files/local", files={"file": open(gcode_disk_path, "rb")}, data={ "path": "karmen" if not path else "karmen/%s" % path, "print": True, }, ) if not request: self.client.connected = False # TODO improve return value return bool(request is not None and request.status_code == 201)
def test_pass_json(self, mock_requests): post_uri("1.2.3.4", json={"some": "data"}) mock_requests.assert_called_with( "http://1.2.3.4/", timeout=2, data=None, files=None, json={"some": "data"} )
def test_try_root(self, mock_requests): post_uri("1.2.3.4") mock_requests.assert_called_with( "http://1.2.3.4/", timeout=2, data=None, files=None, json=None )
def test_try_nothing(self, mock_requests): request = post_uri(None, "/api/version") self.assertEqual(mock_requests.call_count, 0) self.assertEqual(request, None)
def test_add_leading_slash(self, mock_requests): post_uri("1.2.3.4", "api/version") mock_requests.assert_called_with( "http://1.2.3.4/api/version", timeout=2, data=None, files=None, json=None )
def test_pass_protocol_timeout(self, mock_requests): post_uri("1.2.3.4", "/api/version", protocol="https", timeout=3) mock_requests.assert_called_with( "https://1.2.3.4/api/version", timeout=3, data=None, files=None, json=None )
def test_try_hostname(self, mock_requests): post_uri("1.2.3.4", "/api/version") mock_requests.assert_called_with( "http://1.2.3.4/api/version", timeout=2, data=None, files=None, json=None )