diff --git a/.vs/MongoDbGenericRepository/v14/.suo b/.vs/MongoDbGenericRepository/v14/.suo deleted file mode 100644 index 424bfa8..0000000 Binary files a/.vs/MongoDbGenericRepository/v14/.suo and /dev/null differ diff --git a/.vs/config/applicationhost.config b/.vs/config/applicationhost.config deleted file mode 100644 index 173fad6..0000000 --- a/.vs/config/applicationhost.config +++ /dev/null @@ -1,1038 +0,0 @@ - - - - - - - - -
-
-
-
-
-
-
-
- - - -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
- -
-
-
-
-
-
- -
-
-
-
-
- -
-
-
- -
-
- -
-
- -
-
-
- - -
-
-
-
-
-
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/CoreIntegrationTests/CoreIntegrationTests.csproj b/CoreIntegrationTests/CoreIntegrationTests.csproj index 493a082..dbb6dac 100644 --- a/CoreIntegrationTests/CoreIntegrationTests.csproj +++ b/CoreIntegrationTests/CoreIntegrationTests.csproj @@ -1,26 +1,23 @@  - netcoreapp2.1 + net6.0 - - - + + + all runtime; build; native; contentfiles; analyzers - - - - - - - ..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Configuration.dll - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/CoreIntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs b/CoreIntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs index 22b5423..7d00f01 100644 --- a/CoreIntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs +++ b/CoreIntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs @@ -62,18 +62,18 @@ namespace CoreIntegrationTests.Infrastructure } #region IDisposable Support - private bool disposedValue = false; // Pour détecter les appels redondants + private bool _disposedValue; // Pour détecter les appels redondants protected virtual void Dispose(bool disposing) { - if (!disposedValue) + if (!_disposedValue) { if (disposing) { Cleanup(); } - disposedValue = true; + _disposedValue = true; } } diff --git a/CoreIntegrationTests/Infrastructure/MongoDbConfig.cs b/CoreIntegrationTests/Infrastructure/MongoDbConfig.cs index eb87022..c41302a 100644 --- a/CoreIntegrationTests/Infrastructure/MongoDbConfig.cs +++ b/CoreIntegrationTests/Infrastructure/MongoDbConfig.cs @@ -5,8 +5,8 @@ namespace CoreIntegrationTests.Infrastructure { internal static class MongoDbConfig { - private static bool _initialized = false; - private static object _initializationLock = new object(); + private static bool _initialized; + private static object _initializationLock = new(); private static object _initializationTarget; public static void EnsureConfigured() diff --git a/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.Main.cs b/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.Main.cs index 98fc846..893908c 100644 --- a/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.Main.cs +++ b/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.Main.cs @@ -1,6 +1,4 @@ -using MongoDbGenericRepository; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -8,12 +6,12 @@ using System.Linq.Expressions; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; +using MongoDbGenericRepository.Models; using Xunit; namespace CoreIntegrationTests.Infrastructure { - public abstract partial class MongoDbDocumentTestBase : - IClassFixture> + public abstract partial class MongoDbDocumentTestBase where T : TestDoc, new() { @@ -74,7 +72,7 @@ namespace CoreIntegrationTests.Infrastructure // Arrange var document = CreateTestDocument(); // Act - SUT.AddOne(document); + SUT.AddOne(document); // Assert long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count(e => e.Id.Equals(document.Id)) : SUT.Count(e => e.Id.Equals(document.Id), PartitionKey); @@ -87,7 +85,7 @@ namespace CoreIntegrationTests.Infrastructure // Arrange var document = CreateTestDocument(); // Act - await SUT.AddOneAsync(document); + await SUT.AddOneAsync(document); // Assert long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count(e => e.Id.Equals(document.Id)) : SUT.Count(e => e.Id.Equals(document.Id), PartitionKey); @@ -100,7 +98,7 @@ namespace CoreIntegrationTests.Infrastructure // Arrange var documents = CreateTestDocuments(2); // Act - SUT.AddMany(documents); + SUT.AddMany(documents); // Assert long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count(e => e.Id.Equals(documents[0].Id) || e.Id.Equals(documents[1].Id)) @@ -123,7 +121,7 @@ namespace CoreIntegrationTests.Infrastructure ((IPartitionedDocument)documents[2]).PartitionKey = secondPartitionKey; ((IPartitionedDocument)documents[3]).PartitionKey = secondPartitionKey; // Act - SUT.AddMany(documents); + SUT.AddMany(documents); // Assert long count = SUT.Count(e => e.Id.Equals(documents[0].Id) || e.Id.Equals(documents[1].Id), PartitionKey); long secondPartitionCount = SUT.Count(e => e.Id.Equals(documents[2].Id) || e.Id.Equals(documents[3].Id), secondPartitionKey); @@ -141,7 +139,7 @@ namespace CoreIntegrationTests.Infrastructure // Arrange var documents = CreateTestDocuments(2); // Act - await SUT.AddManyAsync(documents); + await SUT.AddManyAsync(documents); // Assert long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count(e => e.Id.Equals(documents[0].Id) || e.Id.Equals(documents[1].Id)) @@ -164,7 +162,7 @@ namespace CoreIntegrationTests.Infrastructure ((IPartitionedDocument)documents[2]).PartitionKey = secondPartitionKey; ((IPartitionedDocument)documents[3]).PartitionKey = secondPartitionKey; // Act - await SUT.AddManyAsync(documents); + await SUT.AddManyAsync(documents); // Assert long count = SUT.Count(e => e.Id.Equals(documents[0].Id) || e.Id.Equals(documents[1].Id), PartitionKey); long secondPartitionCount = SUT.Count(e => e.Id.Equals(documents[2].Id) || e.Id.Equals(documents[3].Id), secondPartitionKey); @@ -185,7 +183,7 @@ namespace CoreIntegrationTests.Infrastructure { // Arrange var document = CreateTestDocument(); - SUT.AddOne(document); + await SUT.AddOneAsync(document); // Act var result = await SUT.GetByIdAsync(document.Id, PartitionKey); // Assert @@ -197,7 +195,7 @@ namespace CoreIntegrationTests.Infrastructure { // Arrange var document = CreateTestDocument(); - SUT.AddOne(document); + SUT.AddOne(document); // Act var result = SUT.GetById(document.Id, PartitionKey); // Assert @@ -209,7 +207,7 @@ namespace CoreIntegrationTests.Infrastructure { // Arrange var document = CreateTestDocument(); - SUT.AddOne(document); + SUT.AddOne(document); // Act var result = await SUT.GetOneAsync(x => x.Id.Equals(document.Id), PartitionKey); // Assert @@ -221,7 +219,7 @@ namespace CoreIntegrationTests.Infrastructure { // Arrange var document = CreateTestDocument(); - SUT.AddOne(document); + SUT.AddOne(document); // Act var result = SUT.GetOne(x => x.Id.Equals(document.Id), PartitionKey); // Assert @@ -233,7 +231,7 @@ namespace CoreIntegrationTests.Infrastructure { // Arrange var document = CreateTestDocument(); - SUT.AddOne(document); + SUT.AddOne(document); // Act var cursor = SUT.GetCursor(x => x.Id.Equals(document.Id), PartitionKey); var count = cursor.CountDocuments(); @@ -246,7 +244,7 @@ namespace CoreIntegrationTests.Infrastructure { // Arrange var document = CreateTestDocument(); - SUT.AddOne(document); + SUT.AddOne(document); // Act var result = await SUT.AnyAsync(x => x.Id.Equals(document.Id), PartitionKey); // Assert @@ -258,7 +256,7 @@ namespace CoreIntegrationTests.Infrastructure { // Arrange var document = CreateTestDocument(); - SUT.AddOne(document); + SUT.AddOne(document); // Act var result = await SUT.AnyAsync(x => x.Id.Equals(Guid.NewGuid()), PartitionKey); // Assert @@ -270,7 +268,7 @@ namespace CoreIntegrationTests.Infrastructure { // Arrange var document = CreateTestDocument(); - SUT.AddOne(document); + SUT.AddOne(document); // Act var result = SUT.Any(x => x.Id.Equals(document.Id), PartitionKey); // Assert @@ -282,7 +280,7 @@ namespace CoreIntegrationTests.Infrastructure { // Arrange var document = CreateTestDocument(); - SUT.AddOne(document); + SUT.AddOne(document); // Act var result = SUT.Any(x => x.Id.Equals(Guid.NewGuid()), PartitionKey); // Assert @@ -296,7 +294,7 @@ namespace CoreIntegrationTests.Infrastructure var documents = CreateTestDocuments(5); var content = GetContent(); documents.ForEach(e => e.SomeContent = content); - SUT.AddMany(documents); + SUT.AddMany(documents); // Act var result = await SUT.GetAllAsync(x => x.SomeContent == content, PartitionKey); // Assert @@ -310,7 +308,7 @@ namespace CoreIntegrationTests.Infrastructure var documents = CreateTestDocuments(5); var content = GetContent(); documents.ForEach(e => e.SomeContent = content); - SUT.AddMany(documents); + SUT.AddMany(documents); // Act var result = SUT.GetAll(x => x.SomeContent == content, PartitionKey); // Assert @@ -324,7 +322,7 @@ namespace CoreIntegrationTests.Infrastructure var documents = CreateTestDocuments(5); var content = GetContent(); documents.ForEach(e => e.SomeContent = content); - SUT.AddMany(documents); + SUT.AddMany(documents); // Act var result = await SUT.CountAsync(x => x.SomeContent == content, PartitionKey); // Assert @@ -338,7 +336,7 @@ namespace CoreIntegrationTests.Infrastructure var documents = CreateTestDocuments(5); var content = GetContent(); documents.ForEach(e => e.SomeContent = content); - SUT.AddMany(documents); + SUT.AddMany(documents); // Act var result = SUT.Count(x => x.SomeContent == content, PartitionKey); // Assert @@ -354,9 +352,9 @@ namespace CoreIntegrationTests.Infrastructure { // Arrange var document = CreateTestDocument(); - SUT.AddOne(document); + SUT.AddOne(document); // Act - var result = SUT.DeleteOne(document); + var result = SUT.DeleteOne(document); // Assert Assert.True(1 == result); Assert.False(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); @@ -367,7 +365,7 @@ namespace CoreIntegrationTests.Infrastructure { // Arrange var document = CreateTestDocument(); - SUT.AddOne(document); + SUT.AddOne(document); // Act var result = SUT.DeleteOne(e => e.Id.Equals(document.Id), PartitionKey); // Assert @@ -380,9 +378,9 @@ namespace CoreIntegrationTests.Infrastructure { // Arrange var document = CreateTestDocument(); - SUT.AddOne(document); + SUT.AddOne(document); // Act - var result = await SUT.DeleteOneAsync(document); + var result = await SUT.DeleteOneAsync(document); // Assert Assert.True(1 == result); Assert.False(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); @@ -393,7 +391,7 @@ namespace CoreIntegrationTests.Infrastructure { // Arrange var document = CreateTestDocument(); - SUT.AddOne(document); + SUT.AddOne(document); // Act var result = await SUT.DeleteOneAsync(e => e.Id.Equals(document.Id), PartitionKey); // Assert @@ -408,7 +406,7 @@ namespace CoreIntegrationTests.Infrastructure var criteria = $"{GetTestName()}.{DocumentTypeName}"; var documents = CreateTestDocuments(5); documents.ForEach(e => e.SomeContent = criteria); - SUT.AddMany(documents); + SUT.AddMany(documents); // Act var result = await SUT.DeleteManyAsync(e => e.SomeContent == criteria, PartitionKey); // Assert @@ -432,9 +430,9 @@ namespace CoreIntegrationTests.Infrastructure ((IPartitionedDocument)documents[4]).PartitionKey = secondKey; } - SUT.AddMany(documents); + SUT.AddMany(documents); // Act - var result = await SUT.DeleteManyAsync(documents); + var result = await SUT.DeleteManyAsync(documents); // Assert Assert.True(5 == result); Assert.False(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName()); @@ -451,7 +449,7 @@ namespace CoreIntegrationTests.Infrastructure var criteria = $"{GetTestName()}.{DocumentTypeName}"; var documents = CreateTestDocuments(5); documents.ForEach(e => e.SomeContent = criteria); - SUT.AddMany(documents); + SUT.AddMany(documents); // Act var result = SUT.DeleteMany(e => e.SomeContent == criteria, PartitionKey); // Assert @@ -475,9 +473,9 @@ namespace CoreIntegrationTests.Infrastructure ((IPartitionedDocument)documents[4]).PartitionKey = secondKey; } - SUT.AddMany(documents); + SUT.AddMany(documents); // Act - var result = SUT.DeleteMany(documents); + var result = SUT.DeleteMany(documents); // Assert Assert.True(5 == result); Assert.False(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName()); @@ -500,7 +498,7 @@ namespace CoreIntegrationTests.Infrastructure var document = CreateTestDocument(); document.SomeContent = someContent; document.Nested.SomeDate = someDate; - SUT.AddOne(document); + SUT.AddOne(document); // Act var result = await SUT.ProjectOneAsync( x => x.Id.Equals(document.Id), @@ -526,7 +524,7 @@ namespace CoreIntegrationTests.Infrastructure var document = CreateTestDocument(); document.SomeContent = someContent; document.Nested.SomeDate = someDate; - SUT.AddOne(document); + SUT.AddOne(document); // Act var result = SUT.ProjectOne( x => x.Id.Equals(document.Id), @@ -556,7 +554,7 @@ namespace CoreIntegrationTests.Infrastructure e.Nested.SomeDate = someDate; }); - SUT.AddMany(documents); + SUT.AddMany(documents); // Act var result = await SUT.ProjectManyAsync( x => x.SomeContent == someContent, @@ -586,7 +584,7 @@ namespace CoreIntegrationTests.Infrastructure e.Nested.SomeDate = someDate; }); - SUT.AddMany(documents); + SUT.AddMany(documents); // Act var result = SUT.ProjectMany( x => x.SomeContent == someContent, @@ -619,7 +617,7 @@ namespace CoreIntegrationTests.Infrastructure e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++); e.SomeContent = criteria; }); - SUT.AddMany(documents); + SUT.AddMany(documents); var expectedMax = documents.OrderByDescending(e => e.Nested.SomeDate).First(); // Act @@ -642,7 +640,7 @@ namespace CoreIntegrationTests.Infrastructure e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++); e.SomeContent = criteria; }); - SUT.AddMany(documents); + SUT.AddMany(documents); var expectedMax = documents.OrderByDescending(e => e.Nested.SomeDate).First(); // Act @@ -665,7 +663,7 @@ namespace CoreIntegrationTests.Infrastructure e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++); e.SomeContent = criteria; }); - SUT.AddMany(documents); + SUT.AddMany(documents); var expectedMin = documents.OrderBy(e => e.Nested.SomeDate).First(); // Act @@ -688,7 +686,7 @@ namespace CoreIntegrationTests.Infrastructure e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++); e.SomeContent = criteria; }); - SUT.AddMany(documents); + SUT.AddMany(documents); var expectedMin = documents.OrderBy(e => e.Nested.SomeDate).First(); // Act @@ -711,7 +709,7 @@ namespace CoreIntegrationTests.Infrastructure e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++); e.SomeContent = criteria; }); - SUT.AddMany(documents); + SUT.AddMany(documents); var expectedMin = documents.OrderBy(e => e.Nested.SomeDate).First(); // Act @@ -734,7 +732,7 @@ namespace CoreIntegrationTests.Infrastructure e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++); e.SomeContent = criteria; }); - SUT.AddMany(documents); + SUT.AddMany(documents); var expectedMin = documents.OrderBy(e => e.Nested.SomeDate).First(); // Act @@ -757,7 +755,7 @@ namespace CoreIntegrationTests.Infrastructure e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++); e.SomeContent = criteria; }); - SUT.AddMany(documents); + SUT.AddMany(documents); var expectedMax = documents.OrderByDescending(e => e.Nested.SomeDate).First(); // Act @@ -780,7 +778,7 @@ namespace CoreIntegrationTests.Infrastructure e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++); e.SomeContent = criteria; }); - SUT.AddMany(documents); + SUT.AddMany(documents); var expectedMin = documents.OrderByDescending(e => e.Nested.SomeDate).First(); // Act @@ -910,7 +908,7 @@ namespace CoreIntegrationTests.Infrastructure // Act Expression > ex = x => x.SomeContent2; Expression > ex2 = x => x.SomeContent3; - var result = await SUT.CreateCombinedTextIndexAsync(new[] { ex, ex2 }, null, PartitionKey); + var result = await SUT.CreateCombinedTextIndexAsync(new[] { ex, ex2 }, null, PartitionKey); // Assert var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey); @@ -937,7 +935,7 @@ namespace CoreIntegrationTests.Infrastructure e.Nested.SomeAmount = 5m; e.SomeContent = criteria; }); - SUT.AddMany(documents); + SUT.AddMany(documents); var expectedSum = documents.Sum(e => e.Nested.SomeAmount); // Act @@ -960,7 +958,7 @@ namespace CoreIntegrationTests.Infrastructure e.Nested.SomeAmount = 5m; e.SomeContent = criteria; }); - SUT.AddMany(documents); + SUT.AddMany(documents); var expectedSum = documents.Sum(e => e.Nested.SomeAmount); // Act diff --git a/CoreUnitTests/.editorconfig b/CoreUnitTests/.editorconfig new file mode 100644 index 0000000..4db872d --- /dev/null +++ b/CoreUnitTests/.editorconfig @@ -0,0 +1,218 @@ +# All Files +[*] +charset = utf-8 +indent_style = space +indent_size = 4 +insert_final_newline = true +trim_trailing_whitespace = true + +######################################### +# File Extension Settings +########################################## + +# .NET Style Rules +# https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/language-rules#net-style-rules +[*.cs] +# "this." and "Me." qualifiers +dotnet_style_qualification_for_field = false:warning +dotnet_style_qualification_for_property = false:warning +dotnet_style_qualification_for_method = false:warning +dotnet_style_qualification_for_event = false:warning +# Language keywords instead of framework type names for type references +dotnet_style_predefined_type_for_locals_parameters_members = true:warning +dotnet_style_predefined_type_for_member_access = true:warning +# Modifier preferences +dotnet_style_require_accessibility_modifiers = always:warning +csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:warning +visual_basic_preferred_modifier_order = Partial,Default,Private,Protected,Public,Friend,NotOverridable,Overridable,MustOverride,Overloads,Overrides,MustInherit,NotInheritable,Static,Shared,Shadows,ReadOnly,WriteOnly,Dim,Const,WithEvents,Widening,Narrowing,Custom,Async:warning +dotnet_style_readonly_field = true:warning +# Parentheses preferences +dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:warning +dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:warning +dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:warning +dotnet_style_parentheses_in_other_operators = never_if_unnecessary:warning +# Expression-level preferences +dotnet_style_object_initializer = true:warning +dotnet_style_collection_initializer = true:warning +dotnet_style_explicit_tuple_names = true:warning +dotnet_style_prefer_inferred_tuple_names = true:warning +dotnet_style_prefer_inferred_anonymous_type_member_names = true:warning +dotnet_style_prefer_auto_properties = true:warning +dotnet_style_prefer_conditional_expression_over_assignment = false:suggestion +dotnet_diagnostic.IDE0045.severity = suggestion +dotnet_style_prefer_conditional_expression_over_return = false:suggestion +dotnet_diagnostic.IDE0046.severity = suggestion +dotnet_style_prefer_compound_assignment = true:warning +dotnet_style_prefer_simplified_interpolation = true:warning +dotnet_style_prefer_simplified_boolean_expressions = true:warning +# Null-checking preferences +dotnet_style_coalesce_expression = true:warning +dotnet_style_null_propagation = true:warning +dotnet_style_prefer_is_null_check_over_reference_equality_method = true:warning +# File header preferences +# file_header_template = \n© PROJECT-AUTHOR\n +# If you use StyleCop, you'll need to disable SA1636: File header copyright text should match. +# dotnet_diagnostic.SA1636.severity = none +# Undocumented +dotnet_style_operator_placement_when_wrapping = end_of_line:warning +csharp_style_prefer_null_check_over_type_check = true:warning +dotnet_analyzer_diagnostic.severity = warning +dotnet_code_quality_unused_parameters = all:warning +dotnet_remove_unnecessary_suppression_exclusions = none:warning +dotnet_sort_system_directives_first = true +dotnet_separate_import_directive_groups = false +dotnet_style_namespace_match_folder = true:suggestion +dotnet_diagnostic.IDE0130.severity = suggestion +dotnet_naming_style.camel_case_style.capitalization = camel_case +dotnet_naming_style.pascal_case_style.capitalization = pascal_case +dotnet_naming_style.first_upper_style.capitalization = first_word_upper +dotnet_naming_style.prefix_interface_with_i_style.capitalization = pascal_case +dotnet_naming_style.prefix_interface_with_i_style.required_prefix = I +dotnet_naming_style.prefix_type_parameters_with_t_style.capitalization = pascal_case +dotnet_naming_style.prefix_type_parameters_with_t_style.required_prefix = T +dotnet_naming_style.disallowed_style.capitalization = pascal_case +dotnet_naming_style.disallowed_style.required_prefix = ____RULE_VIOLATION____ +dotnet_naming_style.disallowed_style.required_suffix = ____RULE_VIOLATION____ +dotnet_naming_style.internal_error_style.capitalization = pascal_case +dotnet_naming_style.internal_error_style.required_prefix = ____INTERNAL_ERROR____ +dotnet_naming_style.internal_error_style.required_suffix = ____INTERNAL_ERROR____ +dotnet_naming_symbols.public_protected_constant_fields_group.applicable_accessibilities = public, protected, protected_internal +dotnet_naming_symbols.public_protected_constant_fields_group.required_modifiers = const +dotnet_naming_symbols.public_protected_constant_fields_group.applicable_kinds = field +dotnet_naming_rule.public_protected_constant_fields_must_be_pascal_case_rule.symbols = public_protected_constant_fields_group +dotnet_naming_rule.public_protected_constant_fields_must_be_pascal_case_rule.style = pascal_case_style +dotnet_naming_rule.public_protected_constant_fields_must_be_pascal_case_rule.severity = warning +dotnet_naming_symbols.public_protected_static_readonly_fields_group.applicable_accessibilities = public, protected, protected_internal +dotnet_naming_symbols.public_protected_static_readonly_fields_group.required_modifiers = static, readonly +dotnet_naming_symbols.public_protected_static_readonly_fields_group.applicable_kinds = field +dotnet_naming_rule.public_protected_static_readonly_fields_must_be_pascal_case_rule.symbols = public_protected_static_readonly_fields_group +dotnet_naming_rule.public_protected_static_readonly_fields_must_be_pascal_case_rule.style = pascal_case_style +dotnet_naming_rule.public_protected_static_readonly_fields_must_be_pascal_case_rule.severity = warning +dotnet_naming_symbols.other_public_protected_fields_group.applicable_accessibilities = public, protected, protected_internal +dotnet_naming_symbols.other_public_protected_fields_group.applicable_kinds = field +dotnet_naming_rule.other_public_protected_fields_disallowed_rule.symbols = other_public_protected_fields_group +dotnet_naming_rule.other_public_protected_fields_disallowed_rule.style = disallowed_style +dotnet_naming_rule.other_public_protected_fields_disallowed_rule.severity = error +dotnet_naming_symbols.stylecop_constant_fields_group.applicable_accessibilities = public, internal, protected_internal, protected, private_protected, private +dotnet_naming_symbols.stylecop_constant_fields_group.required_modifiers = const +dotnet_naming_symbols.stylecop_constant_fields_group.applicable_kinds = field +dotnet_naming_rule.stylecop_constant_fields_must_be_pascal_case_rule.symbols = stylecop_constant_fields_group +dotnet_naming_rule.stylecop_constant_fields_must_be_pascal_case_rule.style = pascal_case_style +dotnet_naming_rule.stylecop_constant_fields_must_be_pascal_case_rule.severity = warning +dotnet_naming_symbols.stylecop_static_readonly_fields_group.applicable_accessibilities = public, internal, protected_internal, protected, private_protected, private +dotnet_naming_symbols.stylecop_static_readonly_fields_group.required_modifiers = static, readonly +dotnet_naming_symbols.stylecop_static_readonly_fields_group.applicable_kinds = field +dotnet_naming_rule.stylecop_static_readonly_fields_must_be_pascal_case_rule.symbols = stylecop_static_readonly_fields_group +dotnet_naming_rule.stylecop_static_readonly_fields_must_be_pascal_case_rule.style = pascal_case_style +dotnet_naming_rule.stylecop_static_readonly_fields_must_be_pascal_case_rule.severity = warning +dotnet_naming_symbols.stylecop_fields_must_be_private_group.applicable_accessibilities = public, internal, protected_internal, protected, private_protected +dotnet_naming_symbols.stylecop_fields_must_be_private_group.applicable_kinds = field +dotnet_naming_rule.stylecop_instance_fields_must_be_private_rule.symbols = stylecop_fields_must_be_private_group +dotnet_naming_rule.stylecop_instance_fields_must_be_private_rule.style = disallowed_style +dotnet_naming_rule.stylecop_instance_fields_must_be_private_rule.severity = error +dotnet_naming_symbols.stylecop_private_fields_group.applicable_accessibilities = private +dotnet_naming_symbols.stylecop_private_fields_group.applicable_kinds = field +dotnet_naming_rule.stylecop_private_fields_must_be_camel_case_rule.symbols = stylecop_private_fields_group +dotnet_naming_rule.stylecop_private_fields_must_be_camel_case_rule.style = camel_case_style +dotnet_naming_rule.stylecop_private_fields_must_be_camel_case_rule.severity = warning +dotnet_naming_symbols.stylecop_local_fields_group.applicable_accessibilities = local +dotnet_naming_symbols.stylecop_local_fields_group.applicable_kinds = local +dotnet_naming_rule.stylecop_local_fields_must_be_camel_case_rule.symbols = stylecop_local_fields_group +dotnet_naming_rule.stylecop_local_fields_must_be_camel_case_rule.style = camel_case_style +dotnet_naming_rule.stylecop_local_fields_must_be_camel_case_rule.severity = silent +dotnet_naming_symbols.sanity_check_uncovered_field_case_group.applicable_accessibilities = * +dotnet_naming_symbols.sanity_check_uncovered_field_case_group.applicable_kinds = field +dotnet_naming_rule.sanity_check_uncovered_field_case_rule.symbols = sanity_check_uncovered_field_case_group +dotnet_naming_rule.sanity_check_uncovered_field_case_rule.style = internal_error_style +dotnet_naming_rule.sanity_check_uncovered_field_case_rule.severity = error +dotnet_naming_symbols.element_group.applicable_kinds = namespace, class, enum, struct, delegate, event, method, property +dotnet_naming_rule.element_rule.symbols = element_group +dotnet_naming_rule.element_rule.style = pascal_case_style +dotnet_naming_rule.element_rule.severity = warning +dotnet_naming_symbols.interface_group.applicable_kinds = interface +dotnet_naming_rule.interface_rule.symbols = interface_group +dotnet_naming_rule.interface_rule.style = prefix_interface_with_i_style +dotnet_naming_rule.interface_rule.severity = warning +dotnet_naming_symbols.type_parameter_group.applicable_kinds = type_parameter +dotnet_naming_rule.type_parameter_rule.symbols = type_parameter_group +dotnet_naming_rule.type_parameter_rule.style = prefix_type_parameters_with_t_style +dotnet_naming_rule.type_parameter_rule.severity = warning +dotnet_naming_symbols.parameters_group.applicable_kinds = parameter +dotnet_naming_rule.parameters_rule.symbols = parameters_group +dotnet_naming_rule.parameters_rule.style = camel_case_style +dotnet_naming_rule.parameters_rule.severity = warning +csharp_style_var_for_built_in_types = true:warning +csharp_style_var_when_type_is_apparent = true:warning +csharp_style_var_elsewhere = true:warning +csharp_style_expression_bodied_methods = true:warning +csharp_style_expression_bodied_constructors = true:warning +csharp_style_expression_bodied_operators = true:warning +csharp_style_expression_bodied_properties = true:warning +csharp_style_expression_bodied_indexers = true:warning +csharp_style_expression_bodied_accessors = true:warning +csharp_style_expression_bodied_lambdas = true:warning +csharp_style_expression_bodied_local_functions = true:warning +csharp_style_pattern_matching_over_is_with_cast_check = true:warning +csharp_style_pattern_matching_over_as_with_null_check = true:warning +csharp_style_prefer_switch_expression = true:warning +csharp_style_prefer_pattern_matching = true:warning +csharp_style_prefer_not_pattern = true:warning +csharp_style_inlined_variable_declaration = true:warning +csharp_prefer_simple_default_expression = true:warning +csharp_style_pattern_local_over_anonymous_function = true:warning +csharp_style_deconstructed_variable_declaration = true:warning +csharp_style_prefer_index_operator = true:warning +csharp_style_prefer_range_operator = true:warning +csharp_style_implicit_object_creation_when_type_is_apparent = true:warning +csharp_style_throw_expression = true:warning +csharp_style_conditional_delegate_call = true:warning +csharp_prefer_braces = true:warning +csharp_prefer_simple_using_statement = true:suggestion +dotnet_diagnostic.IDE0063.severity = suggestion +csharp_using_directive_placement = outside_namespace +csharp_prefer_static_local_function = true:warning +csharp_style_unused_value_expression_statement_preference = discard_variable:suggestion +dotnet_diagnostic.IDE0058.severity = suggestion +csharp_style_unused_value_assignment_preference = discard_variable:suggestion +dotnet_diagnostic.IDE0059.severity = suggestion +csharp_new_line_before_open_brace = all +csharp_new_line_before_else = true +csharp_new_line_before_catch = true +csharp_new_line_before_finally = true +csharp_new_line_before_members_in_object_initializers = true +csharp_new_line_before_members_in_anonymous_types = true +csharp_new_line_between_query_expression_clauses = true +csharp_indent_case_contents = true +csharp_indent_switch_labels = true +csharp_indent_labels = no_change +csharp_indent_block_contents = true +csharp_indent_braces = false +csharp_indent_case_contents_when_block = false +csharp_space_after_cast = false +csharp_space_after_keywords_in_control_flow_statements = true +csharp_space_between_parentheses = false +csharp_space_before_colon_in_inheritance_clause = true +csharp_space_after_colon_in_inheritance_clause = true +csharp_space_around_binary_operators = before_and_after +csharp_space_between_method_declaration_parameter_list_parentheses = false +csharp_space_between_method_declaration_empty_parameter_list_parentheses = false +csharp_space_between_method_declaration_name_and_open_parenthesis = false +csharp_space_between_method_call_parameter_list_parentheses = false +csharp_space_between_method_call_empty_parameter_list_parentheses = false +csharp_space_between_method_call_name_and_opening_parenthesis = false +csharp_space_after_comma = true +csharp_space_before_comma = false +csharp_space_after_dot = false +csharp_space_before_dot = false +csharp_space_after_semicolon_in_for_statement = true +csharp_space_before_semicolon_in_for_statement = false +csharp_space_around_declaration_statements = false +csharp_space_before_open_square_brackets = false +csharp_space_between_empty_square_brackets = false +csharp_space_between_square_brackets = false +csharp_preserve_single_line_statements = false +csharp_preserve_single_line_blocks = true +csharp_style_namespace_declarations = file_scoped:warning +dotnet_diagnostic.CA1707.severity = none + + diff --git a/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyAsyncTests.cs new file mode 100644 index 0000000..2de822e --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyAsyncTests.cs @@ -0,0 +1,77 @@ +using System; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Create; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests; + +public class AddManyAsyncTests : TestMongoRepositoryContext +{ + [Fact] + public async Task WithDocument_ShouldAddOne() + { + // Arrange + var documents = Fixture.CreateMany().ToList(); + Creator = new Mock(); + + // Act + await Sut.AddManyAsync(documents); + + // Assert + Creator.Verify(x => x.AddManyAsync(documents, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithDocumentAndCancellationToken_ShouldAddOne() + { + // Arrange + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + Creator = new Mock(); + + // Act + await Sut.AddManyAsync(documents, token); + + // Assert + Creator.Verify(x => x.AddManyAsync(documents, token), Times.Once); + } + + #region Keyed + + [Fact] + public async Task Keyed_WithDocument_ShouldAddOne() + { + // Arrange + var documents = Fixture.CreateMany>().ToList(); + Creator = new Mock(); + + // Act + await Sut.AddManyAsync, int>(documents); + + // Assert + Creator.Verify(x => x.AddManyAsync, int>(documents, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task Keyed_WithDocumentAndCancellationToken_ShouldAddOne() + { + // Arrange + var documents = Fixture.CreateMany>().ToList(); + var token = new CancellationToken(true); + Creator = new Mock(); + + // Act + await Sut.AddManyAsync, int>(documents, token); + + // Assert + Creator.Verify(x => x.AddManyAsync, int>(documents, token), Times.Once); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs new file mode 100644 index 0000000..1fb839a --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs @@ -0,0 +1,77 @@ +using System; +using System.Linq; +using System.Threading; +using AutoFixture; +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 void WithDocument_ShouldAddOne() + { + // Arrange + var documents = Fixture.CreateMany().ToList(); + Creator = new Mock(); + + // Act + Sut.AddMany(documents); + + // Assert + Creator.Verify(x => x.AddMany(documents, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithDocumentAndCancellationToken_ShouldAddOne() + { + // Arrange + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + Creator = new Mock(); + + // Act + Sut.AddMany(documents, token); + + // Assert + Creator.Verify(x => x.AddMany(documents, token), Times.Once); + } + + #region Keyed + + [Fact] + public void Keyed_WithDocument_ShouldAddOne() + { + // Arrange + var documents = Fixture.CreateMany>().ToList(); + Creator = new Mock(); + + // Act + Sut.AddMany, int>(documents); + + // Assert + Creator.Verify(x => x.AddMany, int>(documents, CancellationToken.None), Times.Once); + } + + [Fact] + public void Keyed_WithDocumentAndCancellationToken_ShouldAddOne() + { + // Arrange + var documents = Fixture.CreateMany>().ToList(); + var token = new CancellationToken(true); + Creator = new Mock(); + + // Act + Sut.AddMany, int>(documents, token); + + // Assert + Creator.Verify(x => x.AddMany, int>(documents, token), Times.Once); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneAsyncTests.cs new file mode 100644 index 0000000..92deacb --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneAsyncTests.cs @@ -0,0 +1,76 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Create; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests; + +public class AddOneAsyncTests : TestMongoRepositoryContext +{ + [Fact] + public async Task WithDocument_ShouldAddOne() + { + // Arrange + var document = Fixture.Create(); + Creator = new Mock(); + + // Act + await Sut.AddOneAsync(document); + + // Assert + Creator.Verify(x => x.AddOneAsync(document, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithDocumentAndCancellationToken_ShouldAddOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + Creator = new Mock(); + + // Act + await Sut.AddOneAsync(document, token); + + // Assert + Creator.Verify(x => x.AddOneAsync(document, token), Times.Once); + } + + #region Keyed + + [Fact] + public async Task Keyed_WithDocument_ShouldAddOne() + { + // Arrange + var document = Fixture.Create>(); + Creator = new Mock(); + + // Act + await Sut.AddOneAsync, int>(document); + + // Assert + Creator.Verify(x => x.AddOneAsync, int>(document, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task Keyed_WithDocumentAndCancellationToken_ShouldAddOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Creator = new Mock(); + + // Act + await Sut.AddOneAsync, int>(document, token); + + // Assert + Creator.Verify(x => x.AddOneAsync, int>(document, token), Times.Once); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs new file mode 100644 index 0000000..f3b419c --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs @@ -0,0 +1,75 @@ +using System; +using System.Threading; +using AutoFixture; +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 void WithDocument_ShouldAddOne() + { + // Arrange + var document = Fixture.Create(); + Creator = new Mock(); + + // Act + Sut.AddOne(document); + + // Assert + Creator.Verify(x => x.AddOne(document, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithDocumentAndCancellationToken_ShouldAddOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + Creator = new Mock(); + + // Act + Sut.AddOne(document, token); + + // Assert + Creator.Verify(x => x.AddOne(document, token), Times.Once); + } + + #region Keyed + + [Fact] + public void Keyed_WithDocument_ShouldAddOne() + { + // Arrange + var document = Fixture.Create>(); + Creator = new Mock(); + + // Act + Sut.AddOne, int>(document); + + // Assert + Creator.Verify(x => x.AddOne, int>(document, CancellationToken.None), Times.Once); + } + + [Fact] + public void Keyed_WithDocumentAndCancellationToken_ShouldAddOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Creator = new Mock(); + + // Act + Sut.AddOne, int>(document, token); + + // Assert + Creator.Verify(x => x.AddOne, int>(document, token), Times.Once); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs new file mode 100644 index 0000000..07bad84 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs @@ -0,0 +1,320 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Delete; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.DeleteTests; + +public class DeleteManyAsyncTests : TestMongoRepositoryContext +{ + [Fact] + public async Task WithDocuments_ShouldDeleteMany() + { + // Arrange + var documents = Fixture.CreateMany().ToList(); + var count = Fixture.Create(); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync(It.IsAny>(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync(documents); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync(documents, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithDocumentsAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var documents = Fixture.CreateMany().ToList(); + var count = Fixture.Create(); + var cancellationToken = new CancellationToken(true); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync(It.IsAny>(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync(documents, cancellationToken); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync(documents, cancellationToken), Times.Once); + } + + [Fact] + public async Task WithFilter_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + + Expression> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync(filter, null, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithFilterAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Expression> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync(filter, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync(filter, null, token), Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKey_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Expression> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync(filter, partitionKey, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + Expression> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync(filter, partitionKey, token), Times.Once); + } + + #region Keyed + + [Fact] + public async Task Keyed_WithDocuments_ShouldDeleteMany() + { + // Arrange + var documents = Fixture.CreateMany>().ToList(); + var count = Fixture.Create(); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync, int>(It.IsAny>>(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync, int>(documents); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync, int>(documents, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task Keyed_WithDocumentsAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var documents = Fixture.CreateMany>().ToList(); + var count = Fixture.Create(); + var cancellationToken = new CancellationToken(true); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync, int>(It.IsAny>>(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync, int>(documents, cancellationToken); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync, int>(documents, cancellationToken), Times.Once); + } + + [Fact] + public async Task Keyed_WithFilter_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync, int>(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync, int>(filter, null, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync, int>(filter, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync, int>(filter, null, token), Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndPartitionKey_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync, int>(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync, int>(filter, partitionKey, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync, int>(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync, int>(filter, partitionKey, token), Times.Once); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyTests.cs new file mode 100644 index 0000000..afa1cb4 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyTests.cs @@ -0,0 +1,294 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Delete; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.DeleteTests; + +public class DeleteManyTests : TestMongoRepositoryContext +{ + [Fact] + public void WithDocuments_ShouldDeleteMany() + { + // Arrange + var documents = Fixture.CreateMany().ToList(); + var count = Fixture.Create(); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteMany(It.IsAny>(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteMany(documents); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany(documents, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithDocumentsAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var documents = Fixture.CreateMany().ToList(); + var count = Fixture.Create(); + var token = new CancellationToken(true); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteMany(It.IsAny>(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteMany(documents, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany(documents, token), Times.Once); + } + + [Fact] + public void WithFilter_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + + Expression> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteMany(It.IsAny>>(), It.IsAny(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteMany(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany(filter, null, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithFilterAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Expression> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteMany(It.IsAny>>(), It.IsAny(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteMany(filter, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany(filter, null, token), Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKey_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Expression> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteMany(It.IsAny>>(), It.IsAny(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteMany(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany(filter, partitionKey, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + Expression> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteMany(It.IsAny>>(), It.IsAny(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteMany(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany(filter, partitionKey, token), Times.Once); + } + + #region Keyed + + [Fact] + public void Keyed_WithDocuments_ShouldDeleteMany() + { + // Arrange + var documents = Fixture.CreateMany>().ToList(); + var count = Fixture.Create(); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteMany, int>(It.IsAny>>(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteMany, int>(documents); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(documents, CancellationToken.None), Times.Once); + } + + [Fact] + public void Keyed_WithDocumentsAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var documents = Fixture.CreateMany>().ToList(); + var count = Fixture.Create(); + var token = new CancellationToken(true); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteMany, int>(It.IsAny>>(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteMany, int>(documents, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(documents, token), Times.Once); + } + + [Fact] + public void Keyed_WithFilter_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteMany, int>(It.IsAny, bool>>>(), null, CancellationToken.None)) + .Returns(count); + + // Act + var result = Sut.DeleteMany, int>(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(filter, null, CancellationToken.None), Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteMany, int>(It.IsAny, bool>>>(), It.IsAny(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteMany, int>(filter, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(filter, null, token), Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndPartitionKey_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteMany, int>(It.IsAny, bool>>>(), It.IsAny(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteMany, int>(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(filter, partitionKey, CancellationToken.None), Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteMany, int>(It.IsAny, bool>>>(), It.IsAny(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteMany, int>(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(filter, partitionKey, token), Times.Once); + } + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs new file mode 100644 index 0000000..9f0fcfe --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs @@ -0,0 +1,296 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Delete; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.DeleteTests; + +public class DeleteOneAsyncTests : TestMongoRepositoryContext +{ + [Fact] + public async Task WithDocument_ShouldDeleteOne() + { + // Arrange + var document = Fixture.Create(); + var count = Fixture.Create(); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync(document); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync(document, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithDocumentAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var document = Fixture.Create(); + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync(document, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync(document, token), Times.Once); + } + + [Fact] + public async Task WithFilter_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + + Expression> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync(It.IsAny>>(), It.IsAny(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync(filter, null, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithFilterAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var token = new CancellationToken(true); + + Expression> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync(It.IsAny>>(), It.IsAny(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync(filter, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync(filter, null, token), Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKey_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Expression> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync(It.IsAny>>(), It.IsAny(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync(filter, partitionKey, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + Expression> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync(It.IsAny>>(), It.IsAny(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync(filter, partitionKey, token), Times.Once); + } + + #region Keyed + + [Fact] + public async Task Keyed_WithDocument_ShouldDeleteOne() + { + // Arrange + var document = Fixture.Create>(); + var count = Fixture.Create(); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync, int>(It.IsAny>(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync, int>(document); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync, int>(document, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task Keyed_WithDocumentAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var document = Fixture.Create>(); + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync, int>(It.IsAny>(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync, int>(document, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync, int>(document, token), Times.Once); + } + + [Fact] + public async Task Keyed_WithFilter_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync, int>(It.IsAny, bool>>>(), It.IsAny(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync, int>(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync, int>(filter, null, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var token = new CancellationToken(true); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync, int>(It.IsAny, bool>>>(), It.IsAny(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync, int>(filter, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync, int>(filter, null, token), Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndPartitionKey_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync, int>(It.IsAny, bool>>>(), It.IsAny(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync, int>(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync, int>(filter, partitionKey, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync, int>(It.IsAny, bool>>>(), It.IsAny(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync, int>(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync, int>(filter, partitionKey, token), Times.Once); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneTests.cs new file mode 100644 index 0000000..bb23e69 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneTests.cs @@ -0,0 +1,317 @@ +namespace CoreUnitTests.BaseMongoRepositoryTests.DeleteTests; + +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using FluentAssertions; +using Infrastructure; +using Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Delete; +using Moq; +using Xunit; + +public class DeleteOneTests : TestMongoRepositoryContext +{ + [Fact] + public void WithDocument_ShouldDeleteOne() + { + // Arrange + var document = Fixture.Create(); + var count = Fixture.Create(); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOne(It.IsAny(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(document); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne(document, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithDocumentAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var document = Fixture.Create(); + var count = Fixture.Create(); + var token = new CancellationToken(true); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOne(It.IsAny(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(document, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne(document, token), Times.Once); + } + + [Fact] + public void WithFilter_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + + Expression> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOne(It.IsAny>>(), It.IsAny(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne(filter, null, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithFilterAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var token = new CancellationToken(true); + + Expression> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup( + x => x.DeleteOne( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(filter, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne(filter, null, token), Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKey_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Expression> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup( + x => x.DeleteOne( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne(filter, partitionKey, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + Expression> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup( + x => x.DeleteOne( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne(filter, partitionKey, token), Times.Once); + } + + [Fact] + public void WithKeyedDocument_ShouldDeleteOne() + { + // Arrange + var document = Fixture.Create>(); + var count = Fixture.Create(); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOne, int>(It.IsAny>(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne, int>(document); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(document, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithKeyedDocumentAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var document = Fixture.Create>(); + var count = Fixture.Create(); + var token = new CancellationToken(true); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOne, int>(It.IsAny>(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne, int>(document, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(document, token), Times.Once); + } + + [Fact] + public void Keyed_WithFilter_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup( + x => x.DeleteOne, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne, int>(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(filter, null, CancellationToken.None), Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var token = new CancellationToken(true); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup( + x => x.DeleteOne, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne, int>(filter, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(filter, null, token), Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndPartitionKey_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup( + x => x.DeleteOne, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne, int>(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(filter, partitionKey, CancellationToken.None), Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup( + x => x.DeleteOne, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne, int>(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(filter, partitionKey, token), Times.Once); + } +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/BaseIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/BaseIndexTests.cs new file mode 100644 index 0000000..1fe74d6 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/BaseIndexTests.cs @@ -0,0 +1,35 @@ +using System; +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/CreateAscendingIndexAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexAsyncTests.cs new file mode 100644 index 0000000..15655eb --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexAsyncTests.cs @@ -0,0 +1,294 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class CreateAscendingIndexAsyncTests : BaseIndexTests +{ + private readonly Expression, object>> keyedFieldExpression = t => t.SomeContent2; + private readonly Expression> fieldExpression = t => t.SomeContent2; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(fieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync(fieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(fieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync(fieldExpression, null, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(fieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync( + fieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(fieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync( + fieldExpression, options, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(fieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync( + fieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(fieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync( + fieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(fieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync( + fieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(fieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync( + fieldExpression, options, partitionKey, token)); + } + + #region Keyed + + [Fact] + public async Task Keyed_WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync, int>(keyedFieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>(keyedFieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync, int>(keyedFieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>(keyedFieldExpression, null, null, token)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync, int>(keyedFieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync, int>(keyedFieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, options, null, token)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync, int>(keyedFieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync, int>(keyedFieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync, int>(keyedFieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync, int>(keyedFieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, options, partitionKey, token)); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateCombinedTextIndexAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateCombinedTextIndexAsyncTests.cs new file mode 100644 index 0000000..425aec9 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateCombinedTextIndexAsyncTests.cs @@ -0,0 +1,295 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; +using CancellationToken = System.Threading.CancellationToken; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class CreateCombinedTextIndexAsyncTests : BaseIndexTests +{ + private readonly List>> fieldExpressions = new() {t => t.SomeContent2, t => t.SomeContent3}; + private readonly List, object>>> keyedFieldExpressions = new() {t => t.SomeContent2, t => t.SomeContent3}; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync(fieldExpressions); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync(fieldExpressions, null, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync(fieldExpressions, token); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync(fieldExpressions, null, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync(fieldExpressions, options); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync( + fieldExpressions, options, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync(fieldExpressions, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync( + fieldExpressions, options, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync(fieldExpressions, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync( + fieldExpressions, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync(fieldExpressions, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync( + fieldExpressions, null, partitionKey, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync(fieldExpressions, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync( + fieldExpressions, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync(fieldExpressions, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync( + fieldExpressions, options, partitionKey, token)); + } + + #region Keyed + + [Fact] + public async Task Keyed_WithKeyedFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync, int>(keyedFieldExpressions); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync, int>(keyedFieldExpressions, null, null, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync, int>(keyedFieldExpressions, token); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync, int>(keyedFieldExpressions, null, null, token)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync, int>(keyedFieldExpressions, options); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync, int>( + keyedFieldExpressions, options, null, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync, int>(keyedFieldExpressions, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync, int>( + keyedFieldExpressions, options, null, token)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync, int>(keyedFieldExpressions, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync, int>( + keyedFieldExpressions, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync, int>(keyedFieldExpressions, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync, int>( + keyedFieldExpressions, null, partitionKey, token)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync, int>(keyedFieldExpressions, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync, int>( + keyedFieldExpressions, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync, int>(keyedFieldExpressions, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync, int>( + keyedFieldExpressions, options, partitionKey, token)); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateDescendingIndexAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateDescendingIndexAsyncTests.cs new file mode 100644 index 0000000..c56f676 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateDescendingIndexAsyncTests.cs @@ -0,0 +1,294 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class CreateDescendingIndexAsyncTests : BaseIndexTests +{ + private readonly Expression, object>> keyedFieldExpression = t => t.SomeContent2; + private readonly Expression> fieldExpression = t => t.SomeContent2; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(fieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync(fieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(fieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync(fieldExpression, null, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(fieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync( + fieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(fieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync( + fieldExpression, options, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(fieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync( + fieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(fieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync( + fieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(fieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync( + fieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(fieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync( + fieldExpression, options, partitionKey, token)); + } + + #region Keyed + + [Fact] + public async Task Keyed_WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync, int>(keyedFieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>(keyedFieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync, int>(keyedFieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>(keyedFieldExpression, null, null, token)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync, int>(keyedFieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync, int>(keyedFieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, options, null, token)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync, int>(keyedFieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync, int>(keyedFieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync, int>(keyedFieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync, int>(keyedFieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, options, partitionKey, token)); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateHashedIndexAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateHashedIndexAsyncTests.cs new file mode 100644 index 0000000..349aadb --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateHashedIndexAsyncTests.cs @@ -0,0 +1,294 @@ +using System; +using System.Linq.Expressions; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; +using CancellationToken = System.Threading.CancellationToken; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class CreateHashedIndexAsyncTests : BaseIndexTests +{ + private readonly Expression> fieldExpression = t => t.SomeContent2; + private readonly Expression, object>> keyedFieldExpression = t => t.SomeContent2; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync(fieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync(fieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync(fieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync(fieldExpression, null, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync(fieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync( + fieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync(fieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync( + fieldExpression, options, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync(fieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync( + fieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync(fieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync( + fieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync(fieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync( + fieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync(fieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync( + fieldExpression, options, partitionKey, token)); + } + + #region Keyed + + [Fact] + public async Task Keyed_WithKeyedFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync, int>(keyedFieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync, int>(keyedFieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync, int>(keyedFieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync, int>(keyedFieldExpression, null, null, token)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync, int>(keyedFieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync, int>( + keyedFieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync, int>(keyedFieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync, int>( + keyedFieldExpression, options, null, token)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync, int>(keyedFieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync, int>( + keyedFieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync, int>(keyedFieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync, int>( + keyedFieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync, int>(keyedFieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync, int>( + keyedFieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync, int>(keyedFieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync, int>( + keyedFieldExpression, options, partitionKey, token)); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexAsyncTests.cs new file mode 100644 index 0000000..7e1f651 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexAsyncTests.cs @@ -0,0 +1,294 @@ +using System; +using System.Linq.Expressions; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; +using CancellationToken = System.Threading.CancellationToken; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class CreateTextIndexAsyncTests : BaseIndexTests +{ + private readonly Expression> fieldExpression = t => t.SomeContent2; + private readonly Expression, object>> keyedFieldExpression = t => t.SomeContent2; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(fieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync(fieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(fieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync(fieldExpression, null, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(fieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + fieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(fieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + fieldExpression, options, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(fieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + fieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(fieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + fieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(fieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + fieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(fieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + fieldExpression, options, partitionKey, token)); + } + + #region Keyed + + [Fact] + public async Task Keyed_WithKeyedFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync, int>(keyedFieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync, int>(keyedFieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync, int>(keyedFieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync, int>(keyedFieldExpression, null, null, token)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync, int>(keyedFieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync, int>( + keyedFieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync, int>(keyedFieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync, int>( + keyedFieldExpression, options, null, token)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync, int>(keyedFieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync, int>( + keyedFieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync, int>(keyedFieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync, int>( + keyedFieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync, int>(keyedFieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync, int>( + keyedFieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync, int>(keyedFieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync, int>( + keyedFieldExpression, options, partitionKey, token)); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/DropIndexAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/DropIndexAsyncTests.cs new file mode 100644 index 0000000..8995b70 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/DropIndexAsyncTests.cs @@ -0,0 +1,145 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class DropIndexAsyncTests: BaseIndexTests +{ + [Fact] + public async Task WitIndexName_DropsIndex() + { + // Arrange + var indexName = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.DropIndexAsync(indexName); + + // Assert + IndexHandler.Verify( + x => x.DropIndexAsync(indexName, null, CancellationToken.None)); + } + + [Fact] + public async Task WitIndexNameAndCancellationToken_DropsIndex() + { + // Arrange + var indexName = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.DropIndexAsync(indexName, token); + + // Assert + IndexHandler.Verify( + x => x.DropIndexAsync(indexName, null, token)); + } + + [Fact] + public async Task WitIndexNameAndPartitionKey_DropsIndex() + { + // Arrange + var indexName = Fixture.Create(); + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.DropIndexAsync(indexName, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.DropIndexAsync(indexName, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WitIndexNameAndPartitionKeyAndCancellationToken_DropsIndex() + { + // Arrange + var indexName = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.DropIndexAsync(indexName, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.DropIndexAsync(indexName, partitionKey, token)); + } + + #region Keyed + + [Fact] + public async Task Keyed_WithIndexName_DropsIndex() + { + // Arrange + var indexName = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.DropIndexAsync, int>(indexName); + + // Assert + IndexHandler.Verify( + x => x.DropIndexAsync, int>(indexName, null, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithIndexNameAndCancellationToken_DropsIndex() + { + // Arrange + var indexName = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.DropIndexAsync, int>(indexName, token); + + // Assert + IndexHandler.Verify( + x => x.DropIndexAsync, int>(indexName, null, token)); + } + + [Fact] + public async Task Keyed_WithIndexNameAndPartitionKey_DropsIndex() + { + // Arrange + var indexName = Fixture.Create(); + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.DropIndexAsync, int>(indexName, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.DropIndexAsync, int>(indexName, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithIndexNameAndPartitionKeyAndCancellationToken_DropsIndex() + { + // Arrange + var indexName = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.DropIndexAsync, int>(indexName, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.DropIndexAsync, int>(indexName, partitionKey, token)); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs new file mode 100644 index 0000000..6c9afb8 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs @@ -0,0 +1,182 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class GetIndexNamesAsyncTests : BaseIndexTests +{ + [Fact] + public async Task WithNoParameters_ReturnsIndexNames() + { + // Arrange + IndexHandler = new Mock(); + var indexName = Fixture.Create(); + + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(It.IsAny(), It.IsAny())) + .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, CancellationToken.None), Times.Once()); + } + + [Fact] + public async Task WithCancellationToken_ReturnsIndexNames() + { + // Arrange + IndexHandler = new Mock(); + var indexName = Fixture.Create(); + var token = new CancellationToken(true); + + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(new List { indexName }); + + // Act + var result = await Sut.GetIndexesNamesAsync(token); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + IndexHandler.Verify(x => x.GetIndexesNamesAsync(null, token), Times.Once()); + } + + [Fact] + public async Task WithPartitionKey_ReturnsIndexNames() + { + // Arrange + IndexHandler = new Mock(); + var indexName = Fixture.Create(); + var partitionKey = Fixture.Create(); + + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(new List { indexName }); + + // Act + var result = await Sut.GetIndexesNamesAsync(partitionKey); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + IndexHandler.Verify(x => x.GetIndexesNamesAsync(partitionKey, CancellationToken.None), Times.Once()); + } + + [Fact] + public async Task WithPartitionKeyAndCancellationToken_ReturnsIndexNames() + { + // Arrange + IndexHandler = new Mock(); + var indexName = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(new List { indexName }); + + // Act + var result = await Sut.GetIndexesNamesAsync(partitionKey, token); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + IndexHandler.Verify(x => x.GetIndexesNamesAsync(partitionKey, token), Times.Once()); + } + + [Fact] + public async Task Keyed_WithNoParameters_ReturnsIndexNames() + { + // Arrange + IndexHandler = new Mock(); + var indexName = Fixture.Create(); + + IndexHandler + .Setup(x => x.GetIndexesNamesAsync, int>(It.IsAny(), It.IsAny())) + .ReturnsAsync(new List { indexName }); + + // Act + var result = await Sut.GetIndexesNamesAsync, int>(); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + IndexHandler.Verify(x => x.GetIndexesNamesAsync, int>(null, CancellationToken.None), Times.Once()); + } + + [Fact] + public async Task Keyed_WithCancellationToken_ReturnsIndexNames() + { + // Arrange + IndexHandler = new Mock(); + var indexName = Fixture.Create(); + var token = new CancellationToken(true); + + IndexHandler + .Setup(x => x.GetIndexesNamesAsync, int>(It.IsAny(), It.IsAny())) + .ReturnsAsync(new List { indexName }); + + // Act + var result = await Sut.GetIndexesNamesAsync, int>(token); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + IndexHandler.Verify(x => x.GetIndexesNamesAsync, int>(null, token), Times.Once()); + } + + [Fact] + public async Task Keyed_WithPartitionKey_ReturnsIndexNames() + { + // Arrange + IndexHandler = new Mock(); + var indexName = Fixture.Create(); + var partitionKey = Fixture.Create(); + + IndexHandler + .Setup(x => x.GetIndexesNamesAsync, int>(It.IsAny(), It.IsAny())) + .ReturnsAsync(new List { indexName }); + + // Act + var result = await Sut.GetIndexesNamesAsync, int>(partitionKey); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + IndexHandler.Verify(x => x.GetIndexesNamesAsync, int>(partitionKey, CancellationToken.None), Times.Once()); + } + + [Fact] + public async Task Keyed_WithPartitionKeyAndCancellationToken_ReturnsIndexNames() + { + // Arrange + IndexHandler = new Mock(); + var indexName = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + IndexHandler + .Setup(x => x.GetIndexesNamesAsync, int>(It.IsAny(), It.IsAny())) + .ReturnsAsync(new List { indexName }); + + // Act + var result = await Sut.GetIndexesNamesAsync, int>(partitionKey, token); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + IndexHandler.Verify(x => x.GetIndexesNamesAsync, int>(partitionKey, token), Times.Once()); + } +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateManyAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateManyAsyncTests.cs new file mode 100644 index 0000000..bc9d818 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateManyAsyncTests.cs @@ -0,0 +1,1055 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Update; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.UpdateTests; + +public class UpdateManyAsyncTests : TestMongoRepositoryContext +{ + private readonly Expression> fieldExpression = x => x.SomeContent; + private readonly FilterDefinition filterDefinition = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + private readonly Expression> filterExpression = x => x.SomeContent == "SomeContent"; + private readonly UpdateDefinition updateDefinition = Builders.Update.Set(x => x.SomeContent, "Updated"); + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var count = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterDefinition, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterDefinition, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterDefinition, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterDefinition, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterExpression, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterExpression, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterExpression, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterExpression, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterExpression, + updateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterExpression, + updateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterExpression, + updateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterExpression, + updateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterDefinition, + updateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndUpdateDefinitionCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterDefinition, + updateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterDefinition, + updateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterDefinition, + updateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + #region Keyed + + private readonly UpdateDefinition> keyedUpdateDefinition = + Builders>.Update.Set(x => x.SomeContent, "Updated"); + + private readonly Expression, string>> keyedFieldExpression = x => x.SomeContent; + private readonly FilterDefinition> keyedFilterDefinition = Builders>.Filter.Eq(x => x.Id, 1); + private readonly Expression, bool>> keyedFilterExpression = x => x.SomeContent == "SomeContent"; + + [Fact] + public async Task Keyed_WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var count = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int, string>(keyedFilterDefinition, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int, string>(keyedFilterDefinition, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int, string>(keyedFilterDefinition, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int, string>(keyedFilterDefinition, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int, string>(keyedFilterExpression, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int, string>(keyedFilterExpression, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int, string>(keyedFilterExpression, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int, string>(keyedFilterExpression, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int>(keyedFilterExpression, keyedUpdateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + keyedFilterExpression, + keyedUpdateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int>(keyedFilterExpression, keyedUpdateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + keyedFilterExpression, + keyedUpdateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int>(keyedFilterExpression, keyedUpdateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + keyedFilterExpression, + keyedUpdateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int>(keyedFilterExpression, keyedUpdateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + keyedFilterExpression, + keyedUpdateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int>(keyedFilterDefinition, keyedUpdateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + keyedFilterDefinition, + keyedUpdateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndUpdateDefinitionCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int>(keyedFilterDefinition, keyedUpdateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + keyedFilterDefinition, + keyedUpdateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int>(keyedFilterDefinition, keyedUpdateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + keyedFilterDefinition, + keyedUpdateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int>(keyedFilterDefinition, keyedUpdateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + keyedFilterDefinition, + keyedUpdateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateManyTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateManyTests.cs new file mode 100644 index 0000000..7ca972e --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateManyTests.cs @@ -0,0 +1,1053 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Update; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.UpdateTests; + +public class UpdateManyTests : TestMongoRepositoryContext +{ + + private readonly Expression> fieldExpression = x => x.SomeContent; + private readonly FilterDefinition filterDefinition = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + private readonly Expression> filterExpression = x => x.SomeContent == "SomeContent"; + private readonly UpdateDefinition updateDefinition = Builders.Update.Set(x => x.SomeContent, "Updated"); + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var count = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterDefinition, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterDefinition, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterDefinition, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterDefinition, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterExpression, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterExpression, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterExpression, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterExpression, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterExpression, + updateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterExpression, + updateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, updateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterExpression, + updateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, updateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterExpression, + updateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterDefinition, + updateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndUpdateDefinitionCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterDefinition, + updateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, updateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterDefinition, + updateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, updateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterDefinition, + updateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + #region Keyed + + private readonly Expression, string>> keyedFieldExpression = x => x.SomeContent; + private readonly FilterDefinition> keyedFilterDefinition = Builders>.Filter.Eq(x => x.Id, 1); + private readonly Expression, bool>> keyedFilterExpression = x => x.SomeContent == "SomeContent"; + private readonly UpdateDefinition> keyedUpdateDefinition = Builders>.Update.Set(x => x.SomeContent, "Updated"); + + [Fact] + public void Keyed_WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var count = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int, string>(keyedFilterDefinition, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int, string>(keyedFilterDefinition, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int, string>(keyedFilterDefinition, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int, string>(keyedFilterDefinition, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int, string>(keyedFilterExpression, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int, string>(keyedFilterExpression, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int, string>(keyedFilterExpression, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int, string>(keyedFilterExpression, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterExpressionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int>(keyedFilterExpression, keyedUpdateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + keyedFilterExpression, + keyedUpdateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterExpressionAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int>(keyedFilterExpression, keyedUpdateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + keyedFilterExpression, + keyedUpdateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterExpressionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int>(keyedFilterExpression, keyedUpdateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + keyedFilterExpression, + keyedUpdateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int>(keyedFilterExpression, keyedUpdateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + keyedFilterExpression, + keyedUpdateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int>(keyedFilterDefinition, keyedUpdateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + keyedFilterDefinition, + keyedUpdateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndUpdateDefinitionCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int>(keyedFilterDefinition, keyedUpdateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + keyedFilterDefinition, + keyedUpdateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int>(keyedFilterDefinition, keyedUpdateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + keyedFilterDefinition, + keyedUpdateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int>(keyedFilterDefinition, keyedUpdateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + keyedFilterDefinition, + keyedUpdateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateOneAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateOneAsyncTests.cs new file mode 100644 index 0000000..9cc0421 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateOneAsyncTests.cs @@ -0,0 +1,1318 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Update; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.UpdateTests; + +public class UpdateOneAsyncTests : TestMongoRepositoryContext +{ + private readonly UpdateDefinition updateDefinition = Builders.Update.Set(x => x.SomeContent, "Updated"); + private readonly Expression> fieldExpression = x => x.SomeContent; + private readonly FilterDefinition filterDefinition = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + private readonly Expression> filterExpression = x => x.SomeContent == "SomeContent"; + + [Fact] + public async Task WithDocument_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOneAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document); + + // Assert + Updater.Verify(x => x.UpdateOneAsync(document, CancellationToken.None), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOneAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, token); + + // Assert + Updater.Verify(x => x.UpdateOneAsync(document, token), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + document, + updateDefinition, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + document, + updateDefinition, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + document, + fieldExpression, + value, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + document, + fieldExpression, + value, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + filterDefinition, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + filterDefinition, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + filterDefinition, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + filterDefinition, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + filterExpression, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + filterExpression, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + filterExpression, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + filterExpression, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + #region Keyed + + private readonly UpdateDefinition> keyedUpdateDefinition = Builders>.Update.Set(x => x.SomeContent, "Updated"); + private readonly Expression, string>> keyedFieldExpression = x => x.SomeContent; + private readonly FilterDefinition> keyedFilterDefinition = Builders>.Filter.Eq(x => x.Id, 1); + private readonly Expression, bool>> keyedFilterExpression = x => x.SomeContent == "SomeContent"; + + [Fact] + public async Task Keyed_WithDocument_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOneAsync, int>(It.IsAny>(), It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int>(document); + + // Assert + Updater.Verify(x => x.UpdateOneAsync, int>(document, CancellationToken.None), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithDocumentAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOneAsync, int>(It.IsAny>(), It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int>(document, token); + + // Assert + Updater.Verify(x => x.UpdateOneAsync, int>(document, token), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithDocumentAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int>( + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int>(document, keyedUpdateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int>( + document, + keyedUpdateDefinition, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int>( + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int>(document, keyedUpdateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int>( + document, + keyedUpdateDefinition, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithDocumentAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(document, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + document, + keyedFieldExpression, + value, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(document, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + document, + keyedFieldExpression, + value, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(keyedFilterDefinition, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(keyedFilterDefinition, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(keyedFilterDefinition, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(keyedFilterDefinition, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(keyedFilterExpression, keyedFieldExpression, value); + + // Assert + var expectedFilter = Builders>.Filter.Where(keyedFilterExpression); + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + It.Is>>(y => y.EquivalentTo(expectedFilter)), + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(keyedFilterExpression, keyedFieldExpression, value, token); + + // Assert + var expectedFilter = Builders>.Filter.Where(keyedFilterExpression); + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + It.Is>>(y => y.EquivalentTo(expectedFilter)), + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(keyedFilterExpression, keyedFieldExpression, value, partitionKey); + + // Assert + var expectedFilter = Builders>.Filter.Where(keyedFilterExpression); + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + It.Is>>(y => y.EquivalentTo(expectedFilter)), + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(keyedFilterExpression, keyedFieldExpression, value, partitionKey, token); + + // Assert + var expectedFilter = Builders>.Filter.Where(keyedFilterExpression); + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + It.Is>>(y => y.EquivalentTo(expectedFilter)), + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + #endregion + + #region ClientSession + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndDocument_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOneAsync, int>( + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int>(session, document); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int>( + session, + document, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndDocumentAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOneAsync, int>( + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int>(session, document, token); + + // Assert + Updater.Verify(x => x.UpdateOneAsync, int>(session, document, token), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndDocumentAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int>( + It.IsAny(), + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int>(session, document, keyedUpdateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int>( + session, + document, + keyedUpdateDefinition, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int>( + It.IsAny(), + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int>(session, document, keyedUpdateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int>( + session, + document, + keyedUpdateDefinition, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndDocumentAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, document, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + document, + keyedFieldExpression, + value, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, document, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + document, + keyedFieldExpression, + value, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, keyedFilterDefinition, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, keyedFilterDefinition, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, keyedFilterDefinition, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, keyedFilterDefinition, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, keyedFilterExpression, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + keyedFilterExpression, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, keyedFilterExpression, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + keyedFilterExpression, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, keyedFilterExpression, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + keyedFilterExpression, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, keyedFilterExpression, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + keyedFilterExpression, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateOneTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateOneTests.cs new file mode 100644 index 0000000..7e1fe53 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateOneTests.cs @@ -0,0 +1,1315 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Update; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.UpdateTests; + +public class UpdateOneTests : TestMongoRepositoryContext +{ + private readonly UpdateDefinition updateDefinition = Builders.Update.Set(x => x.SomeContent, "Updated"); + private readonly Expression> fieldExpression = x => x.SomeContent; + private readonly FilterDefinition filterDefinition = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + private readonly Expression> filterExpression = x => x.SomeContent == "SomeContent"; + + [Fact] + public void WithDocument_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOne(It.IsAny(), It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document); + + // Assert + Updater.Verify(x => x.UpdateOne(document, CancellationToken.None), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOne(It.IsAny(), It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, token); + + // Assert + Updater.Verify(x => x.UpdateOne(document, token), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateOne( + document, + updateDefinition, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateOne( + document, + updateDefinition, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne( + document, + fieldExpression, + value, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne( + document, + fieldExpression, + value, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterDefinition, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne( + filterDefinition, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterDefinition, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne( + filterDefinition, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterDefinition, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOne( + filterDefinition, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterDefinition, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOne( + filterDefinition, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterExpression, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne( + filterExpression, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterExpression, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne( + filterExpression, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterExpression, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOne( + filterExpression, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterExpression, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOne( + filterExpression, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + #region Keyed + + private readonly UpdateDefinition> keyedUpdateDefinition = Builders>.Update.Set(x => x.SomeContent, "Updated"); + private readonly Expression, string>> keyedFieldExpression = x => x.SomeContent; + private readonly FilterDefinition> keyedFilterDefinition = Builders>.Filter.Eq(x => x.Id, 1); + private readonly Expression, bool>> keyedFilterExpression = x => x.SomeContent == "SomeContent"; + + [Fact] + public void Keyed_WithDocument_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOne, int>(It.IsAny>(), It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int>(document); + + // Assert + Updater.Verify(x => x.UpdateOne, int>(document, CancellationToken.None), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithDocumentAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOne, int>(It.IsAny>(), It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int>(document, token); + + // Assert + Updater.Verify(x => x.UpdateOne, int>(document, token), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithDocumentAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int>( + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int>(document, keyedUpdateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateOne, int>( + document, + keyedUpdateDefinition, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int>( + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int>(document, keyedUpdateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int>( + document, + keyedUpdateDefinition, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithDocumentAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(document, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + document, + keyedFieldExpression, + value, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(document, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + document, + keyedFieldExpression, + value, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(keyedFilterDefinition, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(keyedFilterDefinition, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(keyedFilterDefinition, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(keyedFilterDefinition, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(keyedFilterExpression, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(keyedFilterExpression, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(keyedFilterExpression, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(keyedFilterExpression, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + #endregion + + #region client session + + [Fact] + public void Keyed_WithClientSessionHandlerAndDocument_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var session = new Mock().Object; + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOne, int>( + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int>(session, document); + + // Assert + Updater.Verify(x => x.UpdateOne, int>( + session, + document, + CancellationToken.None), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerAndDocumentAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + var session = new Mock().Object; + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOne, int>( + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int>(session, document, token); + + // Assert + Updater.Verify(x => x.UpdateOne, int>(session, document, token), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerAndDocumentAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int>( + It.IsAny(), + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int>(session, document, keyedUpdateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateOne, int>( + session, + document, + keyedUpdateDefinition, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerAndDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int>( + It.IsAny(), + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int>(session, document, keyedUpdateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int>( + session, + document, + keyedUpdateDefinition, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerAndDocumentAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, document, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + document, + keyedFieldExpression, + value, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerAndDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, document, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + document, + keyedFieldExpression, + value, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, keyedFilterDefinition, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, keyedFilterDefinition, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, keyedFilterDefinition, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, keyedFilterDefinition, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, keyedFilterExpression, keyedFieldExpression, value); + + // Assert + var expectedFilterDefinition = new ExpressionFilterDefinition>(keyedFilterExpression); + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + It.Is>>(y => y.EquivalentTo(expectedFilterDefinition)), + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, keyedFilterExpression, keyedFieldExpression, value, token); + + // Assert + var expectedFilterDefinition = new ExpressionFilterDefinition>(keyedFilterExpression); + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + It.Is>>(y => y.EquivalentTo(expectedFilterDefinition)), + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, keyedFilterExpression, keyedFieldExpression, value, partitionKey); + + // Assert + var expectedFilterDefinition = new ExpressionFilterDefinition>(keyedFilterExpression); + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + It.Is>>(y => y.EquivalentTo(expectedFilterDefinition)), + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, keyedFilterExpression, keyedFieldExpression, value, partitionKey, token); + + // Assert + var expectedFilterDefinition = new ExpressionFilterDefinition>(keyedFilterExpression); + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + It.Is>>(y => y.EquivalentTo(expectedFilterDefinition)), + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + #endregion +} diff --git a/CoreUnitTests/CoreUnitTests.csproj b/CoreUnitTests/CoreUnitTests.csproj new file mode 100644 index 0000000..54fd87a --- /dev/null +++ b/CoreUnitTests/CoreUnitTests.csproj @@ -0,0 +1,37 @@ + + + + net6.0 + warnings + + false + true + latest + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/CoreUnitTests/DataAccessTests/MongoDbCreatorTests/AddManyAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbCreatorTests/AddManyAsyncTests.cs new file mode 100644 index 0000000..a310fa4 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbCreatorTests/AddManyAsyncTests.cs @@ -0,0 +1,161 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Create; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbCreatorTests; + +public class AddManyAsyncTests : GenericTestContext +{ + [Fact] + public async Task WithDocuments_AddsMany() + { + // Arrange + var documents = Fixture.CreateMany().ToList(); + var context = MockOf(); + var collection = MockOf>(); + + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + await Sut.AddManyAsync(documents); + + // Assert + collection.Verify( + x => x.InsertManyAsync( + It.Is>(l => l.All(d => documents.Contains(d))), + null, + default), + Times.Once()); + } + + [Fact] + public async Task WithDocumentsHavingNoId_SetsId() + { + // Arrange + var documents = Fixture + .Build>() + .Without(x => x.Id) + .CreateMany() + .ToList(); + var context = MockOf(); + var collection = MockOf>>(); + + context + .Setup(x => x.GetCollection>(It.IsAny())) + .Returns(collection.Object); + + // Act + await Sut.AddManyAsync, string>(documents); + + // Assert + collection.Verify( + x => x.InsertManyAsync( + It.Is>>(l => l.All(d => documents.Contains(d))), + null, + default), + Times.Once()); + + documents.Should().AllSatisfy(d => d.Id.Should().NotBeNull()); + } + + [Fact] + public async Task WithPartitionedDocument_AddsMany() + { + // Arrange + var partitionKey = Fixture.Create(); + var documents = Fixture.Build() + .With(x => x.PartitionKey, partitionKey) + .CreateMany() + .ToList(); + + var context = MockOf(); + var collection = MockOf>(); + + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + await Sut.AddManyAsync(documents); + + // Assert + collection.Verify( + x => x.InsertManyAsync( + It.Is>(l => l.All(d => documents.Contains(d))), + null, + default), + Times.Once()); + + context.Verify(x => x.GetCollection(partitionKey), Times.Once()); + } + + [Fact] + public async Task WithDocumentsAndCancellationToken_AddsMany() + { + // Arrange + var documents = Fixture.CreateMany().ToList(); + var context = MockOf(); + var collection = MockOf>(); + var token = new CancellationToken(true); + + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + await Sut.AddManyAsync(documents, token); + + // Assert + collection.Verify( + x => x.InsertManyAsync( + It.Is>(l => l.All(d => documents.Contains(d))), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithPartitionedDocumentAndCancellationToken_AddsOne() + { + // Arrange + var partitionKey = Fixture.Create(); + var documents = Fixture.Build() + .With(x => x.PartitionKey, partitionKey) + .CreateMany() + .ToList(); + var token = new CancellationToken(true); + + var context = MockOf(); + var collection = MockOf>(); + + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + await Sut.AddManyAsync(documents, token); + + // Assert + collection.Verify( + x => x.InsertManyAsync( + It.Is>(l => l.All(d => documents.Contains(d))), + null, + token), + Times.Once()); + + context.Verify(x => x.GetCollection(partitionKey), Times.Once()); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbCreatorTests/AddManyTests.cs b/CoreUnitTests/DataAccessTests/MongoDbCreatorTests/AddManyTests.cs new file mode 100644 index 0000000..db00290 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbCreatorTests/AddManyTests.cs @@ -0,0 +1,161 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Create; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbCreatorTests; + +public class AddManyTests : GenericTestContext +{ + [Fact] + public void WithDocuments_AddsMany() + { + // Arrange + var documents = Fixture.CreateMany().ToList(); + var context = MockOf(); + var collection = MockOf>(); + + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + Sut.AddMany(documents); + + // Assert + collection.Verify( + x => x.InsertMany( + It.Is>(l => l.All(d => documents.Contains(d))), + null, + default), + Times.Once()); + } + + [Fact] + public void WithDocumentsHavingNoId_SetsId() + { + // Arrange + var documents = Fixture + .Build>() + .Without(x => x.Id) + .CreateMany() + .ToList(); + var context = MockOf(); + var collection = MockOf>>(); + + context + .Setup(x => x.GetCollection>(It.IsAny())) + .Returns(collection.Object); + + // Act + Sut.AddMany, string>(documents); + + // Assert + collection.Verify( + x => x.InsertMany( + It.Is>>(l => l.All(d => documents.Contains(d))), + null, + default), + Times.Once()); + + documents.Should().AllSatisfy(d => d.Id.Should().NotBeNull()); + } + + [Fact] + public void WithPartitionedDocument_AddsMany() + { + // Arrange + var partitionKey = Fixture.Create(); + var documents = Fixture.Build() + .With(x => x.PartitionKey, partitionKey) + .CreateMany() + .ToList(); + + var context = MockOf(); + var collection = MockOf>(); + + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + Sut.AddMany(documents); + + // Assert + collection.Verify( + x => x.InsertMany( + It.Is>(l => l.All(d => documents.Contains(d))), + null, + default), + Times.Once()); + + context.Verify(x => x.GetCollection(partitionKey), Times.Once()); + } + + [Fact] + public void WithDocumentsAndCancellationToken_AddsMany() + { + // Arrange + var documents = Fixture.CreateMany().ToList(); + var context = MockOf(); + var collection = MockOf>(); + var token = new CancellationToken(true); + + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + Sut.AddMany(documents, token); + + // Assert + collection.Verify( + x => x.InsertMany( + It.Is>(l => l.All(d => documents.Contains(d))), + null, + token), + Times.Once()); + } + + [Fact] + public void WithPartitionedDocumentAndCancellationToken_AddsOne() + { + // Arrange + var partitionKey = Fixture.Create(); + var documents = Fixture.Build() + .With(x => x.PartitionKey, partitionKey) + .CreateMany() + .ToList(); + var token = new CancellationToken(true); + + var context = MockOf(); + var collection = MockOf>(); + + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + Sut.AddMany(documents, token); + + // Assert + collection.Verify( + x => x.InsertMany( + It.Is>(l => l.All(d => documents.Contains(d))), + null, + token), + Times.Once()); + + context.Verify(x => x.GetCollection(partitionKey), Times.Once()); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbCreatorTests/AddOneAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbCreatorTests/AddOneAsyncTests.cs new file mode 100644 index 0000000..7589225 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbCreatorTests/AddOneAsyncTests.cs @@ -0,0 +1,153 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Create; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbCreatorTests; + +public class AddOneAsyncTests : GenericTestContext +{ + [Fact] + public async Task WithDocument_AddsOne() + { + // Arrange + var document = new TestDocument(); + var context = MockOf(); + var collection = MockOf>(); + + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + await Sut.AddOneAsync(document); + + // Assert + collection.Verify( + x => x.InsertOneAsync( + It.Is(d => d == document), + null, + default), + Times.Once()); + } + + [Fact] + public async Task WithDocumentHavingNoId_SetsId() + { + // Arrange + var document = new TestDocumentWithKey(); + var context = MockOf(); + var collection = MockOf>>(); + + context + .Setup(x => x.GetCollection>(It.IsAny())) + .Returns(collection.Object); + + // Act + await Sut.AddOneAsync, string>(document); + + // Assert + collection.Verify( + x => x.InsertOneAsync( + It.Is>(d => d == document), + null, + default), + Times.Once()); + + document.Id.Should().NotBeNull(); + } + + [Fact] + public async Task WithPartitionedDocument_AddsOne() + { + // Arrange + var partitionKey = Fixture.Create(); + var document = Fixture.Build() + .With(x => x.PartitionKey, partitionKey) + .Create(); + + var context = MockOf(); + var collection = MockOf>(); + + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + await Sut.AddOneAsync(document); + + // Assert + collection.Verify( + x => x.InsertOneAsync( + It.Is(d => d == document), + null, + default), + Times.Once()); + + context.Verify(x => x.GetCollection(partitionKey), Times.Once()); + } + + [Fact] + public async Task WithDocumentAndCancellationToken_AddsOne() + { + // Arrange + var document = new TestDocument(); + var context = MockOf(); + var collection = MockOf>(); + var token = new CancellationToken(true); + + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + await Sut.AddOneAsync(document, token); + + // Assert + collection.Verify( + x => x.InsertOneAsync( + It.Is(d => d == document), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithPartitionedDocumentAndCancellationToken_AddsOne() + { + // Arrange + var partitionKey = Fixture.Create(); + var document = Fixture.Build() + .With(x => x.PartitionKey, partitionKey) + .Create(); + var token = new CancellationToken(true); + + var context = MockOf(); + var collection = MockOf>(); + + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + await Sut.AddOneAsync(document, token); + + // Assert + collection.Verify( + x => x.InsertOneAsync( + It.Is(d => d == document), + null, + token), + Times.Once()); + + context.Verify(x => x.GetCollection(partitionKey), Times.Once()); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbCreatorTests/AddOneTests.cs b/CoreUnitTests/DataAccessTests/MongoDbCreatorTests/AddOneTests.cs new file mode 100644 index 0000000..9df43d1 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbCreatorTests/AddOneTests.cs @@ -0,0 +1,152 @@ +using System; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Create; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbCreatorTests; + +public class AddOneTests : GenericTestContext +{ + [Fact] + public void WithDocument_AddsOne() + { + // Arrange + var document = new TestDocument(); + var context = MockOf(); + var collection = MockOf>(); + + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + Sut.AddOne(document); + + // Assert + collection.Verify( + x => x.InsertOne( + It.Is(d => d == document), + null, + default), + Times.Once()); + } + + [Fact] + public void WithDocumentHavingNoId_SetsId() + { + // Arrange + var document = new TestDocumentWithKey(); + var context = MockOf(); + var collection = MockOf>>(); + + context + .Setup(x => x.GetCollection>(It.IsAny())) + .Returns(collection.Object); + + // Act + Sut.AddOne, string>(document); + + // Assert + collection.Verify( + x => x.InsertOne( + It.Is>(d => d == document), + null, + default), + Times.Once()); + + document.Id.Should().NotBeNull(); + } + + [Fact] + public void WithPartitionedDocument_AddsOne() + { + // Arrange + var partitionKey = Fixture.Create(); + var document = Fixture.Build() + .With(x => x.PartitionKey, partitionKey) + .Create(); + + var context = MockOf(); + var collection = MockOf>(); + + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + Sut.AddOne(document); + + // Assert + collection.Verify( + x => x.InsertOne( + It.Is(d => d == document), + null, + default), + Times.Once()); + + context.Verify(x => x.GetCollection(partitionKey), Times.Once()); + } + + [Fact] + public void WithDocumentAndCancellationToken_AddsOne() + { + // Arrange + var document = new TestDocument(); + var context = MockOf(); + var collection = MockOf>(); + var token = new CancellationToken(true); + + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + Sut.AddOne(document, token); + + // Assert + collection.Verify( + x => x.InsertOne( + It.Is(d => d == document), + null, + token), + Times.Once()); + } + + [Fact] + public void WithPartitionedDocumentAndCancellationToken_AddsOne() + { + // Arrange + var partitionKey = Fixture.Create(); + var document = Fixture.Build() + .With(x => x.PartitionKey, partitionKey) + .Create(); + var token = new CancellationToken(true); + + var context = MockOf(); + var collection = MockOf>(); + + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + Sut.AddOne(document, token); + + // Assert + collection.Verify( + x => x.InsertOne( + It.Is(d => d == document), + null, + token), + Times.Once()); + + context.Verify(x => x.GetCollection(partitionKey), Times.Once()); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteManyAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteManyAsyncTests.cs new file mode 100644 index 0000000..8dbc7ab --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteManyAsyncTests.cs @@ -0,0 +1,157 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Delete; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbEraserTests; + +public class DeleteManyAsyncTests : GenericTestContext +{ + [Fact] + public async Task WithEmptyDocuments_DeletesNothing() + { + // Arrange + var documents = new List(0); + var token = new CancellationToken(true); + + var collection = MockOf>(); + + var dbContext = MockOf(); + dbContext + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + // Act + var result = await Sut.DeleteManyAsync(documents, token); + + // Assert + result.Should().Be(0); + + var idsToDelete = documents.Select(e => e.Id).ToArray(); + Expression> expectedFilter = x => idsToDelete.Contains(x.Id); + collection.Verify( + x => x.DeleteManyAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + token), + Times.Never); + } + + [Fact] + public async Task WithDocumentsAndCancellationToken_DeletesMany() + { + // Arrange + var count = Fixture.Create(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + + var collection = MockOf>(); + collection + .Setup(x => x.DeleteManyAsync(It.IsAny>(), It.IsAny())) + .ReturnsAsync(new DeleteResult.Acknowledged(count)); + + var dbContext = MockOf(); + dbContext + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + // Act + var result = await Sut.DeleteManyAsync(documents, token); + + // Assert + result.Should().Be(count); + + var idsToDelete = documents.Select(e => e.Id).ToArray(); + Expression> expectedFilter = x => idsToDelete.Contains(x.Id); + collection.Verify( + x => x.DeleteManyAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + token), + Times.Once); + } + + [Fact] + public async Task WithPartitionDocumentsAndCancellationToken_DeletesMany() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var documents = Fixture + .Build() + .With(x => x.PartitionKey, partitionKey) + .CreateMany() + .ToList(); + var token = new CancellationToken(true); + + var collection = MockOf>(); + collection + .Setup(x => x.DeleteManyAsync(It.IsAny>(), It.IsAny())) + .ReturnsAsync(new DeleteResult.Acknowledged(count)); + + var dbContext = MockOf(); + dbContext + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + var result = await Sut.DeleteManyAsync(documents, token); + + // Assert + result.Should().Be(count); + + var idsToDelete = documents.Select(e => e.Id).ToArray(); + Expression> expectedFilter = x => idsToDelete.Contains(x.Id); + collection.Verify( + x => x.DeleteManyAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + token), + Times.Once); + + dbContext.Verify(x => x.GetCollection(partitionKey), Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKeyAndCancellationToken_DeletesOne() + { + // Arrange + var count = Fixture.Create(); + var id = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + var collection = MockOf>(); + collection + .Setup(x => x.DeleteManyAsync(It.IsAny>(), It.IsAny())) + .ReturnsAsync(new DeleteResult.Acknowledged(count)); + + var dbContext = MockOf(); + dbContext + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + Expression> filter = d => d.Id == id; + + // Act + var result = await Sut.DeleteManyAsync(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + collection.Verify( + x => x.DeleteManyAsync( + It.Is>(f => f.EquivalentTo(filter)), + token), + Times.Once); + + dbContext.Verify(x => x.GetCollection(partitionKey), Times.Once); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteManyTests.cs b/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteManyTests.cs new file mode 100644 index 0000000..276dc12 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteManyTests.cs @@ -0,0 +1,157 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Delete; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbEraserTests; + +public class DeleteManyTests : GenericTestContext +{ + [Fact] + public void WithEmptyDocuments_DeletesNothing() + { + // Arrange + var documents = new List(0); + var token = new CancellationToken(true); + + var collection = MockOf>(); + + var dbContext = MockOf(); + dbContext + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + // Act + var result = Sut.DeleteMany(documents, token); + + // Assert + result.Should().Be(0); + + var idsToDelete = documents.Select(e => e.Id).ToArray(); + Expression> expectedFilter = x => idsToDelete.Contains(x.Id); + collection.Verify( + x => x.DeleteMany( + It.Is>(f => f.EquivalentTo(expectedFilter)), + token), + Times.Never); + } + + [Fact] + public void WithDocumentsAndCancellationToken_DeletesMany() + { + // Arrange + var count = Fixture.Create(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + + var collection = MockOf>(); + collection + .Setup(x => x.DeleteMany(It.IsAny>(), It.IsAny())) + .Returns(new DeleteResult.Acknowledged(count)); + + var dbContext = MockOf(); + dbContext + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + // Act + var result = Sut.DeleteMany(documents, token); + + // Assert + result.Should().Be(count); + + var idsToDelete = documents.Select(e => e.Id).ToArray(); + Expression> expectedFilter = x => idsToDelete.Contains(x.Id); + collection.Verify( + x => x.DeleteMany( + It.Is>(f => f.EquivalentTo(expectedFilter)), + token), + Times.Once); + } + + [Fact] + public void WithPartitionDocumentsAndCancellationToken_DeletesMany() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var documents = Fixture + .Build() + .With(x => x.PartitionKey, partitionKey) + .CreateMany() + .ToList(); + var token = new CancellationToken(true); + + var collection = MockOf>(); + collection + .Setup(x => x.DeleteMany(It.IsAny>(), It.IsAny())) + .Returns(new DeleteResult.Acknowledged(count)); + + var dbContext = MockOf(); + dbContext + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + // Act + var result = Sut.DeleteMany(documents, token); + + // Assert + result.Should().Be(count); + + var idsToDelete = documents.Select(e => e.Id).ToArray(); + Expression> expectedFilter = x => idsToDelete.Contains(x.Id); + collection.Verify( + x => x.DeleteMany( + It.Is>(f => f.EquivalentTo(expectedFilter)), + token), + Times.Once); + + dbContext.Verify(x => x.GetCollection(partitionKey), Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKeyAndCancellationToken_DeletesOne() + { + // Arrange + var count = Fixture.Create(); + var id = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + var collection = MockOf>(); + collection + .Setup(x => x.DeleteMany(It.IsAny>(), It.IsAny())) + .Returns(new DeleteResult.Acknowledged(count)); + + var dbContext = MockOf(); + dbContext + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + Expression> filter = d => d.Id == id; + + // Act + var result = Sut.DeleteMany(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + collection.Verify( + x => x.DeleteMany( + It.Is>(f => f.EquivalentTo(filter)), + token), + Times.Once); + + dbContext.Verify(x => x.GetCollection(partitionKey), Times.Once); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteOneAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteOneAsyncTests.cs new file mode 100644 index 0000000..bf1b27d --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteOneAsyncTests.cs @@ -0,0 +1,85 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Delete; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbEraserTests; + +public class DeleteOneAsyncTests : GenericTestContext +{ + [Fact] + public async Task WithDocumentAndCancellationToken_DeletesOne() + { + // Arrange + var count = Fixture.Create(); + var document = Fixture.Create(); + var token = new CancellationToken(true); + + var collection = MockOf>(); + collection + .Setup(x => x.DeleteOneAsync(It.IsAny>(), It.IsAny())) + .ReturnsAsync(new DeleteResult.Acknowledged(count)); + + var dbContext = MockOf(); + dbContext + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + // Act + var result = await Sut.DeleteOneAsync(document, token); + + // Assert + result.Should().Be(count); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + collection.Verify( + x => x.DeleteOneAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + token), + Times.Once()); + } + + [Fact] + public async Task WithFilterAndPartitionKeyAndCancellationToken_DeletesOne() + { + // Arrange + var count = Fixture.Create(); + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + var collection = MockOf>(); + collection + .Setup(x => x.DeleteOneAsync(It.IsAny>(), It.IsAny())) + .ReturnsAsync(new DeleteResult.Acknowledged(count)); + + var dbContext = MockOf(); + dbContext + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + Expression> filter = d => d.Id == document.Id; + + // Act + var result = await Sut.DeleteOneAsync(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + collection.Verify( + x => x.DeleteOneAsync( + It.Is>(f => f.EquivalentTo(filter)), + token), + Times.Once()); + + dbContext.Verify(x => x.GetCollection(partitionKey)); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteOneTests.cs b/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteOneTests.cs new file mode 100644 index 0000000..155a0e5 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteOneTests.cs @@ -0,0 +1,84 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Delete; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbEraserTests; + +public class DeleteOneTests : GenericTestContext +{ + [Fact] + public void WithDocumentAndCancellationToken_DeletesOne() + { + // Arrange + var count = Fixture.Create(); + var document = Fixture.Create(); + var token = new CancellationToken(true); + + var collection = MockOf>(); + collection + .Setup(x => x.DeleteOne(It.IsAny>(), It.IsAny())) + .Returns(new DeleteResult.Acknowledged(count)); + + var dbContext = MockOf(); + dbContext + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + // Act + var result = Sut.DeleteOne(document, token); + + // Assert + result.Should().Be(count); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + collection.Verify( + x => x.DeleteOne( + It.Is>(f => f.EquivalentTo(expectedFilter)), + token), + Times.Once()); + } + + [Fact] + public void WithFilterAndPartitionKeyAndCancellationToken_DeletesOne() + { + // Arrange + var count = Fixture.Create(); + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + var collection = MockOf>(); + collection + .Setup(x => x.DeleteOne(It.IsAny>(), It.IsAny())) + .Returns(new DeleteResult.Acknowledged(count)); + + var dbContext = MockOf(); + dbContext + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + Expression> filter = d => d.Id == document.Id; + + // Act + var result = Sut.DeleteOne(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + collection.Verify( + x => x.DeleteOne( + It.Is>(f => f.EquivalentTo(filter)), + token), + Times.Once()); + + dbContext.Verify(x => x.GetCollection(partitionKey)); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/BaseIndexTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/BaseIndexTests.cs new file mode 100644 index 0000000..ba9ce13 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/BaseIndexTests.cs @@ -0,0 +1,43 @@ +using System.Collections.Generic; +using System.Threading; +using CoreUnitTests.Infrastructure; +using MongoDB.Bson; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Index; +using Moq; + +namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests; + +public class BaseIndexTests : GenericTestContext +{ + protected (Mock>, Mock>) SetupIndexes( + List indexes, + Mock> collection) + { + var asyncCursor = MockOf>(); + var moveNextSequence = asyncCursor + .SetupSequence(x => x.MoveNextAsync(It.IsAny())); + + var currentSequence = asyncCursor + .SetupSequence(x => x.Current); + + foreach (var bsonDocument in indexes) + { + moveNextSequence.ReturnsAsync(true); + currentSequence.Returns(new[] {bsonDocument}); + } + + moveNextSequence.ReturnsAsync(false); + + var indexManager = MockOf>(); + indexManager + .Setup(x => x.ListAsync(It.IsAny())) + .ReturnsAsync(asyncCursor.Object); + + collection + .SetupGet(x => x.Indexes) + .Returns(indexManager.Object); + + return (asyncCursor, indexManager); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateAscendingIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateAscendingIndexAsyncTests.cs new file mode 100644 index 0000000..01f5ebc --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateAscendingIndexAsyncTests.cs @@ -0,0 +1,180 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests; + +public class CreateAscendingIndexAsyncTests : BaseIndexTests +{ + private readonly Expression> fieldExpression = t => t.SomeContent2; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateAscendingIndexAsync(fieldExpression); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>(t => t.Keys.EqualToJson("{\"SomeContent2\":1}")), + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var options = Fixture.Create(); + + SetupContext(collection); + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateAscendingIndexAsync(fieldExpression, options); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":1}") && + t.Options.EqualTo(options)), + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + + var context = SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateAscendingIndexAsync(fieldExpression, partitionKey: partitionKey); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":1}") ), + null, + CancellationToken.None), + Times.Once); + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + + SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateAscendingIndexAsync(fieldExpression, cancellationToken: token); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":1}") ), + null, + token), + Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + var partitionKey = Fixture.Create(); + var options = Fixture.Create(); + + var context = SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateAscendingIndexAsync(fieldExpression, options, partitionKey, token); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":1}") && + t.Options.EqualTo(options)), + null, + token), + Times.Once); + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + } + + private Mock SetupContext(IMock> collection) + { + var context = MockOf(); + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + return context; + } + + private Mock> SetupIndexManager(Mock> collection, string indexName) + { + var indexManager = MockOf>(); + indexManager + .Setup( + x => x.CreateOneAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(indexName); + + collection + .SetupGet(x => x.Indexes) + .Returns(indexManager.Object); + + return indexManager; + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateCombinedTextIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateCombinedTextIndexAsyncTests.cs new file mode 100644 index 0000000..d801586 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateCombinedTextIndexAsyncTests.cs @@ -0,0 +1,190 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; +using Xunit.Abstractions; + +namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests; + +public class CreateCombinedTextIndexAsyncTests : BaseIndexTests +{ + private readonly ITestOutputHelper testOutputHelper; + private readonly List>> fieldExpressions = new() + { + t => t.SomeContent2, + t => t.GroupingKey + }; + + public CreateCombinedTextIndexAsyncTests(ITestOutputHelper testOutputHelper) + => this.testOutputHelper = testOutputHelper; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateCombinedTextIndexAsync(fieldExpressions); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>(t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}", testOutputHelper)), + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var options = Fixture.Create(); + + SetupContext(collection); + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateCombinedTextIndexAsync(fieldExpressions, options); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}") && + t.Options.EqualTo(options)), + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + + var context = SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateCombinedTextIndexAsync(fieldExpressions, partitionKey: partitionKey); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}") ), + null, + CancellationToken.None), + Times.Once); + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + + SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateCombinedTextIndexAsync(fieldExpressions, cancellationToken: token); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}") ), + null, + token), + Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + var partitionKey = Fixture.Create(); + var options = Fixture.Create(); + + var context = SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateCombinedTextIndexAsync(fieldExpressions, options, partitionKey, token); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}") && + t.Options.EqualTo(options)), + null, + token), + Times.Once); + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + } + + private Mock SetupContext(IMock> collection) + { + var context = MockOf(); + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + return context; + } + + private Mock> SetupIndexManager(Mock> collection, string indexName) + { + var indexManager = MockOf>(); + indexManager + .Setup( + x => x.CreateOneAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(indexName); + + collection + .SetupGet(x => x.Indexes) + .Returns(indexManager.Object); + + return indexManager; + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateDescendingIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateDescendingIndexAsyncTests.cs new file mode 100644 index 0000000..8ad433a --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateDescendingIndexAsyncTests.cs @@ -0,0 +1,180 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests; + +public class CreateDescendingIndexAsyncTests : BaseIndexTests +{ + private readonly Expression> fieldExpression = t => t.SomeContent2; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateDescendingIndexAsync(fieldExpression); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>(t => t.Keys.EqualToJson("{\"SomeContent2\":-1}")), + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var options = Fixture.Create(); + + SetupContext(collection); + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateDescendingIndexAsync(fieldExpression, options); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":-1}") && + t.Options.EqualTo(options)), + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + + var context = SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateDescendingIndexAsync(fieldExpression, partitionKey: partitionKey); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":-1}") ), + null, + CancellationToken.None), + Times.Once); + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + + SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateDescendingIndexAsync(fieldExpression, cancellationToken: token); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":-1}") ), + null, + token), + Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + var partitionKey = Fixture.Create(); + var options = Fixture.Create(); + + var context = SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateDescendingIndexAsync(fieldExpression, options, partitionKey, token); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":-1}") && + t.Options.EqualTo(options)), + null, + token), + Times.Once); + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + } + + private Mock SetupContext(IMock> collection) + { + var context = MockOf(); + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + return context; + } + + private Mock> SetupIndexManager(Mock> collection, string indexName) + { + var indexManager = MockOf>(); + indexManager + .Setup( + x => x.CreateOneAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(indexName); + + collection + .SetupGet(x => x.Indexes) + .Returns(indexManager.Object); + + return indexManager; + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateHashedIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateHashedIndexAsyncTests.cs new file mode 100644 index 0000000..74c576d --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateHashedIndexAsyncTests.cs @@ -0,0 +1,185 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; +using Xunit.Abstractions; + +namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests; + +public class CreateHashedIndexAsyncTests : BaseIndexTests +{ + private readonly ITestOutputHelper testOutputHelper; + private readonly Expression> fieldExpression = t => t.SomeContent2; + + public CreateHashedIndexAsyncTests(ITestOutputHelper testOutputHelper) + => this.testOutputHelper = testOutputHelper; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateHashedIndexAsync(fieldExpression); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>(t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}", testOutputHelper)), + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var options = Fixture.Create(); + + SetupContext(collection); + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateHashedIndexAsync(fieldExpression, options); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}") && + t.Options.EqualTo(options)), + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + + var context = SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateHashedIndexAsync(fieldExpression, partitionKey: partitionKey); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}") ), + null, + CancellationToken.None), + Times.Once); + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + + SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateHashedIndexAsync(fieldExpression, cancellationToken: token); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}") ), + null, + token), + Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + var partitionKey = Fixture.Create(); + var options = Fixture.Create(); + + var context = SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateHashedIndexAsync(fieldExpression, options, partitionKey, token); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}") && + t.Options.EqualTo(options)), + null, + token), + Times.Once); + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + } + + private Mock SetupContext(IMock> collection) + { + var context = MockOf(); + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + return context; + } + + private Mock> SetupIndexManager(Mock> collection, string indexName) + { + var indexManager = MockOf>(); + indexManager + .Setup( + x => x.CreateOneAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(indexName); + + collection + .SetupGet(x => x.Indexes) + .Returns(indexManager.Object); + + return indexManager; + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateTextIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateTextIndexAsyncTests.cs new file mode 100644 index 0000000..3c482dd --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateTextIndexAsyncTests.cs @@ -0,0 +1,180 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests; + +public class CreateTextIndexAsyncTests : BaseIndexTests +{ + private readonly Expression> fieldExpression = t => t.SomeContent2; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateTextIndexAsync(fieldExpression); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>(t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\"}")), + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var options = Fixture.Create(); + + SetupContext(collection); + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateTextIndexAsync(fieldExpression, options); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\"}") && + t.Options.EqualTo(options)), + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + + var context = SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateTextIndexAsync(fieldExpression, partitionKey: partitionKey); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\"}") ), + null, + CancellationToken.None), + Times.Once); + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + + SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateTextIndexAsync(fieldExpression, cancellationToken: token); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\"}") ), + null, + token), + Times.Once); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + var partitionKey = Fixture.Create(); + var options = Fixture.Create(); + + var context = SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + var result = await Sut.CreateTextIndexAsync(fieldExpression, options, partitionKey, token); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\"}") && + t.Options.EqualTo(options)), + null, + token), + Times.Once); + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + } + + private Mock SetupContext(Mock> collection) + { + var context = MockOf(); + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + return context; + } + + private Mock> SetupIndexManager(Mock> collection, string indexName) + { + var indexManager = MockOf>(); + indexManager + .Setup( + x => x.CreateOneAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(indexName); + + collection + .SetupGet(x => x.Indexes) + .Returns(indexManager.Object); + + return indexManager; + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/DropIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/DropIndexAsyncTests.cs new file mode 100644 index 0000000..b8b30ac --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/DropIndexAsyncTests.cs @@ -0,0 +1,119 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests; + +public class DropIndexAsyncTests : BaseIndexTests +{ + [Fact] + public async Task WithIndexName_DropsIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + await Sut.DropIndexAsync(expectedIndexName); + + // Assert + indexManger.Verify(x => x.DropOneAsync(expectedIndexName, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithIndexNameAndPartitionKey_DropsIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + + var context = SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + await Sut.DropIndexAsync(expectedIndexName, partitionKey); + + // Assert + indexManger.Verify(x => x.DropOneAsync(expectedIndexName, CancellationToken.None), Times.Once); + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + } + + [Fact] + public async Task WithIndexNameAndCancellationToken_DropsIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + + SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + await Sut.DropIndexAsync(expectedIndexName, cancellationToken: token); + + // Assert + indexManger.Verify(x => x.DropOneAsync(expectedIndexName, token), Times.Once); + } + + [Fact] + public async Task WithIndexNameAndPartitionKeyAndCancellationToken_DropsIndex() + { + // Arrange + var expectedIndexName = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + var partitionKey = Fixture.Create(); + + var context = SetupContext(collection); + + var indexManger = SetupIndexManager(collection, expectedIndexName); + + // Act + await Sut.DropIndexAsync(expectedIndexName, partitionKey, token); + + // Assert + indexManger.Verify(x => x.DropOneAsync(expectedIndexName, token), Times.Once); + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + } + + private Mock SetupContext(IMock> collection) + { + var context = MockOf(); + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + return context; + } + + private Mock> SetupIndexManager(Mock> collection, string indexName) + { + var indexManager = MockOf>(); + indexManager + .Setup( + x => x.CreateOneAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(indexName); + + collection + .SetupGet(x => x.Indexes) + .Returns(indexManager.Object); + + return indexManager; + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/GetIndexNamesAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/GetIndexNamesAsyncTests.cs new file mode 100644 index 0000000..45e8b56 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/GetIndexNamesAsyncTests.cs @@ -0,0 +1,129 @@ +using System; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests; + +public class GetIndexNamesAsyncTests : BaseIndexTests +{ + [Fact] + public async Task WithNoParameters_ReturnsAllIndexNames() + { + // Arrange + var indexNames = Fixture.CreateMany().ToList(); + var indexes = indexNames.Select(x => new BsonDocument {{"name", x}}).ToList(); + + var collection = MockOf>(); + + var context = MockOf(); + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + var (cursor, manager) = SetupIndexes(indexes, collection); + + // Act + var result = await Sut.GetIndexesNamesAsync(); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(indexNames); + context.Verify(x => x.GetCollection(null)); + manager.Verify(x => x.ListAsync(CancellationToken.None)); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None)); + } + + [Fact] + public async Task WithPartitionKey_ReturnsAllIndexNames() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexNames = Fixture.CreateMany().ToList(); + var indexes = indexNames.Select(x => new BsonDocument {{"name", x}}).ToList(); + + var collection = MockOf>(); + + var context = MockOf(); + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + var (cursor, manager) = SetupIndexes(indexes, collection); + + // Act + var result = await Sut.GetIndexesNamesAsync(partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(indexNames); + context.Verify(x => x.GetCollection(partitionKey)); + manager.Verify(x => x.ListAsync(CancellationToken.None)); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None)); + } + + [Fact] + public async Task WithCancellationToken_ReturnsAllIndexNames() + { + // Arrange + var token = new CancellationToken(); + var indexNames = Fixture.CreateMany().ToList(); + var indexes = indexNames.Select(x => new BsonDocument {{"name", x}}).ToList(); + + var collection = MockOf>(); + + var context = MockOf(); + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + var (cursor, manager) = SetupIndexes(indexes, collection); + + // Act + var result = await Sut.GetIndexesNamesAsync(cancellationToken:token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(indexNames); + context.Verify(x => x.GetCollection(null)); + manager.Verify(x => x.ListAsync(token)); + cursor.Verify(x => x.MoveNextAsync(token)); + } + + [Fact] + public async Task WithPartitionKeyCancellationToken_ReturnsAllIndexNames() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(); + var indexNames = Fixture.CreateMany().ToList(); + var indexes = indexNames.Select(x => new BsonDocument {{"name", x}}).ToList(); + + var collection = MockOf>(); + + var context = MockOf(); + context + .Setup(x => x.GetCollection(It.IsAny())) + .Returns(collection.Object); + + var (cursor, manager) = SetupIndexes(indexes, collection); + + // Act + var result = await Sut.GetIndexesNamesAsync(partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(indexNames); + context.Verify(x => x.GetCollection(partitionKey)); + manager.Verify(x => x.ListAsync(token)); + cursor.Verify(x => x.MoveNextAsync(token)); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/AnyAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/AnyAsyncTests.cs new file mode 100644 index 0000000..a1e38a3 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/AnyAsyncTests.cs @@ -0,0 +1,282 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class AnyAsyncTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + + [Fact] + public async Task WithFilter_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.AnyAsync(filter); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(It.Is>(y => y.Expression == filter), null, CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.AnyAsync(filter, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(It.Is>(y => y.Expression == filter), null, token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterAndPartitionKey_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.AnyAsync(filter, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(It.Is>(y => y.Expression == filter), null, CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterAndPartitionKeyAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.AnyAsync(filter, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(It.Is>(y => y.Expression == filter), null, token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithCondition_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.AnyAsync(condition); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(condition, null, CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithConditionAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.AnyAsync(condition, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(condition, null, token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithConditionAndPartitionKey_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.AnyAsync(condition, partitionKey: partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(condition, null, CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithConditionAndPartitionKeyAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.AnyAsync(condition, partitionKey: partitionKey, cancellationToken:token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(condition, null, token), + Times.Once); + result.Should().BeTrue(); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/AnyTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/AnyTests.cs new file mode 100644 index 0000000..a58acaa --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/AnyTests.cs @@ -0,0 +1,281 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class AnyTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + + [Fact] + public void WithFilter_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Any(filter); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocuments(It.Is>(y => y.Expression == filter), null, CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Any(filter, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocuments(It.Is>(y => y.Expression == filter), null, token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterAndPartitionKey_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Any(filter, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocuments(It.Is>(y => y.Expression == filter), null, CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterAndPartitionKeyAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Any(filter, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocuments(It.Is>(y => y.Expression == filter), null, token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithCondition_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Any(condition); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocuments(condition, null, CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithConditionAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Any(condition, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocuments(condition, null, token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithConditionAndPartitionKey_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Any(condition, partitionKey: partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocuments(condition, null, CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithConditionAndPartitionKeyAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Any(condition, partitionKey: partitionKey, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocuments(condition, null, token), + Times.Once); + result.Should().BeTrue(); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/BaseReaderTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/BaseReaderTests.cs new file mode 100644 index 0000000..e4057fa --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/BaseReaderTests.cs @@ -0,0 +1,69 @@ +using System.Collections.Generic; +using System.Threading; +using CoreUnitTests.Infrastructure; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class BaseReaderTests : GenericTestContext +{ + protected Mock> SetupSyncCursor(List documents) + { + var asyncCursor = MockOf>(); + + var moveNextSequence = asyncCursor + .SetupSequence(x => x.MoveNext(It.IsAny())); + + var currentSequence = asyncCursor + .SetupSequence(x => x.Current); + + foreach (var projection in documents) + { + moveNextSequence.Returns(true); + currentSequence.Returns(new[] {projection}); + } + + moveNextSequence.Returns(false); + return asyncCursor; + } + + protected Mock> SetupAsyncCursor(List documents) + { + var asyncCursor = MockOf>(); + + var moveNextSequence = asyncCursor + .SetupSequence(x => x.MoveNextAsync(It.IsAny())); + + var currentSequence = asyncCursor + .SetupSequence(x => x.Current); + + foreach (var projection in documents) + { + moveNextSequence.ReturnsAsync(true); + currentSequence.Returns(new[] {projection}); + } + + moveNextSequence.ReturnsAsync(false); + return asyncCursor; + } + + protected static void SetupFindAsync(Mock> collection, Mock> asyncCursor) => + collection + .Setup( + x => x.FindAsync( + It.IsAny>(), + It.IsAny>(), + It.IsAny())) + .ReturnsAsync(asyncCursor.Object); + + protected static void SetupFindSync(Mock> collection, Mock> asyncCursor) => + collection + .Setup( + x => x.FindSync( + It.IsAny>(), + It.IsAny>(), + It.IsAny())) + .Returns(asyncCursor.Object); +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/CountAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/CountAsyncTests.cs new file mode 100644 index 0000000..22e22ac --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/CountAsyncTests.cs @@ -0,0 +1,282 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class CountAsyncTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + + [Fact] + public async Task WithFilter_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.CountAsync(filter); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(It.Is>(y => y.Expression == filter), null, CancellationToken.None), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public async Task WithFilterAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.CountAsync(filter, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(It.Is>(y => y.Expression == filter), null, token), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public async Task WithFilterAndPartitionKey_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.CountAsync(filter, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(It.Is>(y => y.Expression == filter), null, CancellationToken.None), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public async Task WithFilterAndPartitionKeyAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.CountAsync(filter, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(It.Is>(y => y.Expression == filter), null, token), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public async Task WithCondition_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.CountAsync(condition); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(condition, null, CancellationToken.None), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public async Task WithConditionAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.CountAsync(condition, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(condition, null, token), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public async Task WithConditionAndPartitionKey_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.CountAsync(condition, partitionKey: partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(condition, null, CancellationToken.None), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public async Task WithConditionAndPartitionKeyAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.CountAsync(condition, partitionKey: partitionKey, cancellationToken:token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(condition, null, token), + Times.Once); + result.Should().Be(documents.Count); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/CountTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/CountTests.cs new file mode 100644 index 0000000..bdcdba6 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/CountTests.cs @@ -0,0 +1,281 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class CountTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + + [Fact] + public void WithFilter_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Count(filter); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocuments(It.Is>(y => y.Expression == filter), It.IsAny(), CancellationToken.None), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public void WithFilterAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Count(filter, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocuments(It.Is>(y => y.Expression == filter), It.IsAny(), token), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public void WithFilterAndPartitionKey_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Count(filter, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocuments(It.Is>(y => y.Expression == filter), It.IsAny(), CancellationToken.None), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public void WithFilterAndPartitionKeyAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Count(filter, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocuments(It.Is>(y => y.Expression == filter), It.IsAny(), token), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public void WithCondition_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Count(condition); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocuments(condition, null, CancellationToken.None), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public void WithConditionAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Count(condition, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocuments(condition, null, token), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public void WithConditionAndPartitionKey_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Count(condition, partitionKey: partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocuments(condition, null, CancellationToken.None), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public void WithConditionAndPartitionKeyAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Count(condition, partitionKey: partitionKey, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocuments(condition, null, token), + Times.Once); + result.Should().Be(documents.Count); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllAsyncTests.cs new file mode 100644 index 0000000..db168c5 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllAsyncTests.cs @@ -0,0 +1,283 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetAllAsyncTests : BaseReaderTests +{ + [Fact] + public async Task WithFilter_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + Expression> filter = x => x.Id == documents[0].Id; + var (context, cursor) = SetupAsyncGet(documents, collection); + + + // Act + var result = await Sut.GetAllAsync(filter); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public async Task WithFilterAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(false); + Expression> filter = x => x.Id == documents[0].Id; + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = await Sut.GetAllAsync(filter, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public async Task WithFilterAndPartitionKey_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + Expression> filter = x => x.Id == documents[0].Id; + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = await Sut.GetAllAsync(filter, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public async Task WithFilterAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(false); + Expression> filter = x => x.Id == documents[0].Id; + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = await Sut.GetAllAsync(filter, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public async Task WithCondition_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = await Sut.GetAllAsync(condition); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public async Task WithConditionAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(false); + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = await Sut.GetAllAsync(condition, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public async Task WithConditionAndPartitionKey_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = await Sut.GetAllAsync(condition, partitionKey: partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1)); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public async Task WithConditionAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(false); + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = await Sut.GetAllAsync(condition, partitionKey: partitionKey, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public async Task WithConditionAndFindOptions_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var options = Fixture + .Build() + .Without(x => x.Comment) + .Without(x => x.Hint) + .Create(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = await Sut.GetAllAsync(condition, options); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public async Task WithConditionAndFindOptionsAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var options = Fixture + .Build() + .Without(x => x.Comment) + .Without(x => x.Hint) + .Create(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(false); + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = await Sut.GetAllAsync(condition, options, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public async Task WithConditionAndFindOptionsAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var options = Fixture + .Build() + .Without(x => x.Comment) + .Without(x => x.Hint) + .Create(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(false); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = await Sut.GetAllAsync(condition, options, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + private (Mock, Mock>) SetupAsyncGet( + List documents, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupAsyncCursor(documents); + + SetupFindAsync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllTests.cs new file mode 100644 index 0000000..2d679ab --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllTests.cs @@ -0,0 +1,282 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetAllTests : BaseReaderTests +{ + [Fact] + public void WithFilter_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + Expression> filter = x => x.Id == documents[0].Id; + var (context, cursor) = SetupGet(documents, collection); + + + // Act + var result = Sut.GetAll(filter); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public void WithFilterAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(false); + Expression> filter = x => x.Id == documents[0].Id; + var (context, cursor) = SetupGet(documents, collection); + + // Act + var result = Sut.GetAll(filter, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public void WithFilterAndPartitionKey_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + Expression> filter = x => x.Id == documents[0].Id; + var (context, cursor) = SetupGet(documents, collection, partitionKey); + + // Act + var result = Sut.GetAll(filter, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public void WithFilterAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(false); + Expression> filter = x => x.Id == documents[0].Id; + var (context, cursor) = SetupGet(documents, collection, partitionKey); + + // Act + var result = Sut.GetAll(filter, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public void WithCondition_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var (context, cursor) = SetupGet(documents, collection); + + // Act + var result = Sut.GetAll(condition); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public void WithConditionAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(false); + var (context, cursor) = SetupGet(documents, collection); + + // Act + var result = Sut.GetAll(condition, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public void WithConditionAndPartitionKey_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupGet(documents, collection, partitionKey); + + // Act + var result = Sut.GetAll(condition, partitionKey: partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1)); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public void WithConditionAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(false); + var (context, cursor) = SetupGet(documents, collection, partitionKey); + + // Act + var result = Sut.GetAll(condition, partitionKey: partitionKey, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public void WithConditionAndFindOptions_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var options = Fixture + .Build() + .Without(x => x.Comment) + .Without(x => x.Hint) + .Create(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var (context, cursor) = SetupGet(documents, collection); + + // Act + var result = Sut.GetAll(condition, options); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public void WithConditionAndFindOptionsAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var options = Fixture + .Build() + .Without(x => x.Comment) + .Without(x => x.Hint) + .Create(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(false); + var (context, cursor) = SetupGet(documents, collection); + + // Act + var result = Sut.GetAll(condition, options, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + [Fact] + public void WithConditionAndFindOptionsAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var options = Fixture + .Build() + .Without(x => x.Comment) + .Without(x => x.Hint) + .Create(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(false); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupGet(documents, collection, partitionKey); + + // Act + var result = Sut.GetAll(condition, options, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Exactly(documents.Count)); + cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => documents.Contains(x)); + } + + private (Mock, Mock>) SetupGet( + List documents, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupSyncCursor(documents); + + SetupFindSync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByIdAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByIdAsyncTests.cs new file mode 100644 index 0000000..d9af227 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByIdAsyncTests.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetByIdAsyncTests : BaseReaderTests +{ + [Fact] + public async Task WithId_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = await Sut.GetByIdAsync(documents[0].Id); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithIdAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = await Sut.GetByIdAsync(documents[0].Id, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithIdAndPartitionKey_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = await Sut.GetByIdAsync(documents[0].Id, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithIdAndPartitionKeyAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = await Sut.GetByIdAsync(documents[0].Id, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + private (Mock, Mock>) SetupAsyncGet( + List documents, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupAsyncCursor(documents); + + SetupFindAsync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByIdTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByIdTests.cs new file mode 100644 index 0000000..7d3da07 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByIdTests.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetByIdTests : BaseReaderTests +{ + [Fact] + public void WithId_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = Sut.GetById(documents[0].Id); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithIdAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = Sut.GetById(documents[0].Id, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithIdAndPartitionKey_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = Sut.GetById(documents[0].Id, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithIdAndPartitionKeyAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = Sut.GetById(documents[0].Id, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + private (Mock, Mock>) SetupAsyncGet( + List documents, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupSyncCursor(documents); + + SetupFindSync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxAsyncTests.cs new file mode 100644 index 0000000..000ea7b --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxAsyncTests.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetByMaxAsyncTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + private readonly Expression> selector = x => x.SomeValue; + + [Fact] + public async Task WithFilterAndSelector_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = await Sut.GetByMaxAsync(filter, selector); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = await Sut.GetByMaxAsync(filter, selector, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = await Sut.GetByMaxAsync(filter, selector, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = await Sut.GetByMaxAsync(filter, selector, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + private (Mock, Mock>) SetupAsyncGet( + List documents, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupAsyncCursor(documents); + + SetupFindAsync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxTests.cs new file mode 100644 index 0000000..500afc2 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxTests.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetByMaxTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + private readonly Expression> selector = x => x.SomeValue; + + [Fact] + public void WithFilterAndSelector_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var (context, cursor) = SetupSyncGet(documents, collection); + + // Act + var result = Sut.GetByMax(filter, selector); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + var (context, cursor) = SetupSyncGet(documents, collection); + + // Act + var result = Sut.GetByMax(filter, selector, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupSyncGet(documents, collection, partitionKey); + + // Act + var result = Sut.GetByMax(filter, selector, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupSyncGet(documents, collection, partitionKey); + + // Act + var result = Sut.GetByMax(filter, selector, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + private (Mock, Mock>) SetupSyncGet( + List documents, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupSyncCursor(documents); + + SetupFindSync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinAsyncTests.cs new file mode 100644 index 0000000..5521897 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinAsyncTests.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetByMinAsyncTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + private readonly Expression> selector = x => x.SomeValue; + + [Fact] + public async Task WithFilterAndSelector_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = await Sut.GetByMinAsync(filter, selector); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = await Sut.GetByMinAsync(filter, selector, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = await Sut.GetByMinAsync(filter, selector, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = await Sut.GetByMinAsync(filter, selector, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + private (Mock, Mock>) SetupAsyncGet( + List documents, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupAsyncCursor(documents); + + SetupFindAsync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinTests.cs new file mode 100644 index 0000000..0cd9d9c --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinTests.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetByMinTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + private readonly Expression> selector = x => x.SomeValue; + + [Fact] + public void WithFilterAndSelector_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var (context, cursor) = SetupSyncGet(documents, collection); + + // Act + var result = Sut.GetByMin(filter, selector); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + var (context, cursor) = SetupSyncGet(documents, collection); + + // Act + var result = Sut.GetByMin(filter, selector, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupSyncGet(documents, collection, partitionKey); + + // Act + var result = Sut.GetByMin(filter, selector, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupSyncGet(documents, collection, partitionKey); + + // Act + var result = Sut.GetByMin(filter, selector, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + private (Mock, Mock>) SetupSyncGet( + List documents, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupSyncCursor(documents); + + SetupFindSync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMaxValueAsyncTests .cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMaxValueAsyncTests .cs new file mode 100644 index 0000000..807a1b3 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMaxValueAsyncTests .cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetMaxValueAsyncTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + private readonly Expression> selector = x => x.SomeValue; + + [Fact] + public async Task WithFilterAndSelector_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var (context, cursor) = SetupAsyncGet(value, collection); + + // Act + var result = await Sut.GetMaxValueAsync(filter, selector); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().Be(value); + } + + [Fact] + public async Task WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(value, collection); + + // Act + var result = await Sut.GetMaxValueAsync(filter, selector, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().Be(value); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupAsyncGet(value, collection, partitionKey); + + // Act + var result = await Sut.GetMaxValueAsync(filter, selector, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().Be(value); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(value, collection, partitionKey); + + // Act + var result = await Sut.GetMaxValueAsync(filter, selector, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().Be(value); + } + + private (Mock, Mock>) SetupAsyncGet( + TValue result, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupAsyncCursor(new List {result}); + + SetupFindAsync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMaxValueTests .cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMaxValueTests .cs new file mode 100644 index 0000000..414d605 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMaxValueTests .cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetMaxValueTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + private readonly Expression> selector = x => x.SomeValue; + + [Fact] + public void WithFilterAndSelector_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var (context, cursor) = SetupSyncGet(value, collection); + + // Act + var result = Sut.GetMaxValue(filter, selector); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().Be(value); + } + + [Fact] + public void WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + var (context, cursor) = SetupSyncGet(value, collection); + + // Act + var result = Sut.GetMaxValue(filter, selector, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().Be(value); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupSyncGet(value, collection, partitionKey); + + // Act + var result = Sut.GetMaxValue(filter, selector, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().Be(value); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupSyncGet(value, collection, partitionKey); + + // Act + var result = Sut.GetMaxValue(filter, selector, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().Be(value); + } + + private (Mock, Mock>) SetupSyncGet( + TValue result, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupSyncCursor(new List {result}); + + SetupFindSync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMinValueAsyncTests .cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMinValueAsyncTests .cs new file mode 100644 index 0000000..cf81ed1 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMinValueAsyncTests .cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetMinValueAsyncTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + private readonly Expression> selector = x => x.SomeValue; + + [Fact] + public async Task WithFilterAndSelector_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var (context, cursor) = SetupAsyncGet(value, collection); + + // Act + var result = await Sut.GetMinValueAsync(filter, selector); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().Be(value); + } + + [Fact] + public async Task WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(value, collection); + + // Act + var result = await Sut.GetMinValueAsync(filter, selector, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().Be(value); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupAsyncGet(value, collection, partitionKey); + + // Act + var result = await Sut.GetMinValueAsync(filter, selector, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().Be(value); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(value, collection, partitionKey); + + // Act + var result = await Sut.GetMinValueAsync(filter, selector, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().Be(value); + } + + private (Mock, Mock>) SetupAsyncGet( + TValue result, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupAsyncCursor(new List {result}); + + SetupFindAsync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMinValueTests .cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMinValueTests .cs new file mode 100644 index 0000000..39a12b6 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMinValueTests .cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetMinValueTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + private readonly Expression> selector = x => x.SomeValue; + + [Fact] + public void WithFilterAndSelector_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var (context, cursor) = SetupSyncGet(value, collection); + + // Act + var result = Sut.GetMinValue(filter, selector); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().Be(value); + } + + [Fact] + public void WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + var (context, cursor) = SetupSyncGet(value, collection); + + // Act + var result = Sut.GetMinValue(filter, selector, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().Be(value); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupSyncGet(value, collection, partitionKey); + + // Act + var result = Sut.GetMinValue(filter, selector, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().Be(value); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupSyncGet(value, collection, partitionKey); + + // Act + var result = Sut.GetMinValue(filter, selector, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().Be(value); + } + + private (Mock, Mock>) SetupSyncGet( + TValue result, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupSyncCursor(new List {result}); + + SetupFindSync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetOneAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetOneAsyncTests.cs new file mode 100644 index 0000000..51818de --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetOneAsyncTests.cs @@ -0,0 +1,283 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetOneAsyncTests : BaseReaderTests +{ + [Fact] + public async Task WithFilter_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + Expression> filter = x => x.Id == documents[0].Id; + var (context, cursor) = SetupAsyncGet(documents, collection); + + + // Act + var result = await Sut.GetOneAsync(filter); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithFilterAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + Expression> filter = x => x.Id == documents[0].Id; + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = await Sut.GetOneAsync(filter, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithFilterAndPartitionKey_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + Expression> filter = x => x.Id == documents[0].Id; + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = await Sut.GetOneAsync(filter, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithFilterAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Expression> filter = x => x.Id == documents[0].Id; + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = await Sut.GetOneAsync(filter, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithCondition_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = await Sut.GetOneAsync(condition); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithConditionAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = await Sut.GetOneAsync(condition, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithConditionAndPartitionKey_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = await Sut.GetOneAsync(condition, partitionKey: partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithConditionAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = await Sut.GetOneAsync(condition, partitionKey: partitionKey, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithConditionAndFindOptions_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var options = Fixture + .Build() + .Without(x => x.Comment) + .Without(x => x.Hint) + .Create(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = await Sut.GetOneAsync(condition, options); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithConditionAndFindOptionsAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var options = Fixture + .Build() + .Without(x => x.Comment) + .Without(x => x.Hint) + .Create(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(documents, collection); + + // Act + var result = await Sut.GetOneAsync(condition, options, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public async Task WithConditionAndFindOptionsAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var options = Fixture + .Build() + .Without(x => x.Comment) + .Without(x => x.Hint) + .Create(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(true); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + + // Act + var result = await Sut.GetOneAsync(condition, options, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + private (Mock, Mock>) SetupAsyncGet( + List documents, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupAsyncCursor(documents); + + SetupFindAsync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetOneTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetOneTests.cs new file mode 100644 index 0000000..84944e6 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetOneTests.cs @@ -0,0 +1,282 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetOneTests : BaseReaderTests +{ + [Fact] + public void WithFilter_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + Expression> filter = x => x.Id == documents[0].Id; + var (context, cursor) = SetupSyncGet(documents, collection); + + + // Act + var result = Sut.GetOne(filter); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithFilterAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + Expression> filter = x => x.Id == documents[0].Id; + var (context, cursor) = SetupSyncGet(documents, collection); + + // Act + var result = Sut.GetOne(filter, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithFilterAndPartitionKey_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + Expression> filter = x => x.Id == documents[0].Id; + var (context, cursor) = SetupSyncGet(documents, collection, partitionKey); + + // Act + var result = Sut.GetOne(filter, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithFilterAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Expression> filter = x => x.Id == documents[0].Id; + var (context, cursor) = SetupSyncGet(documents, collection, partitionKey); + + // Act + var result = Sut.GetOne(filter, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithCondition_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var (context, cursor) = SetupSyncGet(documents, collection); + + // Act + var result = Sut.GetOne(condition); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithConditionAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(true); + var (context, cursor) = SetupSyncGet(documents, collection); + + // Act + var result = Sut.GetOne(condition, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithConditionAndPartitionKey_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupSyncGet(documents, collection, partitionKey); + + // Act + var result = Sut.GetOne(condition, partitionKey: partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithConditionAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupSyncGet(documents, collection, partitionKey); + + // Act + var result = Sut.GetOne(condition, partitionKey: partitionKey, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithConditionAndFindOptions_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var options = Fixture + .Build() + .Without(x => x.Comment) + .Without(x => x.Hint) + .Create(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var (context, cursor) = SetupSyncGet(documents, collection); + + // Act + var result = Sut.GetOne(condition, options); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithConditionAndFindOptionsAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var options = Fixture + .Build() + .Without(x => x.Comment) + .Without(x => x.Hint) + .Create(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(true); + var (context, cursor) = SetupSyncGet(documents, collection); + + // Act + var result = Sut.GetOne(condition, options, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + [Fact] + public void WithConditionAndFindOptionsAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var options = Fixture + .Build() + .Without(x => x.Comment) + .Without(x => x.Hint) + .Create(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(true); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupSyncGet(documents, collection, partitionKey); + + // Act + var result = Sut.GetOne(condition, options, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(documents[0]); + } + + private (Mock, Mock>) SetupSyncGet( + List documents, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupSyncCursor(documents); + + SetupFindSync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/ProjectManyAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/ProjectManyAsyncTests.cs new file mode 100644 index 0000000..a4633bd --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/ProjectManyAsyncTests.cs @@ -0,0 +1,123 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class ProjectManyAsyncTests : BaseReaderTests +{ + private readonly Expression> filter = t => string.IsNullOrWhiteSpace(t.SomeContent2); + + private readonly Expression> + projectionExpression = t => new TestProjection {TestDocumentId = t.Id, NestedData = t.Nested.SomeDate}; + + [Fact] + public async Task WithFilterAndProjection_Projects() + { + // Arrange + var collection = MockOf>(); + var projections = Fixture.CreateMany().ToList(); + var (context, cursor) = SetupAsyncProjection(projections, collection); + + // Act + var result = await Sut.ProjectManyAsync(filter, projectionExpression); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.AtLeast(1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => projections.Contains(x)); + } + + [Fact] + public async Task WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var collection = MockOf>(); + var projections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(false); + var (context, cursor) = SetupAsyncProjection(projections, collection); + + // Act + var result = await Sut.ProjectManyAsync(filter, projectionExpression, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.AtLeast(1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => projections.Contains(x)); + } + + [Fact] + public async Task WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var collection = MockOf>(); + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey); + + // Act + var result = await Sut.ProjectManyAsync(filter, projectionExpression, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.AtLeast(1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => projections.Contains(x)); + } + + [Fact] + public async Task WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var collection = MockOf>(); + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(false); + var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey); + + // Act + var result = await Sut.ProjectManyAsync(filter, projectionExpression, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.AtLeast(1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => projections.Contains(x)); + } + + private (Mock, Mock>) SetupAsyncProjection( + List projections, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupAsyncCursor(projections); + + collection + .Setup( + x => x.FindAsync( + It.IsAny>(), + It.IsAny>(), + It.IsAny())) + .ReturnsAsync(asyncCursor.Object); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/ProjectManyTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/ProjectManyTests.cs new file mode 100644 index 0000000..c5f0aea --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/ProjectManyTests.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class ProjectManyTests : BaseReaderTests +{ + private readonly Expression> filter = t => string.IsNullOrWhiteSpace(t.SomeContent2); + + private readonly Expression> + projectionExpression = t => new TestProjection {TestDocumentId = t.Id, NestedData = t.Nested.SomeDate}; + + [Fact] + public void WithFilterAndProjection_Projects() + { + // Arrange + var collection = MockOf>(); + var projections = Fixture.CreateMany().ToList(); + var (context, cursor) = SetupAsyncProjection(projections, collection); + + // Act + var result = Sut.ProjectMany(filter, projectionExpression); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.AtLeast(1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => projections.Contains(x)); + } + + [Fact] + public void WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var collection = MockOf>(); + var projections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(false); + var (context, cursor) = SetupAsyncProjection(projections, collection); + + // Act + var result = Sut.ProjectMany(filter, projectionExpression, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.AtLeast(1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => projections.Contains(x)); + } + + [Fact] + public void WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var collection = MockOf>(); + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey); + + // Act + var result = Sut.ProjectMany(filter, projectionExpression, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.AtLeast(1)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => projections.Contains(x)); + } + + [Fact] + public void WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var collection = MockOf>(); + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(false); + var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey); + + // Act + var result = Sut.ProjectMany(filter, projectionExpression, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.AtLeast(projections.Count)); + result.Should().NotBeNull(); + result.Should().OnlyContain(x => projections.Contains(x)); + } + + private (Mock, Mock>) SetupAsyncProjection( + List projections, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupSyncCursor(projections); + + SetupFindSync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/ProjectOneAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/ProjectOneAsyncTests.cs new file mode 100644 index 0000000..7c21b07 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/ProjectOneAsyncTests.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class ProjectOneAsyncTests : BaseReaderTests +{ + private readonly Expression> filter = t => string.IsNullOrWhiteSpace(t.SomeContent2); + + private readonly Expression> + projectionExpression = t => new TestProjection {TestDocumentId = t.Id, NestedData = t.Nested.SomeDate}; + + [Fact] + public async Task WithFilterAndProjection_Projects() + { + // Arrange + var collection = MockOf>(); + var projections = Fixture.CreateMany().ToList(); + var (context, cursor) = SetupAsyncProjection(projections, collection); + + // Act + var result = await Sut.ProjectOneAsync(filter, projectionExpression); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(projections[0]); + } + + [Fact] + public async Task WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var collection = MockOf>(); + var projections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncProjection(projections, collection); + + // Act + var result = await Sut.ProjectOneAsync(filter, projectionExpression, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(projections[0]); + } + + [Fact] + public async Task WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var collection = MockOf>(); + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey); + + // Act + var result = await Sut.ProjectOneAsync(filter, projectionExpression, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(projections[0]); + } + + [Fact] + public async Task WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var collection = MockOf>(); + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey); + + // Act + var result = await Sut.ProjectOneAsync(filter, projectionExpression, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(projections[0]); + } + + private (Mock, Mock>) SetupAsyncProjection( + List projections, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupAsyncCursor(projections); + + SetupFindAsync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/ProjectOneTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/ProjectOneTests.cs new file mode 100644 index 0000000..eabc833 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/ProjectOneTests.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class ProjectOneTests : BaseReaderTests +{ + private readonly Expression> filter = t => string.IsNullOrWhiteSpace(t.SomeContent2); + + private readonly Expression> + projectionExpression = t => new TestProjection {TestDocumentId = t.Id, NestedData = t.Nested.SomeDate}; + + [Fact] + public void WithFilterAndProjection_Projects() + { + // Arrange + var collection = MockOf>(); + var projections = Fixture.CreateMany().ToList(); + var (context, cursor) = SetupProjection(projections, collection); + + // Act + var result = Sut.ProjectOne(filter, projectionExpression); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(projections[0]); + } + + [Fact] + public void WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var collection = MockOf>(); + var projections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + var (context, cursor) = SetupProjection(projections, collection); + + // Act + var result = Sut.ProjectOne(filter, projectionExpression, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(projections[0]); + } + + [Fact] + public void WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var collection = MockOf>(); + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupProjection(projections, collection, partitionKey); + + // Act + var result = Sut.ProjectOne(filter, projectionExpression, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(projections[0]); + } + + [Fact] + public void WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var collection = MockOf>(); + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupProjection(projections, collection, partitionKey); + + // Act + var result = Sut.ProjectOne(filter, projectionExpression, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().NotBeNull(); + result.Should().Be(projections[0]); + } + + private (Mock, Mock>) SetupProjection( + List projections, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupSyncCursor(projections); + + SetupFindSync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateManyAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateManyAsyncTests.cs new file mode 100644 index 0000000..f0322d1 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateManyAsyncTests.cs @@ -0,0 +1,582 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Update; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbUpdaterTests; + +public class UpdateManyAsyncTests : GenericTestContext +{ + [Fact] + public async Task WithFilterAndUpdateDefinition_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var updateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var collection = SetupCollection(count); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + + // Act + var result = await Sut.UpdateManyAsync( + filter, + updateDefinition); + + // Assert + result.Should().Be(count); + + collection + .Verify( + x => x.UpdateManyAsync( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(updateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithFilterAndUpdateDefinitionAndCancellationToken_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var updateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var collection = SetupCollection(count); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + var token = new CancellationToken(true); + + // Act + var result = await Sut.UpdateManyAsync( + filter, + updateDefinition, + cancellationToken: token); + + // Assert + result.Should().Be(count); + + collection + .Verify( + x => x.UpdateManyAsync( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(updateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithFilterAndUpdateDefinitionAndPartitionKey_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var updateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var collection = SetupCollection(count, partitionKey); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + + // Act + var result = await Sut.UpdateManyAsync( + filter, + updateDefinition, + partitionKey); + + // Assert + result.Should().Be(count); + + collection + .Verify( + x => x.UpdateManyAsync( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(updateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithFilterAndUpdateDefinitionAndPartitionKeyAndCancellationToken_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var updateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var collection = SetupCollection(count, partitionKey); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + var token = new CancellationToken(true); + + // Act + var result = await Sut.UpdateManyAsync( + filter, + updateDefinition, + partitionKey, + cancellationToken: token); + + // Assert + result.Should().Be(count); + + collection + .Verify( + x => x.UpdateManyAsync( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(updateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithFilterAndFieldExpressionAndValue_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var collection = SetupCollection(count); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateManyAsync( + filter, + fieldExpression, + value); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateManyAsync( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + + [Fact] + public async Task WithFilterAndFieldExpressionAndValueAndPartitionKey_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(count, partitionKey); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateManyAsync( + filter, + fieldExpression, + value, + partitionKey); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateManyAsync( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithFilterAndFieldExpressionAndValueAndCancellationToken_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var collection = SetupCollection(count); + var token = new CancellationToken(true); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateManyAsync( + filter, + fieldExpression, + value, + cancellationToken: token); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateManyAsync( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(count, partitionKey); + var token = new CancellationToken(true); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateManyAsync( + filter, + fieldExpression, + value, + partitionKey, + token); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateManyAsync( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValue_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var collection = SetupCollection(count); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateManyAsync( + filterExpression, + fieldExpression, + value); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateManyAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var collection = SetupCollection(count); + var token = new CancellationToken(true); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateManyAsync( + filterExpression, + fieldExpression, + value, + cancellationToken: token); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateManyAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_UpdatesMany() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var count = Fixture.Create(); + var collection = SetupCollection(count, partitionKey); + + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateManyAsync( + filterExpression, + fieldExpression, + value, + partitionKey); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateManyAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_UpdatesMany() + { + // Arrange + var value = Fixture.Create(); + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(count, partitionKey); + var token = new CancellationToken(true); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateManyAsync( + filterExpression, + fieldExpression, + value, + partitionKey, + token); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateManyAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinition_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var updateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var collection = SetupCollection(count); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + + // Act + var result = await Sut.UpdateManyAsync( + filterExpression, + updateDefinition); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateManyAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinitionAndCancellationToken_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var updateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var collection = SetupCollection(count); + var token = new CancellationToken(true); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + + // Act + var result = await Sut.UpdateManyAsync( + filterExpression, + updateDefinition, + cancellationToken: token); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateManyAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinitionAndPartitionKey_UpdatesMany() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var updateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var count = Fixture.Create(); + var collection = SetupCollection(count, partitionKey); + + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + + // Act + var result = await Sut.UpdateManyAsync( + filterExpression, + updateDefinition, + partitionKey); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateManyAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_UpdatesMany() + { + // Arrange + var value = Fixture.Create(); + var count = Fixture.Create(); + var updateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(count, partitionKey); + var token = new CancellationToken(true); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + + // Act + var result = await Sut.UpdateManyAsync( + filterExpression, + updateDefinition, + partitionKey, + token); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateManyAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + private Mock> SetupCollection(long count, string partitionKey = null) + { + var replacedId = Fixture.Create(); + var replaceResult = new ReplaceOneResult.Acknowledged(count, count, BsonValue.Create(replacedId)); + var updateResult = new UpdateResult.Acknowledged(count, count, BsonValue.Create(replacedId)); + + var collection = MockOf>(); + collection + .Setup( + x => x.ReplaceOneAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(replaceResult); + + collection + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(updateResult); + + collection + .Setup( + x => x.ReplaceOneAsync( + It.IsAny(), + It.IsAny>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(replaceResult); + + collection + .Setup( + x => x.UpdateManyAsync( + It.IsAny(), + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(updateResult); + + var dbContext = MockOf(); + dbContext + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + return collection; + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateManyTests.cs b/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateManyTests.cs new file mode 100644 index 0000000..1b87b86 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateManyTests.cs @@ -0,0 +1,581 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Update; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbUpdaterTests; + +public class UpdateManyTests : GenericTestContext +{ + [Fact] + public void WithFilterAndUpdateDefinition_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var updateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var collection = SetupCollection(count); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + + // Act + var result = Sut.UpdateMany( + filter, + updateDefinition); + + // Assert + result.Should().Be(count); + + collection + .Verify( + x => x.UpdateMany( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(updateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithFilterAndUpdateDefinitionAndCancellationToken_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var updateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var collection = SetupCollection(count); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + var token = new CancellationToken(true); + + // Act + var result = Sut.UpdateMany( + filter, + updateDefinition, + cancellationToken: token); + + // Assert + result.Should().Be(count); + + collection + .Verify( + x => x.UpdateMany( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(updateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public void WithFilterAndUpdateDefinitionAndPartitionKey_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var updateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var collection = SetupCollection(count, partitionKey); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + + // Act + var result = Sut.UpdateMany( + filter, + updateDefinition, + partitionKey); + + // Assert + result.Should().Be(count); + + collection + .Verify( + x => x.UpdateMany( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(updateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithFilterAndUpdateDefinitionAndPartitionKeyAndCancellationToken_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var updateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var collection = SetupCollection(count, partitionKey); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + var token = new CancellationToken(true); + + // Act + var result = Sut.UpdateMany( + filter, + updateDefinition, + partitionKey, + token); + + // Assert + result.Should().Be(count); + + collection + .Verify( + x => x.UpdateMany( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(updateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public void WithFilterAndFieldExpressionAndValue_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var collection = SetupCollection(count); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateMany( + filter, + fieldExpression, + value); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateMany( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + + [Fact] + public void WithFilterAndFieldExpressionAndValueAndPartitionKey_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(count, partitionKey); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateMany( + filter, + fieldExpression, + value, + partitionKey); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateMany( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithFilterAndFieldExpressionAndValueAndCancellationToken_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var collection = SetupCollection(count); + var token = new CancellationToken(true); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateMany( + filter, + fieldExpression, + value, + cancellationToken: token); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateMany( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public void WithFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(count, partitionKey); + var token = new CancellationToken(true); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateMany( + filter, + fieldExpression, + value, + partitionKey, + token); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateMany( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValue_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var collection = SetupCollection(count); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateMany( + filterExpression, + fieldExpression, + value); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateMany( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var collection = SetupCollection(count); + var token = new CancellationToken(true); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateMany( + filterExpression, + fieldExpression, + value, + cancellationToken: token); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateMany( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_UpdatesMany() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var count = Fixture.Create(); + var collection = SetupCollection(count, partitionKey); + + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateMany( + filterExpression, + fieldExpression, + value, + partitionKey); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateMany( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_UpdatesMany() + { + // Arrange + var value = Fixture.Create(); + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(count, partitionKey); + var token = new CancellationToken(true); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateMany( + filterExpression, + fieldExpression, + value, + partitionKey, + token); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateMany( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinition_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var updateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var collection = SetupCollection(count); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + + // Act + var result = Sut.UpdateMany( + filterExpression, + updateDefinition); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateMany( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinitionAndCancellationToken_UpdatesMany() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var updateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var collection = SetupCollection(count); + var token = new CancellationToken(true); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + + // Act + var result = Sut.UpdateMany( + filterExpression, + updateDefinition, + cancellationToken: token); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateMany( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinitionAndPartitionKey_UpdatesMany() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var updateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var count = Fixture.Create(); + var collection = SetupCollection(count, partitionKey); + + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + + // Act + var result = Sut.UpdateMany( + filterExpression, + updateDefinition, + partitionKey); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateMany( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_UpdatesMany() + { + // Arrange + var value = Fixture.Create(); + var count = Fixture.Create(); + var updateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(count, partitionKey); + var token = new CancellationToken(true); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + + // Act + var result = Sut.UpdateMany( + filterExpression, + updateDefinition, + partitionKey, + token); + + // Assert + result.Should().Be(count); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateMany( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + private Mock> SetupCollection(long count, string partitionKey = null) + { + var replacedId = Fixture.Create(); + var replaceResult = new ReplaceOneResult.Acknowledged(count, count, BsonValue.Create(replacedId)); + var updateResult = new UpdateResult.Acknowledged(count, count, BsonValue.Create(replacedId)); + + var collection = MockOf>(); + collection + .Setup( + x => x.ReplaceOneAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(replaceResult); + + collection + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(updateResult); + + collection + .Setup( + x => x.ReplaceOneAsync( + It.IsAny(), + It.IsAny>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(replaceResult); + + collection + .Setup( + x => x.UpdateMany( + It.IsAny(), + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(updateResult); + + var dbContext = MockOf(); + dbContext + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + return collection; + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateOneAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateOneAsyncTests.cs new file mode 100644 index 0000000..5149e07 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateOneAsyncTests.cs @@ -0,0 +1,806 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Update; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbUpdaterTests; + +public class UpdateOneAsyncTests : GenericTestContext +{ + [Fact] + public async Task WithDocument_ReplacesOne() + { + // Arrange + var document = Fixture.Create(); + + var collection = SetupCollection(); + + // Act + var result = await Sut.UpdateOneAsync(document); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + ReplaceOptions expectedOptions = null; + collection + .Verify( + x => x.ReplaceOneAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + document, + expectedOptions, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithDocumentAndCancellationToken_ReplacesOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + var collection = SetupCollection(); + + // Act + var result = await Sut.UpdateOneAsync(document, token); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + ReplaceOptions expectedOptions = null; + collection + .Verify( + x => x.ReplaceOneAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + document, + expectedOptions, + token), + Times.Once()); + } + + [Fact] + public async Task WithDocumentAndUpdateDefinition_ReplacesOne() + { + // Arrange + var document = Fixture.Create(); + var collection = SetupCollection(); + var updateDefinition = Builders.Update.Set(x => x.Id, document.Id); + + // Act + var result = await Sut.UpdateOneAsync(document, updateDefinition); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + collection + .Verify( + x => x.UpdateOneAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + updateDefinition, + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithDocumentAndUpdateDefinitionAndCancellationToken_ReplacesOne() + { + // Arrange + var document = Fixture.Create(); + var collection = SetupCollection(); + var updateDefinition = Builders.Update.Set(x => x.Id, document.Id); + var token = new CancellationToken(true); + + // Act + var result = await Sut.UpdateOneAsync(document, updateDefinition, token); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + collection + .Verify( + x => x.UpdateOneAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + updateDefinition, + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithDocumentAndFieldExpressionAndValue_ReplacesOne() + { + // Arrange + var document = Fixture.Create(); + var value = Fixture.Create(); + var collection = SetupCollection(); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateOneAsync(document, fieldExpression, value); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOneAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithDocumentAndFieldExpressionAndValueAndCancellationToken_ReplacesOne() + { + // Arrange + var document = Fixture.Create(); + var value = Fixture.Create(); + var collection = SetupCollection(); + var token = new CancellationToken(true); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateOneAsync(document, fieldExpression, value, token); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOneAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithFilterAndFieldExpressionAndValueAndPartitionKey_ReplacesOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(partitionKey); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateOneAsync( + filter, + fieldExpression, + value, + partitionKey); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOneAsync( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithFilterAndFieldExpressionAndValueAndCancellationToken_ReplacesOne() + { + // Arrange + var value = Fixture.Create(); + var collection = SetupCollection(); + var token = new CancellationToken(true); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateOneAsync( + filter, + fieldExpression, + value, + cancellationToken: token); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOneAsync( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(partitionKey); + var token = new CancellationToken(true); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateOneAsync( + filter, + fieldExpression, + value, + partitionKey, + token); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOneAsync( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ReplacesOne() + { + // Arrange + var value = Fixture.Create(); + var collection = SetupCollection(); + var token = new CancellationToken(true); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateOneAsync( + filterExpression, + fieldExpression, + value, + cancellationToken: token); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateOneAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ReplacesOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(partitionKey); + + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateOneAsync( + filterExpression, + fieldExpression, + value, + partitionKey); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateOneAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(partitionKey); + var token = new CancellationToken(true); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateOneAsync( + filterExpression, + fieldExpression, + value, + partitionKey, + token); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateOneAsync( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithClientSessionAndDocument_ReplacesOne() + { + // Arrange + var session = MockOf(); + var document = Fixture.Create(); + + var collection = SetupCollection(); + + // Act + var result = await Sut.UpdateOneAsync(session.Object, document); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + ReplaceOptions expectedOptions = null; + collection + .Verify( + x => x.ReplaceOneAsync( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + document, + expectedOptions, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithClientSessionAndDocumentAndCancellationToken_ReplacesOne() + { + // Arrange + var session = MockOf(); + var document = Fixture.Create(); + var token = new CancellationToken(true); + var collection = SetupCollection(); + + // Act + var result = await Sut.UpdateOneAsync(session.Object, document, token); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + ReplaceOptions expectedOptions = null; + collection + .Verify( + x => x.ReplaceOneAsync( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + document, + expectedOptions, + token), + Times.Once()); + } + + [Fact] + public async Task WithClientSessionAndDocumentAndUpdateDefinition_ReplacesOne() + { + // Arrange + var session = MockOf(); + var document = Fixture.Create(); + var collection = SetupCollection(); + var updateDefinition = Builders.Update.Set(x => x.Id, document.Id); + + // Act + var result = await Sut.UpdateOneAsync(session.Object, document, updateDefinition); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + collection + .Verify( + x => x.UpdateOneAsync( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + updateDefinition, + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithClientSessionAndDocumentAndUpdateDefinitionAndCancellationToken_ReplacesOne() + { + // Arrange + var session = MockOf(); + var document = Fixture.Create(); + var collection = SetupCollection(); + var updateDefinition = Builders.Update.Set(x => x.Id, document.Id); + var token = new CancellationToken(true); + + // Act + var result = await Sut.UpdateOneAsync(session.Object, document, updateDefinition, token); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + collection + .Verify( + x => x.UpdateOneAsync( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + updateDefinition, + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithClientSessionAndDocumentAndFieldExpressionAndValue_ReplacesOne() + { + // Arrange + var session = MockOf(); + var document = Fixture.Create(); + var value = Fixture.Create(); + var collection = SetupCollection(); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateOneAsync(session.Object, document, fieldExpression, value); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOneAsync( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithClientSessionAndDocumentAndFieldExpressionAndValueAndCancellationToken_ReplacesOne() + { + // Arrange + var session = MockOf(); + var document = Fixture.Create(); + var value = Fixture.Create(); + var collection = SetupCollection(); + var token = new CancellationToken(true); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateOneAsync(session.Object, document, fieldExpression, value, token); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOneAsync( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithClientSessionAndFilterAndFieldExpressionAndValueAndPartitionKey_ReplacesOne() + { + // Arrange + var session = MockOf(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(partitionKey); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateOneAsync( + session.Object, + filter, + fieldExpression, + value, + partitionKey); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOneAsync( + session.Object, + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithClientSessionAndFilterAndFieldExpressionAndValueAndCancellationToken_ReplacesOne() + { + // Arrange + var session = MockOf(); + var value = Fixture.Create(); + var collection = SetupCollection(); + var token = new CancellationToken(true); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateOneAsync( + session.Object, + filter, + fieldExpression, + value, + cancellationToken: token); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOneAsync( + session.Object, + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithClientSessionAndFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne() + { + // Arrange + var session = MockOf(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(partitionKey); + var token = new CancellationToken(true); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateOneAsync( + session.Object, + filter, + fieldExpression, + value, + partitionKey, + token); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOneAsync( + session.Object, + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ReplacesOne() + { + // Arrange + var session = MockOf(); + var value = Fixture.Create(); + var collection = SetupCollection(); + var token = new CancellationToken(true); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateOneAsync( + session.Object, + filterExpression, + fieldExpression, + value, + cancellationToken: token); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateOneAsync( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public async Task WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ReplacesOne() + { + // Arrange + var session = MockOf(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(partitionKey); + + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateOneAsync( + session.Object, + filterExpression, + fieldExpression, + value, + partitionKey); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateOneAsync( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public async Task WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne() + { + // Arrange + var session = MockOf(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(partitionKey); + var token = new CancellationToken(true); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = await Sut.UpdateOneAsync( + session.Object, + filterExpression, + fieldExpression, + value, + partitionKey, + token); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateOneAsync( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + private Mock> SetupCollection(string partitionKey = null) + { + var replacedId = Fixture.Create(); + var count = Fixture.Create(); + var replaceResult = new ReplaceOneResult.Acknowledged(count, 1, BsonValue.Create(replacedId)); + var updateResult = new UpdateResult.Acknowledged(count, 1, BsonValue.Create(replacedId)); + + var collection = MockOf>(); + collection + .Setup( + x => x.ReplaceOneAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(replaceResult); + + collection + .Setup( + x => x.UpdateOneAsync( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(updateResult); + + collection + .Setup( + x => x.ReplaceOneAsync( + It.IsAny(), + It.IsAny>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(replaceResult); + + collection + .Setup( + x => x.UpdateOneAsync( + It.IsAny(), + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(updateResult); + + var dbContext = MockOf(); + dbContext + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + return collection; + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateOneTests.cs b/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateOneTests.cs new file mode 100644 index 0000000..e73728c --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateOneTests.cs @@ -0,0 +1,805 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Update; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbUpdaterTests; + +public class UpdateOneTests : GenericTestContext +{ + [Fact] + public void WithDocument_ReplacesOne() + { + // Arrange + var document = Fixture.Create(); + + var collection = SetupCollection(); + + // Act + var result = Sut.UpdateOne(document); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + ReplaceOptions expectedOptions = null; + collection + .Verify( + x => x.ReplaceOne( + It.Is>(f => f.EquivalentTo(expectedFilter)), + document, + expectedOptions, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithDocumentAndCancellationToken_ReplacesOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + var collection = SetupCollection(); + + // Act + var result = Sut.UpdateOne(document, token); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + ReplaceOptions expectedOptions = null; + collection + .Verify( + x => x.ReplaceOne( + It.Is>(f => f.EquivalentTo(expectedFilter)), + document, + expectedOptions, + token), + Times.Once()); + } + + [Fact] + public void WithDocumentAndUpdateDefinition_ReplacesOne() + { + // Arrange + var document = Fixture.Create(); + var collection = SetupCollection(); + var updateDefinition = Builders.Update.Set(x => x.Id, document.Id); + + // Act + var result = Sut.UpdateOne(document, updateDefinition); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + collection + .Verify( + x => x.UpdateOne( + It.Is>(f => f.EquivalentTo(expectedFilter)), + updateDefinition, + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithDocumentAndUpdateDefinitionAndCancellationToken_ReplacesOne() + { + // Arrange + var document = Fixture.Create(); + var collection = SetupCollection(); + var updateDefinition = Builders.Update.Set(x => x.Id, document.Id); + var token = new CancellationToken(true); + + // Act + var result = Sut.UpdateOne(document, updateDefinition, token); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + collection + .Verify( + x => x.UpdateOne( + It.Is>(f => f.EquivalentTo(expectedFilter)), + updateDefinition, + null, + token), + Times.Once()); + } + + [Fact] + public void WithDocumentAndFieldExpressionAndValue_ReplacesOne() + { + // Arrange + var document = Fixture.Create(); + var value = Fixture.Create(); + var collection = SetupCollection(); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateOne(document, fieldExpression, value); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOne( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithDocumentAndFieldExpressionAndValueAndCancellationToken_ReplacesOne() + { + // Arrange + var document = Fixture.Create(); + var value = Fixture.Create(); + var collection = SetupCollection(); + var token = new CancellationToken(true); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateOne(document, fieldExpression, value, token); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOne( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public void WithFilterAndFieldExpressionAndValueAndPartitionKey_ReplacesOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(partitionKey); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateOne( + filter, + fieldExpression, + value, + partitionKey); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOne( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithFilterAndFieldExpressionAndValueAndCancellationToken_ReplacesOne() + { + // Arrange + var value = Fixture.Create(); + var collection = SetupCollection(); + var token = new CancellationToken(true); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateOne( + filter, + fieldExpression, + value, + cancellationToken: token); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOne( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public void WithFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(partitionKey); + var token = new CancellationToken(true); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateOne( + filter, + fieldExpression, + value, + partitionKey, + token); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOne( + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ReplacesOne() + { + // Arrange + var value = Fixture.Create(); + var collection = SetupCollection(); + var token = new CancellationToken(true); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateOne( + filterExpression, + fieldExpression, + value, + cancellationToken: token); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateOne( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ReplacesOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(partitionKey); + + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateOne( + filterExpression, + fieldExpression, + value, + partitionKey); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateOne( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(partitionKey); + var token = new CancellationToken(true); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateOne( + filterExpression, + fieldExpression, + value, + partitionKey, + token); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateOne( + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public void WithClientSessionAndDocument_ReplacesOne() + { + // Arrange + var session = MockOf(); + var document = Fixture.Create(); + + var collection = SetupCollection(); + + // Act + var result = Sut.UpdateOne(session.Object, document); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + ReplaceOptions expectedOptions = null; + collection + .Verify( + x => x.ReplaceOne( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + document, + expectedOptions, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithClientSessionAndDocumentAndCancellationToken_ReplacesOne() + { + // Arrange + var session = MockOf(); + var document = Fixture.Create(); + var token = new CancellationToken(true); + var collection = SetupCollection(); + + // Act + var result = Sut.UpdateOne(session.Object, document, token); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + ReplaceOptions expectedOptions = null; + collection + .Verify( + x => x.ReplaceOne( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + document, + expectedOptions, + token), + Times.Once()); + } + + [Fact] + public void WithClientSessionAndDocumentAndUpdateDefinition_ReplacesOne() + { + // Arrange + var session = MockOf(); + var document = Fixture.Create(); + var collection = SetupCollection(); + var updateDefinition = Builders.Update.Set(x => x.Id, document.Id); + + // Act + var result = Sut.UpdateOne(session.Object, document, updateDefinition); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + collection + .Verify( + x => x.UpdateOne( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + updateDefinition, + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithClientSessionAndDocumentAndUpdateDefinitionAndCancellationToken_ReplacesOne() + { + // Arrange + var session = MockOf(); + var document = Fixture.Create(); + var collection = SetupCollection(); + var updateDefinition = Builders.Update.Set(x => x.Id, document.Id); + var token = new CancellationToken(true); + + // Act + var result = Sut.UpdateOne(session.Object, document, updateDefinition, token); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + collection + .Verify( + x => x.UpdateOne( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + updateDefinition, + null, + token), + Times.Once()); + } + + [Fact] + public void WithClientSessionAndDocumentAndFieldExpressionAndValue_ReplacesOne() + { + // Arrange + var session = MockOf(); + var document = Fixture.Create(); + var value = Fixture.Create(); + var collection = SetupCollection(); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateOne(session.Object, document, fieldExpression, value); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOne( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithClientSessionAndDocumentAndFieldExpressionAndValueAndCancellationToken_ReplacesOne() + { + // Arrange + var session = MockOf(); + var document = Fixture.Create(); + var value = Fixture.Create(); + var collection = SetupCollection(); + var token = new CancellationToken(true); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateOne(session.Object, document, fieldExpression, value, token); + + // Assert + result.Should().BeTrue(); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOne( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public void WithClientSessionAndFilterAndFieldExpressionAndValueAndPartitionKey_ReplacesOne() + { + // Arrange + var session = MockOf(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(partitionKey); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateOne( + session.Object, + filter, + fieldExpression, + value, + partitionKey); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOne( + session.Object, + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithClientSessionAndFilterAndFieldExpressionAndValueAndCancellationToken_ReplacesOne() + { + // Arrange + var session = MockOf(); + var value = Fixture.Create(); + var collection = SetupCollection(); + var token = new CancellationToken(true); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateOne( + session.Object, + filter, + fieldExpression, + value, + cancellationToken: token); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOne( + session.Object, + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public void WithClientSessionAndFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne() + { + // Arrange + var session = MockOf(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(partitionKey); + var token = new CancellationToken(true); + var filter = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateOne( + session.Object, + filter, + fieldExpression, + value, + partitionKey, + token); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + collection + .Verify( + x => x.UpdateOne( + session.Object, + It.Is>(f => f.EquivalentTo(filter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public void WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ReplacesOne() + { + // Arrange + var session = MockOf(); + var value = Fixture.Create(); + var collection = SetupCollection(); + var token = new CancellationToken(true); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateOne( + session.Object, + filterExpression, + fieldExpression, + value, + cancellationToken: token); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateOne( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + [Fact] + public void WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ReplacesOne() + { + // Arrange + var session = MockOf(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(partitionKey); + + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateOne( + session.Object, + filterExpression, + fieldExpression, + value, + partitionKey); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateOne( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + CancellationToken.None), + Times.Once()); + } + + [Fact] + public void WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne() + { + // Arrange + var session = MockOf(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var collection = SetupCollection(partitionKey); + var token = new CancellationToken(true); + Expression> filterExpression = testDocument => testDocument.SomeContent == "SomeContent"; + Expression> fieldExpression = testDocument => testDocument.SomeContent; + + // Act + var result = Sut.UpdateOne( + session.Object, + filterExpression, + fieldExpression, + value, + partitionKey, + token); + + // Assert + result.Should().BeTrue(); + + var expectedUpdateDefinition = Builders.Update.Set(x => x.SomeContent, value); + var expectedFilter = Builders.Filter.Where(filterExpression); + collection + .Verify( + x => x.UpdateOne( + session.Object, + It.Is>(f => f.EquivalentTo(expectedFilter)), + It.Is>(u => u.EquivalentTo(expectedUpdateDefinition)), + null, + token), + Times.Once()); + } + + private Mock> SetupCollection(string partitionKey = null) + { + var replacedId = Fixture.Create(); + var count = Fixture.Create(); + var replaceResult = new ReplaceOneResult.Acknowledged(count, 1, BsonValue.Create(replacedId)); + var updateResult = new UpdateResult.Acknowledged(count, 1, BsonValue.Create(replacedId)); + + var collection = MockOf>(); + collection + .Setup( + x => x.ReplaceOne( + It.IsAny>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(replaceResult); + + collection + .Setup( + x => x.UpdateOne( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(updateResult); + + collection + .Setup( + x => x.ReplaceOne( + It.IsAny(), + It.IsAny>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(replaceResult); + + collection + .Setup( + x => x.UpdateOne( + It.IsAny(), + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(updateResult); + + var dbContext = MockOf(); + dbContext + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + return collection; + } +} diff --git a/CoreUnitTests/Infrastructure/FilterDefinitionExtensions.cs b/CoreUnitTests/Infrastructure/FilterDefinitionExtensions.cs new file mode 100644 index 0000000..adcaca5 --- /dev/null +++ b/CoreUnitTests/Infrastructure/FilterDefinitionExtensions.cs @@ -0,0 +1,14 @@ +namespace CoreUnitTests.Infrastructure; + +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Driver; + +public static class FilterDefinitionExtensions +{ + public static string RenderToJson(this FilterDefinition filter) + => filter.Render(BsonSerializer.SerializerRegistry.GetSerializer(), BsonSerializer.SerializerRegistry).ToJson(); + + public static bool EquivalentTo(this FilterDefinition filter, FilterDefinition other) + => filter.RenderToJson() == other.RenderToJson(); +} diff --git a/CoreUnitTests/Infrastructure/GenericTestContext.cs b/CoreUnitTests/Infrastructure/GenericTestContext.cs new file mode 100644 index 0000000..9c494d6 --- /dev/null +++ b/CoreUnitTests/Infrastructure/GenericTestContext.cs @@ -0,0 +1,18 @@ +using AutoFixture; +using AutoFixture.AutoMoq; +using Moq; + +namespace CoreUnitTests.Infrastructure; + +public class GenericTestContext +{ + public GenericTestContext() => Fixture = new Fixture().Customize(new AutoMoqCustomization()); + + protected Mock MockOf() + where T : class => + Fixture.Freeze>(); + + protected IFixture Fixture { get; set; } + + protected TSut Sut => Fixture.Create(); +} diff --git a/CoreUnitTests/Infrastructure/IndexExtensions.cs b/CoreUnitTests/Infrastructure/IndexExtensions.cs new file mode 100644 index 0000000..7ff4fff --- /dev/null +++ b/CoreUnitTests/Infrastructure/IndexExtensions.cs @@ -0,0 +1,69 @@ +using System; +using MongoDB.Bson.Serialization; +using MongoDB.Driver; +using MongoDbGenericRepository.Models; +using Xunit.Abstractions; + +namespace CoreUnitTests.Infrastructure; + +public static class IndexExtensions +{ + public static bool EqualToJson(this IndexKeysDefinition keys, string json, ITestOutputHelper output) + { + var indexModelRendered = RenderIndexModelKeys(keys); + var result = indexModelRendered.Equals(json, StringComparison.Ordinal); + if (!result && output != null) + { + output.WriteLine($"Expected: {json}"); + output.WriteLine($"Actual: {indexModelRendered}"); + } + + return result; + } + + public static bool EqualToJson(this IndexKeysDefinition keys, string json) + { + var indexModelRendered = RenderIndexModelKeys(keys); + return indexModelRendered.Equals(json, StringComparison.Ordinal); + } + + public static bool EqualTo(this IndexCreationOptions x, CreateIndexOptions y) => + x.Unique == y.Unique && + x.TextIndexVersion == y.TextIndexVersion && + x.SphereIndexVersion == y.SphereIndexVersion && + x.Sparse == y.Sparse && + x.Name == y.Name && + x.Min == y.Min && + x.Max == y.Max && + x.LanguageOverride == y.LanguageOverride && + x.ExpireAfter == y.ExpireAfter && + x.DefaultLanguage == y.DefaultLanguage && + x.Bits == y.Bits && + x.Background == y.Background && + x.Version == y.Version; + + public static bool EqualTo(this CreateIndexOptions x, IndexCreationOptions y) => + x.Unique == y.Unique && + x.TextIndexVersion == y.TextIndexVersion && + x.SphereIndexVersion == y.SphereIndexVersion && + x.Sparse == y.Sparse && + x.Name == y.Name && + x.Min == y.Min && + x.Max == y.Max && + x.LanguageOverride == y.LanguageOverride && + x.ExpireAfter == y.ExpireAfter && + x.DefaultLanguage == y.DefaultLanguage && + x.Bits == y.Bits && + x.Background == y.Background && + x.Version == y.Version; + + private static string RenderIndexModelKeys(IndexKeysDefinition keys) + { + var indexModelRendered = keys.Render( + BsonSerializer.SerializerRegistry.GetSerializer(), + BsonSerializer.SerializerRegistry); + + var result = indexModelRendered.ToString(); + return result.Replace(" ", ""); + } +} 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/PartitionedTestDocument.cs b/CoreUnitTests/Infrastructure/Model/PartitionedTestDocument.cs new file mode 100644 index 0000000..082e5aa --- /dev/null +++ b/CoreUnitTests/Infrastructure/Model/PartitionedTestDocument.cs @@ -0,0 +1,9 @@ +using MongoDbGenericRepository.Models; + +namespace CoreUnitTests.Infrastructure.Model; + +public class PartitionedTestDocument : TestDocument, IPartitionedDocument +{ + /// + public string PartitionKey { get; set; } = "PartitionedTestDocument"; +} diff --git a/CoreUnitTests/Infrastructure/Model/TestDocument.cs b/CoreUnitTests/Infrastructure/Model/TestDocument.cs new file mode 100644 index 0000000..9d09bf6 --- /dev/null +++ b/CoreUnitTests/Infrastructure/Model/TestDocument.cs @@ -0,0 +1,33 @@ +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 decimal SomeDecimalValue { get; set; } + + public string SomeContent { get; set; } + + public string SomeContent2 { get; set; } + + public string SomeContent3 { get; set; } + + public int GroupingKey { get; set; } + + public Guid OtherGroupingKey { 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..a189b86 --- /dev/null +++ b/CoreUnitTests/Infrastructure/Model/TestDocumentWithKey.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using MongoDbGenericRepository.Models; + +namespace CoreUnitTests.Infrastructure.Model; + +public class TestDocumentWithKey : IDocument + where TKey : IEquatable +{ + public TKey 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 int SomeDecimalValue { 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/TestProjection.cs b/CoreUnitTests/Infrastructure/Model/TestProjection.cs new file mode 100644 index 0000000..c1c8714 --- /dev/null +++ b/CoreUnitTests/Infrastructure/Model/TestProjection.cs @@ -0,0 +1,12 @@ +using System; + +namespace CoreUnitTests.Infrastructure.Model; + +public class TestProjection +{ + public Guid TestDocumentId { get; set; } + + public DateTime NestedData { get; set; } + + public int Count { get; set; } +} diff --git a/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs new file mode 100644 index 0000000..b6e1377 --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs @@ -0,0 +1,29 @@ +using System; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.DataAccess.Delete; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.DataAccess.Read; +using MongoDbGenericRepository.DataAccess.Update; + +namespace CoreUnitTests.Infrastructure; + +public class TestKeyedMongoRepository : BaseMongoRepository + where TKey : IEquatable +{ + 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; + + public void SetEraser(IMongoDbEraser eraser) => MongoDbEraser = eraser; + + public void SetUpdater(IMongoDbUpdater updater) => MongoDbUpdater = updater; +} diff --git a/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs b/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs new file mode 100644 index 0000000..91a988d --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs @@ -0,0 +1,77 @@ +using System; +using AutoFixture; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.DataAccess.Delete; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.DataAccess.Read; +using MongoDbGenericRepository.DataAccess.Update; +using Moq; + +namespace CoreUnitTests.Infrastructure; + +public class TestKeyedMongoRepositoryContext + where TKey : IEquatable +{ + private readonly Mock mongoDatabase; + + private TestKeyedMongoRepository sut; + + protected TestKeyedMongoRepositoryContext() + { + mongoDatabase = new Mock(); + Fixture = new Fixture(); + } + + protected Fixture Fixture { get; set; } + + 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); + } + + if (Eraser != null) + { + sut.SetEraser(Eraser.Object); + } + + if (Updater != null) + { + sut.SetUpdater(Updater.Object); + } + + return sut; + } + } + + protected Mock IndexHandler { get; set; } + + protected Mock Creator { get; set; } + + protected Mock Reader { get; set; } + + protected Mock Eraser { get; set; } + + protected Mock Updater { get; set; } + +} diff --git a/CoreUnitTests/Infrastructure/TestKeyedReadOnlyMongoRepository.cs b/CoreUnitTests/Infrastructure/TestKeyedReadOnlyMongoRepository.cs new file mode 100644 index 0000000..5b6efac --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestKeyedReadOnlyMongoRepository.cs @@ -0,0 +1,30 @@ +using System; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Read; + +namespace CoreUnitTests.Infrastructure; + +public class TestKeyedReadOnlyMongoRepository : ReadOnlyMongoRepository + where TKey : IEquatable +{ + /// + public TestKeyedReadOnlyMongoRepository(string connectionString, string databaseName = null) + : base(connectionString, databaseName) + { + } + + /// + public TestKeyedReadOnlyMongoRepository(IMongoDatabase mongoDatabase) + : base(mongoDatabase) + { + } + + /// + public TestKeyedReadOnlyMongoRepository(IMongoDbContext mongoDbContext) + : base(mongoDbContext) + { + } + + public void SetReader(IMongoDbReader reader) => MongoDbReader = reader; +} diff --git a/CoreUnitTests/Infrastructure/TestKeyedReadOnlyMongoRepositoryContext.cs b/CoreUnitTests/Infrastructure/TestKeyedReadOnlyMongoRepositoryContext.cs new file mode 100644 index 0000000..fab275e --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestKeyedReadOnlyMongoRepositoryContext.cs @@ -0,0 +1,45 @@ +using System; +using AutoFixture; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; + +namespace CoreUnitTests.Infrastructure; + +public class TestKeyedReadOnlyMongoRepositoryContext + where TKey : IEquatable +{ + private readonly Mock mongoDatabase; + + private TestKeyedReadOnlyMongoRepository sut; + + protected TestKeyedReadOnlyMongoRepositoryContext() + { + mongoDatabase = new Mock(); + Fixture = new Fixture(); + } + + protected Fixture Fixture { get; set; } + + protected TestKeyedReadOnlyMongoRepository Sut + { + get + { + if (sut != null) + { + return sut; + } + + sut = new TestKeyedReadOnlyMongoRepository(mongoDatabase.Object); + + if (Reader != null) + { + sut.SetReader(Reader.Object); + } + + return sut; + } + } + + protected Mock Reader { get; set; } +} diff --git a/CoreUnitTests/Infrastructure/TestMongoRepository.cs b/CoreUnitTests/Infrastructure/TestMongoRepository.cs new file mode 100644 index 0000000..4252a4f --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestMongoRepository.cs @@ -0,0 +1,27 @@ +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.DataAccess.Delete; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.DataAccess.Read; +using MongoDbGenericRepository.DataAccess.Update; + +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; + + public void SetEraser(IMongoDbEraser eraser) => MongoDbEraser = eraser; + + public void SetUpdater(IMongoDbUpdater updater) => MongoDbUpdater = updater; +} diff --git a/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs b/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs new file mode 100644 index 0000000..c335e13 --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs @@ -0,0 +1,72 @@ +using AutoFixture; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.DataAccess.Delete; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.DataAccess.Read; +using MongoDbGenericRepository.DataAccess.Update; +using Moq; + +namespace CoreUnitTests.Infrastructure; + +public class TestMongoRepositoryContext +{ + private readonly Mock _mongoDatabase; + + private TestMongoRepository _sut; + + protected TestMongoRepositoryContext() + { + _mongoDatabase = new Mock(); + Fixture = new Fixture(); + } + + public Fixture Fixture { get; set; } + + 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); + } + + if (Eraser != null) + { + _sut.SetEraser(Eraser.Object); + } + + if (Updater != null) + { + _sut.SetUpdater(Updater.Object); + } + } + + return _sut; + } + } + + protected Mock IndexHandler { get; set; } + + protected Mock Creator { get; set; } + + protected Mock Reader { get; set; } + + protected Mock Eraser { get; set; } + + protected Mock Updater { get; set; } +} diff --git a/CoreUnitTests/Infrastructure/TestReadOnlyMongoRepository.cs b/CoreUnitTests/Infrastructure/TestReadOnlyMongoRepository.cs new file mode 100644 index 0000000..2b0c5d3 --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestReadOnlyMongoRepository.cs @@ -0,0 +1,29 @@ +using System; +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Read; + +namespace CoreUnitTests.Infrastructure; + +public class TestReadOnlyMongoRepository : ReadOnlyMongoRepository +{ + /// + public TestReadOnlyMongoRepository(string connectionString, string databaseName = null) + : base(connectionString, databaseName) + { + } + + /// + public TestReadOnlyMongoRepository(IMongoDatabase mongoDatabase) + : base(mongoDatabase) + { + } + + /// + public TestReadOnlyMongoRepository(IMongoDbContext mongoDbContext) + : base(mongoDbContext) + { + } + + public void SetReader(IMongoDbReader reader) => MongoDbReader = reader; +} diff --git a/CoreUnitTests/Infrastructure/TestReadOnlyMongoRepositoryContext.cs b/CoreUnitTests/Infrastructure/TestReadOnlyMongoRepositoryContext.cs new file mode 100644 index 0000000..6c34efe --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestReadOnlyMongoRepositoryContext.cs @@ -0,0 +1,49 @@ +using System; +using AutoFixture; +using AutoFixture.AutoMoq; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; + +namespace CoreUnitTests.Infrastructure; + +public class TestReadOnlyMongoRepositoryContext +{ + private readonly Mock mongoDatabase; + + private TestReadOnlyMongoRepository sut; + + protected TestReadOnlyMongoRepositoryContext() + { + mongoDatabase = new Mock(); + Fixture = new Fixture().Customize(new AutoMoqCustomization()); + } + + protected IFixture Fixture { get; set; } + + protected TestReadOnlyMongoRepository Sut + { + get + { + if (sut != null) + { + return sut; + } + + sut = Fixture.Create(); + + if (Reader != null) + { + sut.SetReader(Reader.Object); + } + + return sut; + } + } + + protected Mock Reader { get; set; } + + protected Mock MockOf() + where T : class => + Fixture.Freeze>(); +} diff --git a/CoreUnitTests/Infrastructure/UpdateDefinitionExtensions.cs b/CoreUnitTests/Infrastructure/UpdateDefinitionExtensions.cs new file mode 100644 index 0000000..e4cd92b --- /dev/null +++ b/CoreUnitTests/Infrastructure/UpdateDefinitionExtensions.cs @@ -0,0 +1,21 @@ +using System; +using MongoDB.Bson.Serialization; +using MongoDB.Driver; + +namespace CoreUnitTests.Infrastructure; + +public static class UpdateDefinitionExtensions +{ + public static bool EquivalentTo(this UpdateDefinition update, UpdateDefinition expected) + { + var renderedUpdate = update.Render( + BsonSerializer.SerializerRegistry.GetSerializer(), + BsonSerializer.SerializerRegistry); + + var renderedExpected = expected.Render( + BsonSerializer.SerializerRegistry.GetSerializer(), + BsonSerializer.SerializerRegistry); + + return renderedUpdate.Equals(renderedExpected); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/AddTests/AddManyAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/AddTests/AddManyAsyncTests.cs new file mode 100644 index 0000000..785bb5c --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/AddTests/AddManyAsyncTests.cs @@ -0,0 +1,43 @@ +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Create; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.AddTests; + +public class AddManyAsyncTests : TestKeyedMongoRepositoryContext +{ + [Fact] + public async Task WithDocument_ShouldAddOne() + { + // Arrange + var documents = Fixture.CreateMany>().ToList(); + Creator = new Mock(); + + // Act + await Sut.AddManyAsync(documents); + + // Assert + Creator.Verify(x => x.AddManyAsync, int>(documents, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithDocumentAndCancellationToken_ShouldAddOne() + { + // Arrange + var documents = Fixture.CreateMany>().ToList(); + var token = new CancellationToken(true); + Creator = new Mock(); + + // Act + await Sut.AddManyAsync(documents, token); + + // Assert + Creator.Verify(x => x.AddManyAsync, int>(documents, token), Times.Once); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/AddTests/AddManyTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/AddTests/AddManyTests.cs new file mode 100644 index 0000000..0878d6c --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/AddTests/AddManyTests.cs @@ -0,0 +1,43 @@ +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Create; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.AddTests; + +public class AddManyTests : TestKeyedMongoRepositoryContext +{ + [Fact] + public void WithDocument_ShouldAddOne() + { + // Arrange + var documents = Fixture.CreateMany>().ToList(); + Creator = new Mock(); + + // Act + Sut.AddMany(documents); + + // Assert + Creator.Verify(x => x.AddMany, int>(documents, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithDocumentAndCancellationToken_ShouldAddOne() + { + // Arrange + var documents = Fixture.CreateMany>().ToList(); + var token = new CancellationToken(true); + Creator = new Mock(); + + // Act + Sut.AddMany(documents, token); + + // Assert + Creator.Verify(x => x.AddMany, int>(documents, token), Times.Once); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/AddTests/AddOneAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/AddTests/AddOneAsyncTests.cs new file mode 100644 index 0000000..da1b8b9 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/AddTests/AddOneAsyncTests.cs @@ -0,0 +1,42 @@ +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Create; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.AddTests; + +public class AddOneAsyncTests : TestKeyedMongoRepositoryContext +{ + [Fact] + public async Task WithDocument_ShouldAddOne() + { + // Arrange + var document = Fixture.Create>(); + Creator = new Mock(); + + // Act + await Sut.AddOneAsync(document); + + // Assert + Creator.Verify(x => x.AddOneAsync, int>(document, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithDocumentAndCancellationToken_ShouldAddOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Creator = new Mock(); + + // Act + await Sut.AddOneAsync(document, token); + + // Assert + Creator.Verify(x => x.AddOneAsync, int>(document, token), Times.Once); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/AddTests/AddOneTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/AddTests/AddOneTests.cs new file mode 100644 index 0000000..640040f --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/AddTests/AddOneTests.cs @@ -0,0 +1,41 @@ +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Create; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.AddTests; + +public class AddOneTests : TestKeyedMongoRepositoryContext +{ + [Fact] + public void WithDocument_ShouldAddOne() + { + // Arrange + var document = Fixture.Create>(); + Creator = new Mock(); + + // Act + Sut.AddOne(document); + + // Assert + Creator.Verify(x => x.AddOne, int>(document, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithDocumentAndCancellationToken_ShouldAddOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Creator = new Mock(); + + // Act + Sut.AddOne(document, token); + + // Assert + Creator.Verify(x => x.AddOne, int>(document, token), Times.Once); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs new file mode 100644 index 0000000..22981c8 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs @@ -0,0 +1,167 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Delete; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.DeleteTests; + +public class DeleteManyAsyncTests : TestKeyedMongoRepositoryContext +{ + [Fact] + public async Task WithDocuments_ShouldDeleteMany() + { + // Arrange + var documents = Fixture.CreateMany>().ToList(); + var count = Fixture.Create(); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync, int>(It.IsAny>>(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync(documents); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync, int>(documents, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithDocumentsAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var documents = Fixture.CreateMany>().ToList(); + var count = Fixture.Create(); + var cancellationToken = new CancellationToken(true); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync, int>(It.IsAny>>(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync(documents, cancellationToken); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync, int>(documents, cancellationToken), Times.Once); + } + + [Fact] + public async Task WithFilter_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync, int>(filter, null, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithFilterAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync(filter, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync, int>(filter, null, token), Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKey_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync, int>(filter, partitionKey, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteManyAsync(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteManyAsync, int>(filter, partitionKey, token), Times.Once); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyTests.cs new file mode 100644 index 0000000..8bcddfa --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyTests.cs @@ -0,0 +1,166 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Delete; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.DeleteTests; + +public class DeleteManyTests : TestKeyedMongoRepositoryContext +{ + [Fact] + public void WithDocuments_ShouldDeleteMany() + { + // Arrange + var documents = Fixture.CreateMany>().ToList(); + var count = Fixture.Create(); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteMany, int>(It.IsAny>>(), CancellationToken.None)) + .Returns(count); + + // Act + var result = Sut.DeleteMany(documents); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(documents, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithDocumentsAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var documents = Fixture.CreateMany>().ToList(); + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteMany, int>(It.IsAny>>(), It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteMany(documents, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(documents, token), Times.Once); + } + + [Fact] + public void WithFilter_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteMany, int>(It.IsAny, bool>>>(), null, CancellationToken.None)) + .Returns(count); + + // Act + var result = Sut.DeleteMany(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(filter, null, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithFilterAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup( + x => x.DeleteMany, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteMany(filter, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(filter, null, token), Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKey_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup( + x => x.DeleteMany, int>( + It.IsAny, bool>>>(), + It.IsAny(), + CancellationToken.None)) + .Returns(count); + + // Act + var result = Sut.DeleteMany(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(filter, partitionKey, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteMany() + { + // Arrange + var content = Fixture.Create(); + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteMany, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteMany(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(filter, partitionKey, token), Times.Once); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs new file mode 100644 index 0000000..4949a8f --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs @@ -0,0 +1,154 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Delete; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.DeleteTests; + +public class DeleteOneAsyncTests : TestKeyedMongoRepositoryContext +{ + [Fact] + public async Task WithDocument_ShouldDeleteOne() + { + // Arrange + var document = Fixture.Create>(); + var count = Fixture.Create(); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync, int>(It.IsAny>(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync(document); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync, int>(document, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithDocumentAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var document = Fixture.Create>(); + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync, int>(It.IsAny>(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync(document, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync, int>(document, token), Times.Once); + } + + [Fact] + public async Task WithFilter_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync, int>(It.IsAny, bool>>>(), It.IsAny(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync, int>(filter, null, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithFilterAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var token = new CancellationToken(true); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync, int>(It.IsAny, bool>>>(), It.IsAny(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync(filter, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync, int>(filter, null, token), Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKey_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync, int>(It.IsAny, bool>>>(), It.IsAny(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync, int>(filter, partitionKey, CancellationToken.None), Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync, int>(It.IsAny, bool>>>(), It.IsAny(), It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.DeleteOneAsync(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync, int>(filter, partitionKey, token), Times.Once); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneTests.cs new file mode 100644 index 0000000..2d24876 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneTests.cs @@ -0,0 +1,168 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Delete; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.DeleteTests; + +public class DeleteOneTests : TestKeyedMongoRepositoryContext +{ + [Fact] + public void WithDocument_ShouldDeleteOne() + { + // Arrange + var document = Fixture.Create>(); + var count = Fixture.Create(); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOne, int>( + It.IsAny>(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(document); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(document, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithDocumentAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var document = Fixture.Create>(); + var count = Fixture.Create(); + var token = new CancellationToken(true); + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOne, int>( + It.IsAny>(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(document, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(document, token), Times.Once); + } + + [Fact] + public void WithFilter_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOne, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(filter, null, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithFilterAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var token = new CancellationToken(true); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOne, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(filter, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(filter, null, token), Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKey_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOne, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(filter, partitionKey, CancellationToken.None), Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var count = Fixture.Create(); + var content = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + Expression, bool>> filter = x => x.SomeContent == content; + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOne, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(filter, partitionKey, token), Times.Once); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateAscendingIndexAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateAscendingIndexAsyncTests.cs new file mode 100644 index 0000000..3404c48 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateAscendingIndexAsyncTests.cs @@ -0,0 +1,154 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.IndexTests; + +public class CreateAscendingIndexAsyncTests : TestKeyedMongoRepositoryContext +{ + private readonly Expression, object>> keyedFieldExpression = t => t.SomeContent2; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(keyedFieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>(keyedFieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(keyedFieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>(keyedFieldExpression, null, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(keyedFieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(keyedFieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, options, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(keyedFieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(keyedFieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(keyedFieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(keyedFieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, options, partitionKey, token)); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateCombinedTextIndexAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateCombinedTextIndexAsyncTests.cs new file mode 100644 index 0000000..f1c46cf --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateCombinedTextIndexAsyncTests.cs @@ -0,0 +1,155 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.IndexTests; + +public class CreateCombinedTextIndexAsyncTests : TestKeyedMongoRepositoryContext +{ + private readonly List, object>>> keyedFieldExpressions = new() {t => t.SomeContent2, t => t.SomeContent3}; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync(keyedFieldExpressions); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync, int>(keyedFieldExpressions, null, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync(keyedFieldExpressions, token); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync, int>(keyedFieldExpressions, null, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync(keyedFieldExpressions, options); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync, int>( + keyedFieldExpressions, options, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync(keyedFieldExpressions, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync, int>( + keyedFieldExpressions, options, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync(keyedFieldExpressions, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync, int>( + keyedFieldExpressions, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync(keyedFieldExpressions, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync, int>( + keyedFieldExpressions, null, partitionKey, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync(keyedFieldExpressions, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync, int>( + keyedFieldExpressions, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateCombinedTextIndexAsync(keyedFieldExpressions, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateCombinedTextIndexAsync, int>( + keyedFieldExpressions, options, partitionKey, token)); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateDescendingIndexAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateDescendingIndexAsyncTests.cs new file mode 100644 index 0000000..c327000 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateDescendingIndexAsyncTests.cs @@ -0,0 +1,154 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.IndexTests; + +public class CreateDescendingIndexAsyncTests : TestKeyedMongoRepositoryContext +{ + private readonly Expression, object>> keyedFieldExpression = t => t.SomeContent2; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(keyedFieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>(keyedFieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(keyedFieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>(keyedFieldExpression, null, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(keyedFieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(keyedFieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, options, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(keyedFieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(keyedFieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(keyedFieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(keyedFieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, options, partitionKey, token)); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateHashedIndexAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateHashedIndexAsyncTests.cs new file mode 100644 index 0000000..d2e60dd --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateHashedIndexAsyncTests.cs @@ -0,0 +1,154 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.IndexTests; + +public class CreateHashedIndexAsyncTests : TestKeyedMongoRepositoryContext +{ + private readonly Expression, object>> keyedFieldExpression = t => t.SomeContent2; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync(keyedFieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync, int>(keyedFieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync(keyedFieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync, int>(keyedFieldExpression, null, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync(keyedFieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync, int>( + keyedFieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync(keyedFieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync, int>( + keyedFieldExpression, options, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync(keyedFieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync, int>( + keyedFieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync(keyedFieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync, int>( + keyedFieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync(keyedFieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync, int>( + keyedFieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateHashedIndexAsync(keyedFieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateHashedIndexAsync, int>( + keyedFieldExpression, options, partitionKey, token)); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateTextIndexAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateTextIndexAsyncTests.cs new file mode 100644 index 0000000..81f2790 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateTextIndexAsyncTests.cs @@ -0,0 +1,154 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.IndexTests; + +public class CreateTextIndexAsyncTests : TestKeyedMongoRepositoryContext +{ + private readonly Expression, object>> keyedFieldExpression = t => t.SomeContent2; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(keyedFieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync, int>(keyedFieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(keyedFieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync, int>(keyedFieldExpression, null, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(keyedFieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync, int>( + keyedFieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(keyedFieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync, int>( + keyedFieldExpression, options, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(keyedFieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync, int>( + keyedFieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(keyedFieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync, int>( + keyedFieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(keyedFieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync, int>( + keyedFieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(keyedFieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync, int>( + keyedFieldExpression, options, partitionKey, token)); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/DropIndexAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/DropIndexAsyncTests.cs new file mode 100644 index 0000000..16386b1 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/DropIndexAsyncTests.cs @@ -0,0 +1,77 @@ +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.IndexTests; + +public class DropIndexAsyncTests : TestKeyedMongoRepositoryContext +{ + [Fact] + public async Task WitIndexName_DropsIndex() + { + // Arrange + var indexName = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.DropIndexAsync>(indexName); + + // Assert + IndexHandler.Verify( + x => x.DropIndexAsync, int>(indexName, null, CancellationToken.None)); + } + + [Fact] + public async Task WitIndexNameAndCancellationToken_DropsIndex() + { + // Arrange + var indexName = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.DropIndexAsync>(indexName, token); + + // Assert + IndexHandler.Verify( + x => x.DropIndexAsync, int>(indexName, null, token)); + } + + [Fact] + public async Task WitIndexNameAndPartitionKey_DropsIndex() + { + // Arrange + var indexName = Fixture.Create(); + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.DropIndexAsync>(indexName, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.DropIndexAsync, int>(indexName, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WitIndexNameAndPartitionKeyAndCancellationToken_DropsIndex() + { + // Arrange + var indexName = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.DropIndexAsync>(indexName, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.DropIndexAsync, int>(indexName, partitionKey, token)); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs new file mode 100644 index 0000000..2d385bf --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs @@ -0,0 +1,98 @@ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.IndexTests; + +public class GetIndexNamesAsyncTests : TestKeyedMongoRepositoryContext +{ + [Fact] + public async Task WithNoParameters_ReturnsIndexNames() + { + // Arrange + IndexHandler = new Mock(); + var indexName = Fixture.Create(); + + IndexHandler + .Setup(x => x.GetIndexesNamesAsync, int>(It.IsAny(), It.IsAny())) + .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, int>(null, CancellationToken.None), Times.Once()); + } + + [Fact] + public async Task WithCancellationToken_ReturnsIndexNames() + { + // Arrange + IndexHandler = new Mock(); + var indexName = Fixture.Create(); + var token = new CancellationToken(true); + + IndexHandler + .Setup(x => x.GetIndexesNamesAsync, int>(It.IsAny(), It.IsAny())) + .ReturnsAsync(new List { indexName }); + + // Act + var result = await Sut.GetIndexesNamesAsync>(token); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + IndexHandler.Verify(x => x.GetIndexesNamesAsync, int>(null, token), Times.Once()); + } + + [Fact] + public async Task WithPartitionKey_ReturnsIndexNames() + { + // Arrange + IndexHandler = new Mock(); + var indexName = Fixture.Create(); + var partitionKey = Fixture.Create(); + + IndexHandler + .Setup(x => x.GetIndexesNamesAsync, int>(It.IsAny(), It.IsAny())) + .ReturnsAsync(new List { indexName }); + + // Act + var result = await Sut.GetIndexesNamesAsync>(partitionKey); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + IndexHandler.Verify(x => x.GetIndexesNamesAsync, int>(partitionKey, CancellationToken.None), Times.Once()); + } + + [Fact] + public async Task WithPartitionKeyAndCancellationToken_ReturnsIndexNames() + { + // Arrange + IndexHandler = new Mock(); + var indexName = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + IndexHandler + .Setup(x => x.GetIndexesNamesAsync, int>(It.IsAny(), It.IsAny())) + .ReturnsAsync(new List { indexName }); + + // Act + var result = await Sut.GetIndexesNamesAsync>(partitionKey, token); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + IndexHandler.Verify(x => x.GetIndexesNamesAsync, int>(partitionKey, token), Times.Once()); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateManyAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateManyAsyncTests.cs new file mode 100644 index 0000000..f8d663c --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateManyAsyncTests.cs @@ -0,0 +1,533 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Update; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.UpdateTests; + +public class UpdateManyAsyncTests : TestKeyedMongoRepositoryContext +{ + private readonly UpdateDefinition> updateDefinition = Builders>.Update.Set(x => x.SomeContent, "Updated"); + private readonly Expression, string>> fieldExpression = x => x.SomeContent; + private readonly FilterDefinition> filterDefinition = Builders>.Filter.Eq(x => x.Id, 1); + private readonly Expression, bool>> filterExpression = x => x.SomeContent == "SomeContent"; + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var count = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + filterDefinition, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + filterDefinition, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + filterDefinition, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + filterDefinition, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + filterExpression, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + filterExpression, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + filterExpression, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + filterExpression, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + filterExpression, + updateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + filterExpression, + updateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + filterExpression, + updateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + filterExpression, + updateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + filterDefinition, + updateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndUpdateDefinitionCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + filterDefinition, + updateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + filterDefinition, + updateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + filterDefinition, + updateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateManyTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateManyTests.cs new file mode 100644 index 0000000..01425e6 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateManyTests.cs @@ -0,0 +1,532 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Update; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.UpdateTests; + +public class UpdateManyTests : TestKeyedMongoRepositoryContext +{ + private readonly Expression, string>> fieldExpression = x => x.SomeContent; + private readonly FilterDefinition> filterDefinition = Builders>.Filter.Eq(x => x.Id, 1); + private readonly Expression, bool>> filterExpression = x => x.SomeContent == "SomeContent"; + private readonly UpdateDefinition> updateDefinition = Builders>.Update.Set(x => x.SomeContent, "Updated"); + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var count = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + filterDefinition, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + filterDefinition, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + filterDefinition, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + filterDefinition, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + filterExpression, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + filterExpression, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + filterExpression, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + filterExpression, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + filterExpression, + updateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + filterExpression, + updateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, updateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + filterExpression, + updateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, updateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + filterExpression, + updateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + filterDefinition, + updateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndUpdateDefinitionCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + filterDefinition, + updateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, updateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + filterDefinition, + updateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, updateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + filterDefinition, + updateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateOneAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateOneAsyncTests.cs new file mode 100644 index 0000000..2e49f52 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateOneAsyncTests.cs @@ -0,0 +1,432 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Update; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.UpdateTests; + +public class UpdateOneAsyncTests : TestKeyedMongoRepositoryContext +{ + private readonly UpdateDefinition> updateDefinition = Builders>.Update.Set(x => x.SomeContent, "Updated"); + private readonly Expression, string>> fieldExpression = x => x.SomeContent; + private readonly FilterDefinition> filterDefinition = Builders>.Filter.Eq(x => x.Id, 1); + private readonly Expression, bool>> filterExpression = x => x.SomeContent == "SomeContent"; + + [Fact] + public async Task WithDocument_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOneAsync, int>(It.IsAny>(), It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document); + + // Assert + Updater.Verify(x => x.UpdateOneAsync, int>(document, CancellationToken.None), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOneAsync, int>(It.IsAny>(), It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, token); + + // Assert + Updater.Verify(x => x.UpdateOneAsync, int>(document, token), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int>( + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int>( + document, + updateDefinition, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int>( + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int>( + document, + updateDefinition, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + document, + fieldExpression, + value, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + document, + fieldExpression, + value, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + filterDefinition, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + filterDefinition, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + filterDefinition, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + filterDefinition, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + filterExpression, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + filterExpression, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + filterExpression, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + filterExpression, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateOneTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateOneTests.cs new file mode 100644 index 0000000..1dd486e --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateOneTests.cs @@ -0,0 +1,432 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Update; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.UpdateTests; + +public class UpdateOneTests : TestKeyedMongoRepositoryContext +{ + private readonly UpdateDefinition> updateDefinition = Builders>.Update.Set(x => x.SomeContent, "Updated"); + private readonly Expression, string>> fieldExpression = x => x.SomeContent; + private readonly FilterDefinition> filterDefinition = Builders>.Filter.Eq(x => x.Id, 1); + private readonly Expression, bool>> filterExpression = x => x.SomeContent == "SomeContent"; + + [Fact] + public void WithDocument_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOne, int>(It.IsAny>(), It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document); + + // Assert + Updater.Verify(x => x.UpdateOne, int>(document, CancellationToken.None), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOne, int>(It.IsAny>(), It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, token); + + // Assert + Updater.Verify(x => x.UpdateOne, int>(document, token), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int>( + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateOne, int>( + document, + updateDefinition, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int>( + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int>( + document, + updateDefinition, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + document, + fieldExpression, + value, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + document, + fieldExpression, + value, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterDefinition, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + filterDefinition, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterDefinition, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + filterDefinition, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterDefinition, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + filterDefinition, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterDefinition, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + filterDefinition, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterExpression, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + filterExpression, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterExpression, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + filterExpression, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterExpression, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + filterExpression, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterExpression, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + filterExpression, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyAsyncTests.cs new file mode 100644 index 0000000..a3a06fb --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyAsyncTests.cs @@ -0,0 +1,101 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class AnyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + + [Fact] + public async Task WithFilter_GetsResult() + { + // Arrange + SetupReader(); + + // Act + var result = await Sut.AnyAsync(filter); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync, int>(filter, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndCancellationToken_GetsResult() + { + // Arrange + var token = new CancellationToken(true); + + SetupReader(); + + // Act + var result = await Sut.AnyAsync(filter, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync, int>(filter, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKey_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + + SetupReader(); + + // Act + var result = await Sut.AnyAsync(filter, partitionKey); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync, int>(filter, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKeyAndCancellationToken_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(); + + // Act + var result = await Sut.AnyAsync(filter, partitionKey, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync, int>(filter, partitionKey, token), + Times.Once); + } + + private void SetupReader() + { + Reader = new Mock(); + Reader + .Setup( + x => x.AnyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyTests.cs new file mode 100644 index 0000000..2918a54 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyTests.cs @@ -0,0 +1,100 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class AnyTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + + [Fact] + public void WithFilter_GetsResult() + { + // Arrange + SetupReader(); + + // Act + var result = Sut.Any(filter); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any, int>(filter, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndCancellationToken_GetsResult() + { + // Arrange + var token = new CancellationToken(true); + + SetupReader(); + + // Act + var result = Sut.Any(filter, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any, int>(filter, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKey_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + + SetupReader(); + + // Act + var result = Sut.Any(filter, partitionKey); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any, int>(filter, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKeyAndCancellationToken_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(); + + // Act + var result = Sut.Any(filter, partitionKey, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any, int>(filter, partitionKey, token), + Times.Once); + } + + private void SetupReader() + { + Reader = new Mock(); + Reader + .Setup( + x => x.Any, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/CountAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/CountAsyncTests.cs new file mode 100644 index 0000000..902c905 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/CountAsyncTests.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class CountAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + + [Fact] + public async Task WithFilter_GetsOne() + { + // Arrange + var count = Fixture.Create(); + + SetupReader(count); + + // Act + var result = await Sut.CountAsync(filter); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync, int>(filter, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndCancellationToken_GetsOne() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(count); + + // Act + var result = await Sut.CountAsync(filter, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync, int>(filter, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKey_GetsOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(count); + + // Act + var result = await Sut.CountAsync(filter, partitionKey); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync, int>(filter, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(count); + + // Act + var result = await Sut.CountAsync(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync, int>(filter, partitionKey, token), + Times.Once); + } + + private void SetupReader(long count) + { + Reader = new Mock(); + Reader + .Setup( + x => x.CountAsync, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/CountTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/CountTests.cs new file mode 100644 index 0000000..c1f0626 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/CountTests.cs @@ -0,0 +1,105 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class CountTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + + [Fact] + public void WithFilter_GetsOne() + { + // Arrange + var count = Fixture.Create(); + + SetupReader(count); + + // Act + var result = Sut.Count(filter); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count, int>(filter, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndCancellationToken_GetsOne() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(count); + + // Act + var result = Sut.Count(filter, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count, int>(filter, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKey_GetsOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(count); + + // Act + var result = Sut.Count(filter, partitionKey); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count, int>(filter, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(count); + + // Act + var result = Sut.Count(filter, partitionKey, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count, int>(filter, partitionKey, token), + Times.Once); + } + + private void SetupReader(long count) + { + Reader = new Mock(); + Reader + .Setup( + x => x.Count, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetAllAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetAllAsyncTests.cs new file mode 100644 index 0000000..049f5db --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetAllAsyncTests.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class GetAllAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + + [Fact] + public async Task WithFilter_GetsOne() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + + SetupReader(document); + + // Act + var result = await Sut.GetAllAsync(filter); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync, int>(filter, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetAllAsync(filter, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync, int>(filter, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetAllAsync(filter, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync, int>(filter, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetAllAsync(filter, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync, int>(filter, partitionKey, token), + Times.Once); + } + + private void SetupReader(List> documents) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetAllAsync, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetAllTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetAllTests.cs new file mode 100644 index 0000000..82aac09 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetAllTests.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class GetAllTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + + [Fact] + public void WithFilter_GetsOne() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + + SetupReader(document); + + // Act + var result = Sut.GetAll(filter); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll, int>(filter, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetAll(filter, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll, int>(filter, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = Sut.GetAll(filter, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll, int>(filter, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetAll(filter, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll, int>(filter, partitionKey, token), + Times.Once); + } + + private void SetupReader(List> documents) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetAll, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(documents); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdAsyncTests.cs new file mode 100644 index 0000000..6d9e8c2 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdAsyncTests.cs @@ -0,0 +1,106 @@ +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class GetByIdAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +{ + [Fact] + public async Task WithId_Gets() + { + // Arrange + var document = Fixture.Create>(); + + SetupReader(document); + + // Act + var result = await Sut.GetByIdAsync>(document.Id); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByIdAsync, int>(document.Id, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithIdAndCancellationToken_Gets() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetByIdAsync>(document.Id, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByIdAsync, int>(document.Id, null, token), + Times.Once); + } + + [Fact] + public async Task WithIdAndPartitionKey_Gets() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetByIdAsync>(document.Id, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByIdAsync, int>(document.Id, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithIdAndPartitionKeyAndCancellationToken_Gets() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetByIdAsync>(document.Id, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByIdAsync, int>(document.Id, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetByIdAsync, int>( + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(document); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdTests.cs new file mode 100644 index 0000000..f9cef3d --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdTests.cs @@ -0,0 +1,105 @@ +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class GetByIdTests : TestKeyedReadOnlyMongoRepositoryContext +{ + [Fact] + public void WithId_Gets() + { + // Arrange + var document = Fixture.Create>(); + + SetupReader(document); + + // Act + var result = Sut.GetById>(document.Id); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetById, int>(document.Id, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithIdAndCancellationToken_Gets() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetById>(document.Id, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetById, int>(document.Id, null, token), + Times.Once); + } + + [Fact] + public void WithIdAndPartitionKey_Gets() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = Sut.GetById>(document.Id, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetById, int>(document.Id, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithIdAndPartitionKeyAndCancellationToken_Gets() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetById>(document.Id, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetById, int>(document.Id, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetById, int>( + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(document); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxAsyncTests.cs new file mode 100644 index 0000000..00f8fba --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxAsyncTests.cs @@ -0,0 +1,112 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class GetByMaxAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, object>> selector = document => document.SomeValue; + + [Fact] + public async Task WithFilterAndSelector_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + + SetupReader(document); + + // Act + var result = await Sut.GetByMaxAsync(filter, selector); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMaxAsync, int>(filter, selector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetByMaxAsync(filter, selector, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMaxAsync, int>(filter, selector, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetByMaxAsync(filter, selector, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMaxAsync, int>(filter, selector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetByMaxAsync(filter, selector, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMaxAsync, int>(filter, selector, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetByMaxAsync, int>( + It.IsAny, bool>>>(), + It.IsAny, object>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(document); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxTests.cs new file mode 100644 index 0000000..b694cdb --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxTests.cs @@ -0,0 +1,111 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class GetByMaxTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, object>> selector = document => document.SomeValue; + + [Fact] + public void WithFilterAndSelector_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + + SetupReader(document); + + // Act + var result = Sut.GetByMax(filter, selector); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMax, int>(filter, selector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetByMax(filter, selector, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMax, int>(filter, selector, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = Sut.GetByMax(filter, selector, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMax, int>(filter, selector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetByMax(filter, selector, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMax, int>(filter, selector, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetByMax, int>( + It.IsAny, bool>>>(), + It.IsAny, object>>>(), + It.IsAny(), + It.IsAny())) + .Returns(document); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMinAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMinAsyncTests.cs new file mode 100644 index 0000000..02032b1 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMinAsyncTests.cs @@ -0,0 +1,112 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class GetByMinAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, object>> selector = document => document.SomeValue; + + [Fact] + public async Task WithFilterAndSelector_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + + SetupReader(document); + + // Act + var result = await Sut.GetByMinAsync(filter, selector); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMinAsync, int>(filter, selector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetByMinAsync(filter, selector, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMinAsync, int>(filter, selector, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetByMinAsync(filter, selector, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMinAsync, int>(filter, selector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetByMinAsync(filter, selector, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMinAsync, int>(filter, selector, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetByMinAsync, int>( + It.IsAny, bool>>>(), + It.IsAny, object>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(document); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMinTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMinTests.cs new file mode 100644 index 0000000..9efd7f2 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMinTests.cs @@ -0,0 +1,111 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class GetByMinTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, object>> selector = document => document.SomeValue; + + [Fact] + public void WithFilterAndSelector_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + + SetupReader(document); + + // Act + var result = Sut.GetByMin(filter, selector); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMin, int>(filter, selector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetByMin(filter, selector, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMin, int>(filter, selector, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = Sut.GetByMin(filter, selector, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMin, int>(filter, selector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetByMin(filter, selector, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMin, int>(filter, selector, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetByMin, int>( + It.IsAny, bool>>>(), + It.IsAny, object>>>(), + It.IsAny(), + It.IsAny())) + .Returns(document); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMaxValueAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMaxValueAsyncTests.cs new file mode 100644 index 0000000..5b16f95 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMaxValueAsyncTests.cs @@ -0,0 +1,108 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class GetMaxValueAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, int>> selector = document => document.SomeValue; + + [Fact] + public async Task WithFilterAndSelector_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + + SetupReader(value); + + // Act + var result = await Sut.GetMaxValueAsync(filter, selector); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValueAsync, int, int>(filter, selector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(value); + + // Act + var result = await Sut.GetMaxValueAsync(filter, selector, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValueAsync, int, int>(filter, selector, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKey_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(value); + + // Act + var result = await Sut.GetMaxValueAsync(filter, selector, partitionKey); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValueAsync, int, int>(filter, selector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(value); + + // Act + var result = await Sut.GetMaxValueAsync(filter, selector, partitionKey, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValueAsync, int, int>(filter, selector, partitionKey, token), + Times.Once); + } + + private void SetupReader(int value) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetMaxValueAsync, int, int>( + It.IsAny, bool>>>(), + It.IsAny, int>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(value); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMaxValueTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMaxValueTests.cs new file mode 100644 index 0000000..ada314c --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMaxValueTests.cs @@ -0,0 +1,107 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class GetMaxValueTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, int>> selector = document => document.SomeValue; + + [Fact] + public void WithFilterAndSelector_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + + SetupReader(value); + + // Act + var result = Sut.GetMaxValue(filter, selector); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValue, int, int>(filter, selector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(value); + + // Act + var result = Sut.GetMaxValue(filter, selector, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValue, int, int>(filter, selector, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKey_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(value); + + // Act + var result = Sut.GetMaxValue(filter, selector, partitionKey); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValue, int, int>(filter, selector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(value); + + // Act + var result = Sut.GetMaxValue(filter, selector, partitionKey, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValue, int, int>(filter, selector, partitionKey, token), + Times.Once); + } + + private void SetupReader(int value) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetMaxValue, int, int>( + It.IsAny, bool>>>(), + It.IsAny, int>>>(), + It.IsAny(), + It.IsAny())) + .Returns(value); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMinValueAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMinValueAsyncTests.cs new file mode 100644 index 0000000..375d672 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMinValueAsyncTests.cs @@ -0,0 +1,108 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class GetMinValueAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, int>> selector = document => document.SomeValue; + + [Fact] + public async Task WithFilterAndSelector_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + + SetupReader(value); + + // Act + var result = await Sut.GetMinValueAsync(filter, selector); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValueAsync, int, int>(filter, selector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(value); + + // Act + var result = await Sut.GetMinValueAsync(filter, selector, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValueAsync, int, int>(filter, selector, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKey_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(value); + + // Act + var result = await Sut.GetMinValueAsync(filter, selector, partitionKey); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValueAsync, int, int>(filter, selector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(value); + + // Act + var result = await Sut.GetMinValueAsync(filter, selector, partitionKey, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValueAsync, int, int>(filter, selector, partitionKey, token), + Times.Once); + } + + private void SetupReader(int value) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetMinValueAsync, int, int>( + It.IsAny, bool>>>(), + It.IsAny, int>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(value); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMinValueTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMinValueTests.cs new file mode 100644 index 0000000..05a6c18 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMinValueTests.cs @@ -0,0 +1,107 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class GetMinValueTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, int>> selector = document => document.SomeValue; + + [Fact] + public void WithFilterAndSelector_GetsMinValue() + { + // Arrange + var value = Fixture.Create(); + + SetupReader(value); + + // Act + var result = Sut.GetMinValue(filter, selector); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValue, int, int>(filter, selector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndCancellationToken_GetsMinValue() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(value); + + // Act + var result = Sut.GetMinValue(filter, selector, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValue, int, int>(filter, selector, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKey_GetsMinValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(value); + + // Act + var result = Sut.GetMinValue(filter, selector, partitionKey); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValue, int, int>(filter, selector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMinValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(value); + + // Act + var result = Sut.GetMinValue(filter, selector, partitionKey, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValue, int, int>(filter, selector, partitionKey, token), + Times.Once); + } + + private void SetupReader(int value) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetMinValue, int, int>( + It.IsAny, bool>>>(), + It.IsAny, int>>>(), + It.IsAny(), + It.IsAny())) + .Returns(value); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs new file mode 100644 index 0000000..01efd4c --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs @@ -0,0 +1,110 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + + [Fact] + public async Task WithFilter_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + + SetupReader(document); + + // Act + var result = await Sut.GetOneAsync(filter); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync, int>(filter, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetOneAsync(filter, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync, int>(filter, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetOneAsync(filter, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync, int>(filter, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetOneAsync(filter, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync, int>(filter, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetOneAsync, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(document); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneTests.cs new file mode 100644 index 0000000..803e999 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneTests.cs @@ -0,0 +1,109 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class GetOneTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + + [Fact] + public void WithFilter_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + + SetupReader(document); + + // Act + var result = Sut.GetOne(filter); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne, int>(filter, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetOne(filter, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne, int>(filter, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = Sut.GetOne(filter, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne, int>(filter, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetOne(filter, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne, int>(filter, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetOne, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(document); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs new file mode 100644 index 0000000..189b5b1 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs @@ -0,0 +1,246 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class GetSortedPaginatedAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.GroupingKey == 1; + private readonly Expression, object>> selector = document => document.GroupingKey; + private readonly SortDefinition> sortDefinition = Builders>.Sort.Ascending(document => document.GroupingKey); + + private const bool DefaultAscending = true; + private const int DefaultSkipNumber = 0; + private const int DefaultTakeNumber = 50; + + [Fact] + public async Task WithFilterAndSortSelector_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortSelector(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, selector); + + // Assert + VerifySelector(result, documents, DefaultAscending, DefaultSkipNumber, DefaultTakeNumber, null, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortSelectorAndAscending_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortSelector(); + var ascending = Fixture.Create(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, selector, ascending); + + // Assert + VerifySelector(result, documents, ascending, DefaultSkipNumber, DefaultTakeNumber, null, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortSelectorAndSkipNumber_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortSelector(); + var skipNumber = Fixture.Create(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, selector, skipNumber: skipNumber); + + // Assert + VerifySelector(result, documents, DefaultAscending, skipNumber, DefaultTakeNumber, null, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortSelectorAndTakeNumber_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortSelector(); + var ascending = Fixture.Create(); + var takeNumber = Fixture.Create(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, selector, ascending, takeNumber: takeNumber); + + // Assert + VerifySelector(result, documents, ascending, DefaultSkipNumber, takeNumber, null, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortSelectorAndPartitionKey_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortSelector(); + var partitionKey = Fixture.Create(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, selector, partitionKey: partitionKey); + + // Assert + VerifySelector(result, documents, DefaultAscending, DefaultSkipNumber, DefaultTakeNumber, partitionKey, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortSelectorAndCancellationToken_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortSelector(); + var cancellationToken = new CancellationToken(true); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, selector, cancellationToken: cancellationToken); + + // Assert + VerifySelector(result, documents, DefaultAscending, DefaultSkipNumber, DefaultTakeNumber, null, cancellationToken); + } + + [Fact] + public async Task WithFilterAndSortDefinition_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortDefinition(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, sortDefinition); + + // Assert + VerifyDefinition(result, documents, DefaultSkipNumber, DefaultTakeNumber, null, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortDefinitionAndSkipNumber_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortDefinition(); + var skipNumber = Fixture.Create(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, sortDefinition, skipNumber); + + // Assert + VerifyDefinition(result, documents, skipNumber, DefaultTakeNumber, null, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortDefinitionAndTakeNumber_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortDefinition(); + var takeNumber = Fixture.Create(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, sortDefinition, takeNumber: takeNumber); + + // Assert + VerifyDefinition(result, documents, DefaultSkipNumber, takeNumber, null, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortDefinitionAndPartitionKey_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortDefinition(); + var partitionKey = Fixture.Create(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, sortDefinition, partitionKey: partitionKey); + + // Assert + VerifyDefinition(result, documents, DefaultSkipNumber, DefaultTakeNumber, partitionKey, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortDefinitionAndCancellationToken_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortDefinition(); + var token = new CancellationToken(true); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, sortDefinition, cancellationToken: token); + + // Assert + VerifyDefinition(result, documents, DefaultSkipNumber, DefaultTakeNumber, null, token); + } + + private List> SetupReaderWithSortSelector() + { + var documents = Fixture.CreateMany>().ToList(); + Reader = new Mock(); + + Reader.Setup( + x => x.GetSortedPaginatedAsync, int>( + It.IsAny, bool>>>(), + It.IsAny, object>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents); + return documents; + } + + private void VerifySelector(List> result, List> documents, bool ascending, int skipNumber, int takeNumber, string partitionKey, CancellationToken cancellationToken) + { + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(documents); + Reader.Verify( + x => x.GetSortedPaginatedAsync, int>( + filter, + selector, + ascending, + skipNumber, + takeNumber, + partitionKey, + cancellationToken), + Times.Once); + } + + private List> SetupReaderWithSortDefinition() + { + var documents = Fixture.CreateMany>().ToList(); + Reader = new Mock(); + + Reader.Setup( + x => x.GetSortedPaginatedAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents); + return documents; + } + + private void VerifyDefinition(List> result, List> documents, int skipNumber, int takeNumber, string partitionKey, CancellationToken cancellationToken) + { + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(documents); + Reader.Verify( + x => x.GetSortedPaginatedAsync, int>( + filter, + sortDefinition, + skipNumber, + takeNumber, + partitionKey, + cancellationToken), + Times.Once); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GroupByTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GroupByTests.cs new file mode 100644 index 0000000..4c39155 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GroupByTests.cs @@ -0,0 +1,243 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, int>> grouping = document => document.GroupingKey; + private readonly Expression>, TestProjection>> projection = documents => new TestProjection {Count = documents.Count()}; + private readonly Expression, bool>> filter = document => document.GroupingKey == 1; + + [Fact] + public void WithGroupingCriteriaAndProjection_Groups() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + Reader = new Mock(); + + Reader.Setup( + x => x.GroupBy, int, TestProjection, int>( + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + + // Act + var result = Sut.GroupBy(grouping, projection); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(projections); + Reader.Verify( + x => x.GroupBy, int, TestProjection, int>(grouping, projection, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithGroupingCriteriaAndProjectionAndCancellationToken_Groups() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + Reader = new Mock(); + + Reader + .Setup( + x => x.GroupBy, int, TestProjection, int>( + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + + // Act + var result = Sut.GroupBy(grouping, projection, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(projections); + Reader.Verify( + x => x.GroupBy, int, TestProjection, int>(grouping, projection, null, token), + Times.Once); + } + + [Fact] + public void WithGroupingCriteriaAndProjectionAndPartitionKey_Groups() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + Reader = new Mock(); + + Reader + .Setup( + x => x.GroupBy, int, TestProjection, int>( + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + + // Act + var result = Sut.GroupBy(grouping, projection, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(projections); + Reader.Verify( + x => x.GroupBy, int, TestProjection, int>(grouping, projection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithGroupingCriteriaAndProjectionAndPartitionKeyAndCancellationToken_Groups() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Reader = new Mock(); + + Reader + .Setup( + x => x.GroupBy, int, TestProjection, int>( + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + + // Act + var result = Sut.GroupBy(grouping, projection, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(projections); + Reader.Verify( + x => x.GroupBy, int, TestProjection, int>(grouping, projection, partitionKey, token), + Times.Once); + } + + [Fact] + public void WithFilterAndGroupingCriteriaAndProjection_Groups() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + Reader = new Mock(); + + Reader.Setup( + x => x.GroupBy, int, TestProjection, int>( + It.IsAny,bool>>>(), + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + + // Act + var result = Sut.GroupBy(filter, grouping, projection); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(projections); + Reader.Verify( + x => x.GroupBy, int, TestProjection, int>(filter, grouping, projection, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndGroupingCriteriaAndProjectionAndCancellationToken_Groups() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + Reader = new Mock(); + + Reader.Setup( + x => x.GroupBy, int, TestProjection, int>( + It.IsAny,bool>>>(), + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + + // Act + var result = Sut.GroupBy(filter, grouping, projection, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(projections); + Reader.Verify( + x => x.GroupBy, int, TestProjection, int>(filter, grouping, projection, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndGroupingCriteriaAndProjectionAndPartitionKey_Groups() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + Reader = new Mock(); + + Reader.Setup( + x => x.GroupBy, int, TestProjection, int>( + It.IsAny,bool>>>(), + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + + // Act + var result = Sut.GroupBy(filter, grouping, projection, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(projections); + Reader.Verify( + x => x.GroupBy, int, TestProjection, int>(filter, grouping, projection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndGroupingCriteriaAndProjectionAndPartitionKeyAndCancellationToken_Groups() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Reader = new Mock(); + + Reader.Setup( + x => x.GroupBy, int, TestProjection, int>( + It.IsAny,bool>>>(), + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + + // Act + var result = Sut.GroupBy(filter, grouping, projection, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(projections); + Reader.Verify( + x => x.GroupBy, int, TestProjection, int>(filter, grouping, projection, partitionKey, token), + Times.Once); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyAsyncTests.cs new file mode 100644 index 0000000..1b2eca6 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyAsyncTests.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class ProjectManyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, TestProjection>> projection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + + [Fact] + public async Task WithFilterAndProjection_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + + SetupReader(projections); + + // Act + var result = await Sut.ProjectManyAsync(filter, projection); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectManyAsync, TestProjection, int>( + filter, + projection, + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + + SetupReader(projections); + + // Act + var result = await Sut.ProjectManyAsync(filter, projection, token); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectManyAsync, TestProjection, int>(filter, projection, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + SetupReader(projections); + + // Act + var result = await Sut.ProjectManyAsync(filter, projection, partitionKey); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectManyAsync, TestProjection, int>(filter, projection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(projections); + + // Act + var result = await Sut.ProjectManyAsync(filter, projection, partitionKey, token); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectManyAsync, TestProjection, int>(filter, projection, partitionKey, token), + Times.Once); + } + + private void SetupReader(List projections) + { + Reader = new Mock(); + Reader + .Setup( + x => x.ProjectManyAsync, TestProjection, int>( + It.IsAny, bool>>>(), + It.IsAny, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(projections); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyTests.cs new file mode 100644 index 0000000..324090e --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyTests.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class ProjectManyTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, TestProjection>> projection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + + [Fact] + public void WithFilterAndProjection_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + + SetupReader(projections); + + // Act + var result = Sut.ProjectMany(filter, projection); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectMany, TestProjection, int>( + filter, + projection, + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + + SetupReader(projections); + + // Act + var result = Sut.ProjectMany(filter, projection, token); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectMany, TestProjection, int>(filter, projection, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + SetupReader(projections); + + // Act + var result = Sut.ProjectMany(filter, projection, partitionKey); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectMany, TestProjection, int>(filter, projection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(projections); + + // Act + var result = Sut.ProjectMany(filter, projection, partitionKey, token); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectMany, TestProjection, int>(filter, projection, partitionKey, token), + Times.Once); + } + + private void SetupReader(List projections) + { + Reader = new Mock(); + Reader + .Setup( + x => x.ProjectMany, TestProjection, int>( + It.IsAny, bool>>>(), + It.IsAny, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneAsyncTests.cs new file mode 100644 index 0000000..4d160f9 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneAsyncTests.cs @@ -0,0 +1,112 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class ProjectOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, TestProjection>> projection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + + [Fact] + public async Task WithFilterAndProjection_Projects() + { + // Arrange + var expected = Fixture.Create(); + + SetupReader(expected); + + // Act + var result = await Sut.ProjectOneAsync(filter, projection); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOneAsync, TestProjection, int>( + filter, + projection, + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var expected = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(expected); + + // Act + var result = await Sut.ProjectOneAsync(filter, projection, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOneAsync, TestProjection, int>(filter, projection, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(expected); + + // Act + var result = await Sut.ProjectOneAsync(filter, projection, partitionKey); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOneAsync, TestProjection, int>(filter, projection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(expected); + + // Act + var result = await Sut.ProjectOneAsync(filter, projection, partitionKey, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOneAsync, TestProjection, int>(filter, projection, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestProjection result) + { + Reader = new Mock(); + Reader + .Setup( + x => x.ProjectOneAsync, TestProjection, int>( + It.IsAny, bool>>>(), + It.IsAny, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(result); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneTests.cs new file mode 100644 index 0000000..d7b5eb3 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneTests.cs @@ -0,0 +1,111 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class ProjectOneTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, TestProjection>> projection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + + [Fact] + public void WithFilterAndProjection_Projects() + { + // Arrange + var expected = Fixture.Create(); + + SetupReader(expected); + + // Act + var result = Sut.ProjectOne(filter, projection); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOne, TestProjection, int>( + filter, + projection, + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var expected = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(expected); + + // Act + var result = Sut.ProjectOne(filter, projection, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOne, TestProjection, int>(filter, projection, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(expected); + + // Act + var result = Sut.ProjectOne(filter, projection, partitionKey); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOne, TestProjection, int>(filter, projection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(expected); + + // Act + var result = Sut.ProjectOne(filter, projection, partitionKey, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOne, TestProjection, int>(filter, projection, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestProjection result) + { + Reader = new Mock(); + Reader + .Setup( + x => x.ProjectOne, TestProjection, int>( + It.IsAny, bool>>>(), + It.IsAny, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(result); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/SumByAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/SumByAsyncTests.cs new file mode 100644 index 0000000..43f613f --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/SumByAsyncTests.cs @@ -0,0 +1,198 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class SumByAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, int>> intSelector = document => document.SomeValue; + private readonly Expression, decimal>> decimalSelector = document => document.SomeDecimalValue; + + [Fact] + public async Task Int_WithFilterAndSelector_Sums() + { + // Arrange + var expected = Fixture.Create(); + + SetupReaderInt(expected); + + // Act + var result = await Sut.SumByAsync(filter, intSelector); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync, int>(filter, intSelector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Int_WithFilterAndSelectorAndCancellationToken_Sums() + { + // Arrange + var expected = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReaderInt(expected); + + // Act + var result = await Sut.SumByAsync(filter, intSelector, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync, int>(filter, intSelector, null, token), + Times.Once); + } + + [Fact] + public async Task Int_WithFilterAndSelectorAndPartitionKey_Sums() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReaderInt(expected); + + // Act + var result = await Sut.SumByAsync(filter, intSelector, partitionKey); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync, int>(filter, intSelector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Int_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_Sums() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReaderInt(expected); + + // Act + var result = await Sut.SumByAsync(filter, intSelector, partitionKey, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync, int>(filter, intSelector, partitionKey, token), + Times.Once); + } + + [Fact] + public async Task Decimal_WithFilterAndSelector_Sums() + { + // Arrange + var expected = Fixture.Create(); + + SetupReaderDecimal(expected); + + // Act + var result = await Sut.SumByAsync(filter, decimalSelector); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync, int>(filter, decimalSelector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Decimal_WithFilterAndSelectorAndCancellationToken_Sums() + { + // Arrange + var expected = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReaderDecimal(expected); + + // Act + var result = await Sut.SumByAsync(filter, decimalSelector, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync, int>(filter, decimalSelector, null, token), + Times.Once); + } + + [Fact] + public async Task Decimal_WithFilterAndSelectorAndPartitionKey_Sums() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReaderDecimal(expected); + + // Act + var result = await Sut.SumByAsync(filter, decimalSelector, partitionKey); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync, int>(filter, decimalSelector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Decimal_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_Sums() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReaderDecimal(expected); + + // Act + var result = await Sut.SumByAsync(filter, decimalSelector, partitionKey, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync, int>(filter, decimalSelector, partitionKey, token), + Times.Once); + } + + private void SetupReaderInt(int expected) + { + Reader = new Mock(); + Reader + .Setup( + x => x.SumByAsync, int>( + It.IsAny, bool>>>(), + It.IsAny,int>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(expected); + } + + private void SetupReaderDecimal(decimal expected) + { + Reader = new Mock(); + Reader + .Setup( + x => x.SumByAsync, int>( + It.IsAny, bool>>>(), + It.IsAny, decimal>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(expected); + } +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/AnyAsyncTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/AnyAsyncTests.cs new file mode 100644 index 0000000..8957937 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/AnyAsyncTests.cs @@ -0,0 +1,349 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class AnyAsyncTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> expression = document => document.SomeContent == "SomeContent"; + private readonly Expression, bool>> keyedExpression = document => document.SomeContent == "SomeContent"; + private readonly FilterDefinition> keyedFilter = Builders>.Filter.Eq(x => x.Id, 1); + + [Fact] + public async Task WithExpression_GetsResult() + { + // Arrange + SetupReader(); + + // Act + var result = await Sut.AnyAsync(expression); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync(expression, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithExpressionAndCancellationToken_GetsResult() + { + // Arrange + var token = new CancellationToken(true); + + SetupReader(); + + // Act + var result = await Sut.AnyAsync(expression, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync(expression, null, token), + Times.Once); + } + + [Fact] + public async Task WithExpressionAndPartitionKey_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + + SetupReader(); + + // Act + var result = await Sut.AnyAsync(expression, partitionKey); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync(expression, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithExpressionAndPartitionKeyAndCancellationToken_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(); + + // Act + var result = await Sut.AnyAsync(expression, partitionKey, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync(expression, partitionKey, token), + Times.Once); + } + + #region keyed + + [Fact] + public async Task Keyed_WithExpression_GetsResult() + { + // Arrange + SetupKeyedReaderWithExpression(); + + // Act + var result = await Sut.AnyAsync, int>(keyedExpression); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync, int>(keyedExpression, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithExpressionAndCancellationToken_GetsResult() + { + // Arrange + var token = new CancellationToken(true); + + SetupKeyedReaderWithExpression(); + + // Act + var result = await Sut.AnyAsync, int>(keyedExpression, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync, int>(keyedExpression, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithExpressionAndPartitionKey_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + + SetupKeyedReaderWithExpression(); + + // Act + var result = await Sut.AnyAsync, int>(keyedExpression, partitionKey); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync, int>(keyedExpression, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithExpressionAndPartitionKeyAndCancellationToken_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderWithExpression(); + + // Act + var result = await Sut.AnyAsync, int>(keyedExpression, partitionKey, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync, int>(keyedExpression, partitionKey, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilter_GetsResult() + { + // Arrange + SetupKeyedReaderWithFilter(); + + // Act + var result = await Sut.AnyAsync, int>(keyedFilter); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync, int>(keyedFilter, null, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndOptions_GetsResult() + { + // Arrange + var options = new CountOptions(); + SetupKeyedReaderWithFilter(); + + // Act + var result = await Sut.AnyAsync, int>(keyedFilter, options); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync, int>(keyedFilter, options, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndCancellationToken_GetsResult() + { + // Arrange + var token = new CancellationToken(true); + + SetupKeyedReaderWithFilter(); + + // Act + var result = await Sut.AnyAsync, int>(keyedFilter, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync, int>(keyedFilter, null, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndOptionsAndCancellationToken_GetsResult() + { + // Arrange + var token = new CancellationToken(true); + var options = new CountOptions(); + + SetupKeyedReaderWithFilter(); + + // Act + var result = await Sut.AnyAsync, int>(keyedFilter, options, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync, int>(keyedFilter, options, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndPartitionKey_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + + SetupKeyedReaderWithFilter(); + + // Act + var result = await Sut.AnyAsync, int>(keyedFilter, partitionKey); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync, int>(keyedFilter, null, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndCountOptionsAndPartitionKey_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + var options = new CountOptions(); + + SetupKeyedReaderWithFilter(); + + // Act + var result = await Sut.AnyAsync, int>(keyedFilter, options, partitionKey); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync, int>(keyedFilter, options, partitionKey, CancellationToken.None), + Times.Once); + } + [Fact] + public async Task Keyed_WithFilterAndPartitionKeyAndCancellationToken_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderWithFilter(); + + // Act + var result = await Sut.AnyAsync, int>(keyedFilter, partitionKey, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync, int>(keyedFilter, null, partitionKey, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndCountOptionsAndPartitionKeyAndCancellationToken_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var options = new CountOptions(); + + SetupKeyedReaderWithFilter(); + + // Act + var result = await Sut.AnyAsync, int>(keyedFilter, options, partitionKey, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.AnyAsync, int>(keyedFilter, options, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReaderWithExpression() + { + Reader = new Mock(); + Reader + .Setup( + x => x.AnyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + } + + private void SetupKeyedReaderWithFilter() + { + Reader = new Mock(); + Reader + .Setup( + x => x.AnyAsync, int>( + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + } + + #endregion + + private void SetupReader() + { + Reader = new Mock(); + Reader + .Setup( + x => x.AnyAsync( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + } +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/AnyTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/AnyTests.cs new file mode 100644 index 0000000..420aaae --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/AnyTests.cs @@ -0,0 +1,349 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class AnyTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> expression = document => document.SomeContent == "SomeContent"; + + [Fact] + public void WithExpression_GetsResult() + { + // Arrange + SetupReader(); + + // Act + var result = Sut.Any(expression); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any(expression, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithExpressionAndCancellationToken_GetsResult() + { + // Arrange + var token = new CancellationToken(true); + + SetupReader(); + + // Act + var result = Sut.Any(expression, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any(expression, null, token), + Times.Once); + } + + [Fact] + public void WithExpressionAndPartitionKey_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + + SetupReader(); + + // Act + var result = Sut.Any(expression, partitionKey); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any(expression, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithExpressionAndPartitionKeyAndCancellationToken_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(); + + // Act + var result = Sut.Any(expression, partitionKey, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any(expression, partitionKey, token), + Times.Once); + } + + private void SetupReader() + { + Reader = new Mock(); + Reader + .Setup( + x => x.Any( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + } + + #region Keyed + + private readonly Expression, bool>> keyedExpression = document => document.SomeContent == "SomeContent"; + private readonly FilterDefinition> keyedFilter = Builders>.Filter.Eq(document => document.SomeContent, "SomeContent"); + + [Fact] + public void Keyed_WithExpression_GetsResult() + { + // Arrange + SetupKeyedReaderWithExpression(); + + // Act + var result = Sut.Any, int>(keyedExpression); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any, int>(keyedExpression, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithExpressionAndCancellationToken_GetsResult() + { + // Arrange + var token = new CancellationToken(true); + + SetupKeyedReaderWithExpression(); + + // Act + var result = Sut.Any, int>(keyedExpression, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any, int>(keyedExpression, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithExpressionAndPartitionKey_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + + SetupKeyedReaderWithExpression(); + + // Act + var result = Sut.Any, int>(keyedExpression, partitionKey); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any, int>(keyedExpression, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithExpressionAndPartitionKeyAndCancellationToken_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderWithExpression(); + + // Act + var result = Sut.Any, int>(keyedExpression, partitionKey, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any, int>(keyedExpression, partitionKey, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilter_GetsResult() + { + // Arrange + SetupKeyedReaderWithFilter(); + + // Act + var result = Sut.Any, int>(keyedFilter); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any, int>(keyedFilter, null, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndCancellationToken_GetsResult() + { + // Arrange + var token = new CancellationToken(true); + + SetupKeyedReaderWithFilter(); + + // Act + var result = Sut.Any, int>(keyedFilter, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any, int>(keyedFilter, null, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndOptions_GetsResult() + { + // Arrange + var options = new CountOptions(); + SetupKeyedReaderWithFilter(); + + // Act + var result = Sut.Any, int>(keyedFilter, options); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any, int>(keyedFilter, options, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndOptionsAndCancellationToken_GetsResult() + { + // Arrange + var options = new CountOptions(); + var token = new CancellationToken(true); + SetupKeyedReaderWithFilter(); + + // Act + var result = Sut.Any, int>(keyedFilter, options, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any, int>(keyedFilter, options, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndPartitionKey_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + + SetupKeyedReaderWithFilter(); + + // Act + var result = Sut.Any, int>(keyedFilter, partitionKey); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any, int>(keyedFilter, null, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndOptionsAndPartitionKey_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + var options = new CountOptions(); + + SetupKeyedReaderWithFilter(); + + // Act + var result = Sut.Any, int>(keyedFilter, options, partitionKey); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any, int>(keyedFilter, options, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndPartitionKeyAndCancellationToken_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderWithFilter(); + + // Act + var result = Sut.Any, int>(keyedFilter, partitionKey, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any, int>(keyedFilter, null, partitionKey, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndOptionsAndPartitionKeyAndCancellationToken_GetsResult() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var options = new CountOptions(); + + SetupKeyedReaderWithFilter(); + + // Act + var result = Sut.Any, int>(keyedFilter, options, partitionKey, token); + + // Assert + result.Should().BeTrue(); + Reader.Verify( + x => x.Any, int>(keyedFilter, options, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReaderWithExpression() + { + Reader = new Mock(); + Reader + .Setup( + x => x.Any, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + } + + private void SetupKeyedReaderWithFilter() + { + Reader = new Mock(); + Reader + .Setup( + x => x.Any, int>( + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/CountAsyncTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/CountAsyncTests.cs new file mode 100644 index 0000000..5a89a57 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/CountAsyncTests.cs @@ -0,0 +1,371 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class CountAsyncTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> expression = document => document.SomeContent == "SomeContent"; + + [Fact] + public async Task WithExpression_Counts() + { + // Arrange + var count = Fixture.Create(); + + SetupReader(count); + + // Act + var result = await Sut.CountAsync(expression); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync(expression, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithExpressionAndCancellationToken_Counts() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(count); + + // Act + var result = await Sut.CountAsync(expression, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync(expression, null, token), + Times.Once); + } + + [Fact] + public async Task WithExpressionAndPartitionKey_Counts() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(count); + + // Act + var result = await Sut.CountAsync(expression, partitionKey); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync(expression, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithExpressionAndPartitionKeyAndCancellationToken_Counts() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(count); + + // Act + var result = await Sut.CountAsync(expression, partitionKey, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync(expression, partitionKey, token), + Times.Once); + } + + private void SetupReader(long count) + { + Reader = new Mock(); + Reader + .Setup( + x => x.CountAsync( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + } + + #region Keyed + + private readonly Expression, bool>> keyedExpression = document => document.SomeContent == "SomeContent"; + private readonly FilterDefinition> keyedFilter = Builders>.Filter.Eq(document => document.Id, 1); + + [Fact] + public async Task Keyed_WithExpression_Counts() + { + // Arrange + var count = Fixture.Create(); + + SetupKeyedReaderWithExpression(count); + + // Act + var result = await Sut.CountAsync, int>(keyedExpression); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync, int>(keyedExpression, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithExpressionAndCancellationToken_Counts() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderWithExpression(count); + + // Act + var result = await Sut.CountAsync, int>(keyedExpression, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync, int>(keyedExpression, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithExpressionAndPartitionKey_Counts() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupKeyedReaderWithExpression(count); + + // Act + var result = await Sut.CountAsync, int>(keyedExpression, partitionKey); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync, int>(keyedExpression, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithExpressionAndPartitionKeyAndCancellationToken_Counts() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderWithExpression(count); + + // Act + var result = await Sut.CountAsync, int>(keyedExpression, partitionKey, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync, int>(keyedExpression, partitionKey, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilter_Counts() + { + // Arrange + var count = Fixture.Create(); + + SetupKeyedReaderWithFilter(count); + + // Act + var result = await Sut.CountAsync, int>(keyedFilter); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync, int>(keyedFilter, null, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndCountOptions_Counts() + { + // Arrange + var count = Fixture.Create(); + var countOptions = new CountOptions(); + + SetupKeyedReaderWithFilter(count); + + // Act + var result = await Sut.CountAsync, int>(keyedFilter, countOptions); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync, int>(keyedFilter, countOptions, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndCancellationToken_Counts() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderWithFilter(count); + + // Act + var result = await Sut.CountAsync, int>(keyedFilter, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync, int>(keyedFilter, null, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndCountOptionsAndCancellationToken_Counts() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + var countOptions = new CountOptions(); + + SetupKeyedReaderWithFilter(count); + + // Act + var result = await Sut.CountAsync, int>(keyedFilter, countOptions, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync, int>(keyedFilter, countOptions, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndPartitionKey_Counts() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupKeyedReaderWithFilter(count); + + // Act + var result = await Sut.CountAsync, int>(keyedFilter, partitionKey); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync, int>(keyedFilter, null, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndCountOptionsAndPartitionKey_Counts() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var countOptions = new CountOptions(); + + SetupKeyedReaderWithFilter(count); + + // Act + var result = await Sut.CountAsync, int>(keyedFilter, countOptions, partitionKey); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync, int>(keyedFilter, countOptions, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndPartitionKeyAndCancellationToken_Counts() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderWithFilter(count); + + // Act + var result = await Sut.CountAsync, int>(keyedFilter, partitionKey, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync, int>(keyedFilter, null, partitionKey, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndCountOptionsPartitionKeyAndCancellationToken_Counts() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var countOptions = new CountOptions(); + + SetupKeyedReaderWithFilter(count); + + // Act + var result = await Sut.CountAsync, int>(keyedFilter, countOptions, partitionKey, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.CountAsync, int>(keyedFilter, countOptions, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReaderWithExpression(long count) + { + Reader = new Mock(); + Reader + .Setup( + x => x.CountAsync, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + } + + private void SetupKeyedReaderWithFilter(long count) + { + Reader = new Mock(); + Reader + .Setup( + x => x.CountAsync, int>( + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/CountTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/CountTests.cs new file mode 100644 index 0000000..4e0044d --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/CountTests.cs @@ -0,0 +1,373 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class CountTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> expression = document => document.SomeContent == "SomeContent"; + + [Fact] + public void WithExpression_Counts() + { + // Arrange + var count = Fixture.Create(); + + SetupReader(count); + + // Act + var result = Sut.Count(expression); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count(expression, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithExpressionAndCancellationToken_Counts() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(count); + + // Act + var result = Sut.Count(expression, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count(expression, null, token), + Times.Once); + } + + [Fact] + public void WithExpressionAndPartitionKey_Counts() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(count); + + // Act + var result = Sut.Count(expression, partitionKey); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count(expression, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithExpressionAndPartitionKeyAndCancellationToken_Counts() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(count); + + // Act + var result = Sut.Count(expression, partitionKey, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count(expression, partitionKey, token), + Times.Once); + } + + private void SetupReader(long count) + { + Reader = new Mock(); + Reader + .Setup( + x => x.Count( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + } + + #region Keyed + + private readonly Expression, bool>> keyedExpression = document => document.SomeContent == "SomeContent"; + + private readonly FilterDefinition> keyedFilter = Builders>.Filter.Eq( + document => document.SomeContent, + "SomeContent"); + + [Fact] + public void Keyed_WithExpression_Counts() + { + // Arrange + var count = Fixture.Create(); + + SetupKeyedReader(count); + + // Act + var result = Sut.Count, int>(keyedExpression); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count, int>(keyedExpression, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithExpressionAndCancellationToken_Counts() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(count); + + // Act + var result = Sut.Count, int>(keyedExpression, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count, int>(keyedExpression, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithExpressionAndPartitionKey_Counts() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupKeyedReader(count); + + // Act + var result = Sut.Count, int>(keyedExpression, partitionKey); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count, int>(keyedExpression, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithExpressionAndPartitionKeyAndCancellationToken_Counts() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(count); + + // Act + var result = Sut.Count, int>(keyedExpression, partitionKey, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count, int>(keyedExpression, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReader(long count) + { + Reader = new Mock(); + Reader + .Setup( + x => x.Count, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + } + + [Fact] + public void Keyed_WithFilter_Counts() + { + // Arrange + var count = Fixture.Create(); + + SetupKeyedReaderWithFilter(count); + + // Act + var result = Sut.Count, int>(keyedFilter); + + // Assert + Reader.Verify( + x => x.Count, int>(keyedFilter, null, null, CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterAndCountOptions_Counts() + { + // Arrange + var count = Fixture.Create(); + var countOptions = new CountOptions(); + + SetupKeyedReaderWithFilter(count); + + // Act + var result = Sut.Count, int>(keyedFilter, countOptions); + + // Assert + Reader.Verify( + x => x.Count, int>(keyedFilter, countOptions, null, CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterAndCancellationToken_Counts() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderWithFilter(count); + + // Act + var result = Sut.Count, int>(keyedFilter, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count, int>(keyedFilter, null, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndCountOptionsAndCancellationToken_Counts() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + var countOptions = new CountOptions(); + + SetupKeyedReaderWithFilter(count); + + // Act + var result = Sut.Count, int>(keyedFilter, countOptions, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count, int>(keyedFilter, countOptions, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndPartitionKey_Counts() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupKeyedReaderWithFilter(count); + + // Act + var result = Sut.Count, int>(keyedFilter, partitionKey); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count, int>(keyedFilter, null, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndCountOptionsAndPartitionKey_Counts() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var countOptions = new CountOptions(); + + SetupKeyedReaderWithFilter(count); + + // Act + var result = Sut.Count, int>(keyedFilter, countOptions, partitionKey); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count, int>(keyedFilter, countOptions, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndPartitionKeyAndCancellationToken_Counts() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderWithFilter(count); + + // Act + var result = Sut.Count, int>(keyedFilter, partitionKey, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count, int>(keyedFilter, null, partitionKey, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndCountOptionsAndPartitionKeyAndCancellationToken_Counts() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var countOptions = new CountOptions(); + + SetupKeyedReaderWithFilter(count); + + // Act + var result = Sut.Count, int>(keyedFilter, countOptions, partitionKey, token); + + // Assert + result.Should().Be(count); + Reader.Verify( + x => x.Count, int>(keyedFilter, countOptions, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReaderWithFilter(long count) + { + Reader = new Mock(); + Reader + .Setup( + x => x.Count, int>( + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetAllAsyncTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetAllAsyncTests.cs new file mode 100644 index 0000000..38c8af7 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetAllAsyncTests.cs @@ -0,0 +1,389 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class GetAllAsyncTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> expression = document => document.SomeContent == "SomeContent"; + + [Fact] + public async Task WithExpression_GetsAll() + { + // Arrange + var document = Fixture.CreateMany().ToList(); + + SetupReader(document); + + // Act + var result = await Sut.GetAllAsync(expression); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync(expression, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithExpressionAndCancellationToken_GetsAll() + { + // Arrange + var document = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetAllAsync(expression, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync(expression, null, token), + Times.Once); + } + + [Fact] + public async Task WithExpressionAndPartitionKey_GetsAll() + { + // Arrange + var document = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetAllAsync(expression, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync(expression, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithExpressionAndPartitionKeyAndCancellationToken_GetsAll() + { + // Arrange + var document = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetAllAsync(expression, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync(expression, partitionKey, token), + Times.Once); + } + + private void SetupReader(List documents) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetAllAsync( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents); + } + + #region Keyed + + private readonly Expression, bool>> keyedExpression = document => document.SomeContent == "SomeContent"; + private readonly FilterDefinition> keyedFilter = Builders>.Filter.Eq(document => document.SomeContent, "SomeContent"); + + [Fact] + public async Task Keyed_WithExpression_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + + SetupReader(document); + + // Act + var result = await Sut.GetAllAsync, int>(keyedExpression); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync, int>(keyedExpression, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithExpressionAndCancellationToken_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetAllAsync, int>(keyedExpression, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync, int>(keyedExpression, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithExpressionAndPartitionKey_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetAllAsync, int>(keyedExpression, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync, int>(keyedExpression, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithExpressionAndPartitionKeyAndCancellationToken_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetAllAsync, int>(keyedExpression, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync, int>(keyedExpression, partitionKey, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilter_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + + SetupReaderWithFilter(document); + + // Act + var result = await Sut.GetAllAsync, int>(keyedFilter); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync, int>(keyedFilter, null, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndOptions_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var options = new FindOptions(); + + SetupReaderWithFilter(document); + + // Act + var result = await Sut.GetAllAsync, int>(keyedFilter, options); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync, int>(keyedFilter, options, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndCancellationToken_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var token = new CancellationToken(true); + + SetupReaderWithFilter(document); + + // Act + var result = await Sut.GetAllAsync, int>(keyedFilter, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync, int>(keyedFilter, null, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndFindOptionsAndCancellationToken_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var token = new CancellationToken(true); + var options = new FindOptions(); + + SetupReaderWithFilter(document); + + // Act + var result = await Sut.GetAllAsync, int>(keyedFilter, options, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync, int>(keyedFilter, options, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndPartitionKey_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var partitionKey = Fixture.Create(); + + SetupReaderWithFilter(document); + + // Act + var result = await Sut.GetAllAsync, int>(keyedFilter, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync, int>(keyedFilter, null, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndFindOptionsAndPartitionKey_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var partitionKey = Fixture.Create(); + var options = new FindOptions(); + + SetupReaderWithFilter(document); + + // Act + var result = await Sut.GetAllAsync, int>(keyedFilter, options, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync, int>(keyedFilter, options, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndPartitionKeyAndCancellationToken_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReaderWithFilter(document); + + // Act + var result = await Sut.GetAllAsync, int>(keyedFilter, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync, int>(keyedFilter, null, partitionKey, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndFindOptionsAndPartitionKeyAndCancellationToken_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var options = new FindOptions(); + + SetupReaderWithFilter(document); + + // Act + var result = await Sut.GetAllAsync, int>(keyedFilter, options, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAllAsync, int>(keyedFilter, options, partitionKey, token), + Times.Once); + } + + private void SetupReaderWithFilter(List> documents) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetAllAsync, int>( + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents); + } + + private void SetupReader(List> documents) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetAllAsync, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetAllTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetAllTests.cs new file mode 100644 index 0000000..a25f5f8 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetAllTests.cs @@ -0,0 +1,388 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class GetAllTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> expression = document => document.SomeContent == "SomeContent"; + + [Fact] + public void WithExpression_GetsAll() + { + // Arrange + var document = Fixture.CreateMany().ToList(); + + SetupReader(document); + + // Act + var result = Sut.GetAll(expression); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll(expression, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithExpressionAndCancellationToken_GetsAll() + { + // Arrange + var document = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetAll(expression, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll(expression, null, token), + Times.Once); + } + + [Fact] + public void WithExpressionAndPartitionKey_GetsAll() + { + // Arrange + var document = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = Sut.GetAll(expression, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll(expression, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithExpressionAndPartitionKeyAndCancellationToken_GetsAll() + { + // Arrange + var document = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetAll(expression, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll(expression, partitionKey, token), + Times.Once); + } + + private void SetupReader(List documents) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetAll( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(documents); + } + + #region Keyed + + private readonly Expression, bool>> keyedExpression = document => document.SomeContent == "SomeContent"; + private readonly FilterDefinition> keyedFilter = Builders>.Filter.Eq(document => document.SomeContent, "SomeContent"); + + [Fact] + public void Keyed_WithExpression_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + + SetupKeyedReader(document); + + // Act + var result = Sut.GetAll, int>(keyedExpression); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll, int>(keyedExpression, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithExpressionAndCancellationToken_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var token = new CancellationToken(true); + + SetupKeyedReader(document); + + // Act + var result = Sut.GetAll, int>(keyedExpression, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll, int>(keyedExpression, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithExpressionAndPartitionKey_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var partitionKey = Fixture.Create(); + + SetupKeyedReader(document); + + // Act + var result = Sut.GetAll, int>(keyedExpression, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll, int>(keyedExpression, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithExpressionAndPartitionKeyAndCancellationToken_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(document); + + // Act + var result = Sut.GetAll, int>(keyedExpression, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll, int>(keyedExpression, partitionKey, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilter_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = Sut.GetAll, int>(keyedFilter); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll, int>(keyedFilter, null, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndFindOptions_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var options = new FindOptions(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = Sut.GetAll, int>(keyedFilter, options); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll, int>(keyedFilter, options, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndCancellationToken_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var token = new CancellationToken(true); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = Sut.GetAll, int>(keyedFilter, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll, int>(keyedFilter, null, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndFindOptionsAndCancellationToken_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var token = new CancellationToken(true); + var options = new FindOptions(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = Sut.GetAll, int>(keyedFilter, options, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll, int>(keyedFilter, options, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndPartitionKey_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var partitionKey = Fixture.Create(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = Sut.GetAll, int>(keyedFilter, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll, int>(keyedFilter, null, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndFindOptionsAndPartitionKey_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var partitionKey = Fixture.Create(); + var options = new FindOptions(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = Sut.GetAll, int>(keyedFilter, options, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll, int>(keyedFilter, options, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndPartitionKeyAndCancellationToken_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = Sut.GetAll, int>(keyedFilter, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll, int>(keyedFilter, null, partitionKey, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndFindOptionsAndPartitionKeyAndCancellationToken_GetsAll() + { + // Arrange + var document = Fixture.CreateMany>().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var options = new FindOptions(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = Sut.GetAll, int>(keyedFilter, options, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetAll, int>(keyedFilter, options, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReaderWithFilter(List> documents) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetAll, int>( + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(documents); + } + + private void SetupKeyedReader(List> documents) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetAll, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(documents); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByIdAsyncTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByIdAsyncTests.cs new file mode 100644 index 0000000..21487fd --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByIdAsyncTests.cs @@ -0,0 +1,203 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class GetByIdAsyncTests : TestReadOnlyMongoRepositoryContext +{ + [Fact] + public async Task WithId_Gets() + { + // Arrange + var document = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetByIdAsync(document.Id); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByIdAsync(document.Id, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithIdAndCancellationToken_Gets() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetByIdAsync(document.Id, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByIdAsync(document.Id, null, token), + Times.Once); + } + + [Fact] + public async Task WithIdAndPartitionKey_Gets() + { + // Arrange + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetByIdAsync(document.Id, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByIdAsync(document.Id, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithIdAndPartitionKeyAndCancellationToken_Gets() + { + // Arrange + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetByIdAsync(document.Id, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByIdAsync(document.Id, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocument document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetByIdAsync( + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(document); + } + + #region Keyed + + [Fact] + public async Task Keyed_WithId_Gets() + { + // Arrange + var document = Fixture.Create>(); + + SetupKeyedReader(document); + + // Act + var result = await Sut.GetByIdAsync, int>(document.Id); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByIdAsync, int>(document.Id, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithIdAndCancellationToken_Gets() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupKeyedReader(document); + + // Act + var result = await Sut.GetByIdAsync, int>(document.Id, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByIdAsync, int>(document.Id, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithIdAndPartitionKey_Gets() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupKeyedReader(document); + + // Act + var result = await Sut.GetByIdAsync, int>(document.Id, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByIdAsync, int>(document.Id, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithIdAndPartitionKeyAndCancellationToken_Gets() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(document); + + // Act + var result = await Sut.GetByIdAsync, int>(document.Id, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByIdAsync, int>(document.Id, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReader(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetByIdAsync, int>( + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(document); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByIdTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByIdTests.cs new file mode 100644 index 0000000..9e3a9b3 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByIdTests.cs @@ -0,0 +1,202 @@ +using System; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class GetByIdTests : TestReadOnlyMongoRepositoryContext +{ + [Fact] + public void WithId_Gets() + { + // Arrange + var document = Fixture.Create(); + + SetupReader(document); + + // Act + var result = Sut.GetById(document.Id); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetById(document.Id, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithIdAndCancellationToken_Gets() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetById(document.Id, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetById(document.Id, null, token), + Times.Once); + } + + [Fact] + public void WithIdAndPartitionKey_Gets() + { + // Arrange + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = Sut.GetById(document.Id, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetById(document.Id, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithIdAndPartitionKeyAndCancellationToken_Gets() + { + // Arrange + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetById(document.Id, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetById(document.Id, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocument document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetById( + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(document); + } + + #region Keyed + + [Fact] + public void Keyed_WithId_Gets() + { + // Arrange + var document = Fixture.Create>(); + + SetupKeyedReader(document); + + // Act + var result = Sut.GetById, int>(document.Id); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetById, int>(document.Id, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithIdAndCancellationToken_Gets() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupKeyedReader(document); + + // Act + var result = Sut.GetById, int>(document.Id, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetById, int>(document.Id, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithIdAndPartitionKey_Gets() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupKeyedReader(document); + + // Act + var result = Sut.GetById, int>(document.Id, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetById, int>(document.Id, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithIdAndPartitionKeyAndCancellationToken_Gets() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(document); + + // Act + var result = Sut.GetById, int>(document.Id, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetById, int>(document.Id, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReader(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetById, int>( + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(document); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByMaxAsyncTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByMaxAsyncTests.cs new file mode 100644 index 0000000..8c307ed --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByMaxAsyncTests.cs @@ -0,0 +1,212 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class GetByMaxAsyncTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> selector = document => document.SomeValue; + + [Fact] + public async Task WithFilterAndSelector_GetsOne() + { + // Arrange + var document = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetByMaxAsync(filter, selector); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMaxAsync(filter, selector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetByMaxAsync(filter, selector, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMaxAsync(filter, selector, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetByMaxAsync(filter, selector, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMaxAsync(filter, selector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetByMaxAsync(filter, selector, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMaxAsync(filter, selector, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocument document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetByMaxAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(document); + } + + #region Keyed + + private readonly Expression, bool>> keyedFilter = document => document.SomeContent == "SomeContent"; + private readonly Expression, object>> keyedSelector = document => document.SomeValue; + + [Fact] + public async Task Keyed_WithFilterAndSelector_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + + SetupReader(document); + + // Act + var result = await Sut.GetByMaxAsync, int>(keyedFilter, keyedSelector); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMaxAsync, int>(keyedFilter, keyedSelector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndSelectorAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetByMaxAsync, int>(keyedFilter, keyedSelector, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMaxAsync, int>(keyedFilter, keyedSelector, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndSelectorAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetByMaxAsync, int>(keyedFilter, keyedSelector, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMaxAsync, int>(keyedFilter, keyedSelector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetByMaxAsync, int>(keyedFilter, keyedSelector, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMaxAsync, int>(keyedFilter, keyedSelector, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetByMaxAsync, int>( + It.IsAny, bool>>>(), + It.IsAny, object>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(document); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByMaxTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByMaxTests.cs new file mode 100644 index 0000000..19f755e --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByMaxTests.cs @@ -0,0 +1,211 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class GetByMaxTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> selector = document => document.SomeValue; + + [Fact] + public void WithFilterAndSelector_GetsOne() + { + // Arrange + var document = Fixture.Create(); + + SetupReader(document); + + // Act + var result = Sut.GetByMax(filter, selector); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMax(filter, selector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetByMax(filter, selector, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMax(filter, selector, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = Sut.GetByMax(filter, selector, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMax(filter, selector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetByMax(filter, selector, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMax(filter, selector, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocument document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetByMax( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(document); + } + + #region Keyed + + private readonly Expression, bool>> keyedFilter = document => document.SomeContent == "SomeContent"; + private readonly Expression, object>> keyedSelector = document => document.SomeValue; + + [Fact] + public void Keyed_WithFilterAndSelector_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + + SetupReader(document); + + // Act + var result = Sut.GetByMax, int>(keyedFilter, keyedSelector); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMax, int>(keyedFilter, keyedSelector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndSelectorAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetByMax, int>(keyedFilter, keyedSelector, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMax, int>(keyedFilter, keyedSelector, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndSelectorAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = Sut.GetByMax, int>(keyedFilter, keyedSelector, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMax, int>(keyedFilter, keyedSelector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetByMax, int>(keyedFilter, keyedSelector, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMax, int>(keyedFilter, keyedSelector, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetByMax, int>( + It.IsAny, bool>>>(), + It.IsAny, object>>>(), + It.IsAny(), + It.IsAny())) + .Returns(document); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByMinAsyncTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByMinAsyncTests.cs new file mode 100644 index 0000000..0f897ad --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByMinAsyncTests.cs @@ -0,0 +1,212 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class GetByMinAsyncTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> selector = document => document.SomeValue; + + [Fact] + public async Task WithFilterAndSelector_GetsOne() + { + // Arrange + var document = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetByMinAsync(filter, selector); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMinAsync(filter, selector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetByMinAsync(filter, selector, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMinAsync(filter, selector, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetByMinAsync(filter, selector, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMinAsync(filter, selector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetByMinAsync(filter, selector, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMinAsync(filter, selector, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocument document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetByMinAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(document); + } + + #region Keyed + + private readonly Expression, bool>> keyedFilter = document => document.SomeContent == "SomeContent"; + private readonly Expression, object>> keyedSelector = document => document.SomeValue; + + [Fact] + public async Task Keyed_WithFilterAndSelector_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + + SetupKeyedReader(document); + + // Act + var result = await Sut.GetByMinAsync, int>(keyedFilter, keyedSelector); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMinAsync, int>(keyedFilter, keyedSelector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndSelectorAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupKeyedReader(document); + + // Act + var result = await Sut.GetByMinAsync, int>(keyedFilter, keyedSelector, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMinAsync, int>(keyedFilter, keyedSelector, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndSelectorAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupKeyedReader(document); + + // Act + var result = await Sut.GetByMinAsync, int>(keyedFilter, keyedSelector, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMinAsync, int>(keyedFilter, keyedSelector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(document); + + // Act + var result = await Sut.GetByMinAsync, int>(keyedFilter, keyedSelector, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMinAsync, int>(keyedFilter, keyedSelector, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReader(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetByMinAsync, int>( + It.IsAny, bool>>>(), + It.IsAny, object>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(document); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByMinTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByMinTests.cs new file mode 100644 index 0000000..29b9f34 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByMinTests.cs @@ -0,0 +1,211 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class GetByMinTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> selector = document => document.SomeValue; + + [Fact] + public void WithFilterAndSelector_GetsOne() + { + // Arrange + var document = Fixture.Create(); + + SetupReader(document); + + // Act + var result = Sut.GetByMin(filter, selector); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMin(filter, selector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetByMin(filter, selector, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMin(filter, selector, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = Sut.GetByMin(filter, selector, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMin(filter, selector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetByMin(filter, selector, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMin(filter, selector, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocument document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetByMin( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(document); + } + + #region Keyed + + private readonly Expression, bool>> keyedFilter = document => document.SomeContent == "SomeContent"; + private readonly Expression, object>> keyedSelector = document => document.SomeValue; + + [Fact] + public void Keyed_WithFilterAndSelector_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + + SetupReader(document); + + // Act + var result = Sut.GetByMin, int>(keyedFilter, keyedSelector); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMin, int>(keyedFilter, keyedSelector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndSelectorAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetByMin, int>(keyedFilter, keyedSelector, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMin, int>(keyedFilter, keyedSelector, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndSelectorAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = Sut.GetByMin, int>(keyedFilter, keyedSelector, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMin, int>(keyedFilter, keyedSelector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetByMin, int>(keyedFilter, keyedSelector, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetByMin, int>(keyedFilter, keyedSelector, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetByMin, int>( + It.IsAny, bool>>>(), + It.IsAny, object>>>(), + It.IsAny(), + It.IsAny())) + .Returns(document); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetMaxValueAsyncTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetMaxValueAsyncTests.cs new file mode 100644 index 0000000..d24d118 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetMaxValueAsyncTests.cs @@ -0,0 +1,204 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class GetMaxValueAsyncTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> selector = document => document.SomeValue; + + [Fact] + public async Task WithFilterAndSelector_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + + SetupReader(value); + + // Act + var result = await Sut.GetMaxValueAsync(filter, selector); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValueAsync(filter, selector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(value); + + // Act + var result = await Sut.GetMaxValueAsync(filter, selector, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValueAsync(filter, selector, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKey_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(value); + + // Act + var result = await Sut.GetMaxValueAsync(filter, selector, partitionKey); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValueAsync(filter, selector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(value); + + // Act + var result = await Sut.GetMaxValueAsync(filter, selector, partitionKey, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValueAsync(filter, selector, partitionKey, token), + Times.Once); + } + + private void SetupReader(int value) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetMaxValueAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(value); + } + + #region Keyed + + private readonly Expression, bool>> keyedFilter = document => document.SomeContent == "SomeContent"; + private readonly Expression, int>> keyedSelector = document => document.SomeValue; + + [Fact] + public async Task Keyed_WithFilterAndSelector_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + + SetupKeyedReader(value); + + // Act + var result = await Sut.GetMaxValueAsync, int, int>(keyedFilter, keyedSelector); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValueAsync, int, int>(keyedFilter, keyedSelector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndSelectorAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(value); + + // Act + var result = await Sut.GetMaxValueAsync, int, int>(keyedFilter, keyedSelector, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValueAsync, int, int>(keyedFilter, keyedSelector, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndSelectorAndPartitionKey_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupKeyedReader(value); + + // Act + var result = await Sut.GetMaxValueAsync, int, int>(keyedFilter, keyedSelector, partitionKey); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValueAsync, int, int>(keyedFilter, keyedSelector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(value); + + // Act + var result = await Sut.GetMaxValueAsync, int, int>(keyedFilter, keyedSelector, partitionKey, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValueAsync, int, int>(keyedFilter, keyedSelector, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReader(int value) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetMaxValueAsync, int, int>( + It.IsAny, bool>>>(), + It.IsAny, int>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(value); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetMaxValueTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetMaxValueTests.cs new file mode 100644 index 0000000..34f65c2 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetMaxValueTests.cs @@ -0,0 +1,203 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class GetMaxValueTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> selector = document => document.SomeValue; + + [Fact] + public void WithFilterAndSelector_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + + SetupReader(value); + + // Act + var result = Sut.GetMaxValue(filter, selector); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValue(filter, selector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(value); + + // Act + var result = Sut.GetMaxValue(filter, selector, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValue(filter, selector, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKey_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(value); + + // Act + var result = Sut.GetMaxValue(filter, selector, partitionKey); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValue(filter, selector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(value); + + // Act + var result = Sut.GetMaxValue(filter, selector, partitionKey, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValue(filter, selector, partitionKey, token), + Times.Once); + } + + private void SetupReader(int value) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetMaxValue( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(value); + } + + #region Keyed + + private readonly Expression, bool>> keyedFilter = document => document.SomeContent == "SomeContent"; + private readonly Expression, int>> keyedSelector = document => document.SomeValue; + + [Fact] + public void Keyed_WithFilterAndSelector_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + + SetupKeyedReader(value); + + // Act + var result = Sut.GetMaxValue, int, int>(keyedFilter, keyedSelector); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValue, int, int>(keyedFilter, keyedSelector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndSelectorAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(value); + + // Act + var result = Sut.GetMaxValue, int, int>(keyedFilter, keyedSelector, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValue, int, int>(keyedFilter, keyedSelector, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndSelectorAndPartitionKey_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupKeyedReader(value); + + // Act + var result = Sut.GetMaxValue, int, int>(keyedFilter, keyedSelector, partitionKey); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValue, int, int>(keyedFilter, keyedSelector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(value); + + // Act + var result = Sut.GetMaxValue, int, int>(keyedFilter, keyedSelector, partitionKey, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMaxValue, int, int>(keyedFilter, keyedSelector, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReader(int value) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetMaxValue, int, int>( + It.IsAny, bool>>>(), + It.IsAny, int>>>(), + It.IsAny(), + It.IsAny())) + .Returns(value); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetMinValueAsyncTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetMinValueAsyncTests.cs new file mode 100644 index 0000000..8743b90 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetMinValueAsyncTests.cs @@ -0,0 +1,204 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class GetMinValueAsyncTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> selector = document => document.SomeValue; + + [Fact] + public async Task WithFilterAndSelector_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + + SetupReader(value); + + // Act + var result = await Sut.GetMinValueAsync(filter, selector); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValueAsync(filter, selector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(value); + + // Act + var result = await Sut.GetMinValueAsync(filter, selector, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValueAsync(filter, selector, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKey_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(value); + + // Act + var result = await Sut.GetMinValueAsync(filter, selector, partitionKey); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValueAsync(filter, selector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(value); + + // Act + var result = await Sut.GetMinValueAsync(filter, selector, partitionKey, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValueAsync(filter, selector, partitionKey, token), + Times.Once); + } + + private void SetupReader(int value) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetMinValueAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(value); + } + + #region Keyed + + private readonly Expression, bool>> keyedFilter = document => document.SomeContent == "SomeContent"; + private readonly Expression, int>> keyedSelector = document => document.SomeValue; + + [Fact] + public async Task Keyed_WithFilterAndSelector_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + + SetupKeyedReader(value); + + // Act + var result = await Sut.GetMinValueAsync, int, int>(keyedFilter, keyedSelector); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValueAsync, int, int>(keyedFilter, keyedSelector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndSelectorAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(value); + + // Act + var result = await Sut.GetMinValueAsync, int, int>(keyedFilter, keyedSelector, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValueAsync, int, int>(keyedFilter, keyedSelector, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndSelectorAndPartitionKey_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupKeyedReader(value); + + // Act + var result = await Sut.GetMinValueAsync, int, int>(keyedFilter, keyedSelector, partitionKey); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValueAsync, int, int>(keyedFilter, keyedSelector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMaxValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(value); + + // Act + var result = await Sut.GetMinValueAsync, int, int>(keyedFilter, keyedSelector, partitionKey, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValueAsync, int, int>(keyedFilter, keyedSelector, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReader(int value) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetMinValueAsync, int, int>( + It.IsAny, bool>>>(), + It.IsAny, int>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(value); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetMinValueTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetMinValueTests.cs new file mode 100644 index 0000000..34f9c58 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetMinValueTests.cs @@ -0,0 +1,203 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class GetMinValueTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> selector = document => document.SomeValue; + + [Fact] + public void WithFilterAndSelector_GetsMinValue() + { + // Arrange + var value = Fixture.Create(); + + SetupReader(value); + + // Act + var result = Sut.GetMinValue(filter, selector); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValue(filter, selector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndCancellationToken_GetsMinValue() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(value); + + // Act + var result = Sut.GetMinValue(filter, selector, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValue(filter, selector, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKey_GetsMinValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(value); + + // Act + var result = Sut.GetMinValue(filter, selector, partitionKey); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValue(filter, selector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMinValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(value); + + // Act + var result = Sut.GetMinValue(filter, selector, partitionKey, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValue(filter, selector, partitionKey, token), + Times.Once); + } + + private void SetupReader(int value) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetMinValue( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(value); + } + + #region Keyed + + private readonly Expression, bool>> keyedFilter = document => document.SomeContent == "SomeContent"; + private readonly Expression, int>> keyedSelector = document => document.SomeValue; + + [Fact] + public void Keyed_WithFilterAndSelector_GetsMinValue() + { + // Arrange + var value = Fixture.Create(); + + SetupKeyedReader(value); + + // Act + var result = Sut.GetMinValue, int, int>(keyedFilter, keyedSelector); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValue, int, int>(keyedFilter, keyedSelector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndSelectorAndCancellationToken_GetsMinValue() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(value); + + // Act + var result = Sut.GetMinValue, int, int>(keyedFilter, keyedSelector, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValue, int, int>(keyedFilter, keyedSelector, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndSelectorAndPartitionKey_GetsMinValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupKeyedReader(value); + + // Act + var result = Sut.GetMinValue, int, int>(keyedFilter, keyedSelector, partitionKey); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValue, int, int>(keyedFilter, keyedSelector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMinValue() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(value); + + // Act + var result = Sut.GetMinValue, int, int>(keyedFilter, keyedSelector, partitionKey, token); + + // Assert + result.Should().Be(value); + Reader.Verify( + x => x.GetMinValue, int, int>(keyedFilter, keyedSelector, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReader(int value) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetMinValue, int, int>( + It.IsAny, bool>>>(), + It.IsAny, int>>>(), + It.IsAny(), + It.IsAny())) + .Returns(value); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs new file mode 100644 index 0000000..561cd2f --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs @@ -0,0 +1,387 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class GetOneAsyncTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> expression = document => document.SomeContent == "SomeContent"; + + [Fact] + public async Task WithExpression_GetsOne() + { + // Arrange + var document = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetOneAsync(expression); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync(expression, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithExpressionAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetOneAsync(expression, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync(expression, null, token), + Times.Once); + } + + [Fact] + public async Task WithExpressionAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetOneAsync(expression, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync(expression, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithExpressionAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetOneAsync(expression, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync(expression, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocument document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetOneAsync( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(document); + } + + #region Keyed + + private readonly Expression, bool>> keyedExpression = document => document.SomeContent == "SomeContent"; + private readonly FilterDefinition> keyedFilter = Builders>.Filter.Eq(document => document.SomeContent, "SomeContent"); + + [Fact] + public async Task Keyed_WithExpression_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + + SetupReader(document); + + // Act + var result = await Sut.GetOneAsync, int>(keyedExpression); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync, int>(keyedExpression, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithExpressionAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetOneAsync, int>(keyedExpression, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync, int>(keyedExpression, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithExpressionAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = await Sut.GetOneAsync, int>(keyedExpression, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync, int>(keyedExpression, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithExpressionAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = await Sut.GetOneAsync, int>(keyedExpression, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync, int>(keyedExpression, partitionKey, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilter_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = await Sut.GetOneAsync, int>(keyedFilter); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync, int>(keyedFilter, null, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndFindOptions_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var options = new FindOptions(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = await Sut.GetOneAsync, int>(keyedFilter, options); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync, int>(keyedFilter, options, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = await Sut.GetOneAsync, int>(keyedFilter, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync, int>(keyedFilter, null, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndFindOptionsAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + var options = new FindOptions(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = await Sut.GetOneAsync, int>(keyedFilter, options, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync, int>(keyedFilter, options, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = await Sut.GetOneAsync, int>(keyedFilter, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync, int>(keyedFilter,null, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndFindOptionsAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var options = new FindOptions(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = await Sut.GetOneAsync, int>(keyedFilter, options, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync, int>(keyedFilter,options, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = await Sut.GetOneAsync, int>(keyedFilter, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync, int>(keyedFilter,null, partitionKey, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndFindOptionsAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var options = new FindOptions(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = await Sut.GetOneAsync, int>(keyedFilter, options, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOneAsync, int>(keyedFilter,options, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReaderWithFilter(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetOneAsync, int>( + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(document); + } + + private void SetupReader(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetOneAsync, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(document); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetOneTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetOneTests.cs new file mode 100644 index 0000000..7a4f0d6 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetOneTests.cs @@ -0,0 +1,386 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class GetOneTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> expression = document => document.SomeContent == "SomeContent"; + + [Fact] + public void WithExpression_GetsOne() + { + // Arrange + var document = Fixture.Create(); + + SetupReader(document); + + // Act + var result = Sut.GetOne(expression); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne(expression, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithExpressionAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetOne(expression, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne(expression, null, token), + Times.Once); + } + + [Fact] + public void WithExpressionAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(document); + + // Act + var result = Sut.GetOne(expression, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne(expression, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithExpressionAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(document); + + // Act + var result = Sut.GetOne(expression, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne(expression, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestDocument document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetOne( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(document); + } + + #region Keyed + + private readonly Expression, bool>> keyedExpression = document => document.SomeContent == "SomeContent"; + private readonly FilterDefinition> keyedFilter = Builders>.Filter.Eq(document => document.SomeContent, "SomeContent"); + + [Fact] + public void Keyed_WithExpression_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + + SetupKeyedReader(document); + + // Act + var result = Sut.GetOne, int>(keyedExpression); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne, int>(keyedExpression, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithExpressionAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupKeyedReader(document); + + // Act + var result = Sut.GetOne, int>(keyedExpression, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne, int>(keyedExpression, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithExpressionAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupKeyedReader(document); + + // Act + var result = Sut.GetOne, int>(keyedExpression, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne, int>(keyedExpression, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithExpressionAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(document); + + // Act + var result = Sut.GetOne, int>(keyedExpression, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne, int>(keyedExpression, partitionKey, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilter_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = Sut.GetOne, int>(keyedFilter); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne, int>(keyedFilter, null, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndFindOptions_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var options = new FindOptions(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = Sut.GetOne, int>(keyedFilter, options); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne, int>(keyedFilter, options, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = Sut.GetOne, int>(keyedFilter, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne, int>(keyedFilter, null, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndFindOptionsAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + var options = new FindOptions(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = Sut.GetOne, int>(keyedFilter, options, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne, int>(keyedFilter, options, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = Sut.GetOne, int>(keyedFilter, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne, int>(keyedFilter, null, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndFindOptionsAndPartitionKey_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var options = new FindOptions(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = Sut.GetOne, int>(keyedFilter, options, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne, int>(keyedFilter, options, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = Sut.GetOne, int>(keyedFilter, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne, int>(keyedFilter, null, partitionKey, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndFindOptionsAndPartitionKeyAndCancellationToken_GetsOne() + { + // Arrange + var document = Fixture.Create>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var options = new FindOptions(); + + SetupKeyedReaderWithFilter(document); + + // Act + var result = Sut.GetOne, int>(keyedFilter, options, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(document); + Reader.Verify( + x => x.GetOne, int>(keyedFilter, options, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReader(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetOne, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) + .Returns(document); + } + + private void SetupKeyedReaderWithFilter(TestDocumentWithKey document) + { + Reader = new Mock(); + Reader + .Setup( + x => x.GetOne, int>( + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(document); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs new file mode 100644 index 0000000..a525bd2 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs @@ -0,0 +1,246 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class GetSortedPaginatedAsyncTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.GroupingKey == 1; + private readonly Expression> selector = document => document.GroupingKey; + private readonly SortDefinition sortDefinition = Builders.Sort.Ascending(document => document.GroupingKey); + + private const bool DefaultAscending = true; + private const int DefaultSkipNumber = 0; + private const int DefaultTakeNumber = 50; + + [Fact] + public async Task WithFilterAndSortSelector_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortSelector(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, selector); + + // Assert + VerifySelector(result, documents, DefaultAscending, DefaultSkipNumber, DefaultTakeNumber, null, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortSelectorAndAscending_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortSelector(); + var ascending = Fixture.Create(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, selector, ascending); + + // Assert + VerifySelector(result, documents, ascending, DefaultSkipNumber, DefaultTakeNumber, null, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortSelectorAndSkipNumber_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortSelector(); + var skipNumber = Fixture.Create(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, selector, skipNumber: skipNumber); + + // Assert + VerifySelector(result, documents, DefaultAscending, skipNumber, DefaultTakeNumber, null, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortSelectorAndTakeNumber_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortSelector(); + var ascending = Fixture.Create(); + var takeNumber = Fixture.Create(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, selector, ascending, takeNumber: takeNumber); + + // Assert + VerifySelector(result, documents, ascending, DefaultSkipNumber, takeNumber, null, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortSelectorAndPartitionKey_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortSelector(); + var partitionKey = Fixture.Create(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, selector, partitionKey: partitionKey); + + // Assert + VerifySelector(result, documents, DefaultAscending, DefaultSkipNumber, DefaultTakeNumber, partitionKey, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortSelectorAndCancellationToken_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortSelector(); + var cancellationToken = new CancellationToken(true); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, selector, cancellationToken: cancellationToken); + + // Assert + VerifySelector(result, documents, DefaultAscending, DefaultSkipNumber, DefaultTakeNumber, null, cancellationToken); + } + + [Fact] + public async Task WithFilterAndSortDefinition_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortDefinition(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, sortDefinition); + + // Assert + VerifyDefinition(result, documents, DefaultSkipNumber, DefaultTakeNumber, null, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortDefinitionAndSkipNumber_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortDefinition(); + var skipNumber = Fixture.Create(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, sortDefinition, skipNumber); + + // Assert + VerifyDefinition(result, documents, skipNumber, DefaultTakeNumber, null, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortDefinitionAndTakeNumber_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortDefinition(); + var takeNumber = Fixture.Create(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, sortDefinition, takeNumber: takeNumber); + + // Assert + VerifyDefinition(result, documents, DefaultSkipNumber, takeNumber, null, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortDefinitionAndPartitionKey_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortDefinition(); + var partitionKey = Fixture.Create(); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, sortDefinition, partitionKey: partitionKey); + + // Assert + VerifyDefinition(result, documents, DefaultSkipNumber, DefaultTakeNumber, partitionKey, CancellationToken.None); + } + + [Fact] + public async Task WithFilterAndSortDefinitionAndCancellationToken_GetsResults() + { + // Arrange + var documents = SetupReaderWithSortDefinition(); + var token = new CancellationToken(true); + + // Act + var result = await Sut.GetSortedPaginatedAsync(filter, sortDefinition, cancellationToken: token); + + // Assert + VerifyDefinition(result, documents, DefaultSkipNumber, DefaultTakeNumber, null, token); + } + + private List SetupReaderWithSortSelector() + { + var documents = Fixture.CreateMany().ToList(); + Reader = new Mock(); + + Reader.Setup( + x => x.GetSortedPaginatedAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents); + return documents; + } + + private void VerifySelector(List result, List documents, bool ascending, int skipNumber, int takeNumber, string partitionKey, CancellationToken cancellationToken) + { + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(documents); + Reader.Verify( + x => x.GetSortedPaginatedAsync( + filter, + selector, + ascending, + skipNumber, + takeNumber, + partitionKey, + cancellationToken), + Times.Once); + } + + private List SetupReaderWithSortDefinition() + { + var documents = Fixture.CreateMany().ToList(); + Reader = new Mock(); + + Reader.Setup( + x => x.GetSortedPaginatedAsync( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents); + return documents; + } + + private void VerifyDefinition(List result, List documents, int skipNumber, int takeNumber, string partitionKey, CancellationToken cancellationToken) + { + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(documents); + Reader.Verify( + x => x.GetSortedPaginatedAsync( + filter, + sortDefinition, + skipNumber, + takeNumber, + partitionKey, + cancellationToken), + Times.Once); + } +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GroupByTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GroupByTests.cs new file mode 100644 index 0000000..090b433 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GroupByTests.cs @@ -0,0 +1,474 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class GroupByTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> grouping = document => document.GroupingKey; + private readonly Expression, TestProjection>> projection = documents => new TestProjection {Count = documents.Count()}; + private readonly Expression> filter = document => document.GroupingKey == 1; + + [Fact] + public void WithGroupingCriteriaAndProjection_Groups() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + Reader = new Mock(); + + Reader.Setup( + x => x.GroupBy( + It.IsAny>>(), + It.IsAny, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + + // Act + var result = Sut.GroupBy(grouping, projection); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(projections); + Reader.Verify( + x => x.GroupBy(grouping, projection, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithGroupingCriteriaAndProjectionAndCancellationToken_Groups() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + Reader = new Mock(); + + Reader + .Setup( + x => x.GroupBy( + It.IsAny>>(), + It.IsAny, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + + // Act + var result = Sut.GroupBy(grouping, projection, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(projections); + Reader.Verify( + x => x.GroupBy(grouping, projection, null, token), + Times.Once); + } + + [Fact] + public void WithGroupingCriteriaAndProjectionAndPartitionKey_Groups() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + Reader = new Mock(); + + Reader + .Setup( + x => x.GroupBy( + It.IsAny>>(), + It.IsAny, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + + // Act + var result = Sut.GroupBy(grouping, projection, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(projections); + Reader.Verify( + x => x.GroupBy(grouping, projection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithGroupingCriteriaAndProjectionAndPartitionKeyAndCancellationToken_Groups() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Reader = new Mock(); + + Reader + .Setup( + x => x.GroupBy( + It.IsAny>>(), + It.IsAny, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + + // Act + var result = Sut.GroupBy(grouping, projection, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(projections); + Reader.Verify( + x => x.GroupBy(grouping, projection, partitionKey, token), + Times.Once); + } + + [Fact] + public void WithFilterAndGroupingCriteriaAndProjection_Groups() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + Reader = new Mock(); + + Reader.Setup( + x => x.GroupBy( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + + // Act + var result = Sut.GroupBy(filter, grouping, projection); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(projections); + Reader.Verify( + x => x.GroupBy(filter, grouping, projection, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndGroupingCriteriaAndProjectionAndCancellationToken_Groups() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + Reader = new Mock(); + + Reader.Setup( + x => x.GroupBy( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + + // Act + var result = Sut.GroupBy(filter, grouping, projection, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(projections); + Reader.Verify( + x => x.GroupBy(filter, grouping, projection, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndGroupingCriteriaAndProjectionAndPartitionKey_Groups() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + Reader = new Mock(); + + Reader.Setup( + x => x.GroupBy( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + + // Act + var result = Sut.GroupBy(filter, grouping, projection, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(projections); + Reader.Verify( + x => x.GroupBy(filter, grouping, projection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndGroupingCriteriaAndProjectionAndPartitionKeyAndCancellationToken_Groups() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Reader = new Mock(); + + Reader.Setup( + x => x.GroupBy( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + + // Act + var result = Sut.GroupBy(filter, grouping, projection, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(projections); + Reader.Verify( + x => x.GroupBy(filter, grouping, projection, partitionKey, token), + Times.Once); + } + + #region Keyed + + private readonly Expression, int>> keyedGrouping = document => document.GroupingKey; + private readonly Expression>, TestProjection>> keyedProjection = documents => new TestProjection {Count = documents.Count()}; + private readonly Expression, bool>> keyedFilter = document => document.GroupingKey == 1; + + [Fact] + public void Keyed_WithGroupingCriteriaAndProjection_Groups() + { + // Arrange + var keyedProjections = Fixture.CreateMany().ToList(); + Reader = new Mock(); + + Reader.Setup( + x => x.GroupBy, int, TestProjection, int>( + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(keyedProjections); + + // Act + var result = Sut.GroupBy, int, TestProjection, int>(keyedGrouping, keyedProjection); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(keyedProjections); + Reader.Verify( + x => x.GroupBy, int, TestProjection, int>(keyedGrouping, keyedProjection, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithGroupingCriteriaAndProjectionAndCancellationToken_Groups() + { + // Arrange + var keyedProjections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + Reader = new Mock(); + + Reader + .Setup( + x => x.GroupBy, int, TestProjection, int>( + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(keyedProjections); + + // Act + var result = Sut.GroupBy, int, TestProjection, int>(keyedGrouping, keyedProjection, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(keyedProjections); + Reader.Verify( + x => x.GroupBy, int, TestProjection, int>(keyedGrouping, keyedProjection, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithGroupingCriteriaAndProjectionAndPartitionKey_Groups() + { + // Arrange + var keyedProjections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + Reader = new Mock(); + + Reader + .Setup( + x => x.GroupBy, int, TestProjection, int>( + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(keyedProjections); + + // Act + var result = Sut.GroupBy, int, TestProjection, int>(keyedGrouping, keyedProjection, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(keyedProjections); + Reader.Verify( + x => x.GroupBy, int, TestProjection, int>(keyedGrouping, keyedProjection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithGroupingCriteriaAndProjectionAndPartitionKeyAndCancellationToken_Groups() + { + // Arrange + var keyedProjections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Reader = new Mock(); + + Reader + .Setup( + x => x.GroupBy, int, TestProjection, int>( + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(keyedProjections); + + // Act + var result = Sut.GroupBy, int, TestProjection, int>(keyedGrouping, keyedProjection, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(keyedProjections); + Reader.Verify( + x => x.GroupBy, int, TestProjection, int>(keyedGrouping, keyedProjection, partitionKey, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndGroupingCriteriaAndProjection_Groups() + { + // Arrange + var keyedProjections = Fixture.CreateMany().ToList(); + Reader = new Mock(); + + Reader.Setup( + x => x.GroupBy, int, TestProjection, int>( + It.IsAny,bool>>>(), + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(keyedProjections); + + // Act + var result = Sut.GroupBy, int, TestProjection, int>(keyedFilter, keyedGrouping, keyedProjection); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(keyedProjections); + Reader.Verify( + x => x.GroupBy, int, TestProjection, int>(keyedFilter, keyedGrouping, keyedProjection, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndGroupingCriteriaAndProjectionAndCancellationToken_Groups() + { + // Arrange + var keyedProjections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + Reader = new Mock(); + + Reader.Setup( + x => x.GroupBy, int, TestProjection, int>( + It.IsAny,bool>>>(), + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(keyedProjections); + + // Act + var result = Sut.GroupBy, int, TestProjection, int>(keyedFilter, keyedGrouping, keyedProjection, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(keyedProjections); + Reader.Verify( + x => x.GroupBy, int, TestProjection, int>(keyedFilter, keyedGrouping, keyedProjection, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndGroupingCriteriaAndProjectionAndPartitionKey_Groups() + { + // Arrange + var keyedProjections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + Reader = new Mock(); + + Reader.Setup( + x => x.GroupBy, int, TestProjection, int>( + It.IsAny,bool>>>(), + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(keyedProjections); + + // Act + var result = Sut.GroupBy, int, TestProjection, int>(keyedFilter, keyedGrouping, keyedProjection, partitionKey); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(keyedProjections); + Reader.Verify( + x => x.GroupBy, int, TestProjection, int>(keyedFilter, keyedGrouping, keyedProjection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndGroupingCriteriaAndProjectionAndPartitionKeyAndCancellationToken_Groups() + { + // Arrange + var keyedProjections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Reader = new Mock(); + + Reader.Setup( + x => x.GroupBy, int, TestProjection, int>( + It.IsAny,bool>>>(), + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(keyedProjections); + + // Act + var result = Sut.GroupBy, int, TestProjection, int>(keyedFilter, keyedGrouping, keyedProjection, partitionKey, token); + + // Assert + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(keyedProjections); + Reader.Verify( + x => x.GroupBy, int, TestProjection, int>(keyedFilter, keyedGrouping, keyedProjection, partitionKey, token), + Times.Once); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/ProjectManyAsyncTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/ProjectManyAsyncTests.cs new file mode 100644 index 0000000..cedbb19 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/ProjectManyAsyncTests.cs @@ -0,0 +1,214 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class ProjectManyAsyncTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> projection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + + [Fact] + public async Task WithFilterAndProjection_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + + SetupReader(projections); + + // Act + var result = await Sut.ProjectManyAsync(filter, projection); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectManyAsync( + filter, + projection, + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + + SetupReader(projections); + + // Act + var result = await Sut.ProjectManyAsync(filter, projection, token); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectManyAsync(filter, projection, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + SetupReader(projections); + + // Act + var result = await Sut.ProjectManyAsync(filter, projection, partitionKey); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectManyAsync(filter, projection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(projections); + + // Act + var result = await Sut.ProjectManyAsync(filter, projection, partitionKey, token); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectManyAsync(filter, projection, partitionKey, token), + Times.Once); + } + + private void SetupReader(List projections) + { + Reader = new Mock(); + Reader + .Setup( + x => x.ProjectManyAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(projections); + } + + #region Keyed + + private readonly Expression, bool>> keyedFilter = document => document.SomeContent == "SomeContent"; + private readonly Expression, TestProjection>> keyedProjection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + + [Fact] + public async Task Keyed_WithFilterAndProjection_Projects() + { + // Arrange + var keyedProjections = Fixture.CreateMany().ToList(); + + SetupKeyedReader(keyedProjections); + + // Act + var result = await Sut.ProjectManyAsync, TestProjection, int>(keyedFilter, keyedProjection); + + // Assert + result.Should().OnlyContain(x => keyedProjections.Contains(x)); + Reader.Verify( + x => x.ProjectManyAsync, TestProjection, int>( + keyedFilter, + keyedProjection, + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var keyedProjections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + + SetupKeyedReader(keyedProjections); + + // Act + var result = await Sut.ProjectManyAsync, TestProjection, int>(keyedFilter, keyedProjection, token); + + // Assert + result.Should().OnlyContain(x => keyedProjections.Contains(x)); + Reader.Verify( + x => x.ProjectManyAsync, TestProjection, int>(keyedFilter, keyedProjection, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var keyedProjections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + SetupKeyedReader(keyedProjections); + + // Act + var result = await Sut.ProjectManyAsync, TestProjection, int>(keyedFilter, keyedProjection, partitionKey); + + // Assert + result.Should().OnlyContain(x => keyedProjections.Contains(x)); + Reader.Verify( + x => x.ProjectManyAsync, TestProjection, int>(keyedFilter, keyedProjection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var keyedProjections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(keyedProjections); + + // Act + var result = await Sut.ProjectManyAsync, TestProjection, int>(keyedFilter, keyedProjection, partitionKey, token); + + // Assert + result.Should().OnlyContain(x => keyedProjections.Contains(x)); + Reader.Verify( + x => x.ProjectManyAsync, TestProjection, int>(keyedFilter, keyedProjection, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReader(List keyedProjections) + { + Reader = new Mock(); + Reader + .Setup( + x => x.ProjectManyAsync, TestProjection, int>( + It.IsAny, bool>>>(), + It.IsAny, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(keyedProjections); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/ProjectManyTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/ProjectManyTests.cs new file mode 100644 index 0000000..2e761fb --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/ProjectManyTests.cs @@ -0,0 +1,214 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class ProjectManyTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> projection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + + [Fact] + public void WithFilterAndProjection_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + + SetupReader(projections); + + // Act + var result = Sut.ProjectMany(filter, projection); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectMany( + filter, + projection, + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + + SetupReader(projections); + + // Act + var result = Sut.ProjectMany(filter, projection, token); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectMany(filter, projection, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + SetupReader(projections); + + // Act + var result = Sut.ProjectMany(filter, projection, partitionKey); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectMany(filter, projection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(projections); + + // Act + var result = Sut.ProjectMany(filter, projection, partitionKey, token); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectMany(filter, projection, partitionKey, token), + Times.Once); + } + + private void SetupReader(List projections) + { + Reader = new Mock(); + Reader + .Setup( + x => x.ProjectMany( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(projections); + } + + #region Keyed + + private readonly Expression, bool>> keyedFilter = document => document.SomeContent == "SomeContent"; + private readonly Expression, TestProjection>> keyedProjection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + + [Fact] + public void Keyed_WithFilterAndProjection_Projects() + { + // Arrange + var keyedProjections = Fixture.CreateMany().ToList(); + + SetupKeyedReader(keyedProjections); + + // Act + var result = Sut.ProjectMany, TestProjection, int>(keyedFilter, keyedProjection); + + // Assert + result.Should().OnlyContain(x => keyedProjections.Contains(x)); + Reader.Verify( + x => x.ProjectMany, TestProjection, int>( + keyedFilter, + keyedProjection, + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var keyedProjections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + + SetupKeyedReader(keyedProjections); + + // Act + var result = Sut.ProjectMany, TestProjection, int>(keyedFilter, keyedProjection, token); + + // Assert + result.Should().OnlyContain(x => keyedProjections.Contains(x)); + Reader.Verify( + x => x.ProjectMany, TestProjection, int>(keyedFilter, keyedProjection, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var keyedProjections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + SetupKeyedReader(keyedProjections); + + // Act + var result = Sut.ProjectMany, TestProjection, int>(keyedFilter, keyedProjection, partitionKey); + + // Assert + result.Should().OnlyContain(x => keyedProjections.Contains(x)); + Reader.Verify( + x => x.ProjectMany, TestProjection, int>(keyedFilter, keyedProjection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var keyedProjections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(keyedProjections); + + // Act + var result = Sut.ProjectMany, TestProjection, int>(keyedFilter, keyedProjection, partitionKey, token); + + // Assert + result.Should().OnlyContain(x => keyedProjections.Contains(x)); + Reader.Verify( + x => x.ProjectMany, TestProjection, int>(keyedFilter, keyedProjection, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReader(List keyedProjections) + { + Reader = new Mock(); + Reader + .Setup( + x => x.ProjectMany, TestProjection, int>( + It.IsAny, bool>>>(), + It.IsAny, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(keyedProjections); + } + + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/ProjectOneAsyncTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/ProjectOneAsyncTests.cs new file mode 100644 index 0000000..40dc725 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/ProjectOneAsyncTests.cs @@ -0,0 +1,212 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class ProjectOneAsyncTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> projection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + + [Fact] + public async Task WithFilterAndProjection_Projects() + { + // Arrange + var expected = Fixture.Create(); + + SetupReader(expected); + + // Act + var result = await Sut.ProjectOneAsync(filter, projection); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOneAsync( + filter, + projection, + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var expected = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(expected); + + // Act + var result = await Sut.ProjectOneAsync(filter, projection, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOneAsync(filter, projection, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(expected); + + // Act + var result = await Sut.ProjectOneAsync(filter, projection, partitionKey); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOneAsync(filter, projection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(expected); + + // Act + var result = await Sut.ProjectOneAsync(filter, projection, partitionKey, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOneAsync(filter, projection, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestProjection result) + { + Reader = new Mock(); + Reader + .Setup( + x => x.ProjectOneAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(result); + } + + #region keyed + + private readonly Expression, bool>> keyedFilter = document => document.SomeContent == "SomeContent"; + private readonly Expression, TestProjection>> keyedProjection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + + [Fact] + public async Task Keyed_WithFilterAndProjection_Projects() + { + // Arrange + var expected = Fixture.Create(); + + SetupKeyedReader(expected); + + // Act + var result = await Sut.ProjectOneAsync, TestProjection, int>(keyedFilter, keyedProjection); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOneAsync, TestProjection, int>( + keyedFilter, + keyedProjection, + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var expected = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(expected); + + // Act + var result = await Sut.ProjectOneAsync, TestProjection, int>(keyedFilter, keyedProjection, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOneAsync, TestProjection, int>(keyedFilter, keyedProjection, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupKeyedReader(expected); + + // Act + var result = await Sut.ProjectOneAsync, TestProjection, int>(keyedFilter, keyedProjection, partitionKey); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOneAsync, TestProjection, int>(keyedFilter, keyedProjection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(expected); + + // Act + var result = await Sut.ProjectOneAsync, TestProjection, int>(keyedFilter, keyedProjection, partitionKey, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOneAsync, TestProjection, int>(keyedFilter, keyedProjection, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReader(TestProjection result) + { + Reader = new Mock(); + Reader + .Setup( + x => x.ProjectOneAsync, TestProjection, int>( + It.IsAny, bool>>>(), + It.IsAny, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(result); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/ProjectOneTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/ProjectOneTests.cs new file mode 100644 index 0000000..7b14d3b --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/ProjectOneTests.cs @@ -0,0 +1,211 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class ProjectOneTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> projection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + + [Fact] + public void WithFilterAndProjection_Projects() + { + // Arrange + var expected = Fixture.Create(); + + SetupReader(expected); + + // Act + var result = Sut.ProjectOne(filter, projection); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOne( + filter, + projection, + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var expected = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(expected); + + // Act + var result = Sut.ProjectOne(filter, projection, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOne(filter, projection, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(expected); + + // Act + var result = Sut.ProjectOne(filter, projection, partitionKey); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOne(filter, projection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(expected); + + // Act + var result = Sut.ProjectOne(filter, projection, partitionKey, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOne(filter, projection, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestProjection result) + { + Reader = new Mock(); + Reader + .Setup( + x => x.ProjectOne( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(result); + } + + #region Keyed + + private readonly Expression, bool>> keyedFilter = document => document.SomeContent == "SomeContent"; + private readonly Expression, TestProjection>> keyedProjection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + + [Fact] + public void Keyed_WithFilterAndProjection_Projects() + { + // Arrange + var expected = Fixture.Create(); + + SetupKeyedReader(expected); + + // Act + var result = Sut.ProjectOne, TestProjection, int>(keyedFilter, keyedProjection); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOne, TestProjection, int>( + keyedFilter, + keyedProjection, + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var expected = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(expected); + + // Act + var result = Sut.ProjectOne, TestProjection, int>(keyedFilter, keyedProjection, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOne, TestProjection, int>(keyedFilter, keyedProjection, null, token), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupKeyedReader(expected); + + // Act + var result = Sut.ProjectOne, TestProjection, int>(keyedFilter, keyedProjection, partitionKey); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOne, TestProjection, int>(keyedFilter, keyedProjection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void Keyed_WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReader(expected); + + // Act + var result = Sut.ProjectOne, TestProjection, int>(keyedFilter, keyedProjection, partitionKey, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOne, TestProjection, int>(keyedFilter, keyedProjection, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReader(TestProjection result) + { + Reader = new Mock(); + Reader + .Setup( + x => x.ProjectOne, TestProjection, int>( + It.IsAny, bool>>>(), + It.IsAny, TestProjection>>>(), + It.IsAny(), + It.IsAny())) + .Returns(result); + } + + #endregion +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/SumByAsyncTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/SumByAsyncTests.cs new file mode 100644 index 0000000..3a18707 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/SumByAsyncTests.cs @@ -0,0 +1,384 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; + +public class SumByAsyncTests : TestReadOnlyMongoRepositoryContext +{ + private readonly Expression> decimalSelector = document => document.SomeDecimalValue; + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> intSelector = document => document.SomeValue; + + [Fact] + public async Task Int_WithFilterAndSelector_Sums() + { + // Arrange + var expected = Fixture.Create(); + + SetupReaderInt(expected); + + // Act + var result = await Sut.SumByAsync(filter, intSelector); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync(filter, intSelector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Int_WithFilterAndSelectorAndCancellationToken_Sums() + { + // Arrange + var expected = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReaderInt(expected); + + // Act + var result = await Sut.SumByAsync(filter, intSelector, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync(filter, intSelector, null, token), + Times.Once); + } + + [Fact] + public async Task Int_WithFilterAndSelectorAndPartitionKey_Sums() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReaderInt(expected); + + // Act + var result = await Sut.SumByAsync(filter, intSelector, partitionKey); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync(filter, intSelector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Int_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_Sums() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReaderInt(expected); + + // Act + var result = await Sut.SumByAsync(filter, intSelector, partitionKey, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync(filter, intSelector, partitionKey, token), + Times.Once); + } + + [Fact] + public async Task Decimal_WithFilterAndSelector_Sums() + { + // Arrange + var expected = Fixture.Create(); + + SetupReaderDecimal(expected); + + // Act + var result = await Sut.SumByAsync(filter, decimalSelector); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync(filter, decimalSelector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Decimal_WithFilterAndSelectorAndCancellationToken_Sums() + { + // Arrange + var expected = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReaderDecimal(expected); + + // Act + var result = await Sut.SumByAsync(filter, decimalSelector, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync(filter, decimalSelector, null, token), + Times.Once); + } + + [Fact] + public async Task Decimal_WithFilterAndSelectorAndPartitionKey_Sums() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReaderDecimal(expected); + + // Act + var result = await Sut.SumByAsync(filter, decimalSelector, partitionKey); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync(filter, decimalSelector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Decimal_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_Sums() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReaderDecimal(expected); + + // Act + var result = await Sut.SumByAsync(filter, decimalSelector, partitionKey, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync(filter, decimalSelector, partitionKey, token), + Times.Once); + } + + private void SetupReaderInt(int expected) + { + Reader = new Mock(); + Reader + .Setup( + x => x.SumByAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(expected); + } + + private void SetupReaderDecimal(decimal expected) + { + Reader = new Mock(); + Reader + .Setup( + x => x.SumByAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(expected); + } + + #region Keyed + + private readonly Expression, bool>> keyedFilter = document => document.SomeContent == "SomeContent"; + private readonly Expression, int>> keyedIntSelector = document => document.SomeValue; + private readonly Expression, decimal>> keyedDecimalSelector = document => document.SomeDecimalValue; + + [Fact] + public async Task Keyed_Int_WithFilterAndSelector_Sums() + { + // Arrange + var expected = Fixture.Create(); + + SetupKeyedReaderInt(expected); + + // Act + var result = await Sut.SumByAsync, int>(keyedFilter, keyedIntSelector); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync, int>(keyedFilter, keyedIntSelector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_Int_WithFilterAndSelectorAndCancellationToken_Sums() + { + // Arrange + var expected = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderInt(expected); + + // Act + var result = await Sut.SumByAsync, int>(keyedFilter, keyedIntSelector, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync, int>(keyedFilter, keyedIntSelector, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_Int_WithFilterAndSelectorAndPartitionKey_Sums() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupKeyedReaderInt(expected); + + // Act + var result = await Sut.SumByAsync, int>(keyedFilter, keyedIntSelector, partitionKey); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync, int>(keyedFilter, keyedIntSelector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_Int_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_Sums() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderInt(expected); + + // Act + var result = await Sut.SumByAsync, int>(keyedFilter, keyedIntSelector, partitionKey, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync, int>(keyedFilter, keyedIntSelector, partitionKey, token), + Times.Once); + } + + [Fact] + public async Task Keyed_Decimal_WithFilterAndSelector_Sums() + { + // Arrange + var expected = Fixture.Create(); + + SetupKeyedReaderDecimal(expected); + + // Act + var result = await Sut.SumByAsync, int>(keyedFilter, keyedDecimalSelector); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync, int>(keyedFilter, keyedDecimalSelector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_Decimal_WithFilterAndSelectorAndCancellationToken_Sums() + { + // Arrange + var expected = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderDecimal(expected); + + // Act + var result = await Sut.SumByAsync, int>(keyedFilter, keyedDecimalSelector, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync, int>(keyedFilter, keyedDecimalSelector, null, token), + Times.Once); + } + + [Fact] + public async Task Keyed_Decimal_WithFilterAndSelectorAndPartitionKey_Sums() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupKeyedReaderDecimal(expected); + + // Act + var result = await Sut.SumByAsync, int>(keyedFilter, keyedDecimalSelector, partitionKey); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync, int>(keyedFilter, keyedDecimalSelector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Keyed_Decimal_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_Sums() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupKeyedReaderDecimal(expected); + + // Act + var result = await Sut.SumByAsync, int>(keyedFilter, keyedDecimalSelector, partitionKey, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync, int>(keyedFilter, keyedDecimalSelector, partitionKey, token), + Times.Once); + } + + private void SetupKeyedReaderInt(int expected) + { + Reader = new Mock(); + Reader + .Setup( + x => x.SumByAsync, int>( + It.IsAny, bool>>>(), + It.IsAny,int>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(expected); + } + + private void SetupKeyedReaderDecimal(decimal expected) + { + Reader = new Mock(); + Reader + .Setup( + x => x.SumByAsync, int>( + It.IsAny, bool>>>(), + It.IsAny, decimal>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(expected); + } + + #endregion +} diff --git a/IntegrationTests/IntegrationTests.csproj b/IntegrationTests/IntegrationTests.csproj index ac1c4ef..ec385c5 100644 --- a/IntegrationTests/IntegrationTests.csproj +++ b/IntegrationTests/IntegrationTests.csproj @@ -11,7 +11,7 @@ Properties IntegrationTests IntegrationTests - v4.6.1 + v4.8 512 diff --git a/MongoDbGenericRepository.sln b/MongoDbGenericRepository.sln index cb7da2b..9d1e6d8 100644 --- a/MongoDbGenericRepository.sln +++ b/MongoDbGenericRepository.sln @@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoDbGenericRepository", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoreIntegrationTests", "CoreIntegrationTests\CoreIntegrationTests.csproj", "{C640C106-7A25-4E49-A0CF-E4F248E5A97F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreUnitTests", "CoreUnitTests\CoreUnitTests.csproj", "{8BE513ED-84F4-47E1-946D-84958CF95E6B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {C640C106-7A25-4E49-A0CF-E4F248E5A97F}.Debug|Any CPU.Build.0 = Debug|Any CPU {C640C106-7A25-4E49-A0CF-E4F248E5A97F}.Release|Any CPU.ActiveCfg = Release|Any CPU {C640C106-7A25-4E49-A0CF-E4F248E5A97F}.Release|Any CPU.Build.0 = Release|Any CPU + {8BE513ED-84F4-47E1-946D-84958CF95E6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8BE513ED-84F4-47E1-946D-84958CF95E6B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8BE513ED-84F4-47E1-946D-84958CF95E6B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8BE513ED-84F4-47E1-946D-84958CF95E6B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs index f163354..a35c5c1 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs @@ -1,35 +1,42 @@ -using MongoDB.Driver; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The IBaseMongoRepository interface exposes the CRUD functionality of the BaseMongoRepository. + /// The IBaseMongoRepository interface exposes the CRUD functionality of the BaseMongoRepository. /// - public interface IBaseMongoRepository : - IReadOnlyMongoRepository, - IBaseMongoRepository_Create, - IBaseMongoRepository_Update, - IBaseMongoRepository_Delete, + public interface IBaseMongoRepository : + IReadOnlyMongoRepository, + IBaseMongoRepository_Create, + IBaseMongoRepository_Update, + IBaseMongoRepository_Delete, IBaseMongoRepository_Index { /// - /// Asynchronously returns a paginated list of the documents matching the filter condition. + /// Asynchronously returns a paginated list of the documents matching the filter condition. /// /// The type representing a Document. /// /// The number of documents you want to skip. Default value is 0. /// The number of documents you want to take. Default value is 50. /// An optional partition key. - Task> GetPaginatedAsync(Expression> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null) + /// An optional cancellation token. + Task> GetPaginatedAsync( + Expression> filter, + int skipNumber = 0, + int takeNumber = 50, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument; /// - /// Asynchronously returns a paginated list of the documents matching the filter condition. + /// Asynchronously returns a paginated list of the documents matching the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -37,23 +44,48 @@ namespace MongoDbGenericRepository /// The number of documents you want to skip. Default value is 0. /// The number of documents you want to take. Default value is 50. /// An optional partition key. - Task> GetPaginatedAsync(Expression> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null) + /// An optional cancellation token. + Task> GetPaginatedAsync( + Expression> filter, + int skipNumber = 0, + int takeNumber = 50, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; /// - /// GetAndUpdateOne with filter + /// GetAndUpdateOne with filter /// /// The type representing a Document. /// /// /// /// - Task GetAndUpdateOne(FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options) + Task GetAndUpdateOne( + FilterDefinition filter, + UpdateDefinition update, + FindOneAndUpdateOptions options) where TDocument : IDocument; /// - /// GetAndUpdateOne with filter + /// GetAndUpdateOne with filter + /// + /// The type representing a Document. + /// + /// + /// + /// The cancellation token. + /// + Task GetAndUpdateOne( + FilterDefinition filter, + UpdateDefinition update, + FindOneAndUpdateOptions options, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// GetAndUpdateOne with filter /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -61,9 +93,29 @@ namespace MongoDbGenericRepository /// /// /// - Task GetAndUpdateOne(FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options) - where TDocument : IDocument - where TKey : IEquatable; - } + Task GetAndUpdateOne( + FilterDefinition filter, + UpdateDefinition update, + FindOneAndUpdateOptions options) + where TDocument : IDocument + where TKey : IEquatable; -} + /// + /// GetAndUpdateOne with filter + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// + /// + /// + /// The cancellation token. + /// + Task GetAndUpdateOne( + FilterDefinition filter, + UpdateDefinition update, + FindOneAndUpdateOptions options, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update.cs b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update.cs index 86ec936..10dc3c2 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update.cs @@ -2,6 +2,7 @@ using MongoDbGenericRepository.Models; using System; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository @@ -21,6 +22,17 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable; + /// + /// Asynchronously Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document with the modifications you want to persist. + /// The cancellation token. + Task UpdateOneAsync(TDocument modifiedDocument, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + /// /// Updates a document. /// @@ -31,6 +43,17 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable; + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document with the modifications you want to persist. + /// The cancellation token. + bool UpdateOne(TDocument modifiedDocument, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + /// /// Takes a document you want to modify and applies the update you have defined in MongoDb. /// @@ -42,6 +65,18 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable; + /// + /// Takes a document you want to modify and applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to modify. + /// The update definition for the document. + /// The cancellation token. + Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + /// /// Takes a document you want to modify and applies the update you have defined in MongoDb. /// @@ -53,6 +88,45 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable; + /// + /// Takes a document you want to modify and applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to modify. + /// The update definition for the document. + /// The cancellation token. + bool UpdateOne(TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entity selected by the filter, updates the property field with the given value.. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + bool UpdateOne(Expression> filter, Expression> field, TField value) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entity selected by the filter, updates the property field with the given value.. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + bool UpdateOne(Expression> filter, Expression> field, TField value, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + /// /// For the entity selected by the filter, updates the property field with the given value.. /// @@ -63,7 +137,22 @@ namespace MongoDbGenericRepository /// The field selector. /// The new value of the property field. /// The partition key for the document. - bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey = null) + bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entity selected by the filter, updates the property field with the given value.. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + /// The cancellation token. + bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -80,6 +169,47 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable; + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document you want to modify. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entity selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + Task UpdateOneAsync(Expression> filter, Expression> field, TField value) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entity selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + Task UpdateOneAsync(Expression> filter, Expression> field, TField value, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + /// /// For the entity selected by the filter, updates the property field with the given value. /// @@ -90,7 +220,22 @@ namespace MongoDbGenericRepository /// The field selector. /// The new value of the property field. /// The partition key for the document. - Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entity selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + /// The cancellation token. + Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -107,6 +252,20 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable; + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document you want to modify. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + bool UpdateOne(TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + /// /// Updates the property field with the given value update a property field in entities. /// @@ -116,8 +275,21 @@ namespace MongoDbGenericRepository /// The document filter. /// The field selector. /// The new value of the property field. - /// The value of the partition key. - Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -131,7 +303,105 @@ namespace MongoDbGenericRepository /// The field selector. /// The new value of the property field. /// The value of the partition key. - bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + /// The cancellation token. + Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + bool UpdateOne(FilterDefinition filter, Expression> field, TField value) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + bool UpdateOne(FilterDefinition filter, Expression> field, TField value, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + /// The cancellation token. + bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + Task UpdateManyAsync(Expression> filter, Expression> field, TField value) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + Task UpdateManyAsync(Expression> filter, Expression> field, TField value, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -145,45 +415,7 @@ namespace MongoDbGenericRepository /// The field selector. /// The new value of the property field. /// The partition key for the document. - Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) + Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey) where TDocument : IDocument where TKey : IEquatable; @@ -197,7 +429,35 @@ namespace MongoDbGenericRepository /// The field selector. /// The new value of the property field. /// The partition key for the document. - long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// The cancellation token. + Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -211,7 +471,45 @@ namespace MongoDbGenericRepository /// The field selector. /// The new value of the property field. /// The value of the partition key. - long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + /// The cancellation token. + Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The cancellation token. + Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -223,7 +521,7 @@ namespace MongoDbGenericRepository /// The document filter. /// The update definition to apply. /// The value of the partition key. - long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) + Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey) where TDocument : IDocument where TKey : IEquatable; @@ -235,8 +533,265 @@ namespace MongoDbGenericRepository /// The document filter. /// The update definition to apply. /// The value of the partition key. - long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + /// The cancellation token. + Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The cancellation token. + Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + /// The cancellation token. + Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + long UpdateMany(Expression> filter, Expression> field, TField value) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + long UpdateMany(Expression> filter, Expression> field, TField value, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + /// The cancellation token. + long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + long UpdateMany(FilterDefinition filter, Expression> field, TField value) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + long UpdateMany(FilterDefinition filter, Expression> field, TField value, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + /// The cancellation token. + long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + long UpdateMany(Expression> filter, UpdateDefinition updateDefinition) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The cancellation token. + long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + /// The cancellation token. + long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The cancellation token. + long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + /// The cancellation token. + long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs index 9e9223b..13cd40f 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs @@ -8,12 +8,73 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository.DataAccess.Update { /// - /// The IBaseMongoRepository_Update_ClientSession interface exposing update functionality with a IClientSessionHandle. + /// The IBaseMongoRepository_Update_ClientSession interface exposing update functionality with a IClientSessionHandle. /// public interface IBaseMongoRepository_Update_ClientSession { /// - /// Updates a document. + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// A boolean value indicating success. + bool UpdateOne( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// The optional cancellation token. + /// A boolean value indicating success. + bool UpdateOne( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// The optional partition key. + /// A boolean value indicating success. + bool UpdateOne( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -25,12 +86,79 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The optional partition key. /// The optional cancellation token. /// A boolean value indicating success. - bool UpdateOne(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + bool UpdateOne( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// A boolean value indicating success. + bool UpdateOne( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// The optional cancellation token. + /// A boolean value indicating success. + bool UpdateOne( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// The optional partition key. + /// A boolean value indicating success. + bool UpdateOne( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -42,12 +170,37 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The optional partition key. /// The optional cancellation token. /// A boolean value indicating success. - bool UpdateOne(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + bool UpdateOne( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The document to modify. + /// The field to update. + /// The value of the field. + /// A boolean value indicating success. + bool UpdateOne( + IClientSessionHandle session, + TDocument documentToModify, + Expression> field, + TField value) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -58,12 +211,29 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The value of the field. /// The optional cancellation token. /// A boolean value indicating success. - bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) + bool UpdateOne( + IClientSessionHandle session, + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The client session. + /// The document with the modifications you want to persist. + /// A boolean value indicating success. + bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -71,12 +241,25 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The document with the modifications you want to persist. /// The optional cancellation token. /// A boolean value indicating success. - bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) + bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The client session. + /// The document to modify. + /// The update definition. + /// A boolean value indicating success. + bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -85,12 +268,77 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The update definition. /// The optional cancellation token. /// A boolean value indicating success. - bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) + bool UpdateOne( + IClientSessionHandle session, + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// A boolean value indicating success. + Task UpdateOneAsync( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// The optional cancellation token. + /// A boolean value indicating success. + Task UpdateOneAsync( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// The optional partition key. + /// A boolean value indicating success. + Task UpdateOneAsync( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -102,12 +350,79 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The optional partition key. /// The optional cancellation token. /// A boolean value indicating success. - Task UpdateOneAsync(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + Task UpdateOneAsync( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// + Task UpdateOneAsync( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// The optional cancellation token. + /// + Task UpdateOneAsync( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// The optional partition key. + /// + Task UpdateOneAsync( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -119,28 +434,70 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The optional partition key. /// The optional cancellation token. /// - Task UpdateOneAsync(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + Task UpdateOneAsync( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field to update. - /// The client session. + /// The client session. + /// The document to modify. + /// The field to update. + /// The value of the field. + /// A boolean value indicating success. + Task UpdateOneAsync( + IClientSessionHandle session, + TDocument documentToModify, + Expression> field, + TField value) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. /// The document to modify. /// The field to update. /// The value of the field. /// The optional cancellation token. /// A boolean value indicating success. - Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) + Task UpdateOneAsync( + IClientSessionHandle session, + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The client session. + /// The document with the modifications you want to persist. + /// A boolean value indicating success. + Task UpdateOneAsync(IClientSessionHandle session, TDocument modifiedDocument) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -148,12 +505,25 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The document with the modifications you want to persist. /// The optional cancellation token. /// A boolean value indicating success. - Task UpdateOneAsync(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) + Task UpdateOneAsync(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The client session. + /// The document to modify. + /// The update definition. + /// A boolean value indicating success. + Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -162,7 +532,11 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The update definition. /// The optional cancellation token. /// A boolean value indicating success. - Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) + Task UpdateOneAsync( + IClientSessionHandle session, + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; } diff --git a/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs b/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs index 185ef77..94000b1 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs @@ -1,56 +1,194 @@ -using MongoDB.Driver; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The IBaseReadOnlyRepository exposes the generic Read Only functionality of the BaseMongoRepository. + /// The IBaseReadOnlyRepository exposes the generic Read Only functionality of the BaseMongoRepository. /// public interface IBaseReadOnlyRepository { /// - /// The connection string. + /// The connection string. /// string ConnectionString { get; } /// - /// The database name. + /// The database name. /// string DatabaseName { get; } #region Read TKey /// - /// Asynchronously returns one document given its id. + /// Asynchronously returns one document given its id. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The Id of the document you want to get. + Task GetByIdAsync(TKey id) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns one document given its id. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The Id of the document you want to get. + /// An optional cancellation Token. + Task GetByIdAsync(TKey id, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns one document given its id. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The Id of the document you want to get. + /// An optional partition key. + Task GetByIdAsync(TKey id, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns one document given its id. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The Id of the document you want to get. /// An optional partition key. /// An optional cancellation Token. - Task GetByIdAsync(TKey id, string partitionKey = null, CancellationToken cancellationToken = default) + Task GetByIdAsync(TKey id, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Returns one document given its id. + /// Returns one document given its id. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The Id of the document you want to get. + TDocument GetById(TKey id) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns one document given its id. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The Id of the document you want to get. + /// An optional cancellation token. + TDocument GetById(TKey id, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns one document given its id. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The Id of the document you want to get. /// An optional partition key. - TDocument GetById(TKey id, string partitionKey = null) + TDocument GetById(TKey id, string partitionKey) where TDocument : IDocument where TKey : IEquatable; /// - /// Asynchronously returns one document given filter definition. + /// Returns one document given its id. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The Id of the document you want to get. + /// An optional partition key. + /// An optional cancellation token. + TDocument GetById(TKey id, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + Task GetOneAsync(FilterDefinition condition) + where TDocument : IDocument where TKey : IEquatable; + + /// + /// Asynchronously returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + Task GetOneAsync(FilterDefinition condition, FindOptions findOption) + where TDocument : IDocument where TKey : IEquatable; + + /// + /// Asynchronously returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional partition key. + Task GetOneAsync(FilterDefinition condition, string partitionKey) + where TDocument : IDocument where TKey : IEquatable; + + /// + /// Asynchronously returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + Task GetOneAsync(FilterDefinition condition, FindOptions findOption, string partitionKey) + where TDocument : IDocument where TKey : IEquatable; + + /// + /// Asynchronously returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional cancellation Token. + Task GetOneAsync(FilterDefinition condition, CancellationToken cancellationToken) + where TDocument : IDocument where TKey : IEquatable; + + /// + /// Asynchronously returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional cancellation Token. + Task GetOneAsync(FilterDefinition condition, FindOptions findOption, CancellationToken cancellationToken) + where TDocument : IDocument where TKey : IEquatable; + + /// + /// Asynchronously returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional partition key. + /// An optional cancellation Token. + Task GetOneAsync(FilterDefinition condition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns one document given filter definition. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -58,46 +196,201 @@ namespace MongoDbGenericRepository /// A mongodb filter option. /// An optional partition key. /// An optional cancellation Token. - Task GetOneAsync(FilterDefinition condition, FindOptions findOption = null, - string partitionKey = null, CancellationToken cancellationToken = default) - where TDocument : IDocument where TKey : IEquatable; + Task GetOneAsync( + FilterDefinition condition, + FindOptions findOption, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; /// - /// Returns one document given filter definition. + /// Returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + TDocument GetOne(FilterDefinition condition) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + TDocument GetOne(FilterDefinition condition, FindOptions findOption) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional partition key. + TDocument GetOne(FilterDefinition condition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns one document given filter definition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A mongodb filter definition. /// A mongodb filter option. /// An optional partition key. - TDocument GetOne(FilterDefinition condition, FindOptions findOption = null, string partitionKey = null) - where TDocument : IDocument where TKey : IEquatable; + TDocument GetOne(FilterDefinition condition, FindOptions findOption, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; /// - /// Asynchronously returns one document given an expression filter. + /// Returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional cancellation token. + TDocument GetOne(FilterDefinition condition, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + + /// + /// Returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional cancellation token. + TDocument GetOne(FilterDefinition condition, FindOptions findOption, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional partition key. + /// An optional cancellation token. + TDocument GetOne(FilterDefinition condition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + /// An optional cancellation token. + TDocument GetOne( + FilterDefinition condition, + FindOptions findOption, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns one document given an expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + Task GetOneAsync(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns one document given an expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional cancellation Token. + Task GetOneAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns one document given an expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + Task GetOneAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns one document given an expression filter. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. /// An optional cancellation Token. - Task GetOneAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) + Task GetOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Returns one document given an expression filter. + /// Returns one document given an expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + TDocument GetOne(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns one document given an expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional cancellation token. + TDocument GetOne(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns one document given an expression filter. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. - TDocument GetOne(Expression> filter, string partitionKey = null) + TDocument GetOne(Expression> filter, string partitionKey) where TDocument : IDocument where TKey : IEquatable; /// - /// Returns a collection cursor. + /// Returns one document given an expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// An optional cancellation token. + TDocument GetOne(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns a collection cursor. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -108,7 +401,86 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Returns true if any of the document of the collection matches the filter condition. + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional cancellation Token. + Task AnyAsync(FilterDefinition condition, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + Task AnyAsync(FilterDefinition condition) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional partition key. + Task AnyAsync(FilterDefinition condition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + Task AnyAsync(FilterDefinition condition, CountOptions countOption) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partition key. + Task AnyAsync(FilterDefinition condition, CountOptions countOption, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional partition key. + /// An optional cancellation Token. + Task AnyAsync(FilterDefinition condition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional cancellation Token. + Task AnyAsync(FilterDefinition condition, CountOptions countOption, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -116,46 +488,275 @@ namespace MongoDbGenericRepository /// A mongodb counting option. /// An optional partition key. /// An optional cancellation Token. - Task AnyAsync(FilterDefinition condition, CountOptions countOption = null, string partitionKey = null, - CancellationToken cancellationToken = default) - where TDocument : IDocument where TKey : IEquatable; + Task AnyAsync( + FilterDefinition condition, + CountOptions countOption, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; /// - /// Returns true if any of the document of the collection matches the filter condition. + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + bool Any(FilterDefinition condition) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional partition key. + bool Any(FilterDefinition condition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + bool Any(FilterDefinition condition, CountOptions countOption) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A mongodb filter definition. /// A mongodb counting option. /// An optional partition key. - bool Any(FilterDefinition condition, CountOptions countOption = null, string partitionKey = null) - where TDocument : IDocument where TKey : IEquatable; + bool Any(FilterDefinition condition, CountOptions countOption, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; /// - /// Returns true if any of the document of the collection matches the filter condition. + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional cancellation token. + bool Any(FilterDefinition condition, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional partition key. + /// An optional cancellation token. + bool Any(FilterDefinition condition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional cancellation token. + bool Any(FilterDefinition condition, CountOptions countOption, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partition key. + /// An optional cancellation token. + bool Any(FilterDefinition condition, CountOptions countOption, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + Task AnyAsync(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional cancellation Token. + Task AnyAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + Task AnyAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. /// An optional cancellation Token. - Task AnyAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) + Task AnyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Returns true if any of the document of the collection matches the filter condition. + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + bool Any(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional cancellation Token. + bool Any(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. - bool Any(Expression> filter, string partitionKey = null) + bool Any(Expression> filter, string partitionKey) where TDocument : IDocument where TKey : IEquatable; /// - /// Asynchronously returns a list of the documents matching the filter condition. + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// An optional cancellation Token. + bool Any(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + Task> GetAllAsync(FilterDefinition condition) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional partition key. + Task> GetAllAsync(FilterDefinition condition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + Task> GetAllAsync(FilterDefinition condition, FindOptions findOption) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + Task> GetAllAsync(FilterDefinition condition, FindOptions findOption, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional cancellation Token. + Task> GetAllAsync(FilterDefinition condition, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional partition key. + /// An optional cancellation Token. + Task> GetAllAsync(FilterDefinition condition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional cancellation Token. + Task> GetAllAsync(FilterDefinition condition, FindOptions findOption, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -163,46 +764,279 @@ namespace MongoDbGenericRepository /// A mongodb filter option. /// An optional partition key. /// An optional cancellation Token. - Task> GetAllAsync(FilterDefinition condition, - FindOptions findOption = null, string partitionKey = null, CancellationToken cancellationToken = default) - where TDocument : IDocument where TKey : IEquatable; + Task> GetAllAsync( + FilterDefinition condition, + FindOptions findOption, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; /// - /// Returns a list of the documents matching the filter condition. + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + List GetAll(FilterDefinition condition) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional partition key. + List GetAll(FilterDefinition condition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + List GetAll(FilterDefinition condition, FindOptions findOption) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns a list of the documents matching the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A mongodb filter definition. /// A mongodb filter option. /// An optional partition key. - List GetAll(FilterDefinition condition, FindOptions findOption = null, string partitionKey = null) - where TDocument : IDocument where TKey : IEquatable; - + List GetAll(FilterDefinition condition, FindOptions findOption, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + /// - /// Asynchronously returns a list of the documents matching the filter condition. + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional cancellation Token. + List GetAll(FilterDefinition condition, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional partition key. + /// An optional cancellation Token. + List GetAll(FilterDefinition condition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional cancellation Token. + List GetAll(FilterDefinition condition, FindOptions findOption, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + /// An optional cancellation Token. + List GetAll( + FilterDefinition condition, + FindOptions findOption, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + Task> GetAllAsync(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional cancellation Token. + Task> GetAllAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + Task> GetAllAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. /// An optional cancellation Token. - Task> GetAllAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) + Task> GetAllAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Returns a list of the documents matching the filter condition. + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + List GetAll(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional cancellation token. + List GetAll(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns a list of the documents matching the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. - List GetAll(Expression> filter, string partitionKey = null) + List GetAll(Expression> filter, string partitionKey) where TDocument : IDocument where TKey : IEquatable; /// - /// Asynchronously counts how many documents match the filter condition. + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// An optional cancellation token. + List GetAll(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + Task CountAsync(FilterDefinition condition) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional partitionKey + Task CountAsync(FilterDefinition condition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + Task CountAsync(FilterDefinition condition, CountOptions countOption) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partitionKey + Task CountAsync(FilterDefinition condition, CountOptions countOption, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional cancellation Token. + Task CountAsync(FilterDefinition condition, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional partitionKey + /// An optional cancellation Token. + Task CountAsync(FilterDefinition condition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional cancellation Token. + Task CountAsync(FilterDefinition condition, CountOptions countOption, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously counts how many documents match the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -210,42 +1044,191 @@ namespace MongoDbGenericRepository /// A mongodb counting option. /// An optional partitionKey /// An optional cancellation Token. - Task CountAsync(FilterDefinition condition, CountOptions countOption = null, - string partitionKey = null, CancellationToken cancellationToken = default) - where TDocument : IDocument where TKey : IEquatable; + Task CountAsync( + FilterDefinition condition, + CountOptions countOption, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; /// - /// Counts how many documents match the filter condition. + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + long Count(FilterDefinition condition) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional partitionKey + long Count(FilterDefinition condition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + long Count(FilterDefinition condition, CountOptions countOption) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Counts how many documents match the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A mongodb filter definition. /// A mongodb counting option. /// An optional partitionKey - long Count(FilterDefinition condition, CountOptions countOption = null, - string partitionKey = null) - where TDocument : IDocument where TKey : IEquatable; - + long Count(FilterDefinition condition, CountOptions countOption, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + /// - /// Asynchronously counts how many documents match the filter condition. + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional cancellation token. + long Count(FilterDefinition condition, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// An optional partitionKey + /// An optional cancellation token. + long Count(FilterDefinition condition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional cancellation token. + long Count(FilterDefinition condition, CountOptions countOption, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partitionKey + /// An optional cancellation token. + long Count(FilterDefinition condition, CountOptions countOption, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + Task CountAsync(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional cancellation Token. + Task CountAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partitionKey + Task CountAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously counts how many documents match the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partitionKey /// An optional cancellation Token. - Task CountAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) + Task CountAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Counts how many documents match the filter condition. + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + long Count(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional Cancellation Token. + long Count(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Counts how many documents match the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partitionKey - long Count(Expression> filter, string partitionKey = null) + long Count(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partitionKey + /// An optional Cancellation Token. + long Count(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -254,7 +1237,53 @@ namespace MongoDbGenericRepository #region Min / Max /// - /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter. + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// The type of the primary key. + /// A LINQ expression filter. + /// A property selector to order by descending. + Task GetByMaxAsync(Expression> filter, Expression> maxValueSelector) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// The type of the primary key. + /// A LINQ expression filter. + /// A property selector to order by descending. + /// An optional cancellation Token. + Task GetByMaxAsync( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// The type of the primary key. + /// A LINQ expression filter. + /// A property selector to order by descending. + /// An optional partitionKey. + Task GetByMaxAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. /// /// The document type. /// The type of the primary key. @@ -265,52 +1294,224 @@ namespace MongoDbGenericRepository Task GetByMaxAsync( Expression> filter, Expression> maxValueSelector, - string partitionKey = null, CancellationToken cancellationToken = default) + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter. + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// The type of the primary key. + /// A LINQ expression filter. + /// A property selector to order by descending. + TDocument GetByMax(Expression> filter, Expression> orderByDescending) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// The type of the primary key. + /// A LINQ expression filter. + /// A property selector to order by descending. + /// An optional cancellation token. + TDocument GetByMax( + Expression> filter, + Expression> orderByDescending, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. /// /// The document type. /// The type of the primary key. /// A LINQ expression filter. /// A property selector to order by descending. /// An optional partitionKey. - TDocument GetByMax(Expression> filter, Expression> orderByDescending, string partitionKey = null) + TDocument GetByMax( + Expression> filter, + Expression> orderByDescending, + string partitionKey) where TDocument : IDocument where TKey : IEquatable; /// - /// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the filter. + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// The type of the primary key. + /// A LINQ expression filter. + /// A property selector to order by descending. + /// An optional partitionKey. + /// An optional cancellation token. + TDocument GetByMax( + Expression> filter, + Expression> orderByDescending, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// The type of the primary key. + /// A LINQ expression filter. + /// A property selector for the minimum value you are looking for. + Task GetByMinAsync(Expression> filter, Expression> minValueSelector) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the + /// filter. /// /// The document type. /// The type of the primary key. /// A LINQ expression filter. /// A property selector for the minimum value you are looking for. - /// An optional partitionKey. /// An optional cancellation Token. - Task GetByMinAsync(Expression> filter, + Task GetByMinAsync( + Expression> filter, Expression> minValueSelector, - string partitionKey = null, - CancellationToken cancellationToken = default) + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the filter. + /// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the + /// filter. /// /// The document type. /// The type of the primary key. /// A LINQ expression filter. /// A property selector for the minimum value you are looking for. /// An optional partitionKey. - TDocument GetByMin(Expression> filter, Expression> minValueSelector, string partitionKey = null) + Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey) where TDocument : IDocument where TKey : IEquatable; /// - /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// The type of the primary key. + /// A LINQ expression filter. + /// A property selector for the minimum value you are looking for. + /// An optional partitionKey. + /// An optional cancellation token. + Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// The type of the primary key. + /// A LINQ expression filter. + /// A property selector for the minimum value you are looking for. + TDocument GetByMin(Expression> filter, Expression> minValueSelector) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// The type of the primary key. + /// A LINQ expression filter. + /// A property selector for the minimum value you are looking for. + /// An optional cancellation token. + TDocument GetByMin( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// The type of the primary key. + /// A LINQ expression filter. + /// A property selector for the minimum value you are looking for. + /// An optional partitionKey. + TDocument GetByMin(Expression> filter, Expression> minValueSelector, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// The type of the primary key. + /// A LINQ expression filter. + /// A property selector for the minimum value you are looking for. + /// An optional partitionKey. + /// An optional cancellation token. + TDocument GetByMin( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the primary key. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector for the maximum value you are looking for. + Task GetMaxValueAsync(Expression> filter, Expression> maxValueSelector) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the primary key. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector for the maximum value you are looking for. + /// An optional cancellation Token. + Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. /// /// The document type. /// The type of the primary key. @@ -318,17 +1519,61 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector for the maximum value you are looking for. /// An optional partitionKey. - /// An optional cancellation Token. Task GetMaxValueAsync( Expression> filter, Expression> maxValueSelector, - string partitionKey = null, - CancellationToken cancellationToken = default) + string partitionKey) where TDocument : IDocument where TKey : IEquatable; /// - /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the primary key. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector for the maximum value you are looking for. + /// An optional partitionKey. + /// An optional cancellation token. + Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the primary key. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + TValue GetMaxValue(Expression> filter, Expression> maxValueSelector) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the primary key. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional cancellation token. + TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. /// /// The document type. /// The type of the primary key. @@ -336,30 +1581,61 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partitionKey. - TValue GetMaxValue(Expression> filter, Expression> maxValueSelector, string partitionKey = null) + TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) where TDocument : IDocument where TKey : IEquatable; /// - /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the primary key. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional partitionKey. + /// An optional cancellation token. + TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the primary key. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + Task GetMinValueAsync(Expression> filter, Expression> minValueSelector) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. /// /// The document type. /// The type of the primary key. /// The type of the value used to order the query. /// A LINQ expression filter. /// A property selector to order by ascending. - /// An optional partition key. /// An optional cancellation Token. Task GetMinValueAsync( Expression> filter, Expression> minValueSelector, - string partitionKey = null, - CancellationToken cancellationToken = default) + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. /// /// The document type. /// The type of the primary key. @@ -367,7 +1643,90 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partition key. - TValue GetMinValue(Expression> filter, Expression> minValueSelector, string partitionKey = null) + Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the primary key. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional partition key. + /// An optional cancellation token. + Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the primary key. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + TValue GetMinValue(Expression> filter, Expression> minValueSelector) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the primary key. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional cancellation token. + TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the primary key. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional partition key. + TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the primary key. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional partition key. + /// An optional cancellation token. + TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -376,60 +1735,139 @@ namespace MongoDbGenericRepository #region Sum /// - /// Sums the values of a selected field for a given filtered collection of documents. + /// Sums the values of a selected field for a given filtered collection of documents. + /// + /// The type representing a Document. + /// The type of the primary key. + /// A LINQ expression filter. + /// The field you want to sum. + Task SumByAsync(Expression> filter, Expression> selector) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Sums the values of a selected field for a given filtered collection of documents. /// /// The type representing a Document. /// The type of the primary key. /// A LINQ expression filter. /// The field you want to sum. - /// The partition key of your document, if any. /// An optional cancellation Token. - Task SumByAsync(Expression> filter, - Expression> selector, - string partitionKey = null, - CancellationToken cancellationToken = default) + Task SumByAsync( + Expression> filter, + Expression> selector, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Sums the values of a selected field for a given filtered collection of documents. + /// Sums the values of a selected field for a given filtered collection of documents. /// /// The type representing a Document. /// The type of the primary key. /// A LINQ expression filter. /// The field you want to sum. /// The partition key of your document, if any. - int SumBy(Expression> filter, - Expression> selector, - string partitionKey = null) + Task SumByAsync(Expression> filter, Expression> selector, string partitionKey) where TDocument : IDocument where TKey : IEquatable; /// - /// Sums the values of a selected field for a given filtered collection of documents. + /// Sums the values of a selected field for a given filtered collection of documents. /// /// The type representing a Document. /// The type of the primary key. /// A LINQ expression filter. /// The field you want to sum. /// The partition key of your document, if any. - Task SumByAsync(Expression> filter, - Expression> selector, - string partitionKey = null) + /// An optional cancellation token. + Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Sums the values of a selected field for a given filtered collection of documents. + /// Sums the values of a selected field for a given filtered collection of documents. /// /// The type representing a Document. /// The type of the primary key. /// A LINQ expression filter. /// The field you want to sum. /// The partition key of your document, if any. - decimal SumBy(Expression> filter, - Expression> selector, - string partitionKey = null) + int SumBy(Expression> filter, Expression> selector, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Sums the values of a selected field for a given filtered collection of documents. + /// + /// The type representing a Document. + /// The type of the primary key. + /// A LINQ expression filter. + /// The field you want to sum. + Task SumByAsync(Expression> filter, Expression> selector) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Sums the values of a selected field for a given filtered collection of documents. + /// + /// The type representing a Document. + /// The type of the primary key. + /// A LINQ expression filter. + /// The field you want to sum. + /// An optional cancellation token. + Task SumByAsync( + Expression> filter, + Expression> selector, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Sums the values of a selected field for a given filtered collection of documents. + /// + /// The type representing a Document. + /// The type of the primary key. + /// A LINQ expression filter. + /// The field you want to sum. + /// The partition key of your document, if any. + Task SumByAsync(Expression> filter, Expression> selector, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Sums the values of a selected field for a given filtered collection of documents. + /// + /// The type representing a Document. + /// The type of the primary key. + /// A LINQ expression filter. + /// The field you want to sum. + /// The partition key of your document, if any. + /// An optional cancellation token. + Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Sums the values of a selected field for a given filtered collection of documents. + /// + /// The type representing a Document. + /// The type of the primary key. + /// A LINQ expression filter. + /// The field you want to sum. + /// The partition key of your document, if any. + decimal SumBy( + Expression> filter, + Expression> selector, + string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; @@ -438,40 +1876,39 @@ namespace MongoDbGenericRepository #region Project TKey /// - /// Asynchronously returns a projected document matching the filter condition. + /// Asynchronously returns a projected document matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + Task ProjectOneAsync( + Expression> filter, + Expression> projection) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Asynchronously returns a projected document matching the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. /// The type representing the model you want to project to. /// A LINQ expression filter. /// The projection expression. - /// An optional partition key. /// An optional cancellation Token. Task ProjectOneAsync( Expression> filter, Expression> projection, - string partitionKey = null, - CancellationToken cancellationToken = default) + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable where TProjection : class; /// - /// Returns a projected document matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type representing the model you want to project to. - /// - /// The projection expression. - /// An optional partition key. - TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - where TProjection : class; - - /// - /// Asynchronously returns a list of projected documents matching the filter condition. + /// Asynchronously returns a projected document matching the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -479,18 +1916,65 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - /// An optional cancellation Token. - Task> ProjectManyAsync( + Task ProjectOneAsync( Expression> filter, Expression> projection, - string partitionKey = null, - CancellationToken cancellationToken = default) + string partitionKey) where TDocument : IDocument where TKey : IEquatable where TProjection : class; /// - /// Asynchronously returns a list of projected documents matching the filter condition. + /// Asynchronously returns a projected document matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + /// An optional partition key. + /// An optional cancellation token. + Task ProjectOneAsync( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Returns a projected document matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// + /// The projection expression. + TProjection ProjectOne(Expression> filter, Expression> projection) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Returns a projected document matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// + /// The projection expression. + /// An optional cancellation token. + TProjection ProjectOne( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Returns a projected document matching the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -498,7 +1982,165 @@ namespace MongoDbGenericRepository /// /// The projection expression. /// An optional partition key. - List ProjectMany(Expression> filter, Expression> projection, string partitionKey = null) + TProjection ProjectOne( + Expression> filter, + Expression> projection, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Returns a projected document matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// + /// The projection expression. + /// An optional partition key. + /// An optional cancellation token. + TProjection ProjectOne( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + Task> ProjectManyAsync( + Expression> filter, + Expression> projection) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + /// An optional cancellation Token. + Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + /// An optional partition key. + Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + /// An optional partition key. + /// An optional cancellation token. + Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// + /// The projection expression. + List ProjectMany( + Expression> filter, + Expression> projection) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// + /// The projection expression. + /// An optional cancellation token. + List ProjectMany( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// + /// The projection expression. + /// An optional partition key. + List ProjectMany( + Expression> filter, + Expression> projection, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// + /// The projection expression. + /// An optional partition key. + /// An optional cancellation token. + List ProjectMany( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -508,8 +2150,44 @@ namespace MongoDbGenericRepository #region Group By /// - /// Groups filtered a collection of documents given a grouping criteria, - /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// Groups filtered a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// + /// The type representing a Document. + /// The type of the grouping criteria. + /// The type of the projected group. + /// The type of the primary key. + /// The grouping criteria. + /// The projected group result. + List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new(); + + /// + /// Groups filtered a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// + /// The type representing a Document. + /// The type of the grouping criteria. + /// The type of the projected group. + /// The type of the primary key. + /// The grouping criteria. + /// The projected group result. + /// An optional cancellation token. + List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new(); + + /// + /// Groups filtered a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. /// /// The type representing a Document. /// The type of the grouping criteria. @@ -521,14 +2199,75 @@ namespace MongoDbGenericRepository List GroupBy( Expression> groupingCriteria, Expression, TProjection>> groupProjection, - string partitionKey = null) + string partitionKey) where TDocument : IDocument where TKey : IEquatable where TProjection : class, new(); /// - /// Groups filtered a collection of documents given a grouping criteria, - /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// Groups filtered a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// + /// The type representing a Document. + /// The type of the grouping criteria. + /// The type of the projected group. + /// The type of the primary key. + /// The grouping criteria. + /// The projected group result. + /// The partition key of your document, if any. + /// An optional cancellation token. + List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new(); + + /// + /// Groups filtered a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// + /// The type representing a Document. + /// The type of the grouping criteria. + /// The type of the projected group. + /// The type of the primary key. + /// A LINQ expression filter. + /// The grouping criteria. + /// The projected group result. + List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new(); + + /// + /// Groups filtered a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// + /// The type representing a Document. + /// The type of the grouping criteria. + /// The type of the projected group. + /// The type of the primary key. + /// A LINQ expression filter. + /// The grouping criteria. + /// The projected group result. + /// An optional cancellation token. + List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new(); + + /// + /// Groups filtered a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. /// /// The type representing a Document. /// The type of the grouping criteria. @@ -542,17 +2281,40 @@ namespace MongoDbGenericRepository Expression> filter, Expression> groupingCriteria, Expression, TProjection>> groupProjection, - string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - where TProjection : class, new(); + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new(); + + /// + /// Groups filtered a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// + /// The type representing a Document. + /// The type of the grouping criteria. + /// The type of the projected group. + /// The type of the primary key. + /// A LINQ expression filter. + /// The grouping criteria. + /// The projected group result. + /// The partition key of your document, if any. + /// An optional cancellation token. + List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new(); #endregion Group By #region Pagination /// - /// Asynchronously returns a paginated list of the documents matching the filter condition. + /// Asynchronously returns a paginated list of the documents matching the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -562,18 +2324,20 @@ namespace MongoDbGenericRepository /// The number of documents you want to skip. Default value is 0. /// The number of documents you want to take. Default value is 50. /// An optional partition key. + /// An optional cancellation token. Task> GetSortedPaginatedAsync( Expression> filter, Expression> sortSelector, bool ascending = true, int skipNumber = 0, int takeNumber = 50, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; /// - /// Asynchronously returns a paginated list of the documents matching the filter condition. + /// Asynchronously returns a paginated list of the documents matching the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -595,4 +2359,4 @@ namespace MongoDbGenericRepository #endregion Pagination } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/Abstractions/IMongoDbContext.cs b/MongoDbGenericRepository/Abstractions/IMongoDbContext.cs index 2a05c8d..e3c70b5 100644 --- a/MongoDbGenericRepository/Abstractions/IMongoDbContext.cs +++ b/MongoDbGenericRepository/Abstractions/IMongoDbContext.cs @@ -1,6 +1,4 @@ using MongoDB.Driver; -using MongoDbGenericRepository.Models; -using System; namespace MongoDbGenericRepository { @@ -27,7 +25,7 @@ namespace MongoDbGenericRepository IMongoCollection GetCollection(string partitionKey = null); /// - /// Drops a collection having a partitionkey, use very carefully. + /// Drops a collection having a partitionKey, use very carefully. /// /// void DropCollection(string partitionKey = null); diff --git a/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs index a61374c..d518853 100644 --- a/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs +++ b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs @@ -9,185 +9,821 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { - public interface IReadOnlyMongoRepository where TKey : IEquatable + /// + /// read only repository interface + /// + /// The key type + public interface IReadOnlyMongoRepository + where TKey : IEquatable { #region Read /// - /// Asynchronously returns one document given its id. + /// Asynchronously returns one document given its id. + /// + /// The type representing a Document. + /// The Id of the document you want to get. + Task GetByIdAsync(TKey id) + where TDocument : IDocument; + + /// + /// Asynchronously returns one document given its id. + /// + /// The type representing a Document. + /// The Id of the document you want to get. + /// The cancellation token. + Task GetByIdAsync(TKey id, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Asynchronously returns one document given its id. /// /// The type representing a Document. /// The Id of the document you want to get. /// An optional partition key. - Task GetByIdAsync(TKey id, string partitionKey = null) where TDocument : IDocument; + Task GetByIdAsync(TKey id, string partitionKey) + where TDocument : IDocument; /// - /// Returns one document given its id. + /// Asynchronously returns one document given its id. /// /// The type representing a Document. /// The Id of the document you want to get. /// An optional partition key. - TDocument GetById(TKey id, string partitionKey = null) where TDocument : IDocument; + /// The cancellation token. + Task GetByIdAsync(TKey id, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; /// - /// Asynchronously returns one document given an expression filter. + /// Returns one document given its id. + /// + /// The type representing a Document. + /// The Id of the document you want to get. + TDocument GetById(TKey id) + where TDocument : IDocument; + + /// + /// Returns one document given its id. + /// + /// The type representing a Document. + /// The Id of the document you want to get. + /// The cancellation token. + TDocument GetById(TKey id, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Returns one document given its id. + /// + /// The type representing a Document. + /// The Id of the document you want to get. + /// An optional partition key. + TDocument GetById(TKey id, string partitionKey) + where TDocument : IDocument; + + /// + /// Returns one document given its id. + /// + /// The type representing a Document. + /// The Id of the document you want to get. + /// An optional partition key. + /// The cancellation token. + TDocument GetById(TKey id, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Asynchronously returns one document given an expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + Task GetOneAsync(Expression> filter) + where TDocument : IDocument; + + + /// + /// Asynchronously returns one document given an expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The cancellation token + Task GetOneAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Asynchronously returns one document given an expression filter. /// /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. - Task GetOneAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument; + Task GetOneAsync(Expression> filter, string partitionKey) + where TDocument : IDocument; /// - /// Returns one document given an expression filter. + /// Asynchronously returns one document given an expression filter. /// /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. - TDocument GetOne(Expression> filter, string partitionKey = null) where TDocument : IDocument; + /// The cancellation token + Task GetOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; /// - /// Returns a collection cursor. + /// Returns one document given an expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + TDocument GetOne(Expression> filter) + where TDocument : IDocument; + + /// + /// Returns one document given an expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The cancellation token + TDocument GetOne(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Returns one document given an expression filter. /// /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. - IFindFluent GetCursor(Expression> filter, string partitionKey = null) where TDocument : IDocument; + TDocument GetOne(Expression> filter, string partitionKey) + where TDocument : IDocument; /// - /// Asynchronously returns true if any of the document of the collection matches the filter condition. + /// Returns one document given an expression filter. /// /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. - Task AnyAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument; + /// The cancellation token + TDocument GetOne(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; /// - /// Returns true if any of the document of the collection matches the filter condition. + /// Returns a collection cursor. /// /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. - bool Any(Expression> filter, string partitionKey = null) where TDocument : IDocument; + IFindFluent GetCursor(Expression> filter, string partitionKey = null) + where TDocument : IDocument; /// - /// Asynchronously returns a list of the documents matching the filter condition. + /// Asynchronously returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + Task AnyAsync(Expression> filter) + where TDocument : IDocument; + + /// + /// Asynchronously returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The cancellation token. + Task AnyAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Asynchronously returns true if any of the document of the collection matches the filter condition. /// /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. - Task> GetAllAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument; + Task AnyAsync(Expression> filter, string partitionKey) + where TDocument : IDocument; /// - /// Returns a list of the documents matching the filter condition. + /// Asynchronously returns true if any of the document of the collection matches the filter condition. /// /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. - List GetAll(Expression> filter, string partitionKey = null) where TDocument : IDocument; + /// The cancellation token. + Task AnyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; /// - /// Asynchronously counts how many documents match the filter condition. + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + bool Any(Expression> filter) + where TDocument : IDocument; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The cancellation token + bool Any(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Returns true if any of the document of the collection matches the filter condition. /// /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. - Task CountAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument; + bool Any(Expression> filter, string partitionKey) + where TDocument : IDocument; /// - /// Counts how many documents match the filter condition. + /// Returns true if any of the document of the collection matches the filter condition. /// /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. - long Count(Expression> filter, string partitionKey = null) where TDocument : IDocument; + /// The cancellation token + bool Any(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + Task> GetAllAsync(Expression> filter) + where TDocument : IDocument; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The cancellation token + Task> GetAllAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + Task> GetAllAsync(Expression> filter, string partitionKey) + where TDocument : IDocument; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token + Task> GetAllAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + List GetAll(Expression> filter) + where TDocument : IDocument; + + /// + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The cancellation token. + List GetAll(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + List GetAll(Expression> filter, string partitionKey) + where TDocument : IDocument; + + /// + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token. + List GetAll(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + Task CountAsync(Expression> filter) + where TDocument : IDocument; + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The cancellation token. + Task CountAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + Task CountAsync(Expression> filter, string partitionKey) + where TDocument : IDocument; + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token. + Task CountAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + long Count(Expression> filter) + where TDocument : IDocument; + + /// + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The Cancellation token. + long Count(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + long Count(Expression> filter, string partitionKey) + where TDocument : IDocument; + + /// + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The Cancellation token. + long Count(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; #endregion #region Min / Max /// - /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter. + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// A LINQ expression filter. + /// A property selector to order by descending. + Task GetByMaxAsync(Expression> filter, Expression> orderByDescending) + where TDocument : IDocument; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// A LINQ expression filter. + /// A property selector to order by descending. + /// The cancellation token. + Task GetByMaxAsync( + Expression> filter, + Expression> orderByDescending, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. /// /// The document type. /// A LINQ expression filter. /// A property selector to order by descending. /// An optional partitionKey. - Task GetByMaxAsync(Expression> filter, Expression> orderByDescending, string partitionKey = null) + Task GetByMaxAsync( + Expression> filter, + Expression> orderByDescending, + string partitionKey) where TDocument : IDocument; /// - /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter. + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// A LINQ expression filter. + /// A property selector to order by descending. + /// An optional partitionKey. + /// The cancellation token. + Task GetByMaxAsync( + Expression> filter, + Expression> orderByDescending, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// A LINQ expression filter. + /// A property selector to order by descending. + /// + TDocument GetByMax(Expression> filter, Expression> orderByDescending) + where TDocument : IDocument; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// A LINQ expression filter. + /// A property selector to order by descending. + /// The cancellation token. + /// + TDocument GetByMax( + Expression> filter, + Expression> orderByDescending, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. /// /// The document type. /// A LINQ expression filter. /// A property selector to order by descending. /// An optional partitionKey. /// - TDocument GetByMax(Expression> filter, Expression> orderByDescending, string partitionKey = null) + TDocument GetByMax(Expression> filter, Expression> orderByDescending, string partitionKey) where TDocument : IDocument; /// - /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter. + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// A LINQ expression filter. + /// A property selector to order by descending. + /// An optional partitionKey. + /// The cancellation token. + /// + TDocument GetByMax( + Expression> filter, + Expression> orderByDescending, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// A LINQ expression filter. + /// A property selector to order by ascending. + Task GetByMinAsync(Expression> filter, Expression> orderByAscending) + where TDocument : IDocument; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// The cancellation token. + Task GetByMinAsync( + Expression> filter, + Expression> orderByAscending, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. /// /// The document type. /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partitionKey. - Task GetByMinAsync(Expression> filter, Expression> orderByAscending, string partitionKey = null) + Task GetByMinAsync( + Expression> filter, + Expression> orderByAscending, + string partitionKey) where TDocument : IDocument; /// - /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter. + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. /// /// The document type. /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partitionKey. - TDocument GetByMin(Expression> filter, Expression> orderByAscending, string partitionKey = null) + /// The cancellation token. + Task GetByMinAsync( + Expression> filter, + Expression> orderByAscending, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument; /// - /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// A LINQ expression filter. + /// A property selector to order by ascending. + TDocument GetByMin(Expression> filter, Expression> orderByAscending) + where TDocument : IDocument; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// The cancellation token. + TDocument GetByMin( + Expression> filter, + Expression> orderByAscending, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional partitionKey. + TDocument GetByMin(Expression> filter, Expression> orderByAscending, string partitionKey) + where TDocument : IDocument; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional partitionKey. + /// The cancellation token. + TDocument GetByMin( + Expression> filter, + Expression> orderByAscending, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + Task GetMaxValueAsync(Expression> filter, Expression> maxValueSelector) + where TDocument : IDocument; + + /// + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// The cancellation token. + Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. /// /// The document type. /// The type of the value used to order the query. /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partitionKey. - Task GetMaxValueAsync(Expression> filter, Expression> maxValueSelector, string partitionKey = null) + Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) where TDocument : IDocument; /// - /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. /// /// The document type. /// The type of the value used to order the query. /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partitionKey. - TValue GetMaxValue(Expression> filter, Expression> maxValueSelector, string partitionKey = null) + /// The cancellation token. + Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument; /// - /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + TValue GetMaxValue(Expression> filter, Expression> maxValueSelector) + where TDocument : IDocument; + + /// + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// The cancellation token. + TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument; + + + /// + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional partitionKey. + TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) + where TDocument : IDocument; + + /// + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional partitionKey. + /// The cancellation token. + TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + Task GetMinValueAsync(Expression> filter, Expression> minValueSelector) + where TDocument : IDocument; + + + /// + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// The cancellation token. + Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument; + + + /// + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. /// /// The document type. /// The type of the value used to order the query. /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partition key. - Task GetMinValueAsync(Expression> filter, Expression> minValueSelector, string partitionKey = null) + Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey) where TDocument : IDocument; /// - /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. /// /// The document type. /// The type of the value used to order the query. /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partition key. - TValue GetMinValue(Expression> filter, Expression> minValueSelector, string partitionKey = null) + /// The cancellation token. + Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + TValue GetMinValue(Expression> filter, Expression> minValueSelector) + where TDocument : IDocument; + + /// + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// The cancellation token. + TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument; + + + /// + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional partition key. + TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + string partitionKey) + where TDocument : IDocument; + + /// + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional partition key. + /// The cancellation token. + TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument; #endregion @@ -195,51 +831,123 @@ namespace MongoDbGenericRepository #region Maths /// - /// Sums the values of a selected field for a given filtered collection of documents. + /// Sums the values of a selected field for a given filtered collection of documents. /// /// The type representing a Document. /// A LINQ expression filter. /// The field you want to sum. - /// The partition key of your document, if any. - Task SumByAsync(Expression> filter, - Expression> selector, - string partitionKey = null) + Task SumByAsync(Expression> filter, Expression> selector) where TDocument : IDocument; /// - /// Sums the values of a selected field for a given filtered collection of documents. + /// Sums the values of a selected field for a given filtered collection of documents. /// /// The type representing a Document. /// A LINQ expression filter. /// The field you want to sum. - /// The partition key of your document, if any. - Task SumByAsync(Expression> filter, - Expression> selector, - string partitionKey = null) + /// The cancellation token. + Task SumByAsync( + Expression> filter, + Expression> selector, + CancellationToken cancellationToken) where TDocument : IDocument; /// - /// Sums the values of a selected field for a given filtered collection of documents. + /// Sums the values of a selected field for a given filtered collection of documents. /// /// The type representing a Document. /// A LINQ expression filter. /// The field you want to sum. /// The partition key of your document, if any. - int SumBy(Expression> filter, - Expression> selector, - string partitionKey = null) + Task SumByAsync(Expression> filter, Expression> selector, string partitionKey) where TDocument : IDocument; /// - /// Sums the values of a selected field for a given filtered collection of documents. + /// Sums the values of a selected field for a given filtered collection of documents. /// /// The type representing a Document. /// A LINQ expression filter. /// The field you want to sum. /// The partition key of your document, if any. - decimal SumBy(Expression> filter, - Expression> selector, - string partitionKey = null) + /// The cancellation token. + Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Sums the values of a selected field for a given filtered collection of documents. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The field you want to sum. + Task SumByAsync(Expression> filter, Expression> selector) + where TDocument : IDocument; + + /// + /// Sums the values of a selected field for a given filtered collection of documents. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The field you want to sum. + /// The cancellation token. + Task SumByAsync( + Expression> filter, + Expression> selector, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Sums the values of a selected field for a given filtered collection of documents. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The field you want to sum. + /// The partition key of your document, if any. + Task SumByAsync(Expression> filter, Expression> selector, string partitionKey) + where TDocument : IDocument; + + /// + /// Sums the values of a selected field for a given filtered collection of documents. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The field you want to sum. + /// The partition key of your document, if any. + /// The cancellation token. + Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Sums the values of a selected field for a given filtered collection of documents. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The field you want to sum. + /// The partition key of your document, if any. + int SumBy( + Expression> filter, + Expression> selector, + string partitionKey = null) + where TDocument : IDocument; + + /// + /// Sums the values of a selected field for a given filtered collection of documents. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The field you want to sum. + /// The partition key of your document, if any. + decimal SumBy( + Expression> filter, + Expression> selector, + string partitionKey = null) where TDocument : IDocument; #endregion Maths @@ -247,51 +955,236 @@ namespace MongoDbGenericRepository #region Project /// - /// Asynchronously returns a projected document matching the filter condition. + /// Asynchronously returns a projected document matching the filter condition. /// /// The type representing a Document. - /// The type of the primary key for a Document. /// The type representing the model you want to project to. /// A LINQ expression filter. /// The projection expression. - /// An optional partition key. - Task ProjectOneAsync(Expression> filter, Expression> projection, string partitionKey = null) + Task ProjectOneAsync(Expression> filter, Expression> projection) where TDocument : IDocument where TProjection : class; /// - /// Returns a projected document matching the filter condition. + /// Asynchronously returns a projected document matching the filter condition. /// /// The type representing a Document. /// The type representing the model you want to project to. /// A LINQ expression filter. /// The projection expression. - /// An optional partition key. - TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey = null) + /// The cancellation token + Task ProjectOneAsync( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) where TDocument : IDocument where TProjection : class; /// - /// Asynchronously returns a list of projected documents matching the filter condition. + /// Asynchronously returns a projected document matching the filter condition. /// /// The type representing a Document. /// The type representing the model you want to project to. /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - Task> ProjectManyAsync(Expression> filter, Expression> projection, string partitionKey = null) + Task ProjectOneAsync( + Expression> filter, + Expression> projection, + string partitionKey) where TDocument : IDocument where TProjection : class; /// - /// Asynchronously returns a list of projected documents matching the filter condition. + /// Asynchronously returns a projected document matching the filter condition. /// /// The type representing a Document. /// The type representing the model you want to project to. /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - List ProjectMany(Expression> filter, Expression> projection, string partitionKey = null) + /// The cancellation token + Task ProjectOneAsync( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class; + + /// + /// Returns a projected document matching the filter condition. + /// + /// The type representing a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + TProjection ProjectOne(Expression> filter, Expression> projection) + where TDocument : IDocument + where TProjection : class; + + /// + /// Returns a projected document matching the filter condition. + /// + /// The type representing a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + /// The cancellation token. + TProjection ProjectOne( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class; + + /// + /// Returns a projected document matching the filter condition. + /// + /// The type representing a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + /// An optional partition key. + TProjection ProjectOne( + Expression> filter, + Expression> projection, + string partitionKey) + where TDocument : IDocument + where TProjection : class; + + /// + /// Returns a projected document matching the filter condition. + /// + /// The type representing a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + /// An optional partition key. + /// The cancellation token. + TProjection ProjectOne( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + Task> ProjectManyAsync( + Expression> filter, + Expression> projection) + where TDocument : IDocument + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + /// The cancellation token. + Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + /// An optional partition key. + Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + string partitionKey) + where TDocument : IDocument + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + /// An optional partition key. + /// The cancellation token. + Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + List ProjectMany(Expression> filter, Expression> projection) + where TDocument : IDocument + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + /// The cancellation token + List ProjectMany( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + /// An optional partition key. + List ProjectMany( + Expression> filter, + Expression> projection, + string partitionKey) + where TDocument : IDocument + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + /// An optional partition key. + /// The cancellation token + List ProjectMany( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TProjection : class; @@ -300,8 +1193,40 @@ namespace MongoDbGenericRepository #region Group By /// - /// Groups a collection of documents given a grouping criteria, - /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// Groups a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// + /// The type representing a Document. + /// The type of the grouping criteria. + /// The type of the projected group. + /// The grouping criteria. + /// The projected group result. + List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection) + where TDocument : IDocument + where TProjection : class, new(); + + /// + /// Groups a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// + /// The type representing a Document. + /// The type of the grouping criteria. + /// The type of the projected group. + /// The grouping criteria. + /// The projected group result. + /// The cancellation token. + List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class, new(); + + /// + /// Groups a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. /// /// The type representing a Document. /// The type of the grouping criteria. @@ -312,13 +1237,68 @@ namespace MongoDbGenericRepository List GroupBy( Expression> groupingCriteria, Expression, TProjection>> groupProjection, - string partitionKey = null) + string partitionKey) where TDocument : IDocument where TProjection : class, new(); /// - /// Groups filtered a collection of documents given a grouping criteria, - /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// Groups a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// + /// The type representing a Document. + /// The type of the grouping criteria. + /// The type of the projected group. + /// The grouping criteria. + /// The projected group result. + /// The partition key of your document, if any. + /// The cancellation token. + List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class, new(); + + /// + /// Groups filtered a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// + /// The type representing a Document. + /// The type of the grouping criteria. + /// The type of the projected group. + /// A LINQ expression filter. + /// The grouping criteria. + /// The projected group result. + List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection) + where TDocument : IDocument + where TProjection : class, new(); + + /// + /// Groups filtered a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// + /// The type representing a Document. + /// The type of the grouping criteria. + /// The type of the projected group. + /// A LINQ expression filter. + /// The grouping criteria. + /// The projected group result. + /// The cancellation token. + List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class, new(); + + /// + /// Groups filtered a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. /// /// The type representing a Document. /// The type of the grouping criteria. @@ -331,50 +1311,73 @@ namespace MongoDbGenericRepository Expression> filter, Expression> groupingCriteria, Expression, TProjection>> groupProjection, - string partitionKey = null) - where TDocument : IDocument - where TProjection : class, new(); + string partitionKey) + where TDocument : IDocument + where TProjection : class, new(); + + /// + /// Groups filtered a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// + /// The type representing a Document. + /// The type of the grouping criteria. + /// The type of the projected group. + /// A LINQ expression filter. + /// The grouping criteria. + /// The projected group result. + /// The partition key of your document, if any. + /// The cancellation token. + List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class, new(); #endregion Group By #region Pagination /// - /// Asynchronously returns a paginated list of the documents matching the filter condition. + /// Asynchronously returns a paginated list of the documents matching the filter condition. /// /// The type representing a Document. - /// The type of the primary key for a Document. /// A LINQ expression filter. /// The property selector. /// Order of the sorting. /// The number of documents you want to skip. Default value is 0. /// The number of documents you want to take. Default value is 50. /// An optional partition key. + /// An optional cancellation token. Task> GetSortedPaginatedAsync( Expression> filter, Expression> sortSelector, bool ascending = true, int skipNumber = 0, int takeNumber = 50, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument; /// - /// Asynchronously returns a paginated list of the documents matching the filter condition. + /// Asynchronously returns a paginated list of the documents matching the filter condition. /// /// The type representing a Document. - /// The type of the primary key for a Document. /// A LINQ expression filter. /// The sort definition. /// The number of documents you want to skip. Default value is 0. /// The number of documents you want to take. Default value is 50. /// An optional partition key. + /// An optional cancellation token. Task> GetSortedPaginatedAsync( Expression> filter, SortDefinition sortDefinition, int skipNumber = 0, int takeNumber = 50, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument; #endregion Pagination diff --git a/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.cs b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.cs index 1dad5fb..0585cc3 100644 --- a/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.cs +++ b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.cs @@ -7,6 +7,5 @@ namespace MongoDbGenericRepository /// public interface IReadOnlyMongoRepository : IBaseReadOnlyRepository, IReadOnlyMongoRepository { - } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/BaseMongoRepository.Create.cs b/MongoDbGenericRepository/BaseMongoRepository.Create.cs index 70d20e5..5b2446f 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Create.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Create.cs @@ -1,82 +1,32 @@ -using MongoDbGenericRepository.DataAccess.Create; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The IBaseMongoRepository_Create interface to expose document creation functionality - /// with document having an Id of type Guid. - /// - public interface IBaseMongoRepository_Create : IBaseMongoRepository_Create - { - /// - /// Asynchronously adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to add. - /// An optional cancellation Token. - Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to add. - void AddOne(TDocument document) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Asynchronously adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The documents you want to add. - /// An optional cancellation Token. - Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The documents you want to add. - void AddMany(IEnumerable documents) - where TDocument : IDocument - where TKey : IEquatable; - } - - /// - /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. - /// Its constructor must be given a connection string and a database name. + /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. + /// Its constructor must be given a connection string and a database name. /// public abstract partial class BaseMongoRepository : IBaseMongoRepository_Create { private readonly object _initLock = new object(); - private MongoDbCreator _mongoDbCreator; + private IMongoDbCreator _mongoDbCreator; /// - /// The MongoDbCreator field. + /// The MongoDbCreator field. /// - protected virtual MongoDbCreator MongoDbCreator + protected virtual IMongoDbCreator MongoDbCreator { get { - if (_mongoDbCreator != null) { return _mongoDbCreator; } + if (_mongoDbCreator != null) + { + return _mongoDbCreator; + } lock (_initLock) { @@ -88,115 +38,127 @@ namespace MongoDbGenericRepository return _mongoDbCreator; } - set { _mongoDbCreator = value; } + set => _mongoDbCreator = value; } - /// - /// Asynchronously adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to add. - /// An optional cancellation Token. - public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) + /// + public virtual async Task AddOneAsync(TDocument document) + where TDocument : IDocument + where TKey : IEquatable + { + await AddOneAsync(document, CancellationToken.None); + } + + /// + public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { await MongoDbCreator.AddOneAsync(document, cancellationToken); } - /// - /// Asynchronously adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The document you want to add. - /// An optional cancellation Token. - public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) + /// + public virtual async Task AddOneAsync(TDocument document) where TDocument : IDocument { - await MongoDbCreator.AddOneAsync(document, cancellationToken); + await AddOneAsync(document, CancellationToken.None); } - /// - /// Adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to add. + /// + public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + { + await AddOneAsync(document, cancellationToken); + } + + /// public virtual void AddOne(TDocument document) where TDocument : IDocument where TKey : IEquatable { - MongoDbCreator.AddOne(document); + AddOne(document, CancellationToken.None); } - /// - /// Adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The document you want to add. - public virtual void AddOne(TDocument document) where TDocument : IDocument + /// + public virtual void AddOne(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable { - MongoDbCreator.AddOne(document); + MongoDbCreator.AddOne(document, cancellationToken); } - /// - /// Asynchronously adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The documents you want to add. - /// An optional cancellation Token. - public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) + /// + public virtual void AddOne(TDocument document) + where TDocument : IDocument + { + AddOne(document, CancellationToken.None); + } + + /// + public virtual void AddOne(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + { + AddOne(document, cancellationToken); + } + + /// + public virtual async Task AddManyAsync(IEnumerable documents) + where TDocument : IDocument + where TKey : IEquatable + { + await AddManyAsync(documents, CancellationToken.None); + } + + /// + public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { await MongoDbCreator.AddManyAsync(documents, cancellationToken); } - /// - /// Asynchronously adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The documents you want to add. - /// An optional cancellation Token. - public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) + /// + public virtual async Task AddManyAsync(IEnumerable documents) where TDocument : IDocument { - await MongoDbCreator.AddManyAsync(documents, cancellationToken); + await AddManyAsync(documents, CancellationToken.None); } - /// - /// Adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The documents you want to add. + /// + public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + { + await AddManyAsync(documents, cancellationToken); + } + + /// public virtual void AddMany(IEnumerable documents) where TDocument : IDocument where TKey : IEquatable { - MongoDbCreator.AddMany(documents); + AddMany(documents, CancellationToken.None); } - /// - /// Adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The documents you want to add. - public virtual void AddMany(IEnumerable documents) + /// + public virtual void AddMany(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + MongoDbCreator.AddMany(documents, cancellationToken); + } + + /// + public virtual void AddMany(IEnumerable documents) where TDocument : IDocument { - MongoDbCreator.AddMany(documents); + AddMany(documents, CancellationToken.None); + } + + /// + public virtual void AddMany(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + { + AddMany(documents, cancellationToken); } } - -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs index e86b060..692c10f 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs @@ -1,115 +1,28 @@ -using MongoDbGenericRepository.DataAccess.Delete; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; +using MongoDbGenericRepository.DataAccess.Delete; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { - public interface IBaseMongoRepository_Delete : IBaseMongoRepository_Delete - { - /// - /// Deletes a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to delete. - /// The number of documents deleted. - long DeleteOne(TDocument document) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to delete. - /// The number of documents deleted. - Task DeleteOneAsync(TDocument document) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - long DeleteOne(Expression> filter, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - Task DeleteOneAsync(Expression> filter, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - Task DeleteManyAsync(Expression> filter, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Asynchronously deletes a list of documents. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The list of documents to delete. - /// The number of documents deleted. - Task DeleteManyAsync(IEnumerable documents) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Deletes a list of documents. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The list of documents to delete. - /// The number of documents deleted. - long DeleteMany(IEnumerable documents) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - long DeleteMany(Expression> filter, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - } - public abstract partial class BaseMongoRepository : IBaseMongoRepository_Delete { - private MongoDbEraser _mongoDbEraser; - protected virtual MongoDbEraser MongoDbEraser + private IMongoDbEraser _mongoDbEraser; + + /// + /// The MongoDbEraser used to delete documents. + /// + protected virtual IMongoDbEraser MongoDbEraser { get { - if (_mongoDbEraser != null) { return _mongoDbEraser; } + if (_mongoDbEraser != null) + { + return _mongoDbEraser; + } lock (_initLock) { @@ -121,224 +34,384 @@ namespace MongoDbGenericRepository return _mongoDbEraser; } - set { _mongoDbEraser = value; } + set => _mongoDbEraser = value; } #region Delete - /// - /// Asynchronously deletes a document. - /// - /// The type representing a Document. - /// The document you want to delete. - /// The number of documents deleted. - public virtual async Task DeleteOneAsync(TDocument document) where TDocument : IDocument + /// + public virtual long DeleteOne(TDocument document) + where TDocument : IDocument { - return await MongoDbEraser.DeleteOneAsync(document); + return DeleteOne(document, CancellationToken.None); } - /// - /// Deletes a document. - /// - /// The type representing a Document. - /// The document you want to delete. - /// The number of documents deleted. - public virtual long DeleteOne(TDocument document) where TDocument : IDocument + /// + public virtual long DeleteOne(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument { - return MongoDbEraser.DeleteOne(document); + return DeleteOne(document, cancellationToken); } - /// - /// Deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual long DeleteOne(Expression> filter, string partitionKey = null) where TDocument : IDocument + /// + public virtual long DeleteOne(Expression> filter) + where TDocument : IDocument { - return MongoDbEraser.DeleteOne(filter, partitionKey); + return DeleteOne(filter, null, CancellationToken.None); } - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument + /// + public virtual long DeleteOne(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument { - return await MongoDbEraser.DeleteOneAsync(filter, partitionKey); + return DeleteOne(filter, null, cancellationToken); } - /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument + /// + public virtual long DeleteOne(Expression> filter, string partitionKey) + where TDocument : IDocument { - return await MongoDbEraser.DeleteManyAsync(filter, partitionKey); + return DeleteOne(filter, partitionKey, CancellationToken.None); } - /// - /// Asynchronously deletes a list of documents. - /// - /// The type representing a Document. - /// The list of documents to delete. - /// The number of documents deleted. - public virtual async Task DeleteManyAsync(IEnumerable documents) where TDocument : IDocument + /// + public virtual long DeleteOne(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return DeleteOne(filter, partitionKey, cancellationToken); + } + + /// + public virtual async Task DeleteOneAsync(TDocument document) + where TDocument : IDocument + { + return await DeleteOneAsync(document, CancellationToken.None); + } + + /// + public virtual async Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await DeleteOneAsync(document, cancellationToken); + } + + /// + public virtual async Task DeleteOneAsync(Expression> filter) + where TDocument : IDocument + { + return await DeleteOneAsync(filter, null, CancellationToken.None); + } + + /// + public virtual async Task DeleteOneAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await DeleteOneAsync(filter, null, cancellationToken); + } + + /// + public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return await DeleteOneAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task DeleteOneAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await DeleteOneAsync(filter, partitionKey, cancellationToken); + } + + /// + public async Task DeleteManyAsync(Expression> filter) + where TDocument : IDocument + { + return await DeleteManyAsync(filter, null, CancellationToken.None); + } + + /// + public async Task DeleteManyAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await DeleteManyAsync(filter, null, cancellationToken); + } + + /// + public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return await DeleteManyAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public async Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await DeleteManyAsync(filter, partitionKey, cancellationToken); + } + + /// + public virtual async Task DeleteManyAsync(IEnumerable documents) + where TDocument : IDocument { return await DeleteManyAsync(documents); } - /// - /// Deletes a list of documents. - /// - /// The type representing a Document. - /// The list of documents to delete. - /// The number of documents deleted. - public virtual long DeleteMany(IEnumerable documents) where TDocument : IDocument + /// + public virtual async Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument { - return DeleteMany(documents); + return await DeleteManyAsync(documents, cancellationToken); } - /// - /// Deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual long DeleteMany(Expression> filter, string partitionKey = null) where TDocument : IDocument + /// + public virtual long DeleteMany(IEnumerable documents) + where TDocument : IDocument { - return MongoDbEraser.DeleteMany(filter, partitionKey); + return DeleteMany(documents, CancellationToken.None); + } + + /// + public virtual long DeleteMany(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + { + return DeleteMany(documents, cancellationToken); + } + + /// + public virtual long DeleteMany(Expression> filter) + where TDocument : IDocument + { + return DeleteMany(filter, null, CancellationToken.None); + } + + /// + public virtual long DeleteMany(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + { + return DeleteMany(filter, null, cancellationToken); + } + + /// + public virtual long DeleteMany(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return DeleteMany(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual long DeleteMany(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return DeleteMany(filter, partitionKey, cancellationToken); } #endregion Delete #region Delete TKey - /// - /// Deletes a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to delete. - /// The number of documents deleted. + /// public virtual long DeleteOne(TDocument document) where TDocument : IDocument where TKey : IEquatable { - return MongoDbEraser.DeleteOne(document); + return DeleteOne(document, CancellationToken.None); } - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to delete. - /// The number of documents deleted. + /// + public virtual long DeleteOne(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbEraser.DeleteOne(document, cancellationToken); + } + + /// + public virtual long DeleteOne(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable + { + return DeleteOne(filter, null, CancellationToken.None); + } + + /// + public virtual long DeleteOne(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return DeleteOne(filter, null, cancellationToken); + } + + /// + public virtual long DeleteOne(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return DeleteOne(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual long DeleteOne(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbEraser.DeleteOne(filter, partitionKey, cancellationToken); + } + + /// public virtual async Task DeleteOneAsync(TDocument document) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbEraser.DeleteOneAsync(document); + return await DeleteOneAsync(document, CancellationToken.None); } - /// - /// Deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual long DeleteOne(Expression> filter, string partitionKey = null) + /// + public virtual async Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return MongoDbEraser.DeleteOne(filter, partitionKey); + return await MongoDbEraser.DeleteOneAsync(document, cancellationToken); } - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey = null) + /// + public virtual async Task DeleteOneAsync(Expression> filter) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbEraser.DeleteOneAsync(filter, partitionKey); + return await DeleteOneAsync(filter, null, CancellationToken.None); } - /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey = null) + /// + public virtual async Task DeleteOneAsync(Expression> filter, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbEraser.DeleteManyAsync(filter, partitionKey); + return await DeleteOneAsync(filter, null, cancellationToken); } - /// - /// Asynchronously deletes a list of documents. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The list of documents to delete. - /// The number of documents deleted. + /// + public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await DeleteOneAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task DeleteOneAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbEraser.DeleteOneAsync(filter, partitionKey, cancellationToken); + } + + /// + public virtual async Task DeleteManyAsync(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable + { + return await DeleteManyAsync(filter, null, CancellationToken.None); + } + + /// + public virtual async Task DeleteManyAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await DeleteManyAsync(filter, null, cancellationToken); + } + + /// + public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await DeleteManyAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task DeleteManyAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbEraser.DeleteManyAsync(filter, partitionKey, cancellationToken); + } + + /// public virtual async Task DeleteManyAsync(IEnumerable documents) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbEraser.DeleteManyAsync(documents); + return await DeleteManyAsync(documents, CancellationToken.None); } - /// - /// Deletes a list of documents. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The list of documents to delete. - /// The number of documents deleted. + /// + public virtual async Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbEraser.DeleteManyAsync(documents, cancellationToken); + } + + /// public virtual long DeleteMany(IEnumerable documents) where TDocument : IDocument where TKey : IEquatable { - return MongoDbEraser.DeleteMany(documents); + return DeleteMany(documents, CancellationToken.None); } - /// - /// Deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual long DeleteMany(Expression> filter, string partitionKey = null) + /// + public virtual long DeleteMany(IEnumerable documents, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return MongoDbEraser.DeleteMany(filter, partitionKey); + return MongoDbEraser.DeleteMany(documents, cancellationToken); + } + + /// + public virtual long DeleteMany(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable + { + return DeleteMany(filter, null, CancellationToken.None); + } + + /// + public virtual long DeleteMany(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return DeleteMany(filter, null, cancellationToken); + } + + /// + public virtual long DeleteMany(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return DeleteMany(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual long DeleteMany(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbEraser.DeleteMany(filter, partitionKey, cancellationToken); } #endregion - } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/BaseMongoRepository.Index.cs b/MongoDbGenericRepository/BaseMongoRepository.Index.cs index fa97458..b56b60f 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Index.cs @@ -1,123 +1,32 @@ -using MongoDbGenericRepository.DataAccess.Index; -using System; +using System; using System.Collections.Generic; -using System.Threading.Tasks; using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using MongoDbGenericRepository.DataAccess.Index; using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { - public interface IBaseMongoRepository_Index : IBaseMongoRepository_Index - { - /// - /// Returns the names of the indexes present on a collection. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// An optional partition key - /// A list containing the names of the indexes on on the concerned collection. - Task> GetIndexesNamesAsync(string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Creates an index on the given field in ascending order. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Creates an index on the given field in descending order. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Creates a hashed index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Creates a combined text index. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The fields we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Drops the index given a field name - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The name of the index - /// An optional partition key - Task DropIndexAsync(string indexName, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - } /// - /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. - /// Its constructor must be given a connection string and a database name. + /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. + /// Its constructor must be given a connection string and a database name. /// public abstract partial class BaseMongoRepository : IBaseMongoRepository_Index { - private MongoDbIndexHandler _mongoDbIndexHandler; - protected virtual MongoDbIndexHandler MongoDbIndexHandler + private IMongoDbIndexHandler _mongoDbIndexHandler; + + /// + /// The MongoDb accessor to manage indexes. + /// + protected virtual IMongoDbIndexHandler MongoDbIndexHandler { get { - if (_mongoDbIndexHandler != null) { return _mongoDbIndexHandler; } + if (_mongoDbIndexHandler != null) + { + return _mongoDbIndexHandler; + } lock (_initLock) { @@ -126,195 +35,886 @@ namespace MongoDbGenericRepository _mongoDbIndexHandler = new MongoDbIndexHandler(MongoDbContext); } } + return _mongoDbIndexHandler; } - set { _mongoDbIndexHandler = value; } - } - - /// - /// Returns the names of the indexes present on a collection. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// An optional partition key - /// A list containing the names of the indexes on on the concerned collection. - public async Task> GetIndexesNamesAsync(string partitionKey = null) - where TDocument : IDocument - { - return await MongoDbIndexHandler.GetIndexesNamesAsync(partitionKey); - } - - /// - /// Returns the names of the indexes present on a collection. - /// - /// The type representing a Document. - /// An optional partition key - /// A list containing the names of the indexes on on the concerned collection. - public async virtual Task> GetIndexesNamesAsync(string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbIndexHandler.GetIndexesNamesAsync(partitionKey); - } - - /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument - { - return await MongoDbIndexHandler.CreateTextIndexAsync(field, indexCreationOptions, partitionKey); - } - - /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async virtual Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbIndexHandler.CreateTextIndexAsync(field, indexCreationOptions, partitionKey); - } - - /// - /// Creates an index on the given field in ascending order. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument - { - return await MongoDbIndexHandler.CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey); - } - - /// - /// Creates an index on the given field in ascending order. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async virtual Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbIndexHandler.CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey); - } - - /// - /// Creates an index on the given field in descending order. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument - { - return await MongoDbIndexHandler.CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey); + set => _mongoDbIndexHandler = value; } /// - public async virtual Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbIndexHandler.CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey); - } - - /// - /// Creates a hashed index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public async Task> GetIndexesNamesAsync() where TDocument : IDocument { - return await MongoDbIndexHandler.CreateHashedIndexAsync(field, indexCreationOptions, partitionKey); + return await GetIndexesNamesAsync(null, CancellationToken.None); + } + + + /// + public async Task> GetIndexesNamesAsync(CancellationToken cancellationToken) + where TDocument : IDocument + { + return await GetIndexesNamesAsync(null, cancellationToken); } /// - public async virtual Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbIndexHandler.CreateHashedIndexAsync(field, indexCreationOptions, partitionKey); - } - - /// - /// Creates a combined text index. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The fields we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public async Task> GetIndexesNamesAsync(string partitionKey) where TDocument : IDocument { - return await MongoDbIndexHandler.CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey); + return await GetIndexesNamesAsync(partitionKey); } /// - public async virtual Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbIndexHandler.CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey); - } - - /// - /// Drops the index given a field name - /// - /// The type representing a Document. - /// The name of the index - /// An optional partition key - public async Task DropIndexAsync(string indexName, string partitionKey = null) + public async Task> GetIndexesNamesAsync(string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument { - await MongoDbIndexHandler.DropIndexAsync(indexName, partitionKey); + return await GetIndexesNamesAsync(partitionKey, cancellationToken); } /// - public async virtual Task DropIndexAsync(string indexName, string partitionKey = null) + public virtual async Task> GetIndexesNamesAsync() where TDocument : IDocument where TKey : IEquatable { - await MongoDbIndexHandler.DropIndexAsync(indexName, partitionKey); + return await GetIndexesNamesAsync(null, CancellationToken.None); + } + + /// + public virtual async Task> GetIndexesNamesAsync(CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetIndexesNamesAsync(null, cancellationToken); + } + + /// + public virtual async Task> GetIndexesNamesAsync(string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetIndexesNamesAsync(partitionKey, CancellationToken.None); + } + + /// + public virtual async Task> GetIndexesNamesAsync(string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbIndexHandler.GetIndexesNamesAsync(partitionKey, cancellationToken); + } + + /// + public async Task CreateTextIndexAsync(Expression> field) + where TDocument : IDocument + { + return await CreateTextIndexAsync(field, null, null, CancellationToken.None); + } + + /// + public async Task CreateTextIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateTextIndexAsync(field, null, null, cancellationToken); + } + + /// + public async Task CreateTextIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + { + return await CreateTextIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public async Task CreateTextIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateTextIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + { + return await CreateTextIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + /// + public async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateTextIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + { + return await CreateTextIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateTextIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateTextIndexAsync(Expression> field) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateTextIndexAsync(field, null, null, CancellationToken.None); + } + + /// + public virtual async Task CreateTextIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateTextIndexAsync(field, null, null, cancellationToken); + } + + /// + public virtual async Task CreateTextIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateTextIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateTextIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateTextIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateTextIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + /// + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateTextIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateTextIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbIndexHandler.CreateTextIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public async Task CreateAscendingIndexAsync(Expression> field) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, null, null, CancellationToken.None); + } + + /// + public async Task CreateAscendingIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, null, null, cancellationToken); + } + + /// + public async Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + /// + public async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public async Task CreateAscendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public async Task CreateAscendingIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateAscendingIndexAsync(Expression> field) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateAscendingIndexAsync(field, null, null, CancellationToken.None); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateAscendingIndexAsync(field, null, null, cancellationToken); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public virtual async Task CreateAscendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateAscendingIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateAscendingIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbIndexHandler.CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public async Task CreateDescendingIndexAsync(Expression> field) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, null, null, CancellationToken.None); + } + + /// + public async Task CreateDescendingIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, null, null, cancellationToken); + } + + /// + public async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + /// + public async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public async Task CreateDescendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public async Task CreateDescendingIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync(Expression> field) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateDescendingIndexAsync(field, null, null, CancellationToken.None); + } + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateDescendingIndexAsync(field, null, null, cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateDescendingIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateDescendingIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbIndexHandler.CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public async Task CreateHashedIndexAsync(Expression> field) + where TDocument : IDocument + { + return await CreateHashedIndexAsync(field, null, null, CancellationToken.None); + } + + /// + public async Task CreateHashedIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateHashedIndexAsync(field, null, null, cancellationToken); + } + + /// + public async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + { + return await CreateHashedIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + /// + public async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateHashedIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public async Task CreateHashedIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + { + return await CreateHashedIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public async Task CreateHashedIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateHashedIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + { + return await CreateHashedIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateHashedIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateHashedIndexAsync(Expression> field) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateHashedIndexAsync(field, null, null, CancellationToken.None); + } + + /// + public virtual async Task CreateHashedIndexAsync( + Expression> field, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateHashedIndexAsync(field, null, null, cancellationToken); + } + + /// + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateHashedIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + /// + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateHashedIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public virtual async Task CreateHashedIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateHashedIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateHashedIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateHashedIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateHashedIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbIndexHandler.CreateHashedIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public async Task CreateCombinedTextIndexAsync(IEnumerable>> fields) + where TDocument : IDocument + { + return await CreateCombinedTextIndexAsync(fields, null, null, CancellationToken.None); + } + + /// + public async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateCombinedTextIndexAsync(fields, null, null, cancellationToken); + } + + /// + public async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + { + return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, null, CancellationToken.None); + } + + /// + public async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, null, cancellationToken); + } + + /// + public async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, string partitionKey) + where TDocument : IDocument + { + return await CreateCombinedTextIndexAsync(fields, null, partitionKey, CancellationToken.None); + } + + /// + public async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateCombinedTextIndexAsync(fields, null, partitionKey, cancellationToken); + } + + /// + public async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + { + return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateCombinedTextIndexAsync(fields, null, null, CancellationToken.None); + } + + /// + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateCombinedTextIndexAsync(fields, null, null, cancellationToken); + } + + /// + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, null, CancellationToken.None); + } + + /// + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, null, cancellationToken); + } + + /// + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateCombinedTextIndexAsync(fields, null, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateCombinedTextIndexAsync(fields, null, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbIndexHandler.CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public async Task DropIndexAsync(string indexName) + where TDocument : IDocument + { + await DropIndexAsync(indexName, null, CancellationToken.None); + } + + /// + public async Task DropIndexAsync(string indexName, CancellationToken cancellationToken) + where TDocument : IDocument + { + await DropIndexAsync(indexName, null, cancellationToken); + } + + /// + public async Task DropIndexAsync(string indexName, string partitionKey) + where TDocument : IDocument + { + await DropIndexAsync(indexName, partitionKey, CancellationToken.None); + } + + /// + public async Task DropIndexAsync(string indexName, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + await DropIndexAsync(indexName, partitionKey, cancellationToken); + } + + /// + public virtual async Task DropIndexAsync(string indexName) + where TDocument : IDocument + where TKey : IEquatable + { + await DropIndexAsync(indexName, null, CancellationToken.None); + } + + /// + public virtual async Task DropIndexAsync(string indexName, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + await DropIndexAsync(indexName, null, cancellationToken); + } + + /// + public virtual async Task DropIndexAsync(string indexName, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + await DropIndexAsync(indexName, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task DropIndexAsync(string indexName, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + await MongoDbIndexHandler.DropIndexAsync(indexName, partitionKey, cancellationToken); } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/BaseMongoRepository.Main.cs b/MongoDbGenericRepository/BaseMongoRepository.Main.cs index 09664e8..b382c23 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Main.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Main.cs @@ -1,21 +1,22 @@ -using MongoDB.Driver; -using MongoDbGenericRepository.Models; -using MongoDbGenericRepository.Utils; -using System; +using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.Models; +using MongoDbGenericRepository.Utils; namespace MongoDbGenericRepository { /// - /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. - /// Its constructor must be given a connection string and a database name. + /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. + /// Its constructor must be given a connection string and a database name. /// public abstract partial class BaseMongoRepository : ReadOnlyMongoRepository, IBaseMongoRepository { /// - /// The constructor taking a connection string and a database name. + /// The constructor taking a connection string and a database name. /// /// The connection string of the MongoDb server. /// The name of the database against which you want to perform operations. @@ -24,90 +25,48 @@ namespace MongoDbGenericRepository } /// - /// The contructor taking a . + /// The constructor taking a . /// - /// A mongodb context implementing + /// A mongodb context implementing protected BaseMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } /// - /// The contructor taking a . + /// The constructor taking a . /// - /// A mongodb context implementing + /// A mongodb context implementing protected BaseMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase) { } - /// - /// Asynchronously returns a paginated list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// The number of documents you want to skip. Default value is 0. - /// The number of documents you want to take. Default value is 50. - /// An optional partition key. + /// public virtual async Task> GetPaginatedAsync( Expression> filter, int skipNumber = 0, int takeNumber = 50, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument { - return await HandlePartitioned(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(); + return await HandlePartitioned(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(cancellationToken); } - /// - /// Asynchronously returns a paginated list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// The number of documents you want to skip. Default value is 0. - /// The number of documents you want to take. Default value is 50. - /// An optional partition key. - public virtual async Task> GetPaginatedAsync(Expression> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null) + /// + public virtual async Task> GetPaginatedAsync( + Expression> filter, + int skipNumber = 0, + int takeNumber = 50, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await HandlePartitioned(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(); - } - - #region Find And Update - - /// - /// GetAndUpdateOne with filter - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// - /// - /// - public virtual async Task GetAndUpdateOne(FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options) where TDocument : IDocument - { - return await GetCollection().FindOneAndUpdateAsync(filter, update, options); + return await HandlePartitioned(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(cancellationToken); } /// - /// GetAndUpdateOne with filter - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// - /// - /// - public virtual async Task GetAndUpdateOne(FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options) - where TDocument : IDocument - where TKey : IEquatable - { - return await GetCollection().FindOneAndUpdateAsync(filter, update, options); - } - - #endregion Find And Update - - /// - /// Sets the value of the document Id if it is not set already. + /// Sets the value of the document Id if it is not set already. /// /// The document type. /// The type of the primary key. @@ -120,6 +79,7 @@ namespace MongoDbGenericRepository { throw new ArgumentNullException(nameof(document)); } + var defaultTKey = default(TKey); if (document.Id == null || (defaultTKey != null @@ -130,24 +90,26 @@ namespace MongoDbGenericRepository } /// - /// Sets the value of the document Id if it is not set already. + /// Sets the value of the document Id if it is not set already. /// /// The document type. /// The document. - protected void FormatDocument(TDocument document) where TDocument : IDocument + protected void FormatDocument(TDocument document) + where TDocument : IDocument { if (document == null) { throw new ArgumentNullException(nameof(document)); } - if (document.Id == default(Guid)) + + if (document.Id == default) { document.Id = Guid.NewGuid(); } } /// - /// Gets a collections for a potentially partitioned document type. + /// Gets a collections for a potentially partitioned document type. /// /// The document type. /// The collection partition key. @@ -159,11 +121,12 @@ namespace MongoDbGenericRepository { return GetCollection(partitionKey); } + return GetCollection(); } /// - /// Gets a collections for the type TDocument with a partition key. + /// Gets a collections for the type TDocument with a partition key. /// /// The document type. /// The collection partition key. @@ -173,5 +136,53 @@ namespace MongoDbGenericRepository { return MongoDbContext.GetCollection(partitionKey); } + + #region Find And Update + + /// + public virtual async Task GetAndUpdateOne( + FilterDefinition filter, + UpdateDefinition update, + FindOneAndUpdateOptions options) + where TDocument : IDocument + { + return await GetAndUpdateOne(filter, update, options, CancellationToken.None); + } + + /// + public virtual async Task GetAndUpdateOne( + FilterDefinition filter, + UpdateDefinition update, + FindOneAndUpdateOptions options, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await GetCollection().FindOneAndUpdateAsync(filter, update, options, cancellationToken); + } + + /// + public virtual async Task GetAndUpdateOne( + FilterDefinition filter, + UpdateDefinition update, + FindOneAndUpdateOptions options) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetAndUpdateOne(filter, update, options, CancellationToken.None); + } + + /// + public virtual async Task GetAndUpdateOne( + FilterDefinition filter, + UpdateDefinition update, + FindOneAndUpdateOptions options, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetCollection().FindOneAndUpdateAsync(filter, update, options, cancellationToken); + } + + #endregion Find And Update } } \ No newline at end of file diff --git a/MongoDbGenericRepository/BaseMongoRepository.Update.ClientSession.cs b/MongoDbGenericRepository/BaseMongoRepository.Update.ClientSession.cs index aa661b0..ef4b774 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Update.ClientSession.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Update.ClientSession.cs @@ -1,197 +1,350 @@ -using MongoDB.Driver; -using MongoDbGenericRepository.DataAccess.Update; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Update; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { public abstract partial class BaseMongoRepository : IBaseMongoRepository_Update_ClientSession { - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The client session. - /// The document with the modifications you want to persist. - /// The optional cancellation token. - /// A boolean value indicating success. - public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument modifiedDocument) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateOneAsync(session, modifiedDocument, CancellationToken.None); + } + + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + TDocument modifiedDocument, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { return await MongoDbUpdater.UpdateOneAsync(session, modifiedDocument, cancellationToken); } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The client session. - /// The document with the modifications you want to persist. - /// The optional cancellation token. - /// A boolean value indicating success. - public virtual bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateOne(session, modifiedDocument, CancellationToken.None); + } + + /// + public virtual bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { return MongoDbUpdater.UpdateOne(session, modifiedDocument, cancellationToken); } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The client session. - /// The document to modify. - /// The update definition. - /// The optional cancellation token. - /// A boolean value indicating success. - public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + TDocument documentToModify, + UpdateDefinition update) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateOneAsync(session, documentToModify, update, CancellationToken.None); + } + + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { return await MongoDbUpdater.UpdateOneAsync(session, documentToModify, update, cancellationToken); } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The client session. - /// The document to modify. - /// The update definition. - /// The optional cancellation token. - /// A boolean value indicating success. - public virtual bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateOne(session, documentToModify, update, CancellationToken.None); + } + + /// + public virtual bool UpdateOne( + IClientSessionHandle session, + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { return MongoDbUpdater.UpdateOne(session, documentToModify, update, cancellationToken); } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field to update. - /// The client session. - /// The document to modify. - /// The field to update. - /// The value of the field. - /// The optional cancellation token. - /// A boolean value indicating success. - public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + TDocument documentToModify, + Expression> field, + TField value) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateOneAsync(session, documentToModify, field, value, CancellationToken.None); + } + + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { return await MongoDbUpdater.UpdateOneAsync(session, documentToModify, field, value, cancellationToken); } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field to update. - /// The client session. - /// The document to modify. - /// The field to update. - /// The value of the field. - /// The optional cancellation token. - /// A boolean value indicating success. - public virtual bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual bool UpdateOne( + IClientSessionHandle session, + TDocument documentToModify, + Expression> field, + TField value) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateOne(session, documentToModify, field, value, CancellationToken.None); + } + + /// + public virtual bool UpdateOne( + IClientSessionHandle session, + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { return MongoDbUpdater.UpdateOne(session, documentToModify, field, value, cancellationToken); } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field to update. - /// The client session. - /// The filter for the update. - /// The field to update. - /// The value of the field. - /// The optional partition key. - /// The optional cancellation token. - /// A boolean value indicating success. - public virtual async Task UpdateOneAsync(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateOneAsync(session, filter, field, value, null, CancellationToken.None); + } + + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateOneAsync(session, filter, field, value, null, cancellationToken); + } + + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateOneAsync(session, filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { return await MongoDbUpdater.UpdateOneAsync(session, filter, field, value, partitionKey, cancellationToken); } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field to update. - /// The client session. - /// The filter for the update. - /// The field to update. - /// The value of the field. - /// The optional partition key. - /// The optional cancellation token. - /// A boolean value indicating success. - public virtual async Task UpdateOneAsync(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateOneAsync(session, filter, field, value, null, CancellationToken.None); + } + + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateOneAsync(session, filter, field, value, null, cancellationToken); + } + + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateOneAsync(session, filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { return await MongoDbUpdater.UpdateOneAsync(session, filter, field, value, partitionKey, cancellationToken); } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field to update. - /// The client session. - /// The filter for the update. - /// The field to update. - /// The value of the field. - /// The optional partition key. - /// The optional cancellation token. - /// A boolean value indicating success. - public virtual bool UpdateOne(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual bool UpdateOne( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateOne(session, filter, field, value, null, CancellationToken.None); + } + + + /// + public virtual bool UpdateOne( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateOne(session, filter, field, value, null, cancellationToken); + } + + /// + public virtual bool UpdateOne( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateOne(session, filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual bool UpdateOne( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { return MongoDbUpdater.UpdateOne(session, filter, field, value, partitionKey, cancellationToken); } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field to update. - /// The client session. - /// The filter for the update. - /// The field to update. - /// The value of the field. - /// The optional partition key. - /// The optional cancellation token. - /// A boolean value indicating success. - public virtual bool UpdateOne(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual bool UpdateOne( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateOne(session, Builders.Filter.Where(filter), field, value, null, CancellationToken.None); + } + + /// + public virtual bool UpdateOne( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateOne(session, Builders.Filter.Where(filter), field, value, null, cancellationToken); + } + + /// + public virtual bool UpdateOne( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateOne(session, Builders.Filter.Where(filter), field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual bool UpdateOne( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { return UpdateOne(session, Builders.Filter.Where(filter), field, value, partitionKey, cancellationToken); } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/BaseMongoRepository.Update.cs b/MongoDbGenericRepository/BaseMongoRepository.Update.cs index 82306b1..9b22fd7 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Update.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Update.cs @@ -1,24 +1,32 @@ -using MongoDB.Driver; +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using MongoDB.Driver; using MongoDbGenericRepository.DataAccess.Update; using MongoDbGenericRepository.Models; -using System; -using System.Linq.Expressions; -using System.Threading.Tasks; namespace MongoDbGenericRepository { /// - /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. - /// Its constructor must be given a connection string and a database name. + /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. + /// Its constructor must be given a connection string and a database name. /// public abstract partial class BaseMongoRepository : IBaseMongoRepository_Update { - private MongoDbUpdater _mongoDbUpdater; - protected virtual MongoDbUpdater MongoDbUpdater + private IMongoDbUpdater _mongoDbUpdater; + + /// + /// The MongoDb accessor to update data. + /// + protected virtual IMongoDbUpdater MongoDbUpdater { get { - if (_mongoDbUpdater != null) { return _mongoDbUpdater; } + if (_mongoDbUpdater != null) + { + return _mongoDbUpdater; + } lock (_initLock) { @@ -30,544 +38,1255 @@ namespace MongoDbGenericRepository return _mongoDbUpdater; } - set { _mongoDbUpdater = value; } + set => _mongoDbUpdater = value; } #region Update - /// - /// Asynchronously Updates a document. - /// - /// The type representing a Document. - /// The document with the modifications you want to persist. - public virtual async Task UpdateOneAsync(TDocument modifiedDocument) where TDocument : IDocument + /// + public virtual async Task UpdateOneAsync(TDocument modifiedDocument) + where TDocument : IDocument { - return await MongoDbUpdater.UpdateOneAsync(modifiedDocument); + return await UpdateOneAsync(modifiedDocument, CancellationToken.None); } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The document with the modifications you want to persist. - public virtual bool UpdateOne(TDocument modifiedDocument) where TDocument : IDocument + /// + public virtual async Task UpdateOneAsync(TDocument modifiedDocument, CancellationToken cancellationToken) + where TDocument : IDocument { - return MongoDbUpdater.UpdateOne(modifiedDocument); + return await MongoDbUpdater.UpdateOneAsync(modifiedDocument, cancellationToken); } - /// - /// Takes a document you want to modify and applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document you want to modify. - /// The update definition for the document. + /// + public virtual bool UpdateOne(TDocument modifiedDocument) + where TDocument : IDocument + { + return UpdateOne(modifiedDocument, CancellationToken.None); + } + + /// + public virtual bool UpdateOne(TDocument modifiedDocument, CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateOne(modifiedDocument, cancellationToken); + } + + /// public virtual async Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update) where TDocument : IDocument { - return await MongoDbUpdater.UpdateOneAsync(documentToModify, update); - + return await UpdateOneAsync(documentToModify, update, CancellationToken.None); } - /// - /// Takes a document you want to modify and applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document you want to modify. - /// The update definition for the document. + /// + public virtual async Task UpdateOneAsync( + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateOneAsync(documentToModify, update, cancellationToken); + } + + /// public virtual bool UpdateOne(TDocument documentToModify, UpdateDefinition update) where TDocument : IDocument { - return MongoDbUpdater.UpdateOne(documentToModify, update); + return UpdateOne(documentToModify, update, CancellationToken.None); } - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the field. - /// The document you want to modify. - /// The field selector. - /// The new value of the property field. + /// + public virtual bool UpdateOne(TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateOne(documentToModify, update, cancellationToken); + } + + /// public virtual bool UpdateOne(TDocument documentToModify, Expression> field, TField value) where TDocument : IDocument { - return MongoDbUpdater.UpdateOne(documentToModify, field, value); + return UpdateOne(documentToModify, field, value, CancellationToken.None); } - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the field. - /// The document you want to modify. - /// The field selector. - /// The new value of the property field. + /// + public virtual bool UpdateOne( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateOne(documentToModify, field, value, cancellationToken); + } + + /// public virtual async Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value) where TDocument : IDocument { - return await MongoDbUpdater.UpdateOneAsync(documentToModify, field, value); + return await UpdateOneAsync(documentToModify, field, value, CancellationToken.None); } - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual async Task UpdateOneAsync( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey); + return await MongoDbUpdater.UpdateOneAsync(documentToModify, field, value, cancellationToken); } - /// - /// For the entity selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value) where TDocument : IDocument { - return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey); + return UpdateOne(filter, field, value, null, CancellationToken.None); } - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey); + return UpdateOne(filter, field, value, null, cancellationToken); } - /// - /// For the entity selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - public virtual async Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument { - return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey); + return UpdateOne(filter, field, value, partitionKey, CancellationToken.None); } - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual async Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey); + return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey, cancellationToken); } - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual async Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne(Expression> filter, Expression> field, TField value) where TDocument : IDocument { - return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey); + return UpdateOne(filter, field, value, null, CancellationToken.None); } - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + /// + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey); + return UpdateOne(filter, field, value, null, cancellationToken); } - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - public async Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) + /// + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument { - return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey); + return UpdateOne(filter, field, value, partitionKey, CancellationToken.None); } - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey); + return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey, cancellationToken); } - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value) where TDocument : IDocument { - return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey); + return await UpdateOneAsync(filter, field, value, null, CancellationToken.None); } - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + /// + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey); + return await UpdateOneAsync(filter, field, value, null, cancellationToken); } - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - public long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) + /// + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument { - return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey); + return await UpdateOneAsync(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual async Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value) + where TDocument : IDocument + { + return await UpdateOneAsync(filter, field, value, null, CancellationToken.None); + } + + /// + public virtual async Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await UpdateOneAsync(filter, field, value, null, cancellationToken); + } + + /// + public virtual async Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + { + return await UpdateOneAsync(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, field, value, null, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, field, value, null, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, field, value, null, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, field, value, null, cancellationToken); + } + + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, updateDefinition, null, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, updateDefinition, null, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, updateDefinition, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey, cancellationToken); + } + + /// + public async Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, updateDefinition, null, CancellationToken.None); + } + + /// + public async Task UpdateManyAsync( + Expression> filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, updateDefinition, null, cancellationToken); + } + + /// + public async Task UpdateManyAsync( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, updateDefinition, partitionKey, CancellationToken.None); + } + + /// + public async Task UpdateManyAsync( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey, cancellationToken); + } + + /// + public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value) + where TDocument : IDocument + { + return UpdateMany(filter, field, value, null, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return UpdateMany(filter, field, value, null, cancellationToken); + } + + /// + public virtual long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + { + return UpdateMany(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual long UpdateMany(Expression> filter, Expression> field, TField value) + where TDocument : IDocument + { + return UpdateMany(filter, field, value, null, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return UpdateMany(filter, field, value, null, cancellationToken); + } + + /// + public virtual long UpdateMany( + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + { + return UpdateMany(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition) + where TDocument : IDocument + { + return UpdateMany(filter, updateDefinition, null, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + FilterDefinition filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return UpdateMany(filter, updateDefinition, null, cancellationToken); + } + + /// + public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey) + where TDocument : IDocument + { + return UpdateMany(filter, updateDefinition, partitionKey, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey, cancellationToken); + } + + /// + public long UpdateMany(Expression> filter, UpdateDefinition updateDefinition) + where TDocument : IDocument + { + return UpdateMany(filter, updateDefinition, null, CancellationToken.None); + } + + /// + public long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return UpdateMany(filter, updateDefinition, null, cancellationToken); + } + + /// + public long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey) + where TDocument : IDocument + { + return UpdateMany(filter, updateDefinition, partitionKey, CancellationToken.None); + } + + /// + public long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey, cancellationToken); } #endregion Update #region Update TKey - /// - /// Asynchronously Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document with the modifications you want to persist. + /// public virtual async Task UpdateOneAsync(TDocument modifiedDocument) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbUpdater.UpdateOneAsync(modifiedDocument); + return await UpdateOneAsync(modifiedDocument, CancellationToken.None); } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document with the modifications you want to persist. + /// + public virtual async Task UpdateOneAsync(TDocument modifiedDocument, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbUpdater.UpdateOneAsync(modifiedDocument, cancellationToken); + } + + /// public virtual bool UpdateOne(TDocument modifiedDocument) where TDocument : IDocument where TKey : IEquatable { - return MongoDbUpdater.UpdateOne(modifiedDocument); + return UpdateOne(modifiedDocument, CancellationToken.None); } - /// - /// Takes a document you want to modify and applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to modify. - /// The update definition for the document. + /// + public virtual bool UpdateOne(TDocument modifiedDocument, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbUpdater.UpdateOne(modifiedDocument, cancellationToken); + } + + /// public virtual async Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbUpdater.UpdateOneAsync(documentToModify, update); + return await UpdateOneAsync(documentToModify, update, CancellationToken.None); } - /// - /// Takes a document you want to modify and applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to modify. - /// The update definition for the document. + /// + public virtual async Task UpdateOneAsync( + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbUpdater.UpdateOneAsync(documentToModify, update, cancellationToken); + } + + /// public virtual bool UpdateOne(TDocument documentToModify, UpdateDefinition update) where TDocument : IDocument where TKey : IEquatable { - return MongoDbUpdater.UpdateOne(documentToModify, update); + return UpdateOne(documentToModify, update, CancellationToken.None); } - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document you want to modify. - /// The field selector. - /// The new value of the property field. + /// + public virtual bool UpdateOne(TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbUpdater.UpdateOne(documentToModify, update, cancellationToken); + } + + /// public virtual bool UpdateOne(TDocument documentToModify, Expression> field, TField value) where TDocument : IDocument where TKey : IEquatable { - return MongoDbUpdater.UpdateOne(documentToModify, field, value); + return UpdateOne(documentToModify, field, value, CancellationToken.None); } - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document you want to modify. - /// The field selector. - /// The new value of the property field. - public virtual async Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value) + /// + public virtual bool UpdateOne( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbUpdater.UpdateOneAsync(documentToModify, field, value); + return MongoDbUpdater.UpdateOne(documentToModify, field, value, cancellationToken); } - /// - /// Updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual async Task UpdateOneAsync( + TDocument documentToModify, + Expression> field, + TField value) where TDocument : IDocument where TKey : IEquatable { - return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey); + return await UpdateOneAsync(documentToModify, field, value, CancellationToken.None); } - /// - /// For the entity selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual async Task UpdateOneAsync( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey); + return await MongoDbUpdater.UpdateOneAsync(documentToModify, field, value, cancellationToken); } - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey); + return UpdateOne(filter, field, value, null, CancellationToken.None); } - /// - /// For the entity selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - public virtual async Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbUpdater.UpdateOneAsync(Builders.Filter.Where(filter), field, value, partitionKey); + return UpdateOne(filter, field, value, null, cancellationToken); } - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - public virtual async Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey); + return UpdateOne(filter, field, value, partitionKey, CancellationToken.None); } - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual async Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey); + return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey, cancellationToken); } - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + /// + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey); + return UpdateOne(filter, field, value, null, CancellationToken.None); } - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - public virtual async Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) + /// + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey); + return UpdateOne(filter, field, value, null, cancellationToken); } - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - public virtual long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { - return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey); + return UpdateOne(filter, field, value, partitionKey, CancellationToken.None); } - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey); + return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey, cancellationToken); } - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - public virtual long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) + /// + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value) where TDocument : IDocument where TKey : IEquatable { - return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey); + return await UpdateOneAsync(filter, field, value, null, CancellationToken.None); } - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + /// + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey); + return await UpdateOneAsync(filter, field, value, null, cancellationToken); + } + + /// + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateOneAsync(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual async Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateOneAsync(Builders.Filter.Where(filter), field, value, null, CancellationToken.None); + } + + /// + public virtual async Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateOneAsync(Builders.Filter.Where(filter), field, value, null, cancellationToken); + } + + /// + public virtual async Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateOneAsync(Builders.Filter.Where(filter), field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbUpdater.UpdateOneAsync( + Builders.Filter.Where(filter), + field, + value, + partitionKey, + cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateManyAsync(filter, field, value, null, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateManyAsync(filter, field, value, null, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateManyAsync(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateManyAsync(filter, field, value, null, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateManyAsync(filter, field, value, null, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateManyAsync(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateManyAsync(filter, updateDefinition, null, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateManyAsync(filter, updateDefinition, null, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateManyAsync(filter, updateDefinition, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateManyAsync(filter, updateDefinition, null, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateManyAsync(filter, updateDefinition, null, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateManyAsync(filter, updateDefinition, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey, cancellationToken); + } + + /// + public virtual long UpdateMany( + Expression> filter, + Expression> field, + TField value) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateMany(filter, field, value, null, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateMany(filter, field, value, null, cancellationToken); + } + + /// + public virtual long UpdateMany( + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateMany(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateMany(filter, field, value, null, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateMany(filter, field, value, null, cancellationToken); + } + + /// + public virtual long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateMany(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual long UpdateMany(Expression> filter, UpdateDefinition updateDefinition) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateMany(filter, updateDefinition, null, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateMany(filter, updateDefinition, null, cancellationToken); + } + + /// + public virtual long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateMany(filter, updateDefinition, partitionKey, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey, cancellationToken); + } + + /// + public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateMany(filter, updateDefinition, null, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + FilterDefinition filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateMany(filter, updateDefinition, null, cancellationToken); + } + + /// + public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateMany(filter, updateDefinition, partitionKey, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey, cancellationToken); } #endregion Update } - -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs b/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs index eded8d5..3e55c59 100644 --- a/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs +++ b/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs @@ -2,7 +2,6 @@ using MongoDB.Driver.Linq; using MongoDbGenericRepository.Models; using System; -using System.Linq; using System.Linq.Expressions; namespace MongoDbGenericRepository.DataAccess.Base @@ -10,7 +9,7 @@ namespace MongoDbGenericRepository.DataAccess.Base /// /// A base class for accessing the Database and its Collections. /// - public class DataAccessBase + public class DataAccessBase : IDataAccessBase { /// /// The MongoDbContext @@ -169,7 +168,6 @@ namespace MongoDbGenericRepository.DataAccess.Base .Limit(1); } - #endregion } } diff --git a/MongoDbGenericRepository/DataAccess/Base/IDataAccessBase.cs b/MongoDbGenericRepository/DataAccess/Base/IDataAccessBase.cs new file mode 100644 index 0000000..af0fb43 --- /dev/null +++ b/MongoDbGenericRepository/DataAccess/Base/IDataAccessBase.cs @@ -0,0 +1,59 @@ +using System; +using System.Linq.Expressions; +using MongoDB.Driver; +using MongoDB.Driver.Linq; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository.DataAccess.Base +{ + /// + /// A interface for accessing the Database and its Collections. + /// + public interface IDataAccessBase + { + /// + /// Gets a IMongoQueryable for a potentially partitioned document type and a filter. + /// + /// The document type. + /// The type of the primary key. + /// The filter definition. + /// The collection partition key. + /// + IMongoQueryable GetQuery(Expression> filter, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets a collections for a potentially partitioned document type. + /// + /// The document type. + /// The type of the primary key. + /// The document. + /// + IMongoCollection HandlePartitioned(TDocument document) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets a collections for a potentially partitioned document type. + /// + /// The document type. + /// The type of the primary key. + /// The collection partition key. + /// + IMongoCollection HandlePartitioned(string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets a collections for the type TDocument with a partition key. + /// + /// The document type. + /// The type of the primary key. + /// The collection partition key. + /// + IMongoCollection GetCollection(string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/DataAccess/Create/IMongoDbCreator.cs b/MongoDbGenericRepository/DataAccess/Create/IMongoDbCreator.cs new file mode 100644 index 0000000..b90bfea --- /dev/null +++ b/MongoDbGenericRepository/DataAccess/Create/IMongoDbCreator.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using MongoDbGenericRepository.DataAccess.Base; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository.DataAccess.Create +{ + /// + /// A interface for adding documents to the Database and its Collections. + /// + public interface IMongoDbCreator : IDataAccessBase + { + /// + /// Asynchronously adds a document to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to add. + /// An optional cancellation Token. + Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Adds a document to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to add. + /// An optional cancellation Token. + void AddOne(TDocument document, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously adds a list of documents to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The documents you want to add. + /// An optional cancellation Token. + Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Adds a list of documents to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The documents you want to add. + /// An optional cancellation Token. + void AddMany(IEnumerable documents, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs b/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs index 7847030..b09b505 100644 --- a/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs +++ b/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs @@ -1,133 +1,111 @@ -using MongoDB.Driver; -using MongoDB.Driver.Linq; -using MongoDbGenericRepository.DataAccess.Base; -using MongoDbGenericRepository.Models; -using MongoDbGenericRepository.Utils; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using MongoDbGenericRepository.DataAccess.Base; +using MongoDbGenericRepository.Models; +using MongoDbGenericRepository.Utils; namespace MongoDbGenericRepository.DataAccess.Create { /// - /// A class to insert MongoDb document. + /// A class to insert MongoDb document. /// - public class MongoDbCreator : DataAccessBase + public class MongoDbCreator : DataAccessBase, IMongoDbCreator { /// - /// The construct of the MongoDbCreator class. + /// The construct of the MongoDbCreator class. /// - /// A instance. + /// A instance. public MongoDbCreator(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } - #region Create TKey - - /// - /// Asynchronously adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to add. - /// An optional cancellation Token. + /// public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { FormatDocument(document); - await HandlePartitioned(document).InsertOneAsync(document, null, cancellationToken); + await HandlePartitioned(document) + .InsertOneAsync(document, null, cancellationToken); } - /// - /// Adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to add. - public virtual void AddOne(TDocument document) + /// + public virtual void AddOne(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { FormatDocument(document); - HandlePartitioned(document).InsertOne(document); + HandlePartitioned(document) + .InsertOne(document, null, cancellationToken); } - /// - /// Asynchronously adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The documents you want to add. - /// An optional cancellation Token. + /// public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - if (!documents.Any()) + var documentsList = documents.ToList(); + + if (!documentsList.Any()) { return; } - foreach (var document in documents) + + foreach (var document in documentsList) { FormatDocument(document); } + // cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5 - if (documents.Any(e => e is IPartitionedDocument)) + if (documentsList.Any(e => e is IPartitionedDocument)) { - foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey)) + foreach (var group in documentsList.GroupBy(e => ((IPartitionedDocument) e).PartitionKey)) { await HandlePartitioned(group.FirstOrDefault()).InsertManyAsync(group.ToList(), null, cancellationToken); } } else { - await GetCollection().InsertManyAsync(documents.ToList(), null, cancellationToken); + await GetCollection().InsertManyAsync(documentsList, null, cancellationToken); } } - /// - /// Adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The documents you want to add. - public virtual void AddMany(IEnumerable documents) + /// + public virtual void AddMany(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - if (!documents.Any()) + var documentList = documents.ToList(); + + if (!documentList.Any()) { return; } - foreach (var document in documents) + + foreach (var document in documentList) { FormatDocument(document); } + // cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5 - if (documents.Any(e => e is IPartitionedDocument)) + if (documentList.Any(e => e is IPartitionedDocument)) { - foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey)) + foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument) e).PartitionKey)) { - HandlePartitioned(group.FirstOrDefault()).InsertMany(group.ToList()); + HandlePartitioned(group.FirstOrDefault()).InsertMany(group.ToList(), cancellationToken: cancellationToken); } } else { - GetCollection().InsertMany(documents.ToList()); + GetCollection().InsertMany(documentList, cancellationToken: cancellationToken); } } - #endregion - /// - /// Sets the value of the document Id if it is not set already. + /// Sets the value of the document Id if it is not set already. /// /// The document type. /// The type of the primary key. @@ -140,6 +118,7 @@ namespace MongoDbGenericRepository.DataAccess.Create { throw new ArgumentNullException(nameof(document)); } + var defaultTKey = default(TKey); if (document.Id == null || (defaultTKey != null @@ -149,4 +128,4 @@ namespace MongoDbGenericRepository.DataAccess.Create } } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs b/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs new file mode 100644 index 0000000..c1fc2f5 --- /dev/null +++ b/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using MongoDbGenericRepository.DataAccess.Base; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository.DataAccess.Delete +{ + /// + /// The MongoDbEraser interface. used to delete documents from the collections. + /// + public interface IMongoDbEraser : IDataAccessBase + { + /// + /// Deletes a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to delete. + /// An optional cancellation token + /// The number of documents deleted. + long DeleteOne(TDocument document, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// An optional cancellation token + /// The number of documents deleted. + long DeleteOne(Expression> filter, string partitionKey = default, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to delete. + /// The cancellation token + /// The number of documents deleted. + Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token + /// The number of documents deleted. + Task DeleteOneAsync(Expression> filter, string partitionKey = default, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token + /// The number of documents deleted. + Task DeleteManyAsync(Expression> filter, string partitionKey = default, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes a list of documents. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The list of documents to delete. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Deletes a list of documents. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The list of documents to delete. + /// The cancellation token + /// The number of documents deleted. + long DeleteMany(IEnumerable documents, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The Cancellation Token + /// The number of documents deleted. + long DeleteMany(Expression> filter, string partitionKey = default, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs b/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs index 1daabac..27ff63a 100644 --- a/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs +++ b/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs @@ -1,180 +1,145 @@ -using MongoDB.Driver; -using MongoDB.Driver.Linq; -using MongoDbGenericRepository.DataAccess.Base; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Base; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository.DataAccess.Delete { - public class MongoDbEraser : DataAccessBase + /// + public class MongoDbEraser : DataAccessBase, IMongoDbEraser { + /// + /// The MongoDbEraser constructor. + /// + /// the MongoDb Context public MongoDbEraser(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } #region Delete TKey - /// - /// Deletes a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to delete. - /// The number of documents deleted. - public virtual long DeleteOne(TDocument document) + /// + public virtual long DeleteOne(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", document.Id); - return HandlePartitioned(document).DeleteOne(filter).DeletedCount; + return HandlePartitioned(document).DeleteOne(filter, cancellationToken).DeletedCount; } - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to delete. - /// The number of documents deleted. - public virtual async Task DeleteOneAsync(TDocument document) + /// + public virtual long DeleteOne(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return HandlePartitioned(partitionKey).DeleteOne(filter, cancellationToken).DeletedCount; + } + + /// + public virtual async Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", document.Id); - return (await HandlePartitioned(document).DeleteOneAsync(filter)).DeletedCount; + return (await HandlePartitioned(document).DeleteOneAsync(filter, cancellationToken)).DeletedCount; } - /// - /// Deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual long DeleteOne(Expression> filter, string partitionKey = null) + /// + public virtual async Task DeleteOneAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return HandlePartitioned(partitionKey).DeleteOne(filter).DeletedCount; + return (await HandlePartitioned(partitionKey).DeleteOneAsync(filter, cancellationToken)).DeletedCount; } - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey = null) + /// + public virtual async Task DeleteManyAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return (await HandlePartitioned(partitionKey).DeleteOneAsync(filter)).DeletedCount; + return (await HandlePartitioned(partitionKey).DeleteManyAsync(filter, cancellationToken)).DeletedCount; } - /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey = null) + /// + public virtual async Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return (await HandlePartitioned(partitionKey).DeleteManyAsync(filter)).DeletedCount; - } + var documentList = documents.ToList(); - /// - /// Asynchronously deletes a list of documents. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The list of documents to delete. - /// The number of documents deleted. - public virtual async Task DeleteManyAsync(IEnumerable documents) - where TDocument : IDocument - where TKey : IEquatable - { - if (!documents.Any()) + if (!documentList.Any()) { return 0; } + // cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5 - if (documents.Any(e => e is IPartitionedDocument)) + if (documentList.Any(e => e is IPartitionedDocument)) { long deleteCount = 0; - foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey)) + foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument) e).PartitionKey)) { - var groupIdsTodelete = group.Select(e => e.Id).ToArray(); - deleteCount += (await HandlePartitioned(group.FirstOrDefault()).DeleteManyAsync(x => groupIdsTodelete.Contains(x.Id))).DeletedCount; + var groupIdsToDelete = group.Select(e => e.Id).ToArray(); + deleteCount += (await HandlePartitioned(group.FirstOrDefault()) + .DeleteManyAsync(x => groupIdsToDelete.Contains(x.Id), cancellationToken)) + .DeletedCount; } + return deleteCount; } - else - { - var idsTodelete = documents.Select(e => e.Id).ToArray(); - return (await HandlePartitioned(documents.FirstOrDefault()).DeleteManyAsync(x => idsTodelete.Contains(x.Id))).DeletedCount; - } + + var idsToDelete = documentList.Select(e => e.Id).ToArray(); + return (await HandlePartitioned(documentList.FirstOrDefault()).DeleteManyAsync(x => idsToDelete.Contains(x.Id), cancellationToken)) + .DeletedCount; } - /// - /// Deletes a list of documents. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The list of documents to delete. - /// The number of documents deleted. - public virtual long DeleteMany(IEnumerable documents) + /// + public virtual long DeleteMany(IEnumerable documents, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - if (!documents.Any()) + var documentList = documents.ToList(); + + if (!documentList.Any()) { return 0; } + // cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5 - if (documents.Any(e => e is IPartitionedDocument)) + if (documentList.Any(e => e is IPartitionedDocument)) { long deleteCount = 0; - foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey)) + foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument) e).PartitionKey)) { - var groupIdsTodelete = group.Select(e => e.Id).ToArray(); - deleteCount += (HandlePartitioned(group.FirstOrDefault()).DeleteMany(x => groupIdsTodelete.Contains(x.Id))).DeletedCount; + var groupIdsToDelete = group.Select(e => e.Id).ToArray(); + deleteCount += HandlePartitioned(group.FirstOrDefault()).DeleteMany(x => groupIdsToDelete.Contains(x.Id), cancellationToken).DeletedCount; } + return deleteCount; } - else - { - var idsTodelete = documents.Select(e => e.Id).ToArray(); - return (HandlePartitioned(documents.FirstOrDefault()).DeleteMany(x => idsTodelete.Contains(x.Id))).DeletedCount; - } + + var idsToDelete = documentList.Select(e => e.Id).ToArray(); + return HandlePartitioned(documentList.FirstOrDefault()).DeleteMany(x => idsToDelete.Contains(x.Id), cancellationToken).DeletedCount; } - /// - /// Deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual long DeleteMany(Expression> filter, string partitionKey = null) + /// + public virtual long DeleteMany(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return HandlePartitioned(partitionKey).DeleteMany(filter).DeletedCount; + return HandlePartitioned(partitionKey).DeleteMany(filter, cancellationToken).DeletedCount; } #endregion - } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/DataAccess/Index/IMongoDbIndexHandler.cs b/MongoDbGenericRepository/DataAccess/Index/IMongoDbIndexHandler.cs new file mode 100644 index 0000000..9c79ab5 --- /dev/null +++ b/MongoDbGenericRepository/DataAccess/Index/IMongoDbIndexHandler.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using MongoDbGenericRepository.DataAccess.Base; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository.DataAccess.Index +{ + /// + /// The MongoDbIndexHandler interface. used to create indexes on collections. + /// + public interface IMongoDbIndexHandler : IDataAccessBase + { + /// + /// Returns the names of the indexes present on a collection. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// An optional partition key + /// An optional Cancellation Token. + /// A list containing the names of the indexes on on the concerned collection. + Task> GetIndexesNamesAsync(string partitionKey = null, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// An optional cancellation token + /// The result of the create index operation. + Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// An optional cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// An optional cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// An optional cancellation token. + /// The result of the create index operation. + Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The fields we want to index. + /// Options for creating an index. + /// An optional partition key. + /// An optional Cancellation token. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Drops the index given a field name + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The name of the index + /// An optional partition key + /// An optional cancellation token, + Task DropIndexAsync(string indexName, string partitionKey = null, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/DataAccess/Index/MongoDbIndexHandler.cs b/MongoDbGenericRepository/DataAccess/Index/MongoDbIndexHandler.cs index 6662670..0bfe00b 100644 --- a/MongoDbGenericRepository/DataAccess/Index/MongoDbIndexHandler.cs +++ b/MongoDbGenericRepository/DataAccess/Index/MongoDbIndexHandler.cs @@ -1,72 +1,80 @@ -using MongoDB.Driver; -using MongoDB.Driver.Linq; -using MongoDbGenericRepository.DataAccess.Base; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Base; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository.DataAccess.Index { - public class MongoDbIndexHandler : DataAccessBase + /// + public class MongoDbIndexHandler : DataAccessBase, IMongoDbIndexHandler { + /// + /// The MongoDbIndexHandler constructor. + /// + /// The mongo db context public MongoDbIndexHandler(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } - /// - /// Returns the names of the indexes present on a collection. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// An optional partition key - /// A list containing the names of the indexes on on the concerned collection. - public async virtual Task> GetIndexesNamesAsync(string partitionKey = null) + /// + public virtual async Task> GetIndexesNamesAsync(string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - var indexCursor = await HandlePartitioned(partitionKey).Indexes.ListAsync(); - var indexes = await indexCursor.ToListAsync(); + var indexCursor = await HandlePartitioned(partitionKey).Indexes.ListAsync(cancellationToken); + var indexes = await indexCursor.ToListAsync(cancellationToken); return indexes.Select(e => e["name"].ToString()).ToList(); } - /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async virtual Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + /// + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { + var model = new CreateIndexModel( + Builders.IndexKeys.Text(field), + indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions) + ); + return await HandlePartitioned(partitionKey).Indexes - .CreateOneAsync( - new CreateIndexModel( - Builders.IndexKeys.Text(field), - indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions) - )); + .CreateOneAsync( + model, + cancellationToken: cancellationToken); } - /// - /// Creates an index on the given field in ascending order. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async virtual Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + { + var collection = HandlePartitioned(partitionKey); + var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions); + var indexKey = Builders.IndexKeys; + return await + collection.Indexes.CreateOneAsync( + new CreateIndexModel(indexKey.Ascending(field), createOptions), + cancellationToken: cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { @@ -74,22 +82,17 @@ namespace MongoDbGenericRepository.DataAccess.Index var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions); var indexKey = Builders.IndexKeys; return await collection.Indexes - .CreateOneAsync( - new CreateIndexModel(indexKey.Ascending(field), createOptions)); + .CreateOneAsync( + new CreateIndexModel(indexKey.Descending(field), createOptions), + cancellationToken: cancellationToken); } - /// - /// Creates an index on the given field in descending order. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async virtual Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + /// + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { @@ -97,45 +100,17 @@ namespace MongoDbGenericRepository.DataAccess.Index var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions); var indexKey = Builders.IndexKeys; return await collection.Indexes - .CreateOneAsync( - new CreateIndexModel(indexKey.Descending(field), createOptions)); + .CreateOneAsync( + new CreateIndexModel(indexKey.Hashed(field), createOptions), + cancellationToken: cancellationToken); } - /// - /// Creates a hashed index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async virtual Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - var collection = HandlePartitioned(partitionKey); - var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions); - var indexKey = Builders.IndexKeys; - return await collection.Indexes - .CreateOneAsync( - new CreateIndexModel(indexKey.Hashed(field), createOptions)); - } - - /// - /// Creates a combined text index. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The fields we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async virtual Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + /// + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { @@ -146,22 +121,19 @@ namespace MongoDbGenericRepository.DataAccess.Index { listOfDefs.Add(Builders.IndexKeys.Text(field)); } + return await collection.Indexes - .CreateOneAsync(new CreateIndexModel(Builders.IndexKeys.Combine(listOfDefs), createOptions)); + .CreateOneAsync( + new CreateIndexModel(Builders.IndexKeys.Combine(listOfDefs), createOptions), + cancellationToken: cancellationToken); } - /// - /// Drops the index given a field name - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The name of the index - /// An optional partition key - public async virtual Task DropIndexAsync(string indexName, string partitionKey = null) + /// + public virtual async Task DropIndexAsync(string indexName, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - await HandlePartitioned(partitionKey).Indexes.DropOneAsync(indexName); + await HandlePartitioned(partitionKey).Indexes.DropOneAsync(indexName, cancellationToken); } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs b/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs new file mode 100644 index 0000000..db14f81 --- /dev/null +++ b/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs @@ -0,0 +1,695 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Base; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository.DataAccess.Read +{ + /// + /// A interface for accessing the Database and its Collections. + /// + public interface IMongoDbReader : IDataAccessBase + { + /// + /// Asynchronously returns a projected document matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + /// An optional partition key. + /// An optional cancellation Token. + Task ProjectOneAsync( + Expression> filter, + Expression> projection, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Returns a projected document matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + /// An optional partition key. + /// The cancellation token. + TProjection ProjectOne( + Expression> filter, + Expression> projection, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// A LINQ expression filter. + /// The projection expression. + /// An optional partition key. + /// An optional cancellation Token. + Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// The document filter. + /// The projection expression. + /// An optional partition key. + /// The cancellation token. + List ProjectMany( + Expression> filter, + Expression> projection, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + + /// + /// Groups a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the grouping criteria. + /// The type of the projected group. + /// The grouping criteria. + /// The projected group result. + /// The partition key of your document, if any. + /// The cancellation token. + List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new(); + + /// + /// Groups filtered a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the grouping criteria. + /// The type of the projected group. + /// A LINQ expression filter. + /// The grouping criteria. + /// The projected group result. + /// The partition key of your document, if any. + /// The cancellation token. + List GroupBy( + Expression> filter, + Expression> selector, + Expression, TProjection>> projection, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new(); + + /// + /// Groups filtered a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the grouping criteria. + /// The type of the projected group. + /// A LINQ expression filter. + /// The grouping criteria. + /// The projected group result. + /// The partition key of your document, if any. + /// An optional cancellation Token. + Task> GroupByAsync( + Expression> filter, + Expression> selector, + Expression, TProjection>> projection, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new(); + + /// + /// Asynchronously returns a paginated list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The property selector. + /// Order of the sorting. + /// The number of documents you want to skip. Default value is 0. + /// The number of documents you want to take. Default value is 50. + /// An optional partition key. + /// An optional cancellation Token. + Task> GetSortedPaginatedAsync( + Expression> filter, + Expression> sortSelector, + bool ascending = true, + int skipNumber = 0, + int takeNumber = 50, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns a paginated list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The sort definition. + /// The number of documents you want to skip. Default value is 0. + /// The number of documents you want to take. Default value is 50. + /// An optional partition key. + /// An optional cancellation Token. + Task> GetSortedPaginatedAsync( + Expression> filter, + SortDefinition sortDefinition, + int skipNumber = 0, + int takeNumber = 50, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns one document given its id. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The Id of the document you want to get. + /// An optional partition key. + /// An optional cancellation Token. + Task GetByIdAsync(TKey id, string partitionKey = null, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns one document given its id. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The Id of the document you want to get. + /// An optional partition key. + /// The cancellation token. + TDocument GetById(TKey id, string partitionKey = null, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + /// An optional cancellation Token. + Task GetOneAsync( + FilterDefinition condition, + FindOptions findOption = null, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns one document given an expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// An optional cancellation Token. + Task GetOneAsync( + Expression> filter, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns one document given filter definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + /// The cancellation token. + TDocument GetOne( + FilterDefinition condition, + FindOptions findOption = null, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns one document given an expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token. + TDocument GetOne(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns a collection cursor. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + IFindFluent GetCursor(Expression> filter, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partition key. + /// An optional cancellation Token. + Task AnyAsync( + FilterDefinition condition, + CountOptions countOption = null, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// An optional cancellation Token. + Task AnyAsync( + Expression> filter, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partition key. + /// The cancellation token. + bool Any( + FilterDefinition condition, + CountOptions countOption = null, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns true if any of the document of the collection matches the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token. + bool Any(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + /// An optional cancellation Token. + Task> GetAllAsync( + FilterDefinition condition, + FindOptions findOption = null, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// An optional cancellation Token. + Task> GetAllAsync( + Expression> filter, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb filter option. + /// An optional partition key. + /// The cancellation token. + List GetAll( + FilterDefinition condition, + FindOptions findOption = null, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns a list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token. + List GetAll( + Expression> filter, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partitionKey + /// An optional cancellation Token. + Task CountAsync( + FilterDefinition condition, + CountOptions countOption = null, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partitionKey + /// An optional cancellation Token. + Task CountAsync( + Expression> filter, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A mongodb filter definition. + /// A mongodb counting option. + /// An optional partitionKey + /// The cancellation token. + long Count( + FilterDefinition condition, + CountOptions countOption = null, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Counts how many documents match the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partitionKey + /// The cancellation token. + long Count(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// The type of the primary key. + /// A LINQ expression filter. + /// A property selector to order by descending. + /// An optional partitionKey. + /// An optional cancellation Token. + Task GetByMaxAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// The type of the primary key. + /// A LINQ expression filter. + /// A property selector to order by descending. + /// An optional partitionKey. + /// The cancellation token. + TDocument GetByMax( + Expression> filter, + Expression> maxValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// The type of the primary key. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional partitionKey. + /// An optional cancellation Token. + Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the + /// filter. + /// + /// The document type. + /// The type of the primary key. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional partitionKey. + /// The cancellation token. + TDocument GetByMin( + Expression> filter, + Expression> minValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the primary key. + /// The type of the field for which you want the maximum value. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional partitionKey. + /// An optional cancellation Token. + Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the primary key. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional partitionKey. + /// The cancellation token. + TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the primary key. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional partition key. + /// An optional cancellation Token. + Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. + /// + /// The document type. + /// The type of the primary key. + /// The type of the value used to order the query. + /// A LINQ expression filter. + /// A property selector to order by ascending. + /// An optional partition key. + /// The cancellation token. + TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Sums the values of a selected field for a given filtered collection of documents. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The field you want to sum. + /// The partition key of your document, if any. + /// An optional cancellation Token. + Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Sums the values of a selected field for a given filtered collection of documents. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The field you want to sum. + /// The partition key of your document, if any. + /// An optional cancellation Token. + Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Sums the values of a selected field for a given filtered collection of documents. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The field you want to sum. + /// The partition key of your document, if any. + int SumBy( + Expression> filter, + Expression> selector, + string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Sums the values of a selected field for a given filtered collection of documents. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The field you want to sum. + /// The partition key of your document, if any. + decimal SumBy( + Expression> filter, + Expression> selector, + string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.GroupBy.cs b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.GroupBy.cs new file mode 100644 index 0000000..c808147 --- /dev/null +++ b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.GroupBy.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository.DataAccess.Read +{ + public partial class MongoDbReader + { + /// + public virtual List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new() + { + return HandlePartitioned(partitionKey) + .Aggregate() + .Group(groupingCriteria, groupProjection) + .ToList(cancellationToken); + } + + /// + public virtual List GroupBy( + Expression> filter, + Expression> selector, + Expression, TProjection>> projection, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new() + { + var collection = HandlePartitioned(partitionKey); + return collection.Aggregate() + .Match(Builders.Filter.Where(filter)) + .Group(selector, projection) + .ToList(cancellationToken); + } + + /// + public virtual async Task> GroupByAsync( + Expression> filter, + Expression> selector, + Expression, TProjection>> projection, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new() + { + var collection = HandlePartitioned(partitionKey); + return await collection.Aggregate() + .Match(Builders.Filter.Where(filter)) + .Group(selector, projection) + .ToListAsync(cancellationToken); + } + + /// + public virtual async Task> GetSortedPaginatedAsync( + Expression> filter, + Expression> sortSelector, + bool ascending = true, + int skipNumber = 0, + int takeNumber = 50, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + { + var sorting = ascending + ? Builders.Sort.Ascending(sortSelector) + : Builders.Sort.Descending(sortSelector); + + return await HandlePartitioned(partitionKey) + .Find(filter) + .Sort(sorting) + .Skip(skipNumber) + .Limit(takeNumber) + .ToListAsync(cancellationToken); + } + + /// + public virtual async Task> GetSortedPaginatedAsync( + Expression> filter, + SortDefinition sortDefinition, + int skipNumber = 0, + int takeNumber = 50, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + { + return await HandlePartitioned(partitionKey) + .Find(filter) + .Sort(sortDefinition) + .Skip(skipNumber) + .Limit(takeNumber) + .ToListAsync(cancellationToken); + } + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs index c343668..05ab62d 100644 --- a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs +++ b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs @@ -1,40 +1,33 @@ -using MongoDB.Driver; -using MongoDB.Driver.Linq; -using MongoDbGenericRepository.DataAccess.Base; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDB.Driver.Linq; +using MongoDbGenericRepository.DataAccess.Base; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository.DataAccess.Read { /// - /// A class to read MongoDb document. + /// A class to read MongoDb document. /// - public partial class MongoDbReader : DataAccessBase + public partial class MongoDbReader : DataAccessBase, IMongoDbReader { /// - /// The construct of the MongoDbReader class. + /// The construct of the MongoDbReader class. /// - /// A instance. + /// A instance. public MongoDbReader(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } #region Read TKey - /// - /// Asynchronously returns one document given its id. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The Id of the document you want to get. - /// An optional partition key. - /// An optional cancellation Token. - public async virtual Task GetByIdAsync(TKey id, string partitionKey = null, CancellationToken cancellationToken = default) + /// + public virtual async Task GetByIdAsync(TKey id, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { @@ -42,90 +35,62 @@ namespace MongoDbGenericRepository.DataAccess.Read return await HandlePartitioned(partitionKey).Find(filter).FirstOrDefaultAsync(cancellationToken); } - /// - /// Returns one document given its id. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The Id of the document you want to get. - /// An optional partition key. - public virtual TDocument GetById(TKey id, string partitionKey = null) + /// + public virtual TDocument GetById(TKey id, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", id); - return HandlePartitioned(partitionKey).Find(filter).FirstOrDefault(); + return HandlePartitioned(partitionKey).Find(filter).FirstOrDefault(cancellationToken); } - /// - /// Asynchronously returns one document given filter definition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A mongodb filter definition. - /// A mongodb filter option. - /// An optional partition key. - /// An optional cancellation Token. - public virtual Task GetOneAsync(FilterDefinition condition, FindOptions findOption = null, + /// + public virtual Task GetOneAsync( + FilterDefinition condition, + FindOptions findOption = null, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return HandlePartitioned(partitionKey).Find(condition, findOption).FirstOrDefaultAsync(cancellationToken); + return HandlePartitioned(partitionKey).Find(condition, findOption).FirstOrDefaultAsync(cancellationToken); } - /// - /// Returns one document given filter definition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A mongodb filter definition. - /// A mongodb filter option. - /// An optional partition key. - public virtual TDocument GetOne(FilterDefinition condition, FindOptions findOption = null, string partitionKey = null) + /// + public virtual TDocument GetOne( + FilterDefinition condition, + FindOptions findOption = null, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return HandlePartitioned(partitionKey).Find(condition, findOption).FirstOrDefault(); + return HandlePartitioned(partitionKey).Find(condition, findOption).FirstOrDefault(cancellationToken); } - /// - /// Asynchronously returns one document given an expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// An optional cancellation Token. - public async virtual Task GetOneAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) + /// + public virtual async Task GetOneAsync( + Expression> filter, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return await HandlePartitioned(partitionKey).Find(filter).FirstOrDefaultAsync(cancellationToken); } - /// - /// Returns one document given an expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - public virtual TDocument GetOne(Expression> filter, string partitionKey = null) + /// + public virtual TDocument GetOne( + Expression> filter, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return HandlePartitioned(partitionKey).Find(filter).FirstOrDefault(); + return HandlePartitioned(partitionKey).Find(filter).FirstOrDefault(cancellationToken); } - /// - /// Returns a collection cursor. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. + /// public virtual IFindFluent GetCursor(Expression> filter, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable @@ -133,16 +98,11 @@ namespace MongoDbGenericRepository.DataAccess.Read return HandlePartitioned(partitionKey).Find(filter); } - /// - /// Returns true if any of the document of the collection matches the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A mongodb filter definition. - /// A mongodb counting option. - /// An optional partition key. - /// An optional cancellation Token. - public virtual async Task AnyAsync(FilterDefinition condition, CountOptions countOption = null, string partitionKey = null, + /// + public virtual async Task AnyAsync( + FilterDefinition condition, + CountOptions countOption = null, + string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable @@ -151,396 +111,297 @@ namespace MongoDbGenericRepository.DataAccess.Read return count > 0; } - /// - /// Returns true if any of the document of the collection matches the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A mongodb filter definition. - /// A mongodb counting option. - /// An optional partition key. - public virtual bool Any(FilterDefinition condition, CountOptions countOption = null, - string partitionKey = null) + /// + public virtual bool Any( + FilterDefinition condition, + CountOptions countOption = null, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - var count = HandlePartitioned(partitionKey).CountDocuments(condition, countOption); + var count = HandlePartitioned(partitionKey).CountDocuments(condition, countOption, cancellationToken); return count > 0; } - /// - /// Returns true if any of the document of the collection matches the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// An optional cancellation Token. - public async virtual Task AnyAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) + /// + public virtual async Task AnyAsync( + Expression> filter, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var count = await HandlePartitioned(partitionKey).CountDocumentsAsync(filter, cancellationToken: cancellationToken); - return (count > 0); + return count > 0; } - /// - /// Returns true if any of the document of the collection matches the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - public virtual bool Any(Expression> filter, string partitionKey = null) + /// + public virtual bool Any( + Expression> filter, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - var count = HandlePartitioned(partitionKey).CountDocuments(filter); - return (count > 0); + var count = HandlePartitioned(partitionKey).CountDocuments(filter, cancellationToken: cancellationToken); + return count > 0; } - /// - /// Asynchronously returns a list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A mongodb filter definition. - /// A mongodb filter option. - /// An optional partition key. - /// An optional cancellation Token. - public virtual Task> GetAllAsync(FilterDefinition condition, - FindOptions findOption = null, string partitionKey = null, CancellationToken cancellationToken = default) + /// + public virtual Task> GetAllAsync( + FilterDefinition condition, + FindOptions findOption = null, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return HandlePartitioned(partitionKey).Find(condition, findOption).ToListAsync(cancellationToken); } - /// - /// Returns a list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A mongodb filter definition. - /// A mongodb filter option. - /// An optional partition key. - public virtual List GetAll(FilterDefinition condition, FindOptions findOption = null, - string partitionKey = null) + /// + public virtual List GetAll( + FilterDefinition condition, + FindOptions findOption = null, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return HandlePartitioned(partitionKey).Find(condition, findOption).ToList(); + return HandlePartitioned(partitionKey).Find(condition, findOption).ToList(cancellationToken); } - /// - /// Asynchronously returns a list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// An optional cancellation Token. - public async virtual Task> GetAllAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) + /// + public virtual async Task> GetAllAsync( + Expression> filter, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return await HandlePartitioned(partitionKey).Find(filter).ToListAsync(cancellationToken); } - /// - /// Returns a list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - public virtual List GetAll(Expression> filter, string partitionKey = null) + /// + public virtual List GetAll( + Expression> filter, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return HandlePartitioned(partitionKey).Find(filter).ToList(); + return HandlePartitioned(partitionKey).Find(filter).ToList(cancellationToken); } - /// - /// Asynchronously counts how many documents match the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A mongodb filter definition. - /// A mongodb counting option. - /// An optional partitionKey - /// An optional cancellation Token. - public virtual Task CountAsync(FilterDefinition condition, CountOptions countOption = null, - string partitionKey = null, CancellationToken cancellationToken = default) + /// + public virtual Task CountAsync( + FilterDefinition condition, + CountOptions countOption = null, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return HandlePartitioned(partitionKey).CountDocumentsAsync(condition, countOption, cancellationToken); } - /// - /// Counts how many documents match the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A mongodb filter definition. - /// A mongodb counting option. - /// An optional partitionKey - public virtual long Count(FilterDefinition condition, CountOptions countOption = null, - string partitionKey = null) + /// + public virtual long Count( + FilterDefinition condition, + CountOptions countOption = null, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return HandlePartitioned(partitionKey).CountDocuments(condition, countOption); + return HandlePartitioned(partitionKey).CountDocuments(condition, countOption, cancellationToken); } - /// - /// Asynchronously counts how many documents match the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partitionKey - /// An optional cancellation Token. - public async virtual Task CountAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) + /// + public virtual async Task CountAsync( + Expression> filter, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return await HandlePartitioned(partitionKey).CountDocumentsAsync(filter, cancellationToken: cancellationToken); } - /// - /// Counts how many documents match the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partitionKey - public virtual long Count(Expression> filter, string partitionKey = null) + /// + public virtual long Count( + Expression> filter, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return HandlePartitioned(partitionKey).Find(filter).CountDocuments(); + return HandlePartitioned(partitionKey).Find(filter).CountDocuments(cancellationToken); } #endregion #region Min / Max - /// - /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter. - /// - /// The document type. - /// The type of the primary key. - /// A LINQ expression filter. - /// A property selector to order by descending. - /// An optional partitionKey. - /// An optional cancellation Token. - public async virtual Task GetByMaxAsync(Expression> filter, Expression> maxValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) + /// + public virtual async Task GetByMaxAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return await GetCollection(partitionKey).Find(Builders.Filter.Where(filter)) - .SortByDescending(maxValueSelector) - .Limit(1) - .FirstOrDefaultAsync(cancellationToken); + .SortByDescending(maxValueSelector) + .Limit(1) + .FirstOrDefaultAsync(cancellationToken); } - /// - /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter. - /// - /// The document type. - /// The type of the primary key. - /// A LINQ expression filter. - /// A property selector to order by descending. - /// An optional partitionKey. - public virtual TDocument GetByMax(Expression> filter, Expression> maxValueSelector, string partitionKey = null) + /// + public virtual TDocument GetByMax( + Expression> filter, + Expression> maxValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return GetCollection(partitionKey).Find(Builders.Filter.Where(filter)) - .SortByDescending(maxValueSelector) - .Limit(1) - .FirstOrDefault(); + .SortByDescending(maxValueSelector) + .Limit(1) + .FirstOrDefault(cancellationToken); } - /// - /// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the filter. - /// - /// The document type. - /// The type of the primary key. - /// A LINQ expression filter. - /// A property selector to order by ascending. - /// An optional partitionKey. - /// An optional cancellation Token. - public async virtual Task GetByMinAsync(Expression> filter, Expression> minValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) + /// + public virtual async Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return await GetCollection(partitionKey).Find(Builders.Filter.Where(filter)) - .SortBy(minValueSelector) - .Limit(1) - .FirstOrDefaultAsync(cancellationToken); + .SortBy(minValueSelector) + .Limit(1) + .FirstOrDefaultAsync(cancellationToken); } - /// - /// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the filter. - /// - /// The document type. - /// The type of the primary key. - /// A LINQ expression filter. - /// A property selector to order by ascending. - /// An optional partitionKey. - public virtual TDocument GetByMin(Expression> filter, Expression> minValueSelector, string partitionKey = null) + /// + public virtual TDocument GetByMin( + Expression> filter, + Expression> minValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return GetCollection(partitionKey).Find(Builders.Filter.Where(filter)) - .SortBy(minValueSelector) - .Limit(1) - .FirstOrDefault(); + .SortBy(minValueSelector) + .Limit(1) + .FirstOrDefault(cancellationToken); } - /// - /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. - /// - /// The document type. - /// The type of the primary key. - /// The type of the field for which you want the maximum value. - /// A LINQ expression filter. - /// A property selector to order by ascending. - /// An optional partitionKey. - /// An optional cancellation Token. - public async virtual Task GetMaxValueAsync(Expression> filter, Expression> maxValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) + /// + public virtual async Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return await GetMaxMongoQuery(filter, maxValueSelector, partitionKey) - .Project(maxValueSelector) - .FirstOrDefaultAsync(cancellationToken); + .Project(maxValueSelector) + .FirstOrDefaultAsync(cancellationToken); } - /// - /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. - /// - /// The document type. - /// The type of the primary key. - /// The type of the value used to order the query. - /// A LINQ expression filter. - /// A property selector to order by ascending. - /// An optional partitionKey. - public virtual TValue GetMaxValue(Expression> filter, Expression> maxValueSelector, string partitionKey = null) + /// + public virtual TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return GetMaxMongoQuery(filter, maxValueSelector, partitionKey) - .Project(maxValueSelector) - .FirstOrDefault(); + .Project(maxValueSelector) + .FirstOrDefault(cancellationToken); } - /// - /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. - /// - /// The document type. - /// The type of the primary key. - /// The type of the value used to order the query. - /// A LINQ expression filter. - /// A property selector to order by ascending. - /// An optional partition key. - /// An optional cancellation Token. - public virtual async Task GetMinValueAsync(Expression> filter, Expression> minValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) + /// + public virtual async Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await GetMinMongoQuery(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefaultAsync(cancellationToken); + return await GetMinMongoQuery(filter, minValueSelector, partitionKey).Project(minValueSelector) + .FirstOrDefaultAsync(cancellationToken); } - /// - /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. - /// - /// The document type. - /// The type of the primary key. - /// The type of the value used to order the query. - /// A LINQ expression filter. - /// A property selector to order by ascending. - /// An optional partition key. - public virtual TValue GetMinValue(Expression> filter, Expression> minValueSelector, string partitionKey = null) + /// + public virtual TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return GetMinMongoQuery(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefault(); + return GetMinMongoQuery(filter, minValueSelector, partitionKey).Project(minValueSelector) + .FirstOrDefault(cancellationToken); } - #endregion Min / Max #region Sum TKey - /// - /// Sums the values of a selected field for a given filtered collection of documents. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// The field you want to sum. - /// The partition key of your document, if any. - /// An optional cancellation Token. - public virtual async Task SumByAsync(Expression> filter, - Expression> selector, - string partitionKey = null, - CancellationToken cancellationToken = default) + /// + public virtual async Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return await GetQuery(filter, partitionKey).SumAsync(selector, cancellationToken); } - /// - /// Sums the values of a selected field for a given filtered collection of documents. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// The field you want to sum. - /// The partition key of your document, if any. - public virtual int SumBy(Expression> filter, - Expression> selector, - string partitionKey = null) + /// + public virtual int SumBy( + Expression> filter, + Expression> selector, + string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return GetQuery(filter, partitionKey).Sum(selector); } - /// - /// Sums the values of a selected field for a given filtered collection of documents. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// The field you want to sum. - /// The partition key of your document, if any. - /// An optional cancellation Token. - public virtual async Task SumByAsync(Expression> filter, - Expression> selector, - string partitionKey = null, CancellationToken cancellationToken = default) + /// + public virtual async Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return await GetQuery(filter, partitionKey).SumAsync(selector, cancellationToken); } - /// - /// Sums the values of a selected field for a given filtered collection of documents. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// The field you want to sum. - /// The partition key of your document, if any. - public virtual decimal SumBy(Expression> filter, - Expression> selector, - string partitionKey = null) + /// + public virtual decimal SumBy( + Expression> filter, + Expression> selector, + string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { @@ -549,155 +410,4 @@ namespace MongoDbGenericRepository.DataAccess.Read #endregion Sum TKey } - - public partial class MongoDbReader - { - /// - /// Groups a collection of documents given a grouping criteria, - /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the grouping criteria. - /// The type of the projected group. - /// The grouping criteria. - /// The projected group result. - /// The partition key of your document, if any. - public virtual List GroupBy( - Expression> groupingCriteria, - Expression, TProjection>> groupProjection, - string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - where TProjection : class, new() - { - return HandlePartitioned(partitionKey) - .Aggregate() - .Group(groupingCriteria, groupProjection) - .ToList(); - - } - - /// - /// Groups filtered a collection of documents given a grouping criteria, - /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the grouping criteria. - /// The type of the projected group. - /// A LINQ expression filter. - /// The grouping criteria. - /// The projected group result. - /// The partition key of your document, if any. - public virtual List GroupBy( - Expression> filter, - Expression> selector, - Expression, TProjection>> projection, - string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - where TProjection : class, new() - { - var collection = HandlePartitioned(partitionKey); - return collection.Aggregate() - .Match(Builders.Filter.Where(filter)) - .Group(selector, projection) - .ToList(); - } - - /// - /// Groups filtered a collection of documents given a grouping criteria, - /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the grouping criteria. - /// The type of the projected group. - /// A LINQ expression filter. - /// The grouping criteria. - /// The projected group result. - /// The partition key of your document, if any. - /// An optional cancellation Token. - public virtual async Task> GroupByAsync( - Expression> filter, - Expression> selector, - Expression, TProjection>> projection, - string partitionKey = null, - CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable - where TProjection : class, new() - { - var collection = HandlePartitioned(partitionKey); - return await collection.Aggregate() - .Match(Builders.Filter.Where(filter)) - .Group(selector, projection) - .ToListAsync(cancellationToken); - } - - /// - /// Asynchronously returns a paginated list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// The property selector. - /// Order of the sorting. - /// The number of documents you want to skip. Default value is 0. - /// The number of documents you want to take. Default value is 50. - /// An optional partition key. - /// An optional cancellation Token. - public virtual async Task> GetSortedPaginatedAsync( - Expression> filter, - Expression> sortSelector, - bool ascending = true, - int skipNumber = 0, - int takeNumber = 50, - string partitionKey = null, - CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable - { - var sorting = ascending - ? Builders.Sort.Ascending(sortSelector) - : Builders.Sort.Descending(sortSelector); - - return await HandlePartitioned(partitionKey) - .Find(filter) - .Sort(sorting) - .Skip(skipNumber) - .Limit(takeNumber) - .ToListAsync(cancellationToken); - } - - /// - /// Asynchronously returns a paginated list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// The sort definition. - /// The number of documents you want to skip. Default value is 0. - /// The number of documents you want to take. Default value is 50. - /// An optional partition key. - /// An optional cancellation Token. - public virtual async Task> GetSortedPaginatedAsync( - Expression> filter, - SortDefinition sortDefinition, - int skipNumber = 0, - int takeNumber = 50, - string partitionKey = null, - CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable - { - return await HandlePartitioned(partitionKey) - .Find(filter) - .Sort(sortDefinition) - .Skip(skipNumber) - .Limit(takeNumber) - .ToListAsync(cancellationToken); - } - } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Project.cs b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Project.cs index 378e86e..ebf0c98 100644 --- a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Project.cs +++ b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Project.cs @@ -1,73 +1,35 @@ -using MongoDB.Driver; -using MongoDB.Driver.Linq; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; -using System.Linq; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository.DataAccess.Read { public partial class MongoDbReader { - /// - /// Asynchronously returns a projected document matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type representing the model you want to project to. - /// A LINQ expression filter. - /// The projection expression. - /// An optional partition key. - /// An optional cancellation Token. + /// public virtual async Task ProjectOneAsync( - Expression> filter, - Expression> projection, + Expression> filter, + Expression> projection, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable where TProjection : class { - return await HandlePartitioned(partitionKey).Find(filter) - .Project(projection) - .FirstOrDefaultAsync(cancellationToken); + return await HandlePartitioned(partitionKey) + .Find(filter) + .Project(projection) + .FirstOrDefaultAsync(cancellationToken); } - /// - /// Returns a projected document matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type representing the model you want to project to. - /// A LINQ expression filter. - /// The projection expression. - /// An optional partition key. - public virtual TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - where TProjection : class - { - return HandlePartitioned(partitionKey).Find(filter) - .Project(projection) - .FirstOrDefault(); - } - - /// - /// Asynchronously returns a list of projected documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type representing the model you want to project to. - /// A LINQ expression filter. - /// The projection expression. - /// An optional partition key. - /// An optional cancellation Token. + /// public virtual async Task> ProjectManyAsync( - Expression> filter, - Expression> projection, + Expression> filter, + Expression> projection, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument @@ -75,27 +37,38 @@ namespace MongoDbGenericRepository.DataAccess.Read where TProjection : class { return await HandlePartitioned(partitionKey).Find(filter) - .Project(projection) - .ToListAsync(cancellationToken); + .Project(projection) + .ToListAsync(cancellationToken); } - /// - /// Asynchronously returns a list of projected documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type representing the model you want to project to. - /// The document filter. - /// The projection expression. - /// An optional partition key. - public virtual List ProjectMany(Expression> filter, Expression> projection, string partitionKey = null) + /// + public virtual TProjection ProjectOne( + Expression> filter, + Expression> projection, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable where TProjection : class { return HandlePartitioned(partitionKey).Find(filter) - .Project(projection) - .ToList(); + .Project(projection) + .FirstOrDefault(cancellationToken); + } + + /// + public virtual List ProjectMany( + Expression> filter, + Expression> projection, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return HandlePartitioned(partitionKey).Find(filter) + .Project(projection) + .ToList(cancellationToken); } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs b/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs new file mode 100644 index 0000000..757606d --- /dev/null +++ b/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs @@ -0,0 +1,525 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Base; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository.DataAccess.Update +{ + /// + /// A interface for updating documents in MongoDb. + /// + public interface IMongoDbUpdater : IDataAccessBase + { + /// + /// Asynchronously Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document with the modifications you want to persist. + /// An optional cancellation token. + Task UpdateOneAsync(TDocument modifiedDocument, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Takes a document you want to modify and applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to modify. + /// The update definition for the document. + /// An optional cancellation token. + Task UpdateOneAsync( + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document you want to modify. + /// The field selector. + /// The new value of the property field. + /// An optional cancellation token. + Task UpdateOneAsync( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + /// An optional cancellation token. + Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entity selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + /// An optional cancellation token. + Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The client session. + /// The document with the modifications you want to persist. + /// The optional cancellation token. + /// + Task UpdateOneAsync(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The client session. + /// The document to modify. + /// The update definition. + /// The optional cancellation token. + /// + Task UpdateOneAsync( + IClientSessionHandle session, + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The document to modify. + /// The field to update. + /// The value of the field. + /// The optional cancellation token. + /// + Task UpdateOneAsync( + IClientSessionHandle session, + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// The optional partition key. + /// The optional cancellation token. + /// + Task UpdateOneAsync( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// The optional partition key. + /// The optional cancellation token. + /// + Task UpdateOneAsync( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document with the modifications you want to persist. + /// The optional cancellation token. + bool UpdateOne(TDocument modifiedDocument, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Takes a document you want to modify and applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to modify. + /// The update definition for the document. + /// The optional cancellation token. + bool UpdateOne(TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document you want to modify. + /// The field selector. + /// The new value of the property field. + /// The optional cancellation token. + bool UpdateOne( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + /// The optional cancellation token. + bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entity selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + /// The optional cancellation token. + bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The client session. + /// The document with the modifications you want to persist. + /// The optional cancellation token. + /// + bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The client session. + /// The document to modify. + /// The update definition. + /// The optional cancellation token. + /// + bool UpdateOne( + IClientSessionHandle session, + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The document to modify. + /// The field to update. + /// The value of the field. + /// The optional cancellation token. + /// + bool UpdateOne( + IClientSessionHandle session, + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// The optional partition key. + /// The optional cancellation token. + /// + bool UpdateOne( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field to update. + /// The client session. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// The optional partition key. + /// The optional cancellation token. + /// + bool UpdateOne( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + /// The optional cancellation token. + Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + /// The optional cancellation token. + Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, apply the update definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// the update definition + /// The value of the partition key. + /// The optional cancellation token. + Task UpdateManyAsync( + Expression> filter, + UpdateDefinition update, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, apply the update definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition. + /// The value of the partition key. + /// The optional cancellation token. + Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + /// The optional cancellation token. + long UpdateMany( + Expression> filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + /// The optional cancellation token. + long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, apply the update definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition. + /// The value of the partition key. + /// The optional cancellation token. + long UpdateMany( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, apply the update definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition. + /// The value of the partition key. + /// The optional cancellation token. + long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs index dbb1283..ff78746 100644 --- a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs +++ b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs @@ -1,82 +1,61 @@ -using MongoDB.Driver; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository.DataAccess.Update { public partial class MongoDbUpdater { - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The client session. - /// The document with the modifications you want to persist. - /// The optional cancellation token. - /// - public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + TDocument modifiedDocument, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", modifiedDocument.Id); var updateRes = await HandlePartitioned(modifiedDocument) - .ReplaceOneAsync(session, filter, modifiedDocument, cancellationToken: cancellationToken) - .ConfigureAwait(false); + .ReplaceOneAsync(session, filter, modifiedDocument, cancellationToken: cancellationToken) + .ConfigureAwait(false); return updateRes.ModifiedCount == 1; } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The client session. - /// The document with the modifications you want to persist. - /// The optional cancellation token. - /// - public virtual bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", modifiedDocument.Id); - var updateRes = HandlePartitioned(modifiedDocument).ReplaceOne(session, filter, modifiedDocument, cancellationToken: cancellationToken); + var updateRes = HandlePartitioned(modifiedDocument) + .ReplaceOne(session, filter, modifiedDocument, cancellationToken: cancellationToken); return updateRes.ModifiedCount == 1; } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The client session. - /// The document to modify. - /// The update definition. - /// The optional cancellation token. - /// - public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", documentToModify.Id); - var updateRes = await HandlePartitioned(documentToModify).UpdateOneAsync(session, filter, update, null, cancellationToken).ConfigureAwait(false); + var updateRes = await HandlePartitioned(documentToModify).UpdateOneAsync(session, filter, update, null, cancellationToken) + .ConfigureAwait(false); return updateRes.ModifiedCount == 1; } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The client session. - /// The document to modify. - /// The update definition. - /// The optional cancellation token. - /// - public virtual bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual bool UpdateOne( + IClientSessionHandle session, + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { @@ -85,107 +64,82 @@ namespace MongoDbGenericRepository.DataAccess.Update return updateRes.ModifiedCount == 1; } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field to update. - /// The client session. - /// The document to modify. - /// The field to update. - /// The value of the field. - /// The optional cancellation token. - /// - public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", documentToModify.Id); var updateRes = await HandlePartitioned(documentToModify) - .UpdateOneAsync(session, filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken) - .ConfigureAwait(false); + .UpdateOneAsync(session, filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken) + .ConfigureAwait(false); return updateRes.ModifiedCount == 1; } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field to update. - /// The client session. - /// The document to modify. - /// The field to update. - /// The value of the field. - /// The optional cancellation token. - /// - public virtual bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual bool UpdateOne( + IClientSessionHandle session, + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", documentToModify.Id); - var updateRes = HandlePartitioned(documentToModify).UpdateOne(session, filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken); + var updateRes = HandlePartitioned(documentToModify).UpdateOne( + session, + filter, + Builders.Update.Set(field, value), + cancellationToken: cancellationToken); return updateRes.ModifiedCount == 1; } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field to update. - /// The client session. - /// The filter for the update. - /// The field to update. - /// The value of the field. - /// The optional partition key. - /// The optional cancellation token. - /// - public virtual async Task UpdateOneAsync(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual async Task UpdateOneAsync( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); - var updateRes = await collection.UpdateOneAsync(session, filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken).ConfigureAwait(false); + var updateRes = await collection.UpdateOneAsync(session, filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken) + .ConfigureAwait(false); return updateRes.ModifiedCount == 1; } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field to update. - /// The client session. - /// The filter for the update. - /// The field to update. - /// The value of the field. - /// The optional partition key. - /// The optional cancellation token. - /// - public virtual Task UpdateOneAsync(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual Task UpdateOneAsync( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return UpdateOneAsync(session, Builders.Filter.Where(filter), field, value, partitionKey, cancellationToken); } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field to update. - /// The client session. - /// The filter for the update. - /// The field to update. - /// The value of the field. - /// The optional partition key. - /// The optional cancellation token. - /// - public virtual bool UpdateOne(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual bool UpdateOne( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { @@ -194,24 +148,18 @@ namespace MongoDbGenericRepository.DataAccess.Update return updateRes.ModifiedCount == 1; } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field to update. - /// The client session. - /// The filter for the update. - /// The field to update. - /// The value of the field. - /// The optional partition key. - /// The optional cancellation token. - /// - public virtual bool UpdateOne(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + /// + public virtual bool UpdateOne( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return UpdateOne(session, Builders.Filter.Where(filter), field, value, partitionKey, cancellationToken); } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs index 2a4cdbd..7529b16 100644 --- a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs +++ b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs @@ -1,311 +1,271 @@ -using MongoDB.Driver; -using MongoDbGenericRepository.DataAccess.Base; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Base; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository.DataAccess.Update { - public partial class MongoDbUpdater : DataAccessBase + /// + /// The MongoDb updater. + /// + public partial class MongoDbUpdater : DataAccessBase, IMongoDbUpdater { + /// + /// Constructor + /// + /// public MongoDbUpdater(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } - /// - /// Asynchronously Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document with the modifications you want to persist. - public virtual async Task UpdateOneAsync(TDocument modifiedDocument) + /// + public virtual async Task UpdateOneAsync(TDocument modifiedDocument, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", modifiedDocument.Id); - var updateRes = await HandlePartitioned(modifiedDocument).ReplaceOneAsync(filter, modifiedDocument); + var updateRes = await HandlePartitioned(modifiedDocument) + .ReplaceOneAsync(filter, modifiedDocument, cancellationToken: cancellationToken); return updateRes.ModifiedCount == 1; } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document with the modifications you want to persist. - public virtual bool UpdateOne(TDocument modifiedDocument) + /// + public virtual bool UpdateOne(TDocument modifiedDocument, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", modifiedDocument.Id); - var updateRes = HandlePartitioned(modifiedDocument).ReplaceOne(filter, modifiedDocument); + var updateRes = HandlePartitioned(modifiedDocument).ReplaceOne(filter, modifiedDocument, cancellationToken: cancellationToken); return updateRes.ModifiedCount == 1; } - /// - /// Takes a document you want to modify and applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to modify. - /// The update definition for the document. - public virtual async Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update) + /// + public virtual async Task UpdateOneAsync( + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", documentToModify.Id); - var updateRes = await HandlePartitioned(documentToModify).UpdateOneAsync(filter, update); + var updateRes = await HandlePartitioned(documentToModify).UpdateOneAsync(filter, update, cancellationToken: cancellationToken); return updateRes.ModifiedCount == 1; } - /// - /// Takes a document you want to modify and applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to modify. - /// The update definition for the document. - public virtual bool UpdateOne(TDocument documentToModify, UpdateDefinition update) + /// + public virtual bool UpdateOne( + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", documentToModify.Id); - var updateRes = HandlePartitioned(documentToModify).UpdateOne(filter, update); + var updateRes = HandlePartitioned(documentToModify).UpdateOne(filter, update, cancellationToken: cancellationToken); return updateRes.ModifiedCount == 1; } - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document you want to modify. - /// The field selector. - /// The new value of the property field. - public virtual async Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value) + /// + public virtual async Task UpdateOneAsync( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", documentToModify.Id); - var updateRes = await HandlePartitioned(documentToModify).UpdateOneAsync(filter, Builders.Update.Set(field, value)); + var updateRes = await HandlePartitioned(documentToModify).UpdateOneAsync( + filter, + Builders.Update.Set(field, value), + cancellationToken: cancellationToken); return updateRes.ModifiedCount == 1; } - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document you want to modify. - /// The field selector. - /// The new value of the property field. - public virtual bool UpdateOne(TDocument documentToModify, Expression> field, TField value) + /// + public virtual bool UpdateOne( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", documentToModify.Id); - var updateRes = HandlePartitioned(documentToModify).UpdateOne(filter, Builders.Update.Set(field, value)); + var updateRes = HandlePartitioned(documentToModify).UpdateOne( + filter, + Builders.Update.Set(field, value), + cancellationToken: cancellationToken); return updateRes.ModifiedCount == 1; } - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); - var updateRes = await collection.UpdateOneAsync(filter, Builders.Update.Set(field, value)); + var updateRes = await collection.UpdateOneAsync(filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken); return updateRes.ModifiedCount == 1; } - /// - /// For the entity selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - public virtual async Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual async Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await UpdateOneAsync(Builders.Filter.Where(filter), field, value, partitionKey); + return await UpdateOneAsync(Builders.Filter.Where(filter), field, value, partitionKey, cancellationToken); } - /// - /// Updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); - var updateRes = collection.UpdateOne(filter, Builders.Update.Set(field, value)); + var updateRes = collection.UpdateOne(filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken); return updateRes.ModifiedCount == 1; } - /// - /// For the entity selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return UpdateOne(Builders.Filter.Where(filter), field, value, partitionKey); + return UpdateOne(Builders.Filter.Where(filter), field, value, partitionKey, cancellationToken); } - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - public virtual async Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await UpdateManyAsync(Builders.Filter.Where(filter), field, value, partitionKey); + return await UpdateManyAsync(Builders.Filter.Where(filter), field, value, partitionKey, cancellationToken); } - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual async Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); - var updateRes = await collection.UpdateManyAsync(filter, Builders.Update.Set(field, value)); + var updateRes = await collection.UpdateManyAsync(filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken); return updateRes.ModifiedCount; } - /// - /// For the entities selected by the filter, apply the update definition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual async Task UpdateManyAsync(Expression> filter, UpdateDefinition update, string partitionKey = null) + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + UpdateDefinition update, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await UpdateManyAsync(Builders.Filter.Where(filter), update, partitionKey); + return await UpdateManyAsync(Builders.Filter.Where(filter), update, partitionKey, cancellationToken); } - /// - /// For the entities selected by the filter, apply the update definition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The update definition. - /// The value of the partition key. - public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); - var updateRes = await collection.UpdateManyAsync(filter, updateDefinition); + var updateRes = await collection.UpdateManyAsync(filter, updateDefinition, cancellationToken: cancellationToken); return updateRes.ModifiedCount; } - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - public virtual long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual long UpdateMany( + Expression> filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return UpdateMany(Builders.Filter.Where(filter), field, value, partitionKey); + return UpdateMany(Builders.Filter.Where(filter), field, value, partitionKey, cancellationToken); } - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); - var updateRes = collection.UpdateMany(filter, Builders.Update.Set(field, value)); + var updateRes = collection.UpdateMany(filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken); return updateRes.ModifiedCount; } - /// - /// For the entities selected by the filter, apply the update definition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The update definition. - /// The value of the partition key. - public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition UpdateDefinition, string partitionKey = null) + /// + public virtual long UpdateMany( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); - var updateRes = collection.UpdateMany(filter, UpdateDefinition); + var updateRes = collection.UpdateMany(filter, updateDefinition, cancellationToken: cancellationToken); + return updateRes.ModifiedCount; + } + + /// + public virtual long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + { + var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); + var updateRes = collection.UpdateMany(filter, updateDefinition, cancellationToken: cancellationToken); return updateRes.ModifiedCount; } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Create.cs b/MongoDbGenericRepository/IBaseMongoRepository.Create.cs new file mode 100644 index 0000000..56be005 --- /dev/null +++ b/MongoDbGenericRepository/IBaseMongoRepository.Create.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository +{ + /// + /// The IBaseMongoRepository_Create interface to expose document creation functionality + /// with document having an Id of type Guid. + /// + public interface IBaseMongoRepository_Create : IBaseMongoRepository_Create + { + /// + /// Asynchronously adds a document to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to add. + Task AddOneAsync(TDocument document) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously adds a document to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to add. + /// An optional cancellation Token. + Task AddOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Adds a document to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to add. + void AddOne(TDocument document) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Adds a document to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to add. + /// The cancellation token. + void AddOne(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously adds a list of documents to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The documents you want to add. + Task AddManyAsync(IEnumerable documents) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously adds a list of documents to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The documents you want to add. + /// An optional cancellation Token. + Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Adds a list of documents to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The documents you want to add. + void AddMany(IEnumerable documents) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Adds a list of documents to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The documents you want to add. + /// The cancellation token + void AddMany(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs b/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs new file mode 100644 index 0000000..1407d5f --- /dev/null +++ b/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs @@ -0,0 +1,300 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository +{ + /// + /// The base Mongo Repository Delete interface. used to delete documents from the collections. + /// + public interface IBaseMongoRepository_Delete : IBaseMongoRepository_Delete + { + /// + /// Deletes a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to delete. + /// The number of documents deleted. + long DeleteOne(TDocument document) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Deletes a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to delete. + /// The cancellation token. + /// The number of documents deleted. + long DeleteOne(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The number of documents deleted. + long DeleteOne(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The cancellation token + /// The number of documents deleted. + long DeleteOne(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The number of documents deleted. + long DeleteOne(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token. + /// The number of documents deleted. + long DeleteOne(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to delete. + /// The number of documents deleted. + Task DeleteOneAsync(TDocument document) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to delete. + /// The Cancellation token + /// The number of documents deleted. + Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The number of documents deleted. + Task DeleteOneAsync(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteOneAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The number of documents deleted. + Task DeleteOneAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The number of documents deleted. + Task DeleteManyAsync(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteManyAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The number of documents deleted. + Task DeleteManyAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes a list of documents. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The list of documents to delete. + /// The number of documents deleted. + Task DeleteManyAsync(IEnumerable documents) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes a list of documents. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The list of documents to delete. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Deletes a list of documents. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The list of documents to delete. + /// The number of documents deleted. + long DeleteMany(IEnumerable documents) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Deletes a list of documents. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The list of documents to delete. + /// The cancellation token + /// The number of documents deleted. + long DeleteMany(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The number of documents deleted. + long DeleteMany(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The cancellation token. + /// The number of documents deleted. + long DeleteMany(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + + /// + /// Deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The number of documents deleted. + long DeleteMany(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token + /// The number of documents deleted. + long DeleteMany(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Index.cs b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs new file mode 100644 index 0000000..cccdc08 --- /dev/null +++ b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs @@ -0,0 +1,747 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository +{ + /// + /// The interface exposing index management functionality for Guid Keyed repositories. + /// + public interface IBaseMongoRepository_Index : IBaseMongoRepository_Index + { + /// + /// Returns the names of the indexes present on a collection. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A list containing the names of the indexes on on the concerned collection. + Task> GetIndexesNamesAsync() + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns the names of the indexes present on a collection. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The cancellation token + /// A list containing the names of the indexes on on the concerned collection. + Task> GetIndexesNamesAsync(CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns the names of the indexes present on a collection. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// An optional partition key + /// A list containing the names of the indexes on on the concerned collection. + Task> GetIndexesNamesAsync(string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Returns the names of the indexes present on a collection. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// An optional partition key + /// The cancellation token + /// A list containing the names of the indexes on on the concerned collection. + Task> GetIndexesNamesAsync(string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// The result of the create index operation. + Task CreateTextIndexAsync(Expression> field) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// The cancellation token + /// The result of the create index operation. + Task CreateTextIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// An optional partition key + /// The result of the create index operation. + Task CreateTextIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// An optional partition key + /// The cancellation token + /// The result of the create index operation. + Task CreateTextIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// The result of the create index operation. + Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// The cancellation token + /// The result of the create index operation. + Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// The result of the create index operation. + Task CreateHashedIndexAsync(Expression> field) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateHashedIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// The result of the create index operation. + Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateHashedIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateHashedIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The fields we want to index. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync(IEnumerable>> fields) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The fields we want to index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync(IEnumerable>> fields, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The fields we want to index. + /// Options for creating an index. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The fields we want to index. + /// Options for creating an index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The fields we want to index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync(IEnumerable>> fields, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The fields we want to index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The fields we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The fields we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Drops the index given a field name + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The name of the index + Task DropIndexAsync(string indexName) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Drops the index given a field name + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The name of the index + /// The cancellation token + Task DropIndexAsync(string indexName, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Drops the index given a field name + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The name of the index + /// An optional partition key + Task DropIndexAsync(string indexName, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Drops the index given a field name + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The name of the index + /// An optional partition key + /// The cancellation token + Task DropIndexAsync(string indexName, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs index 98131fd..c51dc35 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs @@ -1,127 +1,99 @@ -using MongoDbGenericRepository.DataAccess.Create; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The interface exposing data insertion functionality for Key typed repositories. + /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. + /// Its constructor must be given a connection string and a database name. /// - /// - public interface IBaseMongoRepository_Create where TKey : IEquatable + public abstract partial class BaseMongoRepository : IBaseMongoRepository_Create + where TKey : IEquatable { - /// - /// Asynchronously adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The document you want to add. - /// An optional cancellation Token. - Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument; + private IMongoDbCreator _mongoDbCreator; /// - /// Adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. + /// The MongoDb accessor to insert data. /// - /// The type representing a Document. - /// The document you want to add. - void AddOne(TDocument document) where TDocument : IDocument; - - /// - /// Asynchronously adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The documents you want to add. - /// An optional cancellation Token. - Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument; - - /// - /// Adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The documents you want to add. - void AddMany(IEnumerable documents) where TDocument : IDocument; - } - - /// - /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. - /// Its constructor must be given a connection string and a database name. - /// - public abstract partial class BaseMongoRepository : IBaseMongoRepository_Create where TKey : IEquatable - { - private MongoDbCreator _mongoDbCreator; - - /// - /// The MongoDb accessor to insert data. - /// - protected virtual MongoDbCreator MongoDbCreator + protected virtual IMongoDbCreator MongoDbCreator { get { - if(_mongoDbCreator == null) + if (_mongoDbCreator == null) { lock (_initLock) { - if(_mongoDbCreator == null) + if (_mongoDbCreator == null) { _mongoDbCreator = new MongoDbCreator(MongoDbContext); } } } + return _mongoDbCreator; } - set { _mongoDbCreator = value; } + + set => _mongoDbCreator = value; } - /// - /// Asynchronously adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The document you want to add. - /// An optional cancellation Token. - public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument + /// + public virtual async Task AddOneAsync(TDocument document) + where TDocument : IDocument + { + await AddOneAsync(document, CancellationToken.None); + } + + /// + public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument { await MongoDbCreator.AddOneAsync(document, cancellationToken); } - /// - /// Adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The document you want to add. - public virtual void AddOne(TDocument document) where TDocument : IDocument + /// + public virtual void AddOne(TDocument document) + where TDocument : IDocument { - MongoDbCreator.AddOne(document); + AddOne(document, CancellationToken.None); } - /// - /// Asynchronously adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The documents you want to add. - /// An optional cancellation Token. - public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument + /// + public virtual void AddOne(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + { + MongoDbCreator.AddOne(document, cancellationToken); + } + + /// + public virtual async Task AddManyAsync(IEnumerable documents) + where TDocument : IDocument + { + await AddManyAsync(documents, CancellationToken.None); + } + + /// + public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument { await MongoDbCreator.AddManyAsync(documents, cancellationToken); } - /// - /// Adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The documents you want to add. - public virtual void AddMany(IEnumerable documents) where TDocument : IDocument + /// + public virtual void AddMany(IEnumerable documents) + where TDocument : IDocument { - MongoDbCreator.AddMany(documents); + AddMany(documents, CancellationToken.None); + } + + /// + public virtual void AddMany(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + { + MongoDbCreator.AddMany(documents, cancellationToken); } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs index 7324b2e..5010c08 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs @@ -1,108 +1,29 @@ -using MongoDbGenericRepository.DataAccess.Delete; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; +using MongoDbGenericRepository.DataAccess.Delete; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { - /// - /// The interface exposing deletion functionality for Key typed repositories. - /// - /// The type of the document Id. - public interface IBaseMongoRepository_Delete where TKey : IEquatable - { - /// - /// Deletes a document. - /// - /// The type representing a Document. - /// The document you want to delete. - /// The number of documents deleted. - long DeleteOne(TDocument document) - where TDocument : IDocument; - - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The document you want to delete. - /// The number of documents deleted. - Task DeleteOneAsync(TDocument document) - where TDocument : IDocument; - - /// - /// Deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - long DeleteOne(Expression> filter, string partitionKey = null) - where TDocument : IDocument; - - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - Task DeleteOneAsync(Expression> filter, string partitionKey = null) - where TDocument : IDocument; - - /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - Task DeleteManyAsync(Expression> filter, string partitionKey = null) - where TDocument : IDocument; - - /// - /// Asynchronously deletes a list of documents. - /// - /// The type representing a Document. - /// The list of documents to delete. - /// The number of documents deleted. - Task DeleteManyAsync(IEnumerable documents) - where TDocument : IDocument; - - /// - /// Deletes a list of documents. - /// - /// The type representing a Document. - /// The list of documents to delete. - /// The number of documents deleted. - long DeleteMany(IEnumerable documents) - where TDocument : IDocument; - - /// - /// Deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - long DeleteMany(Expression> filter, string partitionKey = null) - where TDocument : IDocument; - } - - public abstract partial class BaseMongoRepository: IBaseMongoRepository_Delete + public abstract partial class BaseMongoRepository : IBaseMongoRepository_Delete where TKey : IEquatable { - private MongoDbEraser _mongoDbEraser; + private IMongoDbEraser _mongoDbEraser; /// - /// The MongoDb accessor to delete data. + /// The MongoDb accessor to delete data. /// - protected virtual MongoDbEraser MongoDbEraser + protected virtual IMongoDbEraser MongoDbEraser { get { - if (_mongoDbEraser != null) { return _mongoDbEraser; } + if (_mongoDbEraser != null) + { + return _mongoDbEraser; + } lock (_initLock) { @@ -111,109 +32,184 @@ namespace MongoDbGenericRepository _mongoDbEraser = new MongoDbEraser(MongoDbContext); } } + return _mongoDbEraser; } - set { _mongoDbEraser = value; } + set => _mongoDbEraser = value; } - /// - /// Deletes a document. - /// - /// The type representing a Document. - /// The document you want to delete. - /// The number of documents deleted. + /// public virtual long DeleteOne(TDocument document) where TDocument : IDocument { - return MongoDbEraser.DeleteOne(document); + return DeleteOne(document, CancellationToken.None); } - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The document you want to delete. - /// The number of documents deleted. + /// + public virtual long DeleteOne(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbEraser.DeleteOne(document, cancellationToken); + } + + /// + public virtual long DeleteOne(Expression> filter) + where TDocument : IDocument + { + return DeleteOne(filter, null, CancellationToken.None); + } + + /// + public virtual long DeleteOne(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + { + return DeleteOne(filter, null, cancellationToken); + } + + /// + public virtual long DeleteOne(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return DeleteOne(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual long DeleteOne(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbEraser.DeleteOne(filter, partitionKey, cancellationToken); + } + + /// public virtual async Task DeleteOneAsync(TDocument document) where TDocument : IDocument { - return await MongoDbEraser.DeleteOneAsync(document); + return await DeleteOneAsync(document, CancellationToken.None); } - /// - /// Deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual long DeleteOne(Expression> filter, string partitionKey = null) + /// + public virtual async Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbEraser.DeleteOne(filter, partitionKey); + return await MongoDbEraser.DeleteOneAsync(document, cancellationToken); } - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey = null) + /// + public virtual async Task DeleteOneAsync(Expression> filter) where TDocument : IDocument { - return await MongoDbEraser.DeleteOneAsync(filter, partitionKey); + return await DeleteOneAsync(filter, null, CancellationToken.None); } - /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey = null) + /// + public virtual async Task DeleteOneAsync(Expression> filter, CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbEraser.DeleteManyAsync(filter, partitionKey); + return await DeleteOneAsync(filter, null, cancellationToken); } - /// - /// Asynchronously deletes a list of documents. - /// - /// The type representing a Document. - /// The list of documents to delete. - /// The number of documents deleted. + /// + public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return await DeleteOneAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task DeleteOneAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbEraser.DeleteOneAsync(filter, partitionKey, cancellationToken); + } + + /// public virtual async Task DeleteManyAsync(IEnumerable documents) where TDocument : IDocument { - return await MongoDbEraser.DeleteManyAsync(documents); + return await DeleteManyAsync(documents, CancellationToken.None); } - /// - /// Deletes a list of documents. - /// - /// The type representing a Document. - /// The list of documents to delete. - /// The number of documents deleted. + /// + public virtual async Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbEraser.DeleteManyAsync(documents, cancellationToken); + } + + /// + public async Task DeleteManyAsync(Expression> filter) + where TDocument : IDocument + { + return await DeleteManyAsync(filter, null, CancellationToken.None); + } + + /// + public async Task DeleteManyAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await DeleteManyAsync(filter, null, cancellationToken); + } + + /// + public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return await DeleteManyAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task DeleteManyAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbEraser.DeleteManyAsync(filter, partitionKey, cancellationToken); + } + + /// public virtual long DeleteMany(IEnumerable documents) where TDocument : IDocument { - return MongoDbEraser.DeleteMany(documents); + return DeleteMany(documents, CancellationToken.None); } - /// - /// Deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual long DeleteMany(Expression> filter, string partitionKey = null) + /// + public virtual long DeleteMany(IEnumerable documents, CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbEraser.DeleteMany(filter, partitionKey); + return MongoDbEraser.DeleteMany(documents, cancellationToken); + } + + /// + public virtual long DeleteMany(Expression> filter) + where TDocument : IDocument + { + return DeleteMany(filter, null, CancellationToken.None); + } + + /// + public virtual long DeleteMany(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + { + return DeleteMany(filter, null, cancellationToken); + } + + /// + public virtual long DeleteMany(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return DeleteMany(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual long DeleteMany(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbEraser.DeleteMany(filter, partitionKey, cancellationToken); } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs index c0847ba..485d899 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs @@ -1,119 +1,33 @@ -using MongoDbGenericRepository.DataAccess.Index; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The interface exposing index management functionality for Key typed repositories. - /// - /// - public interface IBaseMongoRepository_Index where TKey : IEquatable - { - /// - /// Returns the names of the indexes present on a collection. - /// - /// The type representing a Document. - /// An optional partition key - /// A list containing the names of the indexes on on the concerned collection. - Task> GetIndexesNamesAsync(string partitionKey = null) - where TDocument : IDocument; - - /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument; - - /// - /// Creates an index on the given field in ascending order. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument; - - /// - /// Creates an index on the given field in descending order. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument; - - /// - /// Creates a hashed index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument; - - /// - /// Creates a combined text index. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The fields we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument; - - /// - /// Drops the index given a field name - /// - /// The type representing a Document. - /// The name of the index - /// An optional partition key - Task DropIndexAsync(string indexName, string partitionKey = null) - where TDocument : IDocument; - } - - /// - /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. - /// Its constructor must be given a connection string and a database name. + /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. + /// Its constructor must be given a connection string and a database name. /// public abstract partial class BaseMongoRepository : IBaseMongoRepository_Index where TKey : IEquatable { - private MongoDbIndexHandler _mongoDbIndexHandler; + private IMongoDbIndexHandler _mongoDbIndexHandler; /// - /// The MongoDb accessor to manage indexes. + /// The MongoDb accessor to manage indexes. /// - protected virtual MongoDbIndexHandler MongoDbIndexHandler + protected virtual IMongoDbIndexHandler MongoDbIndexHandler { get { - if (_mongoDbIndexHandler != null) { return _mongoDbIndexHandler; } + if (_mongoDbIndexHandler != null) + { + return _mongoDbIndexHandler; + } lock (_initLock) { @@ -122,113 +36,423 @@ namespace MongoDbGenericRepository _mongoDbIndexHandler = new MongoDbIndexHandler(MongoDbContext); } } + return _mongoDbIndexHandler; } - set { _mongoDbIndexHandler = value; } + set => _mongoDbIndexHandler = value; } - /// - /// Returns the names of the indexes present on a collection. - /// - /// The type representing a Document. - /// An optional partition key - /// A list containing the names of the indexes on on the concerned collection. - public async virtual Task> GetIndexesNamesAsync(string partitionKey = null) + /// + public virtual async Task> GetIndexesNamesAsync() + where TDocument : IDocument + { + return await MongoDbIndexHandler.GetIndexesNamesAsync(); + } + + /// + public virtual async Task> GetIndexesNamesAsync(CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbIndexHandler.GetIndexesNamesAsync(cancellationToken: cancellationToken); + } + + /// + public virtual async Task> GetIndexesNamesAsync(string partitionKey) where TDocument : IDocument { return await MongoDbIndexHandler.GetIndexesNamesAsync(partitionKey); } - /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async virtual Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + /// + public virtual async Task> GetIndexesNamesAsync(string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbIndexHandler.CreateTextIndexAsync(field, indexCreationOptions, partitionKey); + return await MongoDbIndexHandler.GetIndexesNamesAsync(partitionKey, cancellationToken); } - /// - /// Creates an index on the given field in ascending order. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async virtual Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + /// + public virtual async Task CreateTextIndexAsync(Expression> field) where TDocument : IDocument { - return await MongoDbIndexHandler.CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey); + return await CreateTextIndexAsync(field, null, null, CancellationToken.None); } - /// - /// Creates an index on the given field in descending order. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async virtual Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + /// + public virtual async Task CreateTextIndexAsync(Expression> field, CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbIndexHandler.CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey); + return await CreateTextIndexAsync(field, null, null, cancellationToken); } - /// - /// Creates a hashed index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The field we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async virtual Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + /// + public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) where TDocument : IDocument { - return await MongoDbIndexHandler.CreateHashedIndexAsync(field, indexCreationOptions, partitionKey); + return await CreateTextIndexAsync(field, indexCreationOptions, null, CancellationToken.None); } - /// - /// Creates a combined text index. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. - /// - /// The type representing a Document. - /// The fields we want to index. - /// Options for creating an index. - /// An optional partition key. - /// The result of the create index operation. - public async virtual Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + /// + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbIndexHandler.CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey); + return await CreateTextIndexAsync(field, indexCreationOptions, null, cancellationToken); } - /// - /// Drops the index given a field name - /// - /// The type representing a Document. - /// The name of the index - /// An optional partition key - public async virtual Task DropIndexAsync(string indexName, string partitionKey = null) + /// + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) where TDocument : IDocument { - await MongoDbIndexHandler.DropIndexAsync(indexName, partitionKey); + return await CreateTextIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateTextIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + { + return await CreateTextIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateTextIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateTextIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbIndexHandler.CreateTextIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateAscendingIndexAsync(Expression> field) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, null, null, CancellationToken.None); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, null, null, cancellationToken); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public virtual async Task CreateAscendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbIndexHandler.CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync(Expression> field) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, null, null, CancellationToken.None); + } + + /// + public virtual async Task CreateDescendingIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, null, null, cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbIndexHandler.CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateHashedIndexAsync(Expression> field) + where TDocument : IDocument + { + return await CreateHashedIndexAsync(field, null, null, CancellationToken.None); + } + + /// + public virtual async Task CreateHashedIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateHashedIndexAsync(field, null, null, cancellationToken); + } + + /// + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + { + return await CreateHashedIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + /// + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateHashedIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public virtual async Task CreateHashedIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + { + return await CreateHashedIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateHashedIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateHashedIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + { + return await CreateHashedIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbIndexHandler.CreateHashedIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields) + where TDocument : IDocument + { + return await CreateCombinedTextIndexAsync(fields, null, null, CancellationToken.None); + } + + /// + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateCombinedTextIndexAsync(fields, null, null, cancellationToken); + } + + /// + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + { + return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, null, CancellationToken.None); + } + + /// + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, null, cancellationToken); + } + + /// + public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, string partitionKey) + where TDocument : IDocument + { + return await CreateCombinedTextIndexAsync(fields, null, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateCombinedTextIndexAsync(fields, null, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + { + return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbIndexHandler.CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public virtual async Task DropIndexAsync(string indexName) + where TDocument : IDocument + { + await DropIndexAsync(indexName, null, CancellationToken.None); + } + + /// + public virtual async Task DropIndexAsync(string indexName, CancellationToken cancellationToken) + where TDocument : IDocument + { + await DropIndexAsync(indexName, null, cancellationToken); + } + + /// + public virtual async Task DropIndexAsync(string indexName, string partitionKey) + where TDocument : IDocument + { + await DropIndexAsync(indexName, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task DropIndexAsync(string indexName, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + await MongoDbIndexHandler.DropIndexAsync(indexName, partitionKey, cancellationToken); } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Main.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Main.cs index ed47b0a..12b41fa 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Main.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Main.cs @@ -1,38 +1,23 @@ -using MongoDB.Driver; +using System; +using MongoDB.Driver; using MongoDbGenericRepository.Models; -using System; namespace MongoDbGenericRepository { /// - /// The interface exposing all the CRUD and Index functionalities for Key typed repositories. + /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. + /// Its constructor must be given a connection string and a database name. /// /// The type of the document Id. - public interface IBaseMongoRepository : - IReadOnlyMongoRepository, - IBaseMongoRepository_Create, - IBaseMongoRepository_Delete, - IBaseMongoRepository_Index, - IBaseMongoRepository_Update - where TKey : IEquatable - { - } - - /// - /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. - /// Its constructor must be given a connection string and a database name. - /// - /// The type of the document Id. - public abstract partial class BaseMongoRepository : - ReadOnlyMongoRepository, + public abstract partial class BaseMongoRepository : + ReadOnlyMongoRepository, IBaseMongoRepository where TKey : IEquatable { - private readonly object _initLock = new object(); /// - /// The constructor taking a connection string and a database name. + /// The constructor taking a connection string and a database name. /// /// The connection string of the MongoDb server. /// The name of the database against which you want to perform operations. @@ -41,23 +26,23 @@ namespace MongoDbGenericRepository } /// - /// The contructor taking a . + /// The constructor taking a . /// - /// A mongodb context implementing + /// A mongodb context implementing protected BaseMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } /// - /// The contructor taking a . + /// The constructor taking a . /// - /// A mongodb context implementing + /// A mongodb context implementing protected BaseMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase) { } /// - /// Gets a collections for a potentially partitioned document type. + /// Gets a collections for a potentially partitioned document type. /// /// The document type. /// The collection partition key. @@ -69,11 +54,12 @@ namespace MongoDbGenericRepository { return GetCollection(partitionKey); } + return GetCollection(); } /// - /// Gets a collections for the type TDocument with a partition key. + /// Gets a collections for the type TDocument with a partition key. /// /// The document type. /// The collection partition key. @@ -84,4 +70,4 @@ namespace MongoDbGenericRepository return MongoDbContext.GetCollection(partitionKey); } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs index cdbb862..2c670c5 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs @@ -1,43 +1,24 @@ -using MongoDB.Driver; -using MongoDbGenericRepository.DataAccess.Read; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Read; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. - /// Its constructor must be given a connection string and a database name. + /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. + /// Its constructor must be given a connection string and a database name. /// - public abstract partial class ReadOnlyMongoRepository : IReadOnlyMongoRepository where TKey : IEquatable + public abstract class ReadOnlyMongoRepository : IReadOnlyMongoRepository + where TKey : IEquatable { /// - /// The connection string. - /// - public string ConnectionString { get; protected set; } - - /// - /// The database name. - /// - public string DatabaseName { get; protected set; } - - /// - /// The MongoDbContext - /// - protected IMongoDbContext MongoDbContext = null; - - /// - /// A MongoDb Reader for read operations - /// - protected MongoDbReader MongoDbReader = null; - - /// - /// The constructor taking a connection string and a database name. + /// The constructor taking a connection string and a database name. /// /// The connection string of the MongoDb server. /// The name of the database against which you want to perform operations. @@ -47,35 +28,201 @@ namespace MongoDbGenericRepository } /// - /// The contructor taking a . + /// The constructor taking a . /// - /// A mongodb context implementing - protected ReadOnlyMongoRepository(IMongoDatabase mongoDatabase) : this(new MongoDbContext(mongoDatabase)) + /// A mongodb context implementing + protected ReadOnlyMongoRepository(IMongoDatabase mongoDatabase) + : this(new MongoDbContext(mongoDatabase)) { } /// - /// The contructor taking a . + /// The constructor taking a . /// - /// A mongodb context implementing + /// A mongodb context implementing protected ReadOnlyMongoRepository(IMongoDbContext mongoDbContext) { SetupMongoDbContext(mongoDbContext); } + /// + /// The MongoDbContext + /// + protected IMongoDbContext MongoDbContext { get; set; } + + /// + /// A MongoDb Reader for read operations + /// + protected IMongoDbReader MongoDbReader { get; set; } + + /// + /// The connection string. + /// + public string ConnectionString { get; protected set; } + + /// + /// The database name. + /// + public string DatabaseName { get; protected set; } + + /// + public virtual List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection) + where TDocument : IDocument + where TProjection : class, new() + { + return GroupBy(groupingCriteria, groupProjection, null, CancellationToken.None); + } + + /// + public virtual List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class, new() + { + return GroupBy(groupingCriteria, groupProjection, null, cancellationToken); + } + + /// + public virtual List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey) + where TDocument : IDocument + where TProjection : class, new() + { + return GroupBy(groupingCriteria, groupProjection, partitionKey, CancellationToken.None); + } + + /// + public virtual List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class, new() + { + return MongoDbReader.GroupBy(groupingCriteria, groupProjection, partitionKey, cancellationToken); + } + + /// + public virtual List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection) + where TDocument : IDocument + where TProjection : class, new() + { + return GroupBy(filter, groupingCriteria, groupProjection, null, CancellationToken.None); + } + + /// + public virtual List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class, new() + { + return GroupBy(filter, groupingCriteria, groupProjection, null, cancellationToken); + } + + /// + public virtual List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey) + where TDocument : IDocument + where TProjection : class, new() + { + return GroupBy(filter, groupingCriteria, groupProjection, partitionKey, CancellationToken.None); + } + + /// + public virtual List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class, new() + { + return MongoDbReader.GroupBy(filter, groupingCriteria, groupProjection, partitionKey, cancellationToken); + } + + /// + public virtual async Task> GetSortedPaginatedAsync( + Expression> filter, + Expression> sortSelector, + bool ascending = true, + int skipNumber = 0, + int takeNumber = 50, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + { + return await MongoDbReader.GetSortedPaginatedAsync( + filter, + sortSelector, + ascending, + skipNumber, + takeNumber, + partitionKey, + cancellationToken); + } + + /// + public virtual async Task> GetSortedPaginatedAsync( + Expression> filter, + SortDefinition sortDefinition, + int skipNumber = 0, + int takeNumber = 50, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + { + return await MongoDbReader.GetSortedPaginatedAsync( + filter, + sortDefinition, + skipNumber, + takeNumber, + partitionKey, + cancellationToken); + } + + /// + /// Setups the repository with a . + /// + /// protected void SetupMongoDbContext(IMongoDbContext mongoDbContext) { MongoDbContext = MongoDbContext ?? mongoDbContext; MongoDbReader = MongoDbReader ?? new MongoDbReader(MongoDbContext); } + /// + /// Setups the repository with a connection string and a database name. + /// + /// + /// + /// The database name. If the database name is null or whitespace it is taken from the + /// connection string + /// protected void SetupMongoDbContext(string connectionString, string databaseName) { - if (databaseName == null) + if (string.IsNullOrWhiteSpace(databaseName)) { var mongoUrl = new MongoUrl(connectionString); - databaseName = databaseName ?? mongoUrl.DatabaseName; + databaseName = mongoUrl.DatabaseName; } + ConnectionString = connectionString; DatabaseName = databaseName; SetupMongoDbContext(new MongoDbContext(connectionString, databaseName)); @@ -83,295 +230,697 @@ namespace MongoDbGenericRepository #region Read - /// - /// Asynchronously returns one document given its id. - /// - /// The type representing a Document. - /// The Id of the document you want to get. - /// An optional partition key. - public async Task GetByIdAsync(TKey id, string partitionKey = null) where TDocument : IDocument + /// + public async Task GetByIdAsync(TKey id) + where TDocument : IDocument { - return await MongoDbReader.GetByIdAsync(id, partitionKey); + return await GetByIdAsync(id, null, CancellationToken.None); } - /// - /// Returns one document given its id. - /// - /// The type representing a Document. - /// The Id of the document you want to get. - /// An optional partition key. - public TDocument GetById(TKey id, string partitionKey = null) where TDocument : IDocument + /// + public async Task GetByIdAsync(TKey id, CancellationToken cancellationToken) + where TDocument : IDocument { - return MongoDbReader.GetById(id, partitionKey); + return await GetByIdAsync(id, null, cancellationToken); } - /// - /// Asynchronously returns one document given an expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - public async Task GetOneAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument + /// + public async Task GetByIdAsync(TKey id, string partitionKey) + where TDocument : IDocument { - return await MongoDbReader.GetOneAsync(filter, partitionKey); + return await GetByIdAsync(id, partitionKey, CancellationToken.None); } - /// - /// Returns one document given an expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - public TDocument GetOne(Expression> filter, string partitionKey = null) where TDocument : IDocument + /// + public async Task GetByIdAsync(TKey id, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument { - return MongoDbReader.GetOne(filter, partitionKey); + return await MongoDbReader.GetByIdAsync(id, partitionKey, cancellationToken); } - /// - /// Returns a collection cursor. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - public IFindFluent GetCursor(Expression> filter, string partitionKey = null) where TDocument : IDocument + /// + public TDocument GetById(TKey id) + where TDocument : IDocument + { + return GetById(id, null, CancellationToken.None); + } + + /// + public TDocument GetById(TKey id, CancellationToken cancellationToken) + where TDocument : IDocument + { + return GetById(id, null, cancellationToken); + } + + /// + public TDocument GetById(TKey id, string partitionKey) + where TDocument : IDocument + { + return GetById(id, partitionKey, CancellationToken.None); + } + + /// + public TDocument GetById(TKey id, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbReader.GetById(id, partitionKey, cancellationToken); + } + + /// + public async Task GetOneAsync(Expression> filter) + where TDocument : IDocument + { + return await GetOneAsync(filter, null, CancellationToken.None); + } + + /// + public async Task GetOneAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await GetOneAsync(filter, null, cancellationToken); + } + + /// + public async Task GetOneAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return await GetOneAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public async Task GetOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbReader.GetOneAsync(filter, partitionKey, cancellationToken); + } + + /// + public TDocument GetOne(Expression> filter) + where TDocument : IDocument + { + return GetOne(filter, null, CancellationToken.None); + } + + /// + public TDocument GetOne(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + { + return GetOne(filter, null, cancellationToken); + } + + /// + public TDocument GetOne(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return GetOne(filter, partitionKey, CancellationToken.None); + } + + /// + public TDocument GetOne(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbReader.GetOne(filter, partitionKey, cancellationToken); + } + + /// + public IFindFluent GetCursor(Expression> filter, string partitionKey = null) + where TDocument : IDocument { return MongoDbReader.GetCursor(filter, partitionKey); } - /// - /// Returns true if any of the document of the collection matches the filter condition. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - public async Task AnyAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument - { - return await MongoDbReader.AnyAsync(filter, partitionKey); - } - - /// - /// Returns true if any of the document of the collection matches the filter condition. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - public bool Any(Expression> filter, string partitionKey = null) where TDocument : IDocument - { - return MongoDbReader.Any(filter, partitionKey); - } - - /// - /// Asynchronously returns a list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - public async Task> GetAllAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument - { - return await MongoDbReader.GetAllAsync(filter, partitionKey); - } - - /// - /// Returns a list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - public List GetAll(Expression> filter, string partitionKey = null) where TDocument : IDocument - { - return MongoDbReader.GetAll(filter, partitionKey); - } - - /// - /// Asynchronously counts how many documents match the filter condition. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partitionKey - public async Task CountAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument - { - return await MongoDbReader.CountAsync(filter, partitionKey); - } - - /// - /// Counts how many documents match the filter condition. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partitionKey - public long Count(Expression> filter, string partitionKey = null) where TDocument : IDocument - { - return MongoDbReader.Count(filter, partitionKey); - } - - /// - /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter. - /// - /// The document type. - /// A LINQ expression filter. - /// A property selector to order by descending. - /// An optional partitionKey. - public async Task GetByMaxAsync(Expression> filter, Expression> maxValueSelector, string partitionKey = null) + /// + public async Task AnyAsync(Expression> filter) where TDocument : IDocument { - return await MongoDbReader.GetByMaxAsync(filter, maxValueSelector, partitionKey); + return await AnyAsync(filter, null, CancellationToken.None); } - /// - /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter. - /// - /// The document type. - /// A LINQ expression filter. - /// A property selector to order by descending. - /// An optional partitionKey. - /// - public TDocument GetByMax(Expression> filter, Expression> maxValueSelector, string partitionKey = null) + /// + public async Task AnyAsync(Expression> filter, CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbReader.GetByMax(filter, maxValueSelector, partitionKey); + return await AnyAsync(filter, null, cancellationToken); } - /// - /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter. - /// - /// The document type. - /// A LINQ expression filter. - /// A property selector to order by ascending. - /// An optional partitionKey. - public async Task GetByMinAsync(Expression> filter, Expression> minValueSelector, string partitionKey = null) + /// + public async Task AnyAsync(Expression> filter, string partitionKey) where TDocument : IDocument { - return await MongoDbReader.GetByMinAsync(filter, minValueSelector, partitionKey); + return await AnyAsync(filter, partitionKey, CancellationToken.None); } - /// - /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter. - /// - /// The document type. - /// A LINQ expression filter. - /// A property selector to order by ascending. - /// An optional partitionKey. - public TDocument GetByMin(Expression> filter, Expression> minValueSelector, string partitionKey = null) + /// + public async Task AnyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbReader.GetByMin(filter, minValueSelector, partitionKey); + return await MongoDbReader.AnyAsync(filter, partitionKey, cancellationToken); } - /// - /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. - /// - /// The document type. - /// The type of the value used to order the query. - /// A LINQ expression filter. - /// A property selector to order by ascending. - /// An optional partitionKey. - public async Task GetMaxValueAsync(Expression> filter, Expression> maxValueSelector, string partitionKey = null) + /// + public bool Any(Expression> filter) where TDocument : IDocument { - return await MongoDbReader.GetMaxValueAsync(filter, maxValueSelector, partitionKey); + return Any(filter, null, CancellationToken.None); } - /// - /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. - /// - /// The document type. - /// The type of the value used to order the query. - /// A LINQ expression filter. - /// A property selector to order by ascending. - /// An optional partitionKey. - public TValue GetMaxValue(Expression> filter, Expression> maxValueSelector, string partitionKey = null) + /// + public bool Any(Expression> filter, CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbReader.GetMaxValue(filter, maxValueSelector, partitionKey); + return Any(filter, null, cancellationToken); } - /// - /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. - /// - /// The document type. - /// The type of the value used to order the query. - /// A LINQ expression filter. - /// A property selector to order by ascending. - /// An optional partition key. - public virtual async Task GetMinValueAsync(Expression> filter, Expression> minValueSelector, string partitionKey = null) + /// + public bool Any(Expression> filter, string partitionKey) where TDocument : IDocument { - return await MongoDbReader.GetMinValueAsync(filter, minValueSelector, partitionKey); + return Any(filter, partitionKey, CancellationToken.None); } - /// - /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. - /// - /// The document type. - /// The type of the value used to order the query. - /// A LINQ expression filter. - /// A property selector to order by ascending. - /// An optional partition key. - public virtual TValue GetMinValue(Expression> filter, Expression> minValueSelector, string partitionKey = null) + /// + public bool Any(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbReader.GetMinValue(filter, minValueSelector, partitionKey); + return MongoDbReader.Any(filter, partitionKey, cancellationToken); + } + + /// + public async Task> GetAllAsync(Expression> filter) + where TDocument : IDocument + { + return await GetAllAsync(filter, null, CancellationToken.None); + } + + /// + public async Task> GetAllAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await GetAllAsync(filter, null, cancellationToken); + } + + /// + public async Task> GetAllAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return await GetAllAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public async Task> GetAllAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbReader.GetAllAsync(filter, partitionKey, cancellationToken); + } + + /// + public List GetAll(Expression> filter) + where TDocument : IDocument + { + return GetAll(filter, null, CancellationToken.None); + } + + /// + public List GetAll(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + { + return GetAll(filter, null, cancellationToken); + } + + /// + public List GetAll(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return GetAll(filter, partitionKey, CancellationToken.None); + } + + /// + public List GetAll(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbReader.GetAll(filter, partitionKey, cancellationToken); + } + + /// + public async Task CountAsync(Expression> filter) + where TDocument : IDocument + { + return await CountAsync(filter, null, CancellationToken.None); + } + + /// + public async Task CountAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CountAsync(filter, null, cancellationToken); + } + + /// + public async Task CountAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return await CountAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public async Task CountAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbReader.CountAsync(filter, partitionKey, cancellationToken); + } + + /// + public long Count(Expression> filter) + where TDocument : IDocument + { + return Count(filter, null, CancellationToken.None); + } + + /// + public long Count(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + { + return Count(filter, null, cancellationToken); + } + + /// + public long Count(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return Count(filter, partitionKey, CancellationToken.None); + } + + /// + public long Count(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbReader.Count(filter, partitionKey, cancellationToken); + } + + + /// + public async Task GetByMaxAsync(Expression> filter, Expression> maxValueSelector) + where TDocument : IDocument + { + return await GetByMaxAsync(filter, maxValueSelector, null, CancellationToken.None); + } + + /// + public async Task GetByMaxAsync( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await GetByMaxAsync(filter, maxValueSelector, null, cancellationToken); + } + + /// + public async Task GetByMaxAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) + where TDocument : IDocument + { + return await GetByMaxAsync(filter, maxValueSelector, partitionKey, CancellationToken.None); + } + + /// + public async Task GetByMaxAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbReader.GetByMaxAsync(filter, maxValueSelector, partitionKey, cancellationToken); + } + + /// + public TDocument GetByMax(Expression> filter, Expression> maxValueSelector) + where TDocument : IDocument + { + return GetByMax(filter, maxValueSelector, null, CancellationToken.None); + } + + /// + public TDocument GetByMax( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return GetByMax(filter, maxValueSelector, null, cancellationToken); + } + + /// + public TDocument GetByMax( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) + where TDocument : IDocument + { + return GetByMax(filter, maxValueSelector, partitionKey, CancellationToken.None); + } + + /// + public TDocument GetByMax( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbReader.GetByMax(filter, maxValueSelector, partitionKey, cancellationToken); + } + + /// + public async Task GetByMinAsync(Expression> filter, Expression> minValueSelector) + where TDocument : IDocument + { + return await GetByMinAsync(filter, minValueSelector, null, CancellationToken.None); + } + + /// + public async Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await GetByMinAsync(filter, minValueSelector, null, cancellationToken); + } + + /// + public async Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey) + where TDocument : IDocument + { + return await GetByMinAsync(filter, minValueSelector, partitionKey, CancellationToken.None); + } + + /// + public async Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbReader.GetByMinAsync(filter, minValueSelector, partitionKey, cancellationToken); + } + + /// + public TDocument GetByMin(Expression> filter, Expression> minValueSelector) + where TDocument : IDocument + { + return GetByMin(filter, minValueSelector, null, CancellationToken.None); + } + + /// + public TDocument GetByMin( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return GetByMin(filter, minValueSelector, null, cancellationToken); + } + + /// + public TDocument GetByMin( + Expression> filter, + Expression> minValueSelector, + string partitionKey) + where TDocument : IDocument + { + return GetByMin(filter, minValueSelector, partitionKey, CancellationToken.None); + } + + /// + public TDocument GetByMin( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbReader.GetByMin(filter, minValueSelector, partitionKey, cancellationToken); + } + + /// + public async Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector) + where TDocument : IDocument + { + return await GetMaxValueAsync(filter, maxValueSelector, null, CancellationToken.None); + } + + /// + public async Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await GetMaxValueAsync(filter, maxValueSelector, null, cancellationToken); + } + + /// + public async Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) + where TDocument : IDocument + { + return await GetMaxValueAsync(filter, maxValueSelector, partitionKey, CancellationToken.None); + } + + /// + public async Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbReader.GetMaxValueAsync(filter, maxValueSelector, partitionKey, cancellationToken); + } + + /// + public TValue GetMaxValue(Expression> filter, Expression> maxValueSelector) + where TDocument : IDocument + { + return GetMaxValue(filter, maxValueSelector, null, CancellationToken.None); + } + + /// + public TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return GetMaxValue(filter, maxValueSelector, null, cancellationToken); + } + + /// + public TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) + where TDocument : IDocument + { + return GetMaxValue(filter, maxValueSelector, partitionKey, CancellationToken.None); + } + + /// + public TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbReader.GetMaxValue(filter, maxValueSelector, partitionKey, cancellationToken); + } + + /// + public virtual async Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector) + where TDocument : IDocument + { + return await GetMinValueAsync(filter, minValueSelector, null, CancellationToken.None); + } + + /// + public virtual async Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await GetMinValueAsync(filter, minValueSelector, null, cancellationToken); + } + + /// + public virtual async Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey) + where TDocument : IDocument + { + return await GetMinValueAsync(filter, minValueSelector, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbReader.GetMinValueAsync(filter, minValueSelector, partitionKey, cancellationToken); + } + + /// + public virtual TValue GetMinValue(Expression> filter, Expression> minValueSelector) + where TDocument : IDocument + { + return GetMinValue(filter, minValueSelector, null, CancellationToken.None); + } + + /// + public virtual TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return GetMinValue(filter, minValueSelector, null, cancellationToken); + } + + /// + public virtual TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + string partitionKey) + where TDocument : IDocument + { + return GetMinValue(filter, minValueSelector, partitionKey, CancellationToken.None); + } + + /// + public virtual TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbReader.GetMinValue(filter, minValueSelector, partitionKey, cancellationToken); } #endregion #region Maths - /// - /// Sums the values of a selected field for a given filtered collection of documents. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// The field you want to sum. - /// The partition key of your document, if any. - public virtual async Task SumByAsync(Expression> filter, - Expression> selector, - string partitionKey = null) + /// + public virtual async Task SumByAsync(Expression> filter, Expression> selector) where TDocument : IDocument { - return await MongoDbReader.SumByAsync(filter, selector, partitionKey); + return await SumByAsync(filter, selector, null, CancellationToken.None); } - /// - /// Sums the values of a selected field for a given filtered collection of documents. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// The field you want to sum. - /// The partition key of your document, if any. - public virtual async Task SumByAsync(Expression> filter, - Expression> selector, - string partitionKey = null) + /// + public virtual async Task SumByAsync( + Expression> filter, + Expression> selector, + CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbReader.SumByAsync(filter, selector, partitionKey); + return await SumByAsync(filter, selector, null, cancellationToken); } - /// - /// Sums the values of a selected field for a given filtered collection of documents. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// The field you want to sum. - /// The partition key of your document, if any. - public virtual int SumBy(Expression> filter, - Expression> selector, - string partitionKey = null) + /// + public virtual async Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey) + where TDocument : IDocument + { + return await SumByAsync(filter, selector, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbReader.SumByAsync(filter, selector, partitionKey, cancellationToken); + } + + /// + public virtual async Task SumByAsync(Expression> filter, Expression> selector) + where TDocument : IDocument + { + return await SumByAsync(filter, selector, null, CancellationToken.None); + } + + /// + public virtual async Task SumByAsync( + Expression> filter, + Expression> selector, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await SumByAsync(filter, selector, null, cancellationToken); + } + + /// + public virtual async Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey) + where TDocument : IDocument + { + return await SumByAsync(filter, selector, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbReader.SumByAsync(filter, selector, partitionKey, cancellationToken); + } + + /// + public virtual int SumBy(Expression> filter, Expression> selector, string partitionKey = null) where TDocument : IDocument { return MongoDbReader.SumBy(filter, selector, partitionKey); } - /// - /// Sums the values of a selected field for a given filtered collection of documents. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// The field you want to sum. - /// The partition key of your document, if any. - public virtual decimal SumBy(Expression> filter, - Expression> selector, - string partitionKey = null) + /// + public virtual decimal SumBy( + Expression> filter, + Expression> selector, + string partitionKey = null) where TDocument : IDocument { return MongoDbReader.SumBy(filter, selector, partitionKey); @@ -381,152 +930,183 @@ namespace MongoDbGenericRepository #region Project - /// - /// Asynchronously returns a projected document matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type representing the model you want to project to. - /// A LINQ expression filter. - /// The projection expression. - /// An optional partition key. - public virtual async Task ProjectOneAsync(Expression> filter, Expression> projection, string partitionKey = null) + /// + public virtual async Task ProjectOneAsync( + Expression> filter, + Expression> projection) where TDocument : IDocument where TProjection : class { - return await MongoDbReader.ProjectOneAsync(filter, projection, partitionKey); + return await ProjectOneAsync(filter, projection, null, CancellationToken.None); } - /// - /// Returns a projected document matching the filter condition. - /// - /// The type representing a Document. - /// The type representing the model you want to project to. - /// A LINQ expression filter. - /// The projection expression. - /// An optional partition key. - public virtual TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey = null) + /// + public virtual async Task ProjectOneAsync( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) where TDocument : IDocument where TProjection : class { - return MongoDbReader.ProjectOne(filter, projection, partitionKey); + return await ProjectOneAsync(filter, projection, null, cancellationToken); } - /// - /// Asynchronously returns a list of projected documents matching the filter condition. - /// - /// The type representing a Document. - /// The type representing the model you want to project to. - /// A LINQ expression filter. - /// The projection expression. - /// An optional partition key. - public virtual async Task> ProjectManyAsync(Expression> filter, Expression> projection, string partitionKey = null) + /// + public virtual async Task ProjectOneAsync( + Expression> filter, + Expression> projection, + string partitionKey) where TDocument : IDocument where TProjection : class { - return await MongoDbReader.ProjectManyAsync(filter, projection, partitionKey); + return await ProjectOneAsync(filter, projection, partitionKey, CancellationToken.None); } - /// - /// Asynchronously returns a list of projected documents matching the filter condition. - /// - /// The type representing a Document. - /// The type representing the model you want to project to. - /// A LINQ expression filter. - /// The projection expression. - /// An optional partition key. - public virtual List ProjectMany(Expression> filter, Expression> projection, string partitionKey = null) + /// + public virtual async Task ProjectOneAsync( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TProjection : class { - return MongoDbReader.ProjectMany(filter, projection, partitionKey); + return await MongoDbReader.ProjectOneAsync(filter, projection, partitionKey, cancellationToken); } + /// + public virtual TProjection ProjectOne( + Expression> filter, + Expression> projection) + where TDocument : IDocument + where TProjection : class + { + return ProjectOne(filter, projection, null, CancellationToken.None); + } + + /// + public virtual TProjection ProjectOne( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class + { + return ProjectOne(filter, projection, null, cancellationToken); + } + + + /// + public virtual TProjection ProjectOne( + Expression> filter, + Expression> projection, + string partitionKey) + where TDocument : IDocument + where TProjection : class + { + return ProjectOne(filter, projection, partitionKey, CancellationToken.None); + } + + /// + public virtual TProjection ProjectOne( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class + { + return MongoDbReader.ProjectOne(filter, projection, partitionKey, cancellationToken); + } + + /// + public virtual async Task> ProjectManyAsync( + Expression> filter, + Expression> projection) + where TDocument : IDocument + where TProjection : class + { + return await ProjectManyAsync(filter, projection, null, CancellationToken.None); + } + + /// + public virtual async Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class + { + return await ProjectManyAsync(filter, projection, null, cancellationToken); + } + + /// + public virtual async Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + string partitionKey) + where TDocument : IDocument + where TProjection : class + { + return await ProjectManyAsync(filter, projection, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class + { + return await MongoDbReader.ProjectManyAsync(filter, projection, partitionKey, cancellationToken); + } + + /// + public virtual List ProjectMany( + Expression> filter, + Expression> projection) + where TDocument : IDocument + where TProjection : class + { + return ProjectMany(filter, projection, null, CancellationToken.None); + } + + /// + public virtual List ProjectMany( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class + { + return ProjectMany(filter, projection, null, cancellationToken); + } + + /// + public virtual List ProjectMany( + Expression> filter, + Expression> projection, + string partitionKey) + where TDocument : IDocument + where TProjection : class + { + return ProjectMany(filter, projection, partitionKey, CancellationToken.None); + } + + /// + public virtual List ProjectMany( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TProjection : class + { + return MongoDbReader.ProjectMany(filter, projection, partitionKey, cancellationToken); + } #endregion Project - - /// - /// Groups a collection of documents given a grouping criteria, - /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. - /// - /// The type representing a Document. - /// The type of the grouping criteria. - /// The type of the projected group. - /// The grouping criteria. - /// The projected group result. - /// The partition key of your document, if any. - public virtual List GroupBy( - Expression> groupingCriteria, - Expression, TProjection>> groupProjection, - string partitionKey = null) - where TDocument : IDocument - where TProjection : class, new() - { - return MongoDbReader.GroupBy(groupingCriteria, groupProjection, partitionKey); - } - - /// - /// Groups filtered a collection of documents given a grouping criteria, - /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. - /// - /// The type representing a Document. - /// The type of the grouping criteria. - /// The type of the projected group. - /// A LINQ expression filter. - /// The grouping criteria. - /// The projected group result. - /// The partition key of your document, if any. - public virtual List GroupBy( - Expression> filter, - Expression> groupingCriteria, - Expression, TProjection>> groupProjection, - string partitionKey = null) - where TDocument : IDocument - where TProjection : class, new() - { - return MongoDbReader.GroupBy(filter, groupingCriteria, groupProjection, partitionKey); - } - - /// - /// Asynchronously returns a paginated list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// The property selector. - /// Order of the sorting. - /// The number of documents you want to skip. Default value is 0. - /// The number of documents you want to take. Default value is 50. - /// An optional partition key. - public virtual async Task> GetSortedPaginatedAsync( - Expression> filter, - Expression> sortSelector, - bool ascending = true, - int skipNumber = 0, - int takeNumber = 50, - string partitionKey = null) - where TDocument : IDocument - { - return await MongoDbReader.GetSortedPaginatedAsync(filter, sortSelector, ascending, skipNumber, takeNumber, partitionKey); - } - - /// - /// Asynchronously returns a paginated list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// The sort definition. - /// The number of documents you want to skip. Default value is 0. - /// The number of documents you want to take. Default value is 50. - /// An optional partition key. - public virtual async Task> GetSortedPaginatedAsync( - Expression> filter, - SortDefinition sortDefinition, - int skipNumber = 0, - int takeNumber = 50, - string partitionKey = null) - where TDocument : IDocument - { - return await MongoDbReader.GetSortedPaginatedAsync(filter, sortDefinition, skipNumber, takeNumber, partitionKey); - } } } \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs index b56d9c4..1dd1870 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs @@ -1,214 +1,29 @@ -using MongoDB.Driver; +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using MongoDB.Driver; using MongoDbGenericRepository.DataAccess.Update; using MongoDbGenericRepository.Models; -using System; -using System.Linq.Expressions; -using System.Threading.Tasks; namespace MongoDbGenericRepository { - public interface IBaseMongoRepository_Update where TKey : IEquatable - { - /// - /// Asynchronously Updates a document. - /// - /// The type representing a Document. - /// The document with the modifications you want to persist. - Task UpdateOneAsync(TDocument modifiedDocument) where TDocument : IDocument; - - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The document with the modifications you want to persist. - bool UpdateOne(TDocument modifiedDocument) where TDocument : IDocument; - - /// - /// Takes a document you want to modify and applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document you want to modify. - /// The update definition for the document. - Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update) - where TDocument : IDocument; - - /// - /// Takes a document you want to modify and applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document you want to modify. - /// The update definition for the document. - bool UpdateOne(TDocument documentToModify, UpdateDefinition update) - where TDocument : IDocument; - - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the field. - /// The document you want to modify. - /// The field selector. - /// The new value of the property field. - bool UpdateOne(TDocument documentToModify, Expression> field, TField value) - where TDocument : IDocument; - - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the field. - /// The document you want to modify. - /// The field selector. - /// The new value of the property field. - Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value) - where TDocument : IDocument; - - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) - where TDocument : IDocument; - - /// - /// For the entity selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey = null) - where TDocument : IDocument; - - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) - where TDocument : IDocument; - - /// - /// For the entity selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) - where TDocument : IDocument; - - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) - where TDocument : IDocument; - - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) - where TDocument : IDocument; - - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) - where TDocument : IDocument; - - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) - where TDocument : IDocument; - - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey = null) - where TDocument : IDocument; - - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) - where TDocument : IDocument; - - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) - where TDocument : IDocument; - - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) - where TDocument : IDocument; - } - public abstract partial class BaseMongoRepository : IBaseMongoRepository_Update where TKey : IEquatable { - private MongoDbUpdater _mongoDbUpdater; - protected virtual MongoDbUpdater MongoDbUpdater + private IMongoDbUpdater _mongoDbUpdater; + + /// + /// The MongoDb accessor to update data. + /// + protected virtual IMongoDbUpdater MongoDbUpdater { get { - if (_mongoDbUpdater != null) { return _mongoDbUpdater; } + if (_mongoDbUpdater != null) + { + return _mongoDbUpdater; + } lock (_initLock) { @@ -220,250 +35,588 @@ namespace MongoDbGenericRepository return _mongoDbUpdater; } - set { _mongoDbUpdater = value; } + set => _mongoDbUpdater = value; } - /// - /// Asynchronously Updates a document. - /// - /// The type representing a Document. - /// The document with the modifications you want to persist. - public virtual async Task UpdateOneAsync(TDocument modifiedDocument) where TDocument : IDocument + /// + public virtual async Task UpdateOneAsync(TDocument modifiedDocument) + where TDocument : IDocument { - return await MongoDbUpdater.UpdateOneAsync(modifiedDocument); + return await UpdateOneAsync(modifiedDocument, CancellationToken.None); } - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The document with the modifications you want to persist. - public virtual bool UpdateOne(TDocument modifiedDocument) where TDocument : IDocument + /// + public virtual async Task UpdateOneAsync(TDocument modifiedDocument, CancellationToken cancellationToken) + where TDocument : IDocument { - return MongoDbUpdater.UpdateOne(modifiedDocument); + return await MongoDbUpdater.UpdateOneAsync(modifiedDocument, cancellationToken); } - /// - /// Takes a document you want to modify and applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document you want to modify. - /// The update definition for the document. + /// + public virtual bool UpdateOne(TDocument modifiedDocument) + where TDocument : IDocument + { + return UpdateOne(modifiedDocument, CancellationToken.None); + } + + /// + public virtual bool UpdateOne(TDocument modifiedDocument, CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateOne(modifiedDocument, cancellationToken); + } + + /// public virtual async Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update) where TDocument : IDocument { - return await MongoDbUpdater.UpdateOneAsync(documentToModify, update); + return await UpdateOneAsync(documentToModify, update, CancellationToken.None); } - /// - /// Takes a document you want to modify and applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document you want to modify. - /// The update definition for the document. + /// + public virtual async Task UpdateOneAsync( + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateOneAsync(documentToModify, update, cancellationToken); + } + + /// public virtual bool UpdateOne(TDocument documentToModify, UpdateDefinition update) where TDocument : IDocument { - return MongoDbUpdater.UpdateOne(documentToModify, update); + return UpdateOne(documentToModify, update, CancellationToken.None); } - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the field. - /// The document you want to modify. - /// The field selector. - /// The new value of the property field. + /// + public virtual bool UpdateOne(TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateOne(documentToModify, update, cancellationToken); + } + + /// public virtual bool UpdateOne(TDocument documentToModify, Expression> field, TField value) where TDocument : IDocument { - return MongoDbUpdater.UpdateOne(documentToModify, field, value); + return UpdateOne(documentToModify, field, value, CancellationToken.None); } - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the field. - /// The document you want to modify. - /// The field selector. - /// The new value of the property field. + /// + public virtual bool UpdateOne( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateOne(documentToModify, field, value, cancellationToken); + } + + /// public virtual async Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value) where TDocument : IDocument { - return await MongoDbUpdater.UpdateOneAsync(documentToModify, field, value); + return await UpdateOneAsync(documentToModify, field, value, CancellationToken.None); } - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual async Task UpdateOneAsync( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey); + return await MongoDbUpdater.UpdateOneAsync(documentToModify, field, value, cancellationToken); } - /// - /// For the entity selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value) where TDocument : IDocument { - return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey); + return UpdateOne(filter, field, value, null, CancellationToken.None); } - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey); + return UpdateOne(filter, field, value, null, cancellationToken); } - /// - /// For the entity selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - public virtual async Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument { - return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey); + return UpdateOne(filter, field, value, partitionKey, CancellationToken.None); } - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - public virtual async Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey); + return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey, cancellationToken); } - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual async Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne(Expression> filter, Expression> field, TField value) where TDocument : IDocument { - return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey); + return UpdateOne(filter, field, value, null, CancellationToken.None); } - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + /// + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey); + return UpdateOne(filter, field, value, null, cancellationToken); } - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - public virtual async Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) + /// + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument { - return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey); + return UpdateOne(filter, field, value, partitionKey, CancellationToken.None); } - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - public virtual long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey); + return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey, cancellationToken); } - /// - /// For the entities selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + /// + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value) where TDocument : IDocument { - return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey); + return await UpdateOneAsync(filter, field, value, null, CancellationToken.None); } - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + /// + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey); + return await UpdateOneAsync(filter, field, value, null, cancellationToken); } - /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The document filter. - /// The update definition to apply. - /// The value of the partition key. - public virtual long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) where TDocument : IDocument + /// + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument { - return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey); + return await UpdateOneAsync(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual async Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value) + where TDocument : IDocument + { + return await UpdateOneAsync(filter, field, value, null, CancellationToken.None); + } + + /// + public virtual async Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await UpdateOneAsync(filter, field, value, null, cancellationToken); + } + + /// + public virtual async Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + { + return await UpdateOneAsync(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, field, value, null, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, field, value, null, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, field, value, null, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, field, value, null, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, updateDefinition, null, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, updateDefinition, null, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, updateDefinition, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, updateDefinition, null, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, updateDefinition, null, cancellationToken); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey) + where TDocument : IDocument + { + return await UpdateManyAsync(filter, updateDefinition, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task UpdateManyAsync( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey, cancellationToken); + } + + /// + public virtual long UpdateMany(Expression> filter, Expression> field, TField value) + where TDocument : IDocument + { + return UpdateMany(filter, field, value, null, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return UpdateMany(filter, field, value, null, cancellationToken); + } + + /// + public virtual long UpdateMany( + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + { + return UpdateMany(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, field, value, null, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, field, value, null, cancellationToken); + } + + /// + public virtual long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey, cancellationToken); + } + + /// + public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition) + where TDocument : IDocument + { + return UpdateMany(filter, updateDefinition, null, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + FilterDefinition filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return UpdateMany(filter, updateDefinition, null, cancellationToken); + } + + /// + public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey) + where TDocument : IDocument + { + return UpdateMany(filter, updateDefinition, partitionKey, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey, cancellationToken); + } + + /// + public virtual long UpdateMany(Expression> filter, UpdateDefinition updateDefinition) + where TDocument : IDocument + { + return UpdateMany(filter, updateDefinition, null, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return UpdateMany(filter, updateDefinition, null, cancellationToken); + } + + /// + public virtual long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey) + where TDocument : IDocument + { + return UpdateMany(filter, updateDefinition, partitionKey, CancellationToken.None); + } + + /// + public virtual long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey, cancellationToken); } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Create.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Create.cs new file mode 100644 index 0000000..bce8e62 --- /dev/null +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Create.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository +{ + /// + /// The interface exposing data insertion functionality for Key typed repositories. + /// + /// + public interface IBaseMongoRepository_Create + where TKey : IEquatable + { + /// + /// Asynchronously adds a document to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The document you want to add. + Task AddOneAsync(TDocument document) + where TDocument : IDocument; + + /// + /// Asynchronously adds a document to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The document you want to add. + /// The cancellation Token. + Task AddOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Adds a document to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The document you want to add. + void AddOne(TDocument document) + where TDocument : IDocument; + + /// + /// Adds a document to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The document you want to add. + /// The cancellation token. + void AddOne(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Asynchronously adds a list of documents to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The documents you want to add. + Task AddManyAsync(IEnumerable documents) + where TDocument : IDocument; + + /// + /// Asynchronously adds a list of documents to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The documents you want to add. + /// An optional cancellation Token. + Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Adds a list of documents to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The documents you want to add. + void AddMany(IEnumerable documents) + where TDocument : IDocument; + + + /// + /// Adds a list of documents to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The documents you want to add. + /// The cancellation token. + void AddMany(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs new file mode 100644 index 0000000..32d269d --- /dev/null +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs @@ -0,0 +1,254 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository +{ + /// + /// The interface exposing deletion functionality for Key typed repositories. + /// + /// The type of the document Id. + public interface IBaseMongoRepository_Delete + where TKey : IEquatable + { + /// + /// Deletes a document. + /// + /// The type representing a Document. + /// The document you want to delete. + /// The number of documents deleted. + long DeleteOne(TDocument document) + where TDocument : IDocument; + + /// + /// Deletes a document. + /// + /// The type representing a Document. + /// The document you want to delete. + /// The cancellation token. + /// The number of documents deleted. + long DeleteOne(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The number of documents deleted. + long DeleteOne(Expression> filter) + where TDocument : IDocument; + + /// + /// Deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The cancellation token. + /// The number of documents deleted. + long DeleteOne(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The number of documents deleted. + long DeleteOne(Expression> filter, string partitionKey) + where TDocument : IDocument; + + /// + /// Deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token. + /// The number of documents deleted. + long DeleteOne(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The document you want to delete. + /// The number of documents deleted. + Task DeleteOneAsync(TDocument document) + where TDocument : IDocument; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The document you want to delete. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument; + + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The number of documents deleted. + Task DeleteOneAsync(Expression> filter) + where TDocument : IDocument; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteOneAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The number of documents deleted. + Task DeleteOneAsync(Expression> filter, string partitionKey) + where TDocument : IDocument; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The number of documents deleted. + Task DeleteManyAsync(Expression> filter) + where TDocument : IDocument; + + /// + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteManyAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The number of documents deleted. + Task DeleteManyAsync(Expression> filter, string partitionKey) + where TDocument : IDocument; + + /// + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Asynchronously deletes a list of documents. + /// + /// The type representing a Document. + /// The list of documents to delete. + /// The number of documents deleted. + Task DeleteManyAsync(IEnumerable documents) + where TDocument : IDocument; + + /// + /// Asynchronously deletes a list of documents. + /// + /// The type representing a Document. + /// The list of documents to delete. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Deletes a list of documents. + /// + /// The type representing a Document. + /// The list of documents to delete. + /// The number of documents deleted. + long DeleteMany(IEnumerable documents) + where TDocument : IDocument; + + /// + /// Deletes a list of documents. + /// + /// The type representing a Document. + /// The list of documents to delete. + /// The cancellation token. + /// The number of documents deleted. + long DeleteMany(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The number of documents deleted. + long DeleteMany(Expression> filter) + where TDocument : IDocument; + + /// + /// Deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The cancellation token + /// The number of documents deleted. + long DeleteMany(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The number of documents deleted. + long DeleteMany(Expression> filter, string partitionKey) + where TDocument : IDocument; + + /// + /// Deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// + /// The number of documents deleted. + long DeleteMany(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs new file mode 100644 index 0000000..f82e6fa --- /dev/null +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs @@ -0,0 +1,640 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository +{ + /// + /// The interface exposing index management functionality for Key typed repositories. + /// + /// + public interface IBaseMongoRepository_Index + where TKey : IEquatable + { + /// + /// Returns the names of the indexes present on a collection. + /// + /// The type representing a Document. + /// A list containing the names of the indexes on on the concerned collection. + Task> GetIndexesNamesAsync() + where TDocument : IDocument; + + /// + /// Returns the names of the indexes present on a collection. + /// + /// The type representing a Document. + /// The cancellation token. + /// A list containing the names of the indexes on on the concerned collection. + Task> GetIndexesNamesAsync(CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Returns the names of the indexes present on a collection. + /// + /// The type representing a Document. + /// An optional partition key + /// A list containing the names of the indexes on on the concerned collection. + Task> GetIndexesNamesAsync(string partitionKey) + where TDocument : IDocument; + + /// + /// Returns the names of the indexes present on a collection. + /// + /// The type representing a Document. + /// An optional partition key + /// The cancellation token + /// A list containing the names of the indexes on on the concerned collection. + Task> GetIndexesNamesAsync(string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// The result of the create index operation. + Task CreateTextIndexAsync(Expression> field) + where TDocument : IDocument; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateTextIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// The result of the create index operation. + Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateTextIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateTextIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + where TDocument : IDocument; + + /// + /// Create a text index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// The result of the create index operation. + Task CreateHashedIndexAsync(Expression> field) + where TDocument : IDocument; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateHashedIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// The result of the create index operation. + Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateHashedIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateHashedIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument; + + /// + /// Creates a hashed index on the given field. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The fields we want to index. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync(IEnumerable>> fields) + where TDocument : IDocument; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The fields we want to index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync(IEnumerable>> fields, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The fields we want to index. + /// Options for creating an index. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The fields we want to index. + /// Options for creating an index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The fields we want to index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync(IEnumerable>> fields, string partitionKey) + where TDocument : IDocument; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The fields we want to index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The fields we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument; + + /// + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The fields we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Drops the index given a field name + /// + /// The type representing a Document. + /// The name of the index + Task DropIndexAsync(string indexName) + where TDocument : IDocument; + + /// + /// Drops the index given a field name + /// + /// The type representing a Document. + /// The name of the index + /// The cancellation token. + Task DropIndexAsync(string indexName, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Drops the index given a field name + /// + /// The type representing a Document. + /// The name of the index + /// An optional partition key + Task DropIndexAsync(string indexName, string partitionKey) + where TDocument : IDocument; + + /// + /// Drops the index given a field name + /// + /// The type representing a Document. + /// The name of the index + /// An optional partition key + /// The cancellation token. + Task DropIndexAsync(string indexName, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Update.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Update.cs new file mode 100644 index 0000000..b7c4220 --- /dev/null +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Update.cs @@ -0,0 +1,822 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository +{ + /// + /// The base Mongo Repository Update interface. used to update documents in the collections. + /// + /// + public interface IBaseMongoRepository_Update + where TKey : IEquatable + { + /// + /// Asynchronously Updates a document. + /// + /// The type representing a Document. + /// The document with the modifications you want to persist. + Task UpdateOneAsync(TDocument modifiedDocument) + where TDocument : IDocument; + + /// + /// Asynchronously Updates a document. + /// + /// The type representing a Document. + /// The document with the modifications you want to persist. + /// The cancellation token. + Task UpdateOneAsync(TDocument modifiedDocument, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The document with the modifications you want to persist. + bool UpdateOne(TDocument modifiedDocument) + where TDocument : IDocument; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The document with the modifications you want to persist. + /// The cancellation token. + bool UpdateOne(TDocument modifiedDocument, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Takes a document you want to modify and applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document you want to modify. + /// The update definition for the document. + Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update) + where TDocument : IDocument; + + /// + /// Takes a document you want to modify and applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document you want to modify. + /// The update definition for the document. + /// The cancellation token. + Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Takes a document you want to modify and applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document you want to modify. + /// The update definition for the document. + bool UpdateOne(TDocument documentToModify, UpdateDefinition update) + where TDocument : IDocument; + + /// + /// Takes a document you want to modify and applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document you want to modify. + /// The update definition for the document. + /// The cancellation token. + bool UpdateOne(TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the field. + /// The document you want to modify. + /// The field selector. + /// The new value of the property field. + bool UpdateOne(TDocument documentToModify, Expression> field, TField value) + where TDocument : IDocument; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the field. + /// The document you want to modify. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + bool UpdateOne( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the field. + /// The document you want to modify. + /// The field selector. + /// The new value of the property field. + Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value) + where TDocument : IDocument; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the field. + /// The document you want to modify. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + Task UpdateOneAsync( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + bool UpdateOne(FilterDefinition filter, Expression> field, TField value) + where TDocument : IDocument; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + /// The cancellation token. + bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entity selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + bool UpdateOne( + Expression> filter, + Expression> field, + TField value) + where TDocument : IDocument; + + /// + /// For the entity selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entity selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument; + + /// + /// For the entity selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + /// The cancellation token. + bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value) + where TDocument : IDocument; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + /// The cancellation token. + Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entity selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + Task UpdateOneAsync(Expression> filter, Expression> field, TField value) + where TDocument : IDocument; + + /// + /// For the entity selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entity selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument; + + /// + /// For the entity selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + /// The cancellation token. + Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + Task UpdateManyAsync(Expression> filter, Expression> field, TField value) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + /// The cancellation token. + Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + /// The cancellation token. + Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The cancellation token. + Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + /// The cancellation token. + Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The cancellation token. + Task UpdateManyAsync( + Expression> filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, string partitionKey) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + /// The cancellation token. + Task UpdateManyAsync( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + long UpdateMany( + Expression> filter, + Expression> field, + TField value) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + long UpdateMany( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + long UpdateMany( + Expression> filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + /// The cancellation token. + long UpdateMany( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The cancellation token. + long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + /// The cancellation token. + long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The cancellation token. + long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + /// The cancellation token. + long UpdateMany( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + long UpdateMany(Expression> filter, UpdateDefinition updateDefinition) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The cancellation token. + long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + /// The cancellation token. + long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.cs new file mode 100644 index 0000000..f290c27 --- /dev/null +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.cs @@ -0,0 +1,18 @@ +using System; + +namespace MongoDbGenericRepository +{ + /// + /// The interface exposing all the CRUD and Index functionalities for Key typed repositories. + /// + /// The type of the document Id. + public interface IBaseMongoRepository : + IReadOnlyMongoRepository, + IBaseMongoRepository_Create, + IBaseMongoRepository_Delete, + IBaseMongoRepository_Index, + IBaseMongoRepository_Update + where TKey : IEquatable + { + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/Models/IndexCreationOptions.cs b/MongoDbGenericRepository/Models/IndexCreationOptions.cs index ad2d60c..669798c 100644 --- a/MongoDbGenericRepository/Models/IndexCreationOptions.cs +++ b/MongoDbGenericRepository/Models/IndexCreationOptions.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace MongoDbGenericRepository.Models { diff --git a/MongoDbGenericRepository/MongoDbGenericRepository.xml b/MongoDbGenericRepository/MongoDbGenericRepository.xml index 9805841..cc35bd8 100644 --- a/MongoDbGenericRepository/MongoDbGenericRepository.xml +++ b/MongoDbGenericRepository/MongoDbGenericRepository.xml @@ -2364,13 +2364,13 @@ - The contructor taking a . + The constructor taking a . A mongodb context implementing - The contructor taking a . + The constructor taking a . A mongodb context implementing @@ -3360,13 +3360,13 @@ - The contructor taking a . + The constructor taking a . A mongodb context implementing - The contructor taking a . + The constructor taking a . A mongodb context implementing @@ -3757,13 +3757,13 @@ - The contructor taking a . + The constructor taking a . A mongodb context implementing - The contructor taking a . + The constructor taking a . A mongodb context implementing @@ -4492,13 +4492,13 @@ - The contructor taking a . + The constructor taking a . A mongodb context implementing - The contructor taking a . + The constructor taking a . A mongodb context implementing diff --git a/MongoDbGenericRepository/ReadOnlyMongoRepository.cs b/MongoDbGenericRepository/ReadOnlyMongoRepository.cs index 1959003..54913fc 100644 --- a/MongoDbGenericRepository/ReadOnlyMongoRepository.cs +++ b/MongoDbGenericRepository/ReadOnlyMongoRepository.cs @@ -1,23 +1,22 @@ -using MongoDB.Driver; -using MongoDbGenericRepository.DataAccess.Read; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. - /// Its constructor must be given a connection string and a database name. + /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. + /// Its constructor must be given a connection string and a database name. /// public abstract class ReadOnlyMongoRepository : ReadOnlyMongoRepository, IReadOnlyMongoRepository { /// - /// The constructor taking a connection string and a database name. + /// The constructor taking a connection string and a database name. /// /// The connection string of the MongoDb server. /// The name of the database against which you want to perform operations. @@ -26,717 +25,24 @@ namespace MongoDbGenericRepository } /// - /// The contructor taking a . + /// The constructor taking a . /// - /// A mongodb context implementing + /// A mongodb context implementing protected ReadOnlyMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } /// - /// The contructor taking a . + /// The constructor taking a . /// - /// A mongodb context implementing + /// A mongodb context implementing protected ReadOnlyMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase) { } - #region Read TKey /// - /// Asynchronously returns one document given its id. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The Id of the document you want to get. - /// An optional partition key. - /// An optional cancellation Token. - public async virtual Task GetByIdAsync(TKey id, string partitionKey = null, CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbReader.GetByIdAsync(id, partitionKey, cancellationToken); - } - - /// - /// Returns one document given its id. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The Id of the document you want to get. - /// An optional partition key. - public virtual TDocument GetById(TKey id, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return MongoDbReader.GetById(id, partitionKey); - } - - /// - /// Asynchronously returns one document given filter definition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A mongodb filter definition. - /// A mongodb filter option. - /// An optional partition key. - /// An optional cancellation Token. - public Task GetOneAsync(FilterDefinition condition, FindOptions findOption = null, string partitionKey = null, - CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable - { - return MongoDbReader.GetOneAsync(condition, findOption, partitionKey, cancellationToken); - } - - /// - /// Returns one document given filter definition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A mongodb filter definition. - /// A mongodb filter option. - /// An optional partition key. - public TDocument GetOne(FilterDefinition condition, FindOptions findOption = null, - string partitionKey = null) where TDocument : IDocument where TKey : IEquatable - { - return MongoDbReader.GetOne(condition, findOption, partitionKey); - } - - /// - /// Asynchronously returns one document given an expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// An optional cancellation Token. - public async virtual Task GetOneAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbReader.GetOneAsync(filter, partitionKey, cancellationToken); - } - - /// - /// Returns one document given an expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - public virtual TDocument GetOne(Expression> filter, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return MongoDbReader.GetOne(filter, partitionKey); - } - - /// - /// Returns a collection cursor. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - public virtual IFindFluent GetCursor(Expression> filter, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return MongoDbReader.GetCursor(filter, partitionKey); - } - - /// - /// Returns true if any of the document of the collection matches the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A mongodb filter definition. - /// A mongodb counting option. - /// An optional partition key. - /// An optional cancellation Token. - public Task AnyAsync(FilterDefinition condition, CountOptions countOption = null, string partitionKey = null, - CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable - { - return MongoDbReader.AnyAsync(condition, countOption, partitionKey, cancellationToken); - } - - /// - /// Returns true if any of the document of the collection matches the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A mongodb filter definition. - /// A mongodb counting option. - /// An optional partition key. - public bool Any(FilterDefinition condition, CountOptions countOption = null, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable - { - return MongoDbReader.Any(condition, countOption, partitionKey); - } - - /// - /// Returns true if any of the document of the collection matches the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// An optional cancellation Token. - public async virtual Task AnyAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbReader.AnyAsync(filter, partitionKey, cancellationToken); - } - - /// - /// Returns true if any of the document of the collection matches the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - public virtual bool Any(Expression> filter, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return MongoDbReader.Any(filter, partitionKey); - } - - /// - /// Asynchronously returns a list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A mongodb filter definition. - /// A mongodb filter option. - /// An optional partition key. - /// An optional cancellation Token. - public Task> GetAllAsync(FilterDefinition condition, FindOptions findOption = null, string partitionKey = null, - CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable - { - return MongoDbReader.GetAllAsync(condition, findOption, partitionKey, cancellationToken); - } - - /// - /// Returns a list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A mongodb filter definition. - /// A mongodb filter option. - /// An optional partition key. - public List GetAll(FilterDefinition condition, FindOptions findOption = null, - string partitionKey = null) where TDocument : IDocument where TKey : IEquatable - { - return MongoDbReader.GetAll(condition, findOption, partitionKey); - } - - /// - /// Asynchronously returns a list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// An optional cancellation Token. - public async virtual Task> GetAllAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbReader.GetAllAsync(filter, partitionKey, cancellationToken); - } - - /// - /// Returns a list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - public virtual List GetAll(Expression> filter, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return MongoDbReader.GetAll(filter, partitionKey); - } - - /// - /// Asynchronously counts how many documents match the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A mongodb filter definition. - /// A mongodb counting option. - /// An optional partitionKey - /// An optional cancellation Token. - public Task CountAsync(FilterDefinition condition, CountOptions countOption = null, string partitionKey = null, - CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable - { - return MongoDbReader.CountAsync(condition, countOption, partitionKey, cancellationToken); - } - - /// - /// Counts how many documents match the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A mongodb filter definition. - /// A mongodb counting option. - /// An optional partitionKey - public long Count(FilterDefinition condition, CountOptions countOption = null, - string partitionKey = null) where TDocument : IDocument where TKey : IEquatable - { - return MongoDbReader.Count(condition, countOption, partitionKey); - } - - /// - /// Asynchronously counts how many documents match the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partitionKey - /// An optional cancellation Token. - public async virtual Task CountAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbReader.CountAsync(filter, partitionKey, cancellationToken); - } - - /// - /// Counts how many documents match the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partitionKey - public virtual long Count(Expression> filter, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return MongoDbReader.Count(filter, partitionKey); - } - - /// - /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter. - /// - /// The document type. - /// The type of the primary key. - /// A LINQ expression filter. - /// A property selector to order by descending. - /// An optional partitionKey. - /// An optional cancellation Token. - public async virtual Task GetByMaxAsync( - Expression> filter, - Expression> maxValueSelector, - string partitionKey = null, CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbReader.GetByMaxAsync(filter, maxValueSelector, partitionKey, cancellationToken); - } - - /// - /// Gets the document with the maximum value of a specified property in a MongoDB collections that is satisfying the filter. - /// - /// The document type. - /// The type of the primary key. - /// A LINQ expression filter. - /// A property selector to order by descending. - /// An optional partitionKey. - public virtual TDocument GetByMax(Expression> filter, Expression> maxValueSelector, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return MongoDbReader.GetByMax(filter, maxValueSelector, partitionKey); - } - - /// - /// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the filter. - /// - /// The document type. - /// The type of the primary key. - /// A LINQ expression filter. - /// A property selector for the minimum value you are looking for. - /// An optional partitionKey. - /// An optional cancellation Token. - public async virtual Task GetByMinAsync(Expression> filter, - Expression> minValueSelector, - string partitionKey = null, - CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbReader.GetByMinAsync(filter, minValueSelector, partitionKey, cancellationToken); - } - - /// - /// Gets the document with the minimum value of a specified property in a MongoDB collections that is satisfying the filter. - /// - /// The document type. - /// The type of the primary key. - /// A LINQ expression filter. - /// A property selector for the minimum value you are looking for. - /// An optional partitionKey. - public virtual TDocument GetByMin(Expression> filter, Expression> minValueSelector, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return MongoDbReader.GetByMin(filter, minValueSelector, partitionKey); - } - - /// - /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. - /// - /// The document type. - /// The type of the primary key. - /// The type of the value used to order the query. - /// A LINQ expression filter. - /// A property selector for the maximum value you are looking for. - /// An optional partitionKey. - /// An optional cancellation Token. - public async virtual Task GetMaxValueAsync( - Expression> filter, - Expression> maxValueSelector, - string partitionKey = null, - CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbReader.GetMaxValueAsync(filter, maxValueSelector, partitionKey, cancellationToken); - } - - /// - /// Gets the maximum value of a property in a mongodb collections that is satisfying the filter. - /// - /// The document type. - /// The type of the primary key. - /// The type of the value used to order the query. - /// A LINQ expression filter. - /// A property selector to order by ascending. - /// An optional partitionKey. - public virtual TValue GetMaxValue(Expression> filter, Expression> maxValueSelector, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return MongoDbReader.GetMaxValue(filter, maxValueSelector, partitionKey); - } - - /// - /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. - /// - /// The document type. - /// The type of the primary key. - /// The type of the value used to order the query. - /// A LINQ expression filter. - /// A property selector to order by ascending. - /// An optional partition key. - /// An optional cancellation Token. - public virtual async Task GetMinValueAsync( - Expression> filter, - Expression> minValueSelector, - string partitionKey = null, - CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbReader.GetMinValueAsync(filter, minValueSelector, partitionKey, cancellationToken); - } - - /// - /// Gets the minimum value of a property in a mongodb collections that is satisfying the filter. - /// - /// The document type. - /// The type of the primary key. - /// The type of the value used to order the query. - /// A LINQ expression filter. - /// A property selector to order by ascending. - /// An optional partition key. - public virtual TValue GetMinValue(Expression> filter, Expression> minValueSelector, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return MongoDbReader.GetMinValue(filter, minValueSelector, partitionKey); - } - - #endregion - - #region Sum TKey - - /// - /// Sums the values of a selected field for a given filtered collection of documents. - /// - /// The type representing a Document. - /// The type of the primary key. - /// A LINQ expression filter. - /// The field you want to sum. - /// The partition key of your document, if any. - /// An optional cancellation Token. - public virtual async Task SumByAsync(Expression> filter, - Expression> selector, - string partitionKey = null, - CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbReader.SumByAsync(filter, selector, partitionKey, cancellationToken); - } - - /// - /// Sums the values of a selected field for a given filtered collection of documents. - /// - /// The type representing a Document. - /// The type of the primary key. - /// A LINQ expression filter. - /// The field you want to sum. - /// The partition key of your document, if any. - public virtual int SumBy(Expression> filter, - Expression> selector, - string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return MongoDbReader.SumBy(filter, selector, partitionKey); - } - - /// - /// Sums the values of a selected field for a given filtered collection of documents. - /// - /// The type representing a Document. - /// The type of the primary key. - /// A LINQ expression filter. - /// The field you want to sum. - /// The partition key of your document, if any. - public virtual async Task SumByAsync(Expression> filter, - Expression> selector, - string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return await MongoDbReader.SumByAsync(filter, selector, partitionKey); - } - - /// - /// Sums the values of a selected field for a given filtered collection of documents. - /// - /// The type representing a Document. - /// The type of the primary key. - /// A LINQ expression filter. - /// The field you want to sum. - /// The partition key of your document, if any. - public virtual decimal SumBy(Expression> filter, - Expression> selector, - string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return MongoDbReader.SumBy(filter, selector, partitionKey); - } - - #endregion Sum TKey - - #region Project TKey - - /// - /// Asynchronously returns a projected document matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type representing the model you want to project to. - /// A LINQ expression filter. - /// The projection expression. - /// An optional partition key. - /// An optional cancellation Token. - public virtual async Task ProjectOneAsync( - Expression> filter, - Expression> projection, - string partitionKey = null, - CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable - where TProjection : class - { - return await MongoDbReader.ProjectOneAsync(filter, projection, partitionKey, cancellationToken); - } - - /// - /// Returns a projected document matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type representing the model you want to project to. - /// A LINQ expression filter. - /// The projection expression. - /// An optional partition key. - public virtual TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - where TProjection : class - { - return MongoDbReader.ProjectOne(filter, projection, partitionKey); - } - - /// - /// Asynchronously returns a list of projected documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type representing the model you want to project to. - /// A LINQ expression filter. - /// The projection expression. - /// An optional partition key. - /// An optional cancellation Token. - public virtual async Task> ProjectManyAsync( - Expression> filter, - Expression> projection, - string partitionKey = null, CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable - where TProjection : class - { - return await MongoDbReader.ProjectManyAsync(filter, projection, partitionKey, cancellationToken); - } - - /// - /// Asynchronously returns a list of projected documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type representing the model you want to project to. - /// The document filter. - /// The projection expression. - /// An optional partition key. - public virtual List ProjectMany(Expression> filter, Expression> projection, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - where TProjection : class - { - return MongoDbReader.ProjectMany(filter, projection, partitionKey); - } - - #endregion Project TKey - - #region Group By TKey - - /// - /// Groups filtered a collection of documents given a grouping criteria, - /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the grouping criteria. - /// The type of the projected group. - /// The grouping criteria. - /// The projected group result. - /// The partition key of your document, if any. - public virtual List GroupBy( - Expression> groupingCriteria, - Expression, TProjection>> groupProjection, - string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - where TProjection : class, new() - { - return MongoDbReader.GroupBy(groupingCriteria, groupProjection, partitionKey); - } - - /// - /// Groups filtered a collection of documents given a grouping criteria, - /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the grouping criteria. - /// The type of the projected group. - /// A LINQ expression filter. - /// The grouping criteria. - /// The projected group result. - /// The partition key of your document, if any. - public virtual List GroupBy( - Expression> filter, - Expression> groupingCriteria, - Expression, TProjection>> groupProjection, - string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - where TProjection : class, new() - { - return MongoDbReader.GroupBy(filter, groupingCriteria, groupProjection, partitionKey); - } - - #endregion Group By TKey - - - #region Pagination - - /// - /// Asynchronously returns a paginated list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// The property selector. - /// Order of the sorting. - /// The number of documents you want to skip. Default value is 0. - /// The number of documents you want to take. Default value is 50. - /// An optional partition key. - public virtual async Task> GetSortedPaginatedAsync( - Expression> filter, - Expression> sortSelector, - bool ascending = true, - int skipNumber = 0, - int takeNumber = 50, - string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - var sorting = ascending - ? Builders.Sort.Ascending(sortSelector) - : Builders.Sort.Descending(sortSelector); - - return await HandlePartitioned(partitionKey) - .Find(filter) - .Sort(sorting) - .Skip(skipNumber) - .Limit(takeNumber) - .ToListAsync(); - } - - /// - /// Asynchronously returns a paginated list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// The sort definition. - /// The number of documents you want to skip. Default value is 0. - /// The number of documents you want to take. Default value is 50. - /// An optional partition key. - /// An optional cancellation Token. - public virtual async Task> GetSortedPaginatedAsync( - Expression> filter, - SortDefinition sortDefinition, - int skipNumber = 0, - int takeNumber = 50, - string partitionKey = null, - CancellationToken cancellationToken = default) - where TDocument : IDocument - where TKey : IEquatable - { - return await HandlePartitioned(partitionKey) - .Find(filter) - .Sort(sortDefinition) - .Skip(skipNumber) - .Limit(takeNumber) - .ToListAsync(cancellationToken); - } - - #endregion Pagination - - - - /// - /// Gets a collections for a potentially partitioned document type. + /// Gets a collections for a potentially partitioned document type. /// /// The document type. /// The type of the primary key. @@ -750,7 +56,7 @@ namespace MongoDbGenericRepository } /// - /// Gets a collections for a potentially partitioned document type. + /// Gets a collections for a potentially partitioned document type. /// /// The document type. /// The type of the primary key. @@ -764,11 +70,12 @@ namespace MongoDbGenericRepository { return GetCollection(partitionKey); } + return GetCollection(); } /// - /// Gets a collections for a potentially partitioned document type. + /// Gets a collections for a potentially partitioned document type. /// /// The document type. /// The document. @@ -780,7 +87,7 @@ namespace MongoDbGenericRepository } /// - /// Gets a collections for the type TDocument with a partition key. + /// Gets a collections for the type TDocument with a partition key. /// /// The document type. /// The type of the primary key. @@ -792,5 +99,1693 @@ namespace MongoDbGenericRepository { return MongoDbReader.GetCollection(partitionKey); } + + #region Read TKey + + /// + public virtual async Task GetByIdAsync(TKey id) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetByIdAsync(id, null, CancellationToken.None); + } + + /// + public virtual async Task GetByIdAsync(TKey id, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetByIdAsync(id, null, cancellationToken); + } + + /// + public virtual async Task GetByIdAsync(TKey id, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetByIdAsync(id, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task GetByIdAsync(TKey id, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbReader.GetByIdAsync(id, partitionKey, cancellationToken); + } + + /// + public virtual TDocument GetById(TKey id) + where TDocument : IDocument + where TKey : IEquatable + { + return GetById(id, null, CancellationToken.None); + } + + /// + public virtual TDocument GetById(TKey id, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return GetById(id, null, cancellationToken); + } + + /// + public virtual TDocument GetById(TKey id, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return GetById(id, partitionKey, CancellationToken.None); + } + + /// + public virtual TDocument GetById(TKey id, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.GetById(id, partitionKey, cancellationToken); + } + + /// + public Task GetOneAsync(FilterDefinition condition) + where TDocument : IDocument where TKey : IEquatable + { + return GetOneAsync(condition, null, null, CancellationToken.None); + } + + /// + public Task GetOneAsync(FilterDefinition condition, CancellationToken cancellationToken) + where TDocument : IDocument where TKey : IEquatable + { + return GetOneAsync(condition, null, null, cancellationToken); + } + + /// + public Task GetOneAsync(FilterDefinition condition, FindOptions findOption) + where TDocument : IDocument where TKey : IEquatable + { + return GetOneAsync(condition, findOption, null, CancellationToken.None); + } + + /// + public Task GetOneAsync(FilterDefinition condition, string partitionKey) + where TDocument : IDocument where TKey : IEquatable + { + return GetOneAsync(condition, null, partitionKey, CancellationToken.None); + } + + /// + public Task GetOneAsync(FilterDefinition condition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument where TKey : IEquatable + { + return GetOneAsync(condition, null, partitionKey, cancellationToken); + } + + /// + public Task GetOneAsync(FilterDefinition condition, FindOptions findOption, CancellationToken cancellationToken) + where TDocument : IDocument where TKey : IEquatable + { + return GetOneAsync(condition, findOption, null, cancellationToken); + } + + /// + public Task GetOneAsync(FilterDefinition condition, FindOptions findOption, string partitionKey) + where TDocument : IDocument where TKey : IEquatable + { + return GetOneAsync(condition, findOption, partitionKey, CancellationToken.None); + } + + /// + public Task GetOneAsync( + FilterDefinition condition, + FindOptions findOption, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument where TKey : IEquatable + { + return MongoDbReader.GetOneAsync(condition, findOption, partitionKey, cancellationToken); + } + + /// + public TDocument GetOne(FilterDefinition condition) + where TDocument : IDocument where TKey : IEquatable + { + return GetOne(condition, null, null, CancellationToken.None); + } + + /// + public TDocument GetOne(FilterDefinition condition, FindOptions findOption) + where TDocument : IDocument where TKey : IEquatable + { + return GetOne(condition, findOption, null, CancellationToken.None); + } + + /// + public TDocument GetOne(FilterDefinition condition, string partitionKey) + where TDocument : IDocument where TKey : IEquatable + { + return GetOne(condition, null, partitionKey, CancellationToken.None); + } + + + /// + public TDocument GetOne(FilterDefinition condition, FindOptions findOption, string partitionKey) + where TDocument : IDocument where TKey : IEquatable + { + return GetOne(condition, findOption, partitionKey, CancellationToken.None); + } + + + /// + public TDocument GetOne(FilterDefinition condition, CancellationToken cancellationToken) + where TDocument : IDocument where TKey : IEquatable + { + return GetOne(condition, null, null, cancellationToken); + } + + /// + public TDocument GetOne(FilterDefinition condition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument where TKey : IEquatable + { + return GetOne(condition, null, partitionKey, cancellationToken); + } + + /// + public TDocument GetOne(FilterDefinition condition, FindOptions findOption, CancellationToken cancellationToken) + where TDocument : IDocument where TKey : IEquatable + { + return GetOne(condition, findOption, null, cancellationToken); + } + + /// + public TDocument GetOne( + FilterDefinition condition, + FindOptions findOption, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument where TKey : IEquatable + { + return MongoDbReader.GetOne(condition, findOption, partitionKey, cancellationToken); + } + + /// + public virtual async Task GetOneAsync(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetOneAsync(filter, null, CancellationToken.None); + } + + /// + public virtual async Task GetOneAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetOneAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task GetOneAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetOneAsync(filter, null, cancellationToken); + } + + /// + public virtual async Task GetOneAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbReader.GetOneAsync(filter, partitionKey, cancellationToken); + } + + /// + public virtual TDocument GetOne(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable + { + return GetOne(filter, null, CancellationToken.None); + } + + /// + public virtual TDocument GetOne(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return GetOne(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual TDocument GetOne(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return GetOne(filter, null, cancellationToken); + } + + /// + public virtual TDocument GetOne(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.GetOne(filter, partitionKey, cancellationToken); + } + + /// + public virtual IFindFluent GetCursor(Expression> filter, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.GetCursor(filter, partitionKey); + } + + /// + public Task AnyAsync(FilterDefinition condition) + where TDocument : IDocument + where TKey : IEquatable + { + return AnyAsync(condition, null, null, CancellationToken.None); + } + + /// + public Task AnyAsync(FilterDefinition condition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return AnyAsync(condition, null, partitionKey, CancellationToken.None); + } + + /// + public Task AnyAsync(FilterDefinition condition, CountOptions countOption) + where TDocument : IDocument + where TKey : IEquatable + { + return AnyAsync(condition, countOption, null, CancellationToken.None); + } + + /// + public Task AnyAsync(FilterDefinition condition, CountOptions countOption, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return AnyAsync(condition, countOption, partitionKey, CancellationToken.None); + } + + /// + public Task AnyAsync(FilterDefinition condition, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return AnyAsync(condition, null, null, cancellationToken); + } + + /// + public Task AnyAsync(FilterDefinition condition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return AnyAsync(condition, null, partitionKey, cancellationToken); + } + + /// + public Task AnyAsync(FilterDefinition condition, CountOptions countOption, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return AnyAsync(condition, countOption, null, cancellationToken); + } + + /// + public Task AnyAsync( + FilterDefinition condition, + CountOptions countOption, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.AnyAsync(condition, countOption, partitionKey, cancellationToken); + } + + /// + public bool Any(FilterDefinition condition) + where TDocument : IDocument + where TKey : IEquatable + { + return Any(condition, null, null, CancellationToken.None); + } + + /// + public bool Any(FilterDefinition condition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return Any(condition, null, partitionKey, CancellationToken.None); + } + + /// + public bool Any(FilterDefinition condition, CountOptions countOption) + where TDocument : IDocument + where TKey : IEquatable + { + return Any(condition, countOption, null, CancellationToken.None); + } + + /// + public bool Any(FilterDefinition condition, CountOptions countOption, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.Any(condition, countOption, partitionKey, CancellationToken.None); + } + + /// + public bool Any(FilterDefinition condition, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return Any(condition, null, null, cancellationToken); + } + + /// + public bool Any(FilterDefinition condition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return Any(condition, null, partitionKey, cancellationToken); + } + + /// + public bool Any(FilterDefinition condition, CountOptions countOption, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return Any(condition, countOption, null, cancellationToken); + } + + /// + public bool Any( + FilterDefinition condition, + CountOptions countOption, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.Any(condition, countOption, partitionKey, cancellationToken); + } + + /// + public virtual async Task AnyAsync(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable + { + return await AnyAsync(filter, null, CancellationToken.None); + } + + /// + public virtual async Task AnyAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await AnyAsync(filter, null, cancellationToken); + } + + /// + public virtual async Task AnyAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await AnyAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task AnyAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbReader.AnyAsync(filter, partitionKey, cancellationToken); + } + + /// + public virtual bool Any(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.Any(filter, null, CancellationToken.None); + } + + /// + public virtual bool Any(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.Any(filter, null, cancellationToken); + } + + /// + public virtual bool Any(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return Any(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual bool Any(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.Any(filter, partitionKey, cancellationToken); + } + + /// + public Task> GetAllAsync(FilterDefinition condition) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAllAsync(condition, null, null, CancellationToken.None); + } + + /// + public Task> GetAllAsync(FilterDefinition condition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAllAsync(condition, null, partitionKey, CancellationToken.None); + } + + /// + public Task> GetAllAsync(FilterDefinition condition, FindOptions findOption) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAllAsync(condition, findOption, null, CancellationToken.None); + } + + /// + public Task> GetAllAsync(FilterDefinition condition, FindOptions findOption, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAllAsync(condition, findOption, partitionKey, CancellationToken.None); + } + + /// + public Task> GetAllAsync(FilterDefinition condition, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAllAsync(condition, null, null, cancellationToken); + } + + /// + public Task> GetAllAsync( + FilterDefinition condition, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAllAsync(condition, null, partitionKey, cancellationToken); + } + + /// + public Task> GetAllAsync( + FilterDefinition condition, + FindOptions findOption, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAllAsync(condition, findOption, null, cancellationToken); + } + + /// + public Task> GetAllAsync( + FilterDefinition condition, + FindOptions findOption, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.GetAllAsync(condition, findOption, partitionKey, cancellationToken); + } + + /// + public List GetAll(FilterDefinition condition) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAll(condition, null, null, CancellationToken.None); + } + + /// + public List GetAll(FilterDefinition condition, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAll(condition, null, null, cancellationToken); + } + + /// + public List GetAll(FilterDefinition condition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAll(condition, null, partitionKey, CancellationToken.None); + } + + /// + public List GetAll(FilterDefinition condition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAll(condition, null, partitionKey, cancellationToken); + } + + /// + public List GetAll(FilterDefinition condition, FindOptions findOption) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAll(condition, findOption, null, CancellationToken.None); + } + + /// + public List GetAll(FilterDefinition condition, FindOptions findOption, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAll(condition, findOption, null, cancellationToken); + } + + /// + public List GetAll(FilterDefinition condition, FindOptions findOption, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAll(condition, findOption, partitionKey, CancellationToken.None); + } + + /// + public List GetAll( + FilterDefinition condition, + FindOptions findOption, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.GetAll(condition, findOption, partitionKey, cancellationToken); + } + + /// + public virtual async Task> GetAllAsync(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetAllAsync(filter, null, CancellationToken.None); + } + + /// + public virtual async Task> GetAllAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetAllAsync(filter, null, cancellationToken); + } + + /// + public virtual async Task> GetAllAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetAllAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task> GetAllAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbReader.GetAllAsync(filter, partitionKey, cancellationToken); + } + + /// + public virtual List GetAll(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAll(filter, null, CancellationToken.None); + } + + /// + public virtual List GetAll(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAll(filter, null, cancellationToken); + } + + /// + public virtual List GetAll(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return GetAll(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual List GetAll( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.GetAll(filter, partitionKey, cancellationToken); + } + + /// + public Task CountAsync(FilterDefinition condition) + where TDocument : IDocument + where TKey : IEquatable + { + return CountAsync(condition, null, null, CancellationToken.None); + } + + /// + public Task CountAsync(FilterDefinition condition, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return CountAsync(condition, null, null, cancellationToken); + } + + /// + public Task CountAsync(FilterDefinition condition, CountOptions countOption) + where TDocument : IDocument + where TKey : IEquatable + { + return CountAsync(condition, countOption, null, CancellationToken.None); + } + + /// + public Task CountAsync(FilterDefinition condition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return CountAsync(condition, null, partitionKey, CancellationToken.None); + } + + /// + public Task CountAsync(FilterDefinition condition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return CountAsync(condition, null, partitionKey, cancellationToken); + } + + /// + public Task CountAsync(FilterDefinition condition, CountOptions countOption, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return CountAsync(condition, countOption, null, cancellationToken); + } + + /// + public Task CountAsync(FilterDefinition condition, CountOptions countOption, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return CountAsync(condition, countOption, partitionKey, CancellationToken.None); + } + + /// + public Task CountAsync( + FilterDefinition condition, + CountOptions countOption, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.CountAsync(condition, countOption, partitionKey, cancellationToken); + } + + /// + public long Count(FilterDefinition condition) + where TDocument : IDocument + where TKey : IEquatable + { + return Count(condition, null, null, CancellationToken.None); + } + + /// + public long Count(FilterDefinition condition, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return Count(condition, null, null, cancellationToken); + } + + /// + public long Count(FilterDefinition condition, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return Count(condition, null, partitionKey, CancellationToken.None); + } + + /// + public long Count(FilterDefinition condition, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return Count(condition, null, partitionKey, cancellationToken); + } + + /// + public long Count(FilterDefinition condition, CountOptions countOption) + where TDocument : IDocument + where TKey : IEquatable + { + return Count(condition, countOption, null, CancellationToken.None); + } + + /// + public long Count(FilterDefinition condition, CountOptions countOption, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return Count(condition, countOption, null, cancellationToken); + } + + /// + public long Count(FilterDefinition condition, CountOptions countOption, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return Count(condition, countOption, partitionKey, CancellationToken.None); + } + + /// + public long Count( + FilterDefinition condition, + CountOptions countOption, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.Count(condition, countOption, partitionKey, cancellationToken); + } + + /// + public virtual async Task CountAsync(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable + { + return await CountAsync(filter, null, CancellationToken.None); + } + + /// + public virtual async Task CountAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CountAsync(filter, null, cancellationToken); + } + + /// + public virtual async Task CountAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await CountAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CountAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbReader.CountAsync(filter, partitionKey, cancellationToken); + } + + /// + public virtual long Count(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable + { + return Count(filter, null, CancellationToken.None); + } + + /// + public virtual long Count(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return Count(filter, null, cancellationToken); + } + + /// + public virtual long Count(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return Count(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual long Count(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.Count(filter, partitionKey, cancellationToken); + } + + /// + public virtual async Task GetByMaxAsync( + Expression> filter, + Expression> maxValueSelector) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetByMaxAsync(filter, maxValueSelector, null, CancellationToken.None); + } + + /// + public virtual async Task GetByMaxAsync( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetByMaxAsync(filter, maxValueSelector, null, cancellationToken); + } + + /// + public virtual async Task GetByMaxAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetByMaxAsync(filter, maxValueSelector, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task GetByMaxAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbReader.GetByMaxAsync(filter, maxValueSelector, partitionKey, cancellationToken); + } + + /// + public virtual TDocument GetByMax(Expression> filter, Expression> maxValueSelector) + where TDocument : IDocument + where TKey : IEquatable + { + return GetByMax(filter, maxValueSelector, null, CancellationToken.None); + } + + /// + public virtual TDocument GetByMax( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return GetByMax(filter, maxValueSelector, null, cancellationToken); + } + + /// + public virtual TDocument GetByMax( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return GetByMax(filter, maxValueSelector, partitionKey, CancellationToken.None); + } + + /// + public virtual TDocument GetByMax( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.GetByMax(filter, maxValueSelector, partitionKey, cancellationToken); + } + + /// + public virtual async Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetByMinAsync(filter, minValueSelector, null, CancellationToken.None); + } + + /// + public virtual async Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetByMinAsync(filter, minValueSelector, null, cancellationToken); + } + + /// + public virtual async Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetByMinAsync(filter, minValueSelector, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbReader.GetByMinAsync(filter, minValueSelector, partitionKey, cancellationToken); + } + + /// + public virtual TDocument GetByMin(Expression> filter, Expression> minValueSelector) + where TDocument : IDocument + where TKey : IEquatable + { + return GetByMin(filter, minValueSelector, null, CancellationToken.None); + } + + /// + public virtual TDocument GetByMin( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return GetByMin(filter, minValueSelector, null, cancellationToken); + } + + /// + public virtual TDocument GetByMin( + Expression> filter, + Expression> minValueSelector, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return GetByMin(filter, minValueSelector, partitionKey, CancellationToken.None); + } + + /// + public virtual TDocument GetByMin( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.GetByMin(filter, minValueSelector, partitionKey, cancellationToken); + } + + /// + public virtual async Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetMaxValueAsync(filter, maxValueSelector, null, CancellationToken.None); + } + + /// + public virtual async Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetMaxValueAsync(filter, maxValueSelector, null, cancellationToken); + } + + /// + public virtual async Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetMaxValueAsync(filter, maxValueSelector, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbReader.GetMaxValueAsync(filter, maxValueSelector, partitionKey, cancellationToken); + } + + /// + public virtual TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector) + where TDocument : IDocument + where TKey : IEquatable + { + return GetMaxValue(filter, maxValueSelector, null, CancellationToken.None); + } + + /// + public virtual TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return GetMaxValue(filter, maxValueSelector, null, cancellationToken); + } + + /// + public virtual TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return GetMaxValue(filter, maxValueSelector, partitionKey, CancellationToken.None); + } + + /// + public virtual TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.GetMaxValue(filter, maxValueSelector, partitionKey, cancellationToken); + } + + /// + public virtual async Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetMinValueAsync(filter, minValueSelector, null, CancellationToken.None); + } + + /// + public virtual async Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetMinValueAsync(filter, minValueSelector, null, cancellationToken); + } + + /// + public virtual async Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetMinValueAsync(filter, minValueSelector, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbReader.GetMinValueAsync(filter, minValueSelector, partitionKey, cancellationToken); + } + + /// + public virtual TValue GetMinValue( + Expression> filter, + Expression> minValueSelector) + where TDocument : IDocument + where TKey : IEquatable + { + return GetMinValue(filter, minValueSelector, null, CancellationToken.None); + } + + /// + public virtual TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return GetMinValue(filter, minValueSelector, null, cancellationToken); + } + + /// + public virtual TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return GetMinValue(filter, minValueSelector, partitionKey, CancellationToken.None); + } + + /// + public virtual TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.GetMinValue(filter, minValueSelector, partitionKey, cancellationToken); + } + + #endregion + + #region Sum TKey + + /// + public virtual async Task SumByAsync(Expression> filter, Expression> selector) + where TDocument : IDocument + where TKey : IEquatable + { + return await SumByAsync(filter, selector, null, CancellationToken.None); + } + + /// + public virtual async Task SumByAsync( + Expression> filter, + Expression> selector, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await SumByAsync(filter, selector, null, cancellationToken); + } + + /// + public virtual async Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await SumByAsync(filter, selector, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbReader.SumByAsync(filter, selector, partitionKey, cancellationToken); + } + + /// + public virtual int SumBy( + Expression> filter, + Expression> selector, + string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.SumBy(filter, selector, partitionKey); + } + + /// + public virtual async Task SumByAsync(Expression> filter, Expression> selector) + where TDocument : IDocument + where TKey : IEquatable + { + return await SumByAsync(filter, selector, null, CancellationToken.None); + } + + /// + public virtual async Task SumByAsync( + Expression> filter, + Expression> selector, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await SumByAsync(filter, selector, null, cancellationToken); + } + + /// + public virtual async Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await SumByAsync(filter, selector, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbReader.SumByAsync(filter, selector, partitionKey, cancellationToken); + } + + /// + public virtual decimal SumBy( + Expression> filter, + Expression> selector, + string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbReader.SumBy(filter, selector, partitionKey); + } + + #endregion Sum TKey + + #region Project TKey + + /// + public virtual async Task ProjectOneAsync( + Expression> filter, + Expression> projection) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return await ProjectOneAsync(filter, projection, null, CancellationToken.None); + } + + /// + public virtual async Task ProjectOneAsync( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return await ProjectOneAsync(filter, projection, null, cancellationToken); + } + + /// + public virtual async Task ProjectOneAsync( + Expression> filter, + Expression> projection, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return await ProjectOneAsync(filter, projection, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task ProjectOneAsync( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return await MongoDbReader.ProjectOneAsync(filter, projection, partitionKey, cancellationToken); + } + + /// + public virtual TProjection ProjectOne( + Expression> filter, + Expression> projection) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return ProjectOne(filter, projection, null, CancellationToken.None); + } + + /// + public virtual TProjection ProjectOne( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return ProjectOne(filter, projection, null, cancellationToken); + } + + /// + public virtual TProjection ProjectOne( + Expression> filter, + Expression> projection, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return ProjectOne(filter, projection, partitionKey, CancellationToken.None); + } + + /// + public virtual TProjection ProjectOne( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return MongoDbReader.ProjectOne(filter, projection, partitionKey, cancellationToken); + } + + /// + public virtual async Task> ProjectManyAsync( + Expression> filter, + Expression> projection) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return await ProjectManyAsync(filter, projection, null, CancellationToken.None); + } + + /// + public virtual async Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return await ProjectManyAsync(filter, projection, null, cancellationToken); + } + + /// + public virtual async Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return await ProjectManyAsync(filter, projection, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return await MongoDbReader.ProjectManyAsync(filter, projection, partitionKey, cancellationToken); + } + + /// + public virtual List ProjectMany( + Expression> filter, + Expression> projection) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return ProjectMany(filter, projection, null, CancellationToken.None); + } + + /// + public virtual List ProjectMany( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return ProjectMany(filter, projection, null, cancellationToken); + } + + /// + public virtual List ProjectMany( + Expression> filter, + Expression> projection, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return ProjectMany(filter, projection, partitionKey, CancellationToken.None); + } + + /// + public virtual List ProjectMany( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return MongoDbReader.ProjectMany(filter, projection, partitionKey, cancellationToken); + } + + #endregion Project TKey + + #region Group By TKey + + /// + public virtual List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new() + { + return GroupBy(groupingCriteria, groupProjection, null, CancellationToken.None); + } + + /// + public virtual List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new() + { + return GroupBy(groupingCriteria, groupProjection, null, cancellationToken); + } + + /// + public virtual List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new() + { + return GroupBy(groupingCriteria, groupProjection, partitionKey, CancellationToken.None); + } + + /// + public virtual List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new() + { + return MongoDbReader.GroupBy(groupingCriteria, groupProjection, partitionKey, cancellationToken); + } + + /// + public virtual List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new() + { + return GroupBy(filter, groupingCriteria, groupProjection, null, CancellationToken.None); + } + + /// + public virtual List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new() + { + return GroupBy(filter, groupingCriteria, groupProjection, null, cancellationToken); + } + + /// + public virtual List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new() + { + return GroupBy(filter, groupingCriteria, groupProjection, partitionKey, CancellationToken.None); + } + + /// + public virtual List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new() + { + return MongoDbReader.GroupBy(filter, groupingCriteria, groupProjection, partitionKey, cancellationToken); + } + + #endregion Group By TKey + + #region Pagination + + /// + public virtual async Task> GetSortedPaginatedAsync( + Expression> filter, + Expression> sortSelector, + bool ascending = true, + int skipNumber = 0, + int takeNumber = 50, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + { + var sorting = ascending + ? Builders.Sort.Ascending(sortSelector) + : Builders.Sort.Descending(sortSelector); + + return await HandlePartitioned(partitionKey) + .Find(filter) + .Sort(sorting) + .Skip(skipNumber) + .Limit(takeNumber) + .ToListAsync(cancellationToken); + } + + /// + public virtual async Task> GetSortedPaginatedAsync( + Expression> filter, + SortDefinition sortDefinition, + int skipNumber = 0, + int takeNumber = 50, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + { + return await HandlePartitioned(partitionKey) + .Find(filter) + .Sort(sortDefinition) + .Skip(skipNumber) + .Limit(takeNumber) + .ToListAsync(cancellationToken); + } + + #endregion Pagination } } \ No newline at end of file