From 54e756c695dd49fa8d33800dc2d6509e0eb34b80 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Thu, 15 Jun 2023 21:56:47 +0100 Subject: [PATCH] unit tests --- .../AddTests/AddManyTests.cs | 33 +++ .../AddTests/AddOneTests.cs | 28 +++ .../IndexTests/BaseIndexTests.cs | 34 +++ .../IndexTests/CreateAscendingIndexTests.cs | 29 +++ .../IndexTests/CreateTextIndexTests.cs | 195 ++++++++++++++++++ .../IndexTests/GetIndexNamesTests.cs | 175 ++++++++++++++++ .../MainTests/AnyTests.cs | 34 +++ .../MainTests/CountTests.cs | 34 +++ CoreUnitTests/CoreUnitTests.csproj | 7 +- CoreUnitTests/Infrastructure/Model/Child.cs | 13 ++ CoreUnitTests/Infrastructure/Model/Nested.cs | 13 ++ .../Infrastructure/Model/TestDocument.cs | 32 +++ .../Model/TestDocumentWithKey.cs | 33 +++ CoreUnitTests/Infrastructure/TestDocument.cs | 6 - .../TestKeyedMongoRepository.cs | 26 ++- .../TestKeyedMongoRepositoryContext.cs | 53 +++++ .../Infrastructure/TestMongoRepository.cs | 29 +++ .../TestMongoRepositoryContext.cs | 51 +++++ 18 files changed, 817 insertions(+), 8 deletions(-) create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/IndexTests/BaseIndexTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs create mode 100644 CoreUnitTests/Infrastructure/Model/Child.cs create mode 100644 CoreUnitTests/Infrastructure/Model/Nested.cs create mode 100644 CoreUnitTests/Infrastructure/Model/TestDocument.cs create mode 100644 CoreUnitTests/Infrastructure/Model/TestDocumentWithKey.cs delete mode 100644 CoreUnitTests/Infrastructure/TestDocument.cs create mode 100644 CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs create mode 100644 CoreUnitTests/Infrastructure/TestMongoRepository.cs create mode 100644 CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs diff --git a/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs new file mode 100644 index 0000000..292e451 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Create; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests; + +public class AddManyTests : TestMongoRepositoryContext +{ + + [Fact] + public async Task AddManyAsync_EnsureTokenPassed() + { + // Arrange + Creator = new Mock(); + var token = new CancellationToken(); + var documents = new List + { + new(), new(), new() + }; + + // Act + await Sut.AddManyAsync(documents, token); + + // Assert + Creator.Verify(x => x.AddManyAsync(documents, token)); + } +} \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs new file mode 100644 index 0000000..dc290da --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs @@ -0,0 +1,28 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Create; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests; + +public class AddOneTests : TestMongoRepositoryContext +{ + [Fact] + public async Task AddOneAsync_EnsureTokenPassed() + { + // Arrange + Creator = new Mock(); + var token = new CancellationToken(); + var document = new TestDocument(); + + // Act + await Sut.AddOneAsync(document, token); + + // Assert + Creator.Verify(x => x.AddOneAsync(document, token)); + } +} \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/BaseIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/BaseIndexTests.cs new file mode 100644 index 0000000..c2276ec --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/BaseIndexTests.cs @@ -0,0 +1,34 @@ +using System.Threading; +using CoreUnitTests.Infrastructure; +using MongoDB.Bson; +using MongoDB.Driver; +using Moq; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class BaseIndexTests : TestMongoRepositoryContext +{ + protected Mock> SetupIndex(BsonDocument index, Mock> collection) + { + var asyncCursor = new Mock>(); + asyncCursor + .SetupSequence(x => x.MoveNextAsync(It.IsAny())) + .ReturnsAsync(true) + .ReturnsAsync(false); + + asyncCursor + .SetupGet(x => x.Current) + .Returns(new[] {index}); + + var indexManager = new Mock>(); + indexManager + .Setup(x => x.ListAsync(It.IsAny())) + .ReturnsAsync(asyncCursor.Object); + + collection + .SetupGet(x => x.Indexes) + .Returns(indexManager.Object); + + return asyncCursor; + } +} \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs new file mode 100644 index 0000000..53a63d2 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs @@ -0,0 +1,29 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class CreateAscendingIndexTests : BaseIndexTests +{ + [Fact] + public async Task CreateAscendingIndexAsync_EnsureTokenPassed() + { + // Arrange + IndexHandler = new Mock(); + var token = new CancellationToken(); + + // Act + Expression> fieldExpression = t => t.SomeContent2; + await Sut.CreateAscendingIndexAsync(fieldExpression, token); + + // Assert + IndexHandler.Verify(x => x.CreateAscendingIndexAsync( + fieldExpression, null, null, token)); + } +} \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs new file mode 100644 index 0000000..8a50381 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs @@ -0,0 +1,195 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using CoreUnitTests.Infrastructure.Model; +using MongoDB.Bson; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class CreateTextIndexTests : BaseIndexTests +{ + private readonly Expression> _fieldExpression = t => t.SomeContent2; + + [Fact] + public async Task Ensure_Creates_Index() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(_fieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync(_fieldExpression, null, null)); + } + + [Fact] + public async Task Ensure_Passes_Options() + { + // Arrange + IndexHandler = new Mock(); + var options = new IndexCreationOptions { Name = "theIndexName" }; + + // Act + await Sut.CreateTextIndexAsync(_fieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + _fieldExpression, options, null)); + } + + [Fact] + public async Task Ensure_Passes_PartitionKey() + { + // Arrange + const string partitionKey = "thePartitionKey"; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(_fieldExpression, partitionKey: partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + _fieldExpression, null, partitionKey)); + } + + [Fact] + public async Task Ensure_Creates_Index_With_CancellationToken() + { + // Arrange + IndexHandler = new Mock(); + var token = new CancellationToken(); + + // Act + await Sut.CreateTextIndexAsync(_fieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync(_fieldExpression, null, null, token)); + } + + [Fact] + public async Task Ensure_Passes_Options_With_CancellationToken() + { + // Arrange + IndexHandler = new Mock(); + var token = new CancellationToken(); + var options = new IndexCreationOptions { Name = "theIndexName" }; + + // Act + await Sut.CreateTextIndexAsync(_fieldExpression, token, options); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + _fieldExpression, options, null, token)); + } + + [Fact] + public async Task Ensure_Passes_PartitionKey_With_CancellationToken() + { + // Arrange + const string partitionKey = "thePartitionKey"; + var token = new CancellationToken(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(_fieldExpression, token, partitionKey: partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + _fieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task Ensure_Creates_Index_Custom_Key() + { + // Arrange + IndexHandler = new Mock(); + Expression> fieldExpression = t => t.SomeContent2; + + // Act + await Sut.CreateTextIndexAsync(fieldExpression); + + // Assert + IndexHandler.Verify(x => x.CreateTextIndexAsync(fieldExpression, null, null, default)); + } + + [Fact] + public async Task Ensure_Passes_CancellationToken_Custom_Key() + { + // Arrange + IndexHandler = new Mock(); + var token = new CancellationToken(); + + // Act + await Sut.CreateTextIndexAsync(t => t.SomeContent2, token); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + t => t.SomeContent2, null, null, token)); + } + + [Fact] + public async Task Ensure_Passes_Options_Custom_Key() + { + // Arrange + IndexHandler = new Mock(); + var options = new IndexCreationOptions { Name = "theIndexName" }; + + // Act + await Sut.CreateTextIndexAsync(t => t.SomeContent2, options); + + // Assert + IndexHandler.Verify(x => x.CreateTextIndexAsync(t => t.SomeContent2, options, null, default)); + } + + [Fact] + public async Task Ensure_Passes_PartitionKey_Custom_Key() + { + // Arrange + const string partitionKey = "thePartitionKey"; + + IndexHandler = new Mock(); + var options = new IndexCreationOptions { Name = "theIndexName" }; + + // Act + await Sut.CreateTextIndexAsync(t => t.SomeContent2, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + t => t.SomeContent2, options, partitionKey, default)); + } + + [Fact] + public async Task Ensure_Passes_PartitionKey_And_CancellationToken_Custom_Key() + { + // Arrange + const string partitionKey = "thePartitionKey"; + const string indexName = "theIndexName"; + var token = new CancellationToken(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(t => t.SomeContent2, token, options, partitionKey); + + // Assert + IndexHandler + .Verify(x => x.CreateTextIndexAsync( + t => t.SomeContent2, options, partitionKey, token)); + } +} \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs new file mode 100644 index 0000000..42369b7 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs @@ -0,0 +1,175 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using CoreUnitTests.Infrastructure.Model; +using MongoDB.Bson; +using MongoDbGenericRepository.DataAccess.Index; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class GetIndexNamesTests : BaseIndexTests +{ + [Fact] + public async Task Ensure_Returns_IndexNames() + { + // Arrange + IndexHandler = new Mock(); + const string indexName = "theIndexName"; + + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(null)) + .ReturnsAsync(new List{indexName}); + + // Act + var result = await Sut.GetIndexesNamesAsync(); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + IndexHandler.Verify(x => x.GetIndexesNamesAsync(null), Times.Once()); + } + + [Fact] + public async Task Ensure_Passes_Provided_CancellationToken() + { + // Arrange + const string indexName = "theIndexName"; + var token = new CancellationToken(); + IndexHandler = new Mock(); + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(null, token)) + .ReturnsAsync(new List{indexName}); + + // Act + var result = await Sut.GetIndexesNamesAsync(token); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + } + + [Fact] + public async Task Ensure_Handles_PartitionKey() + { + // Arrange + const string partitionKey = "thePartitionKey"; + const string indexName = "theIndexName"; + + IndexHandler = new Mock(); + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(partitionKey)) + .ReturnsAsync(new List{indexName}); + + // Act + var result = await Sut.GetIndexesNamesAsync(partitionKey); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + } + + [Fact] + public async Task Ensure_Passes_Provided_CancellationToken_And_Handles_Partition_Key() + { + // Arrange + const string partitionKey = "thePartitionKey"; + const string indexName = "theIndexName"; + var token = new CancellationToken(); + + IndexHandler = new Mock(); + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(partitionKey, token)) + .ReturnsAsync(new List{indexName}); + + // Act + var result = await Sut.GetIndexesNamesAsync(token, partitionKey); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + } + + [Fact] + public async Task Ensure_Returns_IndexNames_Custom_Primary_Key() + { + // Arrange + const string indexName = "theIndexName"; + + IndexHandler = new Mock(); + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(null)) + .ReturnsAsync(new List{indexName}); + + // Act + var result = await Sut.GetIndexesNamesAsync(); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + } + + [Fact] + public async Task Ensure_Passes_Provided_CancellationToken_Custom_Primary_Key() + { + // Arrange + const string indexName = "theIndexName"; + var token = new CancellationToken(); + IndexHandler = new Mock(); + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(null, token)) + .ReturnsAsync(new List{indexName}); + + + // Act + var result = await Sut.GetIndexesNamesAsync(token); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + } + + [Fact] + public async Task Ensure_Handles_PartitionKey_Custom_Primary_Key() + { + // Arrange + const string indexName = "theIndexName"; + const string partitionKey = "thePartitionKey"; + + IndexHandler = new Mock(); + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(partitionKey)) + .ReturnsAsync(new List{indexName}); + + + // Act + var result = await Sut.GetIndexesNamesAsync(partitionKey); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + } + + [Fact] + public async Task Ensure_Passes_Provided_CancellationToken_And_Handles_Partition_Key_Custom_Primary_Key() + { + // Arrange + const string indexName = "theIndexName"; + const string partitionKey = "thePartitionKey"; + var token = new CancellationToken(); + + IndexHandler = new Mock(); + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(partitionKey, token)) + .ReturnsAsync(new List{indexName}); + + // Act + var result = await Sut.GetIndexesNamesAsync(token, partitionKey); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + } +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs new file mode 100644 index 0000000..840ebe9 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs @@ -0,0 +1,34 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.MainTests; + +public class AnyTests : TestMongoRepositoryContext +{ + [Fact] + public async Task AnyAsync_EnsureTokenPassed() + { + // Arrange + var token = new CancellationToken(); + + Reader = new Mock(); + Reader + .Setup(x => x.AnyAsync(It.IsAny>(), null, null, token)) + .ReturnsAsync(true); + + // Act + await Sut.AnyAsync(t => string.IsNullOrWhiteSpace(t.SomeContent2), token); + + // Assert + Reader + .Verify(x => x.AnyAsync( + t => string.IsNullOrWhiteSpace(t.SomeContent2), null, token)); + } +} \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs new file mode 100644 index 0000000..f1f4965 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs @@ -0,0 +1,34 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.MainTests; + +public class CountTests : TestMongoRepositoryContext +{ + [Fact] + public async Task CountAsync_EnsureTokenPassed() + { + // Arrange + var token = new CancellationToken(); + + Reader = new Mock(); + Reader + .Setup(x => x.CountAsync(It.IsAny>>(), null, token)) + .ReturnsAsync(10); + + // Act + var result = await Sut.CountAsync(t => string.IsNullOrWhiteSpace(t.SomeContent2), token); + + // Assert + Assert.Equal(10, result); + Reader.Verify(x => x.CountAsync( + t => string.IsNullOrWhiteSpace(t.SomeContent2), null, token)); + } +} \ No newline at end of file diff --git a/CoreUnitTests/CoreUnitTests.csproj b/CoreUnitTests/CoreUnitTests.csproj index ea5f539..479c330 100644 --- a/CoreUnitTests/CoreUnitTests.csproj +++ b/CoreUnitTests/CoreUnitTests.csproj @@ -2,13 +2,14 @@ net6.0 - enable + warnings false + runtime; build; native; contentfiles; analyzers; buildtransitive @@ -20,4 +21,8 @@ + + + + diff --git a/CoreUnitTests/Infrastructure/Model/Child.cs b/CoreUnitTests/Infrastructure/Model/Child.cs new file mode 100644 index 0000000..0d95214 --- /dev/null +++ b/CoreUnitTests/Infrastructure/Model/Child.cs @@ -0,0 +1,13 @@ +namespace CoreUnitTests.Infrastructure.Model; + +public class Child +{ + public Child(string type, string value) + { + Type = type; + Value = value; + } + + public string Type { get; set; } + public string Value { get; set; } +} \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/Model/Nested.cs b/CoreUnitTests/Infrastructure/Model/Nested.cs new file mode 100644 index 0000000..5908c80 --- /dev/null +++ b/CoreUnitTests/Infrastructure/Model/Nested.cs @@ -0,0 +1,13 @@ +using System; +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; + +namespace CoreUnitTests.Infrastructure.Model; + +public class Nested +{ + public DateTime SomeDate { get; set; } + + [BsonRepresentation(BsonType.Decimal128)] + public decimal SomeAmount { get; set; } +} \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/Model/TestDocument.cs b/CoreUnitTests/Infrastructure/Model/TestDocument.cs new file mode 100644 index 0000000..51f87f7 --- /dev/null +++ b/CoreUnitTests/Infrastructure/Model/TestDocument.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using MongoDbGenericRepository.Models; + +namespace CoreUnitTests.Infrastructure.Model; + + +public class TestDocument : Document +{ + public TestDocument() + { + Version = 2; + Nested = new Nested + { + SomeDate = DateTime.UtcNow + }; + Children = new List(); + } + + public int SomeValue { get; set; } + + public string SomeContent { get; set; } + public string SomeContent2 { get; set; } + public string SomeContent3 { get; set; } + + public int GroupingKey { get; set; } + + public Nested Nested { get; set; } + + public List Children { get; set; } +} + diff --git a/CoreUnitTests/Infrastructure/Model/TestDocumentWithKey.cs b/CoreUnitTests/Infrastructure/Model/TestDocumentWithKey.cs new file mode 100644 index 0000000..2ff425d --- /dev/null +++ b/CoreUnitTests/Infrastructure/Model/TestDocumentWithKey.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using MongoDbGenericRepository.Models; + +namespace CoreUnitTests.Infrastructure.Model; + +public class TestDocumentWithKey : IDocument +{ + public int Id { get; set; } + public int Version { get; set; } + + public TestDocumentWithKey() + { + Version = 2; + Nested = new Nested + { + SomeDate = DateTime.UtcNow + }; + Children = new List(); + } + + public int SomeValue { get; set; } + + public string SomeContent { get; set; } + public string SomeContent2 { get; set; } + public string SomeContent3 { get; set; } + + public int GroupingKey { get; set; } + + public Nested Nested { get; set; } + + public List Children { get; set; } +} \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/TestDocument.cs b/CoreUnitTests/Infrastructure/TestDocument.cs deleted file mode 100644 index ca3f13f..0000000 --- a/CoreUnitTests/Infrastructure/TestDocument.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace CoreUnitTests.Infrastructure; - -public class TestDocument -{ - -} \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs index 569667f..16fbff0 100644 --- a/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs +++ b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs @@ -1,6 +1,30 @@ +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.DataAccess.Read; + namespace CoreUnitTests.Infrastructure; -public class TestKeyedMongoRepository : KeyedMongoRepository +public class TestKeyedMongoRepository : BaseMongoRepository { + public TestKeyedMongoRepository(IMongoDatabase mongoDatabase) + : base(mongoDatabase) + { + } + public void SetIndexHandler(IMongoDbIndexHandler indexHandler) + { + MongoDbIndexHandler = indexHandler; + } + + public void SetDbCreator(IMongoDbCreator creator) + { + MongoDbCreator = creator; + } + + public void SetReader(IMongoDbReader reader) + { + MongoDbReader = reader; + } } \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs b/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs new file mode 100644 index 0000000..8bb92f2 --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs @@ -0,0 +1,53 @@ +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; + +namespace CoreUnitTests.Infrastructure; + +public class TestKeyedMongoRepositoryContext +{ + private readonly Mock _mongoDatabase; + + private TestKeyedMongoRepository _sut; + + protected TestKeyedMongoRepositoryContext() + { + _mongoDatabase = new Mock(); + } + + protected TestKeyedMongoRepository Sut + { + get + { + if (_sut != null) + { + return _sut; + } + + _sut = new TestKeyedMongoRepository(_mongoDatabase.Object); + if (IndexHandler != null) + { + _sut.SetIndexHandler(IndexHandler.Object); + } + + if (Creator != null) + { + _sut.SetDbCreator(Creator.Object); + } + + if (Reader != null) + { + _sut.SetReader(Reader.Object); + } + + return _sut; + } + } + + protected Mock IndexHandler { get; set; } + protected Mock Creator { get; set; } + + protected Mock Reader { get; set; } +} \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/TestMongoRepository.cs b/CoreUnitTests/Infrastructure/TestMongoRepository.cs new file mode 100644 index 0000000..bf16a4c --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestMongoRepository.cs @@ -0,0 +1,29 @@ +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.DataAccess.Read; + +namespace CoreUnitTests.Infrastructure; + +public class TestMongoRepository : BaseMongoRepository +{ + public TestMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase) + { + } + + public void SetIndexHandler(IMongoDbIndexHandler indexHandler) + { + MongoDbIndexHandler = indexHandler; + } + + public void SetDbCreator(IMongoDbCreator creator) + { + MongoDbCreator = creator; + } + + public void SetReader(IMongoDbReader reader) + { + MongoDbReader = reader; + } +} \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs b/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs new file mode 100644 index 0000000..465f06b --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs @@ -0,0 +1,51 @@ +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; + +namespace CoreUnitTests.Infrastructure; + +public class TestMongoRepositoryContext +{ + private readonly Mock _mongoDatabase; + + private TestMongoRepository _sut; + + protected TestMongoRepositoryContext() + { + _mongoDatabase = new Mock(); + } + + protected TestMongoRepository Sut + { + get + { + if (_sut == null) + { + _sut = new TestMongoRepository(_mongoDatabase.Object); + if (IndexHandler != null) + { + _sut.SetIndexHandler(IndexHandler.Object); + } + + if (Creator != null) + { + _sut.SetDbCreator(Creator.Object); + } + + if (Reader != null) + { + _sut.SetReader(Reader.Object); + } + } + + return _sut; + } + } + + protected Mock IndexHandler { get; set; } + protected Mock Creator { get; set; } + + protected Mock Reader { get; set; } +} \ No newline at end of file