glean/glean-core/tests/datetime.rs

188 строки
5.8 KiB
Rust
Исходник Обычный вид История

2019-05-28 12:42:46 +03:00
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
mod common;
use crate::common::*;
use chrono::prelude::*;
use serde_json::json;
use glean_core::metrics::*;
use glean_core::storage::StorageManager;
use glean_core::{CommonMetricData, Lifetime};
2019-05-28 12:42:46 +03:00
// SKIPPED from glean-ac: datetime deserializer should correctly parse integers
2019-05-28 12:42:46 +03:00
// This test doesn't really apply to rkv
#[test]
fn datetime_serializer_should_correctly_serialize_datetime() {
let expected_value = "1983-04-13T12:09+00:00";
let (mut tempdir, _) = tempdir();
2019-05-28 12:42:46 +03:00
{
// We give tempdir to the `new_glean` function...
let (glean, dir) = new_glean(Some(tempdir));
// And then we get it back once that function returns.
tempdir = dir;
2019-05-28 12:42:46 +03:00
2019-05-30 17:27:31 +03:00
let metric = DatetimeMetric::new(
CommonMetricData {
name: "datetime_metric".into(),
category: "telemetry".into(),
send_in_pings: vec!["store1".into()],
disabled: false,
lifetime: Lifetime::User,
..Default::default()
2019-05-30 17:27:31 +03:00
},
TimeUnit::Minute,
);
2019-05-28 12:42:46 +03:00
// `1983-04-13T12:09:14.274+00:00` will be truncated to Minute resolution.
2019-05-30 17:27:31 +03:00
let dt = FixedOffset::east(0)
.ymd(1983, 4, 13)
.and_hms_milli(12, 9, 14, 274);
2019-06-06 16:29:47 +03:00
metric.set(&glean, Some(dt));
2019-05-28 12:42:46 +03:00
let snapshot = StorageManager
.snapshot_as_json(glean.storage(), "store1", true)
.unwrap();
assert_eq!(
json!({"datetime": {"telemetry.datetime_metric": expected_value}}),
2019-05-28 12:42:46 +03:00
snapshot
);
}
// Make a new Glean instance here, which should force reloading of the data from disk
2019-05-28 12:42:46 +03:00
// so we can ensure it persisted, because it has User lifetime
{
let (glean, _) = new_glean(Some(tempdir));
2019-05-28 12:42:46 +03:00
let snapshot = StorageManager
.snapshot_as_json(glean.storage(), "store1", true)
.unwrap();
assert_eq!(
json!({"datetime": {"telemetry.datetime_metric": expected_value}}),
2019-05-28 12:42:46 +03:00
snapshot
);
}
}
#[test]
fn set_value_properly_sets_the_value_in_all_stores() {
let (glean, _t) = new_glean(None);
let store_names: Vec<String> = vec!["store1".into(), "store2".into()];
2019-05-30 17:27:31 +03:00
let metric = DatetimeMetric::new(
CommonMetricData {
name: "datetime_metric".into(),
category: "telemetry".into(),
send_in_pings: store_names.clone(),
disabled: false,
lifetime: Lifetime::Ping,
..Default::default()
2019-05-30 17:27:31 +03:00
},
TimeUnit::Nanosecond,
);
// `1983-04-13T12:09:14.274+00:00` will be truncated to Minute resolution.
2019-05-30 17:27:31 +03:00
let dt = FixedOffset::east(0)
.ymd(1983, 4, 13)
2019-05-30 18:50:35 +03:00
.and_hms_nano(12, 9, 14, 1_560_274);
2019-06-06 16:29:47 +03:00
metric.set(&glean, Some(dt));
for store_name in store_names {
assert_eq!(
"1983-04-13T12:09:14.001560274+00:00",
2019-05-30 17:27:31 +03:00
metric
.test_get_value_as_string(&glean, &store_name)
.unwrap()
);
}
}
// SKIPPED from glean-ac: getSnapshot() returns null if nothing is recorded in the store
// This test doesn't really apply to rkv
// SKIPPED from glean-ac: getSnapshot() correctly clears the stores
// This test doesn't really apply to rkv
#[test]
fn test_that_truncation_works() {
let (glean, _t) = new_glean(None);
// `1985-07-03T12:09:14.000560274+01:00`
2019-05-30 17:27:31 +03:00
let high_res_datetime = FixedOffset::east(3600)
.ymd(1985, 7, 3)
2019-05-30 18:50:35 +03:00
.and_hms_nano(12, 9, 14, 1_560_274);
let store_name = "store1";
// Create an helper struct for defining the truncation cases.
struct TestCase {
2019-06-03 16:45:19 +03:00
case_name: &'static str,
desired_resolution: TimeUnit,
2019-06-03 16:45:19 +03:00
expected_result: &'static str,
}
// Define the single test cases.
let test_cases = vec![
TestCase {
2019-06-03 16:45:19 +03:00
case_name: "nano",
desired_resolution: TimeUnit::Nanosecond,
2019-06-03 16:45:19 +03:00
expected_result: "1985-07-03T12:09:14.001560274+01:00",
},
TestCase {
2019-06-03 16:45:19 +03:00
case_name: "micro",
desired_resolution: TimeUnit::Microsecond,
2019-06-03 16:45:19 +03:00
expected_result: "1985-07-03T12:09:14.001560+01:00",
},
TestCase {
2019-06-03 16:45:19 +03:00
case_name: "milli",
desired_resolution: TimeUnit::Millisecond,
2019-06-03 16:45:19 +03:00
expected_result: "1985-07-03T12:09:14.001+01:00",
},
TestCase {
2019-06-03 16:45:19 +03:00
case_name: "second",
desired_resolution: TimeUnit::Second,
2019-06-03 16:45:19 +03:00
expected_result: "1985-07-03T12:09:14+01:00",
},
TestCase {
2019-06-03 16:45:19 +03:00
case_name: "minute",
desired_resolution: TimeUnit::Minute,
2019-06-03 16:45:19 +03:00
expected_result: "1985-07-03T12:09+01:00",
},
TestCase {
2019-06-03 16:45:19 +03:00
case_name: "hour",
desired_resolution: TimeUnit::Hour,
2019-06-03 16:45:19 +03:00
expected_result: "1985-07-03T12+01:00",
},
TestCase {
2019-06-03 16:45:19 +03:00
case_name: "day",
desired_resolution: TimeUnit::Day,
2019-06-03 16:45:19 +03:00
expected_result: "1985-07-03+01:00",
},
];
// Execute them all.
for t in test_cases {
2019-05-30 17:27:31 +03:00
let metric = DatetimeMetric::new(
CommonMetricData {
2019-05-30 18:50:35 +03:00
name: format!("datetime_metric_{}", t.case_name),
2019-05-30 17:27:31 +03:00
category: "telemetry".into(),
send_in_pings: vec![store_name.into()],
disabled: false,
lifetime: Lifetime::User,
..Default::default()
2019-05-30 17:27:31 +03:00
},
t.desired_resolution,
);
2019-06-06 16:29:47 +03:00
metric.set(&glean, Some(high_res_datetime));
assert_eq!(
t.expected_result,
2019-05-30 17:27:31 +03:00
metric
.test_get_value_as_string(&glean, &store_name)
.unwrap()
);
}
}