|
| 1 | +using FluentAssertions; |
| 2 | +using MongoDB.Bson; |
| 3 | +using MongoDB.Driver; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace JohnKnoop.MongoRepository.IntegrationTests |
| 12 | +{ |
| 13 | + public class MyBaseEntity |
| 14 | + { |
| 15 | + public MyBaseEntity(string name) |
| 16 | + { |
| 17 | + Id = ObjectId.GenerateNewId().ToString(); |
| 18 | + Name = name; |
| 19 | + } |
| 20 | + |
| 21 | + public string Id { get; private set; } |
| 22 | + public string Name { get; private set; } |
| 23 | + } |
| 24 | + |
| 25 | + public class MyDerivedEntity : MyBaseEntity { |
| 26 | + public MyDerivedEntity(string name, int age) : base(name) |
| 27 | + { |
| 28 | + Age = age; |
| 29 | + } |
| 30 | + |
| 31 | + public int Age { get; private set; } |
| 32 | + } |
| 33 | + |
| 34 | + [CollectionDefinition("IntegrationTests", DisableParallelization = true)] |
| 35 | + public class FindOneAndDeleteTests : IClassFixture<LaunchSettingsFixture> |
| 36 | + { |
| 37 | + private const string DbName = "TestDb"; |
| 38 | + private const string CollectionName = "MyEntities"; |
| 39 | + private readonly MongoClient _mongoClient; |
| 40 | + private readonly IRepository<MyBaseEntity> _repository; |
| 41 | + private readonly string _baseEntityId; |
| 42 | + private readonly string _derivedEntityId; |
| 43 | + |
| 44 | + public FindOneAndDeleteTests(LaunchSettingsFixture launchSettingsFixture) |
| 45 | + { |
| 46 | + _mongoClient = new MongoClient(Environment.GetEnvironmentVariable("MongoDbConnectionString")); |
| 47 | + |
| 48 | + MongoRepository.Configure() |
| 49 | + .Database(DbName, x => x |
| 50 | + .MapAlongWithSubclassesInSameAssebmly<MyBaseEntity>(CollectionName) |
| 51 | + ) |
| 52 | + .AutoEnlistWithTransactionScopes() |
| 53 | + .Build(); |
| 54 | + |
| 55 | + // Empty all collections in database |
| 56 | + foreach (var collectionName in _mongoClient.GetDatabase(DbName).ListCollectionNames().ToEnumerable()) |
| 57 | + { |
| 58 | + _mongoClient.GetDatabase(DbName).GetCollection<BsonDocument>(collectionName).DeleteMany(x => true); |
| 59 | + } |
| 60 | + |
| 61 | + // Add test documents to db |
| 62 | + var baseEntityDocument = new MyBaseEntity("Mary"); |
| 63 | + var derivedEntityDocument = new MyDerivedEntity("Joe", 45); |
| 64 | + |
| 65 | + _baseEntityId = baseEntityDocument.Id; |
| 66 | + _derivedEntityId = derivedEntityDocument.Id; |
| 67 | + |
| 68 | + _repository = _mongoClient.GetRepository<MyBaseEntity>(); |
| 69 | + _repository.InsertAsync(baseEntityDocument).Wait(); |
| 70 | + _repository.InsertAsync(derivedEntityDocument).Wait(); |
| 71 | + |
| 72 | + AssertNumberOfDocumentsInCollection(2).Wait(); |
| 73 | + AssertNumberOfDocumentsInTrash(0).Wait(); |
| 74 | + } |
| 75 | + |
| 76 | + private async Task AssertNumberOfDocumentsInCollection(int expected) |
| 77 | + { |
| 78 | + var documentsInCollection = await _mongoClient.GetDatabase(DbName).GetCollection<MyBaseEntity>(CollectionName).CountDocumentsAsync(x => true); |
| 79 | + documentsInCollection.Should().Be(expected); |
| 80 | + } |
| 81 | + |
| 82 | + private async Task AssertNumberOfDocumentsInTrash(int expected) |
| 83 | + { |
| 84 | + var docsInTrash = await _mongoClient.GetDatabase(DbName).GetCollection<BsonDocument>("DeletedObjects").CountDocumentsAsync(x => true); |
| 85 | + docsInTrash.Should().Be(expected); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public async Task BaseEntity_HardDeleted_ShouldBeRemovedFromCollection() |
| 90 | + { |
| 91 | + // By id |
| 92 | + var doc = await _repository.FindOneAndDeleteAsync(_baseEntityId); |
| 93 | + doc.Name.Should().Be("Mary"); |
| 94 | + await AssertNumberOfDocumentsInCollection(1); |
| 95 | + } |
| 96 | + |
| 97 | + [Fact] |
| 98 | + public async Task DerivedEntity_HardDeleted_ShouldBeRemovedFromCollection() |
| 99 | + { |
| 100 | + // By expression |
| 101 | + var doc = await _repository.FindOneAndDeleteAsync<MyDerivedEntity>(x => x.Age == 45); |
| 102 | + doc.Name.Should().Be("Joe"); |
| 103 | + await AssertNumberOfDocumentsInCollection(1); |
| 104 | + } |
| 105 | + |
| 106 | + [Fact] |
| 107 | + public async Task BaseEntity_SoftDeleted_ShouldBeRemovedFromCollectionAndAddedToTrash() |
| 108 | + { |
| 109 | + // By expression |
| 110 | + var doc = await _repository.FindOneAndDeleteAsync(x => x.Name == "Mary", softDelete: true); |
| 111 | + doc.Name.Should().Be("Mary"); |
| 112 | + await AssertNumberOfDocumentsInCollection(1); |
| 113 | + await AssertNumberOfDocumentsInTrash(1); |
| 114 | + } |
| 115 | + |
| 116 | + [Fact] |
| 117 | + public async Task DerivedEntity_SoftDeleted_ShouldBeRemovedFromCollectionAndAddedToTrash() |
| 118 | + { |
| 119 | + // By id |
| 120 | + var doc = await _repository.FindOneAndDeleteAsync(_derivedEntityId, softDelete: true); |
| 121 | + doc.Name.Should().Be("Joe"); |
| 122 | + await AssertNumberOfDocumentsInCollection(1); |
| 123 | + await AssertNumberOfDocumentsInTrash(1); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public async Task SoftDelete_WithAbortedTransactions_ShouldNotAddAnythingToTrash() |
| 128 | + { |
| 129 | + // By id |
| 130 | + using (var transaction = _repository.StartTransaction()) |
| 131 | + { |
| 132 | + var doc = await _repository.FindOneAndDeleteAsync(_baseEntityId, softDelete: true); |
| 133 | + doc.Name.Should().Be("Mary"); |
| 134 | + await transaction.AbortAsync(); |
| 135 | + } |
| 136 | + |
| 137 | + await AssertNumberOfDocumentsInCollection(2); |
| 138 | + await AssertNumberOfDocumentsInTrash(0); |
| 139 | + } |
| 140 | + |
| 141 | + [Fact] |
| 142 | + public async Task HardDeleted_WithAbortedTransactions_ShouldNotRemoveFromCollection() |
| 143 | + { |
| 144 | + // By expression |
| 145 | + using (var transaction = _repository.StartTransaction()) |
| 146 | + { |
| 147 | + var doc = await _repository.FindOneAndDeleteAsync(x => x.Id == _derivedEntityId); |
| 148 | + doc.Name.Should().Be("Joe"); |
| 149 | + } |
| 150 | + |
| 151 | + await AssertNumberOfDocumentsInCollection(2); |
| 152 | + await AssertNumberOfDocumentsInTrash(0); |
| 153 | + } |
| 154 | + |
| 155 | + [Fact] |
| 156 | + public async Task CanRestoreSoftdeletedBaseEntity() |
| 157 | + { |
| 158 | + await _repository.FindOneAndDeleteAsync(_baseEntityId, softDelete: true); |
| 159 | + |
| 160 | + await AssertNumberOfDocumentsInCollection(1); |
| 161 | + await AssertNumberOfDocumentsInTrash(1); |
| 162 | + |
| 163 | + await _repository.RestoreSoftDeletedAsync(_baseEntityId); |
| 164 | + |
| 165 | + await AssertNumberOfDocumentsInCollection(2); |
| 166 | + await AssertNumberOfDocumentsInTrash(0); |
| 167 | + } |
| 168 | + |
| 169 | + [Fact] |
| 170 | + public async Task CanRestoreSoftdeletedDerivedEntity() |
| 171 | + { |
| 172 | + await _repository.FindOneAndDeleteAsync<MyDerivedEntity>(x => x.Age == 45, softDelete: true); |
| 173 | + |
| 174 | + await AssertNumberOfDocumentsInCollection(1); |
| 175 | + await AssertNumberOfDocumentsInTrash(1); |
| 176 | + |
| 177 | + await _repository.RestoreSoftDeletedAsync<MyDerivedEntity>(x => x.Entity.Age == 45); |
| 178 | + |
| 179 | + await AssertNumberOfDocumentsInCollection(2); |
| 180 | + await AssertNumberOfDocumentsInTrash(0); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments