1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
pub mod client;
pub mod models;
pub mod traits;

#[cfg(test)]
mod tests {

    use super::client::{Client, InsertOptions};
    use super::models::Point;
    use mockito::Matcher;

    use super::traits::PointSerialize;
    use influxdb_derives::PointSerialize;

    #[test]
    fn test_derive_write() {
        #[derive(PointSerialize)]
        #[point(measurement = "test")]
        struct Test {
            #[point(tag = "notTicker")]
            ticker: String,
            #[point(tag = "notTicker2")]
            ticker2: String,
            #[point(field = "notPrice")]
            price: f32,
            #[point(field)]
            price2: String,
            #[point(timestamp)]
            data: String,
        }

        let result = Test {
            ticker: "GME".to_string(),
            ticker2: "!GME".to_string(),
            price: 0.32,
            price2: "Hello world".to_string(),
            data: "321321321".to_string(),
        }
        .serialize();
        println!("Wow, very serialized: {}", result);
        assert_eq!(
            "test,notTicker=GME,notTicker2=!GME notPrice=0.32,price2=\"Hello world\"".to_string(),
            result
        );
    }

    #[test]
    fn test_client_write() {
        let api_key = "TEST_API_KEY";

        let mock = mockito::mock("POST", "/api/v2/write")
            .with_status(201)
            .match_header("content-type", "text/plain")
            .match_query(Matcher::AllOf(vec![
                Matcher::UrlEncoded("bucket".into(), "tradely".into()),
                Matcher::UrlEncoded("orgID".into(), "168f31904923e853".into()),
            ]))
            .match_body("test,ticker=GME price=420.69 1613925577")
            .expect(1)
            .create();

        let client = Client::new(mockito::server_url(), String::from(api_key))
            .with_bucket("tradely")
            .with_org_id("168f31904923e853");

        let point = Point::new("test")
            .tag("ticker", "GME")
            .field("price", 420.69)
            .timestamp(1613925577);

        let points: Vec<Point> = vec![point];
        let result =
            tokio_test::block_on(client.insert_points(points, InsertOptions::WithTimestamp(None)));

        assert!(result.is_ok());

        mock.assert();
    }
}