Add unit tests for MicCheck.Api.Segments

Covers CreateSegmentRequestValidator, response mapping (incl. nested
rules), SegmentsController, and gaps in SegmentService/SegmentEvaluator
(FindByIdAsync, not-found paths, nested rules, modulo/percentage edge
cases), raising the namespace from 66% to 98% coverage.
This commit is contained in:
2026-07-05 13:11:35 -07:00
parent 7fc021500c
commit 0595ff5642
5 changed files with 493 additions and 0 deletions

View File

@@ -274,6 +274,51 @@ public class SegmentEvaluatorTests
Assert.That(_evaluator.Evaluate(segment, [], "another-user"), Is.False);
}
[Test]
public void WhenModuloConditionValueIsMalformed_ThenSegmentDoesNotMatch()
{
var segment = BuildSegment(SegmentRuleType.All,
Condition("userId", SegmentConditionOperator.ModuloValueDivisorRemainder, "not-a-modulo-spec"));
var traits = new[] { Trait("userId", "9") };
Assert.That(_evaluator.Evaluate(segment, traits), Is.False);
}
[Test]
public void WhenModuloDivisorIsZero_ThenSegmentDoesNotMatch()
{
var segment = BuildSegment(SegmentRuleType.All,
Condition("userId", SegmentConditionOperator.ModuloValueDivisorRemainder, "0|0"));
var traits = new[] { Trait("userId", "9") };
Assert.That(_evaluator.Evaluate(segment, traits), Is.False);
}
[Test]
public void WhenPercentageSplitValueIsNotANumber_ThenSegmentDoesNotMatch()
{
var segment = BuildSegment(SegmentRuleType.All,
Condition("", SegmentConditionOperator.PercentageSplit, "not-a-number"));
Assert.That(_evaluator.Evaluate(segment, [], "any-user"), Is.False);
}
[Test]
public void WhenARuleHasAChildRuleThatFails_ThenTheParentRuleDoesNotMatchEvenIfItsOwnConditionsPass()
{
var segment = new Segment { Name = "Test", ProjectId = 1, CreatedAt = DateTimeOffset.UtcNow };
var parent = new SegmentRule { Type = SegmentRuleType.All };
parent.Conditions.Add(Condition("plan", SegmentConditionOperator.Equal, "premium"));
var child = new SegmentRule { Type = SegmentRuleType.All };
child.Conditions.Add(Condition("country", SegmentConditionOperator.Equal, "US"));
parent.ChildRules.Add(child);
segment.Rules.Add(parent);
var traits = new[] { Trait("plan", "premium"), Trait("country", "UK") };
Assert.That(_evaluator.Evaluate(segment, traits), Is.False);
}
[Test]
public void WhenRuleTypeIsAny_ThenAtLeastOneConditionMustMatch()
{