oparl_types/
organization_type.rs

1// SPDX-FileCopyrightText: Politik im Blick developers
2// SPDX-FileCopyrightText: Wolfgang Silbermayr <wolfgang@silbermayr.at>
3//
4// SPDX-License-Identifier: AGPL-3.0-or-later OR EUPL-1.2
5
6#[derive(
7    Debug,
8    Clone,
9    PartialEq,
10    Eq,
11    PartialOrd,
12    Ord,
13    Hash,
14    serde::Serialize,
15    serde::Deserialize,
16    strum::AsRefStr,
17    strum::Display,
18    strum::EnumString,
19)]
20#[cfg_attr(feature = "sea-orm", derive(sea_orm::DeriveValueType))]
21#[cfg_attr(feature = "sea-orm", sea_orm(value_type = "String"))]
22pub enum OrganizationType {
23    #[serde(rename = "Gremium")]
24    #[strum(serialize = "Gremium")]
25    Council,
26
27    #[serde(rename = "Partei")]
28    #[strum(serialize = "Partei")]
29    Party,
30
31    #[serde(rename = "Fraktion")]
32    #[strum(serialize = "Fraktion")]
33    ParliamentaryGroup,
34
35    #[serde(rename = "Verwaltungsbereich")]
36    #[strum(serialize = "Verwaltungsbereich")]
37    AdministrativeAmbit,
38
39    #[serde(rename = "externes Gremium")]
40    #[strum(serialize = "externes Gremium")]
41    ExternalCouncil,
42
43    #[serde(rename = "Institution")]
44    #[strum(serialize = "Institution")]
45    Institution,
46
47    #[serde(rename = "Sonstiges")]
48    #[strum(serialize = "Sonstiges")]
49    Other,
50}
51
52#[cfg(test)]
53mod tests {
54    use super::OrganizationType;
55
56    #[test]
57    fn to_string() {
58        assert_eq!(OrganizationType::Council.to_string(), "Gremium");
59        assert_eq!(OrganizationType::Party.to_string(), "Partei");
60        assert_eq!(OrganizationType::ParliamentaryGroup.to_string(), "Fraktion");
61        assert_eq!(
62            OrganizationType::AdministrativeAmbit.to_string(),
63            "Verwaltungsbereich"
64        );
65        assert_eq!(
66            OrganizationType::ExternalCouncil.to_string(),
67            "externes Gremium"
68        );
69        assert_eq!(OrganizationType::Institution.to_string(), "Institution");
70        assert_eq!(OrganizationType::Other.to_string(), "Sonstiges");
71    }
72
73    #[test]
74    fn parse_good() {
75        let parsed: OrganizationType = "Gremium"
76            .parse()
77            .expect("value must be parsable as organization type");
78        assert_eq!(parsed, OrganizationType::Council);
79
80        let parsed: OrganizationType = "Partei"
81            .parse()
82            .expect("value must be parsable as organization type");
83        assert_eq!(parsed, OrganizationType::Party);
84
85        let parsed: OrganizationType = "Fraktion"
86            .parse()
87            .expect("value must be parsable as organization type");
88        assert_eq!(parsed, OrganizationType::ParliamentaryGroup);
89
90        let parsed: OrganizationType = "Institution"
91            .parse()
92            .expect("value must be parsable as organization type");
93        assert_eq!(parsed, OrganizationType::Institution);
94
95        let parsed: OrganizationType = "externes Gremium"
96            .parse()
97            .expect("value must be parsable as organization type");
98        assert_eq!(parsed, OrganizationType::ExternalCouncil);
99
100        let parsed: OrganizationType = "Sonstiges"
101            .parse()
102            .expect("value must be parsable as organization type");
103        assert_eq!(parsed, OrganizationType::Other);
104    }
105
106    #[test]
107    fn deserialize_bad() {
108        assert!("gremium".parse::<OrganizationType>().is_err());
109        assert!("GREMIUM".parse::<OrganizationType>().is_err());
110    }
111}
112
113#[cfg(test)]
114mod serde_tests {
115    use super::OrganizationType;
116    use pretty_assertions::assert_eq;
117    use serde_json::json;
118
119    #[test]
120    fn serialize() {
121        assert_eq!(json!(OrganizationType::Council), json!("Gremium"));
122        assert_eq!(json!(OrganizationType::Party), json!("Partei"));
123        assert_eq!(
124            json!(OrganizationType::ParliamentaryGroup),
125            json!("Fraktion")
126        );
127        assert_eq!(
128            json!(OrganizationType::AdministrativeAmbit),
129            json!("Verwaltungsbereich")
130        );
131        assert_eq!(
132            json!(OrganizationType::ExternalCouncil),
133            json!("externes Gremium")
134        );
135        assert_eq!(json!(OrganizationType::Institution), json!("Institution"));
136        assert_eq!(json!(OrganizationType::Other), json!("Sonstiges"));
137    }
138
139    #[test]
140    fn deserialize_good() {
141        let deserialized: OrganizationType = serde_json::from_value(json!("Gremium"))
142            .expect("value must be deserializable as organization type");
143        assert_eq!(deserialized, OrganizationType::Council);
144
145        let deserialized: OrganizationType = serde_json::from_value(json!("Partei"))
146            .expect("value must be deserializable as organization type");
147        assert_eq!(deserialized, OrganizationType::Party);
148
149        let deserialized: OrganizationType = serde_json::from_value(json!("Fraktion"))
150            .expect("value must be deserializable as organization type");
151        assert_eq!(deserialized, OrganizationType::ParliamentaryGroup);
152
153        let deserialized: OrganizationType = serde_json::from_value(json!("Institution"))
154            .expect("value must be deserializable as organization type");
155        assert_eq!(deserialized, OrganizationType::Institution);
156
157        let deserialized: OrganizationType = serde_json::from_value(json!("externes Gremium"))
158            .expect("value must be deserializable as organization type");
159        assert_eq!(deserialized, OrganizationType::ExternalCouncil);
160
161        let deserialized: OrganizationType = serde_json::from_value(json!("Sonstiges"))
162            .expect("value must be deserializable as organization type");
163        assert_eq!(deserialized, OrganizationType::Other);
164    }
165
166    #[test]
167    fn deserialize_bad() {
168        assert!(serde_json::from_value::<OrganizationType>(json!("gremium")).is_err());
169        assert!(serde_json::from_value::<OrganizationType>(json!("GREMIUM")).is_err());
170        assert!(serde_json::from_value::<OrganizationType>(json!({})).is_err());
171        assert!(serde_json::from_value::<OrganizationType>(json!([])).is_err());
172        assert!(serde_json::from_value::<OrganizationType>(json!(null)).is_err());
173    }
174}