Skip to content

Commit

Permalink
Refine observation assertion messages for keys
Browse files Browse the repository at this point in the history
  • Loading branch information
izeye committed Feb 24, 2024
1 parent 2b5181c commit 7be770f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ private List<String> highCardinalityKeys() {
public SELF hasLowCardinalityKeyValueWithKey(String key) {
isNotNull();
if (this.actual.getLowCardinalityKeyValues().stream().noneMatch(tag -> tag.getKey().equals(key))) {
if (this.actual.getHighCardinalityKeyValue(key) != null) {
failWithMessage(
"Observation should have a low cardinality tag with key <%s> but it was in the wrong (high) cardinality keys. List of all low cardinality keys <%s>",
key, lowCardinalityKeys());
}
failWithMessage(
"Observation should have a low cardinality tag with key <%s> but it's not there. List of all keys <%s>",
key, lowCardinalityKeys());
Expand Down Expand Up @@ -301,6 +306,11 @@ public SELF doesNotHaveLowCardinalityKeyValue(KeyValue keyValue) {
public SELF hasHighCardinalityKeyValueWithKey(String key) {
isNotNull();
if (this.actual.getHighCardinalityKeyValues().stream().noneMatch(tag -> tag.getKey().equals(key))) {
if (this.actual.getLowCardinalityKeyValue(key) != null) {
failWithMessage(
"Observation should have a high cardinality tag with key <%s> but it was in the wrong (low) cardinality keys. List of all high cardinality keys <%s>",
key, highCardinalityKeys());
}
failWithMessage(
"Observation should have a high cardinality tag with key <%s> but it's not there. List of all keys <%s>",
key, highCardinalityKeys());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,22 @@ void should_throw_exception_when_low_cardinality_key_value_missing() {
.isInstanceOf(AssertionError.class);
}

@Test
void should_throw_exception_when_low_cardinality_key_value_missing_but_in_high_cardinality_keys() {
Observation observation = Observation.start("foo", context, registry);
observation.lowCardinalityKeyValue("low", "l");
observation.highCardinalityKeyValue("high", "h");

thenThrownBy(() -> assertThat(context).hasLowCardinalityKeyValue("high", "h"))
.isInstanceOf(AssertionError.class)
.hasMessage(
"Observation should have a low cardinality tag with key <high> but it was in the wrong (high) cardinality keys. List of all low cardinality keys <[low]>");
thenThrownBy(() -> assertThat(context).hasLowCardinalityKeyValueWithKey("high"))
.isInstanceOf(AssertionError.class)
.hasMessage(
"Observation should have a low cardinality tag with key <high> but it was in the wrong (high) cardinality keys. List of all low cardinality keys <[low]>");
}

@Test
void should_not_throw_exception_when_high_cardinality_key_value_exists() {
Observation observation = Observation.start("foo", context, registry);
Expand All @@ -273,6 +289,22 @@ void should_throw_exception_when_high_cardinality_key_value_missing() {
.isInstanceOf(AssertionError.class);
}

@Test
void should_throw_exception_when_high_cardinality_key_value_missing_but_in_low_cardinality_keys() {
Observation observation = Observation.start("foo", context, registry);
observation.lowCardinalityKeyValue("low", "l");
observation.highCardinalityKeyValue("high", "h");

thenThrownBy(() -> assertThat(context).hasHighCardinalityKeyValue("low", "l"))
.isInstanceOf(AssertionError.class)
.hasMessage(
"Observation should have a high cardinality tag with key <low> but it was in the wrong (low) cardinality keys. List of all high cardinality keys <[high]>");
thenThrownBy(() -> assertThat(context).hasHighCardinalityKeyValueWithKey("low"))
.isInstanceOf(AssertionError.class)
.hasMessage(
"Observation should have a high cardinality tag with key <low> but it was in the wrong (low) cardinality keys. List of all high cardinality keys <[high]>");
}

@Test
void should_not_throw_exception_when_high_cardinality_key_value_present() {
thenNoException().isThrownBy(() -> assertThat(context).doesNotHaveHighCardinalityKeyValue("foo", "bar")
Expand Down

0 comments on commit 7be770f

Please sign in to comment.