Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NHibernateRepositoryConfigurer
- {
- // Main configuration used while building the session factory.
- public FluentConfiguration Configuration { get; private set; }
- public ISessionFactory SessionFactory { get; private set; }
- private readonly NHibernateRepositoryConfiguration repositoryConfiguration;
- public NHibernateRepositoryConfigurer()
- {
- repositoryConfiguration = new NHibernateRepositoryConfiguration();
- Configure();
- }
- public NHibernateRepositoryConfigurer(NHibernateRepositoryConfiguration repositoryConfiguration)
- {
- this.repositoryConfiguration = repositoryConfiguration;
- Configure();
- }
- private void Configure()
- {
- var autoPersistenceModel = AutoMap.Assemblies(repositoryConfiguration.Assemblies)
- .Where(repositoryConfiguration.AutomappingExpression);
- Configuration = Fluently.Configure()
- // See the corresponding basic configuration. Defaulted to in-memory
- // SQLite database with SQL log enabled.
- .Database(repositoryConfiguration.PersistenceConfigurer)
- .Mappings(x =>
- {
- x.AutoMappings.Add(autoPersistenceModel);
- foreach (var assembly in repositoryConfiguration.Assemblies)
- {
- x.FluentMappings.AddFromAssembly(assembly);
- }
- });
- // Deploy the corresponding session factory (one per configurer instance).
- SessionFactory = Configuration.BuildSessionFactory();
- }
- }
- public class NHibernateRepositoryConfiguration
- {
- public Assembly[] Assemblies =
- new[] { Assembly.GetAssembly(typeof(IRepositoryEntity)) };
- public Func<Type, bool> AutomappingExpression =
- (x => typeof(IRepositoryEntity).IsAssignableFrom(x));
- public IPersistenceConfigurer PersistenceConfigurer =
- SQLiteConfiguration.Standard.InMemory().ShowSql();
- }
- public class NHibernateRepositoryFactory
- {
- public FluentConfiguration Configuration { get; set; }
- public ISessionFactory RetrieveSessionFactory()
- {
- var autoPersistenceModel = AutoMap.AssemblyOf<RepositoryEntity>()
- .Where(x => !x.IsGenericType && x.IsSubclassOf(typeof(RepositoryEntity)));
- Configuration = Fluently.Configure()
- .Database(SQLiteConfiguration.Standard.ShowSql().InMemory)
- .Mappings(x =>
- {
- x.FluentMappings.AddFromAssemblyOf<RepositoryEntity>();
- x.AutoMappings.Add(autoPersistenceModel);
- });
- return Configuration.BuildSessionFactory();
- }
- }
- using ISampleUnitProxy = IUnitProxy<SampleUnitDescriptor>;
- using ISampleUnit = IUnit<SampleUnitDescriptor, Decimal>;
- using SampleUnitProxy = BinaryUnitProxy<SampleUnitDescriptor>;
- using SampleUnit = BinaryUnit<SampleUnitDescriptor, Decimal>;
- public class RepositoryTestingPackage
- {
- public IQueryable<ISampleUnitProxy> SampleProxySequence { get; private set; }
- public IQueryable<SampleNonUnitEntity> SampleNonUnitEntitySequence { get; private set; }
- public Mock<IRepository> RepositoryMock { get; private set; }
- // Set of arbitrary timestamps and values for testing.
- public String Name1 = "Name1";
- public String Name2 = "Name2";
- public Decimal Value1 = (Decimal) 25.0;
- public Decimal Value2 = (Decimal) 48.0;
- public DateTime Time1 = new DateTime(2000, 1, 1);
- public DateTime Time2 = new DateTime(2000, 2, 1);
- public RepositoryTestingPackage()
- {
- SampleNonUnitEntitySequence = new List<SampleNonUnitEntity>
- {
- new SampleNonUnitEntity
- {
- Value = Value1
- },
- new SampleNonUnitEntity
- {
- Value = Value2
- }
- }.AsQueryable();
- // Setup the unit proxy sequence to be used in the tests.
- // Note that the used repository mock only handles the retrieval
- // of the entities of the 'ITestUnitProxy' type.
- SampleProxySequence = new List<ISampleUnitProxy>
- {
- new SampleUnit
- {
- Descriptor = new SampleUnitDescriptor { Name = Name1, Timestamp = Time1 },
- Value = Value1
- }.ToProxy(),
- new SampleUnit
- {
- Descriptor = new SampleUnitDescriptor { Name = Name2, Timestamp = Time2 },
- Value = Value2
- }.ToProxy()
- }.AsQueryable();
- RepositoryMock = new Mock<IRepository>();
- RepositoryMock.Setup(x => x.RetrieveEntities<ISampleUnitProxy>())
- .Returns(SampleProxySequence);
- RepositoryMock.Setup(x => x.RetrieveEntities(
- // Handle the variant of the 'RetrieveEntities' function that receives
- // a lambda predicate and filters the corresponding elements.
- It.IsAny<Expression<Func<ISampleUnitProxy, bool>>>()))
- // The result is the 'SampleProxySequence' sequence filtered using the
- // corresponding expression.
- .Returns(
- ((Expression<Func<ISampleUnitProxy, bool>> expression) =>
- SampleProxySequence.Where(expression)));
- // We do not provide mock implementations for the corresponding 'Add' and 'Remove'
- // methods, because things could get complicated here and actually we only have
- // to check that these methods are getting called, but not their effect.
- }
- }
- using ISampleUnitProxy = IUnitProxy<SampleUnitDescriptor>;
- using ISampleUnit = IUnit<SampleUnitDescriptor, Decimal>;
- using SampleUnitProxy = BinaryUnitProxy<SampleUnitDescriptor>;
- using SampleUnit = BinaryUnit<SampleUnitDescriptor, Decimal>;
- public abstract class RepositoryTester
- {
- protected readonly RepositoryTestingPackage RepositoryTestingPackage;
- protected readonly IRepository Repository;
- protected RepositoryTester(IRepository repository)
- {
- RepositoryTestingPackage = new RepositoryTestingPackage();
- Repository = repository;
- repository.AddEntities(RepositoryTestingPackage.SampleProxySequence);
- repository.AddEntities(RepositoryTestingPackage.SampleNonUnitEntitySequence);
- }
- [Fact]
- public void RetrieveAllEntities()
- {
- var result = Repository.RetrieveEntities<ISampleUnitProxy>();
- Assert.Equal(RepositoryTestingPackage.SampleProxySequence.Count(), result.Count());
- }
- [Fact]
- public void RetrieveEntitiesByExpression()
- {
- var result = Repository.RetrieveEntities<ISampleUnitProxy>(
- x => x.Descriptor.Name == RepositoryTestingPackage.Name1);
- Assert.Equal(1, result.Count());
- Assert.Equal(RepositoryTestingPackage.Name1, result.First().Descriptor.Name);
- }
- [Fact]
- public void RetrieveEntitiesWithDifferentTypes()
- {
- var result1 = Repository.RetrieveEntities<ISampleUnitProxy>();
- var result2 = Repository.RetrieveEntities<SampleNonUnitEntity>();
- Assert.Equal(2, result1.Count());
- Assert.Equal(2, result2.Count());
- }
- [Fact]
- public void AddNewEntity()
- {
- // Assumed to be new for the test value sequence.
- var unit = new SampleUnit
- {
- Descriptor = new SampleUnitDescriptor
- {
- Name = "New",
- Timestamp = RepositoryTestingPackage.Time1
- },
- Value = RepositoryTestingPackage.Value1
- }.ToProxy();
- Repository.AddEntities(Enumerable.Repeat(unit, 1).AsQueryable());
- var result = Repository.RetrieveEntities<ISampleUnitProxy>();
- Assert.Equal(3, result.Count());
- }
- [Fact]
- public void UpdateEntity()
- {
- var sequence = Repository.RetrieveEntities<ISampleUnitProxy>();
- var unit = sequence.Where(x => x.Descriptor.Name.Equals(RepositoryTestingPackage.Name1));
- // Update the corresponding value and commit it to the repository.
- unit.First().Descriptor.Name = "Changed";
- Repository.AddEntities(unit);
- var result = Repository.RetrieveEntities<ISampleUnitProxy>(x => x.Descriptor.Name == "Changed");
- Assert.Equal(1, result.Count());
- }
- [Fact]
- public void UpdateEntityThroughProxy()
- {
- ISampleUnitProxy unitProxy = Repository.RetrieveEntities<ISampleUnitProxy>(
- x => x.Descriptor.Name == RepositoryTestingPackage.Name1).First();
- var unit = unitProxy.ToUnit<Decimal>();
- // Update the corresponding value and commit it to the repository.
- // Unit is transformed to proxy before the actual commit.
- unit.Value = 196;
- Repository.AddEntities(unit.ToProxy());
- // Ensure that the value was updated and no copies were done.
- var result = Repository.RetrieveEntities<ISampleUnitProxy>();
- Assert.Equal(2, result.Count());
- var updatedUnit = result.ToList().Where(x => x.ToUnit<Decimal>().Value == ((Decimal) 196.0));
- Assert.Equal(1, updatedUnit.Count());
- }
- [Fact]
- public void RemoveAllEntities()
- {
- Repository.RemoveEntities<ISampleUnitProxy>();
- var result = Repository.RetrieveEntities<ISampleUnitProxy>();
- Assert.Equal(0, result.Count());
- }
- [Fact]
- public void RemoveSelectedEntities()
- {
- var sequence = Repository.RetrieveEntities<ISampleUnitProxy>();
- Repository.RemoveEntities(sequence.First(), sequence.First());
- var result = Repository.RetrieveEntities<ISampleUnitProxy>();
- Assert.Equal(1, result.Count());
- }
- [Fact]
- public void RemoveEntitiesByExpression()
- {
- Repository.RemoveEntities<ISampleUnitProxy>(
- x => x.Descriptor.Timestamp == RepositoryTestingPackage.Time1);
- var result = Repository.RetrieveEntities<ISampleUnitProxy>();
- Assert.Equal(1, result.Count());
- Assert.Equal(RepositoryTestingPackage.Time2, result.First().Descriptor.Timestamp);
- }
- }
- public class MemoryRepositoryTests : RepositoryTester
- {
- public MemoryRepositoryTests() : base(new MemoryRepository()) {}
- }
- public class NHibernateRepositoryTests : RepositoryTester
- {
- // NHibernate repository configuration used exclusively for testing.
- // Differs from the default repository configuration in terms of the assembly
- // used for entity automapping.
- private static readonly NHibernateRepositoryConfiguration RepositoryConfiguration =
- new NHibernateRepositoryConfiguration
- {
- Assemblies = new[] { Assembly.GetExecutingAssembly() }
- };
- public NHibernateRepositoryTests()
- : base(new NHibernateRepository(new NHibernateRepositoryConfigurer(RepositoryConfiguration))) {}
- [Fact]
- public void VerifyTestingMappings()
- {
- var repositoryConfigurer = new NHibernateRepositoryConfigurer(RepositoryConfiguration);
- Assert.DoesNotThrow(
- () =>
- {
- using (var session = repositoryConfigurer.SessionFactory.OpenSession())
- {
- new SchemaExport(repositoryConfigurer.Configuration.BuildConfiguration())
- .Execute(true, true, false, session.Connection, null);
- new PersistenceSpecification<SampleUnitDescriptor>(session)
- .CheckProperty(x => x.Id, 1)
- .CheckProperty(x => x.Name, "Name1")
- .CheckProperty(x => x.Timestamp, new DateTime(2000, 10, 10))
- .VerifyTheMappings();
- new PersistenceSpecification<SampleNonUnitEntity>(session)
- .CheckProperty(x => x.Id, 1)
- .CheckProperty(x => x.Value, (Decimal) 20.0)
- .VerifyTheMappings();
- }
- });
- }
- internal class EntityWithoutPersister : RepositoryEntity { }
- [Fact]
- public void RetrieveImpossibleType()
- {
- Assert.Throws<MappingException>(
- () => Repository.RetrieveEntities<EntityWithoutPersister>().Count());
- }
- }
- public sealed class BinaryUnitProxyMapping : ClassMap<BinaryUnitProxy<SampleUnitDescriptor>>
- {
- public BinaryUnitProxyMapping()
- {
- Id(x => x.Id);
- References(x => x.Descriptor).Cascade.All();
- Map(x => x.SerializedValue);
- }
- }
- public class SampleUnitDescriptor : RepositoryEntity, IEquatable<SampleUnitDescriptor>
- {
- public virtual String Name { get; set; }
- public virtual DateTime Timestamp { get; set; }
- #region Autogenerated equality checking methods
- public virtual bool Equals(SampleUnitDescriptor other)
- {
- if (ReferenceEquals(null, other)) return false;
- if (ReferenceEquals(this, other)) return true;
- return Equals(other.Name, Name) && other.Timestamp.Equals(Timestamp);
- }
- public override bool Equals(Object other)
- {
- if (ReferenceEquals(null, other)) return false;
- if (ReferenceEquals(this, other)) return true;
- if (other.GetType() != typeof(SampleUnitDescriptor)) return false;
- return Equals((SampleUnitDescriptor)other);
- }
- public override int GetHashCode()
- {
- unchecked
- {
- return ((Name != null ? Name.GetHashCode() : 0) * 397) ^ Timestamp.GetHashCode();
- }
- }
- public static bool operator ==(SampleUnitDescriptor left, SampleUnitDescriptor right)
- {
- return Equals(left, right);
- }
- public static bool operator !=(SampleUnitDescriptor left, SampleUnitDescriptor right)
- {
- return !Equals(left, right);
- }
- #endregion
- }
- // using SampleUnitValue = Decimal;
- public class SampleNonUnitEntity : RepositoryEntity
- {
- public virtual Decimal Value { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement