-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtests.rs
More file actions
99 lines (89 loc) · 2.98 KB
/
tests.rs
File metadata and controls
99 lines (89 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std::time::Duration;
use pliantdb_core::{
connection::{AccessPolicy, Connection},
test_util::{
Basic, BasicByBrokenParentId, BasicByParentId, BasicCollectionWithNoViews,
BasicCollectionWithOnlyBrokenParentId, ConnectionTest, TestDirectory,
},
};
use super::*;
use crate::Storage;
struct TestHarness {
_directory: TestDirectory,
db: Storage<Basic>,
}
impl TestHarness {
pub async fn new(test: ConnectionTest) -> anyhow::Result<Self> {
let directory = TestDirectory::new(test.to_string());
let db = Storage::<Basic>::open_local(&directory, &Configuration::default()).await?;
Ok(Self {
_directory: directory,
db,
})
}
pub async fn connect(&self) -> anyhow::Result<Storage<Basic>> {
Ok(self.db.clone())
}
}
pliantdb_core::define_connection_test_suite!(TestHarness);
#[tokio::test(flavor = "multi_thread")]
async fn integrity_checks() -> anyhow::Result<()> {
let path = TestDirectory::new("integrity-checks");
// Add a doc with no views installed
{
let db =
Storage::<BasicCollectionWithNoViews>::open_local(&path, &Configuration::default())
.await?;
let collection = db.collection::<BasicCollectionWithNoViews>();
collection.push(&Basic::default().with_parent_id(1)).await?;
}
// Connect with a new view
{
let db = Storage::<BasicCollectionWithOnlyBrokenParentId>::open_local(
&path,
&Configuration::default(),
)
.await?;
// Give the integrity scanner time to run if it were to run (it shouldn't in this configuration).
tokio::time::sleep(Duration::from_millis(100)).await;
// NoUpdate should return data without the validation checker having run.
assert_eq!(
db.view::<BasicByBrokenParentId>()
.with_access_policy(AccessPolicy::NoUpdate)
.query()
.await?
.len(),
0
);
// Regular query should show the correct data
assert_eq!(db.view::<BasicByBrokenParentId>().query().await?.len(), 1);
}
// Connect with a fixed view, and wait for the integrity scanner to work
{
let db = Storage::<Basic>::open_local(
&path,
&Configuration {
views: config::Views {
check_integrity_on_open: true,
},
..Configuration::default()
},
)
.await?;
for _ in 0_u8..10 {
tokio::time::sleep(Duration::from_millis(20)).await;
if db
.view::<BasicByParentId>()
.with_access_policy(AccessPolicy::NoUpdate)
.with_key(Some(1))
.query()
.await?
.len()
== 1
{
return Ok(());
}
}
panic!("Integrity checker didn't run in the allocated time")
}
}