Compare commits

..

1 commit

Author SHA1 Message Date
Bram Dingelstad 9278a1afc1 wip: started working on test 2024-02-24 10:14:16 +01:00
4 changed files with 593 additions and 58 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
.env
/target
/Cargo.lock

View file

@ -17,8 +17,9 @@ lazy_static = "1.4.0"
log = "0.4.20"
regex = "1.7.1"
reqwest = { version = "0.11.14", features = ["json"] }
serde = { version = "^1.0", features = ["derive"], default-features = false }
serde_json = { version = "^1.0", features = ["raw_value"], default-features = false }
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.91"
[dev-dependencies]
dotenv = "0.15.0"
tokio = { version = "1.28.1", features = ["macros"] }

View file

@ -368,48 +368,6 @@ impl Databases {
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn check_database_query() {
let databases = Client::new()
.api_key("secret_FuhJkAoOVZlk8YUT9ZOeYqWBRRZN6OMISJwhb4dTnud")
.build()
.search::<Database>(SearchOptions {
filter: Some(json!(
{
"value": "database",
"property": "object"
}
)),
query: None,
page_size: None,
sort: None,
start_cursor: None,
})
.await;
println!("{databases:#?}");
}
#[tokio::test]
async fn test_blocks() {
let blocks = Client::new()
.api_key("secret_FuhJkAoOVZlk8YUT9ZOeYqWBRRZN6OMISJwhb4dTnud")
.build()
.blocks
.children()
.list(BlockChildrenListOptions {
block_id: "0d253ab0f751443aafb9bcec14012897",
})
.await;
println!("{blocks:#?}")
}
}
#[derive(Debug, Default)]
pub struct DatabaseQueryOptions<'a> {
pub database_id: &'a str,
@ -871,10 +829,6 @@ pub enum DatabaseProperty {
id: String,
name: String,
},
Button {
id: String,
name: String,
},
Unsupported(Value),
}
@ -902,7 +856,6 @@ impl DatabaseProperty {
| Select { id, .. }
| Status { id, .. }
| Title { id, .. }
| Button { id, .. }
| Url { id, .. } => Some(id.to_owned()),
Unsupported(..) => None,
@ -930,7 +883,6 @@ impl DatabaseProperty {
| Rollup { name, .. }
| Select { name, .. }
| Status { name, .. }
| Button { name, .. }
| Title { name, .. }
| Url { name, .. } => Some(name.to_owned()),
@ -1113,9 +1065,6 @@ pub enum Property {
UniqueId {
id: String,
},
Button {
id: String,
},
Unsupported(Value),
}
@ -1146,7 +1095,6 @@ impl Property {
| Url { id, .. }
| Formula { id, .. }
| Verification { id, .. }
| Button { id, .. }
| UniqueId { id, .. } => Some(id.to_owned()),
Unsupported(_) => None,
@ -1221,10 +1169,20 @@ where
(
key.to_owned(),
serde_json::from_value::<Property>(value.to_owned()).unwrap_or_else(|error| {
log::warn!(
"Could not parse value because of error, defaulting to Property::Unsupported:\n= ERROR:\n{error:#?}\n= JSON:\n{}\n---",
serde_json::to_string_pretty(&value).unwrap()
);
println!("GOT HERE????");
match value.get("type") {
Some(Value::String(kind)) => match kind.as_ref() {
"multi_select" => println!("found multi_select!"),
_ => {}
},
Some(_) | None => {
log::warn!(
"Could not parse value because of error, defaulting to Property::Unsupported:\n= ERROR:\n{error:#?}\n= JSON:\n{}\n---",
serde_json::to_string_pretty(&value).unwrap()
);
}
}
Property::Unsupported(value.to_owned())
}),
)
@ -1483,3 +1441,114 @@ pub enum DatabaseFormulaType {
Number,
String,
}
#[cfg(test)]
mod tests {
use super::*;
fn get_key() -> String {
let _ = dotenv::dotenv();
std::env::var("NOTION_API_KEY").unwrap()
}
#[tokio::test]
async fn check_database_query() {
let databases = Client::new()
.api_key(&get_key())
.build()
.search::<Database>(SearchOptions {
filter: Some(json!(
{
"value": "database",
"property": "object"
}
)),
query: None,
page_size: None,
sort: None,
start_cursor: None,
})
.await;
println!("{databases:#?}");
}
#[tokio::test]
async fn test_blocks() {
let blocks = Client::new()
.api_key(&get_key())
.build()
.blocks
.children()
.list(BlockChildrenListOptions {
block_id: "0d253ab0f751443aafb9bcec14012897",
})
.await;
println!("{blocks:#?}")
}
#[test]
fn parse_json_1() {
let data = r#"
{
"id": "FzYl",
"name": "Currently with",
"select": {
"options": [
{
"color": "default",
"description": null,
"id": "nT[N",
"name": "Someone else"
},
{
"color": "red",
"description": null,
"id": "fgu_",
"name": "Me"
}
]
},
"type": "select"
}"#;
serde_json::from_str::<DatabaseProperty>(data).unwrap();
}
// TODO: write test using https://stackoverflow.com/a/75979520
#[test]
fn parse_json_multi_select_failed_because_of_map_instead_of_sequence() {
let data = r#"
{
"id": "pvSs",
"multi_select": {
"options": [
{
"color": "orange",
"description": null,
"id": "|FMC",
"name": "Loan"
},
{
"color": "green",
"description": null,
"id": "\\Iu\\",
"name": "Checking "
},
{
"color": "blue",
"description": null,
"id": "q]rZ",
"name": "Credit"
}
]
},
"name": "Type",
"type": "multi_select"
}
"#;
serde_json::from_str::<Property>(data).unwrap();
}
}

464
unit-testable-json.txt Normal file
View file

@ -0,0 +1,464 @@
{
"id": "FzYl",
"name": "Currently with",
"select": {
"options": [
{
"color": "default",
"description": null,
"id": "nT[N",
"name": "Someone else"
},
{
"color": "red",
"description": null,
"id": "fgu_",
"name": "Me"
}
]
},
"type": "select"
}
---
{
"files": null,
"id": "KTSO",
"name": "Files & media",
"type": "files"
}
---
{
"id": "CEPX",
"name": "Focus",
"select": {
"options": [
{
"color": "orange",
"description": null,
"id": "J^N_",
"name": "Web Dev"
},
{
"color": "purple",
"description": null,
"id": "}xYt",
"name": "Work"
},
{
"color": "blue",
"description": null,
"id": ";<tP",
"name": "University"
},
{
"color": "green",
"description": null,
"id": "x=t?",
"name": "Home"
}
]
},
"type": "select"
}
---
{
"id": "XwUK",
"name": "Remarks",
"rich_text": null,
"type": "rich_text"
}
---
{
"id": "%3BH%60D",
"multi_select": {
"options": [
{
"color": "default",
"description": null,
"id": "02648794-ae08-408a-8ecf-0775bf71f505",
"name": "UK Move"
},
{
"color": "default",
"description": null,
"id": "@Mb:",
"name": "Generic Tasks"
},
{
"color": "default",
"description": null,
"id": "xNf;",
"name": "Finance"
},
{
"color": "default",
"description": null,
"id": "gGrj",
"name": "Management"
},
{
"color": "default",
"description": null,
"id": "uiTJ",
"name": "Gym"
},
{
"color": "default",
"description": null,
"id": "=Dwo",
"name": "Career Development"
},
{
"color": "default",
"description": null,
"id": "cVoq",
"name": "Primary Role"
},
{
"color": "default",
"description": null,
"id": "\\;{[",
"name": "Personal Goals"
}
]
},
"name": "Sub-Focus",
"type": "multi_select"
}
---
{
"id": "SPW%3D",
"name": "Fee",
"number": {
"format": "dollar"
},
"type": "number"
}
---
{
"id": "%40o%60J",
"multi_select": {
"options": [
{
"color": "green",
"description": null,
"id": ">u]:",
"name": "UK"
},
{
"color": "yellow",
"description": null,
"id": "d{Rl",
"name": "US"
}
]
},
"name": "Location",
"type": "multi_select"
}
---
{
"id": "%7CMD%3B",
"multi_select": {
"options": [
{
"color": "pink",
"description": null,
"id": "gks?",
"name": "Yes"
},
{
"color": "default",
"description": null,
"id": "moPz",
"name": "No"
}
]
},
"name": "Subscriptions?",
"type": "multi_select"
}
---
{
"id": "pvSs",
"multi_select": {
"options": [
{
"color": "orange",
"description": null,
"id": "|FMC",
"name": "Loan"
},
{
"color": "green",
"description": null,
"id": "\\Iu\\",
"name": "Checking "
},
{
"color": "blue",
"description": null,
"id": "q]rZ",
"name": "Credit"
}
]
},
"name": "Type",
"type": "multi_select"
}
---
{
"id": "yDni",
"multi_select": {
"options": [
{
"color": "blue",
"description": null,
"id": "a8225f34-713e-48da-92d6-f372a814f048",
"name": "Onboarding"
},
{
"color": "green",
"description": null,
"id": "7f2c6dcf-6c6a-4b9d-8e11-d2966b670bf5",
"name": "Design"
}
]
},
"name": "Tags",
"type": "multi_select"
}
---
{
"id": "H%7D%3BY",
"last_edited_time": null,
"name": "上次编辑时间",
"type": "last_edited_time"
}
---
{
"created_time": null,
"id": "notion%3A%2F%2Fmeetings%2Fcreated_time_property",
"name": "Created time",
"type": "created_time"
}
---
{
"id": "notion%3A%2F%2Fmeetings%2Flast_edited_time_property",
"last_edited_time": null,
"name": "Last edited time",
"type": "last_edited_time"
}
---
{
"created_time": null,
"id": "notion%3A%2F%2Fmeetings%2Fcreated_time_property",
"name": "Created time",
"type": "created_time"
}
---
{
"id": "notion%3A%2F%2Fmeetings%2Flast_edited_time_property",
"last_edited_time": null,
"name": "Last edited time",
"type": "last_edited_time"
}
---
{
"id": "notion%3A%2F%2Fmeetings%2Fmeeting_type_property",
"name": "Type",
"select": {
"options": [
{
"color": "yellow",
"description": null,
"id": "standup",
"name": "Standup"
},
{
"color": "blue",
"description": null,
"id": "brainstorm",
"name": "Brainstorm"
},
{
"color": "green",
"description": null,
"id": "team_weekly",
"name": "Team weekly"
},
{
"color": "pink",
"description": null,
"id": "training",
"name": "Training"
}
]
},
"type": "select"
}
---
{
"id": "notion%3A%2F%2Ftasks%2Fpriority_property",
"name": "Priority",
"select": {
"options": [
{
"color": "green",
"description": null,
"id": "priority_low",
"name": "Low"
},
{
"color": "yellow",
"description": null,
"id": "priority_medium",
"name": "Medium"
},
{
"color": "red",
"description": null,
"id": "priority_high",
"name": "High"
}
]
},
"type": "select"
}
---
{
"id": "notion%3A%2F%2Ftasks%2Ftags_property",
"multi_select": {
"options": [
{
"color": "purple",
"description": null,
"id": "Mobile",
"name": "Mobile"
},
{
"color": "blue",
"description": null,
"id": "Website",
"name": "Website"
},
{
"color": "pink",
"description": null,
"id": "Improvement",
"name": "Improvement"
}
]
},
"name": "Tags",
"type": "multi_select"
}
---
{
"id": "rWCq",
"name": "Address",
"rich_text": null,
"type": "rich_text"
}
---
{
"id": "jhSN",
"name": "Category",
"select": {
"options": [
{
"color": "pink",
"description": null,
"id": "f7672afb-be22-44e8-b01e-76b3edaaa0b2",
"name": "Healing (Social & Emotional Wellbeing) ✨"
},
{
"color": "pink",
"description": null,
"id": "yyXf",
"name": "Personal"
},
{
"color": "pink",
"description": null,
"id": "fee9b2a6-4bb0-4064-b328-23cbf9b7fbc9",
"name": "Medication"
},
{
"color": "pink",
"description": null,
"id": "4cb37ac5-8182-4948-b4a9-a43c3f7b44bd",
"name": "Physical Healing ✨"
},
{
"color": "purple",
"description": null,
"id": "kAOz",
"name": "Work"
},
{
"color": "purple",
"description": null,
"id": "BL[V",
"name": "Home"
},
{
"color": "purple",
"description": null,
"id": "48ad8f71-98f5-463c-a057-cb72ce1c47f1",
"name": "Car"
},
{
"color": "blue",
"description": null,
"id": "<r:U",
"name": "Malachi "
},
{
"color": "blue",
"description": null,
"id": "lQ~P",
"name": "Isaiah"
},
{
"color": "green",
"description": null,
"id": "?Mk{",
"name": "Business"
},
{
"color": "red",
"description": null,
"id": "172a8fff-cae3-477f-8dc0-22837e56936c",
"name": "Meeting"
},
{
"color": "red",
"description": null,
"id": "f27efcba-7687-4494-9438-21c6f11fbfb5",
"name": "DCJ"
},
{
"color": "default",
"description": null,
"id": "0a6ea6ea-1b93-415e-a8bb-d555afbd41a2",
"name": "Yuck"
},
{
"color": "green",
"description": null,
"id": "6c949785-1b8c-4a27-97b5-ebcf0462209f",
"name": "Healing & Growth (Financial) ✨"
}
]
},
"type": "select"
}
---
{
"files": null,
"id": "svj%5C",
"name": "Files & media",
"type": "files"
}
---