Skip to content

Commit

Permalink
Fix copy-pasta in oximeter-db internal datum type representation (#1574)
Browse files Browse the repository at this point in the history
- Fixes copy-paste error when converting between `oximeter`'s
  `DatumType` and the type used to represent it in the database,
  `oximeter_db::model::DbDatumType`.
- Adds a slew of sanity checks verifying these mechanical conversions to
  prevent regressions
  • Loading branch information
bnaecker authored Aug 10, 2022
1 parent aff0d7a commit 000333b
Showing 1 changed file with 67 additions and 5 deletions.
72 changes: 67 additions & 5 deletions oximeter/db/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl From<DbBool> for Datum {
//
// Note that the fields are renamed so that ClickHouse interprets them as correctly referring to
// the nested column names.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub(crate) struct DbFieldList {
#[serde(rename = "fields.name")]
pub names: Vec<String>,
Expand Down Expand Up @@ -138,7 +138,7 @@ impl From<TimeseriesSchema> for DbTimeseriesSchema {
}
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
pub enum DbFieldType {
String,
I64,
Expand Down Expand Up @@ -169,7 +169,7 @@ impl From<FieldType> for DbFieldType {
}
}
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
pub enum DbDatumType {
Bool,
I64,
Expand All @@ -191,7 +191,7 @@ impl From<DatumType> for DbDatumType {
DatumType::String => DbDatumType::String,
DatumType::Bytes => DbDatumType::Bytes,
DatumType::CumulativeI64 => DbDatumType::CumulativeI64,
DatumType::CumulativeF64 => DbDatumType::CumulativeI64,
DatumType::CumulativeF64 => DbDatumType::CumulativeF64,
DatumType::HistogramI64 => DbDatumType::HistogramI64,
DatumType::HistogramF64 => DbDatumType::HistogramF64,
}
Expand All @@ -207,7 +207,7 @@ impl From<DbDatumType> for DatumType {
DbDatumType::String => DatumType::String,
DbDatumType::Bytes => DatumType::Bytes,
DbDatumType::CumulativeI64 => DatumType::CumulativeI64,
DbDatumType::CumulativeF64 => DatumType::CumulativeI64,
DbDatumType::CumulativeF64 => DatumType::CumulativeF64,
DbDatumType::HistogramI64 => DatumType::HistogramI64,
DbDatumType::HistogramF64 => DatumType::HistogramF64,
}
Expand Down Expand Up @@ -891,6 +891,68 @@ mod tests {
let _ = bool::from(DbBool { inner: 10 });
}

#[test]
fn test_db_field_type_conversion() {
macro_rules! check_conversion {
($variant:path, $db_variant:path) => {
assert_eq!(DbFieldType::from($variant), $db_variant);
assert_eq!(FieldType::from($db_variant), $variant);
};
}
check_conversion!(FieldType::String, DbFieldType::String);
check_conversion!(FieldType::I64, DbFieldType::I64);
check_conversion!(FieldType::IpAddr, DbFieldType::IpAddr);
check_conversion!(FieldType::Uuid, DbFieldType::Uuid);
check_conversion!(FieldType::Bool, DbFieldType::Bool);
}

#[test]
fn test_db_datum_type_conversion() {
macro_rules! check_conversion {
($variant:path, $db_variant:path) => {
assert_eq!(DbDatumType::from($variant), $db_variant);
assert_eq!(DatumType::from($db_variant), $variant);
};
}
check_conversion!(DatumType::Bool, DbDatumType::Bool);
check_conversion!(DatumType::I64, DbDatumType::I64);
check_conversion!(DatumType::F64, DbDatumType::F64);
check_conversion!(DatumType::String, DbDatumType::String);
check_conversion!(DatumType::Bytes, DbDatumType::Bytes);
check_conversion!(DatumType::CumulativeI64, DbDatumType::CumulativeI64);
check_conversion!(DatumType::CumulativeF64, DbDatumType::CumulativeF64);
check_conversion!(DatumType::HistogramI64, DbDatumType::HistogramI64);
check_conversion!(DatumType::HistogramF64, DbDatumType::HistogramF64);
}

#[test]
fn test_db_field_list_conversion() {
let db_list = DbFieldList {
names: vec![String::from("field0"), String::from("field1")],
types: vec![DbFieldType::I64, DbFieldType::IpAddr],
sources: vec![DbFieldSource::Target, DbFieldSource::Metric],
};

let list = vec![
FieldSchema {
name: String::from("field0"),
ty: FieldType::I64,
source: FieldSource::Target,
},
FieldSchema {
name: String::from("field1"),
ty: FieldType::IpAddr,
source: FieldSource::Metric,
},
];

assert_eq!(DbFieldList::from(list.clone()), db_list);
assert_eq!(db_list, list.clone().into());
let round_trip: Vec<FieldSchema> =
DbFieldList::from(list.clone()).into();
assert_eq!(round_trip, list);
}

#[test]
fn test_db_histogram() {
let mut hist = Histogram::new(&[0i64, 10, 20]).unwrap();
Expand Down

0 comments on commit 000333b

Please sign in to comment.