Debian Codegraph @

third_party/rust/glean-core/tests/object.rs

raw
1 // This Source Code Form is subject to the terms of the Mozilla Public
2 // License, v. 2.0. If a copy of the MPL was not distributed with this
3 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5 mod common;
6 use crate::common::*;
7
8 use serde_json::json;
9
10 use glean_core::metrics::*;
11 use glean_core::storage::StorageManager;
12 use glean_core::{CommonMetricData, Lifetime};
13
14 #[test]
15 fn object_serializer_should_correctly_serialize_objects() {
16 let (mut tempdir, _) = tempdir();
17
18 {
19 // We give tempdir to the `new_glean` function...
20 let (glean, dir) = new_glean(Some(tempdir));
21 // And then we get it back once that function returns.
22 tempdir = dir;
23
24 let metric = ObjectMetric::new(CommonMetricData {
25 name: "object_metric".into(),
26 category: "telemetry".into(),
27 send_in_pings: vec!["store1".into()],
28 disabled: false,
29 lifetime: Lifetime::User,
30 ..Default::default()
31 });
32
33 let obj = serde_json::from_str("{ \"value\": 1 }").unwrap();
34 metric.set_sync(&glean, obj);
35
36 let snapshot = StorageManager
37 .snapshot_as_json(glean.storage(), "store1", true)
38 .unwrap();
39 assert_eq!(
40 json!({"object": {"telemetry.object_metric": { "value": 1 }}}),
41 snapshot
42 );
43 }
44
45 // Make a new Glean instance here, which should force reloading of the data from disk
46 // so we can ensure it persisted, because it has User lifetime
47 {
48 let (glean, _t) = new_glean(Some(tempdir));
49 let snapshot = StorageManager
50 .snapshot_as_json(glean.storage(), "store1", true)
51 .unwrap();
52 assert_eq!(
53 json!({"object": {"telemetry.object_metric": { "value": 1 }}}),
54 snapshot
55 );
56 }
57 }
58
59 #[test]
60 fn set_value_properly_sets_the_value_in_all_stores() {
61 let (glean, _t) = new_glean(None);
62 let store_names: Vec<String> = vec!["store1".into(), "store2".into()];
63
64 let metric = ObjectMetric::new(CommonMetricData {
65 name: "object_metric".into(),
66 category: "telemetry".into(),
67 send_in_pings: store_names.clone(),
68 disabled: false,
69 lifetime: Lifetime::Ping,
70 ..Default::default()
71 });
72
73 let obj = serde_json::from_str("{ \"value\": 1 }").unwrap();
74 metric.set_sync(&glean, obj);
75
76 for store_name in store_names {
77 let snapshot = StorageManager
78 .snapshot_as_json(glean.storage(), &store_name, true)
79 .unwrap();
80
81 assert_eq!(
82 json!({"object": {"telemetry.object_metric": { "value": 1 }}}),
83 snapshot
84 );
85 }
86 }
87
88 #[test]
89 fn getting_data_json_encoded() {
90 let (glean, _t) = new_glean(None);
91
92 let object: ObjectMetric = ObjectMetric::new(CommonMetricData {
93 name: "transformation".into(),
94 category: "local".into(),
95 send_in_pings: vec!["store1".into()],
96 ..Default::default()
97 });
98
99 let obj_str = "{\"value\":1}";
100 let obj = serde_json::from_str(obj_str).unwrap();
101 object.set_sync(&glean, obj);
102
103 assert_eq!(obj_str, object.get_value(&glean, Some("store1")).unwrap());
104 }