Debian Codegraph @

third_party/rust/glean-core/tests/boolean.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 // SKIPPED from glean-ac: string deserializer should correctly parse integers
15 // This test doesn't really apply to rkv
16
17 #[test]
18 fn boolean_serializer_should_correctly_serialize_boolean() {
19 let (mut tempdir, _) = tempdir();
20
21 {
22 // We give tempdir to the `new_glean` function...
23 let (glean, dir) = new_glean(Some(tempdir));
24 // And then we get it back once that function returns.
25 tempdir = dir;
26
27 let metric = BooleanMetric::new(CommonMetricData {
28 name: "boolean_metric".into(),
29 category: "telemetry".into(),
30 send_in_pings: vec!["store1".into()],
31 disabled: false,
32 lifetime: Lifetime::User,
33 ..Default::default()
34 });
35
36 metric.set_sync(&glean, true);
37
38 let snapshot = StorageManager
39 .snapshot_as_json(glean.storage(), "store1", true)
40 .unwrap();
41 assert_eq!(
42 json!({"boolean": {"telemetry.boolean_metric": true}}),
43 snapshot
44 );
45 }
46
47 // Make a new Glean instance here, which should force reloading of the data from disk
48 // so we can ensure it persisted, because it has User lifetime
49 {
50 let (glean, _t) = new_glean(Some(tempdir));
51 let snapshot = StorageManager
52 .snapshot_as_json(glean.storage(), "store1", true)
53 .unwrap();
54 assert_eq!(
55 json!({"boolean": {"telemetry.boolean_metric": true}}),
56 snapshot
57 );
58 }
59 }
60
61 #[test]
62 fn set_properly_sets_the_value_in_all_stores() {
63 let (glean, _t) = new_glean(None);
64 let store_names: Vec<String> = vec!["store1".into(), "store2".into()];
65
66 let metric = BooleanMetric::new(CommonMetricData {
67 name: "boolean_metric".into(),
68 category: "telemetry".into(),
69 send_in_pings: store_names.clone(),
70 disabled: false,
71 lifetime: Lifetime::Ping,
72 ..Default::default()
73 });
74
75 metric.set_sync(&glean, true);
76
77 // Check that the data was correctly set in each store.
78 for store_name in store_names {
79 let snapshot = StorageManager
80 .snapshot_as_json(glean.storage(), &store_name, true)
81 .unwrap();
82
83 assert_eq!(
84 json!({"boolean": {"telemetry.boolean_metric": true}}),
85 snapshot
86 );
87 }
88 }
89
90 // SKIPPED from glean-ac: booleans are serialized in the correct JSON format
91 // Completely redundant with other tests.