|
| 1 | +namespace Tests.Validators; |
| 2 | + |
| 3 | +public class WhereValidatorCustomSpecTests |
| 4 | +{ |
| 5 | + private static readonly WhereValidator _validator = WhereValidator.Instance; |
| 6 | + |
| 7 | + public record Customer(int Id, string Name); |
| 8 | + |
| 9 | + [Fact] |
| 10 | + public void ReturnsTrue_GivenEmptySpec() |
| 11 | + { |
| 12 | + var customer = new Customer(1, "Customer1"); |
| 13 | + |
| 14 | + var spec = new CustomSpecification<Customer>(); |
| 15 | + |
| 16 | + var result = _validator.IsValid(customer, spec); |
| 17 | + |
| 18 | + result.Should().BeTrue(); |
| 19 | + } |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public void ReturnsTrue_GivenSpecWithSingleWhere_WithValidEntity() |
| 23 | + { |
| 24 | + var customer = new Customer(1, "Customer1"); |
| 25 | + |
| 26 | + var spec = new CustomSpecification<Customer>(); |
| 27 | + spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Id == 1)); |
| 28 | + |
| 29 | + var result = _validator.IsValid(customer, spec); |
| 30 | + |
| 31 | + result.Should().BeTrue(); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void ReturnsFalse_GivenSpecWithSingleWhere_WithInvalidEntity() |
| 36 | + { |
| 37 | + var customer = new Customer(1, "Customer1"); |
| 38 | + |
| 39 | + var spec = new CustomSpecification<Customer>(); |
| 40 | + spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Id == 2)); |
| 41 | + |
| 42 | + var result = _validator.IsValid(customer, spec); |
| 43 | + |
| 44 | + result.Should().BeFalse(); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void ReturnsTrue_GivenSpecWithMultipleWhere_WithValidEntity() |
| 49 | + { |
| 50 | + var customer = new Customer(1, "Customer1"); |
| 51 | + |
| 52 | + var spec = new CustomSpecification<Customer>(); |
| 53 | + spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Id == 1)); |
| 54 | + spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Name == "Customer1")); |
| 55 | + |
| 56 | + var result = _validator.IsValid(customer, spec); |
| 57 | + |
| 58 | + result.Should().BeTrue(); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void ReturnsFalse_GivenSpecWithMultipleWhere_WithSingleInvalidValue() |
| 63 | + { |
| 64 | + var customer = new Customer(1, "Customer1"); |
| 65 | + |
| 66 | + var spec = new CustomSpecification<Customer>(); |
| 67 | + spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Id == 2)); |
| 68 | + spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Name == "Customer1")); |
| 69 | + |
| 70 | + var result = _validator.IsValid(customer, spec); |
| 71 | + |
| 72 | + result.Should().BeFalse(); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void ReturnsFalse_GivenSpecWithMultipleWhere_WithAllInvalidValues() |
| 77 | + { |
| 78 | + var customer = new Customer(1, "Customer1"); |
| 79 | + |
| 80 | + var spec = new CustomSpecification<Customer>(); |
| 81 | + spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Id == 1)); |
| 82 | + spec.Where.Add(new WhereExpressionInfo<Customer>(x => x.Name == "Customer2")); |
| 83 | + |
| 84 | + var result = _validator.IsValid(customer, spec); |
| 85 | + |
| 86 | + result.Should().BeFalse(); |
| 87 | + } |
| 88 | + |
| 89 | + public class CustomSpecification<T> : ISpecification<T> |
| 90 | + { |
| 91 | + public List<WhereExpressionInfo<T>> Where { get; set; } = new(); |
| 92 | + public List<SearchExpressionInfo<T>> Search { get; set; } = new(); |
| 93 | + public IEnumerable<SearchExpressionInfo<T>> SearchCriterias => Search; |
| 94 | + public IEnumerable<WhereExpressionInfo<T>> WhereExpressions => Where; |
| 95 | + |
| 96 | + public ISpecificationBuilder<T> Query => throw new NotImplementedException(); |
| 97 | + public IEnumerable<OrderExpressionInfo<T>> OrderExpressions => throw new NotImplementedException(); |
| 98 | + public IEnumerable<IncludeExpressionInfo> IncludeExpressions => throw new NotImplementedException(); |
| 99 | + public IEnumerable<string> IncludeStrings => throw new NotImplementedException(); |
| 100 | + public Dictionary<string, object> Items => throw new NotImplementedException(); |
| 101 | + public int Take => throw new NotImplementedException(); |
| 102 | + public int Skip => throw new NotImplementedException(); |
| 103 | + public Func<IEnumerable<T>, IEnumerable<T>>? PostProcessingAction => throw new NotImplementedException(); |
| 104 | + public IEnumerable<string> QueryTags => throw new NotImplementedException(); |
| 105 | + public bool CacheEnabled => throw new NotImplementedException(); |
| 106 | + public string? CacheKey => throw new NotImplementedException(); |
| 107 | + public bool AsTracking => throw new NotImplementedException(); |
| 108 | + public bool AsNoTracking => throw new NotImplementedException(); |
| 109 | + public bool AsSplitQuery => throw new NotImplementedException(); |
| 110 | + public bool AsNoTrackingWithIdentityResolution => throw new NotImplementedException(); |
| 111 | + public bool IgnoreQueryFilters => throw new NotImplementedException(); |
| 112 | + public bool IgnoreAutoIncludes => throw new NotImplementedException(); |
| 113 | + public IEnumerable<T> Evaluate(IEnumerable<T> entities) |
| 114 | + => throw new NotImplementedException(); |
| 115 | + public bool IsSatisfiedBy(T entity) |
| 116 | + => throw new NotImplementedException(); |
| 117 | + } |
| 118 | +} |
0 commit comments