From 51d7558eb3aaa9c05347cbbd7111b589ea890076 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Sat, 26 Sep 2026 17:54:54 +0300 Subject: [PATCH] Aligns the audio rating validation with the book rating --- .../Common/ValueObjects/AudioRating.cs | 2 + .../Common/ValueObjects/AudioRatingFixture.cs | 57 ++++++++ .../Common/ValueObjects/AudioRatingTests.cs | 134 ++++++++++++++++++ 3 files changed, 193 insertions(+) create mode 100644 tests/Common/Lumina.Domain.Fixtures/Core/BoundedContexts/AudioLibraryBoundedContext/Common/ValueObjects/AudioRatingFixture.cs create mode 100644 tests/UnitTests/Lumina.Domain.UnitTests/Core/BoundedContexts/AudioLibraryBoundedContext/Common/ValueObjects/AudioRatingTests.cs diff --git a/src/Lumina.Domain/Core/BoundedContexts/AudioLibraryBoundedContext/Common/ValueObjects/AudioRating.cs b/src/Lumina.Domain/Core/BoundedContexts/AudioLibraryBoundedContext/Common/ValueObjects/AudioRating.cs index 212f5505..fbd014ee 100644 --- a/src/Lumina.Domain/Core/BoundedContexts/AudioLibraryBoundedContext/Common/ValueObjects/AudioRating.cs +++ b/src/Lumina.Domain/Core/BoundedContexts/AudioLibraryBoundedContext/Common/ValueObjects/AudioRating.cs @@ -45,6 +45,8 @@ public class AudioRating : Rating /// public static Result Create(decimal value, decimal maxValue, Optional source, Optional voteCount) { + if (maxValue < 0 || value < 0) + return Errors.Metadata.RatingValueMustBePositive; if (value > maxValue) return Errors.Metadata.RatingValueCannotBeGreaterThanMaxValue; diff --git a/tests/Common/Lumina.Domain.Fixtures/Core/BoundedContexts/AudioLibraryBoundedContext/Common/ValueObjects/AudioRatingFixture.cs b/tests/Common/Lumina.Domain.Fixtures/Core/BoundedContexts/AudioLibraryBoundedContext/Common/ValueObjects/AudioRatingFixture.cs new file mode 100644 index 00000000..065feee1 --- /dev/null +++ b/tests/Common/Lumina.Domain.Fixtures/Core/BoundedContexts/AudioLibraryBoundedContext/Common/ValueObjects/AudioRatingFixture.cs @@ -0,0 +1,57 @@ +#region ========================================================================= USING ===================================================================================== +using Bogus; +using Lumina.Domain.Common.Primitives; +using Lumina.Domain.Core.BoundedContexts.AudioLibraryBoundedContext.Common.ValueObjects; +using Lumina.Domain.SharedKernel.Common.Enums.AudioLibrary; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +#endregion + +namespace Lumina.Domain.Fixtures.Core.BoundedContexts.AudioLibraryBoundedContext.Common.ValueObjects; + +/// +/// Fixture class for the domain value object. +/// +[ExcludeFromCodeCoverage] +public class AudioRatingFixture +{ + private readonly Faker _faker = new(); + + /// + /// Creates a random valid . + /// + /// Optional. The numeric value of the rating. + /// Optional. The maximum possible rating value. + /// Optional. The source of the rating. + /// Optional. The number of votes or reviews. + /// The created . + public AudioRating Create( + decimal? value = null, + decimal? maxValue = null, + Optional? source = null, + Optional? voteCount = null) + { + decimal resolvedMaxValue = maxValue ?? 5m; + Result ratingResult = AudioRating.Create( + value ?? _faker.Random.Decimal(0m, resolvedMaxValue), + resolvedMaxValue, + source ?? Optional.Some(_faker.PickRandom()), + voteCount ?? Optional.Some(_faker.Random.Int(0, 10000))); + + if (ratingResult.IsFailure) + throw new InvalidOperationException("Failed to create AudioRating: " + string.Join(", ", ratingResult.Errors)); + return ratingResult.Value; + } + + /// + /// Creates multiple instances with randomized test data. + /// + /// Number of instances to create. + /// List of configured instances. + public List CreateMany(int count = 3) + { + return [.. Enumerable.Range(0, count).Select(_ => Create())]; + } +} diff --git a/tests/UnitTests/Lumina.Domain.UnitTests/Core/BoundedContexts/AudioLibraryBoundedContext/Common/ValueObjects/AudioRatingTests.cs b/tests/UnitTests/Lumina.Domain.UnitTests/Core/BoundedContexts/AudioLibraryBoundedContext/Common/ValueObjects/AudioRatingTests.cs new file mode 100644 index 00000000..9b2405c5 --- /dev/null +++ b/tests/UnitTests/Lumina.Domain.UnitTests/Core/BoundedContexts/AudioLibraryBoundedContext/Common/ValueObjects/AudioRatingTests.cs @@ -0,0 +1,134 @@ +#region ========================================================================= USING ===================================================================================== +using DomainErrors = Lumina.Domain.Common.Errors.Errors; +using Lumina.Domain.Common.Primitives; +using Lumina.Domain.Core.BoundedContexts.AudioLibraryBoundedContext.Common.ValueObjects; +using Lumina.Domain.Fixtures.Core.BoundedContexts.AudioLibraryBoundedContext.Common.ValueObjects; +using Lumina.Domain.SharedKernel.Common.Enums.AudioLibrary; +using System.Diagnostics.CodeAnalysis; +#endregion + +namespace Lumina.Domain.UnitTests.Core.BoundedContexts.AudioLibraryBoundedContext.Common.ValueObjects; + +/// +/// Contains unit tests for the class. +/// +[ExcludeFromCodeCoverage] +public class AudioRatingTests +{ + private readonly AudioRatingFixture _audioRatingFixture = new(); + + [Fact] + public void Create_WhenCalledWithValidValues_ShouldCreateRatingWithAllPropertiesSet() + { + // Act + Result result = AudioRating.Create(4m, 5m, Optional.Some(AudioRatingSource.MusicBrainz), Optional.Some(10)); + + // Assert + Assert.False(result.IsFailure); + Assert.Equal(4m, result.Value.Value); + Assert.Equal(5m, result.Value.MaxValue); + Assert.Equal(Optional.Some(AudioRatingSource.MusicBrainz), result.Value.Source); + Assert.Equal(Optional.Some(10), result.Value.VoteCount); + } + + [Fact] + public void Create_WhenOptionalValuesAreAbsent_ShouldCreateRatingWithoutThem() + { + // Act + Result result = AudioRating.Create(3m, 5m, Optional.None(), Optional.None()); + + // Assert + Assert.False(result.IsFailure); + Assert.False(result.Value.Source.HasValue); + Assert.False(result.Value.VoteCount.HasValue); + } + + [Theory] + [InlineData(-1)] // negative value + [InlineData(-0.5)] // negative fractional value + public void Create_WhenValueIsNegative_ShouldReturnError(double value) + { + // Act + Result result = AudioRating.Create((decimal)value, maxValue: 5, Optional.None(), Optional.None()); + + // Assert + Assert.True(result.IsFailure); + Assert.Equal(DomainErrors.Metadata.RatingValueMustBePositive, result.FirstError); + } + + [Fact] + public void Create_WhenMaxValueIsNegative_ShouldReturnError() + { + // Act + Result result = AudioRating.Create(value: 4m, maxValue: -5, Optional.None(), Optional.None()); + + // Assert + Assert.True(result.IsFailure); + Assert.Equal(DomainErrors.Metadata.RatingValueMustBePositive, result.FirstError); + } + + [Fact] + public void Create_WhenValueIsGreaterThanMaxValue_ShouldReturnError() + { + // Act + Result result = AudioRating.Create(6m, 5m, Optional.None(), Optional.None()); + + // Assert + Assert.True(result.IsFailure); + Assert.Equal(DomainErrors.Metadata.RatingValueCannotBeGreaterThanMaxValue, result.FirstError); + } + + [Fact] + public void ToString_WhenSourceAndVoteCountArePresent_ShouldIncludeThem() + { + // Arrange + AudioRating rating = _audioRatingFixture.Create(value: 4m, maxValue: 5m, source: Optional.Some(AudioRatingSource.LastFm), voteCount: Optional.Some(10)); + + // Act + string result = rating.ToString(); + + // Assert + Assert.Equal("4/5 (LastFm) [10 votes]", result); + } + + [Fact] + public void ToString_WhenSourceAndVoteCountAreAbsent_ShouldReturnValueAndMaxValue() + { + // Arrange + AudioRating rating = _audioRatingFixture.Create(value: 3m, maxValue: 5m, source: Optional.None(), voteCount: Optional.None()); + + // Act + string result = rating.ToString(); + + // Assert + Assert.Equal("3/5", result); + } + + [Fact] + public void Equals_WithSameValues_ShouldReturnTrue() + { + // Arrange + AudioRating firstRating = _audioRatingFixture.Create(value: 4m, maxValue: 5m, source: Optional.Some(AudioRatingSource.User), voteCount: Optional.Some(10)); + AudioRating secondRating = _audioRatingFixture.Create(value: 4m, maxValue: 5m, source: Optional.Some(AudioRatingSource.User), voteCount: Optional.Some(10)); + + // Act + bool result = firstRating.Equals(secondRating); + + // Assert + Assert.True(result); + } + + [Fact] + public void Equals_WithDifferentValue_ShouldReturnFalse() + { + // Arrange + AudioRating firstRating = _audioRatingFixture.Create(value: 1m, maxValue: 5m); + AudioRating secondRating = _audioRatingFixture.Create(value: 4m, maxValue: 5m); + + // Act + bool result = firstRating.Equals(secondRating); + + // Assert + Assert.False(result); + } +}