script.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. use collab_database::fields::{Field, TypeOptionData};
  2. use flowy_database2::entities::{CreateFieldParams, FieldChangesetParams, FieldType};
  3. use flowy_database2::services::cell::stringify_cell_data;
  4. use crate::database::database_editor::DatabaseEditorTest;
  5. pub enum FieldScript {
  6. CreateField {
  7. params: CreateFieldParams,
  8. },
  9. UpdateField {
  10. changeset: FieldChangesetParams,
  11. },
  12. DeleteField {
  13. field: Field,
  14. },
  15. SwitchToField {
  16. field_id: String,
  17. new_field_type: FieldType,
  18. },
  19. UpdateTypeOption {
  20. field_id: String,
  21. type_option: TypeOptionData,
  22. },
  23. AssertFieldCount(usize),
  24. AssertFieldTypeOptionEqual {
  25. field_index: usize,
  26. expected_type_option_data: TypeOptionData,
  27. },
  28. AssertCellContent {
  29. field_id: String,
  30. row_index: usize,
  31. from_field_type: FieldType,
  32. expected_content: String,
  33. },
  34. }
  35. pub struct DatabaseFieldTest {
  36. inner: DatabaseEditorTest,
  37. }
  38. impl DatabaseFieldTest {
  39. pub async fn new() -> Self {
  40. let editor_test = DatabaseEditorTest::new_grid().await;
  41. Self { inner: editor_test }
  42. }
  43. pub fn view_id(&self) -> String {
  44. self.view_id.clone()
  45. }
  46. pub fn field_count(&self) -> usize {
  47. self.field_count
  48. }
  49. pub async fn run_scripts(&mut self, scripts: Vec<FieldScript>) {
  50. for script in scripts {
  51. self.run_script(script).await;
  52. }
  53. }
  54. pub async fn run_script(&mut self, script: FieldScript) {
  55. match script {
  56. FieldScript::CreateField { params } => {
  57. self.field_count += 1;
  58. self
  59. .editor
  60. .create_field_with_type_option(&self.view_id, &params.field_type, params.type_option_data)
  61. .await;
  62. let fields = self.editor.get_fields(&self.view_id, None);
  63. assert_eq!(self.field_count, fields.len());
  64. },
  65. FieldScript::UpdateField { changeset: change } => {
  66. self.editor.update_field(change).await.unwrap();
  67. },
  68. FieldScript::DeleteField { field } => {
  69. if self.editor.get_field(&field.id).is_some() {
  70. self.field_count -= 1;
  71. }
  72. self.editor.delete_field(&field.id).await.unwrap();
  73. let fields = self.editor.get_fields(&self.view_id, None);
  74. assert_eq!(self.field_count, fields.len());
  75. },
  76. FieldScript::SwitchToField {
  77. field_id,
  78. new_field_type,
  79. } => {
  80. //
  81. self
  82. .editor
  83. .switch_to_field_type(&field_id, &new_field_type)
  84. .await
  85. .unwrap();
  86. },
  87. FieldScript::UpdateTypeOption {
  88. field_id,
  89. type_option,
  90. } => {
  91. //
  92. let old_field = self.editor.get_field(&field_id).unwrap();
  93. self
  94. .editor
  95. .update_field_type_option(&self.view_id, &field_id, type_option, old_field)
  96. .await
  97. .unwrap();
  98. },
  99. FieldScript::AssertFieldCount(count) => {
  100. assert_eq!(self.get_fields().len(), count);
  101. },
  102. FieldScript::AssertFieldTypeOptionEqual {
  103. field_index,
  104. expected_type_option_data,
  105. } => {
  106. let fields = self.get_fields();
  107. let field = &fields[field_index];
  108. let type_option_data = field.get_any_type_option(field.field_type).unwrap();
  109. assert_eq!(type_option_data, expected_type_option_data);
  110. },
  111. FieldScript::AssertCellContent {
  112. field_id,
  113. row_index,
  114. from_field_type,
  115. expected_content,
  116. } => {
  117. let field = self.editor.get_field(&field_id).unwrap();
  118. let field_type = FieldType::from(field.field_type);
  119. let rows = self.editor.get_rows(&self.view_id()).await.unwrap();
  120. let row = rows.get(row_index).unwrap();
  121. let cell = row.cells.get(&field_id).unwrap().clone();
  122. let content = stringify_cell_data(&cell, &from_field_type, &field_type, &field);
  123. assert_eq!(content, expected_content);
  124. },
  125. }
  126. }
  127. }
  128. impl std::ops::Deref for DatabaseFieldTest {
  129. type Target = DatabaseEditorTest;
  130. fn deref(&self) -> &Self::Target {
  131. &self.inner
  132. }
  133. }
  134. impl std::ops::DerefMut for DatabaseFieldTest {
  135. fn deref_mut(&mut self) -> &mut Self::Target {
  136. &mut self.inner
  137. }
  138. }