Skip to content

Commit

Permalink
fix rename df error after editing and committing to a new folder (#531)
Browse files Browse the repository at this point in the history
* fix edit then rename error

* clippy

* clippy
  • Loading branch information
EloyMartinez authored Feb 27, 2025
1 parent a12e674 commit d3312a8
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
101 changes: 101 additions & 0 deletions src/lib/src/api/client/workspaces/data_frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,107 @@ mod tests {
.await
}

#[tokio::test]
async fn test_edit_rename_and_commit_data_frame_to_nonexistent_folder() -> Result<(), OxenError>
{
test::run_remote_repo_test_bounding_box_csv_pushed(|_local_repo, remote_repo| async move {
let workspace_id = UserConfig::identifier()?;
let workspace =
api::client::workspaces::create(&remote_repo, DEFAULT_BRANCH_NAME, &workspace_id)
.await?;
assert_eq!(workspace.id, workspace_id);

// Define the original path for the data frame

let original_path = Path::new("annotations")
.join(Path::new("train"))
.join(Path::new("bounding_box.csv"));
let new_path = Path::new("annotations")
.join(Path::new("train"))
.join(Path::new("nonexistent_folder"))
.join(Path::new("bounding_box_edited.csv"));

// Index the original data frame
api::client::workspaces::data_frames::index(
&remote_repo,
&workspace.id,
&original_path,
)
.await?;

let original_df = api::client::data_frames::get(
&remote_repo,
DEFAULT_BRANCH_NAME,
&original_path,
DFOpts::empty(),
)
.await?;

let og_row_count = original_df.data_frame.view.to_df().height();
let new_row = r#"{"file": "train/dog_4.jpg", "label": "dog", "min_x": 15.0, "min_y": 20.0, "width": 300, "height": 400}"#;


// Edit the data frame (this is a placeholder for your actual edit logic)
let edit_response = api::client::workspaces::data_frames::rows::add(
&remote_repo,
&workspace.id,
&original_path,
new_row.to_string(), // Assuming this function takes the new path as a parameter
)
.await;
assert!(edit_response.is_ok());
api::client::workspaces::data_frames::rename_data_frame(
&remote_repo,
&workspace.id,
&original_path,
&new_path,
)
.await?;

// Commit the changes
let user = UserConfig::get()?.to_user();
let new_commit = NewCommitBody {
author: user.name.to_owned(),
email: user.email.to_owned(),
message: "edited data frame".to_string(),
};

api::client::workspaces::commit(
&remote_repo,
DEFAULT_BRANCH_NAME,
&workspace.id,
&new_commit,
)
.await?;

// Verify that the edited data frame has the new name
let edited_df = api::client::data_frames::get(
&remote_repo,
DEFAULT_BRANCH_NAME,
&new_path,
DFOpts::empty(),
)
.await?;
assert_eq!(edited_df.status.status_message, "resource_found");
let new_row_count = edited_df.data_frame.view.to_df().height();
assert_eq!(new_row_count, og_row_count + 1);

// Verify that the original data frame still exists
let original_df = api::client::data_frames::get(
&remote_repo,
DEFAULT_BRANCH_NAME,
&original_path,
DFOpts::empty(),
)
.await?;
assert_eq!(original_df.data_frame.view.to_df().height(), og_row_count);
assert_eq!(original_df.status.status_message, "resource_found");

Ok(remote_repo)
})
.await
}

#[tokio::test]
async fn test_edit_rename_and_commit_data_frame() -> Result<(), OxenError> {
test::run_remote_repo_test_bounding_box_csv_pushed(|_local_repo, remote_repo| async move {
Expand Down
9 changes: 8 additions & 1 deletion src/lib/src/core/v_latest/workspaces/data_frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use std::sync::{Arc, Mutex};

use crate::model::merkle_tree::node::{EMerkleTreeNode, FileNode, StagedMerkleTreeNode};
use crate::model::staged_row_status::StagedRowStatus;
use crate::model::{Commit, EntryDataType, LocalRepository, MerkleHash, Workspace};
use crate::model::{
Commit, EntryDataType, LocalRepository, MerkleHash, StagedEntryStatus, Workspace,
};
use crate::repositories;
use crate::{error::OxenError, util};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -261,6 +263,11 @@ pub fn rename(
file.set_name(new_path.to_str().unwrap());
}

// The og status of this entry would be modified, but since we are changing its destination
// specially if we out it in a new folder, we need to ensure it is set to added
// So in compute_dir_node we set the num_entries of the parent dir to 1.
new_staged_entry.status = StagedEntryStatus::Added;

let mut buf = Vec::new();
new_staged_entry
.serialize(&mut Serializer::new(&mut buf))
Expand Down

0 comments on commit d3312a8

Please sign in to comment.