Add buffer integration test

Rearrange channel crate structure
Get channel buffer from database

co-authored-by: Max <max@zed.dev>
This commit is contained in:
Mikayla 2023-08-21 16:30:57 -07:00
parent ff5035ea37
commit a7a4e2e369
No known key found for this signature in database
33 changed files with 403 additions and 39 deletions

View file

@ -1,41 +0,0 @@
use super::*;
use crate::test_both_dbs;
use language::proto;
use text::Buffer;
test_both_dbs!(test_buffers, test_buffers_postgres, test_buffers_sqlite);
async fn test_buffers(db: &Arc<Database>) {
let buffer_id = db.create_buffer().await.unwrap();
let mut buffer = Buffer::new(0, 0, "".to_string());
let mut operations = Vec::new();
operations.push(buffer.edit([(0..0, "hello world")]));
operations.push(buffer.edit([(5..5, ", cruel")]));
operations.push(buffer.edit([(0..5, "goodbye")]));
operations.push(buffer.undo().unwrap().1);
assert_eq!(buffer.text(), "hello, cruel world");
let operations = operations
.into_iter()
.map(|op| proto::serialize_operation(&language::Operation::Buffer(op)))
.collect::<Vec<_>>();
db.update_buffer(buffer_id, &operations).await.unwrap();
let buffer_data = db.get_buffer(buffer_id).await.unwrap();
let mut buffer_2 = Buffer::new(0, 0, buffer_data.base_text);
buffer_2
.apply_ops(buffer_data.operations.into_iter().map(|operation| {
let operation = proto::deserialize_operation(operation).unwrap();
if let language::Operation::Buffer(operation) = operation {
operation
} else {
unreachable!()
}
}))
.unwrap();
assert_eq!(buffer_2.text(), "hello, cruel world");
}

View file

@ -689,6 +689,34 @@ impl Database {
})
.await
}
pub async fn get_or_create_buffer_for_channel(
&self,
channel_id: ChannelId,
) -> Result<BufferId> {
self.transaction(|tx| async move {
let tx = tx;
let channel = channel::Entity::find_by_id(channel_id)
.one(&*tx)
.await?
.ok_or_else(|| anyhow!("invalid channel"))?;
if let Some(id) = channel.main_buffer_id {
return Ok(id);
} else {
let buffer = buffer::ActiveModel::new().insert(&*tx).await?;
channel::ActiveModel {
id: ActiveValue::Unchanged(channel_id),
main_buffer_id: ActiveValue::Set(Some(buffer.id)),
..Default::default()
}
.update(&*tx)
.await?;
Ok(buffer.id)
}
})
.await
}
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]