|
| 1 | +namespace Tests; |
| 2 | + |
| 3 | +public class SpecificationExtensionsTests |
| 4 | +{ |
| 5 | + private record Address(int Id, string Street); |
| 6 | + private record Person(int Id, string Name, List<string> Names, Address Address); |
| 7 | + |
| 8 | + [Fact] |
| 9 | + public void WithProjectionOf_ReturnsCopyWithProjection() |
| 10 | + { |
| 11 | + var spec = new Specification<Person>(); |
| 12 | + spec.Items.Add("test", "test"); |
| 13 | + spec.Query |
| 14 | + .Where(x => x.Name == "test") |
| 15 | + .Include(x => x.Address) |
| 16 | + .Include("Address") |
| 17 | + .OrderBy(x => x.Id) |
| 18 | + .Search(x => x.Name, "test") |
| 19 | + .Take(2) |
| 20 | + .Skip(3) |
| 21 | + .WithCacheKey("testKey") |
| 22 | + .IgnoreQueryFilters() |
| 23 | + .IgnoreQueryFilters() |
| 24 | + .AsSplitQuery() |
| 25 | + .AsNoTracking() |
| 26 | + .TagWith("testQuery") |
| 27 | + .PostProcessingAction(x => x.Where(x => x.Id > 0)); |
| 28 | + |
| 29 | + var projectionSpec = new Specification<Person, string>(); |
| 30 | + projectionSpec.Query.Select(x => x.Name); |
| 31 | + projectionSpec.Query.SelectMany(x => x.Names); |
| 32 | + projectionSpec.Query.PostProcessingAction(x => x.Select(x => x + "A")); |
| 33 | + |
| 34 | + var newSpec = spec.WithProjectionOf(projectionSpec); |
| 35 | + |
| 36 | + newSpec.Items.Should().NotBeSameAs(spec.Items); |
| 37 | + newSpec.Items.Should().BeEquivalentTo(spec.Items); |
| 38 | + |
| 39 | + newSpec.WhereExpressions.Should().NotBeSameAs(spec.WhereExpressions); |
| 40 | + newSpec.WhereExpressions.Should().Equal(spec.WhereExpressions); |
| 41 | + |
| 42 | + newSpec.IncludeExpressions.Should().NotBeSameAs(spec.IncludeExpressions); |
| 43 | + newSpec.IncludeExpressions.Should().Equal(spec.IncludeExpressions); |
| 44 | + |
| 45 | + newSpec.IncludeStrings.Should().NotBeSameAs(spec.IncludeStrings); |
| 46 | + newSpec.IncludeStrings.Should().Equal(spec.IncludeStrings); |
| 47 | + |
| 48 | + newSpec.OrderExpressions.Should().NotBeSameAs(spec.OrderExpressions); |
| 49 | + newSpec.OrderExpressions.Should().Equal(spec.OrderExpressions); |
| 50 | + |
| 51 | + newSpec.SearchCriterias.Should().NotBeSameAs(spec.SearchCriterias); |
| 52 | + newSpec.SearchCriterias.Should().Equal(spec.SearchCriterias); |
| 53 | + |
| 54 | + newSpec.Take.Should().Be(spec.Take); |
| 55 | + newSpec.Skip.Should().Be(spec.Skip); |
| 56 | + newSpec.CacheKey.Should().Be(spec.CacheKey); |
| 57 | + newSpec.IgnoreQueryFilters.Should().Be(spec.IgnoreQueryFilters); |
| 58 | + newSpec.IgnoreAutoIncludes.Should().Be(spec.IgnoreAutoIncludes); |
| 59 | + newSpec.AsSplitQuery.Should().Be(spec.AsSplitQuery); |
| 60 | + newSpec.AsNoTracking.Should().Be(spec.AsNoTracking); |
| 61 | + newSpec.AsNoTrackingWithIdentityResolution.Should().Be(spec.AsNoTrackingWithIdentityResolution); |
| 62 | + newSpec.AsTracking.Should().Be(spec.AsTracking); |
| 63 | + newSpec.QueryTag.Should().Be(spec.QueryTag); |
| 64 | + |
| 65 | + newSpec.PostProcessingAction.Should().BeSameAs(projectionSpec.PostProcessingAction); |
| 66 | + ((Specification<Person>)newSpec).PostProcessingAction.Should().BeSameAs(spec.PostProcessingAction); |
| 67 | + } |
| 68 | +} |
0 commit comments