lava_api/
tag.rs

1//! Retrieve tags
2
3use serde::Deserialize;
4
5/// Metadata for a tag on the LAVA server
6#[derive(Clone, Deserialize, Debug, PartialEq, Eq)]
7pub struct Tag {
8    /// The unique id of the tag
9    pub id: u32,
10    /// The name of the tag
11    pub name: String,
12    /// An optional description for this tag
13    pub description: Option<String>,
14}
15
16#[cfg(test)]
17mod tests {
18    use super::Tag;
19    use crate::Lava;
20
21    use boulder::{Buildable, Builder};
22    use lava_api_mock::{
23        LavaMock, PaginationLimits, PopulationParams, SharedState, State, Tag as MockTag,
24    };
25    use persian_rug::{Accessor, Context};
26    use std::collections::BTreeMap;
27    use test_log::test;
28
29    impl Tag {
30        pub fn from_mock<'b, B, C>(tag: &MockTag<C>, _context: B) -> Tag
31        where
32            B: 'b + Accessor<Context = C>,
33            C: Context + 'static,
34        {
35            Self {
36                id: tag.id,
37                name: tag.name.clone(),
38                description: tag.description.clone(),
39            }
40        }
41    }
42
43    /// Stream 49 tags with a page limit of 5 from the server
44    #[test(tokio::test)]
45    async fn test_basic() {
46        let state = SharedState::new_populated(PopulationParams::builder().tags(49usize).build());
47        let server = LavaMock::new(
48            state.clone(),
49            PaginationLimits::builder().workers(Some(5)).build(),
50        )
51        .await;
52
53        let mut map = BTreeMap::new();
54        let start = state.access();
55        for t in start.get_iter::<MockTag<State>>() {
56            map.insert(t.id, t.clone());
57        }
58
59        let lava = Lava::new(&server.uri(), None).expect("failed to make lava server");
60
61        let tags = lava.tags().await.expect("failed to get tags");
62
63        let mut seen = BTreeMap::new();
64        for tag in tags {
65            assert!(!seen.contains_key(&tag.id));
66            assert!(map.contains_key(&tag.id));
67            let tk = map.get(&tag.id).unwrap();
68            assert_eq!(tag.id, tk.id);
69            assert_eq!(tag.name, tk.name);
70            assert_eq!(tag.description, tk.description);
71
72            seen.insert(tag.id, tag.clone());
73        }
74        assert_eq!(seen.len(), 49);
75    }
76}