Skip to content

Commit

Permalink
Merge remote branch 'paulbatum/dev'
Browse files Browse the repository at this point in the history
Conflicts:
	src/FluentNHibernate/Visitors/NullMappingModelVisitor.cs
  • Loading branch information
jagregory committed Mar 7, 2010
2 parents f1d7529 + cba14f6 commit d2f3887
Show file tree
Hide file tree
Showing 35 changed files with 664 additions and 39 deletions.
17 changes: 17 additions & 0 deletions src/FluentNHibernate.Testing/Cfg/FluentConfigurationTests.cs
Expand Up @@ -256,6 +256,23 @@ public void WritesFluentMappingsOut()
.ShouldContain(HbmFor<Record>);
}

[Test]
public void WritesFluentMappingsOutToTextWriter()
{
var stringWriter = new StringWriter();

Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory)
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<Record>()
.ExportTo(stringWriter))
.BuildConfiguration();

string export = stringWriter.ToString();
export.ShouldNotBeEmpty();
}

[Test]
public void WritesFluentMappingsOutMergedWhenFlagSet()
{
Expand Down
Expand Up @@ -160,6 +160,22 @@ public void ShouldSetTableNameProperty()
VerifyModel(x => x.TableName.ShouldEqual("xxx"));
}

[Test]
public void ShouldSetParentForeignKeyProperty()
{
Convention(x => x.Key.ForeignKey("xxx"));

VerifyModel(x => x.Key.ForeignKey.ShouldEqual("xxx"));
}

[Test]
public void ShouldSetChildForeignKeyProperty()
{
Convention(x => x.Relationship.ForeignKey("xxx"));

VerifyModel(x => ((ManyToManyMapping)x.Relationship).ForeignKey.ShouldEqual("xxx"));
}

#region Helpers

private void Convention(Action<IManyToManyCollectionInstance> convention)
Expand Down
Expand Up @@ -196,6 +196,26 @@ public void TableNameShouldntBeOverwritten()
VerifyModel(x => x.TableName.ShouldEqual("table"));
}

[Test]
public void ParentForeignKeyConstraintNameShouldntBeOverwritten()
{
Mapping(x => x.Children, x => x.ForeignKeyConstraintNames("parent", "child"));

Convention(x => x.Key.ForeignKey("xxx"));

VerifyModel(x => x.Key.ForeignKey.ShouldEqual("parent"));
}

[Test]
public void ChildForeignKeyConstraintNameShouldntBeOverwritten()
{
Mapping(x => x.Children, x => x.ForeignKeyConstraintNames("parent", "child"));

Convention(x => x.Relationship.ForeignKey("xxx"));

VerifyModel(x => ((ManyToManyMapping)x.Relationship).ForeignKey.ShouldEqual("child"));
}

#region Helpers

private void Convention(Action<IManyToManyCollectionInstance> convention)
Expand Down
Expand Up @@ -144,5 +144,10 @@ public abstract class BaseModelFixture
{
return new ModelTester<StoredProcedurePart, StoredProcedureMapping>(() => new StoredProcedurePart(null, null), x => x.GetStoredProcedureMapping());
}

protected ModelTester<NaturalIdPart<T>, NaturalIdMapping> NaturalId<T>()
{
return new ModelTester<NaturalIdPart<T>, NaturalIdMapping>(() => new NaturalIdPart<T>(), x => ((INaturalIdMappingProvider)x).GetNaturalIdMapping());
}
}
}
@@ -0,0 +1,61 @@
using System.Linq;
using FluentNHibernate.Mapping;
using FluentNHibernate.MappingModel;
using NUnit.Framework;
using FluentNHibernate.Testing.DomainModel.Mapping;

namespace FluentNHibernate.Testing.FluentInterfaceTests
{
[TestFixture]
public class NaturalIdPartTests : BaseModelFixture
{
[Test]
public void ShouldMapProperty()
{
NaturalId<PropertyTarget>()
.Mapping(m => m.Property(x => x.DecimalProperty))
.ModelShouldMatch(x => x.Properties.Single().Name.ShouldEqual("DecimalProperty"));
}

[Test]
public void ShouldMapPropertyWithColumnName()
{
NaturalId<PropertyTarget>()
.Mapping(m => m.Property(x => x.DecimalProperty, "CustomCol"))
.ModelShouldMatch(x => x.Properties.Single().Columns.Single().Name.ShouldEqual("CustomCol"));
}

[Test]
public void ShouldMapReference()
{
NaturalId<PropertyTarget>()
.Mapping(m => m.Reference(x => x.Reference))
.ModelShouldMatch(x => x.ManyToOnes.Single().Name.ShouldEqual("Reference"));
}

[Test]
public void ShouldMapReferenceWithColumnName()
{
NaturalId<PropertyTarget>()
.Mapping(m => m.Reference(x => x.Reference, "CustomCol"))
.ModelShouldMatch(x => x.ManyToOnes.Single().Columns.Single().Name.ShouldEqual("CustomCol"));
}

[Test]
public void ShouldSetReadOnly()
{
NaturalId<PropertyTarget>()
.Mapping(m => m.ReadOnly())
.ModelShouldMatch(x => x.Mutable.ShouldBeFalse());
}

[Test]
public void ShouldSetNotReadOnly()
{
NaturalId<PropertyTarget>()
.Mapping(m => m.Not.ReadOnly())
.ModelShouldMatch(x => x.Mutable.ShouldBeTrue());
}

}
}
2 changes: 2 additions & 0 deletions src/FluentNHibernate.Testing/FluentNHibernate.Testing.csproj
Expand Up @@ -456,6 +456,8 @@
<Compile Include="ConventionsTests\DefaultCascadeHelperTests.cs" />
<Compile Include="ConventionsTests\DefaultLazyHelperTests.cs" />
<Compile Include="ConventionsTests\ProxyHelperTests.cs" />
<Compile Include="FluentInterfaceTests\NaturalIdPartTests.cs" />
<Compile Include="MappingModel\Output\XmlNaturalIdWriterTester.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="..\..\tools\NHibernate\NHibernate.ByteCode.Castle.dll">
Expand Down
Expand Up @@ -63,6 +63,14 @@ public void ShouldWriteCollectionTypeAttribute()
testHelper.VerifyAll(writer);
}

[Test]
public void ShouldNotWriteCollectionTypeWhenEmpty()
{
var bagMapping = new ArrayMapping { CollectionType = TypeReference.Empty };
writer.VerifyXml(bagMapping)
.DoesntHaveAttribute("collection-type");
}

[Test]
public void ShouldWriteFetchAttribute()
{
Expand Down
Expand Up @@ -63,6 +63,14 @@ public void ShouldWriteCollectionTypeAttribute()
testHelper.VerifyAll(writer);
}

[Test]
public void ShouldNotWriteCollectionTypeWhenEmpty()
{
var bagMapping = new BagMapping {CollectionType = TypeReference.Empty};
writer.VerifyXml(bagMapping)
.DoesntHaveAttribute("collection-type");
}

[Test]
public void ShouldWriteFetchAttribute()
{
Expand Down
Expand Up @@ -240,6 +240,17 @@ public void ShouldWriteId()
.Element("id").Exists();
}

[Test]
public void ShouldWriteNaturalId()
{
var mapping = new ClassMapping();

mapping.NaturalId = new NaturalIdMapping();

writer.VerifyXml(mapping)
.Element("natural-id").Exists();
}

[Test]
public void ShouldWriteCompositeId()
{
Expand Down
Expand Up @@ -68,6 +68,14 @@ public void ShouldWriteCollectionTypeAttribute()
testHelper.VerifyAll(writer);
}

[Test]
public void ShouldNotWriteCollectionTypeWhenEmpty()
{
var bagMapping = new ListMapping { CollectionType = TypeReference.Empty };
writer.VerifyXml(bagMapping)
.DoesntHaveAttribute("collection-type");
}

[Test]
public void ShouldWriteFetchAttribute()
{
Expand Down
Expand Up @@ -68,6 +68,14 @@ public void ShouldWriteCollectionTypeAttribute()
testHelper.VerifyAll(writer);
}

[Test]
public void ShouldNotWriteCollectionTypeWhenEmpty()
{
var bagMapping = new MapMapping { CollectionType = TypeReference.Empty };
writer.VerifyXml(bagMapping)
.DoesntHaveAttribute("collection-type");
}

[Test]
public void ShouldWriteFetchAttribute()
{
Expand Down
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using FluentNHibernate.MappingModel;
using FluentNHibernate.MappingModel.Output;
using FluentNHibernate.Testing.Testing;

namespace FluentNHibernate.Testing.MappingModel.Output
{
[TestFixture]
public class XmlNaturalIdWriterTester
{
private IXmlWriter<NaturalIdMapping> writer;

[SetUp]
public void GetWriterFromContainer()
{
var container = new XmlWriterContainer();
writer = container.Resolve<IXmlWriter<NaturalIdMapping>>();
}

[Test]
public void ShouldWriteMutableAttribute()
{
var testHelper = new XmlWriterTestHelper<NaturalIdMapping>();

testHelper.Check(x => x.Mutable, true).MapsToAttribute("mutable");
testHelper.VerifyAll(writer);
}

[Test]
public void ShouldWriteProperties()
{
var mapping = new NaturalIdMapping();
mapping.AddProperty(new PropertyMapping());

writer.VerifyXml(mapping)
.Element("property").Exists();
}

[Test]
public void ShouldWriteManyToOnes()
{
var mapping = new NaturalIdMapping();
mapping.AddReference(new ManyToOneMapping());

writer.VerifyXml(mapping)
.Element("many-to-one").Exists();
}

}
}
Expand Up @@ -63,6 +63,14 @@ public void ShouldWriteCollectionTypeAttribute()
testHelper.VerifyAll(writer);
}

[Test]
public void ShouldNotWriteCollectionTypeWhenEmpty()
{
var bagMapping = new SetMapping { CollectionType = TypeReference.Empty };
writer.VerifyXml(bagMapping)
.DoesntHaveAttribute("collection-type");
}

[Test]
public void ShouldWriteFetchAttribute()
{
Expand Down
37 changes: 37 additions & 0 deletions src/FluentNHibernate.Testing/Utils/TypeReferenceEqualityTests.cs
@@ -1,3 +1,4 @@
using System;
using FluentNHibernate.MappingModel;
using NUnit.Framework;

Expand Down Expand Up @@ -102,5 +103,41 @@ public void EqualsReferenceOnLeftDifferentTypeOnRightShouldNotBeEqual()
{
new TypeReference(typeof(string)).Equals(typeof(int)).ShouldBeFalse();
}

[Test]
public void EmptyShouldEqualEmpty()
{
TypeReference.Empty.Equals(TypeReference.Empty).ShouldBeTrue();
}

[Test]
public void EmptyShouldNotEqualNullTypeReference()
{
var nullTypeReference = (TypeReference)null;
TypeReference.Empty.Equals(nullTypeReference).ShouldBeFalse();
}

[Test]
public void EmptyShouldNotEqualNullType()
{
var nullType = (Type)null;
TypeReference.Empty.Equals(nullType).ShouldBeFalse();
}

[Test]
public void EmptyShouldNotEqualIntTypeReference()
{
var intTypeReference = new TypeReference(typeof(int));
TypeReference.Empty.Equals(intTypeReference).ShouldBeFalse();
}

[Test]
public void IntTypeReferenceShouldNotEqualEmpty()
{
var intTypeReference = new TypeReference(typeof(int));
intTypeReference.Equals(TypeReference.Empty).ShouldBeFalse();
}


}
}
15 changes: 15 additions & 0 deletions src/FluentNHibernate/Cfg/AutoMappingsContainer.cs
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using FluentNHibernate.Automapping;
using NHibernate.Cfg;
using System.IO;

namespace FluentNHibernate.Cfg
{
Expand All @@ -13,6 +14,7 @@ public class AutoMappingsContainer : IEnumerable<AutoPersistenceModel>
{
private readonly IList<AutoPersistenceModel> mappings = new List<AutoPersistenceModel>();
private string exportPath;
private TextWriter exportTextWriter;

internal AutoMappingsContainer()
{}
Expand Down Expand Up @@ -50,6 +52,16 @@ public AutoMappingsContainer ExportTo(string path)
return this;
}

/// <summary>
/// Sets the text writer to write the generated mappings to.
/// </summary>
/// <returns>Fluent mappings configuration</returns>
public AutoMappingsContainer ExportTo(TextWriter textWriter)
{
exportTextWriter = textWriter;
return this;
}

/// <summary>
/// Gets whether any mappings were added
/// </summary>
Expand All @@ -66,6 +78,9 @@ internal void Apply(Configuration cfg)
if (!string.IsNullOrEmpty(exportPath))
mapping.WriteMappingsTo(exportPath);

if (exportTextWriter != null)
mapping.WriteMappingsTo(exportTextWriter);

mapping.Configure(cfg);
}
}
Expand Down

0 comments on commit d2f3887

Please sign in to comment.