public void ShouldSetServerUriAndDatabaseName() { var settings = ConfigureCouchDude.With().ServerUri("http://example.com").DefaultDatabaseName("db1").CreateSettings(); Assert.Equal(new Uri("http://example.com"), settings.ServerUri); Assert.Equal("db1", settings.DefaultDatabaseName); }
public void ShouldSemiInheritAspectsOfConfigurationFromPreviousDeclaration() { var settings = ConfigureCouchDude.With() .ServerUri("http://example.com").DefaultDatabaseName("db1") .MappingEntities() .FromAssembly("CouchDude.Tests") .Implementing <IEntity>() .WhenDocumentType(t => "__" + t.Name.ToLower()) .MappingEntities() .FromAssembly("CouchDude.Tests") .Where(t => t.Name.StartsWith("Entity")) .WhenDocumentType(t => "__" + t.Name.ToLower() + "[{}]") .MappingEntities() .FromAssemblyOf <Entity>() .Where(t => t.Name == "Entity") .WhenIdMember(t => t.GetProperty("Name")) .MappingEntities() .FromAssembly("CouchDude.Tests") .InheritedFrom <Entity>() .WhenRevisionMember(t => t.GetProperty("Id")) .CreateSettings(); var entityConfig = settings.GetConfig(typeof(Entity)); Assert.Equal("__entity[{}]", entityConfig.DocumentType); Assert.Equal("e0b8400a23158b046a", entityConfig.GetId(new Entity { Name = "e0b8400a23158b046a" })); Assert.Equal("4a227e99a2de41689", entityConfig.GetRevision(new Entity { Id = "4a227e99a2de41689" })); }
public void ShouldThrowOnNonePropertyOrFieldRevisionMember() { Assert.Throws <ConfigurationException>(() => ConfigureCouchDude.With() .ServerUri("http://example.com").DefaultDatabaseName("db1") .MappingEntities() .FromAssemblyOf <Entity>() .Implementing <IEntity>() .WhenRevisionMember(entityType => (MemberInfo)entityType.GetMethod("DoStuff")) .CreateSettings()); }
public void ShouldThrowOnNullIdMember() { Assert.Throws <ConfigurationException>(() => ConfigureCouchDude.With() .ServerUri("http://example.com").DefaultDatabaseName("db1") .MappingEntities() .FromAssemblyOf <Entity>() .Implementing <IEntity>() .WhenIdMember(entityType => null) .CreateSettings()); }
public void ShouldNotThrowOnNullRevMember() { Assert.DoesNotThrow(() => ConfigureCouchDude.With() .ServerUri("http://example.com").DefaultDatabaseName("db1") .MappingEntities() .FromAssemblyOf <Entity>() .Implementing <IEntity>() .WhenRevisionMember(entityType => null) .CreateSettings()); }
public void ShouldThrowOnNoneInstanceIdMember() { Assert.Throws <ConfigurationException>(() => ConfigureCouchDude.With() .ServerUri("http://example.com").DefaultDatabaseName("db1") .MappingEntities() .FromAssemblyOf <Entity>() .InheritedFrom <Entity>() .WhenIdMember(entityType => (MemberInfo)entityType.GetField("OkResponse")) .CreateSettings() ); }
public void ShouldExplictlyRegisterTypes() { Settings settings = ConfigureCouchDude.With() .ServerUri("http://example.com").DefaultDatabaseName("db1") .MappingEntitiy <Entity>() .CreateSettings(); var entityConfig = settings.TryGetConfig(typeof(Entity)); var simpleEntityWithoutRevisionConfig = settings.TryGetConfig(typeof(EntityWithoutRevision)); Assert.NotNull(entityConfig); Assert.Null(simpleEntityWithoutRevisionConfig); }
public void ShouldSetDocumentIdToEntityIdConversion() { Settings settings = ConfigureCouchDude.With() .ServerUri("http://example.com").DefaultDatabaseName("db1") .MappingEntities() .FromAssemblyOf <Entity>() .Implementing <IEntity>() .TranslatingDocumentIdToEntityIdAs((documentId, documentType, entityType) => "Document#" + documentId) .CreateSettings(); var entityConfig = settings.TryGetConfig(typeof(Entity)); Assert.Equal("Document#42", entityConfig.ConvertDocumentIdToEntityId("42")); }
public void ShouldSetDocumentTypePolicy() { Settings settings = ConfigureCouchDude.With() .ServerUri("http://example.com").DefaultDatabaseName("db1") .MappingEntities() .FromAssemblyOf <Entity>() .Implementing <IEntity>() .WhenDocumentType(t => "_" + Char.ToLower(t.Name[0]) + t.Name.Substring(1)) .CreateSettings(); var entityConfig = settings.TryGetConfig(typeof(Entity)); Assert.Equal("_entity", entityConfig.DocumentType); }
public void ShouldProvideAbilityToPointToAssemblyByName() { Settings settings = ConfigureCouchDude.With() .ServerUri("http://example.com").DefaultDatabaseName("db1") .MappingEntities() .FromAssembly("CouchDude.Tests") .Where(t => t.GetInterfaces().Any(i => i.Name == "IEntity")) .CreateSettings(); var entityConfig = settings.TryGetConfig(typeof(Entity)); var simpleEntityWithoutRevisionConfig = settings.TryGetConfig(typeof(EntityWithoutRevision)); Assert.NotNull(entityConfig); Assert.NotNull(simpleEntityWithoutRevisionConfig); }
public void ShouldIterateOverAssemblyTypesRegisteringUsingProvidedPredicate() { Settings settings = ConfigureCouchDude.With() .ServerUri("http://example.com").DefaultDatabaseName("db1") .MappingEntities() .FromAssemblyOf <Entity>() .Where(t => t.Name.StartsWith("Entity")) .CreateSettings(); var entityConfig = settings.TryGetConfig(typeof(Entity)); var simpleEntityWithoutRevisionConfig = settings.TryGetConfig(typeof(EntityWithoutRevision)); Assert.NotNull(entityConfig); Assert.NotNull(simpleEntityWithoutRevisionConfig); }
public void ShouldDirectlySetCustomEntityConfig() { var customConfig = Mock.Of <IEntityConfig>( c => c.EntityType == typeof(Entity) && c.DocumentType == "simpleEntity"); Settings settings = ConfigureCouchDude.With() .ServerUri("http://example.com").DefaultDatabaseName("db1") .MappingEntities() .FromAssemblyOf <Entity>() .InheritedFrom <Entity>() .WithCustomConfig(t => customConfig) .CreateSettings(); var entityConfig = settings.TryGetConfig(typeof(Entity)); Assert.Same(customConfig, entityConfig); }
public void ShouldSaveAndQueryDerivedEntities() { var sessionFactory = ConfigureCouchDude.With() .ServerUri("http://127.0.0.1:5984") .DefaultDatabaseName("testdb") .MappingEntities() .FromAssemblyOf <Animal>() .InheritedFrom <Animal>() // It's crutual for now to ensure whole inheritance heirarchy have same id convertion rules .TranslatingEntityIdToDocumentIdAs((id, type, documentType) => id) .TranslatingDocumentIdToEntityIdAs((id, type, entityType) => id) .CreateSettings() .CreateSessionFactory(); var prefix = Guid.NewGuid() + "."; var musa = new Cat { Id = prefix + "0", Name = "Musa" }; var fluffy = new ViciousCat { Id = prefix + "1", Name = "Fluffy" }; var zoidberg = new Chinchilla { Id = prefix + "2", Name = "Dr. Zoidberg" }; using (var session = sessionFactory.CreateSession()) { session.RawApi.Synchronously.Create(throwIfExists: false); session.Save <Animal>(musa, fluffy, zoidberg); session.SaveChanges(); } using (var session = sessionFactory.CreateSession()) { var animals = session.Synchronously .Query <Animal>(new ViewQuery { ViewName = "_all_docs", StartKey = musa.Id, EndKey = zoidberg.Id, IncludeDocs = true }) .ToDictionary(a => a.Id, a => a); Assert.IsType <Cat>(animals[musa.Id]); Assert.IsType <ViciousCat>(animals[fluffy.Id]); Assert.IsType <Chinchilla>(animals[zoidberg.Id]); } }
public void ShouldSaveAndLoadDerivedEntities() { var sessionFactory = ConfigureCouchDude.With() .ServerUri("http://127.0.0.1:5984") .DefaultDatabaseName("testdb") .MappingEntities() .FromAssemblyOf <Animal>() .InheritedFrom <Animal>() // It's crutual for now to ensure whole inheritance heirarchy have same id convertion rules .TranslatingEntityIdToDocumentIdAs((id, type, documentType) => id) .TranslatingDocumentIdToEntityIdAs((id, type, entityType) => id) .CreateSettings() .CreateSessionFactory(); var musa = new Cat { Name = "Musa" }; var fluffy = new ViciousCat { Name = "Fluffy" }; var zoidberg = new Chinchilla { Name = "Dr. Zoidberg" }; using (var session = sessionFactory.CreateSession()) { session.RawApi.Synchronously.Create(throwIfExists: false); session.Save <Animal>(musa, fluffy, zoidberg); session.SaveChanges(); } using (var session = sessionFactory.CreateSession()) { var loadedMusa = session.Synchronously.Load <Animal>(musa.Id); Assert.IsType <Cat>(loadedMusa); var loadedFluffy = session.Synchronously.Load <Animal>(fluffy.Id); Assert.IsType <ViciousCat>(loadedFluffy); var loadedZoidberg = session.Synchronously.Load <Animal>(zoidberg.Id); Assert.IsType <Chinchilla>(loadedZoidberg); } }
public void ShouldSetRevisionMemberPolicy() { Settings settings = ConfigureCouchDude.With() .ServerUri("http://example.com").DefaultDatabaseName("db1") .MappingEntities() .FromAssemblyOf <Entity>() .Implementing <IEntity>() .WhenRevisionMember(entityType => (MemberInfo)entityType.GetProperty("Name")) .CreateSettings(); var entityConfig = settings.TryGetConfig(typeof(Entity)); var simpleEntity = new Entity { Name = "Alex" }; Assert.True(entityConfig.IsRevisionPresent); Assert.Equal("Alex", entityConfig.GetRevision(simpleEntity)); entityConfig.SetRevision(simpleEntity, "John"); Assert.Equal("John", simpleEntity.Name); }
public void ShouldPickLastEntityConfigIfMultiplyHaveRegistred() { var customConfigA = Mock.Of <IEntityConfig>( c => c.EntityType == typeof(Entity) && c.DocumentType == "simpleEntity"); var customConfigB = Mock.Of <IEntityConfig>( c => c.EntityType == typeof(Entity) && c.DocumentType == "simpleEntity"); var settings = ConfigureCouchDude.With() .ServerUri("http://example.com").DefaultDatabaseName("db1") .MappingEntities() .FromAssemblyOf <Entity>() .Where(t => t.Name == "Entity") .WithCustomConfig(t => customConfigA) .MappingEntities() .FromAssembly("CouchDude.Tests") .InheritedFrom <Entity>() .WithCustomConfig(t => customConfigB) .CreateSettings(); var entityConfig = settings.GetConfig(typeof(Entity)); Assert.Same(customConfigB, entityConfig); }
public void ShouldThrowOnIncompleteInitialization() { Assert.Throws <ConfigurationException>( () => ConfigureCouchDude.With().ServerUri("http://example.com").CreateSettings()); Assert.Throws <ConfigurationException>(() => ConfigureCouchDude.With().DefaultDatabaseName("db1").CreateSettings()); }