Skip to content

Commit 4a47526

Browse files
committed
unit tests: list workspace objects
1 parent 64fafeb commit 4a47526

1 file changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright (c) 2026 Calvin Min
2+
// SPDX-License-Identifier: MIT
3+
#include "../../mocks/mock_http_client.h"
4+
5+
#include <databricks/core/config.h>
6+
#include <databricks/workspace/workspace.h>
7+
#include <gtest/gtest.h>
8+
9+
using databricks::test::MockHttpClient;
10+
using ::testing::_;
11+
using ::testing::HasSubstr;
12+
using ::testing::NiceMock;
13+
using ::testing::Return;
14+
using ::testing::Throw;
15+
16+
// ============================================================================
17+
// Test Fixtures
18+
// ============================================================================
19+
20+
// Constructor Tests
21+
class WorkspaceTest : public ::testing::Test {
22+
protected:
23+
databricks::AuthConfig auth;
24+
25+
void SetUp() override {
26+
auth.host = "https://test.databricks.com";
27+
auth.set_token("test_token");
28+
auth.timeout_seconds = 30;
29+
}
30+
};
31+
32+
// Workspace API Mocks
33+
class WorkspaceApiTest : public ::testing::Test {
34+
protected:
35+
void SetUp() override { mock_client_ = std::make_shared<NiceMock<MockHttpClient>>(); }
36+
37+
std::shared_ptr<NiceMock<MockHttpClient>> mock_client_;
38+
};
39+
40+
// ============================================================================
41+
// Constructor Tests
42+
// ============================================================================
43+
TEST_F(WorkspaceTest, ConstructorCreatesValidClient) {
44+
ASSERT_NO_THROW({databricks::Workspace workspace(auth);});
45+
}
46+
47+
TEST_F(WorkspaceTest, ConstructorCreatesValidClientWithManualApiVersion) {
48+
const std::string& api_version = "1.0";
49+
ASSERT_NO_THROW({
50+
databricks::Workspace workspace(auth, api_version);
51+
});
52+
}
53+
54+
TEST_F(WorkspaceTest, MultipleValidWorkspaceClient) {
55+
databricks::AuthConfig mockAuth;
56+
mockAuth.host = "https://workspace1.databricks.com";
57+
mockAuth.set_token("token1");
58+
59+
ASSERT_NO_THROW({
60+
databricks::Workspace workspace1(auth);
61+
databricks::Workspace workspace2(mockAuth);
62+
});
63+
}
64+
65+
// ============================================================================
66+
// Workspace API Test
67+
// ============================================================================
68+
69+
// Test: List Workspace Objects
70+
TEST_F(WorkspaceApiTest, SuccessfulListWorkspaceObjects) {
71+
std::string mock_list_response = R"({
72+
"objects": [
73+
{
74+
"path": "/test/path/notebook1",
75+
"object_type": "NOTEBOOK",
76+
"object_id": 12345,
77+
"language": "PYTHON",
78+
"size": 1024,
79+
"created_at": 1609459200000,
80+
"modified_at": 1609545600000
81+
},
82+
{
83+
"path": "/test/path/directory1",
84+
"object_type": "DIRECTORY",
85+
"object_id": 67890
86+
}
87+
]
88+
})";
89+
90+
EXPECT_CALL(*mock_client_, get("/workspace/list?path=/test/path"))
91+
.WillOnce(Return(MockHttpClient::success_response(mock_list_response)));
92+
93+
databricks::Workspace workspace(mock_client_);
94+
const std::string& mock_path = "/test/path";
95+
auto response = workspace.list(mock_path);
96+
97+
// Verify we got 2 objects back
98+
ASSERT_EQ(response.size(), 2);
99+
100+
// Verify first object (notebook)
101+
EXPECT_EQ(response[0].path, "/test/path/notebook1");
102+
EXPECT_EQ(response[0].object_type, databricks::ObjectType::NOTEBOOK);
103+
EXPECT_EQ(response[0].object_id, 12345);
104+
EXPECT_EQ(response[0].language, databricks::Language::PYTHON);
105+
EXPECT_EQ(response[0].size, 1024);
106+
EXPECT_EQ(response[0].created_at, 1609459200000);
107+
EXPECT_EQ(response[0].modified_at, 1609545600000);
108+
109+
// Verify second object (directory)
110+
EXPECT_EQ(response[1].path, "/test/path/directory1");
111+
EXPECT_EQ(response[1].object_type, databricks::ObjectType::DIRECTORY);
112+
EXPECT_EQ(response[1].object_id, 67890);
113+
}
114+
115+
TEST_F(WorkspaceApiTest, SuccessfulListEmptyWorkspaceObjects) {
116+
const std::string& mock_empty_list_response = R"({
117+
"objects": []
118+
})";
119+
120+
EXPECT_CALL(*mock_client_, get("/workspace/list?path=/test/path"))
121+
.WillOnce(Return(MockHttpClient::success_response(mock_empty_list_response)));
122+
123+
databricks::Workspace workspace(mock_client_);
124+
const std::string& mock_path = "/test/path";
125+
auto response = workspace.list(mock_path);
126+
127+
// Verify we got 0 objects back
128+
ASSERT_EQ(response.size(), 0);
129+
}

0 commit comments

Comments
 (0)