notionrs_schema/object/database/
select.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Deserialize, Serialize, Debug, Default, Clone, PartialEq, Eq, notionrs_macro::Setter)]
4pub struct DatabaseSelectProperty {
5    /// Property Identifier
6    #[serde(skip_serializing)]
7    pub id: Option<String>,
8
9    /// Modify the value of this field when updating the column name of the property.
10    #[serde(skip_serializing)]
11    pub name: String,
12
13    /// Although it is not explicitly stated in the official documentation,
14    /// you can add a description to the property by specifying this.
15    #[serde(skip_serializing)]
16    pub description: Option<String>,
17
18    pub select: DatabaseSelectOptionProperty,
19}
20
21#[derive(Deserialize, Serialize, Debug, Default, Clone, PartialEq, Eq, notionrs_macro::Setter)]
22pub struct DatabaseSelectOptionProperty {
23    options: Vec<crate::object::select::Select>,
24}
25
26impl DatabaseSelectProperty {
27    pub fn options(mut self, options: Vec<crate::object::select::Select>) -> Self {
28        self.select.options = options;
29        self
30    }
31}
32
33// # --------------------------------------------------------------------------------
34//
35// unit test
36//
37// # --------------------------------------------------------------------------------
38#[cfg(test)]
39mod unit_tests {
40
41    use super::*;
42
43    #[test]
44    fn deserialize_database_select_property() {
45        let json_data = r#"
46        {
47            "id": "%40Q%5BM",
48            "name": "Food group",
49            "type": "select",
50            "select": {
51                "options": [
52                {
53                    "id": "e28f74fc-83a7-4469-8435-27eb18f9f9de",
54                    "name": "🥦Vegetable",
55                    "color": "purple"
56                },
57                {
58                    "id": "6132d771-b283-4cd9-ba44-b1ed30477c7f",
59                    "name": "🍎Fruit",
60                    "color": "red"
61                },
62                {
63                    "id": "fc9ea861-820b-4f2b-bc32-44ed9eca873c",
64                    "name": "💪Protein",
65                    "color": "yellow"
66                }
67                ]
68            }
69        }
70        "#;
71
72        let select = serde_json::from_str::<DatabaseSelectProperty>(json_data).unwrap();
73
74        assert_eq!(select.id, Some("%40Q%5BM".to_string()));
75        assert_eq!(select.name, "Food group");
76
77        let options = &select.select.options;
78        assert_eq!(options.len(), 3);
79
80        assert_eq!(
81            options[0].id,
82            Some("e28f74fc-83a7-4469-8435-27eb18f9f9de".to_string())
83        );
84        assert_eq!(options[0].name, "🥦Vegetable");
85        assert_eq!(
86            options[0].color,
87            Some(crate::object::select::SelectColor::Purple)
88        );
89
90        assert_eq!(
91            options[1].id,
92            Some("6132d771-b283-4cd9-ba44-b1ed30477c7f".to_string())
93        );
94        assert_eq!(options[1].name, "🍎Fruit");
95        assert_eq!(
96            options[1].color,
97            Some(crate::object::select::SelectColor::Red)
98        );
99
100        assert_eq!(
101            options[2].id,
102            Some("fc9ea861-820b-4f2b-bc32-44ed9eca873c".to_string())
103        );
104        assert_eq!(options[2].name, "💪Protein");
105        assert_eq!(
106            options[2].color,
107            Some(crate::object::select::SelectColor::Yellow)
108        );
109    }
110}