Skip to content

Commit 3cac6b4

Browse files
committed
additional unit tests w/ exception handling in core workspace code
1 parent 4a47526 commit 3cac6b4

2 files changed

Lines changed: 96 additions & 3 deletions

File tree

src/workspace/workspace.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ std::vector<ObjectInfo> Workspace::list(const std::string& path,
4747
const std::optional<uint64_t>& notebooks_modified_after) {
4848
internal::get_logger()->info("Listing workspace objects at path: " + path);
4949

50+
// Throw exception if path is empty
51+
if (path.length() == 0) {
52+
throw std::invalid_argument("Path cannot be empty");
53+
}
54+
5055
// Build query parameters
5156
std::string query_params = "?path=" + path;
5257
if (notebooks_modified_after.has_value()) {
@@ -63,6 +68,11 @@ std::vector<ObjectInfo> Workspace::list(const std::string& path,
6368
void Workspace::mkdirs(const std::string& path) {
6469
internal::get_logger()->info("Creating workspace directory: " + path);
6570

71+
// Throw exception if path is empty
72+
if (path.length() == 0) {
73+
throw std::invalid_argument("Path cannot be empty");
74+
}
75+
6676
// Build JSON body
6777
json body_json;
6878
body_json["path"] = path;
@@ -79,6 +89,11 @@ void Workspace::mkdirs(const std::string& path) {
7989
ObjectInfo Workspace::get_status(const std::string& path) {
8090
internal::get_logger()->info("Getting status for workspace object: " + path);
8191

92+
// Throw exception if path is empty
93+
if (path.length() == 0) {
94+
throw std::invalid_argument("Path cannot be empty");
95+
}
96+
8297
// Build query parameters
8398
std::string query_params = "?path=" + path;
8499

@@ -92,6 +107,11 @@ ObjectInfo Workspace::get_status(const std::string& path) {
92107
ExportResponse Workspace::export_file(const std::string& path, ExportFormat format) {
93108
internal::get_logger()->info("Exporting workspace object: " + path);
94109

110+
// Throw exception if path is empty
111+
if (path.length() == 0) {
112+
throw std::invalid_argument("Path cannot be empty");
113+
}
114+
95115
// Build query parameters
96116
std::string query_params = "?path=" + path;
97117
query_params += "&format=" + export_format_to_string(format);
@@ -112,6 +132,11 @@ void Workspace::import_file(const std::string& path,
112132
bool overwrite) {
113133
internal::get_logger()->info("Importing workspace object to path: " + path);
114134

135+
// Throw exception for invalid arguments
136+
if (path.length() == 0 || content.length() == 0 ) {
137+
throw std::invalid_argument("Path or Content Input cannot be empty");
138+
}
139+
115140
// Build JSON body
116141
json body_json;
117142
body_json["path"] = path;
@@ -145,6 +170,11 @@ void Workspace::import_file(const ImportRequest& request) {
145170
void Workspace::delete_object(const std::string& path, bool recursive) {
146171
internal::get_logger()->info("Deleting workspace object: " + path + " (recursive=" + (recursive ? "true" : "false") + ")");
147172

173+
// Throw exception if path is empty
174+
if (path.length() == 0) {
175+
throw std::invalid_argument("Path cannot be empty");
176+
}
177+
148178
// Build JSON body
149179
json body_json;
150180
body_json["path"] = path;

tests/unit/workspace/test_workspace.cpp

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using databricks::test::MockHttpClient;
1010
using ::testing::_;
1111
using ::testing::HasSubstr;
12+
using ::testing::Eq;
1213
using ::testing::NiceMock;
1314
using ::testing::Return;
1415
using ::testing::Throw;
@@ -67,7 +68,7 @@ TEST_F(WorkspaceTest, MultipleValidWorkspaceClient) {
6768
// ============================================================================
6869

6970
// Test: List Workspace Objects
70-
TEST_F(WorkspaceApiTest, SuccessfulListWorkspaceObjects) {
71+
TEST_F(WorkspaceApiTest, ListWorkspaceObjectsSuccess) {
7172
std::string mock_list_response = R"({
7273
"objects": [
7374
{
@@ -112,7 +113,8 @@ TEST_F(WorkspaceApiTest, SuccessfulListWorkspaceObjects) {
112113
EXPECT_EQ(response[1].object_id, 67890);
113114
}
114115

115-
TEST_F(WorkspaceApiTest, SuccessfulListEmptyWorkspaceObjects) {
116+
// Test: List Empty Workspace Objects
117+
TEST_F(WorkspaceApiTest, ListEmptyWorkspaceObjectsSuccess) {
116118
const std::string& mock_empty_list_response = R"({
117119
"objects": []
118120
})";
@@ -126,4 +128,65 @@ TEST_F(WorkspaceApiTest, SuccessfulListEmptyWorkspaceObjects) {
126128

127129
// Verify we got 0 objects back
128130
ASSERT_EQ(response.size(), 0);
129-
}
131+
}
132+
133+
// Test: Fail to list Workspace Objects with an empty path
134+
TEST_F(WorkspaceApiTest, ListWorkspaceObjectThrowsInvalidArgument) {
135+
databricks::Workspace workspace(mock_client_);
136+
const std::string& empty_path = "";
137+
138+
EXPECT_THROW(workspace.list(empty_path), std::invalid_argument);
139+
}
140+
141+
// Test: Create a Mock Directory
142+
TEST_F(WorkspaceApiTest, CreateDirectorySuccess) {
143+
EXPECT_CALL(*mock_client_, post("/workspace/mkdirs", Eq(R"({"path":"/test/path"})")))
144+
.WillOnce(Return(MockHttpClient::success_response("")));
145+
146+
databricks::Workspace workspace(mock_client_);
147+
const std::string& mock_path = "/test/path";
148+
workspace.mkdirs(mock_path);
149+
}
150+
151+
// Test: Fail to create an empty Mock Directory
152+
TEST_F(WorkspaceApiTest, CreateEmptyDirectoryThrowsInvalidArgument) {
153+
databricks::Workspace workspace(mock_client_);
154+
const std::string& empty_path = "";
155+
156+
EXPECT_THROW(workspace.mkdirs(empty_path), std::invalid_argument);
157+
}
158+
159+
// Test: Get Status for a Workspace object
160+
TEST_F(WorkspaceApiTest, GetStatusSuccess) {
161+
std::string mock_status_response = R"({
162+
"path": "/test/notebook",
163+
"object_type": "NOTEBOOK",
164+
"object_id": 12345,
165+
"language": "PYTHON",
166+
"size": 2048,
167+
"created_at": 1609459200000,
168+
"modified_at": 1609545600000
169+
})";
170+
171+
EXPECT_CALL(*mock_client_, get("/workspace/get-status?path=/test/notebook"))
172+
.WillOnce(Return(MockHttpClient::success_response(mock_status_response)));
173+
174+
databricks::Workspace workspace(mock_client_);
175+
const std::string& mock_path = "/test/notebook";
176+
auto response = workspace.get_status(mock_path);
177+
178+
// Verify object status
179+
EXPECT_EQ(response.path, "/test/notebook");
180+
EXPECT_EQ(response.object_type, databricks::ObjectType::NOTEBOOK);
181+
EXPECT_EQ(response.object_id, 12345);
182+
EXPECT_EQ(response.language, databricks::Language::PYTHON);
183+
EXPECT_EQ(response.size, 2048);
184+
}
185+
186+
// Test: Fail to get status for an empty path Workspace object
187+
TEST_F(WorkspaceApiTest, GetStatusThrowsInvalidArgument) {
188+
databricks::Workspace workspace(mock_client_);
189+
const std::string& empty_path = "";
190+
191+
EXPECT_THROW(workspace.get_status(empty_path), std::invalid_argument);
192+
}

0 commit comments

Comments
 (0)