gitbutler/tests/deltas/mod.rs
Sebastian Thiel 7941db2b2f
Revert "Merge pull request #3378 from gitbutlerapp/crates"
This reverts commit 423b27c11a, reversing
changes made to b39779366f.
2024-03-30 22:25:36 +01:00

147 lines
4.7 KiB
Rust

mod database {
use crate::shared::test_database;
use gitbutler::deltas::{operations, Database, Delta};
use gitbutler::projects::ProjectId;
use gitbutler::sessions::SessionId;
use std::path;
#[test]
fn insert_query() -> anyhow::Result<()> {
let (db, _tmp) = test_database();
let database = Database::new(db);
let project_id = ProjectId::generate();
let session_id = SessionId::generate();
let file_path = path::PathBuf::from("file_path");
let delta1 = Delta {
timestamp_ms: 0,
operations: vec![operations::Operation::Insert((0, "text".to_string()))],
};
let deltas = vec![delta1.clone()];
database.insert(&project_id, &session_id, &file_path, &deltas)?;
assert_eq!(
database.list_by_project_id_session_id(&project_id, &session_id, &None)?,
vec![(file_path.display().to_string(), vec![delta1])]
.into_iter()
.collect()
);
Ok(())
}
#[test]
fn insert_update() -> anyhow::Result<()> {
let (db, _tmp) = test_database();
let database = Database::new(db);
let project_id = ProjectId::generate();
let session_id = SessionId::generate();
let file_path = path::PathBuf::from("file_path");
let delta1 = Delta {
timestamp_ms: 0,
operations: vec![operations::Operation::Insert((0, "text".to_string()))],
};
let delta2 = Delta {
timestamp_ms: 0,
operations: vec![operations::Operation::Insert((
0,
"updated_text".to_string(),
))],
};
database.insert(&project_id, &session_id, &file_path, &vec![delta1])?;
database.insert(&project_id, &session_id, &file_path, &vec![delta2.clone()])?;
assert_eq!(
database.list_by_project_id_session_id(&project_id, &session_id, &None)?,
vec![(file_path.display().to_string(), vec![delta2])]
.into_iter()
.collect()
);
Ok(())
}
#[test]
fn aggregate_deltas_by_file() -> anyhow::Result<()> {
let (db, _tmp) = test_database();
let database = Database::new(db);
let project_id = ProjectId::generate();
let session_id = SessionId::generate();
let file_path1 = path::PathBuf::from("file_path1");
let file_path2 = path::PathBuf::from("file_path2");
let delta1 = Delta {
timestamp_ms: 1,
operations: vec![operations::Operation::Insert((0, "text".to_string()))],
};
let delta2 = Delta {
timestamp_ms: 2,
operations: vec![operations::Operation::Insert((
0,
"updated_text".to_string(),
))],
};
database.insert(&project_id, &session_id, &file_path1, &vec![delta1.clone()])?;
database.insert(&project_id, &session_id, &file_path2, &vec![delta1.clone()])?;
database.insert(&project_id, &session_id, &file_path2, &vec![delta2.clone()])?;
assert_eq!(
database.list_by_project_id_session_id(&project_id, &session_id, &None)?,
vec![
(file_path1.display().to_string(), vec![delta1.clone()]),
(file_path2.display().to_string(), vec![delta1, delta2])
]
.into_iter()
.collect()
);
Ok(())
}
}
mod document;
mod operations;
mod writer {
use gitbutler::deltas::operations::Operation;
use gitbutler::{deltas, sessions};
use std::vec;
use crate::shared::{Case, Suite};
#[test]
fn write_no_vbranches() -> anyhow::Result<()> {
let suite = Suite::default();
let Case { gb_repository, .. } = &suite.new_case();
let deltas_writer = deltas::Writer::new(gb_repository)?;
let session = gb_repository.get_or_create_current_session()?;
let session_reader = sessions::Reader::open(gb_repository, &session)?;
let deltas_reader = gitbutler::deltas::Reader::new(&session_reader);
let path = "test.txt";
let deltas = vec![
gitbutler::deltas::Delta {
operations: vec![Operation::Insert((0, "hello".to_string()))],
timestamp_ms: 0,
},
gitbutler::deltas::Delta {
operations: vec![Operation::Insert((5, " world".to_string()))],
timestamp_ms: 0,
},
];
deltas_writer.write(path, &deltas).unwrap();
assert_eq!(deltas_reader.read_file(path).unwrap(), Some(deltas));
assert_eq!(deltas_reader.read_file("not found").unwrap(), None);
Ok(())
}
}