2024-09-25 11:44:19 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								use collab_database::fields::date_type_option::DateCellData;
							 | 
						
					
						
							
								
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								use flowy_database2::entities::FieldType;
							 | 
						
					
						
							
								
									
										
										
										
											2023-05-28 16:14:25 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								use lib_infra::util::timestamp;
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-25 11:44:19 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								use std::time::Duration;
							 | 
						
					
						
							
								
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2023-06-03 23:35:55 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								use crate::database::block_test::script::DatabaseRowTest;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#[tokio::test]
							 | 
						
					
						
							
								
									
										
										
										
											2023-05-28 16:14:25 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								async fn created_at_field_test() {
							 | 
						
					
						
							
								
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  let mut test = DatabaseRowTest::new().await;
							 | 
						
					
						
							
								
									
										
										
										
											2024-10-15 09:32:06 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  // Get initial row count
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-26 09:46:16 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  let row_count = test.rows.len();
							 | 
						
					
						
							
								
									
										
										
										
											2024-10-15 09:32:06 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  // Create a new row and assert the row count has increased by 1
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  test.create_empty_row().await;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  test.assert_row_count(row_count + 1).await;
							 | 
						
					
						
							
								
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2023-05-28 16:14:25 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  // Get created time of the new row.
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-26 09:46:16 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  let row = test.get_rows().await.last().cloned().unwrap();
							 | 
						
					
						
							
								
									
										
										
										
											2024-10-15 09:32:06 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  let created_at_field = test.get_first_field(FieldType::CreatedTime).await;
							 | 
						
					
						
							
								
									
										
										
										
											2023-06-03 23:35:55 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  let cell = test
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    .editor
							 | 
						
					
						
							
								
									
										
										
										
											2024-10-15 09:32:06 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    .get_cell(&created_at_field.id, &row.id)
							 | 
						
					
						
							
								
									
										
										
										
											2023-06-03 23:35:55 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    .await
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    .unwrap();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  let created_at_timestamp = DateCellData::from(&cell).timestamp.unwrap();
							 | 
						
					
						
							
								
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2023-05-28 16:14:25 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  assert!(created_at_timestamp > 0);
							 | 
						
					
						
							
								
									
										
										
										
											2023-06-03 23:35:55 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  assert!(created_at_timestamp <= timestamp());
							 | 
						
					
						
							
								
									
										
										
										
											2023-05-28 16:14:25 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#[tokio::test]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								async fn update_at_field_test() {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  let mut test = DatabaseRowTest::new().await;
							 | 
						
					
						
							
								
									
										
										
										
											2024-10-15 09:32:06 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  // Get the first row and the current LastEditedTime field
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-26 09:46:16 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  let row = test.get_rows().await.remove(0);
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-18 05:16:42 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  let last_edit_field = test.get_first_field(FieldType::LastEditedTime).await;
							 | 
						
					
						
							
								
									
										
										
										
											2023-06-03 23:35:55 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  let cell = test
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    .editor
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-26 09:46:16 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    .get_cell(&last_edit_field.id, &row.id)
							 | 
						
					
						
							
								
									
										
										
										
											2023-06-03 23:35:55 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    .await
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    .unwrap();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  let old_updated_at = DateCellData::from(&cell).timestamp.unwrap();
							 | 
						
					
						
							
								
									
										
										
										
											2023-05-28 16:14:25 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-10-15 09:32:06 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  // Wait for 1 second before updating the row
							 | 
						
					
						
							
								
									
										
										
										
											2023-06-04 09:28:13 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  tokio::time::sleep(Duration::from_millis(1000)).await;
							 | 
						
					
						
							
								
									
										
										
										
											2024-10-15 09:32:06 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  // Update the text cell of the first row
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  test.update_text_cell(row.id.clone(), "test").await;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  // Get the updated time of the row
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-26 09:46:16 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  let row = test.get_rows().await.remove(0);
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-18 05:16:42 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  let last_edit_field = test.get_first_field(FieldType::LastEditedTime).await;
							 | 
						
					
						
							
								
									
										
										
										
											2023-06-03 23:35:55 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  let cell = test
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    .editor
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-26 09:46:16 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    .get_cell(&last_edit_field.id, &row.id)
							 | 
						
					
						
							
								
									
										
										
										
											2023-06-03 23:35:55 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    .await
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    .unwrap();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  let new_updated_at = DateCellData::from(&cell).timestamp.unwrap();
							 | 
						
					
						
							
								
									
										
										
										
											2024-10-15 09:32:06 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2023-05-28 16:14:25 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  assert!(old_updated_at < new_updated_at);
							 | 
						
					
						
							
								
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 |