script.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. use crate::database::database_editor::DatabaseEditorTest;
  2. use collab_database::database::gen_row_id;
  3. use collab_database::rows::RowId;
  4. use lib_infra::util::timestamp;
  5. pub enum RowScript {
  6. CreateEmptyRow,
  7. UpdateTextCell { row_id: RowId, content: String },
  8. AssertRowCount(usize),
  9. }
  10. pub struct DatabaseRowTest {
  11. inner: DatabaseEditorTest,
  12. }
  13. impl DatabaseRowTest {
  14. pub async fn new() -> Self {
  15. let editor_test = DatabaseEditorTest::new_grid().await;
  16. Self { inner: editor_test }
  17. }
  18. pub async fn run_scripts(&mut self, scripts: Vec<RowScript>) {
  19. for script in scripts {
  20. self.run_script(script).await;
  21. }
  22. }
  23. pub async fn run_script(&mut self, script: RowScript) {
  24. match script {
  25. RowScript::CreateEmptyRow => {
  26. let params = collab_database::rows::CreateRowParams {
  27. id: gen_row_id(),
  28. timestamp: timestamp(),
  29. ..Default::default()
  30. };
  31. let row_order = self
  32. .editor
  33. .create_row(&self.view_id, None, params)
  34. .await
  35. .unwrap()
  36. .unwrap();
  37. self
  38. .row_by_row_id
  39. .insert(row_order.id.to_string(), row_order.into());
  40. self.rows = self.get_rows().await;
  41. },
  42. RowScript::UpdateTextCell { row_id, content } => {
  43. self.update_text_cell(row_id, &content).await.unwrap();
  44. },
  45. RowScript::AssertRowCount(expected_row_count) => {
  46. assert_eq!(expected_row_count, self.rows.len());
  47. },
  48. }
  49. }
  50. }
  51. impl std::ops::Deref for DatabaseRowTest {
  52. type Target = DatabaseEditorTest;
  53. fn deref(&self) -> &Self::Target {
  54. &self.inner
  55. }
  56. }
  57. impl std::ops::DerefMut for DatabaseRowTest {
  58. fn deref_mut(&mut self) -> &mut Self::Target {
  59. &mut self.inner
  60. }
  61. }