|
1 | | -using Microsoft.EntityFrameworkCore.Metadata; |
2 | | -using Thinktecture.EntityFrameworkCore.Data; |
3 | | -using Thinktecture.TestDatabaseContext; |
4 | | - |
5 | | -namespace Thinktecture.EntityFrameworkCore.BulkOperations.PropertiesProviderTests; |
6 | | - |
7 | | -public class Exclude |
8 | | -{ |
9 | | - [Fact] |
10 | | - public void Should_throw_if_expression_is_null() |
11 | | - { |
12 | | - Action action = () => IEntityPropertiesProvider.Exclude<TestEntity>(null!); |
13 | | - action.Should().Throw<ArgumentNullException>(); |
14 | | - } |
15 | | - |
16 | | - [Fact] |
17 | | - public void Should_throw_if_expression_return_constant() |
18 | | - { |
19 | | - Action action = () => IEntityPropertiesProvider.Exclude<TestEntity>(entity => null!); |
20 | | - action.Should().Throw<NotSupportedException>(); |
21 | | - } |
22 | | - |
23 | | - [Fact] |
24 | | - public void Should_return_all_properties_if_no_properties_provided() |
25 | | - { |
26 | | - var entityType = GetEntityType<TestEntity>(); |
27 | | - var propertiesProvider = IEntityPropertiesProvider.Exclude<TestEntity>(entity => new { }); |
28 | | - var properties = propertiesProvider.GetPropertiesForTempTable(entityType, null); |
29 | | - |
30 | | - properties.Should().HaveCount(entityType.GetProperties().Count()); |
31 | | - } |
32 | | - |
33 | | - [Fact] |
34 | | - public void Should_extract_all_properties_besides_the_one_specified_by_property_accessor() |
35 | | - { |
36 | | - var entityType = GetEntityType<TestEntity>(); |
37 | | - var idProperty = new PropertyWithNavigations(entityType.FindProperty(nameof(TestEntity.Id))!, Array.Empty<INavigation>()); |
38 | | - |
39 | | - var propertiesProvider = IEntityPropertiesProvider.Exclude<TestEntity>(entity => entity.Id); |
40 | | - |
41 | | - var properties = propertiesProvider.GetPropertiesForTempTable(entityType, null); |
42 | | - properties.Should().HaveCount(entityType.GetProperties().Count() - 1); |
43 | | - properties.Should().NotContain(idProperty); |
44 | | - } |
45 | | - |
46 | | - [Fact] |
47 | | - public void Should_extract_all_properties_besides_the_ones_specified_by_expression() |
48 | | - { |
49 | | - var entityType = GetEntityType<TestEntity>(); |
50 | | - var idProperty = new PropertyWithNavigations(entityType.FindProperty(nameof(TestEntity.Id))!, Array.Empty<INavigation>()); |
51 | | - var countProperty = new PropertyWithNavigations(entityType.FindProperty(nameof(TestEntity.Count))!, Array.Empty<INavigation>()); |
52 | | - |
53 | | - var propertiesProvider = IEntityPropertiesProvider.Exclude<TestEntity>(entity => new { entity.Id, entity.Count }); |
54 | | - |
55 | | - var properties = propertiesProvider.GetPropertiesForTempTable(entityType, null); |
56 | | - properties.Should().HaveCount(entityType.GetProperties().Count() - 2); |
57 | | - properties.Should().NotContain(idProperty); |
58 | | - properties.Should().NotContain(countProperty); |
59 | | - } |
60 | | - |
61 | | - private static IEntityType GetEntityType<T>() |
62 | | - { |
63 | | - var options = new DbContextOptionsBuilder<TestDbContext>().UseSqlite("DataSource=:memory:").Options; |
64 | | - return new TestDbContext(options).Model.GetEntityType(typeof(T)); |
65 | | - } |
66 | | -} |
| 1 | +using Microsoft.EntityFrameworkCore.Metadata; |
| 2 | +using Thinktecture.EntityFrameworkCore.Data; |
| 3 | +using Thinktecture.TestDatabaseContext; |
| 4 | + |
| 5 | +namespace Thinktecture.EntityFrameworkCore.BulkOperations.PropertiesProviderTests; |
| 6 | + |
| 7 | +public class Exclude |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public void Should_throw_if_expression_is_null() |
| 11 | + { |
| 12 | + Action action = () => IEntityPropertiesProvider.Exclude<TestEntity>(null!); |
| 13 | + action.Should().Throw<ArgumentNullException>(); |
| 14 | + } |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public void Should_throw_if_expression_return_constant() |
| 18 | + { |
| 19 | + Action action = () => IEntityPropertiesProvider.Exclude<TestEntity>(entity => null!); |
| 20 | + action.Should().Throw<NotSupportedException>(); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void Should_return_all_properties_if_no_properties_provided() |
| 25 | + { |
| 26 | + var entityType = GetEntityType<TestEntity>(); |
| 27 | + var propertiesProvider = IEntityPropertiesProvider.Exclude<TestEntity>(entity => new { }); |
| 28 | + var properties = propertiesProvider.GetPropertiesForTempTable(entityType, null); |
| 29 | + |
| 30 | + properties.Should().HaveCount(entityType.GetProperties().Count()); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void Should_extract_all_properties_besides_the_one_specified_by_property_accessor() |
| 35 | + { |
| 36 | + var entityType = GetEntityType<TestEntity>(); |
| 37 | + var idProperty = new PropertyWithNavigations(entityType.FindProperty(nameof(TestEntity.Id))!, Array.Empty<INavigation>()); |
| 38 | + |
| 39 | + var propertiesProvider = IEntityPropertiesProvider.Exclude<TestEntity>(entity => entity.Id); |
| 40 | + |
| 41 | + var properties = propertiesProvider.GetPropertiesForTempTable(entityType, null); |
| 42 | + properties.Should().HaveCount(entityType.GetProperties().Count() - 1); |
| 43 | + properties.Should().NotContain(idProperty); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void Should_extract_all_properties_besides_the_ones_specified_by_expression() |
| 48 | + { |
| 49 | + var entityType = GetEntityType<TestEntity>(); |
| 50 | + var idProperty = new PropertyWithNavigations(entityType.FindProperty(nameof(TestEntity.Id))!, Array.Empty<INavigation>()); |
| 51 | + var countProperty = new PropertyWithNavigations(entityType.FindProperty(nameof(TestEntity.Count))!, Array.Empty<INavigation>()); |
| 52 | + |
| 53 | + var propertiesProvider = IEntityPropertiesProvider.Exclude<TestEntity>(entity => new { entity.Id, entity.Count }); |
| 54 | + |
| 55 | + var properties = propertiesProvider.GetPropertiesForTempTable(entityType, null); |
| 56 | + properties.Should().HaveCount(entityType.GetProperties().Count() - 2); |
| 57 | + properties.Should().NotContain(idProperty); |
| 58 | + properties.Should().NotContain(countProperty); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void Should_handle_properties_from_base_classes() |
| 63 | + { |
| 64 | + var entityType = GetEntityType<TestEntityWithBaseClass>(); |
| 65 | + var idProperty = new PropertyWithNavigations(entityType.FindProperty(nameof(TestEntityWithBaseClass.Id))!, Array.Empty<INavigation>()); |
| 66 | + var baseProperty = new PropertyWithNavigations(entityType.FindProperty(nameof(TestEntityWithBaseClass.Base_A))!, Array.Empty<INavigation>()); |
| 67 | + |
| 68 | + var propertiesProvider = IEntityPropertiesProvider.Exclude<TestEntityWithBaseClass>(entity => new { entity.Id, entity.Base_A }); |
| 69 | + |
| 70 | + var properties = propertiesProvider.GetPropertiesForTempTable(entityType, null); |
| 71 | + properties.Should().HaveCount(entityType.GetProperties().Count() - 2); |
| 72 | + properties.Should().NotContain(idProperty); |
| 73 | + properties.Should().NotContain(baseProperty); |
| 74 | + } |
| 75 | + |
| 76 | + private static IEntityType GetEntityType<T>() |
| 77 | + { |
| 78 | + var options = new DbContextOptionsBuilder<TestDbContext>().UseSqlite("DataSource=:memory:").Options; |
| 79 | + return new TestDbContext(options).Model.GetEntityType(typeof(T)); |
| 80 | + } |
| 81 | +} |
0 commit comments