|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using FluentNHibernate.Automapping.TestFixtures; |
| 4 | +using FluentNHibernate.Conventions.Helpers.Builders; |
| 5 | +using FluentNHibernate.Conventions.Instances; |
| 6 | +using FluentNHibernate.Mapping; |
| 7 | +using FluentNHibernate.MappingModel; |
| 8 | +using NUnit.Framework; |
| 9 | + |
| 10 | +namespace FluentNHibernate.Testing.ConventionsTests.ApplyingToModel; |
| 11 | + |
| 12 | +[TestFixture] |
| 13 | +public class NaturalIdManyToOneConventionTests |
| 14 | +{ |
| 15 | + private PersistenceModel model; |
| 16 | + |
| 17 | + [SetUp] |
| 18 | + public void CreatePersistenceModel() |
| 19 | + { |
| 20 | + model = new PersistenceModel(); |
| 21 | + } |
| 22 | + |
| 23 | + [Test] |
| 24 | + public void ShouldSetAccessProperty() |
| 25 | + { |
| 26 | + Convention(x => x.Access.Property()); |
| 27 | + |
| 28 | + VerifyModel(x => x.Access.ShouldEqual("property")); |
| 29 | + } |
| 30 | + |
| 31 | + [Test] |
| 32 | + public void ShouldSetCascadeProperty() |
| 33 | + { |
| 34 | + Convention(x => x.Cascade.None()); |
| 35 | + |
| 36 | + VerifyModel(x => x.Cascade.ShouldEqual("none")); |
| 37 | + } |
| 38 | + |
| 39 | + [Test] |
| 40 | + public void ShouldSetClassProperty() |
| 41 | + { |
| 42 | + Convention(x => x.CustomClass(typeof(int))); |
| 43 | + |
| 44 | + VerifyModel(x => x.Class.GetUnderlyingSystemType().ShouldEqual(typeof(int))); |
| 45 | + } |
| 46 | + |
| 47 | + [Test] |
| 48 | + public void ShouldSetColumnProperty() |
| 49 | + { |
| 50 | + Convention(x => x.Column("xxx")); |
| 51 | + |
| 52 | + VerifyModel(x => x.Columns.First().Name.ShouldEqual("xxx")); |
| 53 | + } |
| 54 | + |
| 55 | + [Test] |
| 56 | + public void ShouldSetFetchProperty() |
| 57 | + { |
| 58 | + Convention(x => x.Fetch.Select()); |
| 59 | + |
| 60 | + VerifyModel(x => x.Fetch.ShouldEqual("select")); |
| 61 | + } |
| 62 | + |
| 63 | + [Test] |
| 64 | + public void ShouldSetIndexProperty() |
| 65 | + { |
| 66 | + Convention(x => x.Index("value")); |
| 67 | + |
| 68 | + VerifyModel(x => x.Columns.First().Index.ShouldEqual("value")); |
| 69 | + } |
| 70 | + |
| 71 | + [Test] |
| 72 | + public void ShouldSetInsertProperty() |
| 73 | + { |
| 74 | + Convention(x => x.Insert()); |
| 75 | + |
| 76 | + VerifyModel(x => x.Insert.ShouldBeTrue()); |
| 77 | + } |
| 78 | + |
| 79 | + [Test] |
| 80 | + public void ShouldSetLazyProperty() |
| 81 | + { |
| 82 | + Convention(x => x.LazyLoad()); |
| 83 | + |
| 84 | + VerifyModel(x => x.Lazy.ShouldEqual(Laziness.Proxy.ToString())); |
| 85 | + } |
| 86 | + |
| 87 | + [Test] |
| 88 | + public void ShouldSetNotFoundProperty() |
| 89 | + { |
| 90 | + Convention(x => x.NotFound.Ignore()); |
| 91 | + |
| 92 | + VerifyModel(x => x.NotFound.ShouldEqual("ignore")); |
| 93 | + } |
| 94 | + |
| 95 | + [Test] |
| 96 | + public void ShouldSetNullableProperty() |
| 97 | + { |
| 98 | + Convention(x => x.Nullable()); |
| 99 | + |
| 100 | + VerifyModel(x => x.Columns.First().NotNull.ShouldBeFalse()); |
| 101 | + } |
| 102 | + |
| 103 | + [Test] |
| 104 | + public void ShouldSetPropertyRefProperty() |
| 105 | + { |
| 106 | + Convention(x => x.PropertyRef("xxx")); |
| 107 | + |
| 108 | + VerifyModel(x => x.PropertyRef.ShouldEqual("xxx")); |
| 109 | + } |
| 110 | + |
| 111 | + [Test] |
| 112 | + public void ShouldSetReadOnlyProperty() |
| 113 | + { |
| 114 | + Convention(x => x.ReadOnly()); |
| 115 | + |
| 116 | + VerifyModel(x => |
| 117 | + { |
| 118 | + x.Insert.ShouldBeFalse(); |
| 119 | + x.Update.ShouldBeFalse(); |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + [Test] |
| 124 | + public void ShouldSetUniqueProperty() |
| 125 | + { |
| 126 | + Convention(x => x.Unique()); |
| 127 | + |
| 128 | + VerifyModel(x => x.Columns.First().Unique.ShouldBeTrue()); |
| 129 | + } |
| 130 | + |
| 131 | + [Test] |
| 132 | + public void ShouldSetUniqueKeyProperty() |
| 133 | + { |
| 134 | + Convention(x => x.UniqueKey("xxx")); |
| 135 | + |
| 136 | + VerifyModel(x => x.Columns.First().UniqueKey.ShouldEqual("xxx")); |
| 137 | + } |
| 138 | + |
| 139 | + [Test] |
| 140 | + public void ShouldSetUpdateProperty() |
| 141 | + { |
| 142 | + Convention(x => x.Update()); |
| 143 | + |
| 144 | + VerifyModel(x => x.Update.ShouldBeTrue()); |
| 145 | + } |
| 146 | + |
| 147 | + [Test] |
| 148 | + public void ShouldSetForeignKeyProperty() |
| 149 | + { |
| 150 | + Convention(x => x.ForeignKey("xxx")); |
| 151 | + |
| 152 | + VerifyModel(x => x.ForeignKey.ShouldEqual("xxx")); |
| 153 | + } |
| 154 | + |
| 155 | + [Test] |
| 156 | + public void ShouldSetFormulaProperty() |
| 157 | + { |
| 158 | + Convention(x => x.Formula("xxx")); |
| 159 | + |
| 160 | + VerifyModel(x => x.Formula.ShouldEqual("xxx")); |
| 161 | + } |
| 162 | + |
| 163 | + [Test] |
| 164 | + public void ShouldSetOptimisticLockProperty() |
| 165 | + { |
| 166 | + Convention(x => x.OptimisticLock()); |
| 167 | + |
| 168 | + VerifyModel(x => x.OptimisticLock.ShouldBeTrue()); |
| 169 | + } |
| 170 | + |
| 171 | + #region Helpers |
| 172 | + |
| 173 | + private void Convention(Action<IManyToOneInstance> convention) |
| 174 | + { |
| 175 | + model.Conventions.Add(new ReferenceConventionBuilder().Always(convention)); |
| 176 | + } |
| 177 | + |
| 178 | + private void VerifyModel(Action<ManyToOneMapping> modelVerification) |
| 179 | + { |
| 180 | + var classMap = new ClassMap<ExampleClass>(); |
| 181 | + classMap.Id(x => x.Id); |
| 182 | + var map = classMap.NaturalId().Reference(x => x.Parent); |
| 183 | + |
| 184 | + model.Add(classMap); |
| 185 | + |
| 186 | + var generatedModels = model.BuildMappings(); |
| 187 | + var modelInstance = generatedModels |
| 188 | + .SelectMany(x => x.Classes) |
| 189 | + .First(x => x.Type == typeof(ExampleClass)) |
| 190 | + .NaturalId.ManyToOnes.First(); |
| 191 | + |
| 192 | + modelVerification(modelInstance); |
| 193 | + } |
| 194 | + |
| 195 | + #endregion |
| 196 | +} |
0 commit comments