diff --git a/src/MongoFramework/Attributes/MappingAdapterAttribute.cs b/src/MongoFramework/Attributes/MappingAdapterAttribute.cs index 08fc9f11..8d996e3f 100644 --- a/src/MongoFramework/Attributes/MappingAdapterAttribute.cs +++ b/src/MongoFramework/Attributes/MappingAdapterAttribute.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; using MongoFramework.Infrastructure.Mapping; namespace MongoFramework.Attributes diff --git a/src/MongoFramework/Attributes/RuntimeTypeDiscoveryAttribute.cs b/src/MongoFramework/Attributes/RuntimeTypeDiscoveryAttribute.cs index 5d7910ed..d559b2de 100644 --- a/src/MongoFramework/Attributes/RuntimeTypeDiscoveryAttribute.cs +++ b/src/MongoFramework/Attributes/RuntimeTypeDiscoveryAttribute.cs @@ -2,7 +2,7 @@ namespace MongoFramework.Attributes { - [AttributeUsage(AttributeTargets.Class )] + [AttributeUsage(AttributeTargets.Class)] public class RuntimeTypeDiscoveryAttribute : Attribute { diff --git a/src/MongoFramework/Bson/BsonDiff.cs b/src/MongoFramework/Bson/BsonDiff.cs index 8028e860..3e54a599 100644 --- a/src/MongoFramework/Bson/BsonDiff.cs +++ b/src/MongoFramework/Bson/BsonDiff.cs @@ -1,6 +1,6 @@ -using MongoDB.Bson; -using System; +using System; using System.Linq; +using MongoDB.Bson; namespace MongoFramework.Bson { diff --git a/src/MongoFramework/IMongoDbBucketSet.cs b/src/MongoFramework/IMongoDbBucketSet.cs index 4d96e66e..262619cf 100644 --- a/src/MongoFramework/IMongoDbBucketSet.cs +++ b/src/MongoFramework/IMongoDbBucketSet.cs @@ -3,7 +3,7 @@ namespace MongoFramework { - public interface IMongoDbBucketSet : IMongoDbSet, IQueryable> + public interface IMongoDbBucketSet : IMongoDbSet, IQueryable> where TGroup : class where TSubEntity : class { diff --git a/src/MongoFramework/IMongoDbContext.cs b/src/MongoFramework/IMongoDbContext.cs index 46ffb118..0a7fe865 100644 --- a/src/MongoFramework/IMongoDbContext.cs +++ b/src/MongoFramework/IMongoDbContext.cs @@ -3,7 +3,6 @@ using System.Threading; using System.Threading.Tasks; using MongoFramework.Infrastructure; -using MongoFramework.Infrastructure.Commands; namespace MongoFramework { @@ -19,7 +18,7 @@ public interface IMongoDbContext void SaveChanges(); Task SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken)); - + void Attach(TEntity entity) where TEntity : class; void AttachRange(IEnumerable entities) where TEntity : class; } diff --git a/src/MongoFramework/IMongoDbTenantSet.cs b/src/MongoFramework/IMongoDbTenantSet.cs index 613027b4..5ca9998c 100644 --- a/src/MongoFramework/IMongoDbTenantSet.cs +++ b/src/MongoFramework/IMongoDbTenantSet.cs @@ -1,10 +1,10 @@ using System.Linq; -namespace MongoFramework -{ +namespace MongoFramework +{ public interface IMongoDbTenantSet : IMongoDbSet where TEntity : class { new IMongoDbTenantContext Context { get; } IQueryable GetSearchTextQueryable(string search); - } -} + } +} diff --git a/src/MongoFramework/IndexType.cs b/src/MongoFramework/IndexType.cs index cd8f501e..b1b09227 100644 --- a/src/MongoFramework/IndexType.cs +++ b/src/MongoFramework/IndexType.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace MongoFramework +namespace MongoFramework { public enum IndexType { diff --git a/src/MongoFramework/Infrastructure/Commands/UpdateEntityCommand.cs b/src/MongoFramework/Infrastructure/Commands/UpdateEntityCommand.cs index 0de5505b..157209cd 100644 --- a/src/MongoFramework/Infrastructure/Commands/UpdateEntityCommand.cs +++ b/src/MongoFramework/Infrastructure/Commands/UpdateEntityCommand.cs @@ -26,8 +26,8 @@ public IEnumerable> GetModel(WriteModelOptions options) Validator.ValidateObject(entity, validationContext); var definition = EntityMapping.GetOrCreateDefinition(typeof(TEntity)); - var updateDefintion = UpdateDefinitionHelper.CreateFromDiff(EntityEntry.OriginalValues, EntityEntry.CurrentValues); - yield return new UpdateOneModel(definition.CreateIdFilterFromEntity(entity), updateDefintion); + var updateDefinition = UpdateDefinitionHelper.CreateFromDiff(EntityEntry.OriginalValues, EntityEntry.CurrentValues); + yield return new UpdateOneModel(definition.CreateIdFilterFromEntity(entity), updateDefinition); } } } diff --git a/src/MongoFramework/Infrastructure/DefinitionHelpers/UpdateDefinitionHelper.cs b/src/MongoFramework/Infrastructure/DefinitionHelpers/UpdateDefinitionHelper.cs index c3779cbf..4f9ef331 100644 --- a/src/MongoFramework/Infrastructure/DefinitionHelpers/UpdateDefinitionHelper.cs +++ b/src/MongoFramework/Infrastructure/DefinitionHelpers/UpdateDefinitionHelper.cs @@ -1,6 +1,6 @@ -using MongoDB.Bson; +using System.Linq; +using MongoDB.Bson; using MongoDB.Driver; -using System.Linq; namespace MongoFramework.Infrastructure.DefinitionHelpers { @@ -17,7 +17,7 @@ private static void ApplyDiffUpdate(BsonDocument updateDefinition, string name, var documentAProperties = documentA?.Names ?? Enumerable.Empty(); var documentBProperties = documentB?.Names ?? Enumerable.Empty(); var propertyNames = documentAProperties.Union(documentBProperties); - + var baseName = name; if (!string.IsNullOrEmpty(baseName)) { diff --git a/src/MongoFramework/Infrastructure/Diagnostics/DiagnosticCommand.cs b/src/MongoFramework/Infrastructure/Diagnostics/DiagnosticCommand.cs index b1eee5de..302f8077 100644 --- a/src/MongoFramework/Infrastructure/Diagnostics/DiagnosticCommand.cs +++ b/src/MongoFramework/Infrastructure/Diagnostics/DiagnosticCommand.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using MongoDB.Driver; -using MongoFramework.Infrastructure.Linq; namespace MongoFramework.Infrastructure.Diagnostics { diff --git a/src/MongoFramework/Infrastructure/Diagnostics/DiagnosticRunner.cs b/src/MongoFramework/Infrastructure/Diagnostics/DiagnosticRunner.cs index c9fe3e18..dce6ea74 100644 --- a/src/MongoFramework/Infrastructure/Diagnostics/DiagnosticRunner.cs +++ b/src/MongoFramework/Infrastructure/Diagnostics/DiagnosticRunner.cs @@ -1,8 +1,5 @@ using System; -using System.Collections; using System.Collections.Generic; -using System.Linq; -using System.Text; using MongoDB.Driver; using MongoFramework.Infrastructure.Linq; diff --git a/src/MongoFramework/Infrastructure/Diagnostics/IDiagnosticListener.cs b/src/MongoFramework/Infrastructure/Diagnostics/IDiagnosticListener.cs index 98f0f73b..5f2051e4 100644 --- a/src/MongoFramework/Infrastructure/Diagnostics/IDiagnosticListener.cs +++ b/src/MongoFramework/Infrastructure/Diagnostics/IDiagnosticListener.cs @@ -1,10 +1,9 @@ -using MongoFramework.Infrastructure.Diagnostics; -using System; +using System; namespace MongoFramework.Infrastructure.Diagnostics { public interface IDiagnosticListener : IObserver { - + } } diff --git a/src/MongoFramework/Infrastructure/EntityCommandStaging.cs b/src/MongoFramework/Infrastructure/EntityCommandStaging.cs index b1c3c425..1ddd9640 100644 --- a/src/MongoFramework/Infrastructure/EntityCommandStaging.cs +++ b/src/MongoFramework/Infrastructure/EntityCommandStaging.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; using MongoFramework.Infrastructure.Commands; namespace MongoFramework.Infrastructure diff --git a/src/MongoFramework/Infrastructure/EntityCommandWriter.cs b/src/MongoFramework/Infrastructure/EntityCommandWriter.cs index a09340fd..0ab20b6c 100644 --- a/src/MongoFramework/Infrastructure/EntityCommandWriter.cs +++ b/src/MongoFramework/Infrastructure/EntityCommandWriter.cs @@ -1,12 +1,12 @@ -using MongoDB.Driver; -using MongoFramework.Infrastructure.Commands; -using MongoFramework.Infrastructure.Diagnostics; -using MongoFramework.Infrastructure.Mapping; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using MongoDB.Driver; +using MongoFramework.Infrastructure.Commands; +using MongoFramework.Infrastructure.Diagnostics; +using MongoFramework.Infrastructure.Mapping; namespace MongoFramework.Infrastructure { diff --git a/src/MongoFramework/Infrastructure/EntityEntryContainer.cs b/src/MongoFramework/Infrastructure/EntityEntryContainer.cs index 2858ea9c..7002aba1 100644 --- a/src/MongoFramework/Infrastructure/EntityEntryContainer.cs +++ b/src/MongoFramework/Infrastructure/EntityEntryContainer.cs @@ -1,177 +1,177 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using MongoFramework.Bson; -using MongoFramework.Infrastructure.Mapping; +using System; +using System.Collections.Generic; +using System.Linq; +using MongoFramework.Bson; +using MongoFramework.Infrastructure.Mapping; using MongoFramework.Utilities; -namespace MongoFramework.Infrastructure -{ - public class EntityEntryContainer - { - private Dictionary> EntryLookupByType { get; } = new Dictionary>(); - - public IEnumerable Entries() - { - return EntryLookupByType.Values.SelectMany(e => e); - } - - private void RemoveEntry(EntityEntry entry) - { - if (EntryLookupByType.TryGetValue(typeof(TCollectionBase), out var entries)) - { - entries.Remove(entry); - } - } - - private void AddEntry(EntityEntry entry) - { - if (!EntryLookupByType.TryGetValue(typeof(TCollectionBase), out var entries)) - { - entries = new List(); - EntryLookupByType.Add(typeof(TCollectionBase), entries); - } - - entries.Add(entry); - } - - public EntityEntry GetEntry(TCollectionBase entity) - { - Check.NotNull(entity, nameof(entity)); - - var collectionType = typeof(TCollectionBase); - - if (EntryLookupByType.TryGetValue(collectionType, out var entries)) - { - var entityDefinition = EntityMapping.GetOrCreateDefinition(collectionType); - var entityId = entityDefinition.GetIdValue(entity); - var defaultIdValue = entityDefinition.GetDefaultId(); - foreach (var entry in entries) - { - if (Equals(entityId, defaultIdValue) && ReferenceEquals(entry.Entity, entity)) - { - return entry; - } - else - { - var entryEntityId = entityDefinition.GetIdValue(entry.Entity); - if (!Equals(entryEntityId, defaultIdValue) && entryEntityId.Equals(entityId)) - { - return entry; - } - } - } - } - - return null; +namespace MongoFramework.Infrastructure +{ + public class EntityEntryContainer + { + private Dictionary> EntryLookupByType { get; } = new Dictionary>(); + + public IEnumerable Entries() + { + return EntryLookupByType.Values.SelectMany(e => e); + } + + private void RemoveEntry(EntityEntry entry) + { + if (EntryLookupByType.TryGetValue(typeof(TCollectionBase), out var entries)) + { + entries.Remove(entry); + } } - - public EntityEntry GetEntryById(object id) - { - var collectionType = typeof(TCollectionBase); - - if (EntryLookupByType.TryGetValue(collectionType, out var entries)) - { - var entityDefinition = EntityMapping.GetOrCreateDefinition(collectionType); - - foreach (var entry in entries) - { - var entryEntityId = entityDefinition.GetIdValue(entry.Entity); - if (entryEntityId.Equals(id)) - { - return entry; - } - } - } - - return null; - } - public EntityEntry SetEntityState(TCollectionBase entity, EntityEntryState state) where TCollectionBase : class - { - Check.NotNull(entity, nameof(entity)); - - var entry = GetEntry(entity); - if (entry != null) - { - if (ReferenceEquals(entry.Entity, entity)) - { - if ((entry.State == EntityEntryState.Added && state == EntityEntryState.Deleted) || state == EntityEntryState.Detached) - { - entry.State = EntityEntryState.Detached; - RemoveEntry(entry); - } - else - { - entry.State = state; - } - } - else if (state != EntityEntryState.Detached) - { - RemoveEntry(entry); - entry = new EntityEntry(entity, typeof(TCollectionBase), state); - AddEntry(entry); - } - } - else if (state != EntityEntryState.Detached) - { - entry = new EntityEntry(entity, typeof(TCollectionBase), state); - AddEntry(entry); - } - - return entry; - } - - /// - /// Performed after saving, this resets the state of entries to align with the state in the database. - /// - public void CommitChanges() - { - foreach (var typeGroup in EntryLookupByType.Values) - { - for (var i = typeGroup.Count - 1; i >= 0; i--) - { - var entry = typeGroup[i]; - if (entry.State == EntityEntryState.Added || entry.State == EntityEntryState.Updated) - { - entry.ResetState(); - } - else if (entry.State == EntityEntryState.Deleted) - { - typeGroup.Remove(entry); - } - } - } - } - - /// - /// Performed prior to saving, this detect changes between the original and current state of entries, updating states where necessary. - /// - public void DetectChanges() - { - foreach (var typeGroup in EntryLookupByType.Values) - { - for (var i = typeGroup.Count - 1; i >= 0; i--) - { - var entry = typeGroup[i]; - if (entry.State == EntityEntryState.NoChanges || entry.State == EntityEntryState.Updated) - { - if (BsonDiff.HasDifferences(entry.OriginalValues, entry.CurrentValues)) - { - entry.State = EntityEntryState.Updated; - } - else - { - entry.State = EntityEntryState.NoChanges; - } - } - } - } - } - + + private void AddEntry(EntityEntry entry) + { + if (!EntryLookupByType.TryGetValue(typeof(TCollectionBase), out var entries)) + { + entries = new List(); + EntryLookupByType.Add(typeof(TCollectionBase), entries); + } + + entries.Add(entry); + } + + public EntityEntry GetEntry(TCollectionBase entity) + { + Check.NotNull(entity, nameof(entity)); + + var collectionType = typeof(TCollectionBase); + + if (EntryLookupByType.TryGetValue(collectionType, out var entries)) + { + var entityDefinition = EntityMapping.GetOrCreateDefinition(collectionType); + var entityId = entityDefinition.GetIdValue(entity); + var defaultIdValue = entityDefinition.GetDefaultId(); + foreach (var entry in entries) + { + if (Equals(entityId, defaultIdValue) && ReferenceEquals(entry.Entity, entity)) + { + return entry; + } + else + { + var entryEntityId = entityDefinition.GetIdValue(entry.Entity); + if (!Equals(entryEntityId, defaultIdValue) && entryEntityId.Equals(entityId)) + { + return entry; + } + } + } + } + + return null; + } + + public EntityEntry GetEntryById(object id) + { + var collectionType = typeof(TCollectionBase); + + if (EntryLookupByType.TryGetValue(collectionType, out var entries)) + { + var entityDefinition = EntityMapping.GetOrCreateDefinition(collectionType); + + foreach (var entry in entries) + { + var entryEntityId = entityDefinition.GetIdValue(entry.Entity); + if (entryEntityId.Equals(id)) + { + return entry; + } + } + } + + return null; + } + public EntityEntry SetEntityState(TCollectionBase entity, EntityEntryState state) where TCollectionBase : class + { + Check.NotNull(entity, nameof(entity)); + + var entry = GetEntry(entity); + if (entry != null) + { + if (ReferenceEquals(entry.Entity, entity)) + { + if ((entry.State == EntityEntryState.Added && state == EntityEntryState.Deleted) || state == EntityEntryState.Detached) + { + entry.State = EntityEntryState.Detached; + RemoveEntry(entry); + } + else + { + entry.State = state; + } + } + else if (state != EntityEntryState.Detached) + { + RemoveEntry(entry); + entry = new EntityEntry(entity, typeof(TCollectionBase), state); + AddEntry(entry); + } + } + else if (state != EntityEntryState.Detached) + { + entry = new EntityEntry(entity, typeof(TCollectionBase), state); + AddEntry(entry); + } + + return entry; + } + + /// + /// Performed after saving, this resets the state of entries to align with the state in the database. + /// + public void CommitChanges() + { + foreach (var typeGroup in EntryLookupByType.Values) + { + for (var i = typeGroup.Count - 1; i >= 0; i--) + { + var entry = typeGroup[i]; + if (entry.State == EntityEntryState.Added || entry.State == EntityEntryState.Updated) + { + entry.ResetState(); + } + else if (entry.State == EntityEntryState.Deleted) + { + typeGroup.Remove(entry); + } + } + } + } + + /// + /// Performed prior to saving, this detect changes between the original and current state of entries, updating states where necessary. + /// + public void DetectChanges() + { + foreach (var typeGroup in EntryLookupByType.Values) + { + for (var i = typeGroup.Count - 1; i >= 0; i--) + { + var entry = typeGroup[i]; + if (entry.State == EntityEntryState.NoChanges || entry.State == EntityEntryState.Updated) + { + if (BsonDiff.HasDifferences(entry.OriginalValues, entry.CurrentValues)) + { + entry.State = EntityEntryState.Updated; + } + else + { + entry.State = EntityEntryState.NoChanges; + } + } + } + } + } + public void EnforceMultiTenant(string tenantId) { - foreach (var pair in EntryLookupByType) - { + foreach (var pair in EntryLookupByType) + { if (!typeof(IHaveTenantId).IsAssignableFrom(pair.Key)) { continue; @@ -181,15 +181,15 @@ public void EnforceMultiTenant(string tenantId) { throw new MultiTenantException($"Entity type {pair.Key.Name} is in the context, but no tenant ID was provided."); } - - var typeGroup = pair.Value; - for (var i = typeGroup.Count - 1; i >= 0; i--) + + var typeGroup = pair.Value; + for (var i = typeGroup.Count - 1; i >= 0; i--) { var entry = typeGroup[i]; var tenantItem = entry.Entity as IHaveTenantId; if (tenantItem.TenantId != tenantId) { - throw new MultiTenantException($"Entity type {entry.EntityType.Name}, tenant ID does not match. Expected: {tenantId}, entity has: {tenantItem.TenantId}"); + throw new MultiTenantException($"Entity type {entry.EntityType.Name}, tenant ID does not match. Expected: {tenantId}, entity has: {tenantItem.TenantId}"); } } @@ -197,9 +197,9 @@ public void EnforceMultiTenant(string tenantId) } } - public void Clear() - { - EntryLookupByType.Clear(); - } - } -} + public void Clear() + { + EntryLookupByType.Clear(); + } + } +} diff --git a/src/MongoFramework/Infrastructure/Indexing/EntityIndexWriter.cs b/src/MongoFramework/Infrastructure/Indexing/EntityIndexWriter.cs index 48136e99..9cb06faf 100644 --- a/src/MongoFramework/Infrastructure/Indexing/EntityIndexWriter.cs +++ b/src/MongoFramework/Infrastructure/Indexing/EntityIndexWriter.cs @@ -1,10 +1,10 @@ -using MongoFramework.Infrastructure.Diagnostics; -using MongoFramework.Infrastructure.Mapping; -using System; +using System; using System.Collections.Concurrent; using System.Linq; using System.Threading; using System.Threading.Tasks; +using MongoFramework.Infrastructure.Diagnostics; +using MongoFramework.Infrastructure.Mapping; namespace MongoFramework.Infrastructure.Indexing { diff --git a/src/MongoFramework/Infrastructure/Indexing/IndexModelBuilder.cs b/src/MongoFramework/Infrastructure/Indexing/IndexModelBuilder.cs index 10902ca4..b8c59752 100644 --- a/src/MongoFramework/Infrastructure/Indexing/IndexModelBuilder.cs +++ b/src/MongoFramework/Infrastructure/Indexing/IndexModelBuilder.cs @@ -1,7 +1,5 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Text; using MongoDB.Driver; using MongoFramework.Infrastructure.Mapping; diff --git a/src/MongoFramework/Infrastructure/Internal/GenericsHelper.cs b/src/MongoFramework/Infrastructure/Internal/GenericsHelper.cs index 2475ef5c..75d79f70 100644 --- a/src/MongoFramework/Infrastructure/Internal/GenericsHelper.cs +++ b/src/MongoFramework/Infrastructure/Internal/GenericsHelper.cs @@ -1,9 +1,7 @@ using System; using System.Collections.Concurrent; -using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Text; namespace MongoFramework.Infrastructure.Internal { diff --git a/src/MongoFramework/Infrastructure/Internal/TypeExtensions.cs b/src/MongoFramework/Infrastructure/Internal/TypeExtensions.cs index 4077e4ed..d2f38cfc 100644 --- a/src/MongoFramework/Infrastructure/Internal/TypeExtensions.cs +++ b/src/MongoFramework/Infrastructure/Internal/TypeExtensions.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; namespace MongoFramework.Infrastructure.Internal { diff --git a/src/MongoFramework/Infrastructure/Linq/AggregateExecutionModel.cs b/src/MongoFramework/Infrastructure/Linq/AggregateExecutionModel.cs index 14998ab2..84fba25c 100644 --- a/src/MongoFramework/Infrastructure/Linq/AggregateExecutionModel.cs +++ b/src/MongoFramework/Infrastructure/Linq/AggregateExecutionModel.cs @@ -1,7 +1,5 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq.Expressions; -using System.Text; using MongoDB.Bson; using MongoDB.Bson.Serialization; diff --git a/src/MongoFramework/Infrastructure/Linq/IMongoFrameworkQueryProvider.cs b/src/MongoFramework/Infrastructure/Linq/IMongoFrameworkQueryProvider.cs index c46bc179..9a2e5335 100644 --- a/src/MongoFramework/Infrastructure/Linq/IMongoFrameworkQueryProvider.cs +++ b/src/MongoFramework/Infrastructure/Linq/IMongoFrameworkQueryProvider.cs @@ -1,7 +1,6 @@ using System.Linq; using System.Linq.Expressions; using System.Threading; -using System.Threading.Tasks; namespace MongoFramework.Infrastructure.Linq { diff --git a/src/MongoFramework/Infrastructure/Linq/IMongoFrameworkQueryable.cs b/src/MongoFramework/Infrastructure/Linq/IMongoFrameworkQueryable.cs index 19105c21..97222fd4 100644 --- a/src/MongoFramework/Infrastructure/Linq/IMongoFrameworkQueryable.cs +++ b/src/MongoFramework/Infrastructure/Linq/IMongoFrameworkQueryable.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; -using System.Linq; -using System.Threading; +using System.Linq; namespace MongoFramework.Infrastructure.Linq { diff --git a/src/MongoFramework/Infrastructure/Linq/MongoFrameworkQueryProvider.cs b/src/MongoFramework/Infrastructure/Linq/MongoFrameworkQueryProvider.cs index acaf6c14..82970cf2 100644 --- a/src/MongoFramework/Infrastructure/Linq/MongoFrameworkQueryProvider.cs +++ b/src/MongoFramework/Infrastructure/Linq/MongoFrameworkQueryProvider.cs @@ -1,10 +1,4 @@ -using MongoDB.Bson; -using MongoDB.Bson.Serialization; -using MongoDB.Driver; -using MongoDB.Driver.Linq; -using MongoFramework.Infrastructure.Diagnostics; -using MongoFramework.Infrastructure.Mapping; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; @@ -12,6 +6,12 @@ using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Driver; +using MongoDB.Driver.Linq; +using MongoFramework.Infrastructure.Diagnostics; +using MongoFramework.Infrastructure.Mapping; namespace MongoFramework.Infrastructure.Linq { @@ -102,7 +102,7 @@ public object ExecuteAsync(Expression expression, CancellationToken cancellation if (model.ResultTransformer != null) { executor = Expression.Invoke( - model.ResultTransformer, + model.ResultTransformer, Expression.Convert(executor, model.ResultTransformer.Parameters[0].Type), Expression.Constant(cancellationToken) ); @@ -157,7 +157,7 @@ private AggregateExecutionModel GetExecutionModel(Expression expression, bool is result.ResultTransformer = ResultTransformers.Transform(expression, serializer.ValueType, isAsync) as LambdaExpression; //Note: In the future this can change from the initial reflection to a `TryTransform` function where it checks the expression itself - // The reason we are doing this method first is to weed out the bugs and any core missing functionality. + // The reason we are doing this method first is to weed out the bugs and any core missing functionality. } return result; @@ -236,7 +236,7 @@ private async IAsyncEnumerable ExecuteModelAsync(AggregateExec foreach (var item in resultBatch) { if (item is TEntity entityItem && - (model.ResultTransformer == null || + (model.ResultTransformer == null || model.ResultTransformer.ReturnType == typeof(ValueTask) || model.ResultTransformer.ReturnType == typeof(Task))) { diff --git a/src/MongoFramework/Infrastructure/Linq/QueryHelper.cs b/src/MongoFramework/Infrastructure/Linq/QueryHelper.cs index f17569b1..49350bf4 100644 --- a/src/MongoFramework/Infrastructure/Linq/QueryHelper.cs +++ b/src/MongoFramework/Infrastructure/Linq/QueryHelper.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using MongoDB.Bson; +using System.Linq; using MongoFramework.Infrastructure.Mapping; namespace MongoFramework.Infrastructure.Linq diff --git a/src/MongoFramework/Infrastructure/Mapping/DefaultMappingPack.cs b/src/MongoFramework/Infrastructure/Mapping/DefaultMappingPack.cs index 4056d2dd..b786320a 100644 --- a/src/MongoFramework/Infrastructure/Mapping/DefaultMappingPack.cs +++ b/src/MongoFramework/Infrastructure/Mapping/DefaultMappingPack.cs @@ -1,5 +1,5 @@ -using MongoFramework.Infrastructure.Mapping.Processors; -using System.Collections.Generic; +using System.Collections.Generic; +using MongoFramework.Infrastructure.Mapping.Processors; namespace MongoFramework.Infrastructure.Mapping { diff --git a/src/MongoFramework/Infrastructure/Mapping/EntityDefinition.cs b/src/MongoFramework/Infrastructure/Mapping/EntityDefinition.cs index d703bd5f..5c979cd5 100644 --- a/src/MongoFramework/Infrastructure/Mapping/EntityDefinition.cs +++ b/src/MongoFramework/Infrastructure/Mapping/EntityDefinition.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; namespace MongoFramework.Infrastructure.Mapping { diff --git a/src/MongoFramework/Infrastructure/Mapping/EntityDefinitionExtensions.cs b/src/MongoFramework/Infrastructure/Mapping/EntityDefinitionExtensions.cs index c293c793..3bb0b60c 100644 --- a/src/MongoFramework/Infrastructure/Mapping/EntityDefinitionExtensions.cs +++ b/src/MongoFramework/Infrastructure/Mapping/EntityDefinitionExtensions.cs @@ -9,7 +9,7 @@ public static class EntityDefinitionExtensions { public static IEntityProperty GetIdProperty(this IEntityDefinition definition) { - return definition.GetAllProperties().Where(m => m.IsKey).FirstOrDefault(); + return definition.GetAllProperties().FirstOrDefault(m => m.IsKey); } public static string GetIdName(this IEntityDefinition definition) @@ -25,7 +25,7 @@ public static object GetIdValue(this IEntityDefinition definition, object entity public static object GetDefaultId(this IEntityDefinition definition) { var idPropertyType = definition.GetIdProperty()?.PropertyType; - if (idPropertyType != null && idPropertyType.IsValueType) + if (idPropertyType is { IsValueType: true }) { return Activator.CreateInstance(idPropertyType); } @@ -73,11 +73,12 @@ public static IEntityProperty GetProperty(this IEntityDefinition definition, str return default; } - private class TraversalState + private sealed class TraversalState { public HashSet SeenTypes { get; set; } public IEnumerable Properties { get; set; } } + public static IEnumerable TraverseProperties(this IEntityDefinition definition) { var stack = new Stack(); diff --git a/src/MongoFramework/Infrastructure/Mapping/EntityKeyGenerator.cs b/src/MongoFramework/Infrastructure/Mapping/EntityKeyGenerator.cs index f9e48c6b..f54bef45 100644 --- a/src/MongoFramework/Infrastructure/Mapping/EntityKeyGenerator.cs +++ b/src/MongoFramework/Infrastructure/Mapping/EntityKeyGenerator.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization; namespace MongoFramework.Infrastructure.Mapping { diff --git a/src/MongoFramework/Infrastructure/Mapping/EntityMapping.cs b/src/MongoFramework/Infrastructure/Mapping/EntityMapping.cs index 919438f6..b1017bda 100644 --- a/src/MongoFramework/Infrastructure/Mapping/EntityMapping.cs +++ b/src/MongoFramework/Infrastructure/Mapping/EntityMapping.cs @@ -1,11 +1,11 @@ -using MongoDB.Bson; -using MongoDB.Bson.Serialization; -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Threading; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; namespace MongoFramework.Infrastructure.Mapping { diff --git a/src/MongoFramework/Infrastructure/Mapping/IMappingProcessor.cs b/src/MongoFramework/Infrastructure/Mapping/IMappingProcessor.cs index 36e4331d..4d8b48df 100644 --- a/src/MongoFramework/Infrastructure/Mapping/IMappingProcessor.cs +++ b/src/MongoFramework/Infrastructure/Mapping/IMappingProcessor.cs @@ -1,5 +1,4 @@ using MongoDB.Bson.Serialization; -using System; namespace MongoFramework.Infrastructure.Mapping { diff --git a/src/MongoFramework/Infrastructure/Mapping/Processors/BsonKnownTypesProcessor.cs b/src/MongoFramework/Infrastructure/Mapping/Processors/BsonKnownTypesProcessor.cs index 746bbd95..da2d40fe 100644 --- a/src/MongoFramework/Infrastructure/Mapping/Processors/BsonKnownTypesProcessor.cs +++ b/src/MongoFramework/Infrastructure/Mapping/Processors/BsonKnownTypesProcessor.cs @@ -1,5 +1,4 @@ -using System; -using System.Reflection; +using System.Reflection; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Attributes; diff --git a/src/MongoFramework/Infrastructure/Mapping/Processors/CollectionNameProcessor.cs b/src/MongoFramework/Infrastructure/Mapping/Processors/CollectionNameProcessor.cs index 9613ac71..ad4b210d 100644 --- a/src/MongoFramework/Infrastructure/Mapping/Processors/CollectionNameProcessor.cs +++ b/src/MongoFramework/Infrastructure/Mapping/Processors/CollectionNameProcessor.cs @@ -1,8 +1,5 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations.Schema; using System.Reflection; -using System.Text; using MongoDB.Bson.Serialization; namespace MongoFramework.Infrastructure.Mapping.Processors diff --git a/src/MongoFramework/Infrastructure/Mapping/Processors/DecimalSerializationProcessor.cs b/src/MongoFramework/Infrastructure/Mapping/Processors/DecimalSerializationProcessor.cs index f477b97f..7b6149ae 100644 --- a/src/MongoFramework/Infrastructure/Mapping/Processors/DecimalSerializationProcessor.cs +++ b/src/MongoFramework/Infrastructure/Mapping/Processors/DecimalSerializationProcessor.cs @@ -1,6 +1,6 @@ -using MongoDB.Bson.Serialization; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; -using MongoDB.Bson; namespace MongoFramework.Infrastructure.Mapping.Processors { diff --git a/src/MongoFramework/Infrastructure/Mapping/Processors/EntityIdProcessor.cs b/src/MongoFramework/Infrastructure/Mapping/Processors/EntityIdProcessor.cs index 67b33dd1..5c04a52b 100644 --- a/src/MongoFramework/Infrastructure/Mapping/Processors/EntityIdProcessor.cs +++ b/src/MongoFramework/Infrastructure/Mapping/Processors/EntityIdProcessor.cs @@ -1,10 +1,9 @@ -using MongoDB.Bson; -using MongoDB.Bson.Serialization; -using MongoDB.Bson.Serialization.IdGenerators; -using System; +using System; using System.ComponentModel.DataAnnotations; -using System.Linq; using System.Reflection; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.IdGenerators; namespace MongoFramework.Infrastructure.Mapping.Processors { @@ -20,7 +19,7 @@ public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap) idProperty = property; break; } - + if (property.ElementName.Equals("id", StringComparison.InvariantCultureIgnoreCase)) { //We don't break here just in case another property has the KeyAttribute diff --git a/src/MongoFramework/Infrastructure/Mapping/Processors/ExtraElementsProcessor.cs b/src/MongoFramework/Infrastructure/Mapping/Processors/ExtraElementsProcessor.cs index 537d9240..3a0eb886 100644 --- a/src/MongoFramework/Infrastructure/Mapping/Processors/ExtraElementsProcessor.cs +++ b/src/MongoFramework/Infrastructure/Mapping/Processors/ExtraElementsProcessor.cs @@ -1,9 +1,7 @@ -using MongoDB.Bson.Serialization; -using MongoFramework.Attributes; -using System; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Generic; using System.Reflection; +using MongoDB.Bson.Serialization; +using MongoFramework.Attributes; namespace MongoFramework.Infrastructure.Mapping.Processors { diff --git a/src/MongoFramework/Infrastructure/Mapping/Processors/HierarchyProcessor.cs b/src/MongoFramework/Infrastructure/Mapping/Processors/HierarchyProcessor.cs index 405ae19c..c7d0b8f5 100644 --- a/src/MongoFramework/Infrastructure/Mapping/Processors/HierarchyProcessor.cs +++ b/src/MongoFramework/Infrastructure/Mapping/Processors/HierarchyProcessor.cs @@ -1,5 +1,4 @@ using MongoDB.Bson.Serialization; -using System; namespace MongoFramework.Infrastructure.Mapping.Processors { diff --git a/src/MongoFramework/Infrastructure/Mapping/Processors/IndexProcessor.cs b/src/MongoFramework/Infrastructure/Mapping/Processors/IndexProcessor.cs index 0eb490fc..ca86bb1e 100644 --- a/src/MongoFramework/Infrastructure/Mapping/Processors/IndexProcessor.cs +++ b/src/MongoFramework/Infrastructure/Mapping/Processors/IndexProcessor.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Collections.Generic; using System.Reflection; using MongoDB.Bson.Serialization; using MongoFramework.Attributes; diff --git a/src/MongoFramework/Infrastructure/Mapping/Processors/MappingAdapterProcessor.cs b/src/MongoFramework/Infrastructure/Mapping/Processors/MappingAdapterProcessor.cs index 281c94f9..ef5f40be 100644 --- a/src/MongoFramework/Infrastructure/Mapping/Processors/MappingAdapterProcessor.cs +++ b/src/MongoFramework/Infrastructure/Mapping/Processors/MappingAdapterProcessor.cs @@ -1,8 +1,5 @@ using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations.Schema; using System.Reflection; -using System.Text; using MongoDB.Bson.Serialization; using MongoFramework.Attributes; @@ -14,7 +11,7 @@ public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap) { var adapterAttribute = definition.EntityType.GetCustomAttribute(); - if (adapterAttribute == null) + if (adapterAttribute == null) { return; } diff --git a/src/MongoFramework/Infrastructure/Mapping/Processors/NestedTypeProcessor.cs b/src/MongoFramework/Infrastructure/Mapping/Processors/NestedTypeProcessor.cs index c16fb1dd..eb49a868 100644 --- a/src/MongoFramework/Infrastructure/Mapping/Processors/NestedTypeProcessor.cs +++ b/src/MongoFramework/Infrastructure/Mapping/Processors/NestedTypeProcessor.cs @@ -1,10 +1,5 @@ -using MongoDB.Bson; -using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization; using MongoFramework.Infrastructure.Internal; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; namespace MongoFramework.Infrastructure.Mapping.Processors { diff --git a/src/MongoFramework/Infrastructure/Mapping/Processors/PropertyMappingProcessor.cs b/src/MongoFramework/Infrastructure/Mapping/Processors/PropertyMappingProcessor.cs index 355de1e9..ee4efbad 100644 --- a/src/MongoFramework/Infrastructure/Mapping/Processors/PropertyMappingProcessor.cs +++ b/src/MongoFramework/Infrastructure/Mapping/Processors/PropertyMappingProcessor.cs @@ -1,9 +1,7 @@ -using MongoDB.Bson.Serialization; -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; using System.Reflection; +using MongoDB.Bson.Serialization; namespace MongoFramework.Infrastructure.Mapping.Processors { @@ -23,7 +21,7 @@ public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap) continue; } - //Skip overridden properties + //Skip overridden properties var getMethod = property.GetMethod; if (property.GetMethod.IsVirtual && getMethod.GetBaseDefinition().DeclaringType != entityType) { @@ -45,7 +43,7 @@ public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap) //Do the mapping var memberMap = classMap.MapMember(property); - + //Set custom element name with the "ColumnAttribute" var columnAttribute = property.GetCustomAttribute(); if (columnAttribute != null) diff --git a/src/MongoFramework/Infrastructure/Mapping/Processors/TypeDiscoveryProcessor.cs b/src/MongoFramework/Infrastructure/Mapping/Processors/TypeDiscoveryProcessor.cs index 8dc262f8..0175ba53 100644 --- a/src/MongoFramework/Infrastructure/Mapping/Processors/TypeDiscoveryProcessor.cs +++ b/src/MongoFramework/Infrastructure/Mapping/Processors/TypeDiscoveryProcessor.cs @@ -1,7 +1,4 @@ -using System; -using MongoDB.Bson.Serialization; -using System.Reflection; -using MongoFramework.Attributes; +using MongoDB.Bson.Serialization; using MongoFramework.Infrastructure.Serialization; namespace MongoFramework.Infrastructure.Mapping.Processors diff --git a/src/MongoFramework/Infrastructure/Serialization/TypeDiscoverySerializationProvider.cs b/src/MongoFramework/Infrastructure/Serialization/TypeDiscoverySerializationProvider.cs index 5042d625..166ea364 100644 --- a/src/MongoFramework/Infrastructure/Serialization/TypeDiscoverySerializationProvider.cs +++ b/src/MongoFramework/Infrastructure/Serialization/TypeDiscoverySerializationProvider.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Reflection; using MongoDB.Bson.Serialization; using MongoFramework.Attributes; diff --git a/src/MongoFramework/Infrastructure/Serialization/TypeDiscoverySerializer.cs b/src/MongoFramework/Infrastructure/Serialization/TypeDiscoverySerializer.cs index cfe6686e..bcce74e3 100644 --- a/src/MongoFramework/Infrastructure/Serialization/TypeDiscoverySerializer.cs +++ b/src/MongoFramework/Infrastructure/Serialization/TypeDiscoverySerializer.cs @@ -1,13 +1,12 @@ -using MongoDB.Bson; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using MongoDB.Bson; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; -using System; -using System.Collections.Concurrent; -using System.Linq; -using System.Threading; using MongoFramework.Infrastructure.Mapping; -using System.Collections.Generic; namespace MongoFramework.Infrastructure.Serialization { @@ -167,29 +166,26 @@ private IBsonSerializer GetRealSerializer(Type type) { return new DictionaryInterfaceImplementerSerializer>(); } - else if (type.IsGenericType && DictionaryTypes.Contains(type.GetGenericTypeDefinition())) + + if (type.IsGenericType && DictionaryTypes.Contains(type.GetGenericTypeDefinition())) { var serializerType = typeof(DictionaryInterfaceImplementerSerializer<>).MakeGenericType(type); var serializer = (IBsonSerializer)Activator.CreateInstance(serializerType); return serializer; } - else + + if (EntityMapping.IsValidTypeToMap(type)) { - if (EntityMapping.IsValidTypeToMap(type)) - { - //Force the type to be processed by the Entity Mapper - EntityMapping.TryRegisterType(type, out _); + //Force the type to be processed by the Entity Mapper + EntityMapping.TryRegisterType(type, out _); - var classMap = BsonClassMap.LookupClassMap(type); - var serializerType = typeof(BsonClassMapSerializer<>).MakeGenericType(type); - var serializer = (IBsonSerializer)Activator.CreateInstance(serializerType, classMap); - return serializer; - } - else - { - return BsonSerializer.LookupSerializer(type); - } + var classMap = BsonClassMap.LookupClassMap(type); + var serializerType = typeof(BsonClassMapSerializer<>).MakeGenericType(type); + var serializer = (IBsonSerializer)Activator.CreateInstance(serializerType, classMap); + return serializer; } + + return BsonSerializer.LookupSerializer(type); } public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value) diff --git a/src/MongoFramework/Linq/ExpressionExtensions.cs b/src/MongoFramework/Linq/ExpressionExtensions.cs index 7881b1aa..aa47be49 100644 --- a/src/MongoFramework/Linq/ExpressionExtensions.cs +++ b/src/MongoFramework/Linq/ExpressionExtensions.cs @@ -2,9 +2,7 @@ // source: https://stackoverflow.com/questions/457316/combining-two-expressions-expressionfunct-bool // using System; -using System.Collections.Generic; using System.Linq.Expressions; -using System.Text; namespace MongoFramework.Linq { @@ -12,7 +10,7 @@ public static class ExpressionExtensions { public static Expression> AndAlso(this Expression> expr1, Expression> expr2) { - ParameterExpression parameter1 = expr1.Parameters[0]; + var parameter1 = expr1.Parameters[0]; var visitor = new ReplaceParameterVisitor(expr2.Parameters[0], parameter1); var body2WithParam1 = visitor.Visit(expr2.Body); return Expression.Lambda>(Expression.AndAlso(expr1.Body, body2WithParam1), parameter1); @@ -33,7 +31,7 @@ protected override Expression VisitParameter(ParameterExpression node) { if (ReferenceEquals(node, _oldParameter)) { - return _newParameter; + return _newParameter; } return base.VisitParameter(node); diff --git a/src/MongoFramework/Linq/LinqExtensions.cs b/src/MongoFramework/Linq/LinqExtensions.cs index 50b95533..2fedd40e 100644 --- a/src/MongoFramework/Linq/LinqExtensions.cs +++ b/src/MongoFramework/Linq/LinqExtensions.cs @@ -1,3 +1,8 @@ +using System; +using System.Collections; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Driver; @@ -5,11 +10,6 @@ using MongoDB.Driver.Linq; using MongoFramework.Infrastructure.Linq; using MongoFramework.Infrastructure.Mapping; -using System; -using System.Collections; -using System.Linq; -using System.Linq.Expressions; -using System.Reflection; namespace MongoFramework.Linq { @@ -21,7 +21,7 @@ public static string ToQuery(this IQueryable queryable) { return mongoFrameworkQueryable.ToQuery(); } - + throw new ArgumentException($"Queryable must implement interface {nameof(IMongoFrameworkQueryable)}", nameof(queryable)); } diff --git a/src/MongoFramework/MongoDbBucketSet.cs b/src/MongoFramework/MongoDbBucketSet.cs index f4182fc8..26a2859c 100644 --- a/src/MongoFramework/MongoDbBucketSet.cs +++ b/src/MongoFramework/MongoDbBucketSet.cs @@ -5,15 +5,13 @@ using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; -using MongoFramework.Infrastructure; using MongoFramework.Infrastructure.Commands; -using MongoFramework.Infrastructure.Indexing; using MongoFramework.Infrastructure.Mapping; using MongoFramework.Utilities; namespace MongoFramework { - public class MongoDbBucketSet : IMongoDbBucketSet + public class MongoDbBucketSet : IMongoDbBucketSet where TGroup : class where TSubEntity : class { @@ -114,7 +112,7 @@ private IQueryable> GetQueryable() { return Context.Query>(); } - + public Type ElementType => GetQueryable().ElementType; public Expression Expression => GetQueryable().Expression; diff --git a/src/MongoFramework/MongoDbConnection.cs b/src/MongoFramework/MongoDbConnection.cs index 797cc7c2..780c4c88 100644 --- a/src/MongoFramework/MongoDbConnection.cs +++ b/src/MongoFramework/MongoDbConnection.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Concurrent; using MongoDB.Driver; -using MongoFramework.Infrastructure; using MongoFramework.Infrastructure.Diagnostics; using MongoFramework.Utilities; diff --git a/src/MongoFramework/MongoDbContext.cs b/src/MongoFramework/MongoDbContext.cs index 4d638a99..7c3a11de 100644 --- a/src/MongoFramework/MongoDbContext.cs +++ b/src/MongoFramework/MongoDbContext.cs @@ -1,14 +1,14 @@ -using MongoFramework.Infrastructure; -using MongoFramework.Infrastructure.Commands; -using MongoFramework.Infrastructure.Indexing; -using MongoFramework.Infrastructure.Internal; -using MongoFramework.Infrastructure.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading; using System.Threading.Tasks; +using MongoFramework.Infrastructure; +using MongoFramework.Infrastructure.Commands; +using MongoFramework.Infrastructure.Indexing; +using MongoFramework.Infrastructure.Internal; +using MongoFramework.Infrastructure.Linq; using MongoFramework.Utilities; namespace MongoFramework @@ -59,9 +59,9 @@ private IEnumerable GenerateWriteCommands() } } - protected virtual void AfterDetectChanges() {} + protected virtual void AfterDetectChanges() { } - protected virtual WriteModelOptions GetWriteModelOptions() => WriteModelOptions.Default; + protected virtual WriteModelOptions GetWriteModelOptions() => WriteModelOptions.Default; public virtual void SaveChanges() { @@ -95,7 +95,7 @@ public virtual async Task SaveChangesAsync(CancellationToken cancellationToken = AfterDetectChanges(); var commands = GenerateWriteCommands(); var writeModelOptions = GetWriteModelOptions(); - + var commandsByEntityType = commands.GroupBy(c => c.EntityType); foreach (var entityTypeCommands in commandsByEntityType) { @@ -108,7 +108,7 @@ public virtual async Task SaveChangesAsync(CancellationToken cancellationToken = ChangeTracker.CommitChanges(); CommandStaging.CommitChanges(); } - + private static async Task InternalSaveChangesAsync(IMongoDbConnection connection, IEnumerable commands, WriteModelOptions options, CancellationToken cancellationToken) where TEntity : class { await EntityIndexWriter.ApplyIndexingAsync(connection); @@ -133,7 +133,7 @@ public IQueryable Query() where TEntity : class var provider = new MongoFrameworkQueryProvider(Connection); return new MongoFrameworkQueryable(provider); } - + /// /// Marks the entity as unchanged in the change tracker and starts tracking. /// @@ -156,7 +156,7 @@ public virtual void AttachRange(IEnumerable entities) where TE ChangeTracker.SetEntityState(entity, EntityEntryState.NoChanges); } } - + public void Dispose() { Dispose(true); diff --git a/src/MongoFramework/MongoDbSet.cs b/src/MongoFramework/MongoDbSet.cs index 86d3741f..18ed905a 100644 --- a/src/MongoFramework/MongoDbSet.cs +++ b/src/MongoFramework/MongoDbSet.cs @@ -1,239 +1,239 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; using MongoDB.Driver; -using MongoFramework.Infrastructure; -using MongoFramework.Infrastructure.Commands; -using MongoFramework.Infrastructure.Linq; -using MongoFramework.Infrastructure.Linq.Processors; +using MongoFramework.Infrastructure; +using MongoFramework.Infrastructure.Commands; +using MongoFramework.Infrastructure.Linq; +using MongoFramework.Infrastructure.Linq.Processors; using MongoFramework.Infrastructure.Mapping; using MongoFramework.Utilities; -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; - -namespace MongoFramework -{ - /// - /// Basic Mongo "DbSet", providing entity tracking support. - /// - /// - public class MongoDbSet : IMongoDbSet where TEntity : class - { - public IMongoDbContext Context { get; } - - public MongoDbSet(IMongoDbContext context) - { - Check.NotNull(context, nameof(context)); - Context = context; - } - - public virtual TEntity Create() - { - var entity = Activator.CreateInstance(); - Add(entity); - return entity; - } - - /// - /// Finds an entity with the given primary key value. If an entity with the given primary key value - /// is being tracked by the context, then it is returned immediately without making a request to the - /// database. Otherwise, a query is made to the database for an entity with the given primary key value - /// and this entity, if found, is attached to the context and returned. If no entity is found, then - /// null is returned. - /// - /// The value of the primary key for the entity to be found. - /// The entity found, or null. - public virtual TEntity Find(object id) - { - Check.NotNull(id, nameof(id)); - - var tracked = Context.ChangeTracker.GetEntryById(id); - - if (tracked != null) - { - return tracked.Entity as TEntity; - } - - var entityDefinition = EntityMapping.GetOrCreateDefinition(typeof(TEntity)); - var filter = entityDefinition.CreateIdFilter(id); - - var collection = Context.Connection.GetDatabase().GetCollection(entityDefinition.CollectionName); - var cursor = collection.Find(filter); - var entity = cursor.FirstOrDefault(); - - if (entity != null) - { - Context.ChangeTracker.SetEntityState(entity, EntityEntryState.NoChanges); - } - - return entity; - } - - /// - /// Finds an entity with the given primary key value. If an entity with the given primary key value - /// is being tracked by the context, then it is returned immediately without making a request to the - /// database. Otherwise, a query is made to the database for an entity with the given primary key value - /// and this entity, if found, is attached to the context and returned. If no entity is found, then - /// null is returned. - /// - /// The value of the primary key for the entity to be found. - /// The entity found, or null. - public virtual async ValueTask FindAsync(object id) - { - Check.NotNull(id, nameof(id)); - - var tracked = Context.ChangeTracker.GetEntryById(id); - - if (tracked != null) - { - return tracked.Entity as TEntity; - } - - var entityDefinition = EntityMapping.GetOrCreateDefinition(typeof(TEntity)); - var filter = entityDefinition.CreateIdFilter(id); - - var collection = Context.Connection.GetDatabase().GetCollection(entityDefinition.CollectionName); - var cursor = await collection.FindAsync(filter); - var entity = await cursor.FirstOrDefaultAsync(); - - if (entity != null) - { - Context.ChangeTracker.SetEntityState(entity, EntityEntryState.NoChanges); - } - - return entity; - } - - - /// - /// Marks the entity for insertion into the database. - /// - /// - public virtual void Add(TEntity entity) - { + +namespace MongoFramework +{ + /// + /// Basic Mongo "DbSet", providing entity tracking support. + /// + /// + public class MongoDbSet : IMongoDbSet where TEntity : class + { + public IMongoDbContext Context { get; } + + public MongoDbSet(IMongoDbContext context) + { + Check.NotNull(context, nameof(context)); + Context = context; + } + + public virtual TEntity Create() + { + var entity = Activator.CreateInstance(); + Add(entity); + return entity; + } + + /// + /// Finds an entity with the given primary key value. If an entity with the given primary key value + /// is being tracked by the context, then it is returned immediately without making a request to the + /// database. Otherwise, a query is made to the database for an entity with the given primary key value + /// and this entity, if found, is attached to the context and returned. If no entity is found, then + /// null is returned. + /// + /// The value of the primary key for the entity to be found. + /// The entity found, or null. + public virtual TEntity Find(object id) + { + Check.NotNull(id, nameof(id)); + + var tracked = Context.ChangeTracker.GetEntryById(id); + + if (tracked != null) + { + return tracked.Entity as TEntity; + } + + var entityDefinition = EntityMapping.GetOrCreateDefinition(typeof(TEntity)); + var filter = entityDefinition.CreateIdFilter(id); + + var collection = Context.Connection.GetDatabase().GetCollection(entityDefinition.CollectionName); + var cursor = collection.Find(filter); + var entity = cursor.FirstOrDefault(); + + if (entity != null) + { + Context.ChangeTracker.SetEntityState(entity, EntityEntryState.NoChanges); + } + + return entity; + } + + /// + /// Finds an entity with the given primary key value. If an entity with the given primary key value + /// is being tracked by the context, then it is returned immediately without making a request to the + /// database. Otherwise, a query is made to the database for an entity with the given primary key value + /// and this entity, if found, is attached to the context and returned. If no entity is found, then + /// null is returned. + /// + /// The value of the primary key for the entity to be found. + /// The entity found, or null. + public virtual async ValueTask FindAsync(object id) + { + Check.NotNull(id, nameof(id)); + + var tracked = Context.ChangeTracker.GetEntryById(id); + + if (tracked != null) + { + return tracked.Entity as TEntity; + } + + var entityDefinition = EntityMapping.GetOrCreateDefinition(typeof(TEntity)); + var filter = entityDefinition.CreateIdFilter(id); + + var collection = Context.Connection.GetDatabase().GetCollection(entityDefinition.CollectionName); + var cursor = await collection.FindAsync(filter); + var entity = await cursor.FirstOrDefaultAsync(); + + if (entity != null) + { + Context.ChangeTracker.SetEntityState(entity, EntityEntryState.NoChanges); + } + + return entity; + } + + + /// + /// Marks the entity for insertion into the database. + /// + /// + public virtual void Add(TEntity entity) + { + Check.NotNull(entity, nameof(entity)); + + Context.ChangeTracker.SetEntityState(entity, EntityEntryState.Added); + } + /// + /// Marks the collection of entities for insertion into the database. + /// + /// + public virtual void AddRange(IEnumerable entities) + { + Check.NotNull(entities, nameof(entities)); + + foreach (var entity in entities) + { + Context.ChangeTracker.SetEntityState(entity, EntityEntryState.Added); + } + } + + /// + /// Marks the entity for updating. + /// + /// + public virtual void Update(TEntity entity) + { + Check.NotNull(entity, nameof(entity)); + + Context.ChangeTracker.SetEntityState(entity, EntityEntryState.Updated); + } + /// + /// Marks the collection of entities for updating. + /// + /// + public virtual void UpdateRange(IEnumerable entities) + { + Check.NotNull(entities, nameof(entities)); + + foreach (var entity in entities) + { + Context.ChangeTracker.SetEntityState(entity, EntityEntryState.Updated); + } + } + + /// + /// Marks the entity for deletion. + /// + /// + public virtual void Remove(TEntity entity) + { Check.NotNull(entity, nameof(entity)); - - Context.ChangeTracker.SetEntityState(entity, EntityEntryState.Added); - } - /// - /// Marks the collection of entities for insertion into the database. - /// - /// - public virtual void AddRange(IEnumerable entities) - { - Check.NotNull(entities, nameof(entities)); - - foreach (var entity in entities) - { - Context.ChangeTracker.SetEntityState(entity, EntityEntryState.Added); - } - } - - /// - /// Marks the entity for updating. - /// - /// - public virtual void Update(TEntity entity) - { - Check.NotNull(entity, nameof(entity)); - - Context.ChangeTracker.SetEntityState(entity, EntityEntryState.Updated); - } - /// - /// Marks the collection of entities for updating. - /// - /// - public virtual void UpdateRange(IEnumerable entities) - { - Check.NotNull(entities, nameof(entities)); - - foreach (var entity in entities) - { - Context.ChangeTracker.SetEntityState(entity, EntityEntryState.Updated); - } - } - - /// - /// Marks the entity for deletion. - /// - /// - public virtual void Remove(TEntity entity) - { - Check.NotNull(entity, nameof(entity)); - - Context.ChangeTracker.SetEntityState(entity, EntityEntryState.Deleted); - } - /// - /// Marks the collection of entities for deletion. - /// - /// - public virtual void RemoveRange(IEnumerable entities) - { - Check.NotNull(entities, nameof(entities)); - - foreach (var entity in entities) - { - Context.ChangeTracker.SetEntityState(entity, EntityEntryState.Deleted); - } - } - /// - /// Stages a deletion for a range of entities that match the predicate - /// - /// - public virtual void RemoveRange(Expression> predicate) - { - Context.CommandStaging.Add(new RemoveEntityRangeCommand(predicate)); - } - /// - /// Stages a deletion for the entity that matches the specified ID - /// - /// - public virtual void RemoveById(object entityId) - { - Context.CommandStaging.Add(new RemoveEntityByIdCommand(entityId)); - } - - [Obsolete("Use SaveChanges on the IMongoDbContext")] - public void SaveChanges() - { - Context.SaveChanges(); - } - - [Obsolete("Use SaveChangesAsync on the IMongoDbContext")] - public async Task SaveChangesAsync(CancellationToken cancellationToken = default) - { - await Context.SaveChangesAsync(cancellationToken); - } - - #region IQueryable Implementation - - protected virtual IQueryable GetQueryable(bool trackEntities) - { - var queryable = Context.Query(); + + Context.ChangeTracker.SetEntityState(entity, EntityEntryState.Deleted); + } + /// + /// Marks the collection of entities for deletion. + /// + /// + public virtual void RemoveRange(IEnumerable entities) + { + Check.NotNull(entities, nameof(entities)); + + foreach (var entity in entities) + { + Context.ChangeTracker.SetEntityState(entity, EntityEntryState.Deleted); + } + } + /// + /// Stages a deletion for a range of entities that match the predicate + /// + /// + public virtual void RemoveRange(Expression> predicate) + { + Context.CommandStaging.Add(new RemoveEntityRangeCommand(predicate)); + } + /// + /// Stages a deletion for the entity that matches the specified ID + /// + /// + public virtual void RemoveById(object entityId) + { + Context.CommandStaging.Add(new RemoveEntityByIdCommand(entityId)); + } + + [Obsolete("Use SaveChanges on the IMongoDbContext")] + public void SaveChanges() + { + Context.SaveChanges(); + } + + [Obsolete("Use SaveChangesAsync on the IMongoDbContext")] + public async Task SaveChangesAsync(CancellationToken cancellationToken = default) + { + await Context.SaveChangesAsync(cancellationToken); + } + + #region IQueryable Implementation + + protected virtual IQueryable GetQueryable(bool trackEntities) + { + var queryable = Context.Query(); if (trackEntities) { var provider = queryable.Provider as IMongoFrameworkQueryProvider; provider.EntityProcessors.Add(new EntityTrackingProcessor(Context)); - } - return queryable; - } - - public Expression Expression => GetQueryable(true).Expression; - - public Type ElementType => GetQueryable(true).ElementType; - - public IQueryProvider Provider => GetQueryable(true).Provider; - - public IEnumerator GetEnumerator() - { - return GetQueryable(true).GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); + } + return queryable; + } + + public Expression Expression => GetQueryable(true).Expression; + + public Type ElementType => GetQueryable(true).ElementType; + + public IQueryProvider Provider => GetQueryable(true).Provider; + + public IEnumerator GetEnumerator() + { + return GetQueryable(true).GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); } public virtual IQueryable AsNoTracking() @@ -241,8 +241,8 @@ public virtual IQueryable AsNoTracking() return GetQueryable(false); } - #endregion - + #endregion + } - + } \ No newline at end of file diff --git a/src/MongoFramework/MongoDbTenantContext.cs b/src/MongoFramework/MongoDbTenantContext.cs index 48e5a921..ffbbd199 100644 --- a/src/MongoFramework/MongoDbTenantContext.cs +++ b/src/MongoFramework/MongoDbTenantContext.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using MongoFramework.Infrastructure; using MongoFramework.Infrastructure.Commands; using MongoFramework.Utilities; @@ -24,11 +23,11 @@ protected override WriteModelOptions GetWriteModelOptions() { return new WriteModelOptions { TenantId = TenantId }; } - + public virtual void CheckEntity(IHaveTenantId entity) { Check.NotNull(entity, nameof(entity)); - + if (entity.TenantId != TenantId) { throw new MultiTenantException($"Entity type {entity.GetType().Name}, tenant ID does not match. Expected: {TenantId}, Entity has: {entity.TenantId}"); @@ -38,13 +37,13 @@ public virtual void CheckEntity(IHaveTenantId entity) public virtual void CheckEntities(IEnumerable entities) { Check.NotNull(entities, nameof(entities)); - + foreach (var entity in entities) { CheckEntity(entity); } } - + /// /// Marks the entity as unchanged in the change tracker and starts tracking. /// diff --git a/src/MongoFramework/MongoDbTenantSet.cs b/src/MongoFramework/MongoDbTenantSet.cs index 8eb8dda1..0b1449f2 100644 --- a/src/MongoFramework/MongoDbTenantSet.cs +++ b/src/MongoFramework/MongoDbTenantSet.cs @@ -1,6 +1,3 @@ -using MongoFramework.Infrastructure.Linq; -using MongoFramework.Infrastructure.Linq.Processors; -using MongoFramework.Linq; using System; using System.Collections.Generic; using System.Linq; @@ -9,7 +6,10 @@ using MongoDB.Driver; using MongoFramework.Infrastructure; using MongoFramework.Infrastructure.Commands; +using MongoFramework.Infrastructure.Linq; +using MongoFramework.Infrastructure.Linq.Processors; using MongoFramework.Infrastructure.Mapping; +using MongoFramework.Linq; using MongoFramework.Utilities; namespace MongoFramework @@ -25,9 +25,9 @@ public class MongoDbTenantSet : MongoDbSet, IMongoDbTenantSet< public MongoDbTenantSet(IMongoDbContext context) : base(context) { - Context = context as IMongoDbTenantContext ?? throw new ArgumentException("Context provided to a MongoDbTenantSet must be IMongoDbTenantContext",nameof(context)); + Context = context as IMongoDbTenantContext ?? throw new ArgumentException("Context provided to a MongoDbTenantSet must be IMongoDbTenantContext", nameof(context)); } - + /// /// Finds an entity with the given primary key value. If an entity with the given primary key value /// is being tracked by the context, then it is returned immediately without making a request to the @@ -119,7 +119,7 @@ public override void AddRange(IEnumerable entities) } base.AddRange(entities); } - + public override void Update(TEntity entity) { Context.CheckEntity(entity); @@ -150,7 +150,7 @@ public override void RemoveRange(Expression> predicate) var filter = predicate.AndAlso(o => o.TenantId == key); base.RemoveRange(filter); } - + #region IQueryable Implementation protected override IQueryable GetQueryable(bool trackEntities) @@ -164,9 +164,9 @@ protected override IQueryable GetQueryable(bool trackEntities) } return queryable; } - + public IQueryable GetSearchTextQueryable(string search) - { + { var key = Context.TenantId; var queryable = Context.Query().WhereFilter(b => b.Text(search)).Where(c => c.TenantId == key); var provider = queryable.Provider as IMongoFrameworkQueryProvider; diff --git a/src/MongoFramework/Utilities/Check.cs b/src/MongoFramework/Utilities/Check.cs index e6298ac8..a36c5a2c 100644 --- a/src/MongoFramework/Utilities/Check.cs +++ b/src/MongoFramework/Utilities/Check.cs @@ -9,107 +9,107 @@ namespace MongoFramework.Utilities { - [DebuggerStepThrough] - public static class Check - { - public static T NotNull(T value, string parameterName) - { - if (value is null) - { - NotEmpty(parameterName, nameof(parameterName)); - throw new ArgumentNullException(parameterName); - } - - return value; - } - - public static IReadOnlyList NotEmpty(IReadOnlyList value, string parameterName) - { - NotNull(value, parameterName); - - if (value.Count == 0) - { - NotEmpty(parameterName, nameof(parameterName)); - - throw new ArgumentException($"The collection argument '{parameterName}' must contain at least one element."); - } - - return value; - } - - public static string NotEmpty(string value, string parameterName) - { - Exception e = null; - if (value is null) - { - e = new ArgumentNullException(parameterName); - } - else if (value.Trim().Length == 0) - { - e = new ArgumentException($"The string argument '{parameterName}' cannot be empty."); - } - - if (e != null) - { - NotEmpty(parameterName, nameof(parameterName)); - - throw e; - } - - return value; - } - - public static string NullButNotEmpty(string value, string parameterName) - { - if (!(value is null) - && value.Length == 0) - { - NotEmpty(parameterName, nameof(parameterName)); - - throw new ArgumentException("The string argument '{argumentName}' cannot be empty."); - } - - return value; - } - - public static IReadOnlyList HasNoNulls(IReadOnlyList value, string parameterName) - where T : class - { - NotNull(value, parameterName); - - if (value.Any(e => e == null)) - { - NotEmpty(parameterName, nameof(parameterName)); - - throw new ArgumentException(parameterName); - } - - return value; - } - - public static IReadOnlyList HasNoEmptyElements( - IReadOnlyList value, - string parameterName) - { - NotNull(value, parameterName); - - if (value.Any(s => string.IsNullOrWhiteSpace(s))) - { - NotEmpty(parameterName, nameof(parameterName)); - - throw new ArgumentException($"The collection argument '{parameterName}' must not contain any empty elements."); - } - - return value; - } - - [Conditional("DEBUG")] - public static void DebugAssert(bool condition, string message) - { - if (!condition) - { - throw new Exception($"Check.DebugAssert failed: {message}"); - } - } - } + [DebuggerStepThrough] + public static class Check + { + public static T NotNull(T value, string parameterName) + { + if (value is null) + { + NotEmpty(parameterName, nameof(parameterName)); + throw new ArgumentNullException(parameterName); + } + + return value; + } + + public static IReadOnlyList NotEmpty(IReadOnlyList value, string parameterName) + { + NotNull(value, parameterName); + + if (value.Count == 0) + { + NotEmpty(parameterName, nameof(parameterName)); + + throw new ArgumentException($"The collection argument '{parameterName}' must contain at least one element."); + } + + return value; + } + + public static string NotEmpty(string value, string parameterName) + { + Exception e = null; + if (value is null) + { + e = new ArgumentNullException(parameterName); + } + else if (value.Trim().Length == 0) + { + e = new ArgumentException($"The string argument '{parameterName}' cannot be empty."); + } + + if (e != null) + { + NotEmpty(parameterName, nameof(parameterName)); + + throw e; + } + + return value; + } + + public static string NullButNotEmpty(string value, string parameterName) + { + if (!(value is null) + && value.Length == 0) + { + NotEmpty(parameterName, nameof(parameterName)); + + throw new ArgumentException("The string argument '{argumentName}' cannot be empty."); + } + + return value; + } + + public static IReadOnlyList HasNoNulls(IReadOnlyList value, string parameterName) + where T : class + { + NotNull(value, parameterName); + + if (value.Any(e => e == null)) + { + NotEmpty(parameterName, nameof(parameterName)); + + throw new ArgumentException(parameterName); + } + + return value; + } + + public static IReadOnlyList HasNoEmptyElements( + IReadOnlyList value, + string parameterName) + { + NotNull(value, parameterName); + + if (value.Any(s => string.IsNullOrWhiteSpace(s))) + { + NotEmpty(parameterName, nameof(parameterName)); + + throw new ArgumentException($"The collection argument '{parameterName}' must not contain any empty elements."); + } + + return value; + } + + [Conditional("DEBUG")] + public static void DebugAssert(bool condition, string message) + { + if (!condition) + { + throw new Exception($"Check.DebugAssert failed: {message}"); + } + } + } } diff --git a/tests/Directory.Build.props b/tests/Directory.Build.props index 4427e25d..3c7e2327 100644 --- a/tests/Directory.Build.props +++ b/tests/Directory.Build.props @@ -3,5 +3,5 @@ Latest - + \ No newline at end of file diff --git a/tests/MongoFramework.Benchmarks/BenchmarkDb.cs b/tests/MongoFramework.Benchmarks/BenchmarkDb.cs index 6dfb7804..e3d83b55 100644 --- a/tests/MongoFramework.Benchmarks/BenchmarkDb.cs +++ b/tests/MongoFramework.Benchmarks/BenchmarkDb.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Text; using MongoDB.Driver; namespace MongoFramework.Benchmarks diff --git a/tests/MongoFramework.Benchmarks/Infrastructure/Indexing/IndexModelBuilderBenchmark.cs b/tests/MongoFramework.Benchmarks/Infrastructure/Indexing/IndexModelBuilderBenchmark.cs index 00507638..e1c33007 100644 --- a/tests/MongoFramework.Benchmarks/Infrastructure/Indexing/IndexModelBuilderBenchmark.cs +++ b/tests/MongoFramework.Benchmarks/Infrastructure/Indexing/IndexModelBuilderBenchmark.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Jobs; using MongoFramework.Attributes; diff --git a/tests/MongoFramework.Benchmarks/Infrastructure/Internal/GenericMethodInvokeBenchmark.cs b/tests/MongoFramework.Benchmarks/Infrastructure/Internal/GenericMethodInvokeBenchmark.cs index a398df46..3907f233 100644 --- a/tests/MongoFramework.Benchmarks/Infrastructure/Internal/GenericMethodInvokeBenchmark.cs +++ b/tests/MongoFramework.Benchmarks/Infrastructure/Internal/GenericMethodInvokeBenchmark.cs @@ -25,8 +25,8 @@ public string DirectReflection() public string GenericHelper() { var method = GenericsHelper.GetMethodDelegate>( - typeof(GenericMethodInvokeBenchmark), - nameof(MyGenericMethod), + typeof(GenericMethodInvokeBenchmark), + nameof(MyGenericMethod), typeof(int) ); diff --git a/tests/MongoFramework.Benchmarks/Infrastructure/Mapping/EntityMappingBenchmark.cs b/tests/MongoFramework.Benchmarks/Infrastructure/Mapping/EntityMappingBenchmark.cs index 4250b413..abf61819 100644 --- a/tests/MongoFramework.Benchmarks/Infrastructure/Mapping/EntityMappingBenchmark.cs +++ b/tests/MongoFramework.Benchmarks/Infrastructure/Mapping/EntityMappingBenchmark.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Text; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Jobs; using MongoDB.Bson; diff --git a/tests/MongoFramework.Benchmarks/Infrastructure/Serialization/TypeDiscovery_FindTypeBenchmark.cs b/tests/MongoFramework.Benchmarks/Infrastructure/Serialization/TypeDiscovery_FindTypeBenchmark.cs index 6483e4a4..fa60cbf1 100644 --- a/tests/MongoFramework.Benchmarks/Infrastructure/Serialization/TypeDiscovery_FindTypeBenchmark.cs +++ b/tests/MongoFramework.Benchmarks/Infrastructure/Serialization/TypeDiscovery_FindTypeBenchmark.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Jobs; using MongoFramework.Infrastructure.Serialization; diff --git a/tests/MongoFramework.Benchmarks/MongoDbDriverHelper.cs b/tests/MongoFramework.Benchmarks/MongoDbDriverHelper.cs index 6b4c5b16..8015c0c0 100644 --- a/tests/MongoFramework.Benchmarks/MongoDbDriverHelper.cs +++ b/tests/MongoFramework.Benchmarks/MongoDbDriverHelper.cs @@ -1,10 +1,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Linq; using System.Reflection; -using System.Text; -using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Bson.Serialization; @@ -15,7 +12,7 @@ public static class MongoDbDriverHelper public static void ResetDriver() { //Primarily introduced to better test TypeDiscoverySerializer, this is designed to reset the MongoDB driver - //as if the assembly just loaded. It is likely incomplete and would be easily subject to breaking in future + //as if the assembly just loaded. It is likely incomplete and would be easily subject to breaking in future //driver updates. If someone knows a better way to reset the MongoDB driver, please open a pull request! var classMapField = typeof(BsonClassMap).GetField("__classMaps", BindingFlags.NonPublic | BindingFlags.Static); diff --git a/tests/MongoFramework.Benchmarks/MongoDbSetComparisonBenchmark.cs b/tests/MongoFramework.Benchmarks/MongoDbSetComparisonBenchmark.cs index fe984100..a6119cdd 100644 --- a/tests/MongoFramework.Benchmarks/MongoDbSetComparisonBenchmark.cs +++ b/tests/MongoFramework.Benchmarks/MongoDbSetComparisonBenchmark.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Generic; using System.Linq; -using System.Text; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Jobs; using MongoFramework.Attributes; diff --git a/tests/MongoFramework.Benchmarks/Program.cs b/tests/MongoFramework.Benchmarks/Program.cs index c72dee24..06fbda88 100644 --- a/tests/MongoFramework.Benchmarks/Program.cs +++ b/tests/MongoFramework.Benchmarks/Program.cs @@ -1,5 +1,4 @@ -using System; -using BenchmarkDotNet.Running; +using BenchmarkDotNet.Running; namespace MongoFramework.Benchmarks { diff --git a/tests/MongoFramework.Tests/AssertExtensions.cs b/tests/MongoFramework.Tests/AssertExtensions.cs index 48506165..3c0f495c 100644 --- a/tests/MongoFramework.Tests/AssertExtensions.cs +++ b/tests/MongoFramework.Tests/AssertExtensions.cs @@ -1,6 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; +using System; using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace MongoFramework.Tests { diff --git a/tests/MongoFramework.Tests/Bson/GetDifferencesTests.cs b/tests/MongoFramework.Tests/Bson/GetDifferencesTests.cs index 6296c2b0..71f8c779 100644 --- a/tests/MongoFramework.Tests/Bson/GetDifferencesTests.cs +++ b/tests/MongoFramework.Tests/Bson/GetDifferencesTests.cs @@ -1,9 +1,9 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using MongoDB.Bson; -using MongoFramework.Bson; -using System; +using System; using System.Collections.Generic; using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MongoDB.Bson; +using MongoFramework.Bson; namespace MongoFramework.Tests.Bson { diff --git a/tests/MongoFramework.Tests/Bson/HasDifferencesTests.cs b/tests/MongoFramework.Tests/Bson/HasDifferencesTests.cs index ae28f6c2..6489bda5 100644 --- a/tests/MongoFramework.Tests/Bson/HasDifferencesTests.cs +++ b/tests/MongoFramework.Tests/Bson/HasDifferencesTests.cs @@ -1,9 +1,9 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using MongoDB.Bson; -using MongoFramework.Bson; -using System; +using System; using System.Collections.Generic; using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MongoDB.Bson; +using MongoFramework.Bson; namespace MongoFramework.Tests.Bson { diff --git a/tests/MongoFramework.Tests/Infrastructure/Commands/AddEntityCommandTests.cs b/tests/MongoFramework.Tests/Infrastructure/Commands/AddEntityCommandTests.cs index c06d29eb..89258c8d 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Commands/AddEntityCommandTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Commands/AddEntityCommandTests.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations; using System.Linq; -using System.Text; -using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoFramework.Infrastructure; using MongoFramework.Infrastructure.Commands; diff --git a/tests/MongoFramework.Tests/Infrastructure/Commands/RemoveEntityByIdCommandTests.cs b/tests/MongoFramework.Tests/Infrastructure/Commands/RemoveEntityByIdCommandTests.cs index ab1f18cc..14bf74c9 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Commands/RemoveEntityByIdCommandTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Commands/RemoveEntityByIdCommandTests.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoFramework.Infrastructure; using MongoFramework.Infrastructure.Commands; diff --git a/tests/MongoFramework.Tests/Infrastructure/Commands/RemoveEntityCommandTests.cs b/tests/MongoFramework.Tests/Infrastructure/Commands/RemoveEntityCommandTests.cs index 10738a79..1de49179 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Commands/RemoveEntityCommandTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Commands/RemoveEntityCommandTests.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoFramework.Infrastructure; using MongoFramework.Infrastructure.Commands; diff --git a/tests/MongoFramework.Tests/Infrastructure/Commands/RemoveEntityRangeCommandTests.cs b/tests/MongoFramework.Tests/Infrastructure/Commands/RemoveEntityRangeCommandTests.cs index a24cd799..d65a8dbe 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Commands/RemoveEntityRangeCommandTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Commands/RemoveEntityRangeCommandTests.cs @@ -1,8 +1,5 @@ using System; -using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoFramework.Infrastructure; using MongoFramework.Infrastructure.Commands; diff --git a/tests/MongoFramework.Tests/Infrastructure/Commands/UpdateEntityCommandTests.cs b/tests/MongoFramework.Tests/Infrastructure/Commands/UpdateEntityCommandTests.cs index 1c1df420..56f7468e 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Commands/UpdateEntityCommandTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Commands/UpdateEntityCommandTests.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations; using System.Linq; -using System.Text; -using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoFramework.Infrastructure; using MongoFramework.Infrastructure.Commands; diff --git a/tests/MongoFramework.Tests/Infrastructure/DefinitionHelpers/UpdateDefinitionHelperTests.cs b/tests/MongoFramework.Tests/Infrastructure/DefinitionHelpers/UpdateDefinitionHelperTests.cs index 5be0744d..b57c659f 100644 --- a/tests/MongoFramework.Tests/Infrastructure/DefinitionHelpers/UpdateDefinitionHelperTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/DefinitionHelpers/UpdateDefinitionHelperTests.cs @@ -1,10 +1,10 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Driver; using MongoFramework.Infrastructure.DefinitionHelpers; -using System; -using System.Collections.Generic; namespace MongoFramework.Tests.Infrastructure.DefinitionHelpers { diff --git a/tests/MongoFramework.Tests/Infrastructure/EntityEntryContainerTests.cs b/tests/MongoFramework.Tests/Infrastructure/EntityEntryContainerTests.cs index 3315ce5d..02e53f68 100644 --- a/tests/MongoFramework.Tests/Infrastructure/EntityEntryContainerTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/EntityEntryContainerTests.cs @@ -1,242 +1,241 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using MongoFramework.Infrastructure; -using System; -using System.Linq; - -namespace MongoFramework.Tests.Infrastructure -{ - [TestClass] - public class EntityEntryContainerTests : TestBase - { - public class EntityEntryContainerModel - { - public string Id { get; set; } - public string Title { get; set; } - } - - public class EntityCollectionOverriddenEqualsModel - { - public string Id { get; set; } - - public string EqualsProperty { get; set; } - - public override bool Equals(object obj) - { - if (obj is EntityCollectionOverriddenEqualsModel model) - { - return EqualsProperty == model.EqualsProperty; - } - - return false; - } - - public override int GetHashCode() - { - return EqualsProperty?.GetHashCode() ?? base.GetHashCode(); - } - } - - public class EntityEntryContainerTenantModel : IHaveTenantId - { - public string TenantId { get; set; } - public string Id { get; set; } - public string Title { get; set; } - } - - [TestMethod] - public void AddNewEntry() - { - var entryContainer = new EntityEntryContainer(); - var entity = new EntityEntryContainerModel - { - Title = "EntityEntryContainerTests.AddNewEntry" - }; - entryContainer.SetEntityState(entity, EntityEntryState.Added); - - var entry = entryContainer.GetEntry(entity); - Assert.AreEqual(entity, entry.Entity); - Assert.AreEqual(EntityEntryState.Added, entry.State); - } - - [TestMethod] - public void UpdateExistingEntryIdMatch() - { - var entryContainer = new EntityEntryContainer(); - var entity = new EntityEntryContainerModel - { - Id = "123", - Title = "EntityEntryContainerTests.UpdateExistingEntryWithId-1" - }; - entryContainer.SetEntityState(entity, EntityEntryState.Added); - - var entry = entryContainer.GetEntry(entity); - Assert.AreEqual(entity, entry.Entity); - Assert.AreEqual(EntityEntryState.Added, entry.State); - - var updatedEntity = new EntityEntryContainerModel - { - Id = "123", - Title = "EntityEntryContainerTests.UpdateExistingEntryWithId-2" - }; - entryContainer.SetEntityState(updatedEntity, EntityEntryState.Updated); - - Assert.IsFalse(entryContainer.Entries().Any(e => e.Entity == entity)); - - entry = entryContainer.GetEntry(updatedEntity); - Assert.AreEqual(updatedEntity, entry.Entity); - Assert.AreEqual("EntityEntryContainerTests.UpdateExistingEntryWithId-2", (entry.Entity as EntityEntryContainerModel).Title); - } - - [TestMethod] - public void UpdateExistingEntryInstanceMatch() - { - var entryContainer = new EntityEntryContainer(); - var entity = new EntityEntryContainerModel - { - Title = "EntityEntryContainerTests.UpdateExistingEntryWithoutId" - }; - entryContainer.SetEntityState(entity, EntityEntryState.Added); - - var entry = entryContainer.GetEntry(entity); - Assert.AreEqual(entity, entry.Entity); - Assert.AreEqual(EntityEntryState.Added, entry.State); - - entryContainer.SetEntityState(entity, EntityEntryState.NoChanges); - - entry = entryContainer.GetEntry(entity); - Assert.AreEqual(entity, entry.Entity); - Assert.AreEqual(EntityEntryState.NoChanges, entry.State); - } - - [TestMethod] - public void EntryDoesntMatchOnEqualityOverride() - { - var entryContainer = new EntityEntryContainer(); - var entityA = new EntityCollectionOverriddenEqualsModel - { - EqualsProperty = "EntityEntryContainerTests.EntityCollectionOverriddenEqualsModel" - }; - entryContainer.SetEntityState(entityA, EntityEntryState.Added); - - var entityB = new EntityCollectionOverriddenEqualsModel - { - EqualsProperty = "EntityEntryContainerTests.EntityCollectionOverriddenEqualsModel" - }; - entryContainer.SetEntityState(entityB, EntityEntryState.Added); - Assert.AreEqual(2, entryContainer.Entries().Count()); - } - - [TestMethod] - public void GetExistingEntryIdMatch() - { - var entryContainer = new EntityEntryContainer(); - var entity = new EntityEntryContainerModel - { - Id = "123", - Title = "EntityEntryContainerTests.GetExistingEntryIdMatch" - }; - entryContainer.SetEntityState(entity, EntityEntryState.Added); - - var entry = entryContainer.GetEntryById("123"); - Assert.AreEqual(entity, entry.Entity); - Assert.AreEqual(EntityEntryState.Added, entry.State); - - entryContainer.SetEntityState(entity, EntityEntryState.NoChanges); - - entry = entryContainer.GetEntryById("123"); - Assert.AreEqual(entity, entry.Entity); - Assert.AreEqual(EntityEntryState.NoChanges, entry.State); - } - - [TestMethod] - public void RemoveRange() - { - var entryContainer = new EntityEntryContainer(); - var entities = new[] - { - new EntityEntryContainerModel - { - Title = "EntityEntryContainerTests.RemoveRange-1" - }, - new EntityEntryContainerModel - { - Title = "EntityEntryContainerTests.RemoveRange-2" - }, - new EntityEntryContainerModel - { - Id = "123", - Title = "EntityEntryContainerTests.RemoveRange-3" - } - }; - - foreach (var entity in entities) - { - entryContainer.SetEntityState(entity, EntityEntryState.Added); - entryContainer.SetEntityState(entity, EntityEntryState.Detached); - } - - Assert.IsFalse(entryContainer.Entries().Any()); - } - - [TestMethod] - public void ClearTracker() - { - var entryContainer = new EntityEntryContainer(); - var entity = new EntityEntryContainerModel - { - Title = "EntityEntryContainerTests.ClearTracker" - }; - entryContainer.SetEntityState(entity, EntityEntryState.NoChanges); - - entryContainer.Clear(); - - Assert.IsFalse(entryContainer.Entries().Any()); - } - - [TestMethod] - public void RemoveNonExistentEntities() - { - var entryContainer = new EntityEntryContainer(); - Assert.IsNull(entryContainer.SetEntityState(new EntityEntryContainerModel { }, EntityEntryState.Detached)); +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MongoFramework.Infrastructure; + +namespace MongoFramework.Tests.Infrastructure +{ + [TestClass] + public class EntityEntryContainerTests : TestBase + { + public class EntityEntryContainerModel + { + public string Id { get; set; } + public string Title { get; set; } + } + + public class EntityCollectionOverriddenEqualsModel + { + public string Id { get; set; } + + public string EqualsProperty { get; set; } + + public override bool Equals(object obj) + { + if (obj is EntityCollectionOverriddenEqualsModel model) + { + return EqualsProperty == model.EqualsProperty; + } + + return false; + } + + public override int GetHashCode() + { + return EqualsProperty?.GetHashCode() ?? base.GetHashCode(); + } + } + + public class EntityEntryContainerTenantModel : IHaveTenantId + { + public string TenantId { get; set; } + public string Id { get; set; } + public string Title { get; set; } + } + + [TestMethod] + public void AddNewEntry() + { + var entryContainer = new EntityEntryContainer(); + var entity = new EntityEntryContainerModel + { + Title = "EntityEntryContainerTests.AddNewEntry" + }; + entryContainer.SetEntityState(entity, EntityEntryState.Added); + + var entry = entryContainer.GetEntry(entity); + Assert.AreEqual(entity, entry.Entity); + Assert.AreEqual(EntityEntryState.Added, entry.State); + } + + [TestMethod] + public void UpdateExistingEntryIdMatch() + { + var entryContainer = new EntityEntryContainer(); + var entity = new EntityEntryContainerModel + { + Id = "123", + Title = "EntityEntryContainerTests.UpdateExistingEntryWithId-1" + }; + entryContainer.SetEntityState(entity, EntityEntryState.Added); + + var entry = entryContainer.GetEntry(entity); + Assert.AreEqual(entity, entry.Entity); + Assert.AreEqual(EntityEntryState.Added, entry.State); + + var updatedEntity = new EntityEntryContainerModel + { + Id = "123", + Title = "EntityEntryContainerTests.UpdateExistingEntryWithId-2" + }; + entryContainer.SetEntityState(updatedEntity, EntityEntryState.Updated); + + Assert.IsFalse(entryContainer.Entries().Any(e => e.Entity == entity)); + + entry = entryContainer.GetEntry(updatedEntity); + Assert.AreEqual(updatedEntity, entry.Entity); + Assert.AreEqual("EntityEntryContainerTests.UpdateExistingEntryWithId-2", (entry.Entity as EntityEntryContainerModel).Title); + } + + [TestMethod] + public void UpdateExistingEntryInstanceMatch() + { + var entryContainer = new EntityEntryContainer(); + var entity = new EntityEntryContainerModel + { + Title = "EntityEntryContainerTests.UpdateExistingEntryWithoutId" + }; + entryContainer.SetEntityState(entity, EntityEntryState.Added); + + var entry = entryContainer.GetEntry(entity); + Assert.AreEqual(entity, entry.Entity); + Assert.AreEqual(EntityEntryState.Added, entry.State); + + entryContainer.SetEntityState(entity, EntityEntryState.NoChanges); + + entry = entryContainer.GetEntry(entity); + Assert.AreEqual(entity, entry.Entity); + Assert.AreEqual(EntityEntryState.NoChanges, entry.State); + } + + [TestMethod] + public void EntryDoesntMatchOnEqualityOverride() + { + var entryContainer = new EntityEntryContainer(); + var entityA = new EntityCollectionOverriddenEqualsModel + { + EqualsProperty = "EntityEntryContainerTests.EntityCollectionOverriddenEqualsModel" + }; + entryContainer.SetEntityState(entityA, EntityEntryState.Added); + + var entityB = new EntityCollectionOverriddenEqualsModel + { + EqualsProperty = "EntityEntryContainerTests.EntityCollectionOverriddenEqualsModel" + }; + entryContainer.SetEntityState(entityB, EntityEntryState.Added); + Assert.AreEqual(2, entryContainer.Entries().Count()); + } + + [TestMethod] + public void GetExistingEntryIdMatch() + { + var entryContainer = new EntityEntryContainer(); + var entity = new EntityEntryContainerModel + { + Id = "123", + Title = "EntityEntryContainerTests.GetExistingEntryIdMatch" + }; + entryContainer.SetEntityState(entity, EntityEntryState.Added); + + var entry = entryContainer.GetEntryById("123"); + Assert.AreEqual(entity, entry.Entity); + Assert.AreEqual(EntityEntryState.Added, entry.State); + + entryContainer.SetEntityState(entity, EntityEntryState.NoChanges); + + entry = entryContainer.GetEntryById("123"); + Assert.AreEqual(entity, entry.Entity); + Assert.AreEqual(EntityEntryState.NoChanges, entry.State); + } + + [TestMethod] + public void RemoveRange() + { + var entryContainer = new EntityEntryContainer(); + var entities = new[] + { + new EntityEntryContainerModel + { + Title = "EntityEntryContainerTests.RemoveRange-1" + }, + new EntityEntryContainerModel + { + Title = "EntityEntryContainerTests.RemoveRange-2" + }, + new EntityEntryContainerModel + { + Id = "123", + Title = "EntityEntryContainerTests.RemoveRange-3" + } + }; + + foreach (var entity in entities) + { + entryContainer.SetEntityState(entity, EntityEntryState.Added); + entryContainer.SetEntityState(entity, EntityEntryState.Detached); + } + + Assert.IsFalse(entryContainer.Entries().Any()); + } + + [TestMethod] + public void ClearTracker() + { + var entryContainer = new EntityEntryContainer(); + var entity = new EntityEntryContainerModel + { + Title = "EntityEntryContainerTests.ClearTracker" + }; + entryContainer.SetEntityState(entity, EntityEntryState.NoChanges); + + entryContainer.Clear(); + + Assert.IsFalse(entryContainer.Entries().Any()); + } + + [TestMethod] + public void RemoveNonExistentEntities() + { + var entryContainer = new EntityEntryContainer(); + Assert.IsNull(entryContainer.SetEntityState(new EntityEntryContainerModel { }, EntityEntryState.Detached)); + } + + [TestMethod] + public void EnforceMultiTenantRequiresTenantId() + { + var entryContainer = new EntityEntryContainer(); + var entity = new EntityEntryContainerTenantModel + { + TenantId = TestConfiguration.GetTenantId(), + Title = "EntityEntryContainerTests.EnforceMultiTenantRequiresTenantId" + }; + entryContainer.SetEntityState(entity, EntityEntryState.Added); + + Assert.ThrowsException(() => entryContainer.EnforceMultiTenant(null)); + Assert.ThrowsException(() => entryContainer.EnforceMultiTenant(" ")); } - [TestMethod] - public void EnforceMultiTenantRequiresTenantId() - { - var entryContainer = new EntityEntryContainer(); - var entity = new EntityEntryContainerTenantModel - { - TenantId = TestConfiguration.GetTenantId(), - Title = "EntityEntryContainerTests.EnforceMultiTenantRequiresTenantId" - }; - entryContainer.SetEntityState(entity, EntityEntryState.Added); - - Assert.ThrowsException(() => entryContainer.EnforceMultiTenant(null)); - Assert.ThrowsException(() => entryContainer.EnforceMultiTenant(" ")); - } - - [TestMethod] - public void EnforceMultiTenantsSkipsNonTenantModels() - { - var entryContainer = new EntityEntryContainer(); - - var entity = new EntityEntryContainerModel - { - Title = "EntityEntryContainerTests.RequireTenantIdForTenantModels" - }; - entryContainer.SetEntityState(entity, EntityEntryState.Added); - - var entity2 = new EntityEntryContainerTenantModel - { - TenantId = TestConfiguration.GetTenantId(), - Title = "EntityEntryContainerTests.RequireTenantIdForTenantModels" - }; + [TestMethod] + public void EnforceMultiTenantsSkipsNonTenantModels() + { + var entryContainer = new EntityEntryContainer(); + + var entity = new EntityEntryContainerModel + { + Title = "EntityEntryContainerTests.RequireTenantIdForTenantModels" + }; + entryContainer.SetEntityState(entity, EntityEntryState.Added); + + var entity2 = new EntityEntryContainerTenantModel + { + TenantId = TestConfiguration.GetTenantId(), + Title = "EntityEntryContainerTests.RequireTenantIdForTenantModels" + }; entryContainer.SetEntityState(entity2, EntityEntryState.Added); entryContainer.EnforceMultiTenant(TestConfiguration.GetTenantId()); Assert.AreEqual(entryContainer.Entries().Count(), 2); - } - - } + } + + } } \ No newline at end of file diff --git a/tests/MongoFramework.Tests/Infrastructure/Indexing/EntityIndexWriterTests.cs b/tests/MongoFramework.Tests/Infrastructure/Indexing/EntityIndexWriterTests.cs index 97b3b961..f0641e71 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Indexing/EntityIndexWriterTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Indexing/EntityIndexWriterTests.cs @@ -1,10 +1,10 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoDB.Driver; using MongoDB.Driver.GeoJsonObjectModel; using MongoFramework.Attributes; using MongoFramework.Infrastructure.Indexing; -using System; -using System.Threading.Tasks; namespace MongoFramework.Tests.Infrastructure.Indexing { diff --git a/tests/MongoFramework.Tests/Infrastructure/Indexing/IndexModelBuilderTests.cs b/tests/MongoFramework.Tests/Infrastructure/Indexing/IndexModelBuilderTests.cs index 922ca465..b3288ba2 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Indexing/IndexModelBuilderTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Indexing/IndexModelBuilderTests.cs @@ -1,9 +1,9 @@ +using System.Collections.Generic; +using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoDB.Driver.GeoJsonObjectModel; using MongoFramework.Attributes; using MongoFramework.Infrastructure.Indexing; -using System.Collections.Generic; -using System.Linq; namespace MongoFramework.Tests.Infrastructure.Indexing.Processors { @@ -106,7 +106,7 @@ public class MultipleIndexesOnSinglePropertyModel public void IndexNaming() { var indexModel = IndexModelBuilder.BuildModel(); - + Assert.AreEqual(2, indexModel.Count()); Assert.IsTrue(indexModel.Any(m => m.Options.Name == null)); Assert.IsTrue(indexModel.Any(m => m.Options.Name == "MyCustomIndexName")); diff --git a/tests/MongoFramework.Tests/Infrastructure/Linq/MongoFrameworkQueryableTests.cs b/tests/MongoFramework.Tests/Infrastructure/Linq/MongoFrameworkQueryableTests.cs index 4da1df80..dde5c004 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Linq/MongoFrameworkQueryableTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Linq/MongoFrameworkQueryableTests.cs @@ -1,9 +1,9 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoDB.Driver; using MongoFramework.Infrastructure; using MongoFramework.Infrastructure.Linq; using MongoFramework.Infrastructure.Mapping; -using System.Linq; namespace MongoFramework.Tests.Infrastructure.Linq { diff --git a/tests/MongoFramework.Tests/Infrastructure/Linq/Processors/EntityTrackingProcessorTests.cs b/tests/MongoFramework.Tests/Infrastructure/Linq/Processors/EntityTrackingProcessorTests.cs index a405a805..50f95947 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Linq/Processors/EntityTrackingProcessorTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Linq/Processors/EntityTrackingProcessorTests.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoFramework.Infrastructure; using MongoFramework.Infrastructure.Linq.Processors; diff --git a/tests/MongoFramework.Tests/Infrastructure/Mapping/EntityDefinitionExtensionTests.cs b/tests/MongoFramework.Tests/Infrastructure/Mapping/EntityDefinitionExtensionTests.cs index 3a03608f..82bedb9f 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Mapping/EntityDefinitionExtensionTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Mapping/EntityDefinitionExtensionTests.cs @@ -1,143 +1,141 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoFramework.Infrastructure.Commands; -using MongoFramework.Infrastructure.Mapping; - -namespace MongoFramework.Tests.Infrastructure.Mapping -{ - [TestClass] - public class EntityDefinitionExtensionTests : TestBase - { - public class IdNameParentModel - { - public string Id { get; set; } - } - - public class IdNameChildModel : IdNameParentModel - { - public string Description { get; set; } - } - - public class TraverseMappingModel - { - public string Id { get; set; } - public NestedTraverseMappingModel NestedModel { get; set; } - public NestedTraverseMappingModel RepeatedType { get; set; } - public TraverseMappingModel RecursionType { get; set; } - - public NestedTraverseMappingModel[] ArrayModel { get; set; } - public IEnumerable EnumerableModel { get; set; } - public List ListModel { get; set; } - - } - public class NestedTraverseMappingModel - { - public string PropertyOne { get; set; } - public int PropertyTwo { get; set; } - public InnerNestedTraverseMappingModel InnerModel { get; set; } - } - public class InnerNestedTraverseMappingModel - { - public string InnerMostProperty { get; set; } - public TraverseMappingModel NestedRecursionType { get; set; } - } - - public class OverridePropertyBaseModel - { - public virtual string TargetProperty { get; set; } - } - - public class OverridePropertyChildModel : OverridePropertyBaseModel - { - public override string TargetProperty { get; set; } - } - - public class OverridePropertyGrandChildModel : OverridePropertyChildModel - { - - } - +using MongoFramework.Infrastructure.Mapping; + +namespace MongoFramework.Tests.Infrastructure.Mapping +{ + [TestClass] + public class EntityDefinitionExtensionTests : TestBase + { + public class IdNameParentModel + { + public string Id { get; set; } + } + + public class IdNameChildModel : IdNameParentModel + { + public string Description { get; set; } + } + + public class TraverseMappingModel + { + public string Id { get; set; } + public NestedTraverseMappingModel NestedModel { get; set; } + public NestedTraverseMappingModel RepeatedType { get; set; } + public TraverseMappingModel RecursionType { get; set; } + + public NestedTraverseMappingModel[] ArrayModel { get; set; } + public IEnumerable EnumerableModel { get; set; } + public List ListModel { get; set; } + + } + public class NestedTraverseMappingModel + { + public string PropertyOne { get; set; } + public int PropertyTwo { get; set; } + public InnerNestedTraverseMappingModel InnerModel { get; set; } + } + public class InnerNestedTraverseMappingModel + { + public string InnerMostProperty { get; set; } + public TraverseMappingModel NestedRecursionType { get; set; } + } + + public class OverridePropertyBaseModel + { + public virtual string TargetProperty { get; set; } + } + + public class OverridePropertyChildModel : OverridePropertyBaseModel + { + public override string TargetProperty { get; set; } + } + + public class OverridePropertyGrandChildModel : OverridePropertyChildModel + { + + } + public class TenantModel : IHaveTenantId { public string Id { get; set; } public string TenantId { get; set; } - } - - [TestMethod] - public void GetIdNameChecksInheritence() - { - var definition = EntityMapping.RegisterType(typeof(IdNameChildModel)); - var parentDefinition = EntityMapping.GetOrCreateDefinition(typeof(IdNameParentModel)); - - Assert.AreEqual("Id", definition.GetIdName()); - Assert.AreEqual("Id", parentDefinition.GetIdName()); - } - - [TestMethod] - public void TraverseMapping() - { - var definition = EntityMapping.RegisterType(typeof(TraverseMappingModel)); - var result = definition.TraverseProperties().ToArray(); - - Assert.AreEqual(32, result.Length); - Assert.IsTrue(result.Any(m => m.EntityType == typeof(NestedTraverseMappingModel))); - Assert.IsTrue(result.Any(m => m.EntityType == typeof(InnerNestedTraverseMappingModel))); - - Assert.IsTrue(result.Any(m => m.FullPath == "RecursionType")); - - Assert.IsTrue(result.Any(m => m.FullPath == "NestedModel.PropertyOne")); - Assert.IsTrue(result.Any(m => m.FullPath == "NestedModel.InnerModel")); - Assert.IsTrue(result.Any(m => m.FullPath == "NestedModel.InnerModel.InnerMostProperty")); - Assert.IsTrue(result.Any(m => m.FullPath == "NestedModel.InnerModel.NestedRecursionType")); - - Assert.IsTrue(result.Any(m => m.FullPath == "RepeatedType.PropertyOne")); - Assert.IsTrue(result.Any(m => m.FullPath == "RepeatedType.InnerModel")); - Assert.IsTrue(result.Any(m => m.FullPath == "RepeatedType.InnerModel.InnerMostProperty")); - Assert.IsTrue(result.Any(m => m.FullPath == "RepeatedType.InnerModel.NestedRecursionType")); - - Assert.IsTrue(result.Any(m => m.FullPath == "ArrayModel")); - Assert.IsTrue(result.Any(m => m.FullPath == "ArrayModel.InnerModel")); - Assert.IsTrue(result.Any(m => m.FullPath == "ArrayModel.InnerModel.InnerMostProperty")); - Assert.IsTrue(result.Any(m => m.FullPath == "ArrayModel.InnerModel.NestedRecursionType")); - - Assert.IsTrue(result.Any(m => m.FullPath == "EnumerableModel")); - Assert.IsTrue(result.Any(m => m.FullPath == "EnumerableModel.InnerModel")); - Assert.IsTrue(result.Any(m => m.FullPath == "EnumerableModel.InnerModel.InnerMostProperty")); - Assert.IsTrue(result.Any(m => m.FullPath == "EnumerableModel.InnerModel.NestedRecursionType")); - - Assert.IsTrue(result.Any(m => m.FullPath == "ListModel")); - Assert.IsTrue(result.Any(m => m.FullPath == "ListModel.InnerModel")); - Assert.IsTrue(result.Any(m => m.FullPath == "ListModel.InnerModel.InnerMostProperty")); - Assert.IsTrue(result.Any(m => m.FullPath == "ListModel.InnerModel.NestedRecursionType")); - } - - [TestMethod] - public void GetInheritedPropertiesTakesBaseProperties() - { - var definition = EntityMapping.RegisterType(typeof(OverridePropertyGrandChildModel)); - var inheritedProperties = definition.GetInheritedProperties().ToArray(); - Assert.AreEqual(1, inheritedProperties.Length); - Assert.AreEqual(typeof(OverridePropertyBaseModel), inheritedProperties[0].EntityType); - } - [TestMethod] - public void GetAllPropertiesTakesBaseProperties() - { - var definition = EntityMapping.RegisterType(typeof(OverridePropertyChildModel)); - var allProperties = definition.GetAllProperties().ToArray(); - Assert.AreEqual(1, allProperties.Length); - Assert.AreEqual(typeof(OverridePropertyBaseModel), allProperties[0].EntityType); } - - [TestMethod] - public void GetTenantModelIdRequiresTenant() - { - var definition = EntityMapping.RegisterType(typeof(TenantModel)); - Assert.ThrowsException(() => definition.CreateIdFilter("id")); - } - - } -} + + [TestMethod] + public void GetIdNameChecksInheritence() + { + var definition = EntityMapping.RegisterType(typeof(IdNameChildModel)); + var parentDefinition = EntityMapping.GetOrCreateDefinition(typeof(IdNameParentModel)); + + Assert.AreEqual("Id", definition.GetIdName()); + Assert.AreEqual("Id", parentDefinition.GetIdName()); + } + + [TestMethod] + public void TraverseMapping() + { + var definition = EntityMapping.RegisterType(typeof(TraverseMappingModel)); + var result = definition.TraverseProperties().ToArray(); + + Assert.AreEqual(32, result.Length); + Assert.IsTrue(result.Any(m => m.EntityType == typeof(NestedTraverseMappingModel))); + Assert.IsTrue(result.Any(m => m.EntityType == typeof(InnerNestedTraverseMappingModel))); + + Assert.IsTrue(result.Any(m => m.FullPath == "RecursionType")); + + Assert.IsTrue(result.Any(m => m.FullPath == "NestedModel.PropertyOne")); + Assert.IsTrue(result.Any(m => m.FullPath == "NestedModel.InnerModel")); + Assert.IsTrue(result.Any(m => m.FullPath == "NestedModel.InnerModel.InnerMostProperty")); + Assert.IsTrue(result.Any(m => m.FullPath == "NestedModel.InnerModel.NestedRecursionType")); + + Assert.IsTrue(result.Any(m => m.FullPath == "RepeatedType.PropertyOne")); + Assert.IsTrue(result.Any(m => m.FullPath == "RepeatedType.InnerModel")); + Assert.IsTrue(result.Any(m => m.FullPath == "RepeatedType.InnerModel.InnerMostProperty")); + Assert.IsTrue(result.Any(m => m.FullPath == "RepeatedType.InnerModel.NestedRecursionType")); + + Assert.IsTrue(result.Any(m => m.FullPath == "ArrayModel")); + Assert.IsTrue(result.Any(m => m.FullPath == "ArrayModel.InnerModel")); + Assert.IsTrue(result.Any(m => m.FullPath == "ArrayModel.InnerModel.InnerMostProperty")); + Assert.IsTrue(result.Any(m => m.FullPath == "ArrayModel.InnerModel.NestedRecursionType")); + + Assert.IsTrue(result.Any(m => m.FullPath == "EnumerableModel")); + Assert.IsTrue(result.Any(m => m.FullPath == "EnumerableModel.InnerModel")); + Assert.IsTrue(result.Any(m => m.FullPath == "EnumerableModel.InnerModel.InnerMostProperty")); + Assert.IsTrue(result.Any(m => m.FullPath == "EnumerableModel.InnerModel.NestedRecursionType")); + + Assert.IsTrue(result.Any(m => m.FullPath == "ListModel")); + Assert.IsTrue(result.Any(m => m.FullPath == "ListModel.InnerModel")); + Assert.IsTrue(result.Any(m => m.FullPath == "ListModel.InnerModel.InnerMostProperty")); + Assert.IsTrue(result.Any(m => m.FullPath == "ListModel.InnerModel.NestedRecursionType")); + } + + [TestMethod] + public void GetInheritedPropertiesTakesBaseProperties() + { + var definition = EntityMapping.RegisterType(typeof(OverridePropertyGrandChildModel)); + var inheritedProperties = definition.GetInheritedProperties().ToArray(); + Assert.AreEqual(1, inheritedProperties.Length); + Assert.AreEqual(typeof(OverridePropertyBaseModel), inheritedProperties[0].EntityType); + } + [TestMethod] + public void GetAllPropertiesTakesBaseProperties() + { + var definition = EntityMapping.RegisterType(typeof(OverridePropertyChildModel)); + var allProperties = definition.GetAllProperties().ToArray(); + Assert.AreEqual(1, allProperties.Length); + Assert.AreEqual(typeof(OverridePropertyBaseModel), allProperties[0].EntityType); + } + + [TestMethod] + public void GetTenantModelIdRequiresTenant() + { + var definition = EntityMapping.RegisterType(typeof(TenantModel)); + Assert.ThrowsException(() => definition.CreateIdFilter("id")); + } + + } +} diff --git a/tests/MongoFramework.Tests/Infrastructure/Mapping/EntityMappingTests.cs b/tests/MongoFramework.Tests/Infrastructure/Mapping/EntityMappingTests.cs index c300a940..a44290c1 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Mapping/EntityMappingTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Mapping/EntityMappingTests.cs @@ -1,9 +1,7 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using MongoFramework.Infrastructure.Mapping; -using System; -using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; +using System; using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MongoFramework.Infrastructure.Mapping; namespace MongoFramework.Tests.Infrastructure.Mapping { @@ -18,7 +16,7 @@ public class MappingLockModel /// /// A potentially common issue for web application startup, this tests that multiple threads /// can map a class at the same time without concurrency issues. - /// + /// /// Relates to: https://github.com/TurnerSoftware/MongoFramework/issues/7 /// [TestMethod] diff --git a/tests/MongoFramework.Tests/Infrastructure/Mapping/MappingTestBase.cs b/tests/MongoFramework.Tests/Infrastructure/Mapping/MappingTestBase.cs index 6737bbea..41cdfc92 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Mapping/MappingTestBase.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Mapping/MappingTestBase.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoFramework.Infrastructure.Mapping; namespace MongoFramework.Tests.Infrastructure.Mapping diff --git a/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/CollectionNameProcessorTests.cs b/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/CollectionNameProcessorTests.cs index 3da77cc4..02186707 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/CollectionNameProcessorTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/CollectionNameProcessorTests.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.ComponentModel.DataAnnotations.Schema; using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoFramework.Infrastructure.Mapping; using MongoFramework.Infrastructure.Mapping.Processors; diff --git a/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/EntityIdProcessorTests.cs b/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/EntityIdProcessorTests.cs index 48680128..fe7de834 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/EntityIdProcessorTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/EntityIdProcessorTests.cs @@ -1,12 +1,12 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.IdGenerators; using MongoFramework.Infrastructure.Mapping; using MongoFramework.Infrastructure.Mapping.Processors; -using System; -using System.ComponentModel.DataAnnotations; -using System.Linq; namespace MongoFramework.Tests.Infrastructure.Mapping.Processors { diff --git a/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/ExtraElementsProcessorTests.cs b/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/ExtraElementsProcessorTests.cs index f5f81260..c5bfd18f 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/ExtraElementsProcessorTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/ExtraElementsProcessorTests.cs @@ -1,12 +1,12 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoDB.Bson.Serialization; using MongoFramework.Attributes; using MongoFramework.Infrastructure; using MongoFramework.Infrastructure.Mapping; using MongoFramework.Infrastructure.Mapping.Processors; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; namespace MongoFramework.Tests.Infrastructure.Mapping.Processors { diff --git a/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/HierarchyProcessorTests.cs b/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/HierarchyProcessorTests.cs index 0834e361..85badf47 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/HierarchyProcessorTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/HierarchyProcessorTests.cs @@ -1,8 +1,8 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoDB.Bson.Serialization; using MongoFramework.Infrastructure.Mapping; using MongoFramework.Infrastructure.Mapping.Processors; -using System.Linq; namespace MongoFramework.Tests.Infrastructure.Mapping.Processors { diff --git a/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/MappingAdapterProcessorTests.cs b/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/MappingAdapterProcessorTests.cs index 601468ab..741b788a 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/MappingAdapterProcessorTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/MappingAdapterProcessorTests.cs @@ -1,15 +1,11 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using MongoDB.Bson; +using System; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoDB.Bson.Serialization; -using MongoDB.Bson.Serialization.IdGenerators; using MongoFramework.Attributes; using MongoFramework.Infrastructure.Mapping; using MongoFramework.Infrastructure.Mapping.Processors; -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; namespace MongoFramework.Tests.Infrastructure.Mapping.Processors { @@ -26,7 +22,7 @@ public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap) var definitionIndexes = definition.Indexes.ToList(); definitionIndexes.Add(new EntityIndex - { + { Property = definition.GetProperty("UserName"), IsUnique = true, SortOrder = IndexSortOrder.Ascending @@ -53,7 +49,7 @@ public class AdapterTestModelMappingAdapterNoInterface [MappingAdapter(typeof(AdapterTestModelMappingAdapterNoInterface))] public class AdapterTestModelNoInterface { - // broken adapter + // broken adapter } public class AdapterTestModelMappingAdapterConstructor : IMappingProcessor @@ -72,7 +68,7 @@ public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap) [MappingAdapter(typeof(AdapterTestModelMappingAdapterConstructor))] public class AdapterTestModelConstructor { - // broken adapter + // broken adapter } [TestMethod] diff --git a/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/NestedTypeProcessorTests.cs b/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/NestedTypeProcessorTests.cs index f4887f3e..812b79f7 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/NestedTypeProcessorTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/NestedTypeProcessorTests.cs @@ -1,9 +1,8 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoDB.Bson.Serialization; using MongoFramework.Infrastructure.Mapping; using MongoFramework.Infrastructure.Mapping.Processors; -using System.Collections.Generic; -using System.Linq; namespace MongoFramework.Tests.Infrastructure.Mapping.Processors { diff --git a/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/PropertyMappingProcessorTests.cs b/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/PropertyMappingProcessorTests.cs index 6547c22d..80c0acee 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/PropertyMappingProcessorTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Mapping/Processors/PropertyMappingProcessorTests.cs @@ -1,10 +1,10 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoFramework.Infrastructure.Mapping; using MongoFramework.Infrastructure.Mapping.Processors; -using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; namespace MongoFramework.Tests.Infrastructure.Mapping.Processors { diff --git a/tests/MongoFramework.Tests/Infrastructure/Serialization/TypeDiscoveryIntegrationTests.cs b/tests/MongoFramework.Tests/Infrastructure/Serialization/TypeDiscoveryIntegrationTests.cs index ea7a56ab..d45e706a 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Serialization/TypeDiscoveryIntegrationTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Serialization/TypeDiscoveryIntegrationTests.cs @@ -1,12 +1,7 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using MongoDB.Bson; -using MongoDB.Bson.Serialization; -using MongoFramework.Attributes; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Reflection; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MongoFramework.Attributes; namespace MongoFramework.Tests.Infrastructure.Serialization { diff --git a/tests/MongoFramework.Tests/Infrastructure/Serialization/TypeDiscoverySerializationTests.cs b/tests/MongoFramework.Tests/Infrastructure/Serialization/TypeDiscoverySerializationTests.cs index 23a6258f..e33b3d74 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Serialization/TypeDiscoverySerializationTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Serialization/TypeDiscoverySerializationTests.cs @@ -1,4 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoDB.Bson; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; @@ -6,8 +8,6 @@ using MongoFramework.Infrastructure.Mapping; using MongoFramework.Infrastructure.Mapping.Processors; using MongoFramework.Infrastructure.Serialization; -using System; -using System.Collections.Generic; namespace MongoFramework.Tests.Infrastructure.Serialization { diff --git a/tests/MongoFramework.Tests/Linq/LinqExtensionsTests.cs b/tests/MongoFramework.Tests/Linq/LinqExtensionsTests.cs index aa369f3f..012a6cc3 100644 --- a/tests/MongoFramework.Tests/Linq/LinqExtensionsTests.cs +++ b/tests/MongoFramework.Tests/Linq/LinqExtensionsTests.cs @@ -1,215 +1,213 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using MongoDB.Bson; -using MongoDB.Driver; -using MongoFramework.Attributes; -using MongoFramework.Infrastructure; -using MongoFramework.Infrastructure.Linq; -using MongoFramework.Infrastructure.Mapping; -using MongoFramework.Linq; -using System; -using System.Collections.Generic; -using System.Linq; - -namespace MongoFramework.Tests.Linq -{ - [TestClass] - public class LinqExtensionsTests : TestBase - { - public class LinqExtensionsModel - { - public string Id { get; set; } - } - public class WhereIdMatchesGuidModel - { - public Guid Id { get; set; } - public string Description { get; set; } - } - public class WhereIdMatchesObjectIdModel - { - public ObjectId Id { get; set; } - public string Description { get; set; } - } - public class WhereIdMatchesStringModel - { - public string Id { get; set; } - public string Description { get; set; } - } - - public class SearchTextModel - { - public string Id { get; set; } - [Index(IndexType.Text)] - public string Text { get; set; } - public int MiscField { get; set; } - } - - public class SearchTextTenantModel : IHaveTenantId - { - public string TenantId { get; set; } - public string Id { get; set; } - [Index(IndexType.Text)] - public string Text { get; set; } - public int MiscField { get; set; } - } - - [TestMethod] - public void ValidToQuery() - { - EntityMapping.RegisterType(typeof(LinqExtensionsModel)); - - var connection = TestConfiguration.GetConnection(); - var provider = new MongoFrameworkQueryProvider(connection); - var queryable = new MongoFrameworkQueryable(provider); - var result = LinqExtensions.ToQuery(queryable); - - Assert.AreEqual("db.LinqExtensionsModel.aggregate([])", result); - } - - [TestMethod] - [ExpectedException(typeof(ArgumentException), "ArgumentException")] - public void InvalidToQuery() - { - LinqExtensions.ToQuery(null); - } - - [TestMethod] - public void WhereIdMatchesGuids() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var entityCollection = new[] - { - new WhereIdMatchesGuidModel { Description = "1" }, - new WhereIdMatchesGuidModel { Description = "2" }, - new WhereIdMatchesGuidModel { Description = "3" }, - new WhereIdMatchesGuidModel { Description = "4" } - }; - dbSet.AddRange(entityCollection); - context.SaveChanges(); - - var provider = new MongoFrameworkQueryProvider(connection); - var queryable = new MongoFrameworkQueryable(provider); - - var entityIds = entityCollection.Select(e => e.Id).Take(2); - - var idMatchQueryable = LinqExtensions.WhereIdMatches(queryable, entityIds); - - Assert.AreEqual(2, idMatchQueryable.Count()); - Assert.IsTrue(idMatchQueryable.ToList().All(e => entityIds.Contains(e.Id))); - } - - [TestMethod] - public void WhereIdMatchesObjectIds() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var entityCollection = new[] - { - new WhereIdMatchesObjectIdModel { Description = "1" }, - new WhereIdMatchesObjectIdModel { Description = "2" }, - new WhereIdMatchesObjectIdModel { Description = "3" }, - new WhereIdMatchesObjectIdModel { Description = "4" } - }; - dbSet.AddRange(entityCollection); - context.SaveChanges(); - - var provider = new MongoFrameworkQueryProvider(connection); - var queryable = new MongoFrameworkQueryable(provider); - - var entityIds = entityCollection.Select(e => e.Id).Take(2); - - var idMatchQueryable = LinqExtensions.WhereIdMatches(queryable, entityIds); - - Assert.AreEqual(2, idMatchQueryable.Count()); - Assert.IsTrue(idMatchQueryable.ToList().All(e => entityIds.Contains(e.Id))); - } - - [TestMethod] - public void WhereIdMatchesStringIds() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var entityCollection = new[] - { - new WhereIdMatchesStringModel { Description = "1" }, - new WhereIdMatchesStringModel { Description = "2" }, - new WhereIdMatchesStringModel { Description = "3" }, - new WhereIdMatchesStringModel { Description = "4" } - }; - dbSet.AddRange(entityCollection); - context.SaveChanges(); - - var provider = new MongoFrameworkQueryProvider(connection); - var queryable = new MongoFrameworkQueryable(provider); - - var entityIds = entityCollection.Select(e => e.Id).Take(2); - - var idMatchQueryable = LinqExtensions.WhereIdMatches(queryable, entityIds); - - Assert.AreEqual(2, idMatchQueryable.Count()); - Assert.IsTrue(idMatchQueryable.ToList().All(e => entityIds.Contains(e.Id))); - } - - [TestMethod] - public void SearchText() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - dbSet.AddRange(new SearchTextModel[] - { - new SearchTextModel { MiscField = 1, Text = "The quick brown fox jumps over the lazy dog." }, - new SearchTextModel { MiscField = 2, Text = "The five boxing wizards jump quickly." }, - new SearchTextModel { MiscField = 3, Text = "The quick brown fox jumps over the lazy dog." }, - new SearchTextModel { MiscField = 4, Text = "Jived fox nymph grabs quick waltz." }, - }); - context.SaveChanges(); - - Assert.AreEqual(4, dbSet.SearchText("quick").Count()); - Assert.AreEqual(0, dbSet.SearchText("the").Count()); //Stop words aren't used in text indexes: https://docs.mongodb.com/manual/core/index-text/#supported-languages-and-stop-words - Assert.AreEqual(2, dbSet.SearchText("dog").Count()); - Assert.AreEqual(1, dbSet.SearchText("jived").Count()); - - Assert.AreEqual(1, dbSet.SearchText("quick").Where(e => e.MiscField == 3).Count()); - } - - [TestMethod] - public void SearchTextTenant() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbTenantContext(connection, TestConfiguration.GetTenantId()); - var dbSet = new MongoDbTenantSet(context); - - var models = new SearchTextTenantModel[] - { - new SearchTextTenantModel { MiscField = 1, Text = "The quick brown fox jumps over the lazy dog." }, - new SearchTextTenantModel { MiscField = 2, Text = "The five boxing wizards jump quickly." }, - new SearchTextTenantModel { MiscField = 3, Text = "The quick brown fox jumps over the lazy dog." }, - new SearchTextTenantModel { MiscField = 4, Text = "Jived fox nymph grabs quick waltz." }, - }; - - dbSet.AddRange(models); +using System; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MongoDB.Bson; +using MongoDB.Driver; +using MongoFramework.Attributes; +using MongoFramework.Infrastructure.Linq; +using MongoFramework.Infrastructure.Mapping; +using MongoFramework.Linq; + +namespace MongoFramework.Tests.Linq +{ + [TestClass] + public class LinqExtensionsTests : TestBase + { + public class LinqExtensionsModel + { + public string Id { get; set; } + } + public class WhereIdMatchesGuidModel + { + public Guid Id { get; set; } + public string Description { get; set; } + } + public class WhereIdMatchesObjectIdModel + { + public ObjectId Id { get; set; } + public string Description { get; set; } + } + public class WhereIdMatchesStringModel + { + public string Id { get; set; } + public string Description { get; set; } + } + + public class SearchTextModel + { + public string Id { get; set; } + [Index(IndexType.Text)] + public string Text { get; set; } + public int MiscField { get; set; } + } + + public class SearchTextTenantModel : IHaveTenantId + { + public string TenantId { get; set; } + public string Id { get; set; } + [Index(IndexType.Text)] + public string Text { get; set; } + public int MiscField { get; set; } + } + + [TestMethod] + public void ValidToQuery() + { + EntityMapping.RegisterType(typeof(LinqExtensionsModel)); + + var connection = TestConfiguration.GetConnection(); + var provider = new MongoFrameworkQueryProvider(connection); + var queryable = new MongoFrameworkQueryable(provider); + var result = LinqExtensions.ToQuery(queryable); + + Assert.AreEqual("db.LinqExtensionsModel.aggregate([])", result); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException), "ArgumentException")] + public void InvalidToQuery() + { + LinqExtensions.ToQuery(null); + } + + [TestMethod] + public void WhereIdMatchesGuids() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var entityCollection = new[] + { + new WhereIdMatchesGuidModel { Description = "1" }, + new WhereIdMatchesGuidModel { Description = "2" }, + new WhereIdMatchesGuidModel { Description = "3" }, + new WhereIdMatchesGuidModel { Description = "4" } + }; + dbSet.AddRange(entityCollection); + context.SaveChanges(); + + var provider = new MongoFrameworkQueryProvider(connection); + var queryable = new MongoFrameworkQueryable(provider); + + var entityIds = entityCollection.Select(e => e.Id).Take(2); + + var idMatchQueryable = LinqExtensions.WhereIdMatches(queryable, entityIds); + + Assert.AreEqual(2, idMatchQueryable.Count()); + Assert.IsTrue(idMatchQueryable.ToList().All(e => entityIds.Contains(e.Id))); + } + + [TestMethod] + public void WhereIdMatchesObjectIds() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var entityCollection = new[] + { + new WhereIdMatchesObjectIdModel { Description = "1" }, + new WhereIdMatchesObjectIdModel { Description = "2" }, + new WhereIdMatchesObjectIdModel { Description = "3" }, + new WhereIdMatchesObjectIdModel { Description = "4" } + }; + dbSet.AddRange(entityCollection); + context.SaveChanges(); + + var provider = new MongoFrameworkQueryProvider(connection); + var queryable = new MongoFrameworkQueryable(provider); + + var entityIds = entityCollection.Select(e => e.Id).Take(2); + + var idMatchQueryable = LinqExtensions.WhereIdMatches(queryable, entityIds); + + Assert.AreEqual(2, idMatchQueryable.Count()); + Assert.IsTrue(idMatchQueryable.ToList().All(e => entityIds.Contains(e.Id))); + } + + [TestMethod] + public void WhereIdMatchesStringIds() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var entityCollection = new[] + { + new WhereIdMatchesStringModel { Description = "1" }, + new WhereIdMatchesStringModel { Description = "2" }, + new WhereIdMatchesStringModel { Description = "3" }, + new WhereIdMatchesStringModel { Description = "4" } + }; + dbSet.AddRange(entityCollection); + context.SaveChanges(); + + var provider = new MongoFrameworkQueryProvider(connection); + var queryable = new MongoFrameworkQueryable(provider); + + var entityIds = entityCollection.Select(e => e.Id).Take(2); + + var idMatchQueryable = LinqExtensions.WhereIdMatches(queryable, entityIds); + + Assert.AreEqual(2, idMatchQueryable.Count()); + Assert.IsTrue(idMatchQueryable.ToList().All(e => entityIds.Contains(e.Id))); + } + + [TestMethod] + public void SearchText() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + dbSet.AddRange(new SearchTextModel[] + { + new SearchTextModel { MiscField = 1, Text = "The quick brown fox jumps over the lazy dog." }, + new SearchTextModel { MiscField = 2, Text = "The five boxing wizards jump quickly." }, + new SearchTextModel { MiscField = 3, Text = "The quick brown fox jumps over the lazy dog." }, + new SearchTextModel { MiscField = 4, Text = "Jived fox nymph grabs quick waltz." }, + }); + context.SaveChanges(); + + Assert.AreEqual(4, dbSet.SearchText("quick").Count()); + Assert.AreEqual(0, dbSet.SearchText("the").Count()); //Stop words aren't used in text indexes: https://docs.mongodb.com/manual/core/index-text/#supported-languages-and-stop-words + Assert.AreEqual(2, dbSet.SearchText("dog").Count()); + Assert.AreEqual(1, dbSet.SearchText("jived").Count()); + + Assert.AreEqual(1, dbSet.SearchText("quick").Where(e => e.MiscField == 3).Count()); + } + + [TestMethod] + public void SearchTextTenant() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbTenantContext(connection, TestConfiguration.GetTenantId()); + var dbSet = new MongoDbTenantSet(context); + + var models = new SearchTextTenantModel[] + { + new SearchTextTenantModel { MiscField = 1, Text = "The quick brown fox jumps over the lazy dog." }, + new SearchTextTenantModel { MiscField = 2, Text = "The five boxing wizards jump quickly." }, + new SearchTextTenantModel { MiscField = 3, Text = "The quick brown fox jumps over the lazy dog." }, + new SearchTextTenantModel { MiscField = 4, Text = "Jived fox nymph grabs quick waltz." }, + }; + + dbSet.AddRange(models); context.SaveChanges(); //add some extras to ensure only our tenant's results are coming back. - var context2 = new MongoDbTenantContext(connection, "second"); - var dbSet2 = new MongoDbTenantSet(context); - dbSet2.AddRange(models); + var context2 = new MongoDbTenantContext(connection, "second"); + var dbSet2 = new MongoDbTenantSet(context); + dbSet2.AddRange(models); context2.SaveChanges(); - - Assert.AreEqual(4, dbSet.SearchText("quick").Count()); + + Assert.AreEqual(4, dbSet.SearchText("quick").Count()); Assert.AreEqual(0, dbSet.SearchText("the").Count()); //Stop words aren't used in text indexes: https://docs.mongodb.com/manual/core/index-text/#supported-languages-and-stop-words - Assert.AreEqual(2, dbSet.SearchText("dog").Count()); - Assert.AreEqual(1, dbSet.SearchText("jived").Count()); - - Assert.AreEqual(1, dbSet.SearchText("quick").Where(e => e.MiscField == 3).Count()); - } - } -} + Assert.AreEqual(2, dbSet.SearchText("dog").Count()); + Assert.AreEqual(1, dbSet.SearchText("jived").Count()); + + Assert.AreEqual(1, dbSet.SearchText("quick").Where(e => e.MiscField == 3).Count()); + } + } +} diff --git a/tests/MongoFramework.Tests/Linq/LinqExtensions_SearchGeoTests.cs b/tests/MongoFramework.Tests/Linq/LinqExtensions_SearchGeoTests.cs index 46011ba5..d2e5fa38 100644 --- a/tests/MongoFramework.Tests/Linq/LinqExtensions_SearchGeoTests.cs +++ b/tests/MongoFramework.Tests/Linq/LinqExtensions_SearchGeoTests.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoDB.Driver.GeoJsonObjectModel; using MongoFramework.Attributes; diff --git a/tests/MongoFramework.Tests/Linq/QueryableAsyncExtensionsMultiTenantTests.cs b/tests/MongoFramework.Tests/Linq/QueryableAsyncExtensionsMultiTenantTests.cs index fc9a3230..39ffa251 100644 --- a/tests/MongoFramework.Tests/Linq/QueryableAsyncExtensionsMultiTenantTests.cs +++ b/tests/MongoFramework.Tests/Linq/QueryableAsyncExtensionsMultiTenantTests.cs @@ -1,11 +1,7 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; -using MongoFramework.Infrastructure; -using MongoFramework.Infrastructure.Linq; -using MongoFramework.Infrastructure.Mapping; using MongoFramework.Linq; namespace MongoFramework.Tests.Linq @@ -39,7 +35,7 @@ private MongoDbTenantSet SetupTwoTenantsData(string tenantI var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet(context); - var context2 = new MongoDbTenantContext(connection, tenantId+"-2"); + var context2 = new MongoDbTenantContext(connection, tenantId + "-2"); var dbSet2 = new MongoDbTenantSet(context2); dbSet.AddRange(GetModels()); @@ -102,7 +98,7 @@ public async Task FirstAsync_NoValue() dbSet.AddRange(GetModels()); context.SaveChanges(); - var context2 = new MongoDbTenantContext(connection, tenantId+"-2"); + var context2 = new MongoDbTenantContext(connection, tenantId + "-2"); var dbSet2 = new MongoDbTenantSet(context2); await Assert.ThrowsExceptionAsync(async () => await dbSet2.FirstAsync()); @@ -139,7 +135,7 @@ public async Task FirstOrDefaultAsync_NoValue() dbSet.AddRange(GetModels()); context.SaveChanges(); - var context2 = new MongoDbTenantContext(connection, tenantId+"-2"); + var context2 = new MongoDbTenantContext(connection, tenantId + "-2"); var dbSet2 = new MongoDbTenantSet(context2); Assert.IsNull(await dbSet2.FirstOrDefaultAsync()); @@ -176,7 +172,7 @@ public async Task SingleAsync_NoValue() dbSet.AddRange(GetModels()); context.SaveChanges(); - var context2 = new MongoDbTenantContext(connection, tenantId+"-2"); + var context2 = new MongoDbTenantContext(connection, tenantId + "-2"); var dbSet2 = new MongoDbTenantSet(context2); await Assert.ThrowsExceptionAsync(async () => await dbSet2.SingleAsync()); @@ -192,7 +188,7 @@ public async Task SingleAsync_HasValue() dbSet.AddRange(GetModels()); context.SaveChanges(); - var context2 = new MongoDbTenantContext(connection, tenantId+"-2"); + var context2 = new MongoDbTenantContext(connection, tenantId + "-2"); var dbSet2 = new MongoDbTenantSet(context2); dbSet2.Add(new QueryableAsyncModel { Title = "SingleAsync_HasValue.1" }); @@ -230,7 +226,7 @@ public async Task SingleOrDefaultAsync_NoValue() dbSet.AddRange(GetModels()); context.SaveChanges(); - var context2 = new MongoDbTenantContext(connection, tenantId+"-2"); + var context2 = new MongoDbTenantContext(connection, tenantId + "-2"); var dbSet2 = new MongoDbTenantSet(context2); Assert.IsNull(await dbSet2.SingleOrDefaultAsync()); @@ -246,7 +242,7 @@ public async Task SingleOrDefaultAsync_HasValue() dbSet.AddRange(GetModels()); context.SaveChanges(); - var context2 = new MongoDbTenantContext(connection, tenantId+"-2"); + var context2 = new MongoDbTenantContext(connection, tenantId + "-2"); var dbSet2 = new MongoDbTenantSet(context2); dbSet2.Add(new QueryableAsyncModel { Title = "SingleOrDefaultAsync_HasValue.1" }); @@ -284,7 +280,7 @@ public async Task CountAsync_NoValues() dbSet.AddRange(GetModels()); context.SaveChanges(); - var context2 = new MongoDbTenantContext(connection, tenantId+"-2"); + var context2 = new MongoDbTenantContext(connection, tenantId + "-2"); var dbSet2 = new MongoDbTenantSet(context2); var result = await dbSet2.CountAsync(); @@ -320,7 +316,7 @@ public async Task MaxAsync_NoValues() dbSet.AddRange(GetModels()); context.SaveChanges(); - var context2 = new MongoDbTenantContext(connection, tenantId+"-2"); + var context2 = new MongoDbTenantContext(connection, tenantId + "-2"); var dbSet2 = new MongoDbTenantSet(context2); await Assert.ThrowsExceptionAsync(async () => await dbSet2.Select(e => e.IntNumber).MaxAsync()); @@ -364,7 +360,7 @@ public async Task MinAsync_NoValues() dbSet.AddRange(GetModels()); context.SaveChanges(); - var context2 = new MongoDbTenantContext(connection, tenantId+"-2"); + var context2 = new MongoDbTenantContext(connection, tenantId + "-2"); var dbSet2 = new MongoDbTenantSet(context2); await Assert.ThrowsExceptionAsync(async () => await dbSet2.Select(e => e.IntNumber).MinAsync()); @@ -408,7 +404,7 @@ public async Task AnyAsync_NoValues() dbSet.AddRange(GetModels()); context.SaveChanges(); - var context2 = new MongoDbTenantContext(connection, tenantId+"-2"); + var context2 = new MongoDbTenantContext(connection, tenantId + "-2"); var dbSet2 = new MongoDbTenantSet(context2); var result = await dbSet2.AnyAsync(); diff --git a/tests/MongoFramework.Tests/Linq/QueryableAsyncExtensions_SumTests.cs b/tests/MongoFramework.Tests/Linq/QueryableAsyncExtensions_SumTests.cs index 25e33daa..2709398c 100644 --- a/tests/MongoFramework.Tests/Linq/QueryableAsyncExtensions_SumTests.cs +++ b/tests/MongoFramework.Tests/Linq/QueryableAsyncExtensions_SumTests.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Linq; using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoFramework.Infrastructure; diff --git a/tests/MongoFramework.Tests/MongoDbBucketSetTests.cs b/tests/MongoFramework.Tests/MongoDbBucketSetTests.cs index 1815b2f8..6d9a403b 100644 --- a/tests/MongoFramework.Tests/MongoDbBucketSetTests.cs +++ b/tests/MongoFramework.Tests/MongoDbBucketSetTests.cs @@ -279,7 +279,7 @@ public void ContinuousSubEntityAccessAcrossBuckets() BucketSize = 2, EntityTimeProperty = "Date" }); - + dbSet.AddRange(new EntityGroup { Name = "Group1" diff --git a/tests/MongoFramework.Tests/MongoDbContextTenantTests.cs b/tests/MongoFramework.Tests/MongoDbContextTenantTests.cs index ba7e9e29..c8ea63cb 100644 --- a/tests/MongoFramework.Tests/MongoDbContextTenantTests.cs +++ b/tests/MongoFramework.Tests/MongoDbContextTenantTests.cs @@ -1,243 +1,243 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using MongoFramework.Attributes; -using System; -using System.Linq; -using System.Threading.Tasks; - -namespace MongoFramework.Tests -{ - [TestClass] - public class MongoDbContextTenantTests : TestBase - { - class DbSetModel : IHaveTenantId - { - public string Id { get; set; } - public string TenantId { get; set; } - } - class BucketGroupModel - { - public string Name { get; set; } - } - class BucketSubEntity - { - public string Label { get; set; } - public DateTime Date { get; set; } - } - - class SecondModel - { - public string Id { get; set; } - } - - class MongoDbContextTestContext : MongoDbTenantContext - { - public MongoDbContextTestContext(IMongoDbConnection connection, string tenantId) : base(connection, tenantId) { } - public MongoDbTenantSet DbSet { get; set; } - [BucketSetOptions(5, nameof(BucketSubEntity.Date))] - public MongoDbBucketSet DbBucketSet { get; set; } - } - - [TestMethod] - public void ContextCreatedWithOptions() - { - using (var context = new MongoDbContextTestContext(TestConfiguration.GetConnection(), TestConfiguration.GetTenantId())) - { - context.DbSet.Add(new DbSetModel()); - Assert.IsFalse(context.DbSet.Any()); - context.SaveChanges(); - Assert.IsTrue(context.DbSet.Any()); - } - } - - [TestMethod] - public void ContextCreatesDbSets() - { - using (var context = new MongoDbContextTestContext(TestConfiguration.GetConnection(), TestConfiguration.GetTenantId())) - { - Assert.IsNotNull(context.DbSet); - Assert.IsNotNull(context.DbBucketSet); - } - } - - [TestMethod] - public void DbSetsHaveOptionsApplied() - { - using (var context = new MongoDbContextTestContext(TestConfiguration.GetConnection(), TestConfiguration.GetTenantId())) - { - Assert.AreEqual(5, context.DbBucketSet.BucketSize); - } - } - - [TestMethod] - public void GenericSetReturnsCorrectSet() - { - using (var context = new MongoDbContextTestContext(TestConfiguration.GetConnection(), TestConfiguration.GetTenantId())) - { - Assert.IsInstanceOfType(context.Set(),typeof(MongoDbTenantSet)); - } - } - - [TestMethod] - public void GenericSetReturnsNewSet() - { - using (var context = new MongoDbContextTestContext(TestConfiguration.GetConnection(), TestConfiguration.GetTenantId())) - { - Assert.IsInstanceOfType(context.Set(),typeof(MongoDbSet)); - } - } - - [TestMethod] - public void ContextSavesDbSets() - { - using (var context = new MongoDbContextTestContext(TestConfiguration.GetConnection(), TestConfiguration.GetTenantId())) - { - context.DbSet.Add(new DbSetModel()); - Assert.IsFalse(context.DbSet.Any()); - context.SaveChanges(); - Assert.IsTrue(context.DbSet.Any()); - Assert.AreEqual(TestConfiguration.GetTenantId(), context.DbSet.First().TenantId); - } - } - - [TestMethod] - public async Task ContextSavesDbSetsAsync() - { - using (var context = new MongoDbContextTestContext(TestConfiguration.GetConnection(), TestConfiguration.GetTenantId())) - { - context.DbSet.Add(new DbSetModel()); - Assert.IsFalse(context.DbSet.Any()); - await context.SaveChangesAsync().ConfigureAwait(false); - Assert.IsTrue(context.DbSet.Any()); - Assert.AreEqual(TestConfiguration.GetTenantId(), context.DbSet.First().TenantId); - } - } - - [TestMethod] - public void SuccessfullyAttachUntrackedEntity() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var model = new DbSetModel - { - Id = "abcd" - }; - - dbSet.Add(model); - - context.SaveChanges(); - - ResetMongoDb(); - - context = new MongoDbTenantContext(connection, tenantId); - dbSet = new MongoDbTenantSet(context); - - var result = dbSet.AsNoTracking().FirstOrDefault(); - - context.Attach(result); - - Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(result).State); - } - - [TestMethod] - public void SuccessfullyAttachUntrackedEntities() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var entities = new[] { - new DbSetModel - { - Id = "abcd" - }, - new DbSetModel - { - Id = "efgh" - } - }; - - dbSet.AddRange(entities); - - context.SaveChanges(); - - ResetMongoDb(); - - context = new MongoDbTenantContext(connection, tenantId); - dbSet = new MongoDbTenantSet(context); - - var result = dbSet.AsNoTracking().ToList(); - - context.AttachRange(result); - - Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(result[0]).State); - Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(result[1]).State); - } - - [TestMethod] - public void AttachRejectsMismatchedEntity() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var model = new DbSetModel - { - Id = "abcd" - }; - - dbSet.Add(model); - - context.SaveChanges(); - - ResetMongoDb(); - - context = new MongoDbTenantContext(connection, tenantId); - dbSet = new MongoDbTenantSet(context); - - var result = dbSet.AsNoTracking().FirstOrDefault(); - result.TenantId = tenantId + "a"; - - Assert.ThrowsException(() => context.Attach(result)); - } - - [TestMethod] - public void AttachRejectsMismatchedEntities() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var entities = new[] { - new DbSetModel - { - Id = "abcd" - }, - new DbSetModel - { - Id = "efgh" - } - }; - - dbSet.AddRange(entities); - - context.SaveChanges(); - - ResetMongoDb(); - - context = new MongoDbTenantContext(connection, tenantId); - dbSet = new MongoDbTenantSet(context); - - var result = dbSet.AsNoTracking().ToList(); - result[0].TenantId = tenantId + "a"; - - Assert.ThrowsException(() => context.AttachRange(result)); - } - - } +using System; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MongoFramework.Attributes; + +namespace MongoFramework.Tests +{ + [TestClass] + public class MongoDbContextTenantTests : TestBase + { + class DbSetModel : IHaveTenantId + { + public string Id { get; set; } + public string TenantId { get; set; } + } + class BucketGroupModel + { + public string Name { get; set; } + } + class BucketSubEntity + { + public string Label { get; set; } + public DateTime Date { get; set; } + } + + class SecondModel + { + public string Id { get; set; } + } + + class MongoDbContextTestContext : MongoDbTenantContext + { + public MongoDbContextTestContext(IMongoDbConnection connection, string tenantId) : base(connection, tenantId) { } + public MongoDbTenantSet DbSet { get; set; } + [BucketSetOptions(5, nameof(BucketSubEntity.Date))] + public MongoDbBucketSet DbBucketSet { get; set; } + } + + [TestMethod] + public void ContextCreatedWithOptions() + { + using (var context = new MongoDbContextTestContext(TestConfiguration.GetConnection(), TestConfiguration.GetTenantId())) + { + context.DbSet.Add(new DbSetModel()); + Assert.IsFalse(context.DbSet.Any()); + context.SaveChanges(); + Assert.IsTrue(context.DbSet.Any()); + } + } + + [TestMethod] + public void ContextCreatesDbSets() + { + using (var context = new MongoDbContextTestContext(TestConfiguration.GetConnection(), TestConfiguration.GetTenantId())) + { + Assert.IsNotNull(context.DbSet); + Assert.IsNotNull(context.DbBucketSet); + } + } + + [TestMethod] + public void DbSetsHaveOptionsApplied() + { + using (var context = new MongoDbContextTestContext(TestConfiguration.GetConnection(), TestConfiguration.GetTenantId())) + { + Assert.AreEqual(5, context.DbBucketSet.BucketSize); + } + } + + [TestMethod] + public void GenericSetReturnsCorrectSet() + { + using (var context = new MongoDbContextTestContext(TestConfiguration.GetConnection(), TestConfiguration.GetTenantId())) + { + Assert.IsInstanceOfType(context.Set(), typeof(MongoDbTenantSet)); + } + } + + [TestMethod] + public void GenericSetReturnsNewSet() + { + using (var context = new MongoDbContextTestContext(TestConfiguration.GetConnection(), TestConfiguration.GetTenantId())) + { + Assert.IsInstanceOfType(context.Set(), typeof(MongoDbSet)); + } + } + + [TestMethod] + public void ContextSavesDbSets() + { + using (var context = new MongoDbContextTestContext(TestConfiguration.GetConnection(), TestConfiguration.GetTenantId())) + { + context.DbSet.Add(new DbSetModel()); + Assert.IsFalse(context.DbSet.Any()); + context.SaveChanges(); + Assert.IsTrue(context.DbSet.Any()); + Assert.AreEqual(TestConfiguration.GetTenantId(), context.DbSet.First().TenantId); + } + } + + [TestMethod] + public async Task ContextSavesDbSetsAsync() + { + using (var context = new MongoDbContextTestContext(TestConfiguration.GetConnection(), TestConfiguration.GetTenantId())) + { + context.DbSet.Add(new DbSetModel()); + Assert.IsFalse(context.DbSet.Any()); + await context.SaveChangesAsync().ConfigureAwait(false); + Assert.IsTrue(context.DbSet.Any()); + Assert.AreEqual(TestConfiguration.GetTenantId(), context.DbSet.First().TenantId); + } + } + + [TestMethod] + public void SuccessfullyAttachUntrackedEntity() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var model = new DbSetModel + { + Id = "abcd" + }; + + dbSet.Add(model); + + context.SaveChanges(); + + ResetMongoDb(); + + context = new MongoDbTenantContext(connection, tenantId); + dbSet = new MongoDbTenantSet(context); + + var result = dbSet.AsNoTracking().FirstOrDefault(); + + context.Attach(result); + + Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(result).State); + } + + [TestMethod] + public void SuccessfullyAttachUntrackedEntities() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var entities = new[] { + new DbSetModel + { + Id = "abcd" + }, + new DbSetModel + { + Id = "efgh" + } + }; + + dbSet.AddRange(entities); + + context.SaveChanges(); + + ResetMongoDb(); + + context = new MongoDbTenantContext(connection, tenantId); + dbSet = new MongoDbTenantSet(context); + + var result = dbSet.AsNoTracking().ToList(); + + context.AttachRange(result); + + Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(result[0]).State); + Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(result[1]).State); + } + + [TestMethod] + public void AttachRejectsMismatchedEntity() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var model = new DbSetModel + { + Id = "abcd" + }; + + dbSet.Add(model); + + context.SaveChanges(); + + ResetMongoDb(); + + context = new MongoDbTenantContext(connection, tenantId); + dbSet = new MongoDbTenantSet(context); + + var result = dbSet.AsNoTracking().FirstOrDefault(); + result.TenantId = tenantId + "a"; + + Assert.ThrowsException(() => context.Attach(result)); + } + + [TestMethod] + public void AttachRejectsMismatchedEntities() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var entities = new[] { + new DbSetModel + { + Id = "abcd" + }, + new DbSetModel + { + Id = "efgh" + } + }; + + dbSet.AddRange(entities); + + context.SaveChanges(); + + ResetMongoDb(); + + context = new MongoDbTenantContext(connection, tenantId); + dbSet = new MongoDbTenantSet(context); + + var result = dbSet.AsNoTracking().ToList(); + result[0].TenantId = tenantId + "a"; + + Assert.ThrowsException(() => context.AttachRange(result)); + } + + } } \ No newline at end of file diff --git a/tests/MongoFramework.Tests/MongoDbContextTests.cs b/tests/MongoFramework.Tests/MongoDbContextTests.cs index ed263efc..d6bf2438 100644 --- a/tests/MongoFramework.Tests/MongoDbContextTests.cs +++ b/tests/MongoFramework.Tests/MongoDbContextTests.cs @@ -1,8 +1,8 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using MongoFramework.Attributes; -using System; +using System; using System.Linq; using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MongoFramework.Attributes; namespace MongoFramework.Tests { @@ -85,7 +85,7 @@ public async Task ContextSavesDbSetsAsync() Assert.IsTrue(context.DbSet.Any()); } } - + [TestMethod] public void SuccessfullyAttachUntrackedEntity() { diff --git a/tests/MongoFramework.Tests/MongoDbDriverHelper.cs b/tests/MongoFramework.Tests/MongoDbDriverHelper.cs index 608c587a..4b88677b 100644 --- a/tests/MongoFramework.Tests/MongoDbDriverHelper.cs +++ b/tests/MongoFramework.Tests/MongoDbDriverHelper.cs @@ -1,10 +1,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Linq; using System.Reflection; -using System.Text; -using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Bson.Serialization; @@ -15,7 +12,7 @@ public static class MongoDbDriverHelper public static void ResetDriver() { //Primarily introduced to better test TypeDiscoverySerializer, this is designed to reset the MongoDB driver - //as if the assembly just loaded. It is likely incomplete and would be easily subject to breaking in future + //as if the assembly just loaded. It is likely incomplete and would be easily subject to breaking in future //driver updates. If someone knows a better way to reset the MongoDB driver, please open a pull request! var classMapField = typeof(BsonClassMap).GetField("__classMaps", BindingFlags.NonPublic | BindingFlags.Static); diff --git a/tests/MongoFramework.Tests/MongoDbSetTests.cs b/tests/MongoFramework.Tests/MongoDbSetTests.cs index 9b26cd6e..b9948e26 100644 --- a/tests/MongoFramework.Tests/MongoDbSetTests.cs +++ b/tests/MongoFramework.Tests/MongoDbSetTests.cs @@ -1,111 +1,110 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; using System; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Threading.Tasks; -using MongoFramework.Linq; - -namespace MongoFramework.Tests -{ - [TestClass] - public class MongoDbSetTests : TestBase - { - public class TestModel - { - public string Id { get; set; } - - public string Description { get; set; } - public bool BooleanField { get; set; } - } - - [TestMethod] - public void SuccessfulInsertAndQueryBack() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - dbSet.Add(new TestModel - { - Description = "ValueSync" - }); - - Assert.IsFalse(dbSet.Any(m => m.Description == "ValueSync")); - context.SaveChanges(); - Assert.IsTrue(dbSet.Any(m => m.Description == "ValueSync")); - } - - [TestMethod] - public async Task SuccessfulInsertAndQueryBackAsync() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - dbSet.Add(new TestModel - { - Description = "ValueAsync" - }); - - Assert.IsFalse(dbSet.Any(m => m.Description == "ValueAsync")); - await context.SaveChangesAsync(); - Assert.IsTrue(dbSet.Any(m => m.Description == "ValueAsync")); - } - - [TestMethod] - public void SuccessfulInsertAndFind() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var model = new TestModel - { - Description = "SuccessfulInsertAndFind" - }; - - dbSet.Add(model); - - context.SaveChanges(); - - context = new MongoDbContext(connection); - dbSet = new MongoDbSet(context); +using System.Linq; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MongoFramework.Linq; + +namespace MongoFramework.Tests +{ + [TestClass] + public class MongoDbSetTests : TestBase + { + public class TestModel + { + public string Id { get; set; } + + public string Description { get; set; } + public bool BooleanField { get; set; } + } + + [TestMethod] + public void SuccessfulInsertAndQueryBack() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + dbSet.Add(new TestModel + { + Description = "ValueSync" + }); + + Assert.IsFalse(dbSet.Any(m => m.Description == "ValueSync")); + context.SaveChanges(); + Assert.IsTrue(dbSet.Any(m => m.Description == "ValueSync")); + } + + [TestMethod] + public async Task SuccessfulInsertAndQueryBackAsync() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + dbSet.Add(new TestModel + { + Description = "ValueAsync" + }); + + Assert.IsFalse(dbSet.Any(m => m.Description == "ValueAsync")); + await context.SaveChangesAsync(); + Assert.IsTrue(dbSet.Any(m => m.Description == "ValueAsync")); + } + + [TestMethod] + public void SuccessfulInsertAndFind() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var model = new TestModel + { + Description = "SuccessfulInsertAndFind" + }; + + dbSet.Add(model); + + context.SaveChanges(); + + context = new MongoDbContext(connection); + dbSet = new MongoDbSet(context); Assert.AreEqual("SuccessfulInsertAndFind", dbSet.Find(model.Id).Description); - Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(model).State); - } - - [TestMethod] - public void SuccessfulNullFind() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var model = new TestModel - { - Description = "SuccessfulNullFind" - }; - - dbSet.Add(model); - - context.SaveChanges(); - - Assert.IsNull(dbSet.Find("abcd")); - } - - [TestMethod] - public void SuccessfullyFindTracked() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var model = new TestModel - { - Id = "abcd", - Description = "SuccessfullyFindTracked" - }; - + Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(model).State); + } + + [TestMethod] + public void SuccessfulNullFind() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var model = new TestModel + { + Description = "SuccessfulNullFind" + }; + + dbSet.Add(model); + + context.SaveChanges(); + + Assert.IsNull(dbSet.Find("abcd")); + } + + [TestMethod] + public void SuccessfullyFindTracked() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var model = new TestModel + { + Id = "abcd", + Description = "SuccessfullyFindTracked" + }; + dbSet.Add(model); //Note: not saving, but still should be found as tracked @@ -113,272 +112,272 @@ public void SuccessfullyFindTracked() Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Added, context.ChangeTracker.GetEntry(model).State); } - [TestMethod] - public void FindRequiresId() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - Assert.ThrowsException(() => dbSet.Find(null)); - } - - [TestMethod] - public async Task SuccessfulInsertAndFindAsync() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var model = new TestModel - { - Description = "SuccessfulInsertAndFind" - }; - - dbSet.Add(model); - - context.SaveChanges(); - - context = new MongoDbContext(connection); - dbSet = new MongoDbSet(context); - Assert.AreEqual("SuccessfulInsertAndFind", (await dbSet.FindAsync(model.Id)).Description); - Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(model).State); - } - - [TestMethod] - public async Task SuccessfulNullFindAsync() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var model = new TestModel - { - Description = "SuccessfulNullFind" - }; - - dbSet.Add(model); - - context.SaveChanges(); - - Assert.IsNull(await dbSet.FindAsync("abcd")); - } - - [TestMethod] - public async Task SuccessfullyFindAsyncTracked() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var model = new TestModel - { - Id = "abcd", - Description = "SuccessfullyFindTracked" - }; - - dbSet.Add(model); - - //Note: not saving, but still should be found as tracked - Assert.AreEqual("SuccessfullyFindTracked", (await dbSet.FindAsync(model.Id)).Description); - Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Added, context.ChangeTracker.GetEntry(model).State); - } - - [TestMethod] - public async Task FindAsyncRequiresId() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - await Assert.ThrowsExceptionAsync(async () => await dbSet.FindAsync(null)); - } - - [TestMethod] - public void SuccessfullyUpdateEntity() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var entity = new TestModel - { - Description = "SuccessfullyUpdateEntity" - }; - - dbSet.Add(entity); - context.SaveChanges(); - - dbSet = new MongoDbSet(context); - - entity.Description = "SuccessfullyUpdateEntity-Updated"; - dbSet.Update(entity); - - Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyUpdateEntity-Updated")); - context.SaveChanges(); - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyUpdateEntity-Updated")); - } - - [TestMethod] - public void SuccessfullyUpdateRange() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var entities = new[] { - new TestModel - { - Description = "SuccessfullyUpdateRange.1" - }, - new TestModel - { - Description = "SuccessfullyUpdateRange.2" - } - }; - - dbSet.AddRange(entities); - context.SaveChanges(); - - dbSet = new MongoDbSet(context); - - entities[0].Description = "SuccessfullyUpdateRange.1-Updated"; - entities[1].Description = "SuccessfullyUpdateRange.2-Updated"; - dbSet.UpdateRange(entities); - - Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyUpdateRange.1-Updated" || m.Description == "SuccessfullyUpdateRange.2-Updated")); - context.SaveChanges(); - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyUpdateRange.1-Updated")); - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyUpdateRange.2-Updated")); - } - - [TestMethod] - public void SuccessfullyRemoveEntity() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var entity = new TestModel - { - Description = "SuccessfullyRemoveEntity" - }; - - dbSet.Add(entity); - context.SaveChanges(); - - dbSet = new MongoDbSet(context); - - dbSet.Remove(entity); - - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntity")); - context.SaveChanges(); - Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntity")); - } - - [TestMethod] - public void SuccessfullyRemoveRange() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var entities = new[] { - new TestModel - { - Description = "SuccessfullyRemoveRange.1" - }, - new TestModel - { - Description = "SuccessfullyRemoveRange.2" - } - }; - - dbSet.AddRange(entities); - context.SaveChanges(); - - dbSet = new MongoDbSet(context); - - dbSet.RemoveRange(entities); - - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.1")); - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.2")); - context.SaveChanges(); - Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.1")); - Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.2")); - } - - [TestMethod] - public void SuccessfullyRemoveEntityById() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var entity = new TestModel - { - Description = "SuccessfullyRemoveEntityById" - }; - - dbSet.Add(entity); - context.SaveChanges(); - - dbSet = new MongoDbSet(context); - - dbSet.RemoveById(entity.Id); - - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntityById")); - context.SaveChanges(); - Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntityById")); - } - - [TestMethod] - public void SuccessfullyRemoveRangeByPredicate() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var entities = new[] - { - new TestModel - { - Description = "SuccessfullyRemoveRangeByPredicate" - }, - new TestModel - { - Description = "SuccessfullyRemoveRangeByPredicate", - BooleanField = true - } - }; - - dbSet.AddRange(entities); - context.SaveChanges(); - - dbSet = new MongoDbSet(context); - - dbSet.RemoveRange(e => e.BooleanField); - - Assert.AreEqual(2, dbSet.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); - context.SaveChanges(); - Assert.AreEqual(1, dbSet.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); - Assert.IsNotNull(dbSet.FirstOrDefault(m => m.Id == entities[0].Id)); + [TestMethod] + public void FindRequiresId() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + Assert.ThrowsException(() => dbSet.Find(null)); } - [TestMethod] - public void SuccessfullyLinqFindTracked() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var model = new TestModel - { - Id = "abcd", - Description = "SuccessfullyFindTracked.1" - }; - - dbSet.Add(model); + [TestMethod] + public async Task SuccessfulInsertAndFindAsync() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var model = new TestModel + { + Description = "SuccessfulInsertAndFind" + }; + + dbSet.Add(model); + + context.SaveChanges(); + + context = new MongoDbContext(connection); + dbSet = new MongoDbSet(context); + Assert.AreEqual("SuccessfulInsertAndFind", (await dbSet.FindAsync(model.Id)).Description); + Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(model).State); + } + + [TestMethod] + public async Task SuccessfulNullFindAsync() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var model = new TestModel + { + Description = "SuccessfulNullFind" + }; + + dbSet.Add(model); + + context.SaveChanges(); + + Assert.IsNull(await dbSet.FindAsync("abcd")); + } + + [TestMethod] + public async Task SuccessfullyFindAsyncTracked() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var model = new TestModel + { + Id = "abcd", + Description = "SuccessfullyFindTracked" + }; + + dbSet.Add(model); + + //Note: not saving, but still should be found as tracked + Assert.AreEqual("SuccessfullyFindTracked", (await dbSet.FindAsync(model.Id)).Description); + Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Added, context.ChangeTracker.GetEntry(model).State); + } + + [TestMethod] + public async Task FindAsyncRequiresId() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + await Assert.ThrowsExceptionAsync(async () => await dbSet.FindAsync(null)); + } + + [TestMethod] + public void SuccessfullyUpdateEntity() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var entity = new TestModel + { + Description = "SuccessfullyUpdateEntity" + }; + + dbSet.Add(entity); + context.SaveChanges(); + + dbSet = new MongoDbSet(context); + + entity.Description = "SuccessfullyUpdateEntity-Updated"; + dbSet.Update(entity); + + Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyUpdateEntity-Updated")); + context.SaveChanges(); + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyUpdateEntity-Updated")); + } + + [TestMethod] + public void SuccessfullyUpdateRange() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var entities = new[] { + new TestModel + { + Description = "SuccessfullyUpdateRange.1" + }, + new TestModel + { + Description = "SuccessfullyUpdateRange.2" + } + }; + + dbSet.AddRange(entities); + context.SaveChanges(); + + dbSet = new MongoDbSet(context); + + entities[0].Description = "SuccessfullyUpdateRange.1-Updated"; + entities[1].Description = "SuccessfullyUpdateRange.2-Updated"; + dbSet.UpdateRange(entities); + + Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyUpdateRange.1-Updated" || m.Description == "SuccessfullyUpdateRange.2-Updated")); + context.SaveChanges(); + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyUpdateRange.1-Updated")); + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyUpdateRange.2-Updated")); + } + + [TestMethod] + public void SuccessfullyRemoveEntity() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var entity = new TestModel + { + Description = "SuccessfullyRemoveEntity" + }; + + dbSet.Add(entity); + context.SaveChanges(); + + dbSet = new MongoDbSet(context); + + dbSet.Remove(entity); + + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntity")); + context.SaveChanges(); + Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntity")); + } + + [TestMethod] + public void SuccessfullyRemoveRange() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var entities = new[] { + new TestModel + { + Description = "SuccessfullyRemoveRange.1" + }, + new TestModel + { + Description = "SuccessfullyRemoveRange.2" + } + }; + + dbSet.AddRange(entities); + context.SaveChanges(); + + dbSet = new MongoDbSet(context); + + dbSet.RemoveRange(entities); + + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.1")); + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.2")); + context.SaveChanges(); + Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.1")); + Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.2")); + } + + [TestMethod] + public void SuccessfullyRemoveEntityById() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var entity = new TestModel + { + Description = "SuccessfullyRemoveEntityById" + }; + + dbSet.Add(entity); + context.SaveChanges(); + + dbSet = new MongoDbSet(context); + + dbSet.RemoveById(entity.Id); + + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntityById")); + context.SaveChanges(); + Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntityById")); + } + + [TestMethod] + public void SuccessfullyRemoveRangeByPredicate() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var entities = new[] + { + new TestModel + { + Description = "SuccessfullyRemoveRangeByPredicate" + }, + new TestModel + { + Description = "SuccessfullyRemoveRangeByPredicate", + BooleanField = true + } + }; + + dbSet.AddRange(entities); + context.SaveChanges(); + + dbSet = new MongoDbSet(context); + + dbSet.RemoveRange(e => e.BooleanField); + + Assert.AreEqual(2, dbSet.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); + context.SaveChanges(); + Assert.AreEqual(1, dbSet.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); + Assert.IsNotNull(dbSet.FirstOrDefault(m => m.Id == entities[0].Id)); + } + + [TestMethod] + public void SuccessfullyLinqFindTracked() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var model = new TestModel + { + Id = "abcd", + Description = "SuccessfullyFindTracked.1" + }; + + dbSet.Add(model); context.SaveChanges(); @@ -388,23 +387,23 @@ public void SuccessfullyLinqFindTracked() result.Description = "changed"; context.ChangeTracker.DetectChanges(); - Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Updated, context.ChangeTracker.GetEntry(result).State); - } - - [TestMethod] - public async Task SuccessfullyLinqFindTrackedAsync() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var model = new TestModel - { - Id = "abcd", - Description = "SuccessfullyFindTracked.1" - }; - - dbSet.Add(model); + Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Updated, context.ChangeTracker.GetEntry(result).State); + } + + [TestMethod] + public async Task SuccessfullyLinqFindTrackedAsync() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var model = new TestModel + { + Id = "abcd", + Description = "SuccessfullyFindTracked.1" + }; + + dbSet.Add(model); context.SaveChanges(); @@ -414,63 +413,63 @@ public async Task SuccessfullyLinqFindTrackedAsync() result.Description = "changed"; context.ChangeTracker.DetectChanges(); - Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Updated, context.ChangeTracker.GetEntry(result).State); - } - - [TestMethod] - public void SuccessfullyLinqFindNoTracking() - { - var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var model = new TestModel - { - Id = "abcd", - Description = "SuccessfullyLinqFindNoTracking.1" - }; - - dbSet.Add(model); + Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Updated, context.ChangeTracker.GetEntry(result).State); + } + + [TestMethod] + public void SuccessfullyLinqFindNoTracking() + { + var connection = TestConfiguration.GetConnection(); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var model = new TestModel + { + Id = "abcd", + Description = "SuccessfullyLinqFindNoTracking.1" + }; + + dbSet.Add(model); context.SaveChanges(); ResetMongoDb(); - context = new MongoDbContext(connection); - dbSet = new MongoDbSet(context); + context = new MongoDbContext(connection); + dbSet = new MongoDbSet(context); var result = dbSet.AsNoTracking().FirstOrDefault(); - Assert.IsNull(context.ChangeTracker.GetEntry(result)); - } - - [TestMethod] - public async Task SuccessfullyLinqFindNoTrackingAsync() - { + Assert.IsNull(context.ChangeTracker.GetEntry(result)); + } + + [TestMethod] + public async Task SuccessfullyLinqFindNoTrackingAsync() + { var connection = TestConfiguration.GetConnection(); - var context = new MongoDbContext(connection); - var dbSet = new MongoDbSet(context); - - var model = new TestModel - { - Id = "abcd", - Description = "SuccessfullyFindTracked.1" - }; - - dbSet.Add(model); + var context = new MongoDbContext(connection); + var dbSet = new MongoDbSet(context); + + var model = new TestModel + { + Id = "abcd", + Description = "SuccessfullyFindTracked.1" + }; + + dbSet.Add(model); context.SaveChanges(); ResetMongoDb(); - - context = new MongoDbContext(connection); - dbSet = new MongoDbSet(context); + + context = new MongoDbContext(connection); + dbSet = new MongoDbSet(context); var result = await dbSet.AsNoTracking().FirstOrDefaultAsync(); - Assert.IsNull(context.ChangeTracker.GetEntry(result)); + Assert.IsNull(context.ChangeTracker.GetEntry(result)); } - - } - + + } + } \ No newline at end of file diff --git a/tests/MongoFramework.Tests/MongoDbTenantSetTests.cs b/tests/MongoFramework.Tests/MongoDbTenantSetTests.cs index 5fc6d403..7fdf5cab 100644 --- a/tests/MongoFramework.Tests/MongoDbTenantSetTests.cs +++ b/tests/MongoFramework.Tests/MongoDbTenantSetTests.cs @@ -1,44 +1,42 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; using System; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Threading.Tasks; -using MongoDB.Driver; -using MongoFramework.Attributes; -using System.Runtime.CompilerServices; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MongoDB.Driver; +using MongoFramework.Attributes; using MongoFramework.Linq; -namespace MongoFramework.Tests -{ - [TestClass] - public class MongoDbTenantSetTests : TestBase - { - public class TestModel : IHaveTenantId - { - public string Id { get; set; } - public string TenantId { get; set; } - - public string Description { get; set; } - public bool BooleanField { get; set; } - } - public class TestUniqueModel : IHaveTenantId - { - public string Id { get; set; } - public string TenantId { get; set; } - - [Index(IndexType.Standard, IsUnique = true, IsTenantExclusive = true)] - public string UserName { get; set; } - } +namespace MongoFramework.Tests +{ + [TestClass] + public class MongoDbTenantSetTests : TestBase + { + public class TestModel : IHaveTenantId + { + public string Id { get; set; } + public string TenantId { get; set; } + + public string Description { get; set; } + public bool BooleanField { get; set; } + } + public class TestUniqueModel : IHaveTenantId + { + public string Id { get; set; } + public string TenantId { get; set; } + + [Index(IndexType.Standard, IsUnique = true, IsTenantExclusive = true)] + public string UserName { get; set; } + } [TestMethod] public void SuccessfulCreateTenantId() { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var model = dbSet.Create(); - + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var model = dbSet.Create(); + Assert.AreEqual(tenantId, model.TenantId); } @@ -47,40 +45,40 @@ public void SuccessfulLimitsQueryToTenant() { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); + var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet(context); - var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); + var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); var dbSet2 = new MongoDbTenantSet(context2); - - - var entities = new[] { - new TestModel - { - Description = "SuccessfulLimitsQueryToTenant.1" - }, - new TestModel - { - Description = "SuccessfulLimitsQueryToTenant.2", - BooleanField = true - } - }; - - var entities2 = new[] { - new TestModel - { - Description = "SuccessfulLimitsQueryToTenant.1" - }, - new TestModel - { - Description = "SuccessfulLimitsQueryToTenant.2", - BooleanField = true - } - }; - - dbSet.AddRange(entities); - dbSet2.AddRange(entities2); - + + + var entities = new[] { + new TestModel + { + Description = "SuccessfulLimitsQueryToTenant.1" + }, + new TestModel + { + Description = "SuccessfulLimitsQueryToTenant.2", + BooleanField = true + } + }; + + var entities2 = new[] { + new TestModel + { + Description = "SuccessfulLimitsQueryToTenant.1" + }, + new TestModel + { + Description = "SuccessfulLimitsQueryToTenant.2", + BooleanField = true + } + }; + + dbSet.AddRange(entities); + dbSet2.AddRange(entities2); + context.SaveChanges(); context2.SaveChanges(); @@ -90,752 +88,752 @@ public void SuccessfulLimitsQueryToTenant() Assert.AreEqual(1, dbSet.Where(e => e.BooleanField).Count()); Assert.AreEqual(1, dbSet2.Where(e => e.BooleanField).Count()); - } - - [TestMethod] - public void SuccessfulInsertAndQueryBack() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - dbSet.Add(new TestModel - { - Description = "SuccessfulInsertAndQueryBack" - }); - - Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfulInsertAndQueryBack")); - context.SaveChanges(); - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfulInsertAndQueryBack")); - Assert.IsTrue(dbSet.Any(m => m.TenantId == tenantId)); - } - - [TestMethod] - public async Task SuccessfulInsertAndQueryBackAsync() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - dbSet.Add(new TestModel - { - Description = "SuccessfulInsertAndQueryBackAsync" - }); - - Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfulInsertAndQueryBackAsync")); - await context.SaveChangesAsync(); - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfulInsertAndQueryBackAsync")); - Assert.IsTrue(dbSet.Any(m => m.TenantId == tenantId)); - } - - [TestMethod] - public void SuccessfulInsertAndFind() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); - var dbSet2 = new MongoDbTenantSet(context2); - - var entity1 = new TestModel {Description = "SuccessfulInsertAndFind.1"}; - var entity2 = new TestModel {Description = "SuccessfulInsertAndFind.2"}; - - dbSet.Add(entity1); - dbSet2.Add(entity2); - - context.SaveChanges(); - context2.SaveChanges(); - - context = new MongoDbTenantContext(connection, tenantId); - dbSet = new MongoDbTenantSet(context); - Assert.AreEqual("SuccessfulInsertAndFind.1", dbSet.Find(entity1.Id).Description); - Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(entity1).State); - } - - [TestMethod] - public void SuccessfulNullFind() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); - var dbSet2 = new MongoDbTenantSet(context2); - - var entity1 = new TestModel {Description = "SuccessfulInsertAndFind.1"}; - var entity2 = new TestModel {Description = "SuccessfulInsertAndFind.2"}; - - dbSet.Add(entity1); - dbSet2.Add(entity2); - - context.SaveChanges(); - context2.SaveChanges(); - - Assert.IsNull(dbSet.Find("abcd")); - } - - [TestMethod] - public void BlocksWrongTenantFind() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); - var dbSet2 = new MongoDbTenantSet(context2); - - var entity1 = new TestModel {Description = "SuccessfulInsertAndFind.1"}; - var entity2 = new TestModel {Description = "SuccessfulInsertAndFind.2"}; - - dbSet.Add(entity1); - dbSet2.Add(entity2); - - context.SaveChanges(); - context2.SaveChanges(); - - Assert.IsNull(dbSet.Find(entity2.Id)); - } - - [TestMethod] - public void SuccessfullyFindTracked() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); - var dbSet2 = new MongoDbTenantSet(context2); - - var model = new TestModel - { - Id = "abcd", - Description = "SuccessfullyFindTracked.1" - }; - - var model2 = new TestModel - { - Id = "abcd", - Description = "SuccessfullyFindTracked.2" - }; - - dbSet.Add(model); - dbSet2.Add(model2); - - //Note: not saving, but still should be found as tracked - Assert.AreEqual("SuccessfullyFindTracked.1", dbSet.Find(model.Id).Description); - Assert.AreEqual("SuccessfullyFindTracked.2", dbSet2.Find(model2.Id).Description); - Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Added, context.ChangeTracker.GetEntry(model).State); - Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Added, context.ChangeTracker.GetEntry(model2).State); - } - - [TestMethod] - public void FindRequiresId() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - Assert.ThrowsException(() => dbSet.Find(null)); - } - - [TestMethod] - public async Task SuccessfulInsertAndFindAsync() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); - var dbSet2 = new MongoDbTenantSet(context2); - - var entity1 = new TestModel {Description = "SuccessfulInsertAndFind.1"}; - var entity2 = new TestModel {Description = "SuccessfulInsertAndFind.2"}; - - dbSet.Add(entity1); - dbSet2.Add(entity2); - - context.SaveChanges(); - context2.SaveChanges(); - - context = new MongoDbTenantContext(connection, tenantId); - dbSet = new MongoDbTenantSet(context); - Assert.AreEqual("SuccessfulInsertAndFind.1", (await dbSet.FindAsync(entity1.Id)).Description); - Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(entity1).State); - } - - [TestMethod] - public async Task SuccessfulNullFindAsync() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); - var dbSet2 = new MongoDbTenantSet(context2); - - var entity1 = new TestModel {Description = "SuccessfulInsertAndFind.1"}; - var entity2 = new TestModel {Description = "SuccessfulInsertAndFind.1"}; - - dbSet.Add(entity1); - dbSet2.Add(entity2); - - context.SaveChanges(); - context2.SaveChanges(); - - Assert.IsNull(await dbSet.FindAsync("abcd")); - } - - [TestMethod] - public async Task BlocksWrongTenantFindAsync() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); - var dbSet2 = new MongoDbTenantSet(context2); - - var entity1 = new TestModel {Description = "SuccessfulInsertAndFind.1"}; - var entity2 = new TestModel {Description = "SuccessfulInsertAndFind.1"}; - - dbSet.Add(entity1); - dbSet2.Add(entity2); - - context.SaveChanges(); - context2.SaveChanges(); - - Assert.IsNull(await dbSet.FindAsync(entity2.Id)); - } - - [TestMethod] - public async Task SuccessfullyFindAsyncTracked() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); - var dbSet2 = new MongoDbTenantSet(context2); - - var model = new TestModel - { - Id = "abcd", - Description = "SuccessfullyFindTracked.1" - }; - - var model2 = new TestModel - { - Id = "abcd", - Description = "SuccessfullyFindTracked.2" - }; - - dbSet.Add(model); - dbSet2.Add(model2); - - //Note: not saving, but still should be found as tracked - Assert.AreEqual("SuccessfullyFindTracked.1", (await dbSet.FindAsync(model.Id)).Description); - Assert.AreEqual("SuccessfullyFindTracked.2", (await dbSet2.FindAsync(model2.Id)).Description); - Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Added, context.ChangeTracker.GetEntry(model).State); - Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Added, context.ChangeTracker.GetEntry(model2).State); - } - - [TestMethod] - public async Task FindAsyncRequiresId() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - await Assert.ThrowsExceptionAsync(async () => await dbSet.FindAsync(null)); - } - - [TestMethod] - public void SuccessfullyUpdateEntity() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var entity = new TestModel - { - Description = "SuccessfullyUpdateEntity" - }; - - dbSet.Add(entity); - context.SaveChanges(); - - dbSet = new MongoDbTenantSet(context); - - entity.Description = "SuccessfullyUpdateEntity-Updated"; - dbSet.Update(entity); - - Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyUpdateEntity-Updated")); - context.SaveChanges(); - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyUpdateEntity-Updated")); - Assert.IsTrue(dbSet.First(m => m.Description == "SuccessfullyUpdateEntity-Updated").TenantId == tenantId); - } - - - [TestMethod] - public void SuccessfullyBlocksUpdateEntity() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var entity = new TestModel - { - Description = "SuccessfullyBlocksUpdateEntity" - }; - - dbSet.Add(entity); - context.SaveChanges(); - - dbSet = new MongoDbTenantSet(context); - entity.TenantId = "qweasd"; - entity.Description = "SuccessfullyBlocksUpdateEntity-Updated"; - Assert.ThrowsException(() => dbSet.Update(entity)); - - } - - [TestMethod] - public void SuccessfullyBlocksUpdateChangedEntity() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var entity = new TestModel - { - Description = "SuccessfullyBlocksUpdateChangedEntity" - }; - - dbSet.Add(entity); - context.SaveChanges(); - entity.Description = "SuccessfullyBlocksUpdateChangedEntity-Updated"; - - dbSet.Update(entity); - - //changing tenant ID after state is updated - entity.TenantId = "qweasd"; - Assert.ThrowsException(() => context.SaveChanges()); - } - - - [TestMethod] - public void SuccessfullyUpdateRange() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var entities = new[] { - new TestModel - { - Description = "SuccessfullyUpdateRange.1" - }, - new TestModel - { - Description = "SuccessfullyUpdateRange.2" - } - }; - - dbSet.AddRange(entities); - context.SaveChanges(); - - dbSet = new MongoDbTenantSet(context); - - entities[0].Description = "SuccessfullyUpdateRange.1-Updated"; - entities[1].Description = "SuccessfullyUpdateRange.2-Updated"; - dbSet.UpdateRange(entities); - - Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyUpdateRange.1-Updated" || m.Description == "SuccessfullyUpdateRange.2-Updated")); - context.SaveChanges(); - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyUpdateRange.1-Updated")); - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyUpdateRange.2-Updated")); - } - - [TestMethod] - public void SuccessfullyBlocksUpdateRange() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var entities = new[] { - new TestModel - { - Description = "SuccessfullyBlocksUpdateRange.1" - }, - new TestModel - { - Description = "SuccessfullyBlocksUpdateRange.2" - } - }; - - dbSet.AddRange(entities); - context.SaveChanges(); - - dbSet = new MongoDbTenantSet(context); - - entities[0].Description = "SuccessfullyBlocksUpdateRange.1-Updated"; - entities[0].TenantId = "qweasd"; - - entities[1].Description = "SuccessfullyBlocksUpdateRange.2-Updated"; - entities[1].TenantId = "qweasd"; - Assert.ThrowsException(() => dbSet.UpdateRange(entities)); - } - - [TestMethod] - public void SuccessfullyRemoveEntity() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var entity = new TestModel - { - Description = "SuccessfullyRemoveEntity" - }; - - dbSet.Add(entity); - context.SaveChanges(); - - dbSet = new MongoDbTenantSet(context); - - dbSet.Remove(entity); - - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntity")); - context.SaveChanges(); - Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntity")); - } - - [TestMethod] - public void SuccessfullyBlocksRemoveEntity() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var entity = new TestModel - { - Description = "SuccessfullyBlocksRemoveEntity" - }; - - dbSet.Add(entity); - context.SaveChanges(); - - entity.TenantId = "qweasd"; - - dbSet = new MongoDbTenantSet(context); - - Assert.ThrowsException(() => dbSet.Remove(entity)); - - } - - [TestMethod] - public void SuccessfullyRemoveRange() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var entities = new[] { - new TestModel - { - Description = "SuccessfullyRemoveRange.1" - }, - new TestModel - { - Description = "SuccessfullyRemoveRange.2" - } - }; - - dbSet.AddRange(entities); - context.SaveChanges(); - - dbSet = new MongoDbTenantSet(context); - - dbSet.RemoveRange(entities); - - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.1")); - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.2")); - context.SaveChanges(); - Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.1")); - Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.2")); - } - - [TestMethod] - public void SuccessfullyBlocksRemoveRange() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var entities = new[] { - new TestModel - { - Description = "SuccessfullyBlocksRemoveRange.1" - }, - new TestModel - { - Description = "SuccessfullyBlocksRemoveRange.2" - } - }; - - dbSet.AddRange(entities); - context.SaveChanges(); - - dbSet = new MongoDbTenantSet(context); - - entities[0].TenantId = "qweasd"; - entities[1].TenantId = "qweasd"; - - Assert.ThrowsException(() => dbSet.RemoveRange(entities)); - - } - - [TestMethod] - public void SuccessfullyRemoveEntityById() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); + } + + [TestMethod] + public void SuccessfulInsertAndQueryBack() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + dbSet.Add(new TestModel + { + Description = "SuccessfulInsertAndQueryBack" + }); + + Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfulInsertAndQueryBack")); + context.SaveChanges(); + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfulInsertAndQueryBack")); + Assert.IsTrue(dbSet.Any(m => m.TenantId == tenantId)); + } + + [TestMethod] + public async Task SuccessfulInsertAndQueryBackAsync() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + dbSet.Add(new TestModel + { + Description = "SuccessfulInsertAndQueryBackAsync" + }); + + Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfulInsertAndQueryBackAsync")); + await context.SaveChangesAsync(); + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfulInsertAndQueryBackAsync")); + Assert.IsTrue(dbSet.Any(m => m.TenantId == tenantId)); + } + + [TestMethod] + public void SuccessfulInsertAndFind() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); + var dbSet2 = new MongoDbTenantSet(context2); + + var entity1 = new TestModel { Description = "SuccessfulInsertAndFind.1" }; + var entity2 = new TestModel { Description = "SuccessfulInsertAndFind.2" }; + + dbSet.Add(entity1); + dbSet2.Add(entity2); + + context.SaveChanges(); + context2.SaveChanges(); + + context = new MongoDbTenantContext(connection, tenantId); + dbSet = new MongoDbTenantSet(context); + Assert.AreEqual("SuccessfulInsertAndFind.1", dbSet.Find(entity1.Id).Description); + Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(entity1).State); + } + + [TestMethod] + public void SuccessfulNullFind() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); + var dbSet2 = new MongoDbTenantSet(context2); + + var entity1 = new TestModel { Description = "SuccessfulInsertAndFind.1" }; + var entity2 = new TestModel { Description = "SuccessfulInsertAndFind.2" }; + + dbSet.Add(entity1); + dbSet2.Add(entity2); + + context.SaveChanges(); + context2.SaveChanges(); + + Assert.IsNull(dbSet.Find("abcd")); + } + + [TestMethod] + public void BlocksWrongTenantFind() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); + var dbSet2 = new MongoDbTenantSet(context2); + + var entity1 = new TestModel { Description = "SuccessfulInsertAndFind.1" }; + var entity2 = new TestModel { Description = "SuccessfulInsertAndFind.2" }; + + dbSet.Add(entity1); + dbSet2.Add(entity2); + + context.SaveChanges(); + context2.SaveChanges(); + + Assert.IsNull(dbSet.Find(entity2.Id)); + } + + [TestMethod] + public void SuccessfullyFindTracked() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); + var dbSet2 = new MongoDbTenantSet(context2); + + var model = new TestModel + { + Id = "abcd", + Description = "SuccessfullyFindTracked.1" + }; + + var model2 = new TestModel + { + Id = "abcd", + Description = "SuccessfullyFindTracked.2" + }; + + dbSet.Add(model); + dbSet2.Add(model2); + + //Note: not saving, but still should be found as tracked + Assert.AreEqual("SuccessfullyFindTracked.1", dbSet.Find(model.Id).Description); + Assert.AreEqual("SuccessfullyFindTracked.2", dbSet2.Find(model2.Id).Description); + Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Added, context.ChangeTracker.GetEntry(model).State); + Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Added, context.ChangeTracker.GetEntry(model2).State); + } + + [TestMethod] + public void FindRequiresId() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + Assert.ThrowsException(() => dbSet.Find(null)); + } + + [TestMethod] + public async Task SuccessfulInsertAndFindAsync() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); var dbSet2 = new MongoDbTenantSet(context2); - - var entity = new TestModel - { - Description = "SuccessfullyRemoveEntityById" - }; - - dbSet.Add(entity); - context.SaveChanges(); - - var entity2 = new TestModel - { - Description = "SuccessfullyRemoveEntityById" - }; - - dbSet2.Add(entity2); - context2.SaveChanges(); - - dbSet = new MongoDbTenantSet(context); - + + var entity1 = new TestModel { Description = "SuccessfulInsertAndFind.1" }; + var entity2 = new TestModel { Description = "SuccessfulInsertAndFind.2" }; + + dbSet.Add(entity1); + dbSet2.Add(entity2); + + context.SaveChanges(); + context2.SaveChanges(); + + context = new MongoDbTenantContext(connection, tenantId); + dbSet = new MongoDbTenantSet(context); + Assert.AreEqual("SuccessfulInsertAndFind.1", (await dbSet.FindAsync(entity1.Id)).Description); + Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.NoChanges, context.ChangeTracker.GetEntry(entity1).State); + } + + [TestMethod] + public async Task SuccessfulNullFindAsync() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); + var dbSet2 = new MongoDbTenantSet(context2); + + var entity1 = new TestModel { Description = "SuccessfulInsertAndFind.1" }; + var entity2 = new TestModel { Description = "SuccessfulInsertAndFind.1" }; + + dbSet.Add(entity1); + dbSet2.Add(entity2); + + context.SaveChanges(); + context2.SaveChanges(); + + Assert.IsNull(await dbSet.FindAsync("abcd")); + } + + [TestMethod] + public async Task BlocksWrongTenantFindAsync() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); + var dbSet2 = new MongoDbTenantSet(context2); + + var entity1 = new TestModel { Description = "SuccessfulInsertAndFind.1" }; + var entity2 = new TestModel { Description = "SuccessfulInsertAndFind.1" }; + + dbSet.Add(entity1); + dbSet2.Add(entity2); + + context.SaveChanges(); + context2.SaveChanges(); + + Assert.IsNull(await dbSet.FindAsync(entity2.Id)); + } + + [TestMethod] + public async Task SuccessfullyFindAsyncTracked() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); + var dbSet2 = new MongoDbTenantSet(context2); + + var model = new TestModel + { + Id = "abcd", + Description = "SuccessfullyFindTracked.1" + }; + + var model2 = new TestModel + { + Id = "abcd", + Description = "SuccessfullyFindTracked.2" + }; + + dbSet.Add(model); + dbSet2.Add(model2); + + //Note: not saving, but still should be found as tracked + Assert.AreEqual("SuccessfullyFindTracked.1", (await dbSet.FindAsync(model.Id)).Description); + Assert.AreEqual("SuccessfullyFindTracked.2", (await dbSet2.FindAsync(model2.Id)).Description); + Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Added, context.ChangeTracker.GetEntry(model).State); + Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Added, context.ChangeTracker.GetEntry(model2).State); + } + + [TestMethod] + public async Task FindAsyncRequiresId() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + await Assert.ThrowsExceptionAsync(async () => await dbSet.FindAsync(null)); + } + + [TestMethod] + public void SuccessfullyUpdateEntity() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var entity = new TestModel + { + Description = "SuccessfullyUpdateEntity" + }; + + dbSet.Add(entity); + context.SaveChanges(); + + dbSet = new MongoDbTenantSet(context); + + entity.Description = "SuccessfullyUpdateEntity-Updated"; + dbSet.Update(entity); + + Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyUpdateEntity-Updated")); + context.SaveChanges(); + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyUpdateEntity-Updated")); + Assert.IsTrue(dbSet.First(m => m.Description == "SuccessfullyUpdateEntity-Updated").TenantId == tenantId); + } + + + [TestMethod] + public void SuccessfullyBlocksUpdateEntity() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var entity = new TestModel + { + Description = "SuccessfullyBlocksUpdateEntity" + }; + + dbSet.Add(entity); + context.SaveChanges(); + + dbSet = new MongoDbTenantSet(context); + entity.TenantId = "qweasd"; + entity.Description = "SuccessfullyBlocksUpdateEntity-Updated"; + Assert.ThrowsException(() => dbSet.Update(entity)); + + } + + [TestMethod] + public void SuccessfullyBlocksUpdateChangedEntity() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var entity = new TestModel + { + Description = "SuccessfullyBlocksUpdateChangedEntity" + }; + + dbSet.Add(entity); + context.SaveChanges(); + entity.Description = "SuccessfullyBlocksUpdateChangedEntity-Updated"; + + dbSet.Update(entity); + + //changing tenant ID after state is updated + entity.TenantId = "qweasd"; + Assert.ThrowsException(() => context.SaveChanges()); + } + + + [TestMethod] + public void SuccessfullyUpdateRange() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var entities = new[] { + new TestModel + { + Description = "SuccessfullyUpdateRange.1" + }, + new TestModel + { + Description = "SuccessfullyUpdateRange.2" + } + }; + + dbSet.AddRange(entities); + context.SaveChanges(); + + dbSet = new MongoDbTenantSet(context); + + entities[0].Description = "SuccessfullyUpdateRange.1-Updated"; + entities[1].Description = "SuccessfullyUpdateRange.2-Updated"; + dbSet.UpdateRange(entities); + + Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyUpdateRange.1-Updated" || m.Description == "SuccessfullyUpdateRange.2-Updated")); + context.SaveChanges(); + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyUpdateRange.1-Updated")); + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyUpdateRange.2-Updated")); + } + + [TestMethod] + public void SuccessfullyBlocksUpdateRange() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var entities = new[] { + new TestModel + { + Description = "SuccessfullyBlocksUpdateRange.1" + }, + new TestModel + { + Description = "SuccessfullyBlocksUpdateRange.2" + } + }; + + dbSet.AddRange(entities); + context.SaveChanges(); + + dbSet = new MongoDbTenantSet(context); + + entities[0].Description = "SuccessfullyBlocksUpdateRange.1-Updated"; + entities[0].TenantId = "qweasd"; + + entities[1].Description = "SuccessfullyBlocksUpdateRange.2-Updated"; + entities[1].TenantId = "qweasd"; + Assert.ThrowsException(() => dbSet.UpdateRange(entities)); + } + + [TestMethod] + public void SuccessfullyRemoveEntity() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var entity = new TestModel + { + Description = "SuccessfullyRemoveEntity" + }; + + dbSet.Add(entity); + context.SaveChanges(); + + dbSet = new MongoDbTenantSet(context); + + dbSet.Remove(entity); + + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntity")); + context.SaveChanges(); + Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntity")); + } + + [TestMethod] + public void SuccessfullyBlocksRemoveEntity() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var entity = new TestModel + { + Description = "SuccessfullyBlocksRemoveEntity" + }; + + dbSet.Add(entity); + context.SaveChanges(); + + entity.TenantId = "qweasd"; + + dbSet = new MongoDbTenantSet(context); + + Assert.ThrowsException(() => dbSet.Remove(entity)); + + } + + [TestMethod] + public void SuccessfullyRemoveRange() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var entities = new[] { + new TestModel + { + Description = "SuccessfullyRemoveRange.1" + }, + new TestModel + { + Description = "SuccessfullyRemoveRange.2" + } + }; + + dbSet.AddRange(entities); + context.SaveChanges(); + + dbSet = new MongoDbTenantSet(context); + + dbSet.RemoveRange(entities); + + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.1")); + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.2")); + context.SaveChanges(); + Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.1")); + Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveRange.2")); + } + + [TestMethod] + public void SuccessfullyBlocksRemoveRange() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var entities = new[] { + new TestModel + { + Description = "SuccessfullyBlocksRemoveRange.1" + }, + new TestModel + { + Description = "SuccessfullyBlocksRemoveRange.2" + } + }; + + dbSet.AddRange(entities); + context.SaveChanges(); + + dbSet = new MongoDbTenantSet(context); + + entities[0].TenantId = "qweasd"; + entities[1].TenantId = "qweasd"; + + Assert.ThrowsException(() => dbSet.RemoveRange(entities)); + + } + + [TestMethod] + public void SuccessfullyRemoveEntityById() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); + var dbSet2 = new MongoDbTenantSet(context2); + + var entity = new TestModel + { + Description = "SuccessfullyRemoveEntityById" + }; + + dbSet.Add(entity); + context.SaveChanges(); + + var entity2 = new TestModel + { + Description = "SuccessfullyRemoveEntityById" + }; + + dbSet2.Add(entity2); + context2.SaveChanges(); + + dbSet = new MongoDbTenantSet(context); + dbSet.RemoveById(entity.Id); //mismatched tenant, should not delete anything dbSet.RemoveById(entity2.Id); - - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntityById")); - Assert.IsTrue(dbSet2.Any(m => m.Description == "SuccessfullyRemoveEntityById")); - context.SaveChanges(); - Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntityById")); - Assert.IsTrue(dbSet2.Any(m => m.Description == "SuccessfullyRemoveEntityById")); - } - - [TestMethod] - public async Task SuccessfullyRemoveEntityByIdAsync() - { + + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntityById")); + Assert.IsTrue(dbSet2.Any(m => m.Description == "SuccessfullyRemoveEntityById")); + context.SaveChanges(); + Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntityById")); + Assert.IsTrue(dbSet2.Any(m => m.Description == "SuccessfullyRemoveEntityById")); + } + + [TestMethod] + public async Task SuccessfullyRemoveEntityByIdAsync() + { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); + var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet(context); - var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); + var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); var dbSet2 = new MongoDbTenantSet(context2); - - var entity = new TestModel - { - Description = "SuccessfullyRemoveEntityByIdAsync" - }; - - dbSet.Add(entity); - await context.SaveChangesAsync(); - - var entity2 = new TestModel - { - Description = "SuccessfullyRemoveEntityByIdAsync" - }; - - dbSet2.Add(entity2); - await context2.SaveChangesAsync(); - - dbSet = new MongoDbTenantSet(context); - + + var entity = new TestModel + { + Description = "SuccessfullyRemoveEntityByIdAsync" + }; + + dbSet.Add(entity); + await context.SaveChangesAsync(); + + var entity2 = new TestModel + { + Description = "SuccessfullyRemoveEntityByIdAsync" + }; + + dbSet2.Add(entity2); + await context2.SaveChangesAsync(); + + dbSet = new MongoDbTenantSet(context); + dbSet.RemoveById(entity.Id); //mismatched tenant, should not delete anything dbSet.RemoveById(entity2.Id); - - Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntityByIdAsync")); - Assert.IsTrue(dbSet2.Any(m => m.Description == "SuccessfullyRemoveEntityByIdAsync")); - await context.SaveChangesAsync(); - Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntityByIdAsync")); - Assert.IsTrue(dbSet2.Any(m => m.Description == "SuccessfullyRemoveEntityByIdAsync")); - } - - [TestMethod] - public void SuccessfullyRemoveRangeByPredicate() - { + + Assert.IsTrue(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntityByIdAsync")); + Assert.IsTrue(dbSet2.Any(m => m.Description == "SuccessfullyRemoveEntityByIdAsync")); + await context.SaveChangesAsync(); + Assert.IsFalse(dbSet.Any(m => m.Description == "SuccessfullyRemoveEntityByIdAsync")); + Assert.IsTrue(dbSet2.Any(m => m.Description == "SuccessfullyRemoveEntityByIdAsync")); + } + + [TestMethod] + public void SuccessfullyRemoveRangeByPredicate() + { var connection = TestConfiguration.GetConnection(); var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); + var context = new MongoDbTenantContext(connection, tenantId); var dbSet = new MongoDbTenantSet(context); - var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); + var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); var dbSet2 = new MongoDbTenantSet(context2); - - - var entities = new[] { - new TestModel - { - Description = "SuccessfullyRemoveRangeByPredicate" - }, - new TestModel - { - Description = "SuccessfullyRemoveRangeByPredicate", - BooleanField = true - } - }; - - var entities2 = new[] { - new TestModel - { - Description = "SuccessfullyRemoveRangeByPredicate" - }, - new TestModel - { - Description = "SuccessfullyRemoveRangeByPredicate", - BooleanField = true - } - }; - - dbSet.AddRange(entities); - dbSet2.AddRange(entities2); - + + + var entities = new[] { + new TestModel + { + Description = "SuccessfullyRemoveRangeByPredicate" + }, + new TestModel + { + Description = "SuccessfullyRemoveRangeByPredicate", + BooleanField = true + } + }; + + var entities2 = new[] { + new TestModel + { + Description = "SuccessfullyRemoveRangeByPredicate" + }, + new TestModel + { + Description = "SuccessfullyRemoveRangeByPredicate", + BooleanField = true + } + }; + + dbSet.AddRange(entities); + dbSet2.AddRange(entities2); + context.SaveChanges(); context2.SaveChanges(); - dbSet = new MongoDbTenantSet(context); - - dbSet.RemoveRange(e => e.BooleanField); - - Assert.AreEqual(2, dbSet.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); - Assert.AreEqual(2, dbSet2.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); - context.SaveChanges(); - Assert.AreEqual(1, dbSet.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); - Assert.AreEqual(2, dbSet2.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); - Assert.IsNotNull(dbSet.FirstOrDefault(m => m.Id == entities[0].Id)); + dbSet = new MongoDbTenantSet(context); + + dbSet.RemoveRange(e => e.BooleanField); + + Assert.AreEqual(2, dbSet.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); + Assert.AreEqual(2, dbSet2.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); + context.SaveChanges(); + Assert.AreEqual(1, dbSet.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); + Assert.AreEqual(2, dbSet2.Count(m => m.Description == "SuccessfullyRemoveRangeByPredicate")); + Assert.IsNotNull(dbSet.FirstOrDefault(m => m.Id == entities[0].Id)); } [TestMethod] public void SuccessfullyReturnsBaseContext() { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - Assert.IsInstanceOfType((dbSet as IMongoDbSet).Context, typeof(IMongoDbContext)); + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + Assert.IsInstanceOfType((dbSet as IMongoDbSet).Context, typeof(IMongoDbContext)); } [TestMethod] public void SuccessfullyBlocksNulls() { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - Assert.ThrowsException(() => dbSet.Add(null)); - Assert.ThrowsException(() => dbSet.AddRange(null)); - Assert.ThrowsException(() => dbSet.Update(null)); - Assert.ThrowsException(() => dbSet.UpdateRange(null)); - } - - [TestMethod] - public void AllowsUniquesByTenant() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); - var dbSet2 = new MongoDbTenantSet(context2); - - dbSet.Add(new TestUniqueModel{UserName = "AllowsUniquesByTenant.1"}); - dbSet.Add(new TestUniqueModel{UserName = "AllowsUniquesByTenant.2"}); - dbSet2.Add(new TestUniqueModel{UserName = "AllowsUniquesByTenant.1"}); - dbSet2.Add(new TestUniqueModel{UserName = "AllowsUniquesByTenant.2"}); - - context.SaveChanges(); - context2.SaveChanges(); - - Assert.AreEqual(2, dbSet.Count()); - Assert.AreEqual(2, dbSet2.Count()); - } - - [TestMethod] - public void BlocksDuplicatesByTenant() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); - var dbSet2 = new MongoDbTenantSet(context2); - - dbSet.Add(new TestUniqueModel{UserName = "BlocksDuplicatesByTenant"}); - dbSet2.Add(new TestUniqueModel{UserName = "BlocksDuplicatesByTenant"}); - - context.SaveChanges(); - context2.SaveChanges(); - - dbSet.Add(new TestUniqueModel{UserName = "BlocksDuplicatesByTenant"}); - Assert.ThrowsException>(() => context.SaveChanges()); - } - [TestMethod] - public void SuccessfullyLinqFindTracked() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var model = new TestModel - { - Id = "abcd", - Description = "SuccessfullyFindTracked.1" - }; - - dbSet.Add(model); + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + Assert.ThrowsException(() => dbSet.Add(null)); + Assert.ThrowsException(() => dbSet.AddRange(null)); + Assert.ThrowsException(() => dbSet.Update(null)); + Assert.ThrowsException(() => dbSet.UpdateRange(null)); + } + + [TestMethod] + public void AllowsUniquesByTenant() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); + var dbSet2 = new MongoDbTenantSet(context2); + + dbSet.Add(new TestUniqueModel { UserName = "AllowsUniquesByTenant.1" }); + dbSet.Add(new TestUniqueModel { UserName = "AllowsUniquesByTenant.2" }); + dbSet2.Add(new TestUniqueModel { UserName = "AllowsUniquesByTenant.1" }); + dbSet2.Add(new TestUniqueModel { UserName = "AllowsUniquesByTenant.2" }); + + context.SaveChanges(); + context2.SaveChanges(); + + Assert.AreEqual(2, dbSet.Count()); + Assert.AreEqual(2, dbSet2.Count()); + } + + [TestMethod] + public void BlocksDuplicatesByTenant() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var context2 = new MongoDbTenantContext(connection, tenantId + "-alt"); + var dbSet2 = new MongoDbTenantSet(context2); + + dbSet.Add(new TestUniqueModel { UserName = "BlocksDuplicatesByTenant" }); + dbSet2.Add(new TestUniqueModel { UserName = "BlocksDuplicatesByTenant" }); + + context.SaveChanges(); + context2.SaveChanges(); + + dbSet.Add(new TestUniqueModel { UserName = "BlocksDuplicatesByTenant" }); + Assert.ThrowsException>(() => context.SaveChanges()); + } + [TestMethod] + public void SuccessfullyLinqFindTracked() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var model = new TestModel + { + Id = "abcd", + Description = "SuccessfullyFindTracked.1" + }; + + dbSet.Add(model); context.SaveChanges(); @@ -845,24 +843,24 @@ public void SuccessfullyLinqFindTracked() result.Description = "changed"; context.ChangeTracker.DetectChanges(); - Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Updated, context.ChangeTracker.GetEntry(result).State); - } - - [TestMethod] - public async Task SuccessfullyLinqFindTrackedAsync() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var model = new TestModel - { - Id = "abcd", - Description = "SuccessfullyFindTracked.1" - }; - - dbSet.Add(model); + Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Updated, context.ChangeTracker.GetEntry(result).State); + } + + [TestMethod] + public async Task SuccessfullyLinqFindTrackedAsync() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var model = new TestModel + { + Id = "abcd", + Description = "SuccessfullyFindTracked.1" + }; + + dbSet.Add(model); context.SaveChanges(); @@ -872,64 +870,64 @@ public async Task SuccessfullyLinqFindTrackedAsync() result.Description = "changed"; context.ChangeTracker.DetectChanges(); - Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Updated, context.ChangeTracker.GetEntry(result).State); - } - - [TestMethod] - public void SuccessfullyLinqFindNoTracking() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var model = new TestModel - { - Id = "abcd", - Description = "SuccessfullyLinqFindNoTracking.1" - }; - - dbSet.Add(model); + Assert.AreEqual(MongoFramework.Infrastructure.EntityEntryState.Updated, context.ChangeTracker.GetEntry(result).State); + } + + [TestMethod] + public void SuccessfullyLinqFindNoTracking() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var model = new TestModel + { + Id = "abcd", + Description = "SuccessfullyLinqFindNoTracking.1" + }; + + dbSet.Add(model); context.SaveChanges(); ResetMongoDb(); - context = new MongoDbTenantContext(connection, tenantId); - dbSet = new MongoDbTenantSet(context); + context = new MongoDbTenantContext(connection, tenantId); + dbSet = new MongoDbTenantSet(context); var result = dbSet.AsNoTracking().FirstOrDefault(); - Assert.IsNull(context.ChangeTracker.GetEntry(result)); - } - - [TestMethod] - public async Task SuccessfullyLinqFindNoTrackingAsync() - { - var connection = TestConfiguration.GetConnection(); - var tenantId = TestConfiguration.GetTenantId(); - var context = new MongoDbTenantContext(connection, tenantId); - var dbSet = new MongoDbTenantSet(context); - - var model = new TestModel - { - Id = "abcd", - Description = "SuccessfullyFindTracked.1" - }; - - dbSet.Add(model); + Assert.IsNull(context.ChangeTracker.GetEntry(result)); + } + + [TestMethod] + public async Task SuccessfullyLinqFindNoTrackingAsync() + { + var connection = TestConfiguration.GetConnection(); + var tenantId = TestConfiguration.GetTenantId(); + var context = new MongoDbTenantContext(connection, tenantId); + var dbSet = new MongoDbTenantSet(context); + + var model = new TestModel + { + Id = "abcd", + Description = "SuccessfullyFindTracked.1" + }; + + dbSet.Add(model); context.SaveChanges(); ResetMongoDb(); - - context = new MongoDbTenantContext(connection, tenantId); - dbSet = new MongoDbTenantSet(context); + + context = new MongoDbTenantContext(connection, tenantId); + dbSet = new MongoDbTenantSet(context); var result = await dbSet.AsNoTracking().FirstOrDefaultAsync(); - Assert.IsNull(context.ChangeTracker.GetEntry(result)); + Assert.IsNull(context.ChangeTracker.GetEntry(result)); } - } + } } \ No newline at end of file diff --git a/tests/MongoFramework.Tests/Profiling/MiniProfiler/MiniProfilerDiagnosticListenerTests.cs b/tests/MongoFramework.Tests/Profiling/MiniProfiler/MiniProfilerDiagnosticListenerTests.cs index da8feecb..399b8692 100644 --- a/tests/MongoFramework.Tests/Profiling/MiniProfiler/MiniProfilerDiagnosticListenerTests.cs +++ b/tests/MongoFramework.Tests/Profiling/MiniProfiler/MiniProfilerDiagnosticListenerTests.cs @@ -119,7 +119,7 @@ public void ProfilingRead() Name = "ProfilingRead" }); } - + context.SaveChanges(); var profiler = SEProfiling.MiniProfiler.StartNew(); diff --git a/tests/MongoFramework.Tests/TestBase.cs b/tests/MongoFramework.Tests/TestBase.cs index 8892687d..bf5cb512 100644 --- a/tests/MongoFramework.Tests/TestBase.cs +++ b/tests/MongoFramework.Tests/TestBase.cs @@ -3,7 +3,6 @@ using MongoFramework.Infrastructure.Indexing; using MongoFramework.Infrastructure.Mapping; using MongoFramework.Infrastructure.Serialization; -using System; namespace MongoFramework.Tests { diff --git a/tests/MongoFramework.Tests/Utilities/CheckTests.cs b/tests/MongoFramework.Tests/Utilities/CheckTests.cs index 6f1dbeca..4f7f904b 100644 --- a/tests/MongoFramework.Tests/Utilities/CheckTests.cs +++ b/tests/MongoFramework.Tests/Utilities/CheckTests.cs @@ -9,54 +9,54 @@ namespace MongoFramework.Tests.Utilities { [TestClass] - public class CheckTest - { - [TestMethod] - public void Not_null_throws_when_arg_is_null() - { - Assert.ThrowsException(() => Check.NotNull(null, "foo")); - } - - [TestMethod] - public void Not_null_throws_when_arg_name_empty() - { - Assert.ThrowsException(() => Check.NotNull(null as object, string.Empty)); - } - - [TestMethod] - public void Not_empty_throws_when_empty() - { - Assert.ThrowsException(() => Check.NotEmpty("", string.Empty)); - } - - [TestMethod] - public void Not_empty_throws_when_whitespace() - { - Assert.ThrowsException(() => Check.NotEmpty(" ", string.Empty)); - } - - [TestMethod] - public void Not_empty_throws_when_parameter_name_null() - { - Assert.ThrowsException(() => Check.NotEmpty(null, null)); - } - - [TestMethod] - public void Generic_Not_empty_throws_when_arg_is_empty() - { - Assert.ThrowsException(() => Check.NotEmpty(Array.Empty(), "foo")); - } - - [TestMethod] - public void Generic_Not_empty_throws_when_arg_is_null() - { - Assert.ThrowsException(() => Check.NotEmpty(null, "foo")); - } - - [TestMethod] - public void Generic_Not_empty_throws_when_arg_name_empty() - { - Assert.ThrowsException(() => Check.NotEmpty(null, string.Empty)); - } - } + public class CheckTest + { + [TestMethod] + public void Not_null_throws_when_arg_is_null() + { + Assert.ThrowsException(() => Check.NotNull(null, "foo")); + } + + [TestMethod] + public void Not_null_throws_when_arg_name_empty() + { + Assert.ThrowsException(() => Check.NotNull(null as object, string.Empty)); + } + + [TestMethod] + public void Not_empty_throws_when_empty() + { + Assert.ThrowsException(() => Check.NotEmpty("", string.Empty)); + } + + [TestMethod] + public void Not_empty_throws_when_whitespace() + { + Assert.ThrowsException(() => Check.NotEmpty(" ", string.Empty)); + } + + [TestMethod] + public void Not_empty_throws_when_parameter_name_null() + { + Assert.ThrowsException(() => Check.NotEmpty(null, null)); + } + + [TestMethod] + public void Generic_Not_empty_throws_when_arg_is_empty() + { + Assert.ThrowsException(() => Check.NotEmpty(Array.Empty(), "foo")); + } + + [TestMethod] + public void Generic_Not_empty_throws_when_arg_is_null() + { + Assert.ThrowsException(() => Check.NotEmpty(null, "foo")); + } + + [TestMethod] + public void Generic_Not_empty_throws_when_arg_name_empty() + { + Assert.ThrowsException(() => Check.NotEmpty(null, string.Empty)); + } + } }