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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
use crate::ids::BlockId;
use crate::models::search::PropertyCondition::Text;
use crate::models::search::{
DatabaseQuery, FilterCondition, FilterProperty, FilterValue, NotionSearch, TextCondition,
};
use crate::models::Object;
use crate::NotionApi;
fn test_token() -> String {
let token = {
if let Some(token) = std::env::var("NOTION_API_TOKEN").ok() {
token
} else if let Some(token) = std::fs::read_to_string(".api_token").ok() {
token
} else {
panic!("No API Token found in environment variable 'NOTION_API_TOKEN'!")
}
};
token.trim().to_string()
}
fn test_client() -> NotionApi {
NotionApi::new(test_token()).unwrap()
}
#[tokio::test]
async fn list_databases() -> Result<(), Box<dyn std::error::Error>> {
let api = test_client();
dbg!(api.list_databases().await?);
Ok(())
}
#[tokio::test]
async fn search_databases() -> Result<(), Box<dyn std::error::Error>> {
let api = test_client();
let response = api
.search(NotionSearch::Filter {
property: FilterProperty::Object,
value: FilterValue::Database,
})
.await?;
assert!(response.results.len() > 0);
Ok(())
}
#[tokio::test]
async fn search_pages() -> Result<(), Box<dyn std::error::Error>> {
let api = test_client();
let response = api
.search(NotionSearch::Filter {
property: FilterProperty::Object,
value: FilterValue::Page,
})
.await?;
assert!(response.results.len() > 0);
Ok(())
}
#[tokio::test]
async fn get_database() -> Result<(), Box<dyn std::error::Error>> {
let api = test_client();
let response = api
.search(NotionSearch::Filter {
value: FilterValue::Database,
property: FilterProperty::Object,
})
.await?;
let db = response
.results()
.iter()
.filter_map(|o| match o {
Object::Database { database } => Some(database),
_ => None,
})
.next()
.expect("Test expected to find at least one database in notion")
.clone();
// todo: fix this clone issue
let db_result = api.get_database(db.clone()).await?;
assert_eq!(db, db_result);
Ok(())
}
#[tokio::test]
async fn get_block_children() -> Result<(), Box<dyn std::error::Error>> {
let api = test_client();
let search_response = api
.search(NotionSearch::Filter {
value: FilterValue::Page,
property: FilterProperty::Object,
})
.await?;
println!("{:?}", search_response.results.len());
for object in search_response.results {
match object {
Object::Page { page } => api
.get_block_children(BlockId::from(page.id))
.await
.unwrap(),
_ => panic!("Should not have received anything but pages!"),
};
}
Ok(())
}
#[tokio::test]
async fn query_database() -> Result<(), Box<dyn std::error::Error>> {
let api = test_client();
let response = api
.search(NotionSearch::Filter {
value: FilterValue::Database,
property: FilterProperty::Object,
})
.await?;
let db = response
.results()
.iter()
.filter_map(|o| match o {
Object::Database { database } => Some(database),
_ => None,
})
.next()
.expect("Test expected to find at least one database in notion")
.clone();
let pages = api
.query_database(
db,
DatabaseQuery {
filter: Some(FilterCondition {
property: "Name".to_string(),
condition: Text(TextCondition::Contains("First".to_string())),
}),
..Default::default()
},
)
.await?;
assert_eq!(pages.results().len(), 1);
Ok(())
}
|