|
| 1 | +using System.ComponentModel.DataAnnotations; |
| 2 | +using System.ComponentModel.DataAnnotations.Schema; |
| 3 | + |
| 4 | +using FluentCommand.Import; |
| 5 | + |
| 6 | +namespace FluentCommand.SqlServer.Tests; |
| 7 | + |
| 8 | +public class ImportDefinitionBuilderTests |
| 9 | +{ |
| 10 | + [Table("TestModel", Schema = "UT")] |
| 11 | + private class TestModel |
| 12 | + { |
| 13 | + [Key] |
| 14 | + public int Id { get; set; } |
| 15 | + public string Name { get; set; } = ""; |
| 16 | + public bool IsActive { get; set; } |
| 17 | + |
| 18 | + [Column("Created", TypeName = "datetime2")] |
| 19 | + public DateTime CreatedAt { get; set; } = DateTime.UtcNow; |
| 20 | + |
| 21 | + [NotMapped] |
| 22 | + public string NotMappedProperty { get; set; } = ""; |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void AutoMap_Should_Map_All_Eligible_Properties() |
| 27 | + { |
| 28 | + var definition = ImportDefinitionBuilder<TestModel>.Build(b => b.AutoMap()); |
| 29 | + |
| 30 | + definition.Should().NotBeNull(); |
| 31 | + definition.Name.Should().Be("TestModel"); |
| 32 | + definition.TargetTable.Should().Be("UT.TestModel"); |
| 33 | + definition.Fields.Should().Contain(f => f.Name == nameof(TestModel.Id)); |
| 34 | + definition.Fields.Should().Contain(f => f.Name == nameof(TestModel.Name)); |
| 35 | + definition.Fields.Should().Contain(f => f.Name == nameof(TestModel.IsActive)); |
| 36 | + definition.Fields.Should().Contain(f => f.Name == "Created"); |
| 37 | + definition.Fields.Should().NotContain(f => f.Name == nameof(TestModel.NotMappedProperty)); |
| 38 | + |
| 39 | + definition.Fields.First(f => f.Name == nameof(TestModel.Id)).IsKey.Should().BeTrue(); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void Field_Should_Allow_Explicit_Field_Configuration() |
| 44 | + { |
| 45 | + var definition = ImportDefinitionBuilder<TestModel>.Build(b => b |
| 46 | + .Field(m => m.Name) |
| 47 | + .DisplayName("Custom Name") |
| 48 | + .Required() |
| 49 | + ); |
| 50 | + |
| 51 | + var field = definition.Fields.FirstOrDefault(f => f.Name == nameof(TestModel.Name)); |
| 52 | + field.Should().NotBeNull(); |
| 53 | + field.DisplayName.Should().Be("Custom Name"); |
| 54 | + field.IsRequired.Should().BeTrue(); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void Name_And_TargetTable_Should_Set_Properties() |
| 59 | + { |
| 60 | + var definition = ImportDefinitionBuilder<TestModel>.Build(b => b |
| 61 | + .Name("MyImport") |
| 62 | + .TargetTable("dbo.MyTable") |
| 63 | + ); |
| 64 | + |
| 65 | + definition.Name.Should().Be("MyImport"); |
| 66 | + definition.TargetTable.Should().Be("dbo.MyTable"); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public void CanInsert_And_CanUpdate_Should_Set_Properties() |
| 71 | + { |
| 72 | + var definition = ImportDefinitionBuilder<TestModel>.Build(b => b |
| 73 | + .CanInsert(false) |
| 74 | + .CanUpdate(false) |
| 75 | + ); |
| 76 | + |
| 77 | + definition.CanInsert.Should().BeFalse(); |
| 78 | + definition.CanUpdate.Should().BeFalse(); |
| 79 | + } |
| 80 | +} |
0 commit comments