| 
									
										
										
										
											2023-06-03 23:35:55 +08:00
										 |  |  | use std::time::Duration;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 |  |  | use flowy_database2::entities::FieldType;
 | 
					
						
							|  |  |  | use flowy_database2::services::field::DateCellData;
 | 
					
						
							| 
									
										
										
										
											2023-05-28 16:14:25 +08:00
										 |  |  | use lib_infra::util::timestamp;
 | 
					
						
							| 
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-03 23:35:55 +08:00
										 |  |  | use crate::database::block_test::script::DatabaseRowTest;
 | 
					
						
							|  |  |  | use crate::database::block_test::script::RowScript::*;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-28 16:14:25 +08:00
										 |  |  | // Create a new row at the end of the grid and check the create time is valid.
 | 
					
						
							| 
									
										
										
										
											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;
 | 
					
						
							| 
									
										
										
										
											2023-06-14 22:16:33 +08:00
										 |  |  |   let row_count = test.row_details.len();
 | 
					
						
							| 
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 |  |  |   test
 | 
					
						
							|  |  |  |     .run_scripts(vec![CreateEmptyRow, AssertRowCount(row_count + 1)])
 | 
					
						
							|  |  |  |     .await;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-28 16:14:25 +08:00
										 |  |  |   // Get created time of the new row.
 | 
					
						
							| 
									
										
										
										
											2023-06-14 22:16:33 +08:00
										 |  |  |   let row_detail = test.get_rows().await.last().cloned().unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-05-31 17:34:41 +08:00
										 |  |  |   let updated_at_field = test.get_first_field(FieldType::CreatedTime);
 | 
					
						
							| 
									
										
										
										
											2023-06-03 23:35:55 +08:00
										 |  |  |   let cell = test
 | 
					
						
							|  |  |  |     .editor
 | 
					
						
							| 
									
										
										
										
											2023-06-14 22:16:33 +08:00
										 |  |  |     .get_cell(&updated_at_field.id, &row_detail.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
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Update row and check the update time is valid.
 | 
					
						
							|  |  |  | #[tokio::test]
 | 
					
						
							|  |  |  | async fn update_at_field_test() {
 | 
					
						
							|  |  |  |   let mut test = DatabaseRowTest::new().await;
 | 
					
						
							| 
									
										
										
										
											2023-06-14 22:16:33 +08:00
										 |  |  |   let row_detail = test.get_rows().await.remove(0);
 | 
					
						
							| 
									
										
										
										
											2023-06-03 23:35:55 +08:00
										 |  |  |   let last_edit_field = test.get_first_field(FieldType::LastEditedTime);
 | 
					
						
							|  |  |  |   let cell = test
 | 
					
						
							|  |  |  |     .editor
 | 
					
						
							| 
									
										
										
										
											2023-06-14 22:16:33 +08:00
										 |  |  |     .get_cell(&last_edit_field.id, &row_detail.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-04 09:28:13 +08:00
										 |  |  |   tokio::time::sleep(Duration::from_millis(1000)).await;
 | 
					
						
							| 
									
										
										
										
											2023-05-28 16:14:25 +08:00
										 |  |  |   test
 | 
					
						
							|  |  |  |     .run_script(UpdateTextCell {
 | 
					
						
							| 
									
										
										
										
											2023-06-14 22:16:33 +08:00
										 |  |  |       row_id: row_detail.row.id.clone(),
 | 
					
						
							| 
									
										
										
										
											2023-05-28 16:14:25 +08:00
										 |  |  |       content: "test".to_string(),
 | 
					
						
							|  |  |  |     })
 | 
					
						
							|  |  |  |     .await;
 | 
					
						
							| 
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-28 16:14:25 +08:00
										 |  |  |   // Get the updated time of the row.
 | 
					
						
							| 
									
										
										
										
											2023-06-14 22:16:33 +08:00
										 |  |  |   let row_detail = test.get_rows().await.remove(0);
 | 
					
						
							| 
									
										
										
										
											2023-06-03 23:35:55 +08:00
										 |  |  |   let last_edit_field = test.get_first_field(FieldType::LastEditedTime);
 | 
					
						
							|  |  |  |   let cell = test
 | 
					
						
							|  |  |  |     .editor
 | 
					
						
							| 
									
										
										
										
											2023-06-14 22:16:33 +08:00
										 |  |  |     .get_cell(&last_edit_field.id, &row_detail.row.id)
 | 
					
						
							| 
									
										
										
										
											2023-06-03 23:35:55 +08:00
										 |  |  |     .await
 | 
					
						
							|  |  |  |     .unwrap();
 | 
					
						
							|  |  |  |   let new_updated_at = DateCellData::from(&cell).timestamp.unwrap();
 | 
					
						
							| 
									
										
										
										
											2023-05-28 16:14:25 +08:00
										 |  |  |   assert!(old_updated_at < new_updated_at);
 | 
					
						
							| 
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 |  |  | }
 |