public void EntityProcessorsFiresOnEnumerationOfTEntity() { EntityMapping.RegisterType(typeof(MongoFrameworkQueryableModel)); var connection = TestConfiguration.GetConnection(); var provider = new MongoFrameworkQueryProvider <MongoFrameworkQueryableModel>(connection); var queryable = new MongoFrameworkQueryable <MongoFrameworkQueryableModel>(provider); var processor = new TestProcessor <MongoFrameworkQueryableModel>(); provider.EntityProcessors.Add(processor); var entityCollection = new EntityCollection <MongoFrameworkQueryableModel>(); var writerPipeline = new EntityWriterPipeline <MongoFrameworkQueryableModel>(connection); writerPipeline.AddCollection(entityCollection); entityCollection.Update(new MongoFrameworkQueryableModel { Title = "EntityProcessorFireTest" }, EntityEntryState.Added); writerPipeline.Write(); foreach (var entity in queryable) { //Do nothing } Assert.IsTrue(processor.EntityProcessed); }
public void WhereIdMatchesGuids() { var connection = TestConfiguration.GetConnection(); var writerPipeline = new EntityWriterPipeline <WhereIdMatchesGuidModel>(TestConfiguration.GetConnection()); var entityCollection = new EntityCollection <WhereIdMatchesGuidModel>() { new WhereIdMatchesGuidModel { Description = "1" }, new WhereIdMatchesGuidModel { Description = "2" }, new WhereIdMatchesGuidModel { Description = "3" }, new WhereIdMatchesGuidModel { Description = "4" } }; writerPipeline.AddCollection(entityCollection); writerPipeline.Write(); var collection = TestConfiguration.GetConnection().GetDatabase().GetCollection <WhereIdMatchesGuidModel>(nameof(WhereIdMatchesGuidModel)); var underlyingQueryable = collection.AsQueryable(); var queryable = new MongoFrameworkQueryable <WhereIdMatchesGuidModel, WhereIdMatchesGuidModel>(connection, underlyingQueryable); 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))); }
public void AddMixedTypeEntities() { var connection = TestConfiguration.GetConnection(); var entityCollection = new EntityCollection <TestModel>(); var pipeline = new EntityWriterPipeline <TestModel>(connection); var reader = new EntityReader <TestModel>(connection); pipeline.AddCollection(entityCollection); var entities = new[] { new TestModel { Title = "DbEntityWriterTests.AddMixedTypeEntities" }, new ExtendedTestModel { Title = "DbEntityWriterTests.AddMixedTypeEntities", AdditionalField = "AdditionalFieldSet" } }; foreach (var entity in entities) { entityCollection.Update(entity, EntityEntryState.Added); } pipeline.Write(); Assert.IsTrue(reader.AsQueryable().OfType <TestModel>().Any(e => e.Title == "DbEntityWriterTests.AddMixedTypeEntities")); Assert.IsTrue(reader.AsQueryable().OfType <ExtendedTestModel>().Any(e => e.AdditionalField == "AdditionalFieldSet")); }
public virtual async Task SaveChangesAsync(CancellationToken cancellationToken = default) { await EntityIndexWriter.ApplyIndexingAsync(cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); await EntityWriterPipeline.WriteAsync(cancellationToken).ConfigureAwait(false); }
public void LoadRelationship() { var connection = TestConfiguration.GetConnection(); var relatedEntity = new StringIdModel { Description = "LoadRelationship-RelatedItem" }; var writerPipeline = new EntityWriterPipeline <StringIdModel>(connection); var entityCollection = new EntityCollection <StringIdModel>() { relatedEntity }; writerPipeline.AddCollection(entityCollection); writerPipeline.Write(); var entity = new SingleEntityIntegrationModel { RelatedItemId = relatedEntity.Id }; new NavigationPropertyMutator <SingleEntityIntegrationModel>().MutateEntity(entity, MutatorType.Select, connection); Assert.AreEqual("LoadRelationship-RelatedItem", entity.RelatedItem.Description); }
public void EntityProcessorsRunWhenToDictionaryIsUsed() { EntityMapping.RegisterType(typeof(MongoFrameworkQueryableModel)); var connection = TestConfiguration.GetConnection(); var provider = new MongoFrameworkQueryProvider <MongoFrameworkQueryableModel>(connection); var queryable = new MongoFrameworkQueryable <MongoFrameworkQueryableModel>(provider); var processor = new TestProcessor <MongoFrameworkQueryableModel>(); provider.EntityProcessors.Add(processor); var entityCollection = new EntityCollection <MongoFrameworkQueryableModel>(); var writerPipeline = new EntityWriterPipeline <MongoFrameworkQueryableModel>(connection); writerPipeline.AddCollection(entityCollection); entityCollection.Update(new MongoFrameworkQueryableModel { Title = "EntityProcessorsRunWithToDictionaryTest" }, EntityEntryState.Added); writerPipeline.Write(); var result = queryable.ToDictionary(m => m.Id); Assert.AreEqual("EntityProcessorsRunWithToDictionaryTest", result.FirstOrDefault().Value.Title); Assert.IsTrue(processor.EntityProcessed); }
public void ReadMixedEntities() { var connection = TestConfiguration.GetConnection(); var entityCollection = new EntityCollection <A>(); var reader = new EntityReader <A>(connection); var writerPipeline = new EntityWriterPipeline <A>(connection); writerPipeline.AddCollection(entityCollection); entityCollection.Update(new A { Description = "DbEntityReaderTests.ReadMixedEntities" }, EntityEntryState.Added); entityCollection.Update(new B { BIsForBoolean = true, Description = "DbEntityReaderTests.ReadMixedEntities" }, EntityEntryState.Added); writerPipeline.Write(); var readMixedEntitiesQuery = reader.AsQueryable().Where(e => e.Description == "DbEntityReaderTests.ReadMixedEntities"); Assert.AreEqual(2, readMixedEntitiesQuery.Count()); Assert.AreEqual(2, readMixedEntitiesQuery.OfType <A>().Count()); Assert.AreEqual(1, readMixedEntitiesQuery.OfType <B>().Count()); }
public void WriteUpdatesCollection() { var connection = TestConfiguration.GetConnection(); var entityCollection = new EntityCollection <TestModel>(); var pipeline = new EntityWriterPipeline <TestModel>(connection); pipeline.AddCollection(entityCollection); var entity = new TestModel { Title = "EntityWriterPipelineTests.WriteUpdatesCollection" }; entityCollection.Update(entity, EntityEntryState.Added); var entry = entityCollection.GetEntry(entity); Assert.AreEqual(EntityEntryState.Added, entry.State); pipeline.Write(); Assert.AreEqual(EntityEntryState.NoChanges, entry.State); entry.State = EntityEntryState.Updated; pipeline.Write(); Assert.AreEqual(EntityEntryState.NoChanges, entry.State); entry.State = EntityEntryState.Deleted; pipeline.Write(); entry = entityCollection.GetEntry(entity); Assert.IsNull(entry); }
public void WhereIdMatchesStringIds() { var connection = TestConfiguration.GetConnection(); var writerPipeline = new EntityWriterPipeline <WhereIdMatchesStringModel>(connection); var entityCollection = new EntityCollection <WhereIdMatchesStringModel>() { new WhereIdMatchesStringModel { Description = "1" }, new WhereIdMatchesStringModel { Description = "2" }, new WhereIdMatchesStringModel { Description = "3" }, new WhereIdMatchesStringModel { Description = "4" } }; writerPipeline.AddCollection(entityCollection); writerPipeline.Write(); var provider = new MongoFrameworkQueryProvider <WhereIdMatchesStringModel>(connection); var queryable = new MongoFrameworkQueryable <WhereIdMatchesStringModel>(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))); }
public void EntityProcessorsNotFiredWhenNotTEntity() { EntityMapping.RegisterType(typeof(MongoFrameworkQueryableModel)); var connection = TestConfiguration.GetConnection(); var collection = connection.GetDatabase().GetCollection <MongoFrameworkQueryableModel>(nameof(MongoFrameworkQueryableModel)); var underlyingQueryable = collection.AsQueryable(); var queryable = new MongoFrameworkQueryable <MongoFrameworkQueryableModel, MongoFrameworkQueryableModel>(connection, underlyingQueryable); var processor = new TestProcessor <MongoFrameworkQueryableModel>(); queryable.EntityProcessors.Add(processor); var entityCollection = new EntityCollection <MongoFrameworkQueryableModel>(); var writerPipeline = new EntityWriterPipeline <MongoFrameworkQueryableModel>(connection); writerPipeline.AddCollection(entityCollection); entityCollection.Update(new MongoFrameworkQueryableModel { Title = "EntityProcessorNoFireTest" }, EntityEntryState.Added); writerPipeline.Write(); foreach (var titles in queryable.Select(e => e.Title)) { //Do nothing } Assert.IsFalse(processor.EntityProcessed); }
public virtual void Remove(TGroup group) { if (group == null) { throw new ArgumentNullException(nameof(group)); } EntityWriterPipeline.StageCommand(new RemoveBucketCommand <TGroup, TSubEntity>(group)); }
/// <summary> /// Initialise a new entity reader and writer to the specified database. /// </summary> /// <param name="connection"></param> public void SetConnection(IMongoDbConnection connection) { Connection = connection; EntityWriterPipeline = new EntityWriterPipeline <TEntity>(connection); EntityReader = new EntityReader <TEntity>(connection); EntityIndexWriter = new EntityIndexWriter <TEntity>(connection); EntityRelationshipWriter = new EntityRelationshipWriter <TEntity>(connection); ChangeTracker = new EntityCollection <TEntity>(); EntityWriterPipeline.AddCollection(ChangeTracker); }
public void SetConnection(IMongoDbConnection connection) { EntityWriterPipeline = new EntityWriterPipeline <EntityBucket <TGroup, TSubEntity> >(connection); EntityReader = new EntityReader <EntityBucket <TGroup, TSubEntity> >(connection); EntityIndexWriter = new EntityIndexWriter <EntityBucket <TGroup, TSubEntity> >(connection); BucketStagingCollection = new EntityBucketStagingCollection <TGroup, TSubEntity>(EntityReader, BucketSize); ChangeTracker = new EntityCollection <EntityBucket <TGroup, TSubEntity> >(); EntityWriterPipeline.AddCollection(ChangeTracker); EntityWriterPipeline.AddCollection(BucketStagingCollection); }
public virtual void Add(TGroup group, TSubEntity entity) { if (group == null) { throw new ArgumentNullException(nameof(group)); } if (entity == null) { throw new ArgumentNullException(nameof(entity)); } EntityWriterPipeline.StageCommand(new AddToBucketCommand <TGroup, TSubEntity>(group, entity, EntityTimeProperty, BucketSize)); }
public void ValidObjectId() { var connection = TestConfiguration.GetConnection(); var writerPipeline = new EntityWriterPipeline <MongoDbUtilityModel>(connection); var entityCollection = new EntityCollection <MongoDbUtilityModel>(); writerPipeline.AddCollection(entityCollection); var entity = new MongoDbUtilityModel(); entityCollection.Update(entity, EntityEntryState.Added); writerPipeline.Write(); Assert.IsTrue(MongoDbUtility.IsValidObjectId(entity.Id)); }
public virtual void AddRange(TGroup group, IEnumerable <TSubEntity> entities) { if (group == null) { throw new ArgumentNullException(nameof(group)); } if (entities == null) { throw new ArgumentNullException(nameof(entities)); } foreach (var entity in entities) { EntityWriterPipeline.StageCommand(new AddToBucketCommand <TGroup, TSubEntity>(group, entity, EntityTimeProperty, BucketSize)); } }
public void WriteFromCollection() { var connection = TestConfiguration.GetConnection(); var entityCollection = new EntityCollection <TestModel>(); var pipeline = new EntityWriterPipeline <TestModel>(connection); var entity = new TestModel { Title = "EntityWriterPipelineTests.WriteFromCollection" }; entityCollection.Add(entity); pipeline.AddCollection(entityCollection); pipeline.Write(); Assert.IsNotNull(entity.Id); }
public void WriteFromStaging() { var connection = TestConfiguration.GetConnection(); var pipeline = new EntityWriterPipeline <TestModel>(connection); var entity = new TestModel { Title = "EntityWriterPipelineTests.WriteFromCollection" }; var command = EntityCommandBuilder <TestModel> .CreateCommand( new EntityEntry <TestModel>(entity, EntityEntryState.Added) ); pipeline.StageCommand(command); pipeline.Write(); Assert.IsNotNull(entity.Id); }
public async Task MixedActionWriteAsync() { var connection = TestConfiguration.GetConnection(); var entityCollection = new EntityCollection <TestModel>(); var pipeline = new EntityWriterPipeline <TestModel>(connection); var reader = new EntityReader <TestModel>(connection); pipeline.AddCollection(entityCollection); var updateEntity = new TestModel { Title = "EntityWriterPipelineTests.MixedActionWriteAsync-UpdateEntity" }; var deleteEntity = new TestModel { Title = "EntityWriterPipelineTests.MixedActionWriteAsync-DeleteEntity" }; entityCollection.Update(updateEntity, EntityEntryState.Added); entityCollection.Update(deleteEntity, EntityEntryState.Added); await pipeline.WriteAsync().ConfigureAwait(false); entityCollection.Clear(); var addedEntity = new TestModel { Title = "EntityWriterPipelineTests.MixedActionWriteAsync-AddEntity" }; updateEntity.Title = "EntityWriterPipelineTests.MixedActionWriteAsync-UpdateEntity-Updated"; entityCollection.Update(addedEntity, EntityEntryState.Added); entityCollection.Update(updateEntity, EntityEntryState.Updated); entityCollection.Update(deleteEntity, EntityEntryState.Deleted); await pipeline.WriteAsync().ConfigureAwait(false); Assert.IsTrue(reader.AsQueryable().Where(e => e.Id == addedEntity.Id).Any()); Assert.IsFalse(reader.AsQueryable().Where(e => e.Id == deleteEntity.Id).Any()); var dbEntity = reader.AsQueryable().Where(e => e.Id == updateEntity.Id).FirstOrDefault(); Assert.AreEqual("EntityWriterPipelineTests.MixedActionWriteAsync-UpdateEntity-Updated", dbEntity.Title); }
public void EnumerateQueryable() { EntityMapping.RegisterType(typeof(MongoFrameworkQueryableModel)); var connection = TestConfiguration.GetConnection(); var provider = new MongoFrameworkQueryProvider <MongoFrameworkQueryableModel>(connection); var queryable = new MongoFrameworkQueryable <MongoFrameworkQueryableModel>(provider); var entityCollection = new EntityCollection <MongoFrameworkQueryableModel>(); var writerPipeline = new EntityWriterPipeline <MongoFrameworkQueryableModel>(connection); writerPipeline.AddCollection(entityCollection); entityCollection.Update(new MongoFrameworkQueryableModel { Title = "EnumerateQueryable" }, EntityEntryState.Added); writerPipeline.Write(); foreach (var entity in queryable) { Assert.AreEqual("EnumerateQueryable", entity.Title); } }
/// <summary> /// Writes all of the items in the changeset to the database. /// </summary> /// <returns></returns> public virtual void SaveChanges() { EntityIndexWriter.ApplyIndexing(); EntityRelationshipWriter.CommitEntityRelationships(ChangeTracker); EntityWriterPipeline.Write(); }
/// <summary> /// Stages a deletion for the entity that matches the specified ID /// </summary> /// <param name="entityId"></param> public virtual void RemoveById(object entityId) { EntityWriterPipeline.StageCommand(new RemoveEntityByIdCommand <TEntity>(entityId)); }
public virtual void SaveChanges() { EntityIndexWriter.ApplyIndexing(); EntityWriterPipeline.Write(); }
/// <summary> /// Stages a deletion for a range of entities that match the predicate /// </summary> /// <param name="targetField"></param> public virtual void RemoveRange(Expression <Func <TEntity, bool> > predicate) { EntityWriterPipeline.StageCommand(new RemoveEntityRangeCommand <TEntity>(predicate)); }
public virtual void SaveChanges() { EntityIndexWriter.ApplyIndexing(); EntityWriterPipeline.Write(); BucketStagingCollection.Clear(); }
public void SetConnection(IMongoDbConnection connection) { EntityWriterPipeline = new EntityWriterPipeline <EntityBucket <TGroup, TSubEntity> >(connection); EntityReader = new EntityReader <EntityBucket <TGroup, TSubEntity> >(connection); EntityIndexWriter = new EntityIndexWriter <EntityBucket <TGroup, TSubEntity> >(connection); }