mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-12-18 23:02:31 +03:00
3902bb9b2d
Sub-Handlers are objects, and with that comes a lot of complexity. However, there seems to be on need for it at all, and a first steps is to turn these into methods to truly understand what's going on.
151 lines
4.7 KiB
Rust
151 lines
4.7 KiB
Rust
mod database {
|
|
use std::path;
|
|
|
|
use gitbutler_core::{
|
|
deltas::{operations, Database, Delta},
|
|
projects::ProjectId,
|
|
sessions::SessionId,
|
|
};
|
|
|
|
use gitbutler_testsupport::test_database;
|
|
|
|
#[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, &[delta1])?;
|
|
database.insert(&project_id, &session_id, &file_path, &[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, &[delta1.clone()])?;
|
|
database.insert(&project_id, &session_id, &file_path2, &[delta1.clone()])?;
|
|
database.insert(&project_id, &session_id, &file_path2, &[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 std::vec;
|
|
|
|
use gitbutler_core::{deltas, deltas::operations::Operation, sessions};
|
|
|
|
use gitbutler_testsupport::{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_core::deltas::Reader::new(&session_reader);
|
|
|
|
let path = "test.txt";
|
|
let deltas = vec![
|
|
gitbutler_core::deltas::Delta {
|
|
operations: vec![Operation::Insert((0, "hello".to_string()))],
|
|
timestamp_ms: 0,
|
|
},
|
|
gitbutler_core::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(())
|
|
}
|
|
}
|