aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jake Swenson <jake@jakeswenson.com> 2021-08-29 13:02:59 -0700
committerGravatar Jake Swenson <jake@jakeswenson.com> 2021-08-29 13:02:59 -0700
commita78a8d9444426a608888755eb1537905b7d1d96a (patch)
tree264fb77020ec9fc73b63a2258f731811b00e8ecc
parent1c22088640e5deeadd61017bd90920b652685b5f (diff)
downloadnotion-a78a8d9444426a608888755eb1537905b7d1d96a.tar.gz
notion-a78a8d9444426a608888755eb1537905b7d1d96a.tar.zst
notion-a78a8d9444426a608888755eb1537905b7d1d96a.zip
Split NotionApi tests out
-rw-r--r--src/lib.rs172
-rw-r--r--src/tests.rs161
2 files changed, 165 insertions, 168 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 98fe0fa..fab44ef 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,5 @@
use crate::ids::{BlockId, DatabaseId};
+use crate::models::error::ErrorResponse;
use crate::models::search::{DatabaseQuery, SearchRequest};
use crate::models::{Block, Database, ListResponse, Object, Page};
use ids::AsIdentifier;
@@ -7,10 +8,11 @@ use reqwest::{header, Client, ClientBuilder, RequestBuilder};
pub mod ids;
pub mod models;
-
-use crate::models::error::ErrorResponse;
pub use chrono;
+#[cfg(test)]
+mod tests;
+
const NOTION_API_VERSION: &str = "2021-08-16";
/// An wrapper Error type for all errors produced by the [`NotionApi`](NotionApi) client.
@@ -198,169 +200,3 @@ impl NotionApi {
}
}
}
-
-#[cfg(test)]
-mod tests {
- 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 {
- std::env::set_var("RUST_LOG", "notion");
- 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(())
- }
-}
diff --git a/src/tests.rs b/src/tests.rs
new file mode 100644
index 0000000..a0f2dea
--- /dev/null
+++ b/src/tests.rs
@@ -0,0 +1,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(())
+}