11// Copyright (c) 2025 Calvin Min
22// SPDX-License-Identifier: MIT
3+ #include " ../../mocks/mock_http_client.h"
4+
35#include < databricks/core/config.h>
46#include < databricks/jobs/jobs.h>
57#include < gtest/gtest.h>
68
9+ using databricks::test::MockHttpClient;
10+ using ::testing::_;
11+ using ::testing::Return;
12+
713// Test fixture for Jobs tests
814class JobsTest : public ::testing::Test {
915protected:
@@ -16,6 +22,18 @@ class JobsTest : public ::testing::Test {
1622 }
1723};
1824
25+ // Test fixture for Jobs API tests with mocks
26+ class JobsApiTest : public ::testing::Test {
27+ protected:
28+ databricks::AuthConfig auth;
29+
30+ void SetUp () override {
31+ auth.host = " https://test.databricks.com" ;
32+ auth.set_token (" test_token" );
33+ auth.timeout_seconds = 30 ;
34+ }
35+ };
36+
1937// Test: Jobs client construction
2038TEST_F (JobsTest, ConstructorCreatesValidClient) {
2139 ASSERT_NO_THROW ({ databricks::Jobs jobs (auth); });
@@ -227,3 +245,48 @@ TEST_F(JobsTest, MultipleClientsCanCoexist) {
227245 // Both should coexist without issues
228246 });
229247}
248+
249+ // Test: Cancel Run
250+ TEST_F (JobsApiTest, CancelRunReturnsTrueAndCallsApi) {
251+ // Setup
252+ auto mock_client = std::make_shared<MockHttpClient>();
253+ EXPECT_CALL (*mock_client, post (" /jobs/runs/cancel" , _))
254+ .WillOnce (Return (MockHttpClient::success_response (R"( {"result":"OK"})" )));
255+
256+ // check_response should be called and not throw
257+ EXPECT_CALL (*mock_client, check_response (_, " cancelJob" )).Times (1 );
258+
259+ // Execute call with Mock Client
260+ databricks::Jobs jobs (mock_client);
261+ EXPECT_TRUE (jobs.cancel_run (42 ));
262+ }
263+
264+ // Test: Get Run Output of Completed Run
265+ TEST_F (JobsApiTest, GetRunOutputCompleted) {
266+ // Setup
267+ auto mock_client = std::make_shared<MockHttpClient>();
268+
269+ EXPECT_CALL (*mock_client, get (" /jobs/runs/get-output?run_id=123" ))
270+ .WillOnce (Return (MockHttpClient::success_response (R"( {"notebook_output":"success"} )" )));
271+
272+ EXPECT_CALL (*mock_client, check_response (_, " getRunOutput" )).Times (1 );
273+
274+ databricks::Jobs jobs (mock_client);
275+ auto output = jobs.get_run_output (123 );
276+ EXPECT_EQ (output.notebook_output , " success" );
277+ }
278+
279+ // Test: Get Run Output of a Failed Run
280+ TEST_F (JobsApiTest, GetRunOutputFailedRun) {
281+ // Setup
282+ auto mock_client = std::make_shared<MockHttpClient>();
283+ EXPECT_CALL (*mock_client, get (" /jobs/runs/get-output?run_id=123" ))
284+ .WillOnce (Return (MockHttpClient::success_response (R"( {"error":"error message","notebook_output":"failed"})" )));
285+
286+ EXPECT_CALL (*mock_client, check_response (_, " getRunOutput" )).Times (1 );
287+
288+ databricks::Jobs jobs (mock_client);
289+ auto output = jobs.get_run_output (123 );
290+ EXPECT_EQ (output.error , " error message" );
291+ EXPECT_EQ (output.notebook_output , " failed" );
292+ }
0 commit comments