From 14e261261c28217c1660575272e0aa51c2688aab Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sun, 11 Jun 2023 18:39:22 +0100 Subject: [PATCH 01/40] interfaces to seperate files --- .../CoreIntegrationTests.csproj | 4 +- CoreUnitTests/CoreUnitTests.csproj | 23 + CoreUnitTests/UnitTest1.cs | 11 + MongoDbGenericRepository.sln | 6 + .../BaseMongoRepository.Create.cs | 57 +- .../BaseMongoRepository.Delete.cs | 100 +-- .../BaseMongoRepository.Index.cs | 104 +-- .../BaseMongoRepository.Main.cs | 4 +- .../BaseMongoRepository.Update.cs | 5 +- .../DataAccess/Base/DataAccessBase.cs | 2 +- .../DataAccess/Base/IDataAccessBase.cs | 56 ++ .../DataAccess/Create/IMongoDbCreator.cs | 106 +++ .../DataAccess/Create/MongoDbCreator.cs | 2 +- .../DataAccess/Delete/IMongoDbEraser.cs | 151 +++++ .../DataAccess/Delete/MongoDbEraser.cs | 2 +- .../DataAccess/Index/IMongoDbIndexHandler.cs | 156 +++++ .../DataAccess/Index/MongoDbIndexHandler.cs | 2 +- .../DataAccess/Read/IMongoDbReader.cs | 631 ++++++++++++++++++ .../DataAccess/Read/MongoDbReader.Project.cs | 2 +- .../DataAccess/Update/IMongoDbUpdater.cs | 432 ++++++++++++ .../DataAccess/Update/MongoDbUpdater.cs | 2 +- .../IBaseMongoRepository.Create.cs | 61 ++ .../IBaseMongoRepository.Delete.cs | 103 +++ .../IBaseMongoRepository.Index.cs | 108 +++ .../BaseMongoRepository.TKey.Create.cs | 45 +- .../BaseMongoRepository.TKey.Delete.cs | 87 +-- .../BaseMongoRepository.TKey.Index.cs | 95 +-- .../BaseMongoRepository.TKey.Main.cs | 18 +- .../BaseMongoRepository.TKey.ReadOnly.cs | 6 +- .../BaseMongoRepository.TKey.Update.cs | 198 +----- .../IBaseMongoRepository.TKey.Create.cs | 49 ++ .../IBaseMongoRepository.TKey.Delete.cs | 91 +++ .../IBaseMongoRepository.TKey.Index.cs | 98 +++ .../IBaseMongoRepository.TKey.Update.cs | 201 ++++++ .../IBaseMongoRepository.TKey.cs | 18 + .../MongoDbGenericRepository.csproj | 2 +- .../MongoDbGenericRepository.xml | 16 +- .../ReadOnlyMongoRepository.cs | 4 +- 38 files changed, 2348 insertions(+), 710 deletions(-) create mode 100644 CoreUnitTests/CoreUnitTests.csproj create mode 100644 CoreUnitTests/UnitTest1.cs create mode 100644 MongoDbGenericRepository/DataAccess/Base/IDataAccessBase.cs create mode 100644 MongoDbGenericRepository/DataAccess/Create/IMongoDbCreator.cs create mode 100644 MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs create mode 100644 MongoDbGenericRepository/DataAccess/Index/IMongoDbIndexHandler.cs create mode 100644 MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs create mode 100644 MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs create mode 100644 MongoDbGenericRepository/IBaseMongoRepository.Create.cs create mode 100644 MongoDbGenericRepository/IBaseMongoRepository.Delete.cs create mode 100644 MongoDbGenericRepository/IBaseMongoRepository.Index.cs create mode 100644 MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Create.cs create mode 100644 MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs create mode 100644 MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs create mode 100644 MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Update.cs create mode 100644 MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.cs diff --git a/CoreIntegrationTests/CoreIntegrationTests.csproj b/CoreIntegrationTests/CoreIntegrationTests.csproj index 493a082..49ae519 100644 --- a/CoreIntegrationTests/CoreIntegrationTests.csproj +++ b/CoreIntegrationTests/CoreIntegrationTests.csproj @@ -1,13 +1,13 @@  - netcoreapp2.1 + net6.0 - + all diff --git a/CoreUnitTests/CoreUnitTests.csproj b/CoreUnitTests/CoreUnitTests.csproj new file mode 100644 index 0000000..35cd8dc --- /dev/null +++ b/CoreUnitTests/CoreUnitTests.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + diff --git a/CoreUnitTests/UnitTest1.cs b/CoreUnitTests/UnitTest1.cs new file mode 100644 index 0000000..8db911c --- /dev/null +++ b/CoreUnitTests/UnitTest1.cs @@ -0,0 +1,11 @@ +using Xunit; + +namespace CoreUnitTests; + +public class UnitTest1 +{ + [Fact] + public void Test1() + { + } +} \ No newline at end of file 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/BaseMongoRepository.Create.cs b/MongoDbGenericRepository/BaseMongoRepository.Create.cs index 70d20e5..13465b4 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Create.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Create.cs @@ -7,59 +7,6 @@ using System.Threading.Tasks; 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. @@ -67,12 +14,12 @@ namespace MongoDbGenericRepository public abstract partial class BaseMongoRepository : IBaseMongoRepository_Create { private readonly object _initLock = new object(); - private MongoDbCreator _mongoDbCreator; + private volatile IMongoDbCreator _mongoDbCreator; /// /// The MongoDbCreator field. /// - protected virtual MongoDbCreator MongoDbCreator + protected virtual IMongoDbCreator MongoDbCreator { get { diff --git a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs index e86b060..cba59a8 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs @@ -7,105 +7,11 @@ using System.Threading.Tasks; 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 volatile IMongoDbEraser _mongoDbEraser; + + protected virtual IMongoDbEraser MongoDbEraser { get { diff --git a/MongoDbGenericRepository/BaseMongoRepository.Index.cs b/MongoDbGenericRepository/BaseMongoRepository.Index.cs index fa97458..f74972a 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Index.cs @@ -7,113 +7,15 @@ 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. /// public abstract partial class BaseMongoRepository : IBaseMongoRepository_Index { - private MongoDbIndexHandler _mongoDbIndexHandler; - protected virtual MongoDbIndexHandler MongoDbIndexHandler + private volatile IMongoDbIndexHandler _mongoDbIndexHandler; + + protected virtual IMongoDbIndexHandler MongoDbIndexHandler { get { diff --git a/MongoDbGenericRepository/BaseMongoRepository.Main.cs b/MongoDbGenericRepository/BaseMongoRepository.Main.cs index 09664e8..f86f9c7 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Main.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Main.cs @@ -24,7 +24,7 @@ namespace MongoDbGenericRepository } /// - /// The contructor taking a . + /// The constructor taking a . /// /// A mongodb context implementing protected BaseMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext) @@ -32,7 +32,7 @@ namespace MongoDbGenericRepository } /// - /// The contructor taking a . + /// The constructor taking a . /// /// A mongodb context implementing protected BaseMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase) diff --git a/MongoDbGenericRepository/BaseMongoRepository.Update.cs b/MongoDbGenericRepository/BaseMongoRepository.Update.cs index 82306b1..c4272f5 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Update.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Update.cs @@ -13,8 +13,9 @@ namespace MongoDbGenericRepository /// public abstract partial class BaseMongoRepository : IBaseMongoRepository_Update { - private MongoDbUpdater _mongoDbUpdater; - protected virtual MongoDbUpdater MongoDbUpdater + private volatile IMongoDbUpdater _mongoDbUpdater; + + protected virtual IMongoDbUpdater MongoDbUpdater { get { diff --git a/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs b/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs index eded8d5..a59d0b4 100644 --- a/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs +++ b/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs @@ -10,7 +10,7 @@ namespace MongoDbGenericRepository.DataAccess.Base /// /// A base class for accessing the Database and its Collections. /// - public class DataAccessBase + public class DataAccessBase : IDataAccessBase { /// /// The MongoDbContext diff --git a/MongoDbGenericRepository/DataAccess/Base/IDataAccessBase.cs b/MongoDbGenericRepository/DataAccess/Base/IDataAccessBase.cs new file mode 100644 index 0000000..b81ea09 --- /dev/null +++ b/MongoDbGenericRepository/DataAccess/Base/IDataAccessBase.cs @@ -0,0 +1,56 @@ +using System; +using System.Linq.Expressions; +using MongoDB.Driver; +using MongoDB.Driver.Linq; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository.DataAccess.Base +{ + 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..018b8c4 --- /dev/null +++ b/MongoDbGenericRepository/DataAccess/Create/IMongoDbCreator.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +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.Create +{ + 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. + 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; + + /// + /// 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/MongoDbCreator.cs b/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs index 7847030..de95085 100644 --- a/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs +++ b/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs @@ -14,7 +14,7 @@ namespace MongoDbGenericRepository.DataAccess.Create /// /// A class to insert MongoDb document. /// - public class MongoDbCreator : DataAccessBase + public class MongoDbCreator : DataAccessBase, IMongoDbCreator { /// /// The construct of the MongoDbCreator class. diff --git a/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs b/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs new file mode 100644 index 0000000..43eec28 --- /dev/null +++ b/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs @@ -0,0 +1,151 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDB.Driver.Linq; +using MongoDbGenericRepository.DataAccess.Base; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository.DataAccess.Delete +{ + 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. + /// The number of documents deleted. + long DeleteOne(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. + /// 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. + /// 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; + + /// + /// 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/Delete/MongoDbEraser.cs b/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs index 1daabac..b28d027 100644 --- a/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs +++ b/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs @@ -10,7 +10,7 @@ using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Delete { - public class MongoDbEraser : DataAccessBase + public class MongoDbEraser : DataAccessBase, IMongoDbEraser { public MongoDbEraser(IMongoDbContext mongoDbContext) : base(mongoDbContext) { diff --git a/MongoDbGenericRepository/DataAccess/Index/IMongoDbIndexHandler.cs b/MongoDbGenericRepository/DataAccess/Index/IMongoDbIndexHandler.cs new file mode 100644 index 0000000..b9b8338 --- /dev/null +++ b/MongoDbGenericRepository/DataAccess/Index/IMongoDbIndexHandler.cs @@ -0,0 +1,156 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDB.Driver.Linq; +using MongoDbGenericRepository.DataAccess.Base; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository.DataAccess.Index +{ + 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 + /// 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; + + /// + /// 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/Index/MongoDbIndexHandler.cs b/MongoDbGenericRepository/DataAccess/Index/MongoDbIndexHandler.cs index 6662670..dbf1b9a 100644 --- a/MongoDbGenericRepository/DataAccess/Index/MongoDbIndexHandler.cs +++ b/MongoDbGenericRepository/DataAccess/Index/MongoDbIndexHandler.cs @@ -10,7 +10,7 @@ using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Index { - public class MongoDbIndexHandler : DataAccessBase + public class MongoDbIndexHandler : DataAccessBase, IMongoDbIndexHandler { public MongoDbIndexHandler(IMongoDbContext mongoDbContext) : base(mongoDbContext) { diff --git a/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs b/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs new file mode 100644 index 0000000..f3c6e38 --- /dev/null +++ b/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs @@ -0,0 +1,631 @@ +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 +{ + 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. + 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. + /// + /// 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. + List ProjectMany(Expression> filter, Expression> projection, string partitionKey = null) + 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. + List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey = null) + 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. + List GroupBy( + Expression> filter, + Expression> selector, + Expression, TProjection>> projection, + string partitionKey = null) + 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. + TDocument GetById(TKey id, string partitionKey = null) + 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. + TDocument GetOne(FilterDefinition condition, FindOptions findOption = null, string partitionKey = null) + 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) + 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. + bool Any(FilterDefinition condition, CountOptions countOption = null, + 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 LINQ expression filter. + /// An optional partition key. + bool Any(Expression> filter, string partitionKey = null) + 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. + List GetAll(FilterDefinition condition, FindOptions findOption = null, + string partitionKey = null) + 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) + 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 + long Count(FilterDefinition condition, CountOptions countOption = null, + string partitionKey = null) + 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) + 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. + TDocument GetByMax(Expression> filter, Expression> maxValueSelector, string partitionKey = null) + 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. + TDocument GetByMin(Expression> filter, Expression> minValueSelector, string partitionKey = null) + 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. + TValue GetMaxValue(Expression> filter, Expression> maxValueSelector, string partitionKey = null) + 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. + TValue GetMinValue(Expression> filter, Expression> minValueSelector, 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. + /// 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; + + /// + /// 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/Read/MongoDbReader.Project.cs b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Project.cs index 378e86e..61639ee 100644 --- a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Project.cs +++ b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Project.cs @@ -10,7 +10,7 @@ using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Read { - public partial class MongoDbReader + public partial class MongoDbReader : IMongoDbReader { /// /// Asynchronously returns a projected document matching the filter condition. diff --git a/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs b/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs new file mode 100644 index 0000000..7550d16 --- /dev/null +++ b/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs @@ -0,0 +1,432 @@ +using System; +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.Update +{ + 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. + Task UpdateOneAsync(TDocument modifiedDocument) + 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. + Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update) + 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. + Task UpdateOneAsync(TDocument documentToModify, 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 value of the partition key. + Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + 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. + Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + 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(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 client session. + /// The document to modify. + /// The update definition. + /// The optional cancellation token. + /// + Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(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 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(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. + /// The optional cancellation token. + /// + Task UpdateOneAsync(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(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. + /// The optional cancellation token. + /// + Task UpdateOneAsync(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(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 document with the modifications you want to persist. + bool UpdateOne(TDocument modifiedDocument) + 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. + bool UpdateOne(TDocument documentToModify, UpdateDefinition update) + 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. + bool UpdateOne(TDocument documentToModify, Expression> field, TField value) + 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. + bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + 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. + bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey = null) + 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(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 client session. + /// The document to modify. + /// The update definition. + /// The optional cancellation token. + /// + bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(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 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(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. + /// The optional cancellation token. + /// + bool UpdateOne(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(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. + /// The optional cancellation token. + /// + bool UpdateOne(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(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. + 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, 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. + Task UpdateManyAsync(Expression> filter, UpdateDefinition update, string partitionKey = null) + 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 type of the field. + /// The document filter. + /// The update definition. + /// 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, 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 = 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. + long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + 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 type of the field. + /// The document filter. + /// The update definition. + /// The value of the partition key. + long UpdateMany(FilterDefinition filter, UpdateDefinition UpdateDefinition, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// 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/Update/MongoDbUpdater.cs b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs index 2a4cdbd..8fc8c93 100644 --- a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs +++ b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Update { - public partial class MongoDbUpdater : DataAccessBase + public partial class MongoDbUpdater : DataAccessBase, IMongoDbUpdater { public MongoDbUpdater(IMongoDbContext mongoDbContext) : base(mongoDbContext) { diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Create.cs b/MongoDbGenericRepository/IBaseMongoRepository.Create.cs new file mode 100644 index 0000000..8dc50e3 --- /dev/null +++ b/MongoDbGenericRepository/IBaseMongoRepository.Create.cs @@ -0,0 +1,61 @@ +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. + /// 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; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs b/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs new file mode 100644 index 0000000..06b2134 --- /dev/null +++ b/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading.Tasks; +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; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Index.cs b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs new file mode 100644 index 0000000..2b711f4 --- /dev/null +++ b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading.Tasks; +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; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs index 98131fd..3b9c28f 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs @@ -7,59 +7,18 @@ using System.Threading.Tasks; 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. - /// An optional cancellation Token. - Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) 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; - - /// - /// 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; + private volatile IMongoDbCreator _mongoDbCreator; /// /// The MongoDb accessor to insert data. /// - protected virtual MongoDbCreator MongoDbCreator + protected virtual IMongoDbCreator MongoDbCreator { get { diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs index 7324b2e..b77ad4f 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs @@ -7,98 +7,15 @@ using System.Threading.Tasks; 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 where TKey : IEquatable { - private MongoDbEraser _mongoDbEraser; + private volatile IMongoDbEraser _mongoDbEraser; /// /// The MongoDb accessor to delete data. /// - protected virtual MongoDbEraser MongoDbEraser + protected virtual IMongoDbEraser MongoDbEraser { get { diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs index c0847ba..7363be5 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs @@ -7,96 +7,6 @@ using System.Threading.Tasks; 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. @@ -104,12 +14,12 @@ namespace MongoDbGenericRepository public abstract partial class BaseMongoRepository : IBaseMongoRepository_Index where TKey : IEquatable { - private MongoDbIndexHandler _mongoDbIndexHandler; + private volatile IMongoDbIndexHandler _mongoDbIndexHandler; /// /// The MongoDb accessor to manage indexes. /// - protected virtual MongoDbIndexHandler MongoDbIndexHandler + protected virtual IMongoDbIndexHandler MongoDbIndexHandler { get { @@ -122,6 +32,7 @@ namespace MongoDbGenericRepository _mongoDbIndexHandler = new MongoDbIndexHandler(MongoDbContext); } } + return _mongoDbIndexHandler; } set { _mongoDbIndexHandler = value; } diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Main.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Main.cs index ed47b0a..eeca6ab 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Main.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Main.cs @@ -4,20 +4,6 @@ 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 - { - } - /// /// 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. @@ -41,7 +27,7 @@ namespace MongoDbGenericRepository } /// - /// The contructor taking a . + /// The constructor taking a . /// /// A mongodb context implementing protected BaseMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext) @@ -49,7 +35,7 @@ namespace MongoDbGenericRepository } /// - /// The contructor taking a . + /// The constructor taking a . /// /// A mongodb context implementing protected BaseMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase) diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs index cdbb862..a506411 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs @@ -34,7 +34,7 @@ namespace MongoDbGenericRepository /// /// A MongoDb Reader for read operations /// - protected MongoDbReader MongoDbReader = null; + protected IMongoDbReader MongoDbReader = null; /// /// The constructor taking a connection string and a database name. @@ -47,7 +47,7 @@ namespace MongoDbGenericRepository } /// - /// The contructor taking a . + /// The constructor taking a . /// /// A mongodb context implementing protected ReadOnlyMongoRepository(IMongoDatabase mongoDatabase) : this(new MongoDbContext(mongoDatabase)) @@ -55,7 +55,7 @@ namespace MongoDbGenericRepository } /// - /// The contructor taking a . + /// The constructor taking a . /// /// A mongodb context implementing protected ReadOnlyMongoRepository(IMongoDbContext mongoDbContext) diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs index b56d9c4..f6cdc50 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs @@ -7,204 +7,12 @@ 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 volatile IMongoDbUpdater _mongoDbUpdater; + + protected virtual IMongoDbUpdater MongoDbUpdater { get { diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Create.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Create.cs new file mode 100644 index 0000000..8994fde --- /dev/null +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Create.cs @@ -0,0 +1,49 @@ +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. + /// An optional cancellation Token. + Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) 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; + + /// + /// 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; + } +} \ 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..fabb293 --- /dev/null +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +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; + + /// + /// 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; + } +} \ 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..951904b --- /dev/null +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +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. + /// 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; + } +} \ 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..1fbd407 --- /dev/null +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Update.cs @@ -0,0 +1,201 @@ +using System; +using System.Linq.Expressions; +using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.Models; + +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; + } +} \ 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/MongoDbGenericRepository.csproj b/MongoDbGenericRepository/MongoDbGenericRepository.csproj index aeeb853..45c60c1 100644 --- a/MongoDbGenericRepository/MongoDbGenericRepository.csproj +++ b/MongoDbGenericRepository/MongoDbGenericRepository.csproj @@ -1,7 +1,7 @@  - net472;netstandard2.0; + netstandard2.1;net48 true MongoDbGenericRepository 1.4.8 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..201de31 100644 --- a/MongoDbGenericRepository/ReadOnlyMongoRepository.cs +++ b/MongoDbGenericRepository/ReadOnlyMongoRepository.cs @@ -26,7 +26,7 @@ namespace MongoDbGenericRepository } /// - /// The contructor taking a . + /// The constructor taking a . /// /// A mongodb context implementing protected ReadOnlyMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext) @@ -34,7 +34,7 @@ namespace MongoDbGenericRepository } /// - /// The contructor taking a . + /// The constructor taking a . /// /// A mongodb context implementing protected ReadOnlyMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase) From 02b9385fd8c182e2524b5e0b4613d02e5522fd11 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sun, 11 Jun 2023 20:23:44 +0100 Subject: [PATCH 02/40] fixed various warnings --- .../BaseMongoDbRepositoryTests.cs | 6 +- .../Infrastructure/MongoDbConfig.cs | 4 +- .../MongoDbDocumentTestBase.Main.cs | 100 +++++----- .../Abstractions/IMongoDbContext.cs | 2 - .../IReadOnlyMongoRepository.TKey.cs | 5 +- .../BaseMongoRepository.Delete.cs | 3 + .../BaseMongoRepository.Index.cs | 19 +- .../BaseMongoRepository.Update.cs | 4 +- .../DataAccess/Base/DataAccessBase.cs | 1 - .../DataAccess/Base/IDataAccessBase.cs | 3 + .../DataAccess/Create/IMongoDbCreator.cs | 51 +---- .../DataAccess/Create/MongoDbCreator.cs | 32 ++-- .../DataAccess/Delete/IMongoDbEraser.cs | 50 +---- .../DataAccess/Delete/MongoDbEraser.cs | 48 +++-- .../DataAccess/Index/IMongoDbIndexHandler.cs | 50 +---- .../DataAccess/Index/MongoDbIndexHandler.cs | 20 +- .../DataAccess/Read/IMongoDbReader.cs | 49 +---- .../DataAccess/Read/MongoDbReader.Main.cs | 16 +- .../DataAccess/Update/IMongoDbUpdater.cs | 58 +----- .../Update/MongoDbUpdater.ClientSession.cs | 124 +----------- .../DataAccess/Update/MongoDbUpdater.cs | 180 +++--------------- .../IBaseMongoRepository.Delete.cs | 3 + .../IBaseMongoRepository.Index.cs | 3 + .../BaseMongoRepository.TKey.Create.cs | 8 +- .../BaseMongoRepository.TKey.Index.cs | 14 +- .../BaseMongoRepository.TKey.ReadOnly.cs | 19 +- .../BaseMongoRepository.TKey.Update.cs | 3 + .../IBaseMongoRepository.TKey.Update.cs | 4 + .../Models/IndexCreationOptions.cs | 4 - .../ReadOnlyMongoRepository.cs | 17 +- 30 files changed, 245 insertions(+), 655 deletions(-) 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/MongoDbGenericRepository/Abstractions/IMongoDbContext.cs b/MongoDbGenericRepository/Abstractions/IMongoDbContext.cs index 2a05c8d..bca34b4 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 { diff --git a/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs index a61374c..4290a2b 100644 --- a/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs +++ b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs @@ -2,13 +2,16 @@ 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 { + /// + /// read only repository interface + /// + /// The key type public interface IReadOnlyMongoRepository where TKey : IEquatable { #region Read diff --git a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs index cba59a8..a5e8748 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs @@ -11,6 +11,9 @@ namespace MongoDbGenericRepository { private volatile IMongoDbEraser _mongoDbEraser; + /// + /// The MongoDbEraser used to delete documents. + /// protected virtual IMongoDbEraser MongoDbEraser { get diff --git a/MongoDbGenericRepository/BaseMongoRepository.Index.cs b/MongoDbGenericRepository/BaseMongoRepository.Index.cs index f74972a..f77af83 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Index.cs @@ -15,6 +15,9 @@ namespace MongoDbGenericRepository { private volatile IMongoDbIndexHandler _mongoDbIndexHandler; + /// + /// The MongoDb accessor to manage indexes. + /// protected virtual IMongoDbIndexHandler MongoDbIndexHandler { get @@ -37,7 +40,6 @@ namespace MongoDbGenericRepository /// 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) @@ -50,9 +52,10 @@ namespace MongoDbGenericRepository /// Returns the names of the indexes present on a collection. /// /// The type representing a Document. + /// The type for the key of the 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) where TDocument : IDocument where TKey : IEquatable { @@ -86,7 +89,7 @@ namespace MongoDbGenericRepository /// 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) where TDocument : IDocument where TKey : IEquatable { @@ -120,7 +123,7 @@ namespace MongoDbGenericRepository /// 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) where TDocument : IDocument where TKey : IEquatable { @@ -144,7 +147,7 @@ namespace MongoDbGenericRepository } /// - public async virtual Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public virtual async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { @@ -168,7 +171,7 @@ namespace MongoDbGenericRepository } /// - public async virtual Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public virtual async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { @@ -192,7 +195,7 @@ namespace MongoDbGenericRepository } /// - 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) where TDocument : IDocument where TKey : IEquatable { @@ -212,7 +215,7 @@ namespace MongoDbGenericRepository } /// - public async virtual Task DropIndexAsync(string indexName, string partitionKey = null) + public virtual async Task DropIndexAsync(string indexName, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { diff --git a/MongoDbGenericRepository/BaseMongoRepository.Update.cs b/MongoDbGenericRepository/BaseMongoRepository.Update.cs index c4272f5..7bc1497 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Update.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Update.cs @@ -15,6 +15,9 @@ namespace MongoDbGenericRepository { private volatile IMongoDbUpdater _mongoDbUpdater; + /// + /// The MongoDb accessor to update data. + /// protected virtual IMongoDbUpdater MongoDbUpdater { get @@ -203,7 +206,6 @@ namespace MongoDbGenericRepository /// 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. diff --git a/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs b/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs index a59d0b4..1875161 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 diff --git a/MongoDbGenericRepository/DataAccess/Base/IDataAccessBase.cs b/MongoDbGenericRepository/DataAccess/Base/IDataAccessBase.cs index b81ea09..af0fb43 100644 --- a/MongoDbGenericRepository/DataAccess/Base/IDataAccessBase.cs +++ b/MongoDbGenericRepository/DataAccess/Base/IDataAccessBase.cs @@ -6,6 +6,9 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository.DataAccess.Base { + /// + /// A interface for accessing the Database and its Collections. + /// public interface IDataAccessBase { /// diff --git a/MongoDbGenericRepository/DataAccess/Create/IMongoDbCreator.cs b/MongoDbGenericRepository/DataAccess/Create/IMongoDbCreator.cs index 018b8c4..00b299e 100644 --- a/MongoDbGenericRepository/DataAccess/Create/IMongoDbCreator.cs +++ b/MongoDbGenericRepository/DataAccess/Create/IMongoDbCreator.cs @@ -1,15 +1,15 @@ using System; using System.Collections.Generic; -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.Create { + /// + /// A interface for adding documents to the Database and its Collections. + /// public interface IMongoDbCreator : IDataAccessBase { /// @@ -57,50 +57,5 @@ namespace MongoDbGenericRepository.DataAccess.Create void AddMany(IEnumerable documents) where TDocument : IDocument where TKey : IEquatable; - - /// - /// 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/MongoDbCreator.cs b/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs index de95085..30f8693 100644 --- a/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs +++ b/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs @@ -1,6 +1,4 @@ -using MongoDB.Driver; -using MongoDB.Driver.Linq; -using MongoDbGenericRepository.DataAccess.Base; +using MongoDbGenericRepository.DataAccess.Base; using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Utils; using System; @@ -69,25 +67,29 @@ namespace MongoDbGenericRepository.DataAccess.Create 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.ToList(), null, cancellationToken); } } @@ -102,25 +104,29 @@ namespace MongoDbGenericRepository.DataAccess.Create 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()); } } else { - GetCollection().InsertMany(documents.ToList()); + GetCollection().InsertMany(documentList.ToList()); } } diff --git a/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs b/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs index 43eec28..61a3d9d 100644 --- a/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs +++ b/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs @@ -2,13 +2,14 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; -using MongoDB.Driver; -using MongoDB.Driver.Linq; 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 { /// @@ -102,50 +103,5 @@ namespace MongoDbGenericRepository.DataAccess.Delete long DeleteMany(Expression> filter, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; - - /// - /// 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/Delete/MongoDbEraser.cs b/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs index b28d027..5cf6486 100644 --- a/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs +++ b/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs @@ -1,5 +1,4 @@ using MongoDB.Driver; -using MongoDB.Driver.Linq; using MongoDbGenericRepository.DataAccess.Base; using MongoDbGenericRepository.Models; using System; @@ -10,8 +9,13 @@ using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Delete { + /// public class MongoDbEraser : DataAccessBase, IMongoDbEraser { + /// + /// The MongoDbEraser constructor. + /// + /// the MongoDb Context public MongoDbEraser(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } @@ -104,26 +108,27 @@ namespace MongoDbGenericRepository.DataAccess.Delete 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 += (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))).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))).DeletedCount; } /// @@ -137,26 +142,27 @@ namespace MongoDbGenericRepository.DataAccess.Delete 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))).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)).DeletedCount; } /// diff --git a/MongoDbGenericRepository/DataAccess/Index/IMongoDbIndexHandler.cs b/MongoDbGenericRepository/DataAccess/Index/IMongoDbIndexHandler.cs index b9b8338..247ad6a 100644 --- a/MongoDbGenericRepository/DataAccess/Index/IMongoDbIndexHandler.cs +++ b/MongoDbGenericRepository/DataAccess/Index/IMongoDbIndexHandler.cs @@ -2,13 +2,14 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; -using MongoDB.Driver; -using MongoDB.Driver.Linq; using MongoDbGenericRepository.DataAccess.Base; using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository.DataAccess.Index { + /// + /// The MongoDbIndexHandler interface. used to create indexes on collections. + /// public interface IMongoDbIndexHandler : IDataAccessBase { /// @@ -107,50 +108,5 @@ namespace MongoDbGenericRepository.DataAccess.Index Task DropIndexAsync(string indexName, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; - - /// - /// 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/Index/MongoDbIndexHandler.cs b/MongoDbGenericRepository/DataAccess/Index/MongoDbIndexHandler.cs index dbf1b9a..a4bf5e8 100644 --- a/MongoDbGenericRepository/DataAccess/Index/MongoDbIndexHandler.cs +++ b/MongoDbGenericRepository/DataAccess/Index/MongoDbIndexHandler.cs @@ -1,5 +1,4 @@ using MongoDB.Driver; -using MongoDB.Driver.Linq; using MongoDbGenericRepository.DataAccess.Base; using MongoDbGenericRepository.Models; using System; @@ -10,8 +9,13 @@ using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Index { + /// public class MongoDbIndexHandler : DataAccessBase, IMongoDbIndexHandler { + /// + /// The MongoDbIndexHandler constructor. + /// + /// The mongo db context public MongoDbIndexHandler(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } @@ -23,7 +27,7 @@ namespace MongoDbGenericRepository.DataAccess.Index /// 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) where TDocument : IDocument where TKey : IEquatable { @@ -43,7 +47,7 @@ namespace MongoDbGenericRepository.DataAccess.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) where TDocument : IDocument where TKey : IEquatable { @@ -66,7 +70,7 @@ namespace MongoDbGenericRepository.DataAccess.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) where TDocument : IDocument where TKey : IEquatable { @@ -89,7 +93,7 @@ namespace MongoDbGenericRepository.DataAccess.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 CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { @@ -112,7 +116,7 @@ namespace MongoDbGenericRepository.DataAccess.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 CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { @@ -135,7 +139,7 @@ namespace MongoDbGenericRepository.DataAccess.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) where TDocument : IDocument where TKey : IEquatable { @@ -157,7 +161,7 @@ namespace MongoDbGenericRepository.DataAccess.Index /// 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) where TDocument : IDocument where TKey : IEquatable { diff --git a/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs b/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs index f3c6e38..8665bd8 100644 --- a/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs +++ b/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs @@ -5,12 +5,14 @@ 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 interface for accessing the Database and its Collections. + /// public interface IMongoDbReader : IDataAccessBase { /// @@ -582,50 +584,5 @@ namespace MongoDbGenericRepository.DataAccess.Read string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; - - /// - /// 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/Read/MongoDbReader.Main.cs b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs index c343668..7252cc4 100644 --- a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs +++ b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs @@ -34,7 +34,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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 { @@ -98,7 +98,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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 { @@ -176,7 +176,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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 { @@ -240,7 +240,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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 { @@ -302,7 +302,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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 { @@ -336,7 +336,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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 { @@ -373,7 +373,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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 { @@ -411,7 +411,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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 { diff --git a/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs b/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs index 7550d16..abc8124 100644 --- a/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs +++ b/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs @@ -3,12 +3,14 @@ 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.Update { + /// + /// A interface for updating documents in MongoDb. + /// public interface IMongoDbUpdater : IDataAccessBase { /// @@ -323,8 +325,7 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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 update definiton /// The value of the partition key. Task UpdateManyAsync(Expression> filter, UpdateDefinition update, string partitionKey = null) where TDocument : IDocument @@ -335,7 +336,6 @@ namespace MongoDbGenericRepository.DataAccess.Update /// /// 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. @@ -376,56 +376,10 @@ namespace MongoDbGenericRepository.DataAccess.Update /// /// 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 update definition. /// The value of the partition key. - long UpdateMany(FilterDefinition filter, UpdateDefinition UpdateDefinition, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// 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) + long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; } diff --git a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs index dbb1283..edc0c5c 100644 --- a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs +++ b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs @@ -9,15 +9,7 @@ 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)) where TDocument : IDocument where TKey : IEquatable @@ -29,15 +21,7 @@ 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 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)) where TDocument : IDocument where TKey : IEquatable @@ -47,16 +31,7 @@ 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 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)) where TDocument : IDocument where TKey : IEquatable @@ -66,16 +41,7 @@ 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 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)) where TDocument : IDocument where TKey : IEquatable @@ -85,18 +51,7 @@ 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)) where TDocument : IDocument where TKey : IEquatable @@ -109,18 +64,7 @@ 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 bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable @@ -130,19 +74,7 @@ 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 async Task UpdateOneAsync(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable @@ -152,19 +84,7 @@ 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 Task UpdateOneAsync(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable @@ -172,19 +92,7 @@ namespace MongoDbGenericRepository.DataAccess.Update 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)) where TDocument : IDocument where TKey : IEquatable @@ -194,19 +102,7 @@ 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)) where TDocument : IDocument where TKey : IEquatable diff --git a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs index 8fc8c93..2d59aba 100644 --- a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs +++ b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs @@ -3,23 +3,24 @@ using MongoDbGenericRepository.DataAccess.Base; using MongoDbGenericRepository.Models; using System; using System.Linq.Expressions; -using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Update { + /// + /// 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) where TDocument : IDocument where TKey : IEquatable @@ -29,12 +30,7 @@ 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 document with the modifications you want to persist. + /// public virtual bool UpdateOne(TDocument modifiedDocument) where TDocument : IDocument where TKey : IEquatable @@ -44,13 +40,7 @@ namespace MongoDbGenericRepository.DataAccess.Update 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) where TDocument : IDocument where TKey : IEquatable @@ -60,13 +50,7 @@ namespace MongoDbGenericRepository.DataAccess.Update 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) where TDocument : IDocument where TKey : IEquatable @@ -76,15 +60,7 @@ namespace MongoDbGenericRepository.DataAccess.Update 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) where TDocument : IDocument where TKey : IEquatable @@ -94,15 +70,7 @@ namespace MongoDbGenericRepository.DataAccess.Update 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) where TDocument : IDocument where TKey : IEquatable @@ -112,16 +80,7 @@ namespace MongoDbGenericRepository.DataAccess.Update 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) where TDocument : IDocument where TKey : IEquatable @@ -131,16 +90,7 @@ namespace MongoDbGenericRepository.DataAccess.Update 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) where TDocument : IDocument where TKey : IEquatable @@ -148,16 +98,7 @@ namespace MongoDbGenericRepository.DataAccess.Update return await UpdateOneAsync(Builders.Filter.Where(filter), field, value, partitionKey); } - /// - /// 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) where TDocument : IDocument where TKey : IEquatable @@ -167,16 +108,7 @@ namespace MongoDbGenericRepository.DataAccess.Update 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) where TDocument : IDocument where TKey : IEquatable @@ -184,16 +116,7 @@ namespace MongoDbGenericRepository.DataAccess.Update return UpdateOne(Builders.Filter.Where(filter), field, value, partitionKey); } - /// - /// 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) where TDocument : IDocument where TKey : IEquatable @@ -201,16 +124,7 @@ namespace MongoDbGenericRepository.DataAccess.Update return await UpdateManyAsync(Builders.Filter.Where(filter), field, value, partitionKey); } - /// - /// 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) where TDocument : IDocument where TKey : IEquatable @@ -220,15 +134,7 @@ namespace MongoDbGenericRepository.DataAccess.Update 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) where TDocument : IDocument where TKey : IEquatable @@ -236,15 +142,7 @@ namespace MongoDbGenericRepository.DataAccess.Update return await UpdateManyAsync(Builders.Filter.Where(filter), update, partitionKey); } - /// - /// 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) where TDocument : IDocument where TKey : IEquatable @@ -254,16 +152,7 @@ namespace MongoDbGenericRepository.DataAccess.Update 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) where TDocument : IDocument where TKey : IEquatable @@ -271,16 +160,7 @@ namespace MongoDbGenericRepository.DataAccess.Update return UpdateMany(Builders.Filter.Where(filter), field, value, partitionKey); } - /// - /// 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) where TDocument : IDocument where TKey : IEquatable @@ -290,21 +170,13 @@ namespace MongoDbGenericRepository.DataAccess.Update 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) 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); return updateRes.ModifiedCount; } } diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs b/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs index 06b2134..eba3e72 100644 --- a/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs +++ b/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs @@ -6,6 +6,9 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { + /// + /// The base Mongo Repository Delete interface. used to delete documents from the collections. + /// public interface IBaseMongoRepository_Delete : IBaseMongoRepository_Delete { /// diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Index.cs b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs index 2b711f4..3db3ecf 100644 --- a/MongoDbGenericRepository/IBaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs @@ -6,6 +6,9 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { + /// + /// The interface exposing index management functionality for Guid Keyed repositories. + /// public interface IBaseMongoRepository_Index : IBaseMongoRepository_Index { /// diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs index 3b9c28f..850e1a4 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs @@ -11,7 +11,8 @@ 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. /// - public abstract partial class BaseMongoRepository : IBaseMongoRepository_Create where TKey : IEquatable + public abstract partial class BaseMongoRepository : IBaseMongoRepository_Create + where TKey : IEquatable { private volatile IMongoDbCreator _mongoDbCreator; @@ -22,11 +23,11 @@ namespace MongoDbGenericRepository { get { - if(_mongoDbCreator == null) + if (_mongoDbCreator == null) { lock (_initLock) { - if(_mongoDbCreator == null) + if (_mongoDbCreator == null) { _mongoDbCreator = new MongoDbCreator(MongoDbContext); } @@ -34,6 +35,7 @@ namespace MongoDbGenericRepository } return _mongoDbCreator; } + set { _mongoDbCreator = value; } } diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs index 7363be5..2e37414 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs @@ -44,7 +44,7 @@ namespace MongoDbGenericRepository /// 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(string partitionKey = null) where TDocument : IDocument { return await MongoDbIndexHandler.GetIndexesNamesAsync(partitionKey); @@ -60,7 +60,7 @@ namespace MongoDbGenericRepository /// 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) where TDocument : IDocument { return await MongoDbIndexHandler.CreateTextIndexAsync(field, indexCreationOptions, partitionKey); @@ -76,7 +76,7 @@ namespace MongoDbGenericRepository /// 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) where TDocument : IDocument { return await MongoDbIndexHandler.CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey); @@ -92,7 +92,7 @@ namespace MongoDbGenericRepository /// 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 CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument { return await MongoDbIndexHandler.CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey); @@ -108,7 +108,7 @@ namespace MongoDbGenericRepository /// 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 CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument { return await MongoDbIndexHandler.CreateHashedIndexAsync(field, indexCreationOptions, partitionKey); @@ -124,7 +124,7 @@ namespace MongoDbGenericRepository /// 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) where TDocument : IDocument { return await MongoDbIndexHandler.CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey); @@ -136,7 +136,7 @@ namespace MongoDbGenericRepository /// 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 DropIndexAsync(string indexName, string partitionKey = null) where TDocument : IDocument { await MongoDbIndexHandler.DropIndexAsync(indexName, partitionKey); diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs index a506411..626670a 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; -using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository @@ -29,12 +28,12 @@ namespace MongoDbGenericRepository /// /// The MongoDbContext /// - protected IMongoDbContext MongoDbContext = null; + protected IMongoDbContext MongoDbContext; /// /// A MongoDb Reader for read operations /// - protected IMongoDbReader MongoDbReader = null; + protected IMongoDbReader MongoDbReader; /// /// The constructor taking a connection string and a database name. @@ -63,19 +62,29 @@ namespace MongoDbGenericRepository SetupMongoDbContext(mongoDbContext); } + /// + /// 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)); diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs index f6cdc50..9da9cfc 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs @@ -12,6 +12,9 @@ namespace MongoDbGenericRepository { private volatile IMongoDbUpdater _mongoDbUpdater; + /// + /// The MongoDb accessor to update data. + /// protected virtual IMongoDbUpdater MongoDbUpdater { get diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Update.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Update.cs index 1fbd407..45a9ebd 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Update.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Update.cs @@ -6,6 +6,10 @@ 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 { /// 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/ReadOnlyMongoRepository.cs b/MongoDbGenericRepository/ReadOnlyMongoRepository.cs index 201de31..7d8325a 100644 --- a/MongoDbGenericRepository/ReadOnlyMongoRepository.cs +++ b/MongoDbGenericRepository/ReadOnlyMongoRepository.cs @@ -1,5 +1,4 @@ using MongoDB.Driver; -using MongoDbGenericRepository.DataAccess.Read; using MongoDbGenericRepository.Models; using System; using System.Collections.Generic; @@ -51,7 +50,7 @@ namespace MongoDbGenericRepository /// 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 { @@ -109,7 +108,7 @@ namespace MongoDbGenericRepository /// 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 { @@ -180,7 +179,7 @@ namespace MongoDbGenericRepository /// 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 { @@ -238,7 +237,7 @@ namespace MongoDbGenericRepository /// 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 { @@ -296,7 +295,7 @@ namespace MongoDbGenericRepository /// 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 { @@ -326,7 +325,7 @@ namespace MongoDbGenericRepository /// A property selector to order by descending. /// An optional partitionKey. /// An optional cancellation Token. - public async virtual Task GetByMaxAsync( + public virtual async Task GetByMaxAsync( Expression> filter, Expression> maxValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) @@ -360,7 +359,7 @@ namespace MongoDbGenericRepository /// 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, + public virtual async Task GetByMinAsync(Expression> filter, Expression> minValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) @@ -395,7 +394,7 @@ namespace MongoDbGenericRepository /// A property selector for the maximum value you are looking for. /// An optional partitionKey. /// An optional cancellation Token. - public async virtual Task GetMaxValueAsync( + public virtual async Task GetMaxValueAsync( Expression> filter, Expression> maxValueSelector, string partitionKey = null, From e23f659ffe7946ec0c9c228bc6b8c7b1c9cf759c Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Thu, 15 Jun 2023 18:32:12 +0100 Subject: [PATCH 03/40] removing volatile --- IntegrationTests/IntegrationTests.csproj | 2 +- .../Abstractions/IReadOnlyMongoRepository.TKey.cs | 3 --- MongoDbGenericRepository/BaseMongoRepository.Create.cs | 2 +- MongoDbGenericRepository/BaseMongoRepository.Delete.cs | 4 ++-- MongoDbGenericRepository/BaseMongoRepository.Index.cs | 2 +- MongoDbGenericRepository/BaseMongoRepository.Update.cs | 2 +- .../BaseMongoRepository.TKey.Create.cs | 2 +- .../BaseMongoRepository.TKey.Delete.cs | 2 +- .../BaseMongoRepository.TKey.Index.cs | 2 +- .../BaseMongoRepository.TKey.ReadOnly.cs | 10 +--------- .../BaseMongoRepository.TKey.Update.cs | 2 +- 11 files changed, 11 insertions(+), 22 deletions(-) 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/Abstractions/IReadOnlyMongoRepository.TKey.cs b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs index 4290a2b..6e93142 100644 --- a/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs +++ b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs @@ -253,7 +253,6 @@ namespace MongoDbGenericRepository /// 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. @@ -346,7 +345,6 @@ namespace MongoDbGenericRepository /// 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. @@ -366,7 +364,6 @@ namespace MongoDbGenericRepository /// 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. diff --git a/MongoDbGenericRepository/BaseMongoRepository.Create.cs b/MongoDbGenericRepository/BaseMongoRepository.Create.cs index 13465b4..53e37fa 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Create.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Create.cs @@ -14,7 +14,7 @@ namespace MongoDbGenericRepository public abstract partial class BaseMongoRepository : IBaseMongoRepository_Create { private readonly object _initLock = new object(); - private volatile IMongoDbCreator _mongoDbCreator; + private IMongoDbCreator _mongoDbCreator; /// /// The MongoDbCreator field. diff --git a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs index a5e8748..5974289 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs @@ -9,8 +9,8 @@ namespace MongoDbGenericRepository { public abstract partial class BaseMongoRepository : IBaseMongoRepository_Delete { - private volatile IMongoDbEraser _mongoDbEraser; - + private IMongoDbEraser _mongoDbEraser; + /// /// The MongoDbEraser used to delete documents. /// diff --git a/MongoDbGenericRepository/BaseMongoRepository.Index.cs b/MongoDbGenericRepository/BaseMongoRepository.Index.cs index f77af83..90741a6 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Index.cs @@ -13,7 +13,7 @@ namespace MongoDbGenericRepository /// public abstract partial class BaseMongoRepository : IBaseMongoRepository_Index { - private volatile IMongoDbIndexHandler _mongoDbIndexHandler; + private IMongoDbIndexHandler _mongoDbIndexHandler; /// /// The MongoDb accessor to manage indexes. diff --git a/MongoDbGenericRepository/BaseMongoRepository.Update.cs b/MongoDbGenericRepository/BaseMongoRepository.Update.cs index 7bc1497..6e96164 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Update.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Update.cs @@ -13,7 +13,7 @@ namespace MongoDbGenericRepository /// public abstract partial class BaseMongoRepository : IBaseMongoRepository_Update { - private volatile IMongoDbUpdater _mongoDbUpdater; + private IMongoDbUpdater _mongoDbUpdater; /// /// The MongoDb accessor to update data. diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs index 850e1a4..abd4833 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs @@ -14,7 +14,7 @@ namespace MongoDbGenericRepository public abstract partial class BaseMongoRepository : IBaseMongoRepository_Create where TKey : IEquatable { - private volatile IMongoDbCreator _mongoDbCreator; + private IMongoDbCreator _mongoDbCreator; /// /// The MongoDb accessor to insert data. diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs index b77ad4f..8d48668 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs @@ -10,7 +10,7 @@ namespace MongoDbGenericRepository public abstract partial class BaseMongoRepository: IBaseMongoRepository_Delete where TKey : IEquatable { - private volatile IMongoDbEraser _mongoDbEraser; + private IMongoDbEraser _mongoDbEraser; /// /// The MongoDb accessor to delete data. diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs index 2e37414..b7e56ce 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs @@ -14,7 +14,7 @@ namespace MongoDbGenericRepository public abstract partial class BaseMongoRepository : IBaseMongoRepository_Index where TKey : IEquatable { - private volatile IMongoDbIndexHandler _mongoDbIndexHandler; + private IMongoDbIndexHandler _mongoDbIndexHandler; /// /// The MongoDb accessor to manage indexes. diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs index 626670a..55691d4 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs @@ -390,15 +390,7 @@ 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) where TDocument : IDocument where TProjection : class diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs index 9da9cfc..53015c1 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs @@ -10,7 +10,7 @@ namespace MongoDbGenericRepository public abstract partial class BaseMongoRepository : IBaseMongoRepository_Update where TKey : IEquatable { - private volatile IMongoDbUpdater _mongoDbUpdater; + private IMongoDbUpdater _mongoDbUpdater; /// /// The MongoDb accessor to update data. From 0cf6a7e5d38137f6f893e39c3f484b31929fb241 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Thu, 15 Jun 2023 21:35:51 +0100 Subject: [PATCH 04/40] added overloads for delete --- CoreUnitTests/CoreUnitTests.csproj | 4 +- CoreUnitTests/Infrastructure/TestDocument.cs | 6 + .../TestKeyedMongoRepository.cs | 6 + .../DeleteTests/DeleteOneAsyncTests.cs | 80 ++++++ .../BaseMongoRepository.Delete.cs | 266 +++++++++++------- .../DataAccess/Delete/IMongoDbEraser.cs | 13 +- .../DataAccess/Delete/MongoDbEraser.cs | 70 ++--- .../IBaseMongoRepository.Delete.cs | 101 ++++++- .../BaseMongoRepository.TKey.Delete.cs | 148 +++++----- .../IBaseMongoRepository.TKey.Delete.cs | 88 +++++- 10 files changed, 545 insertions(+), 237 deletions(-) create mode 100644 CoreUnitTests/Infrastructure/TestDocument.cs create mode 100644 CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs diff --git a/CoreUnitTests/CoreUnitTests.csproj b/CoreUnitTests/CoreUnitTests.csproj index 35cd8dc..ea5f539 100644 --- a/CoreUnitTests/CoreUnitTests.csproj +++ b/CoreUnitTests/CoreUnitTests.csproj @@ -8,8 +8,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/CoreUnitTests/Infrastructure/TestDocument.cs b/CoreUnitTests/Infrastructure/TestDocument.cs new file mode 100644 index 0000000..ca3f13f --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestDocument.cs @@ -0,0 +1,6 @@ +namespace CoreUnitTests.Infrastructure; + +public class TestDocument +{ + +} \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs new file mode 100644 index 0000000..569667f --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs @@ -0,0 +1,6 @@ +namespace CoreUnitTests.Infrastructure; + +public class TestKeyedMongoRepository : KeyedMongoRepository +{ + +} \ No newline at end of file diff --git a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs new file mode 100644 index 0000000..629bc99 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs @@ -0,0 +1,80 @@ +using System.Threading; +using System.Threading.Tasks; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.DeleteTests; + +public class DeleteOneAsyncTests +{ + [Fact] + public async Task DeleteOneAsync_WithDocument_ShouldDeleteOne() + { + // Arrange + var repository = GetNewRepository(); + var document = new TestDocument {Name = "DeleteOneAsync_WithDocument_ShouldDeleteOne"}; + await repository.InsertOneAsync(document); + + // Act + var result = await repository.DeleteOneAsync(document); + + // Assert + result.Should().Be(1); + } + + [Fact] + public async Task DeleteOneAsync_WithDocumentAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var repository = GetNewRepository(); + var document = new TestDocument {Name = "DeleteOneAsync_WithDocumentAndCancellationToken_ShouldDeleteOne"}; + await repository.InsertOneAsync(document); + + // Act + var result = await repository.DeleteOneAsync(document, CancellationToken.None); + + // Assert + result.Should().Be(1); + } + + [Fact] + public async Task DeleteOneAsync_WithDocumentAndCancellationToken_ShouldDeleteOneWithCancellationToken() + { + // Arrange + var repository = GetNewRepository(); + var document = new TestDocument {Name = "DeleteOneAsync_WithDocumentAndCancellationToken_ShouldDeleteOneWithCancellationToken"}; + await repository.InsertOneAsync(document); + + // Act + var result = await repository.DeleteOneAsync(document, new CancellationToken(true)); + + // Assert + result.Should().Be(0); + } + + [Fact] + public async Task DeleteOneAsync_WithFilter_ShouldDeleteOne() + { + // Arrange + var repository = GetNewRepository(); + var document = new TestDocument {Name = "DeleteOneAsync_WithFilter_ShouldDeleteOne"}; + await repository.InsertOneAsync(document); + + // Act + var result = await repository.DeleteOneAsync(x => x.Name == document.Name); + + // Assert + result.Should().Be(1); + } + + [Fact] + public async Task DeleteOneAsync_WithFilterAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var repository = GetNewRepository(); + var document = new TestDocument {Name = "DeleteOneAsync_WithFilterAndCancellationToken_ShouldDeleteOne"}; + await repository.InsertOneAsync(document); + + // Act + var result = await repository.DeleteOneAsync(x => x + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs index 5974289..a7e3125 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs @@ -3,6 +3,7 @@ using MongoDbGenericRepository.Models; using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository @@ -35,94 +36,115 @@ namespace MongoDbGenericRepository #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 async Task DeleteOneAsync(TDocument document) + where TDocument : IDocument { - return await MongoDbEraser.DeleteOneAsync(document); + return await DeleteOneAsync(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 async Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbEraser.DeleteOneAsync(document, cancellationToken); + } + + /// + public virtual long DeleteOne(TDocument document) + where TDocument : IDocument { return MongoDbEraser.DeleteOne(document); } - /// - /// 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, string partitionKey = null) + where TDocument : IDocument { return MongoDbEraser.DeleteOne(filter, partitionKey); } - /// - /// 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 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) where TDocument : IDocument + /// + 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 DeleteManyAsync(IEnumerable documents) where TDocument : IDocument + /// + 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 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 MongoDbEraser.DeleteManyAsync(filter, partitionKey, CancellationToken.None); + } + + /// + 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 await DeleteManyAsync(documents, cancellationToken); + } + + /// + public virtual long DeleteMany(IEnumerable documents) + where TDocument : IDocument { return DeleteMany(documents); } - /// - /// 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(Expression> filter, string partitionKey = null) + where TDocument : IDocument { return MongoDbEraser.DeleteMany(filter, partitionKey); } @@ -131,13 +153,7 @@ namespace MongoDbGenericRepository #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 @@ -145,18 +161,20 @@ namespace MongoDbGenericRepository return MongoDbEraser.DeleteOne(document); } - /// - /// 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) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbEraser.DeleteOneAsync(document); + return await DeleteOneAsync(document, CancellationToken.None); + } + + /// + public virtual async Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbEraser.DeleteOneAsync(document, cancellationToken); } /// @@ -174,48 +192,84 @@ namespace MongoDbGenericRepository return MongoDbEraser.DeleteOne(filter, partitionKey); } - /// - /// 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); + } + + /// + public virtual async Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbEraser.DeleteManyAsync(documents, cancellationToken); } /// diff --git a/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs b/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs index 61a3d9d..12ec088 100644 --- a/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs +++ b/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using MongoDbGenericRepository.DataAccess.Base; using MongoDbGenericRepository.Models; @@ -41,8 +42,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// 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) + Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -53,8 +55,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// 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 = null) + Task DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -65,8 +68,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// 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 = null) + Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -76,8 +80,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// 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) + Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; diff --git a/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs b/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs index 5cf6486..d3d0574 100644 --- a/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs +++ b/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Delete @@ -22,13 +23,7 @@ namespace MongoDbGenericRepository.DataAccess.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 @@ -37,29 +32,16 @@ namespace MongoDbGenericRepository.DataAccess.Delete return HandlePartitioned(document).DeleteOne(filter).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 async Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) 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) where TDocument : IDocument where TKey : IEquatable @@ -67,44 +49,24 @@ namespace MongoDbGenericRepository.DataAccess.Delete return HandlePartitioned(partitionKey).DeleteOne(filter).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 DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return (await HandlePartitioned(partitionKey).DeleteOneAsync(filter)).DeletedCount; + return (await HandlePartitioned(partitionKey).DeleteOneAsync(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(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { return (await HandlePartitioned(partitionKey).DeleteManyAsync(filter)).DeletedCount; } - /// - /// 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) + /// + public virtual async Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -122,13 +84,15 @@ namespace MongoDbGenericRepository.DataAccess.Delete 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; + deleteCount += (await HandlePartitioned(group.FirstOrDefault()) + .DeleteManyAsync(x => groupIdsToDelete.Contains(x.Id), cancellationToken: cancellationToken)) + .DeletedCount; } return deleteCount; } var idsToDelete = documentList.Select(e => e.Id).ToArray(); - return (await HandlePartitioned(documentList.FirstOrDefault()).DeleteManyAsync(x => idsToDelete.Contains(x.Id))).DeletedCount; + return (await HandlePartitioned(documentList.FirstOrDefault()).DeleteManyAsync(x => idsToDelete.Contains(x.Id), cancellationToken: cancellationToken)).DeletedCount; } /// diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs b/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs index eba3e72..fcfb229 100644 --- a/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs +++ b/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using MongoDbGenericRepository.Models; @@ -33,6 +34,18 @@ namespace MongoDbGenericRepository 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; + /// /// Deletes a document matching the condition of the LINQ expression filter. /// @@ -45,6 +58,29 @@ namespace MongoDbGenericRepository 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. /// @@ -53,7 +89,43 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. - Task DeleteOneAsync(Expression> filter, string partitionKey = null) + 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; @@ -65,7 +137,20 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. - Task DeleteManyAsync(Expression> filter, string partitionKey = null) + 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; @@ -80,6 +165,18 @@ namespace MongoDbGenericRepository 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. /// diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs index 8d48668..d3047b9 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs @@ -3,11 +3,12 @@ using MongoDbGenericRepository.Models; using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository { - public abstract partial class BaseMongoRepository: IBaseMongoRepository_Delete + public abstract partial class BaseMongoRepository : IBaseMongoRepository_Delete where TKey : IEquatable { private IMongoDbEraser _mongoDbEraser; @@ -33,100 +34,113 @@ namespace MongoDbGenericRepository 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); } - /// - /// 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 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 async Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbEraser.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 MongoDbEraser.DeleteOneAsync(filter, partitionKey, cancellationToken); + } + + /// public virtual long DeleteOne(Expression> filter, string partitionKey = null) where TDocument : IDocument { return MongoDbEraser.DeleteOne(filter, partitionKey); } - /// - /// 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 - { - return await MongoDbEraser.DeleteOneAsync(filter, partitionKey); - } - - /// - /// 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 - { - return await MongoDbEraser.DeleteManyAsync(filter, partitionKey); - } - - /// - /// 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 { - 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); } - /// - /// 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 { diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs index fabb293..42217fc 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using MongoDbGenericRepository.Models; @@ -10,7 +11,8 @@ namespace MongoDbGenericRepository /// The interface exposing deletion functionality for Key typed repositories. /// /// The type of the document Id. - public interface IBaseMongoRepository_Delete where TKey : IEquatable + public interface IBaseMongoRepository_Delete + where TKey : IEquatable { /// /// Deletes a document. @@ -30,6 +32,16 @@ namespace MongoDbGenericRepository 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; + /// /// Deletes a document matching the condition of the LINQ expression filter. /// @@ -40,6 +52,25 @@ namespace MongoDbGenericRepository 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. + /// 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. /// @@ -47,7 +78,37 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. - Task DeleteOneAsync(Expression> filter, string partitionKey = null) + 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; /// @@ -57,7 +118,18 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. - Task DeleteManyAsync(Expression> filter, string partitionKey = null) + 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; /// @@ -69,6 +141,16 @@ namespace MongoDbGenericRepository 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. /// From 54e756c695dd49fa8d33800dc2d6509e0eb34b80 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Thu, 15 Jun 2023 21:56:47 +0100 Subject: [PATCH 05/40] unit tests --- .../AddTests/AddManyTests.cs | 33 +++ .../AddTests/AddOneTests.cs | 28 +++ .../IndexTests/BaseIndexTests.cs | 34 +++ .../IndexTests/CreateAscendingIndexTests.cs | 29 +++ .../IndexTests/CreateTextIndexTests.cs | 195 ++++++++++++++++++ .../IndexTests/GetIndexNamesTests.cs | 175 ++++++++++++++++ .../MainTests/AnyTests.cs | 34 +++ .../MainTests/CountTests.cs | 34 +++ CoreUnitTests/CoreUnitTests.csproj | 7 +- CoreUnitTests/Infrastructure/Model/Child.cs | 13 ++ CoreUnitTests/Infrastructure/Model/Nested.cs | 13 ++ .../Infrastructure/Model/TestDocument.cs | 32 +++ .../Model/TestDocumentWithKey.cs | 33 +++ CoreUnitTests/Infrastructure/TestDocument.cs | 6 - .../TestKeyedMongoRepository.cs | 26 ++- .../TestKeyedMongoRepositoryContext.cs | 53 +++++ .../Infrastructure/TestMongoRepository.cs | 29 +++ .../TestMongoRepositoryContext.cs | 51 +++++ 18 files changed, 817 insertions(+), 8 deletions(-) create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/IndexTests/BaseIndexTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs create mode 100644 CoreUnitTests/Infrastructure/Model/Child.cs create mode 100644 CoreUnitTests/Infrastructure/Model/Nested.cs create mode 100644 CoreUnitTests/Infrastructure/Model/TestDocument.cs create mode 100644 CoreUnitTests/Infrastructure/Model/TestDocumentWithKey.cs delete mode 100644 CoreUnitTests/Infrastructure/TestDocument.cs create mode 100644 CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs create mode 100644 CoreUnitTests/Infrastructure/TestMongoRepository.cs create mode 100644 CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs diff --git a/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs new file mode 100644 index 0000000..292e451 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Create; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests; + +public class AddManyTests : TestMongoRepositoryContext +{ + + [Fact] + public async Task AddManyAsync_EnsureTokenPassed() + { + // Arrange + Creator = new Mock(); + var token = new CancellationToken(); + var documents = new List + { + new(), new(), new() + }; + + // Act + await Sut.AddManyAsync(documents, token); + + // Assert + Creator.Verify(x => x.AddManyAsync(documents, token)); + } +} \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs new file mode 100644 index 0000000..dc290da --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs @@ -0,0 +1,28 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Create; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests; + +public class AddOneTests : TestMongoRepositoryContext +{ + [Fact] + public async Task AddOneAsync_EnsureTokenPassed() + { + // Arrange + Creator = new Mock(); + var token = new CancellationToken(); + var document = new TestDocument(); + + // Act + await Sut.AddOneAsync(document, token); + + // Assert + Creator.Verify(x => x.AddOneAsync(document, token)); + } +} \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/BaseIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/BaseIndexTests.cs new file mode 100644 index 0000000..c2276ec --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/BaseIndexTests.cs @@ -0,0 +1,34 @@ +using System.Threading; +using CoreUnitTests.Infrastructure; +using MongoDB.Bson; +using MongoDB.Driver; +using Moq; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class BaseIndexTests : TestMongoRepositoryContext +{ + protected Mock> SetupIndex(BsonDocument index, Mock> collection) + { + var asyncCursor = new Mock>(); + asyncCursor + .SetupSequence(x => x.MoveNextAsync(It.IsAny())) + .ReturnsAsync(true) + .ReturnsAsync(false); + + asyncCursor + .SetupGet(x => x.Current) + .Returns(new[] {index}); + + var indexManager = new Mock>(); + indexManager + .Setup(x => x.ListAsync(It.IsAny())) + .ReturnsAsync(asyncCursor.Object); + + collection + .SetupGet(x => x.Indexes) + .Returns(indexManager.Object); + + return asyncCursor; + } +} \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs new file mode 100644 index 0000000..53a63d2 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs @@ -0,0 +1,29 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class CreateAscendingIndexTests : BaseIndexTests +{ + [Fact] + public async Task CreateAscendingIndexAsync_EnsureTokenPassed() + { + // Arrange + IndexHandler = new Mock(); + var token = new CancellationToken(); + + // Act + Expression> fieldExpression = t => t.SomeContent2; + await Sut.CreateAscendingIndexAsync(fieldExpression, token); + + // Assert + IndexHandler.Verify(x => x.CreateAscendingIndexAsync( + fieldExpression, null, null, token)); + } +} \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs new file mode 100644 index 0000000..8a50381 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs @@ -0,0 +1,195 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using CoreUnitTests.Infrastructure.Model; +using MongoDB.Bson; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class CreateTextIndexTests : BaseIndexTests +{ + private readonly Expression> _fieldExpression = t => t.SomeContent2; + + [Fact] + public async Task Ensure_Creates_Index() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(_fieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync(_fieldExpression, null, null)); + } + + [Fact] + public async Task Ensure_Passes_Options() + { + // Arrange + IndexHandler = new Mock(); + var options = new IndexCreationOptions { Name = "theIndexName" }; + + // Act + await Sut.CreateTextIndexAsync(_fieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + _fieldExpression, options, null)); + } + + [Fact] + public async Task Ensure_Passes_PartitionKey() + { + // Arrange + const string partitionKey = "thePartitionKey"; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(_fieldExpression, partitionKey: partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + _fieldExpression, null, partitionKey)); + } + + [Fact] + public async Task Ensure_Creates_Index_With_CancellationToken() + { + // Arrange + IndexHandler = new Mock(); + var token = new CancellationToken(); + + // Act + await Sut.CreateTextIndexAsync(_fieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync(_fieldExpression, null, null, token)); + } + + [Fact] + public async Task Ensure_Passes_Options_With_CancellationToken() + { + // Arrange + IndexHandler = new Mock(); + var token = new CancellationToken(); + var options = new IndexCreationOptions { Name = "theIndexName" }; + + // Act + await Sut.CreateTextIndexAsync(_fieldExpression, token, options); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + _fieldExpression, options, null, token)); + } + + [Fact] + public async Task Ensure_Passes_PartitionKey_With_CancellationToken() + { + // Arrange + const string partitionKey = "thePartitionKey"; + var token = new CancellationToken(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(_fieldExpression, token, partitionKey: partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + _fieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task Ensure_Creates_Index_Custom_Key() + { + // Arrange + IndexHandler = new Mock(); + Expression> fieldExpression = t => t.SomeContent2; + + // Act + await Sut.CreateTextIndexAsync(fieldExpression); + + // Assert + IndexHandler.Verify(x => x.CreateTextIndexAsync(fieldExpression, null, null, default)); + } + + [Fact] + public async Task Ensure_Passes_CancellationToken_Custom_Key() + { + // Arrange + IndexHandler = new Mock(); + var token = new CancellationToken(); + + // Act + await Sut.CreateTextIndexAsync(t => t.SomeContent2, token); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + t => t.SomeContent2, null, null, token)); + } + + [Fact] + public async Task Ensure_Passes_Options_Custom_Key() + { + // Arrange + IndexHandler = new Mock(); + var options = new IndexCreationOptions { Name = "theIndexName" }; + + // Act + await Sut.CreateTextIndexAsync(t => t.SomeContent2, options); + + // Assert + IndexHandler.Verify(x => x.CreateTextIndexAsync(t => t.SomeContent2, options, null, default)); + } + + [Fact] + public async Task Ensure_Passes_PartitionKey_Custom_Key() + { + // Arrange + const string partitionKey = "thePartitionKey"; + + IndexHandler = new Mock(); + var options = new IndexCreationOptions { Name = "theIndexName" }; + + // Act + await Sut.CreateTextIndexAsync(t => t.SomeContent2, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateTextIndexAsync( + t => t.SomeContent2, options, partitionKey, default)); + } + + [Fact] + public async Task Ensure_Passes_PartitionKey_And_CancellationToken_Custom_Key() + { + // Arrange + const string partitionKey = "thePartitionKey"; + const string indexName = "theIndexName"; + var token = new CancellationToken(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateTextIndexAsync(t => t.SomeContent2, token, options, partitionKey); + + // Assert + IndexHandler + .Verify(x => x.CreateTextIndexAsync( + t => t.SomeContent2, options, partitionKey, token)); + } +} \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs new file mode 100644 index 0000000..42369b7 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs @@ -0,0 +1,175 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using CoreUnitTests.Infrastructure.Model; +using MongoDB.Bson; +using MongoDbGenericRepository.DataAccess.Index; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class GetIndexNamesTests : BaseIndexTests +{ + [Fact] + public async Task Ensure_Returns_IndexNames() + { + // Arrange + IndexHandler = new Mock(); + const string indexName = "theIndexName"; + + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(null)) + .ReturnsAsync(new List{indexName}); + + // Act + var result = await Sut.GetIndexesNamesAsync(); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + IndexHandler.Verify(x => x.GetIndexesNamesAsync(null), Times.Once()); + } + + [Fact] + public async Task Ensure_Passes_Provided_CancellationToken() + { + // Arrange + const string indexName = "theIndexName"; + var token = new CancellationToken(); + IndexHandler = new Mock(); + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(null, token)) + .ReturnsAsync(new List{indexName}); + + // Act + var result = await Sut.GetIndexesNamesAsync(token); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + } + + [Fact] + public async Task Ensure_Handles_PartitionKey() + { + // Arrange + const string partitionKey = "thePartitionKey"; + const string indexName = "theIndexName"; + + IndexHandler = new Mock(); + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(partitionKey)) + .ReturnsAsync(new List{indexName}); + + // Act + var result = await Sut.GetIndexesNamesAsync(partitionKey); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + } + + [Fact] + public async Task Ensure_Passes_Provided_CancellationToken_And_Handles_Partition_Key() + { + // Arrange + const string partitionKey = "thePartitionKey"; + const string indexName = "theIndexName"; + var token = new CancellationToken(); + + IndexHandler = new Mock(); + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(partitionKey, token)) + .ReturnsAsync(new List{indexName}); + + // Act + var result = await Sut.GetIndexesNamesAsync(token, partitionKey); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + } + + [Fact] + public async Task Ensure_Returns_IndexNames_Custom_Primary_Key() + { + // Arrange + const string indexName = "theIndexName"; + + IndexHandler = new Mock(); + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(null)) + .ReturnsAsync(new List{indexName}); + + // Act + var result = await Sut.GetIndexesNamesAsync(); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + } + + [Fact] + public async Task Ensure_Passes_Provided_CancellationToken_Custom_Primary_Key() + { + // Arrange + const string indexName = "theIndexName"; + var token = new CancellationToken(); + IndexHandler = new Mock(); + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(null, token)) + .ReturnsAsync(new List{indexName}); + + + // Act + var result = await Sut.GetIndexesNamesAsync(token); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + } + + [Fact] + public async Task Ensure_Handles_PartitionKey_Custom_Primary_Key() + { + // Arrange + const string indexName = "theIndexName"; + const string partitionKey = "thePartitionKey"; + + IndexHandler = new Mock(); + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(partitionKey)) + .ReturnsAsync(new List{indexName}); + + + // Act + var result = await Sut.GetIndexesNamesAsync(partitionKey); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + } + + [Fact] + public async Task Ensure_Passes_Provided_CancellationToken_And_Handles_Partition_Key_Custom_Primary_Key() + { + // Arrange + const string indexName = "theIndexName"; + const string partitionKey = "thePartitionKey"; + var token = new CancellationToken(); + + IndexHandler = new Mock(); + IndexHandler + .Setup(x => x.GetIndexesNamesAsync(partitionKey, token)) + .ReturnsAsync(new List{indexName}); + + // Act + var result = await Sut.GetIndexesNamesAsync(token, partitionKey); + + // Assert + Assert.NotNull(result); + Assert.Contains(result, x => x == indexName); + } +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs new file mode 100644 index 0000000..840ebe9 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs @@ -0,0 +1,34 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.MainTests; + +public class AnyTests : TestMongoRepositoryContext +{ + [Fact] + public async Task AnyAsync_EnsureTokenPassed() + { + // Arrange + var token = new CancellationToken(); + + Reader = new Mock(); + Reader + .Setup(x => x.AnyAsync(It.IsAny>(), null, null, token)) + .ReturnsAsync(true); + + // Act + await Sut.AnyAsync(t => string.IsNullOrWhiteSpace(t.SomeContent2), token); + + // Assert + Reader + .Verify(x => x.AnyAsync( + t => string.IsNullOrWhiteSpace(t.SomeContent2), null, token)); + } +} \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs new file mode 100644 index 0000000..f1f4965 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs @@ -0,0 +1,34 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.MainTests; + +public class CountTests : TestMongoRepositoryContext +{ + [Fact] + public async Task CountAsync_EnsureTokenPassed() + { + // Arrange + var token = new CancellationToken(); + + Reader = new Mock(); + Reader + .Setup(x => x.CountAsync(It.IsAny>>(), null, token)) + .ReturnsAsync(10); + + // Act + var result = await Sut.CountAsync(t => string.IsNullOrWhiteSpace(t.SomeContent2), token); + + // Assert + Assert.Equal(10, result); + Reader.Verify(x => x.CountAsync( + t => string.IsNullOrWhiteSpace(t.SomeContent2), null, token)); + } +} \ No newline at end of file diff --git a/CoreUnitTests/CoreUnitTests.csproj b/CoreUnitTests/CoreUnitTests.csproj index ea5f539..479c330 100644 --- a/CoreUnitTests/CoreUnitTests.csproj +++ b/CoreUnitTests/CoreUnitTests.csproj @@ -2,13 +2,14 @@ net6.0 - enable + warnings false + runtime; build; native; contentfiles; analyzers; buildtransitive @@ -20,4 +21,8 @@ + + + + diff --git a/CoreUnitTests/Infrastructure/Model/Child.cs b/CoreUnitTests/Infrastructure/Model/Child.cs new file mode 100644 index 0000000..0d95214 --- /dev/null +++ b/CoreUnitTests/Infrastructure/Model/Child.cs @@ -0,0 +1,13 @@ +namespace CoreUnitTests.Infrastructure.Model; + +public class Child +{ + public Child(string type, string value) + { + Type = type; + Value = value; + } + + public string Type { get; set; } + public string Value { get; set; } +} \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/Model/Nested.cs b/CoreUnitTests/Infrastructure/Model/Nested.cs new file mode 100644 index 0000000..5908c80 --- /dev/null +++ b/CoreUnitTests/Infrastructure/Model/Nested.cs @@ -0,0 +1,13 @@ +using System; +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; + +namespace CoreUnitTests.Infrastructure.Model; + +public class Nested +{ + public DateTime SomeDate { get; set; } + + [BsonRepresentation(BsonType.Decimal128)] + public decimal SomeAmount { get; set; } +} \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/Model/TestDocument.cs b/CoreUnitTests/Infrastructure/Model/TestDocument.cs new file mode 100644 index 0000000..51f87f7 --- /dev/null +++ b/CoreUnitTests/Infrastructure/Model/TestDocument.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using MongoDbGenericRepository.Models; + +namespace CoreUnitTests.Infrastructure.Model; + + +public class TestDocument : Document +{ + public TestDocument() + { + Version = 2; + Nested = new Nested + { + SomeDate = DateTime.UtcNow + }; + Children = new List(); + } + + public int SomeValue { get; set; } + + public string SomeContent { get; set; } + public string SomeContent2 { get; set; } + public string SomeContent3 { get; set; } + + public int GroupingKey { get; set; } + + public Nested Nested { get; set; } + + public List Children { get; set; } +} + diff --git a/CoreUnitTests/Infrastructure/Model/TestDocumentWithKey.cs b/CoreUnitTests/Infrastructure/Model/TestDocumentWithKey.cs new file mode 100644 index 0000000..2ff425d --- /dev/null +++ b/CoreUnitTests/Infrastructure/Model/TestDocumentWithKey.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using MongoDbGenericRepository.Models; + +namespace CoreUnitTests.Infrastructure.Model; + +public class TestDocumentWithKey : IDocument +{ + public int Id { get; set; } + public int Version { get; set; } + + public TestDocumentWithKey() + { + Version = 2; + Nested = new Nested + { + SomeDate = DateTime.UtcNow + }; + Children = new List(); + } + + public int SomeValue { get; set; } + + public string SomeContent { get; set; } + public string SomeContent2 { get; set; } + public string SomeContent3 { get; set; } + + public int GroupingKey { get; set; } + + public Nested Nested { get; set; } + + public List Children { get; set; } +} \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/TestDocument.cs b/CoreUnitTests/Infrastructure/TestDocument.cs deleted file mode 100644 index ca3f13f..0000000 --- a/CoreUnitTests/Infrastructure/TestDocument.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace CoreUnitTests.Infrastructure; - -public class TestDocument -{ - -} \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs index 569667f..16fbff0 100644 --- a/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs +++ b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs @@ -1,6 +1,30 @@ +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.DataAccess.Read; + namespace CoreUnitTests.Infrastructure; -public class TestKeyedMongoRepository : KeyedMongoRepository +public class TestKeyedMongoRepository : BaseMongoRepository { + public TestKeyedMongoRepository(IMongoDatabase mongoDatabase) + : base(mongoDatabase) + { + } + public void SetIndexHandler(IMongoDbIndexHandler indexHandler) + { + MongoDbIndexHandler = indexHandler; + } + + public void SetDbCreator(IMongoDbCreator creator) + { + MongoDbCreator = creator; + } + + public void SetReader(IMongoDbReader reader) + { + MongoDbReader = reader; + } } \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs b/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs new file mode 100644 index 0000000..8bb92f2 --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs @@ -0,0 +1,53 @@ +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; + +namespace CoreUnitTests.Infrastructure; + +public class TestKeyedMongoRepositoryContext +{ + private readonly Mock _mongoDatabase; + + private TestKeyedMongoRepository _sut; + + protected TestKeyedMongoRepositoryContext() + { + _mongoDatabase = new Mock(); + } + + protected TestKeyedMongoRepository Sut + { + get + { + if (_sut != null) + { + return _sut; + } + + _sut = new TestKeyedMongoRepository(_mongoDatabase.Object); + if (IndexHandler != null) + { + _sut.SetIndexHandler(IndexHandler.Object); + } + + if (Creator != null) + { + _sut.SetDbCreator(Creator.Object); + } + + if (Reader != null) + { + _sut.SetReader(Reader.Object); + } + + return _sut; + } + } + + protected Mock IndexHandler { get; set; } + protected Mock Creator { get; set; } + + protected Mock Reader { get; set; } +} \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/TestMongoRepository.cs b/CoreUnitTests/Infrastructure/TestMongoRepository.cs new file mode 100644 index 0000000..bf16a4c --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestMongoRepository.cs @@ -0,0 +1,29 @@ +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.DataAccess.Read; + +namespace CoreUnitTests.Infrastructure; + +public class TestMongoRepository : BaseMongoRepository +{ + public TestMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase) + { + } + + public void SetIndexHandler(IMongoDbIndexHandler indexHandler) + { + MongoDbIndexHandler = indexHandler; + } + + public void SetDbCreator(IMongoDbCreator creator) + { + MongoDbCreator = creator; + } + + public void SetReader(IMongoDbReader reader) + { + MongoDbReader = reader; + } +} \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs b/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs new file mode 100644 index 0000000..465f06b --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs @@ -0,0 +1,51 @@ +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; + +namespace CoreUnitTests.Infrastructure; + +public class TestMongoRepositoryContext +{ + private readonly Mock _mongoDatabase; + + private TestMongoRepository _sut; + + protected TestMongoRepositoryContext() + { + _mongoDatabase = new Mock(); + } + + protected TestMongoRepository Sut + { + get + { + if (_sut == null) + { + _sut = new TestMongoRepository(_mongoDatabase.Object); + if (IndexHandler != null) + { + _sut.SetIndexHandler(IndexHandler.Object); + } + + if (Creator != null) + { + _sut.SetDbCreator(Creator.Object); + } + + if (Reader != null) + { + _sut.SetReader(Reader.Object); + } + } + + return _sut; + } + } + + protected Mock IndexHandler { get; set; } + protected Mock Creator { get; set; } + + protected Mock Reader { get; set; } +} \ No newline at end of file From c6545b9448fd5493392c4aa0f26bd7b145bd84ec Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Fri, 16 Jun 2023 11:51:40 +0100 Subject: [PATCH 06/40] keyed Delete unit tests --- .../IndexTests/BaseIndexTests.cs | 5 +- .../IndexTests/CreateAscendingIndexTests.cs | 6 +- .../IndexTests/CreateTextIndexTests.cs | 42 ++--- .../IndexTests/GetIndexNamesTests.cs | 61 ++++--- .../MainTests/AnyTests.cs | 4 +- .../MainTests/CountTests.cs | 4 +- CoreUnitTests/CoreUnitTests.csproj | 2 + .../Model/TestDocumentWithKey.cs | 7 +- .../TestKeyedMongoRepository.cs | 10 +- .../TestKeyedMongoRepositoryContext.cs | 23 ++- .../Infrastructure/TestMongoRepository.cs | 5 +- .../TestMongoRepositoryContext.cs | 10 +- .../DeleteTests/DeleteManyAsyncTests.cs | 167 ++++++++++++++++++ .../DeleteTests/DeleteManyTests.cs | 82 +++++++++ .../DeleteTests/DeleteOneAsyncTests.cs | 134 ++++++++++---- .../DeleteTests/DeleteOneTests.cs | 82 +++++++++ CoreUnitTests/UnitTest1.cs | 11 -- .../Abstractions/IBaseMongoRepository.cs | 33 ++-- .../BaseMongoRepository.TKey.Delete.cs | 15 +- 19 files changed, 565 insertions(+), 138 deletions(-) create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyTests.cs create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneTests.cs delete mode 100644 CoreUnitTests/UnitTest1.cs diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/BaseIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/BaseIndexTests.cs index c2276ec..1fe74d6 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/BaseIndexTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/BaseIndexTests.cs @@ -1,3 +1,4 @@ +using System; using System.Threading; using CoreUnitTests.Infrastructure; using MongoDB.Bson; @@ -18,7 +19,7 @@ public class BaseIndexTests : TestMongoRepositoryContext asyncCursor .SetupGet(x => x.Current) - .Returns(new[] {index}); + .Returns(new[] { index }); var indexManager = new Mock>(); indexManager @@ -28,7 +29,7 @@ public class BaseIndexTests : TestMongoRepositoryContext collection .SetupGet(x => x.Indexes) .Returns(indexManager.Object); - + return asyncCursor; } } \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs index 53a63d2..abdb22c 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs @@ -11,7 +11,7 @@ namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; public class CreateAscendingIndexTests : BaseIndexTests { - [Fact] + /*[Fact] public async Task CreateAscendingIndexAsync_EnsureTokenPassed() { // Arrange @@ -20,10 +20,10 @@ public class CreateAscendingIndexTests : BaseIndexTests // Act Expression> fieldExpression = t => t.SomeContent2; - await Sut.CreateAscendingIndexAsync(fieldExpression, token); + // await Sut.CreateAscendingIndexAsync(fieldExpression, token); // Assert IndexHandler.Verify(x => x.CreateAscendingIndexAsync( fieldExpression, null, null, token)); - } + }*/ } \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs index 8a50381..0d216d3 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs @@ -3,8 +3,6 @@ using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; using CoreUnitTests.Infrastructure.Model; -using MongoDB.Bson; -using MongoDB.Driver; using MongoDbGenericRepository.DataAccess.Index; using MongoDbGenericRepository.Models; using Moq; @@ -21,7 +19,7 @@ public class CreateTextIndexTests : BaseIndexTests { // Arrange IndexHandler = new Mock(); - + // Act await Sut.CreateTextIndexAsync(_fieldExpression); @@ -29,14 +27,14 @@ public class CreateTextIndexTests : BaseIndexTests IndexHandler.Verify( x => x.CreateTextIndexAsync(_fieldExpression, null, null)); } - + [Fact] public async Task Ensure_Passes_Options() { // Arrange IndexHandler = new Mock(); var options = new IndexCreationOptions { Name = "theIndexName" }; - + // Act await Sut.CreateTextIndexAsync(_fieldExpression, options); @@ -45,7 +43,7 @@ public class CreateTextIndexTests : BaseIndexTests x => x.CreateTextIndexAsync( _fieldExpression, options, null)); } - + [Fact] public async Task Ensure_Passes_PartitionKey() { @@ -53,7 +51,7 @@ public class CreateTextIndexTests : BaseIndexTests const string partitionKey = "thePartitionKey"; IndexHandler = new Mock(); - + // Act await Sut.CreateTextIndexAsync(_fieldExpression, partitionKey: partitionKey); @@ -62,14 +60,15 @@ public class CreateTextIndexTests : BaseIndexTests x => x.CreateTextIndexAsync( _fieldExpression, null, partitionKey)); } - + + /* [Fact] public async Task Ensure_Creates_Index_With_CancellationToken() { // Arrange IndexHandler = new Mock(); var token = new CancellationToken(); - + // Act await Sut.CreateTextIndexAsync(_fieldExpression, token); @@ -77,7 +76,7 @@ public class CreateTextIndexTests : BaseIndexTests IndexHandler.Verify( x => x.CreateTextIndexAsync(_fieldExpression, null, null, token)); } - + [Fact] public async Task Ensure_Passes_Options_With_CancellationToken() { @@ -85,7 +84,7 @@ public class CreateTextIndexTests : BaseIndexTests IndexHandler = new Mock(); var token = new CancellationToken(); var options = new IndexCreationOptions { Name = "theIndexName" }; - + // Act await Sut.CreateTextIndexAsync(_fieldExpression, token, options); @@ -94,7 +93,7 @@ public class CreateTextIndexTests : BaseIndexTests x => x.CreateTextIndexAsync( _fieldExpression, options, null, token)); } - + [Fact] public async Task Ensure_Passes_PartitionKey_With_CancellationToken() { @@ -102,7 +101,7 @@ public class CreateTextIndexTests : BaseIndexTests const string partitionKey = "thePartitionKey"; var token = new CancellationToken(); IndexHandler = new Mock(); - + // Act await Sut.CreateTextIndexAsync(_fieldExpression, token, partitionKey: partitionKey); @@ -118,21 +117,21 @@ public class CreateTextIndexTests : BaseIndexTests // Arrange IndexHandler = new Mock(); Expression> fieldExpression = t => t.SomeContent2; - + // Act await Sut.CreateTextIndexAsync(fieldExpression); // Assert IndexHandler.Verify(x => x.CreateTextIndexAsync(fieldExpression, null, null, default)); } - + [Fact] public async Task Ensure_Passes_CancellationToken_Custom_Key() { // Arrange IndexHandler = new Mock(); var token = new CancellationToken(); - + // Act await Sut.CreateTextIndexAsync(t => t.SomeContent2, token); @@ -141,21 +140,21 @@ public class CreateTextIndexTests : BaseIndexTests x => x.CreateTextIndexAsync( t => t.SomeContent2, null, null, token)); } - + [Fact] public async Task Ensure_Passes_Options_Custom_Key() { // Arrange IndexHandler = new Mock(); var options = new IndexCreationOptions { Name = "theIndexName" }; - + // Act await Sut.CreateTextIndexAsync(t => t.SomeContent2, options); // Assert IndexHandler.Verify(x => x.CreateTextIndexAsync(t => t.SomeContent2, options, null, default)); } - + [Fact] public async Task Ensure_Passes_PartitionKey_Custom_Key() { @@ -173,7 +172,7 @@ public class CreateTextIndexTests : BaseIndexTests x => x.CreateTextIndexAsync( t => t.SomeContent2, options, partitionKey, default)); } - + [Fact] public async Task Ensure_Passes_PartitionKey_And_CancellationToken_Custom_Key() { @@ -183,7 +182,7 @@ public class CreateTextIndexTests : BaseIndexTests var token = new CancellationToken(); var options = new IndexCreationOptions { Name = indexName }; IndexHandler = new Mock(); - + // Act await Sut.CreateTextIndexAsync(t => t.SomeContent2, token, options, partitionKey); @@ -192,4 +191,5 @@ public class CreateTextIndexTests : BaseIndexTests .Verify(x => x.CreateTextIndexAsync( t => t.SomeContent2, options, partitionKey, token)); } + */ } \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs index 42369b7..9fbae96 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using CoreUnitTests.Infrastructure.Model; -using MongoDB.Bson; using MongoDbGenericRepository.DataAccess.Index; using Moq; using Xunit; @@ -21,7 +20,7 @@ public class GetIndexNamesTests : BaseIndexTests IndexHandler .Setup(x => x.GetIndexesNamesAsync(null)) - .ReturnsAsync(new List{indexName}); + .ReturnsAsync(new List { indexName }); // Act var result = await Sut.GetIndexesNamesAsync(); @@ -32,6 +31,7 @@ public class GetIndexNamesTests : BaseIndexTests IndexHandler.Verify(x => x.GetIndexesNamesAsync(null), Times.Once()); } + /* [Fact] public async Task Ensure_Passes_Provided_CancellationToken() { @@ -41,7 +41,7 @@ public class GetIndexNamesTests : BaseIndexTests IndexHandler = new Mock(); IndexHandler .Setup(x => x.GetIndexesNamesAsync(null, token)) - .ReturnsAsync(new List{indexName}); + .ReturnsAsync(new List { indexName }); // Act var result = await Sut.GetIndexesNamesAsync(token); @@ -50,7 +50,8 @@ public class GetIndexNamesTests : BaseIndexTests Assert.NotNull(result); Assert.Contains(result, x => x == indexName); } - + */ + [Fact] public async Task Ensure_Handles_PartitionKey() { @@ -61,7 +62,7 @@ public class GetIndexNamesTests : BaseIndexTests IndexHandler = new Mock(); IndexHandler .Setup(x => x.GetIndexesNamesAsync(partitionKey)) - .ReturnsAsync(new List{indexName}); + .ReturnsAsync(new List { indexName }); // Act var result = await Sut.GetIndexesNamesAsync(partitionKey); @@ -70,8 +71,8 @@ public class GetIndexNamesTests : BaseIndexTests Assert.NotNull(result); Assert.Contains(result, x => x == indexName); } - - [Fact] + + /*[Fact] public async Task Ensure_Passes_Provided_CancellationToken_And_Handles_Partition_Key() { // Arrange @@ -82,7 +83,7 @@ public class GetIndexNamesTests : BaseIndexTests IndexHandler = new Mock(); IndexHandler .Setup(x => x.GetIndexesNamesAsync(partitionKey, token)) - .ReturnsAsync(new List{indexName}); + .ReturnsAsync(new List { indexName }); // Act var result = await Sut.GetIndexesNamesAsync(token, partitionKey); @@ -90,18 +91,18 @@ public class GetIndexNamesTests : BaseIndexTests // Assert Assert.NotNull(result); Assert.Contains(result, x => x == indexName); - } - - [Fact] + }*/ + + /*[Fact] public async Task Ensure_Returns_IndexNames_Custom_Primary_Key() { // Arrange const string indexName = "theIndexName"; - + IndexHandler = new Mock(); IndexHandler .Setup(x => x.GetIndexesNamesAsync(null)) - .ReturnsAsync(new List{indexName}); + .ReturnsAsync(new List { indexName }); // Act var result = await Sut.GetIndexesNamesAsync(); @@ -109,9 +110,9 @@ public class GetIndexNamesTests : BaseIndexTests // Assert Assert.NotNull(result); Assert.Contains(result, x => x == indexName); - } - - [Fact] + }*/ + + /*[Fact] public async Task Ensure_Passes_Provided_CancellationToken_Custom_Primary_Key() { // Arrange @@ -120,18 +121,17 @@ public class GetIndexNamesTests : BaseIndexTests IndexHandler = new Mock(); IndexHandler .Setup(x => x.GetIndexesNamesAsync(null, token)) - .ReturnsAsync(new List{indexName}); - - + .ReturnsAsync(new List { indexName }); + // Act var result = await Sut.GetIndexesNamesAsync(token); // Assert Assert.NotNull(result); Assert.Contains(result, x => x == indexName); - } - - [Fact] + }*/ + + /*[Fact] public async Task Ensure_Handles_PartitionKey_Custom_Primary_Key() { // Arrange @@ -141,8 +141,7 @@ public class GetIndexNamesTests : BaseIndexTests IndexHandler = new Mock(); IndexHandler .Setup(x => x.GetIndexesNamesAsync(partitionKey)) - .ReturnsAsync(new List{indexName}); - + .ReturnsAsync(new List { indexName }); // Act var result = await Sut.GetIndexesNamesAsync(partitionKey); @@ -150,20 +149,20 @@ public class GetIndexNamesTests : BaseIndexTests // Assert Assert.NotNull(result); Assert.Contains(result, x => x == indexName); - } - - [Fact] + }*/ + + /*[Fact] public async Task Ensure_Passes_Provided_CancellationToken_And_Handles_Partition_Key_Custom_Primary_Key() { // Arrange const string indexName = "theIndexName"; const string partitionKey = "thePartitionKey"; var token = new CancellationToken(); - + IndexHandler = new Mock(); IndexHandler .Setup(x => x.GetIndexesNamesAsync(partitionKey, token)) - .ReturnsAsync(new List{indexName}); + .ReturnsAsync(new List { indexName }); // Act var result = await Sut.GetIndexesNamesAsync(token, partitionKey); @@ -171,5 +170,5 @@ public class GetIndexNamesTests : BaseIndexTests // Assert Assert.NotNull(result); Assert.Contains(result, x => x == indexName); - } -} + }*/ +} \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs index 840ebe9..a697fe5 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs @@ -12,7 +12,7 @@ namespace CoreUnitTests.BaseMongoRepositoryTests.MainTests; public class AnyTests : TestMongoRepositoryContext { - [Fact] + /*[Fact] public async Task AnyAsync_EnsureTokenPassed() { // Arrange @@ -30,5 +30,5 @@ public class AnyTests : TestMongoRepositoryContext Reader .Verify(x => x.AnyAsync( t => string.IsNullOrWhiteSpace(t.SomeContent2), null, token)); - } + }*/ } \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs index f1f4965..f84db84 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs @@ -12,7 +12,7 @@ namespace CoreUnitTests.BaseMongoRepositoryTests.MainTests; public class CountTests : TestMongoRepositoryContext { - [Fact] + /*[Fact] public async Task CountAsync_EnsureTokenPassed() { // Arrange @@ -30,5 +30,5 @@ public class CountTests : TestMongoRepositoryContext Assert.Equal(10, result); Reader.Verify(x => x.CountAsync( t => string.IsNullOrWhiteSpace(t.SomeContent2), null, token)); - } + }*/ } \ No newline at end of file diff --git a/CoreUnitTests/CoreUnitTests.csproj b/CoreUnitTests/CoreUnitTests.csproj index 479c330..3e3ce97 100644 --- a/CoreUnitTests/CoreUnitTests.csproj +++ b/CoreUnitTests/CoreUnitTests.csproj @@ -8,6 +8,8 @@ + + diff --git a/CoreUnitTests/Infrastructure/Model/TestDocumentWithKey.cs b/CoreUnitTests/Infrastructure/Model/TestDocumentWithKey.cs index 2ff425d..2a76873 100644 --- a/CoreUnitTests/Infrastructure/Model/TestDocumentWithKey.cs +++ b/CoreUnitTests/Infrastructure/Model/TestDocumentWithKey.cs @@ -4,11 +4,12 @@ using MongoDbGenericRepository.Models; namespace CoreUnitTests.Infrastructure.Model; -public class TestDocumentWithKey : IDocument +public class TestDocumentWithKey : IDocument + where TKey : IEquatable { - public int Id { get; set; } + public TKey Id { get; set; } public int Version { get; set; } - + public TestDocumentWithKey() { Version = 2; diff --git a/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs index 16fbff0..8780599 100644 --- a/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs +++ b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs @@ -1,12 +1,15 @@ +using System; using MongoDB.Driver; using MongoDbGenericRepository; using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.DataAccess.Delete; using MongoDbGenericRepository.DataAccess.Index; using MongoDbGenericRepository.DataAccess.Read; namespace CoreUnitTests.Infrastructure; -public class TestKeyedMongoRepository : BaseMongoRepository +public class TestKeyedMongoRepository : BaseMongoRepository + where TKey : IEquatable { public TestKeyedMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase) @@ -27,4 +30,9 @@ public class TestKeyedMongoRepository : BaseMongoRepository { MongoDbReader = reader; } + + public void SetEraser(IMongoDbEraser eraser) + { + MongoDbEraser = eraser; + } } \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs b/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs index 8bb92f2..55b4bd5 100644 --- a/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs +++ b/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs @@ -1,23 +1,30 @@ +using System; +using AutoFixture; using MongoDB.Driver; using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.DataAccess.Delete; using MongoDbGenericRepository.DataAccess.Index; using MongoDbGenericRepository.DataAccess.Read; using Moq; namespace CoreUnitTests.Infrastructure; -public class TestKeyedMongoRepositoryContext +public class TestKeyedMongoRepositoryContext + where TKey : IEquatable { private readonly Mock _mongoDatabase; - private TestKeyedMongoRepository _sut; + private TestKeyedMongoRepository _sut; protected TestKeyedMongoRepositoryContext() { _mongoDatabase = new Mock(); + Fixture = new Fixture(); } - protected TestKeyedMongoRepository Sut + protected Fixture Fixture { get; set; } + + protected TestKeyedMongoRepository Sut { get { @@ -26,7 +33,7 @@ public class TestKeyedMongoRepositoryContext return _sut; } - _sut = new TestKeyedMongoRepository(_mongoDatabase.Object); + _sut = new TestKeyedMongoRepository(_mongoDatabase.Object); if (IndexHandler != null) { _sut.SetIndexHandler(IndexHandler.Object); @@ -42,12 +49,20 @@ public class TestKeyedMongoRepositoryContext _sut.SetReader(Reader.Object); } + if (Eraser != null) + { + _sut.SetEraser(Eraser.Object); + } + return _sut; } } protected Mock IndexHandler { get; set; } + protected Mock Creator { get; set; } protected Mock Reader { get; set; } + + protected Mock Eraser { get; set; } } \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/TestMongoRepository.cs b/CoreUnitTests/Infrastructure/TestMongoRepository.cs index bf16a4c..e9ba7ac 100644 --- a/CoreUnitTests/Infrastructure/TestMongoRepository.cs +++ b/CoreUnitTests/Infrastructure/TestMongoRepository.cs @@ -8,10 +8,11 @@ namespace CoreUnitTests.Infrastructure; public class TestMongoRepository : BaseMongoRepository { - public TestMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase) + public TestMongoRepository(IMongoDatabase mongoDatabase) + : base(mongoDatabase) { } - + public void SetIndexHandler(IMongoDbIndexHandler indexHandler) { MongoDbIndexHandler = indexHandler; diff --git a/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs b/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs index 465f06b..227b356 100644 --- a/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs +++ b/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs @@ -1,5 +1,7 @@ +using AutoFixture; using MongoDB.Driver; using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.DataAccess.Delete; using MongoDbGenericRepository.DataAccess.Index; using MongoDbGenericRepository.DataAccess.Read; using Moq; @@ -15,8 +17,11 @@ public class TestMongoRepositoryContext protected TestMongoRepositoryContext() { _mongoDatabase = new Mock(); + Fixture = new Fixture(); } + public Fixture Fixture { get; set; } + protected TestMongoRepository Sut { get @@ -31,7 +36,7 @@ public class TestMongoRepositoryContext if (Creator != null) { - _sut.SetDbCreator(Creator.Object); + _sut.SetDbCreator(Creator.Object); } if (Reader != null) @@ -45,7 +50,10 @@ public class TestMongoRepositoryContext } protected Mock IndexHandler { get; set; } + protected Mock Creator { get; set; } protected Mock Reader { get; set; } + + protected Mock Eraser { get; set; } } \ No newline at end of file diff --git a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs new file mode 100644 index 0000000..4b95709 --- /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(); + 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(); + + 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(); + + 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); + } +} \ No newline at end of file diff --git a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyTests.cs new file mode 100644 index 0000000..c82848a --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyTests.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +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>>())) + .Returns(count); + + // Act + var result = Sut.DeleteMany(documents); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(documents), 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)) + .Returns(count); + + // Act + var result = Sut.DeleteMany(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(filter, null), 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>>>(), null)) + .Returns(count); + + // Act + var result = Sut.DeleteMany(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(filter, partitionKey), Times.Once); + } +} \ No newline at end of file diff --git a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs index 629bc99..de0c204 100644 --- a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs +++ b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs @@ -1,80 +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 +public class DeleteOneAsyncTests : TestKeyedMongoRepositoryContext { [Fact] - public async Task DeleteOneAsync_WithDocument_ShouldDeleteOne() + public async Task WithDocument_ShouldDeleteOne() { // Arrange - var repository = GetNewRepository(); - var document = new TestDocument {Name = "DeleteOneAsync_WithDocument_ShouldDeleteOne"}; - await repository.InsertOneAsync(document); + 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 repository.DeleteOneAsync(document); + var result = await Sut.DeleteOneAsync(document); // Assert - result.Should().Be(1); + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync, int>(document, CancellationToken.None), Times.Once); } [Fact] - public async Task DeleteOneAsync_WithDocumentAndCancellationToken_ShouldDeleteOne() + public async Task WithDocumentAndCancellationToken_ShouldDeleteOne() { // Arrange - var repository = GetNewRepository(); - var document = new TestDocument {Name = "DeleteOneAsync_WithDocumentAndCancellationToken_ShouldDeleteOne"}; - await repository.InsertOneAsync(document); + var document = Fixture.Create>(); + var count = Fixture.Create(); + var token = new CancellationToken(); + + Eraser = new Mock(); + + Eraser + .Setup(x => x.DeleteOneAsync, int>(It.IsAny>(), It.IsAny())) + .ReturnsAsync(count); // Act - var result = await repository.DeleteOneAsync(document, CancellationToken.None); + var result = await Sut.DeleteOneAsync(document, token); // Assert - result.Should().Be(1); + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync, int>(document, token), Times.Once); } [Fact] - public async Task DeleteOneAsync_WithDocumentAndCancellationToken_ShouldDeleteOneWithCancellationToken() + public async Task WithFilter_ShouldDeleteOne() { // Arrange - var repository = GetNewRepository(); - var document = new TestDocument {Name = "DeleteOneAsync_WithDocumentAndCancellationToken_ShouldDeleteOneWithCancellationToken"}; - await repository.InsertOneAsync(document); + 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 repository.DeleteOneAsync(document, new CancellationToken(true)); + var result = await Sut.DeleteOneAsync(filter); // Assert - result.Should().Be(0); + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync, int>(filter, null, CancellationToken.None), Times.Once); } [Fact] - public async Task DeleteOneAsync_WithFilter_ShouldDeleteOne() + public async Task WithFilterAndCancellationToken_ShouldDeleteOne() { // Arrange - var repository = GetNewRepository(); - var document = new TestDocument {Name = "DeleteOneAsync_WithFilter_ShouldDeleteOne"}; - await repository.InsertOneAsync(document); + var count = Fixture.Create(); + var content = Fixture.Create(); + var token = new CancellationToken(); + + 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 repository.DeleteOneAsync(x => x.Name == document.Name); + var result = await Sut.DeleteOneAsync(filter, token); // Assert - result.Should().Be(1); + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOneAsync, int>(filter, null, token), Times.Once); } [Fact] - public async Task DeleteOneAsync_WithFilterAndCancellationToken_ShouldDeleteOne() + public async Task WithFilterAndPartitionKey_ShouldDeleteOne() { // Arrange - var repository = GetNewRepository(); - var document = new TestDocument {Name = "DeleteOneAsync_WithFilterAndCancellationToken_ShouldDeleteOne"}; - await repository.InsertOneAsync(document); + 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 repository.DeleteOneAsync(x => x + 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(); + + 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); } } \ No newline at end of file diff --git a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneTests.cs new file mode 100644 index 0000000..f49d487 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneTests.cs @@ -0,0 +1,82 @@ +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>())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(document); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(document), 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())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(filter, null), 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())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(filter, partitionKey), Times.Once); + } +} \ No newline at end of file diff --git a/CoreUnitTests/UnitTest1.cs b/CoreUnitTests/UnitTest1.cs deleted file mode 100644 index 8db911c..0000000 --- a/CoreUnitTests/UnitTest1.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Xunit; - -namespace CoreUnitTests; - -public class UnitTest1 -{ - [Fact] - public void Test1() - { - } -} \ No newline at end of file diff --git a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs index f163354..ead6f9f 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs @@ -1,24 +1,24 @@ -using MongoDB.Driver; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Linq.Expressions; 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. /// @@ -29,7 +29,7 @@ namespace MongoDbGenericRepository 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. @@ -42,7 +42,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// GetAndUpdateOne with filter + /// GetAndUpdateOne with filter /// /// The type representing a Document. /// @@ -53,7 +53,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// GetAndUpdateOne with filter + /// GetAndUpdateOne with filter /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -62,8 +62,7 @@ namespace MongoDbGenericRepository /// /// Task GetAndUpdateOne(FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options) - where TDocument : IDocument - where TKey : IEquatable; + where TDocument : IDocument + where TKey : IEquatable; } - -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs index d3047b9..ed45e15 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs @@ -41,6 +41,13 @@ namespace MongoDbGenericRepository return MongoDbEraser.DeleteOne(document); } + /// + public virtual long DeleteOne(Expression> filter, string partitionKey = null) + where TDocument : IDocument + { + return MongoDbEraser.DeleteOne(filter, partitionKey); + } + /// public virtual async Task DeleteOneAsync(TDocument document) where TDocument : IDocument @@ -83,13 +90,6 @@ namespace MongoDbGenericRepository return await MongoDbEraser.DeleteOneAsync(filter, partitionKey, cancellationToken); } - /// - public virtual long DeleteOne(Expression> filter, string partitionKey = null) - where TDocument : IDocument - { - return MongoDbEraser.DeleteOne(filter, partitionKey); - } - /// public virtual async Task DeleteManyAsync(IEnumerable documents) where TDocument : IDocument @@ -104,7 +104,6 @@ namespace MongoDbGenericRepository return await MongoDbEraser.DeleteManyAsync(documents, cancellationToken); } - /// public async Task DeleteManyAsync(Expression> filter) where TDocument : IDocument From f09cfc53c2f2f4d1b41a0fba37fc3d6f7b4e8a8b Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Fri, 16 Jun 2023 17:20:04 +0100 Subject: [PATCH 07/40] Delete unit tests for the repos --- .../DeleteTests/DeleteManyAsyncTests.cs | 320 ++++++++++++++++++ .../DeleteTests/DeleteManyTests.cs | 154 +++++++++ .../DeleteTests/DeleteOneAsyncTests.cs | 296 ++++++++++++++++ .../DeleteTests/DeleteOneTests.cs | 147 ++++++++ .../Infrastructure/TestMongoRepository.cs | 6 + .../TestMongoRepositoryContext.cs | 5 + .../DeleteTests/DeleteManyTests.cs | 3 +- .../DeleteTests/DeleteOneTests.cs | 1 - .../BaseMongoRepository.Delete.cs | 23 +- 9 files changed, 938 insertions(+), 17 deletions(-) create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneTests.cs diff --git a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs new file mode 100644 index 0000000..83b8f21 --- /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(); + 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(); + + 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(); + + 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(); + 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(); + + 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(); + + 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 +} \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyTests.cs new file mode 100644 index 0000000..dbe19b6 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyTests.cs @@ -0,0 +1,154 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +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>())) + .Returns(count); + + // Act + var result = Sut.DeleteMany(documents); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany(documents), 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>>(), null)) + .Returns(count); + + // Act + var result = Sut.DeleteMany(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany(filter, null), 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())) + .Returns(count); + + // Act + var result = Sut.DeleteMany(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany(filter, partitionKey), 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>>())) + .Returns(count); + + // Act + var result = Sut.DeleteMany, int>(documents); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(documents), 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)) + .Returns(count); + + // Act + var result = Sut.DeleteMany, int>(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(filter, null), 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())) + .Returns(count); + + // Act + var result = Sut.DeleteMany, int>(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteMany, int>(filter, partitionKey), Times.Once); + } + + #endregion +} \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs new file mode 100644 index 0000000..d99e762 --- /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(); + + 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(); + + 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(); + + 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(); + + 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(); + + 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(); + + 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 +} \ No newline at end of file diff --git a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneTests.cs new file mode 100644 index 0000000..5d6385c --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneTests.cs @@ -0,0 +1,147 @@ +using System; +using System.Linq.Expressions; +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 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())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(document); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne(document), 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())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne(filter, null), 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())) + .Returns(count); + + // Act + var result = Sut.DeleteOne(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne(filter, partitionKey), 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>())) + .Returns(count); + + // Act + var result = Sut.DeleteOne, int>(document); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(document), 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())) + .Returns(count); + + // Act + var result = Sut.DeleteOne, int>(filter); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(filter, null), 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())) + .Returns(count); + + // Act + var result = Sut.DeleteOne, int>(filter, partitionKey); + + // Assert + result.Should().Be(count); + Eraser.Verify(x => x.DeleteOne, int>(filter, partitionKey), Times.Once); + } +} \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/TestMongoRepository.cs b/CoreUnitTests/Infrastructure/TestMongoRepository.cs index e9ba7ac..b204f07 100644 --- a/CoreUnitTests/Infrastructure/TestMongoRepository.cs +++ b/CoreUnitTests/Infrastructure/TestMongoRepository.cs @@ -1,6 +1,7 @@ using MongoDB.Driver; using MongoDbGenericRepository; using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.DataAccess.Delete; using MongoDbGenericRepository.DataAccess.Index; using MongoDbGenericRepository.DataAccess.Read; @@ -27,4 +28,9 @@ public class TestMongoRepository : BaseMongoRepository { MongoDbReader = reader; } + + public void SetEraser(IMongoDbEraser eraser) + { + MongoDbEraser = eraser; + } } \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs b/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs index 227b356..6dea5f0 100644 --- a/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs +++ b/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs @@ -43,6 +43,11 @@ public class TestMongoRepositoryContext { _sut.SetReader(Reader.Object); } + + if (Eraser != null) + { + _sut.SetEraser(Eraser.Object); + } } return _sut; diff --git a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyTests.cs index c82848a..90e3b85 100644 --- a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyTests.cs +++ b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyTests.cs @@ -56,6 +56,7 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext result.Should().Be(count); Eraser.Verify(x => x.DeleteMany, int>(filter, null), Times.Once); } + [Fact] public void WithFilterAndPartitionKey_ShouldDeleteMany() { @@ -69,7 +70,7 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteMany, int>(It.IsAny, bool>>>(), null)) + .Setup(x => x.DeleteMany, int>(It.IsAny, bool>>>(), It.IsAny())) .Returns(count); // Act diff --git a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneTests.cs index f49d487..721746b 100644 --- a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneTests.cs +++ b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneTests.cs @@ -1,6 +1,5 @@ using System; using System.Linq.Expressions; -using System.Threading; using AutoFixture; using CoreUnitTests.Infrastructure; using CoreUnitTests.Infrastructure.Model; diff --git a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs index a7e3125..72b3aa4 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs @@ -161,6 +161,14 @@ namespace MongoDbGenericRepository return MongoDbEraser.DeleteOne(document); } + /// + public virtual long DeleteOne(Expression> filter, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbEraser.DeleteOne(filter, partitionKey); + } + /// public virtual async Task DeleteOneAsync(TDocument document) where TDocument : IDocument @@ -177,21 +185,6 @@ namespace MongoDbGenericRepository return await MongoDbEraser.DeleteOneAsync(document, cancellationToken); } - /// - /// 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) - where TDocument : IDocument - where TKey : IEquatable - { - return MongoDbEraser.DeleteOne(filter, partitionKey); - } - /// public virtual async Task DeleteOneAsync(Expression> filter) where TDocument : IDocument From 23780697fb8e4ebb0154b011d9de7e53073100ce Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Fri, 16 Jun 2023 22:40:46 +0100 Subject: [PATCH 08/40] added some missing cancellation tokens and fixed incorrect tests --- CoreUnitTests/.editorconfig | 218 ++++++++++++++++++ .../AddTests/AddManyTests.cs | 6 +- .../AddTests/AddOneTests.cs | 4 +- .../DeleteTests/DeleteManyAsyncTests.cs | 21 +- .../DeleteTests/DeleteOneAsyncTests.cs | 14 +- .../DeleteTests/DeleteOneTests.cs | 206 +++++++++++++++-- .../IndexTests/CreateAscendingIndexTests.cs | 4 +- .../IndexTests/CreateTextIndexTests.cs | 12 +- .../IndexTests/GetIndexNamesTests.cs | 10 +- .../MainTests/AnyTests.cs | 4 +- .../MainTests/CountTests.cs | 4 +- CoreUnitTests/CoreUnitTests.csproj | 3 + .../MongoDbEraserTests/DeleteOneTests.cs | 202 ++++++++++++++++ .../FilterDefinitionExtensions.cs | 14 ++ .../Infrastructure/GenericTestContext.cs | 23 ++ .../Model/PartitionedTestDocument.cs | 9 + .../DeleteTests/DeleteManyAsyncTests.cs | 8 +- .../DeleteTests/DeleteOneAsyncTests.cs | 8 +- .../DeleteTests/DeleteOneTests.cs | 101 +++++++- .../BaseMongoRepository.Delete.cs | 114 ++++++--- .../DataAccess/Delete/IMongoDbEraser.cs | 6 +- .../DataAccess/Delete/MongoDbEraser.cs | 64 ++--- .../IBaseMongoRepository.Delete.cs | 104 ++++++--- .../BaseMongoRepository.TKey.Delete.cs | 34 ++- .../IBaseMongoRepository.TKey.Delete.cs | 59 ++++- 25 files changed, 1077 insertions(+), 175 deletions(-) create mode 100644 CoreUnitTests/.editorconfig create mode 100644 CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteOneTests.cs create mode 100644 CoreUnitTests/Infrastructure/FilterDefinitionExtensions.cs create mode 100644 CoreUnitTests/Infrastructure/GenericTestContext.cs create mode 100644 CoreUnitTests/Infrastructure/Model/PartitionedTestDocument.cs 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/AddManyTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs index 292e451..3fd2788 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs @@ -12,13 +12,13 @@ namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests; public class AddManyTests : TestMongoRepositoryContext { - + [Fact] public async Task AddManyAsync_EnsureTokenPassed() { // Arrange Creator = new Mock(); - var token = new CancellationToken(); + var token = new CancellationToken(true); var documents = new List { new(), new(), new() @@ -30,4 +30,4 @@ public class AddManyTests : TestMongoRepositoryContext // Assert Creator.Verify(x => x.AddManyAsync(documents, token)); } -} \ No newline at end of file +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs index dc290da..6396c7f 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs @@ -16,7 +16,7 @@ public class AddOneTests : TestMongoRepositoryContext { // Arrange Creator = new Mock(); - var token = new CancellationToken(); + var token = new CancellationToken(true); var document = new TestDocument(); // Act @@ -25,4 +25,4 @@ public class AddOneTests : TestMongoRepositoryContext // Assert Creator.Verify(x => x.AddOneAsync(document, token)); } -} \ No newline at end of file +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs index 83b8f21..2feed33 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs @@ -42,7 +42,7 @@ public class DeleteManyAsyncTests : TestMongoRepositoryContext // Arrange var documents = Fixture.CreateMany().ToList(); var count = Fixture.Create(); - var cancellationToken = new CancellationToken(); + var cancellationToken = new CancellationToken(true); Eraser = new Mock(); Eraser @@ -89,7 +89,7 @@ public class DeleteManyAsyncTests : TestMongoRepositoryContext // Arrange var content = Fixture.Create(); var count = Fixture.Create(); - var token = new CancellationToken(); + var token = new CancellationToken(true); Expression> filter = x => x.SomeContent == content; @@ -144,7 +144,7 @@ public class DeleteManyAsyncTests : TestMongoRepositoryContext var content = Fixture.Create(); var count = Fixture.Create(); var partitionKey = Fixture.Create(); - var token = new CancellationToken(); + var token = new CancellationToken(true); Expression> filter = x => x.SomeContent == content; @@ -193,19 +193,20 @@ public class DeleteManyAsyncTests : TestMongoRepositoryContext // Arrange var documents = Fixture.CreateMany>().ToList(); var count = Fixture.Create(); - var cancellationToken = new CancellationToken(); + var cancellationToken = new CancellationToken(true); Eraser = new Mock(); Eraser - .Setup(x => x.DeleteManyAsync, int>(It.IsAny>>(), It.IsAny())) - .ReturnsAsync(count); + .Setup(x => x.DeleteManyAsync, int>(documents, cancellationToken)) + .ReturnsAsync(count) + .Verifiable(); // Act var result = await Sut.DeleteManyAsync, int>(documents, cancellationToken); // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteManyAsync, int>(documents, cancellationToken), Times.Once); + Eraser.Verify(); } [Fact] @@ -240,7 +241,7 @@ public class DeleteManyAsyncTests : TestMongoRepositoryContext // Arrange var content = Fixture.Create(); var count = Fixture.Create(); - var token = new CancellationToken(); + var token = new CancellationToken(true); Expression, bool>> filter = x => x.SomeContent == content; @@ -295,7 +296,7 @@ public class DeleteManyAsyncTests : TestMongoRepositoryContext var content = Fixture.Create(); var count = Fixture.Create(); var partitionKey = Fixture.Create(); - var token = new CancellationToken(); + var token = new CancellationToken(true); Expression, bool>> filter = x => x.SomeContent == content; @@ -317,4 +318,4 @@ public class DeleteManyAsyncTests : TestMongoRepositoryContext } #endregion -} \ No newline at end of file +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs index d99e762..9f0fcfe 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs @@ -40,7 +40,7 @@ public class DeleteOneAsyncTests : TestMongoRepositoryContext // Arrange var document = Fixture.Create(); var count = Fixture.Create(); - var token = new CancellationToken(); + var token = new CancellationToken(true); Eraser = new Mock(); @@ -85,7 +85,7 @@ public class DeleteOneAsyncTests : TestMongoRepositoryContext // Arrange var count = Fixture.Create(); var content = Fixture.Create(); - var token = new CancellationToken(); + var token = new CancellationToken(true); Expression> filter = x => x.SomeContent == content; @@ -134,7 +134,7 @@ public class DeleteOneAsyncTests : TestMongoRepositoryContext var count = Fixture.Create(); var content = Fixture.Create(); var partitionKey = Fixture.Create(); - var token = new CancellationToken(); + var token = new CancellationToken(true); Expression> filter = x => x.SomeContent == content; @@ -180,7 +180,7 @@ public class DeleteOneAsyncTests : TestMongoRepositoryContext // Arrange var document = Fixture.Create>(); var count = Fixture.Create(); - var token = new CancellationToken(); + var token = new CancellationToken(true); Eraser = new Mock(); @@ -225,7 +225,7 @@ public class DeleteOneAsyncTests : TestMongoRepositoryContext // Arrange var count = Fixture.Create(); var content = Fixture.Create(); - var token = new CancellationToken(); + var token = new CancellationToken(true); Expression, bool>> filter = x => x.SomeContent == content; @@ -274,7 +274,7 @@ public class DeleteOneAsyncTests : TestMongoRepositoryContext var count = Fixture.Create(); var content = Fixture.Create(); var partitionKey = Fixture.Create(); - var token = new CancellationToken(); + var token = new CancellationToken(true); Expression, bool>> filter = x => x.SomeContent == content; @@ -293,4 +293,4 @@ public class DeleteOneAsyncTests : TestMongoRepositoryContext } #endregion -} \ No newline at end of file +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneTests.cs index 5d6385c..bb23e69 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteOneTests.cs @@ -1,15 +1,16 @@ -using System; +namespace CoreUnitTests.BaseMongoRepositoryTests.DeleteTests; + +using System; using System.Linq.Expressions; +using System.Threading; using AutoFixture; -using CoreUnitTests.Infrastructure; -using CoreUnitTests.Infrastructure.Model; using FluentAssertions; +using Infrastructure; +using Infrastructure.Model; using MongoDbGenericRepository.DataAccess.Delete; using Moq; using Xunit; -namespace CoreUnitTests.BaseMongoRepositoryTests.DeleteTests; - public class DeleteOneTests : TestMongoRepositoryContext { [Fact] @@ -21,7 +22,7 @@ public class DeleteOneTests : TestMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteOne(It.IsAny())) + .Setup(x => x.DeleteOne(It.IsAny(), It.IsAny())) .Returns(count); // Act @@ -29,7 +30,28 @@ public class DeleteOneTests : TestMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteOne(document), Times.Once); + 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] @@ -44,7 +66,7 @@ public class DeleteOneTests : TestMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteOne(It.IsAny>>(), It.IsAny())) + .Setup(x => x.DeleteOne(It.IsAny>>(), It.IsAny(), It.IsAny())) .Returns(count); // Act @@ -52,7 +74,35 @@ public class DeleteOneTests : TestMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteOne(filter, null), Times.Once); + 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] @@ -68,7 +118,11 @@ public class DeleteOneTests : TestMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteOne(It.IsAny>>(), It.IsAny())) + .Setup( + x => x.DeleteOne( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) .Returns(count); // Act @@ -76,7 +130,36 @@ public class DeleteOneTests : TestMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteOne(filter, partitionKey), Times.Once); + 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] @@ -88,7 +171,7 @@ public class DeleteOneTests : TestMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteOne, int>(It.IsAny>())) + .Setup(x => x.DeleteOne, int>(It.IsAny>(), It.IsAny())) .Returns(count); // Act @@ -96,8 +179,30 @@ public class DeleteOneTests : TestMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteOne, int>(document), Times.Once); + 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() { @@ -110,7 +215,11 @@ public class DeleteOneTests : TestMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteOne, int>(It.IsAny, bool>>>(), It.IsAny())) + .Setup( + x => x.DeleteOne, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) .Returns(count); // Act @@ -118,7 +227,35 @@ public class DeleteOneTests : TestMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteOne, int>(filter, null), Times.Once); + 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] @@ -134,7 +271,11 @@ public class DeleteOneTests : TestMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteOne, int>(It.IsAny, bool>>>(), It.IsAny())) + .Setup( + x => x.DeleteOne, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) .Returns(count); // Act @@ -142,6 +283,35 @@ public class DeleteOneTests : TestMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteOne, int>(filter, partitionKey), Times.Once); + Eraser.Verify(x => x.DeleteOne, int>(filter, partitionKey, CancellationToken.None), Times.Once); } -} \ No newline at end of file + + [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/CreateAscendingIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs index abdb22c..85b5b70 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs @@ -16,7 +16,7 @@ public class CreateAscendingIndexTests : BaseIndexTests { // Arrange IndexHandler = new Mock(); - var token = new CancellationToken(); + var token = new CancellationToken(true); // Act Expression> fieldExpression = t => t.SomeContent2; @@ -26,4 +26,4 @@ public class CreateAscendingIndexTests : BaseIndexTests IndexHandler.Verify(x => x.CreateAscendingIndexAsync( fieldExpression, null, null, token)); }*/ -} \ No newline at end of file +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs index 0d216d3..e7dd3be 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs @@ -67,7 +67,7 @@ public class CreateTextIndexTests : BaseIndexTests { // Arrange IndexHandler = new Mock(); - var token = new CancellationToken(); + var token = new CancellationToken(true); // Act await Sut.CreateTextIndexAsync(_fieldExpression, token); @@ -82,7 +82,7 @@ public class CreateTextIndexTests : BaseIndexTests { // Arrange IndexHandler = new Mock(); - var token = new CancellationToken(); + var token = new CancellationToken(true); var options = new IndexCreationOptions { Name = "theIndexName" }; // Act @@ -99,7 +99,7 @@ public class CreateTextIndexTests : BaseIndexTests { // Arrange const string partitionKey = "thePartitionKey"; - var token = new CancellationToken(); + var token = new CancellationToken(true); IndexHandler = new Mock(); // Act @@ -130,7 +130,7 @@ public class CreateTextIndexTests : BaseIndexTests { // Arrange IndexHandler = new Mock(); - var token = new CancellationToken(); + var token = new CancellationToken(true); // Act await Sut.CreateTextIndexAsync(t => t.SomeContent2, token); @@ -179,7 +179,7 @@ public class CreateTextIndexTests : BaseIndexTests // Arrange const string partitionKey = "thePartitionKey"; const string indexName = "theIndexName"; - var token = new CancellationToken(); + var token = new CancellationToken(true); var options = new IndexCreationOptions { Name = indexName }; IndexHandler = new Mock(); @@ -192,4 +192,4 @@ public class CreateTextIndexTests : BaseIndexTests t => t.SomeContent2, options, partitionKey, token)); } */ -} \ No newline at end of file +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs index 9fbae96..a23350a 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs @@ -37,7 +37,7 @@ public class GetIndexNamesTests : BaseIndexTests { // Arrange const string indexName = "theIndexName"; - var token = new CancellationToken(); + var token = new CancellationToken(true); IndexHandler = new Mock(); IndexHandler .Setup(x => x.GetIndexesNamesAsync(null, token)) @@ -78,7 +78,7 @@ public class GetIndexNamesTests : BaseIndexTests // Arrange const string partitionKey = "thePartitionKey"; const string indexName = "theIndexName"; - var token = new CancellationToken(); + var token = new CancellationToken(true); IndexHandler = new Mock(); IndexHandler @@ -117,7 +117,7 @@ public class GetIndexNamesTests : BaseIndexTests { // Arrange const string indexName = "theIndexName"; - var token = new CancellationToken(); + var token = new CancellationToken(true); IndexHandler = new Mock(); IndexHandler .Setup(x => x.GetIndexesNamesAsync(null, token)) @@ -157,7 +157,7 @@ public class GetIndexNamesTests : BaseIndexTests // Arrange const string indexName = "theIndexName"; const string partitionKey = "thePartitionKey"; - var token = new CancellationToken(); + var token = new CancellationToken(true); IndexHandler = new Mock(); IndexHandler @@ -171,4 +171,4 @@ public class GetIndexNamesTests : BaseIndexTests Assert.NotNull(result); Assert.Contains(result, x => x == indexName); }*/ -} \ No newline at end of file +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs index a697fe5..7d8c18d 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs @@ -16,7 +16,7 @@ public class AnyTests : TestMongoRepositoryContext public async Task AnyAsync_EnsureTokenPassed() { // Arrange - var token = new CancellationToken(); + var token = new CancellationToken(true); Reader = new Mock(); Reader @@ -31,4 +31,4 @@ public class AnyTests : TestMongoRepositoryContext .Verify(x => x.AnyAsync( t => string.IsNullOrWhiteSpace(t.SomeContent2), null, token)); }*/ -} \ No newline at end of file +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs index f84db84..7c5c0c2 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs @@ -16,7 +16,7 @@ public class CountTests : TestMongoRepositoryContext public async Task CountAsync_EnsureTokenPassed() { // Arrange - var token = new CancellationToken(); + var token = new CancellationToken(true); Reader = new Mock(); Reader @@ -31,4 +31,4 @@ public class CountTests : TestMongoRepositoryContext Reader.Verify(x => x.CountAsync( t => string.IsNullOrWhiteSpace(t.SomeContent2), null, token)); }*/ -} \ No newline at end of file +} diff --git a/CoreUnitTests/CoreUnitTests.csproj b/CoreUnitTests/CoreUnitTests.csproj index 3e3ce97..d417073 100644 --- a/CoreUnitTests/CoreUnitTests.csproj +++ b/CoreUnitTests/CoreUnitTests.csproj @@ -5,10 +5,13 @@ warnings false + true + latest + diff --git a/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteOneTests.cs b/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteOneTests.cs new file mode 100644 index 0000000..a3506be --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteOneTests.cs @@ -0,0 +1,202 @@ +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 WithDocument_DeletesOne() + { + // Arrange + var count = Fixture.Create(); + var document = Fixture.Create(); + 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(Fixture.Create>()); + + // Act + var result = Sut.DeleteOne(document); + + // Assert + result.Should().Be(count); + + var expectedFilter = Builders.Filter.Eq("Id", document.Id); + collection.Verify( + x => x.DeleteOne( + It.Is>(f => f.EquivalentTo(expectedFilter)), + CancellationToken.None)); + } + + [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(Fixture.Create>()); + + // 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)); + } + + [Fact] + public void WithFilter_DeletesOne() + { + // Arrange + var count = Fixture.Create(); + var document = Fixture.Create(); + + 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(Fixture.Create>()); + + Expression> filter = d => d.SomeContent == document.SomeContent; + + // Act + var result = Sut.DeleteOne(filter); + + // Assert + result.Should().Be(count); + collection.Verify( + x => x.DeleteOne( + It.Is>(f => f.EquivalentTo(filter)), CancellationToken.None)); + } + + [Fact] + public void WithFilterAndCancellationToken_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(Fixture.Create>()); + + Expression> filter = d => d.Id == document.Id; + + // Act + var result = Sut.DeleteOne(filter, cancellationToken: token); + + // Assert + result.Should().Be(count); + collection.Verify( + x => x.DeleteOne( + It.Is>(f => f.EquivalentTo(filter)), token)); + } + + [Fact] + public void WithFilterAndPartitionKey_DeletesOne() + { + // Arrange + var count = Fixture.Create(); + var document = Fixture.Create(); + var partitionKey = Fixture.Create(); + + 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(Fixture.Create>()); + + Expression> filter = d => d.Id == document.Id; + + // Act + var result = Sut.DeleteOne(filter, partitionKey); + + // Assert + result.Should().Be(count); + collection.Verify( + x => x.DeleteOne( + It.Is>(f => f.EquivalentTo(filter)), CancellationToken.None)); + + dbContext.Verify(x => x.GetCollection(partitionKey)); + } + + [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(Fixture.Create>()); + + 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)); + + dbContext.Verify(x => x.GetCollection(partitionKey)); + } +} 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..9b0a708 --- /dev/null +++ b/CoreUnitTests/Infrastructure/GenericTestContext.cs @@ -0,0 +1,23 @@ +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 + { + return Fixture.Freeze>(); + } + + protected IFixture Fixture { get; set; } + + protected TSut Sut { get => Fixture.Create(); } +} \ 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..da6e69f --- /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"; +} \ No newline at end of file diff --git a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs index 4b95709..22981c8 100644 --- a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs +++ b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs @@ -42,7 +42,7 @@ public class DeleteManyAsyncTests : TestKeyedMongoRepositoryContext // Arrange var documents = Fixture.CreateMany>().ToList(); var count = Fixture.Create(); - var cancellationToken = new CancellationToken(); + var cancellationToken = new CancellationToken(true); Eraser = new Mock(); Eraser @@ -89,7 +89,7 @@ public class DeleteManyAsyncTests : TestKeyedMongoRepositoryContext // Arrange var content = Fixture.Create(); var count = Fixture.Create(); - var token = new CancellationToken(); + var token = new CancellationToken(true); Expression, bool>> filter = x => x.SomeContent == content; @@ -144,7 +144,7 @@ public class DeleteManyAsyncTests : TestKeyedMongoRepositoryContext var content = Fixture.Create(); var count = Fixture.Create(); var partitionKey = Fixture.Create(); - var token = new CancellationToken(); + var token = new CancellationToken(true); Expression, bool>> filter = x => x.SomeContent == content; @@ -164,4 +164,4 @@ public class DeleteManyAsyncTests : TestKeyedMongoRepositoryContext result.Should().Be(count); Eraser.Verify(x => x.DeleteManyAsync, int>(filter, partitionKey, token), Times.Once); } -} \ No newline at end of file +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs index de0c204..4949a8f 100644 --- a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs +++ b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs @@ -40,7 +40,7 @@ public class DeleteOneAsyncTests : TestKeyedMongoRepositoryContext // Arrange var document = Fixture.Create>(); var count = Fixture.Create(); - var token = new CancellationToken(); + var token = new CancellationToken(true); Eraser = new Mock(); @@ -85,7 +85,7 @@ public class DeleteOneAsyncTests : TestKeyedMongoRepositoryContext // Arrange var count = Fixture.Create(); var content = Fixture.Create(); - var token = new CancellationToken(); + var token = new CancellationToken(true); Expression, bool>> filter = x => x.SomeContent == content; @@ -134,7 +134,7 @@ public class DeleteOneAsyncTests : TestKeyedMongoRepositoryContext var count = Fixture.Create(); var content = Fixture.Create(); var partitionKey = Fixture.Create(); - var token = new CancellationToken(); + var token = new CancellationToken(true); Expression, bool>> filter = x => x.SomeContent == content; @@ -151,4 +151,4 @@ public class DeleteOneAsyncTests : TestKeyedMongoRepositoryContext result.Should().Be(count); Eraser.Verify(x => x.DeleteOneAsync, int>(filter, partitionKey, token), Times.Once); } -} \ No newline at end of file +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneTests.cs index 721746b..2d24876 100644 --- a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneTests.cs +++ b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneTests.cs @@ -1,5 +1,6 @@ using System; using System.Linq.Expressions; +using System.Threading; using AutoFixture; using CoreUnitTests.Infrastructure; using CoreUnitTests.Infrastructure.Model; @@ -21,7 +22,9 @@ public class DeleteOneTests : TestKeyedMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteOne, int>(It.IsAny>())) + .Setup(x => x.DeleteOne, int>( + It.IsAny>(), + It.IsAny())) .Returns(count); // Act @@ -29,7 +32,30 @@ public class DeleteOneTests : TestKeyedMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteOne, int>(document), Times.Once); + 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] @@ -44,7 +70,10 @@ public class DeleteOneTests : TestKeyedMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteOne, int>(It.IsAny, bool>>>(), It.IsAny())) + .Setup(x => x.DeleteOne, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) .Returns(count); // Act @@ -52,7 +81,34 @@ public class DeleteOneTests : TestKeyedMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteOne, int>(filter, null), Times.Once); + 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] @@ -68,7 +124,10 @@ public class DeleteOneTests : TestKeyedMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteOne, int>(It.IsAny, bool>>>(), It.IsAny())) + .Setup(x => x.DeleteOne, int>( + It.IsAny, bool>>>(), + It.IsAny(), + It.IsAny())) .Returns(count); // Act @@ -76,6 +135,34 @@ public class DeleteOneTests : TestKeyedMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteOne, int>(filter, partitionKey), Times.Once); + Eraser.Verify(x => x.DeleteOne, int>(filter, partitionKey, CancellationToken.None), Times.Once); } -} \ No newline at end of file + + [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/MongoDbGenericRepository/BaseMongoRepository.Delete.cs b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs index 72b3aa4..76a17a1 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs @@ -36,6 +36,49 @@ namespace MongoDbGenericRepository #region Delete + /// + public virtual long DeleteOne(TDocument document) + where TDocument : IDocument + { + return DeleteOne(document, CancellationToken.None); + } + + /// + 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 @@ -50,20 +93,6 @@ namespace MongoDbGenericRepository return await MongoDbEraser.DeleteOneAsync(document, cancellationToken); } - /// - public virtual long DeleteOne(TDocument document) - where TDocument : IDocument - { - return MongoDbEraser.DeleteOne(document); - } - - /// - - public virtual long DeleteOne(Expression> filter, string partitionKey = null) - where TDocument : IDocument - { - return MongoDbEraser.DeleteOne(filter, partitionKey); - } /// public virtual async Task DeleteOneAsync(Expression> filter) @@ -118,7 +147,7 @@ namespace MongoDbGenericRepository public async Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbEraser.DeleteManyAsync(filter, partitionKey, CancellationToken.None); + return await MongoDbEraser.DeleteManyAsync(filter, partitionKey, cancellationToken); } /// @@ -158,15 +187,47 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable { - return MongoDbEraser.DeleteOne(document); + return DeleteOne(document, CancellationToken.None); } /// - public virtual long DeleteOne(Expression> filter, string partitionKey = null) + public virtual long DeleteOne(TDocument document, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return MongoDbEraser.DeleteOne(filter, partitionKey); + 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); } /// @@ -265,13 +326,7 @@ namespace MongoDbGenericRepository return await MongoDbEraser.DeleteManyAsync(documents, cancellationToken); } - /// - /// 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) where TDocument : IDocument where TKey : IEquatable @@ -279,14 +334,7 @@ namespace MongoDbGenericRepository return MongoDbEraser.DeleteMany(documents); } - /// - /// 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) where TDocument : IDocument where TKey : IEquatable diff --git a/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs b/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs index 12ec088..23a1eaf 100644 --- a/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs +++ b/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs @@ -19,8 +19,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// 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) + long DeleteOne(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -31,8 +32,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// 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 = null) + long DeleteOne(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; diff --git a/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs b/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs index d3d0574..902b107 100644 --- a/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs +++ b/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs @@ -1,12 +1,12 @@ -using MongoDB.Driver; -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 { @@ -14,7 +14,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete public class MongoDbEraser : DataAccessBase, IMongoDbEraser { /// - /// The MongoDbEraser constructor. + /// The MongoDbEraser constructor. /// /// the MongoDb Context public MongoDbEraser(IMongoDbContext mongoDbContext) : base(mongoDbContext) @@ -24,12 +24,20 @@ namespace MongoDbGenericRepository.DataAccess.Delete #region Delete TKey /// - 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; + } + + /// + public virtual long DeleteOne(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + { + return HandlePartitioned(partitionKey).DeleteOne(filter, cancellationToken).DeletedCount; } /// @@ -42,15 +50,10 @@ namespace MongoDbGenericRepository.DataAccess.Delete } /// - public virtual long DeleteOne(Expression> filter, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - { - return HandlePartitioned(partitionKey).DeleteOne(filter).DeletedCount; - } - - /// - public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + public virtual async Task DeleteOneAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -58,7 +61,10 @@ namespace MongoDbGenericRepository.DataAccess.Delete } /// - public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + public virtual async Task DeleteManyAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -71,7 +77,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete where TKey : IEquatable { var documentList = documents.ToList(); - + if (!documentList.Any()) { return 0; @@ -81,22 +87,24 @@ namespace MongoDbGenericRepository.DataAccess.Delete if (documentList.Any(e => e is IPartitionedDocument)) { long deleteCount = 0; - foreach (var group in documentList.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), cancellationToken: cancellationToken)) + .DeleteManyAsync(x => groupIdsToDelete.Contains(x.Id), cancellationToken)) .DeletedCount; } + return deleteCount; } var idsToDelete = documentList.Select(e => e.Id).ToArray(); - return (await HandlePartitioned(documentList.FirstOrDefault()).DeleteManyAsync(x => idsToDelete.Contains(x.Id), cancellationToken: cancellationToken)).DeletedCount; + return (await HandlePartitioned(documentList.FirstOrDefault()).DeleteManyAsync(x => idsToDelete.Contains(x.Id), cancellationToken)) + .DeletedCount; } /// - /// Deletes a list of documents. + /// Deletes a list of documents. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -107,7 +115,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete where TKey : IEquatable { var documentList = documents.ToList(); - + if (!documentList.Any()) { return 0; @@ -117,11 +125,12 @@ namespace MongoDbGenericRepository.DataAccess.Delete if (documentList.Any(e => e is IPartitionedDocument)) { long deleteCount = 0; - foreach (var group in documentList.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; + deleteCount += HandlePartitioned(group.FirstOrDefault()).DeleteMany(x => groupIdsToDelete.Contains(x.Id)).DeletedCount; } + return deleteCount; } @@ -130,7 +139,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete } /// - /// Deletes the documents matching the condition of the LINQ expression filter. + /// 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. @@ -145,6 +154,5 @@ namespace MongoDbGenericRepository.DataAccess.Delete } #endregion - } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs b/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs index fcfb229..930870f 100644 --- a/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs +++ b/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs @@ -8,12 +8,12 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The base Mongo Repository Delete interface. used to delete documents from the collections. + /// The base Mongo Repository Delete interface. used to delete documents from the collections. /// public interface IBaseMongoRepository_Delete : IBaseMongoRepository_Delete { /// - /// Deletes a document. + /// Deletes a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -24,7 +24,67 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// 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. @@ -35,7 +95,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// 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. @@ -47,19 +107,7 @@ namespace MongoDbGenericRepository 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. + /// 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. @@ -70,7 +118,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// 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. @@ -82,7 +130,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// 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. @@ -94,7 +142,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// 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. @@ -107,7 +155,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// 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. @@ -118,7 +166,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// 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. @@ -130,7 +178,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// 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. @@ -142,7 +190,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// 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. @@ -155,7 +203,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Asynchronously deletes a list of documents. + /// Asynchronously deletes a list of documents. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -166,7 +214,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Asynchronously deletes a list of documents. + /// Asynchronously deletes a list of documents. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -178,7 +226,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Deletes a list of documents. + /// Deletes a list of documents. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -189,7 +237,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Deletes the documents matching the condition of the LINQ expression filter. + /// 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. diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs index ed45e15..839a92b 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs @@ -38,14 +38,42 @@ namespace MongoDbGenericRepository public virtual long DeleteOne(TDocument document) where TDocument : IDocument { - return MongoDbEraser.DeleteOne(document); + return DeleteOne(document, CancellationToken.None); } /// - public virtual long DeleteOne(Expression> filter, string partitionKey = null) + public virtual long DeleteOne(TDocument document, CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbEraser.DeleteOne(filter, partitionKey); + 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); } /// diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs index 42217fc..c747d91 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs @@ -23,6 +23,56 @@ namespace MongoDbGenericRepository 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. /// @@ -42,15 +92,6 @@ namespace MongoDbGenericRepository Task DeleteOneAsync(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. - /// 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. From b285c7d91997647d03c954c6d28f2d6f3785e48c Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Fri, 16 Jun 2023 22:41:45 +0100 Subject: [PATCH 09/40] reverting verify change --- .../DeleteTests/DeleteManyAsyncTests.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs index 2feed33..07bad84 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyAsyncTests.cs @@ -197,16 +197,15 @@ public class DeleteManyAsyncTests : TestMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteManyAsync, int>(documents, cancellationToken)) - .ReturnsAsync(count) - .Verifiable(); + .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(); + Eraser.Verify(x => x.DeleteManyAsync, int>(documents, cancellationToken), Times.Once); } [Fact] From 1b8f795e00d9c4bfe7f5507793ad7b9eb983d80a Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sat, 17 Jun 2023 17:49:49 +0100 Subject: [PATCH 10/40] added cancellation tokens to sync deletes and added more unit tests --- .../DeleteTests/DeleteManyTests.cs | 166 ++++++++++++++++-- .../DeleteManyAsyncTests.cs | 157 +++++++++++++++++ .../MongoDbEraserTests/DeleteManyTests.cs | 157 +++++++++++++++++ .../MongoDbEraserTests/DeleteOneAsyncTests.cs | 85 +++++++++ .../MongoDbEraserTests/DeleteOneTests.cs | 132 +------------- .../Model/PartitionedTestDocument.cs | 2 +- .../DeleteTests/DeleteManyTests.cs | 97 +++++++++- .../BaseMongoRepository.Delete.cs | 84 +++++++-- .../DataAccess/Delete/IMongoDbEraser.cs | 10 +- .../DataAccess/Delete/MongoDbEraser.cs | 35 ++-- .../IBaseMongoRepository.Delete.cs | 52 +++++- .../BaseMongoRepository.TKey.Delete.cs | 34 +++- .../IBaseMongoRepository.TKey.Delete.cs | 42 ++++- 13 files changed, 861 insertions(+), 192 deletions(-) create mode 100644 CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteManyAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteManyTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteOneAsyncTests.cs diff --git a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyTests.cs index dbe19b6..afa1cb4 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/DeleteTests/DeleteManyTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Threading; using AutoFixture; using CoreUnitTests.Infrastructure; using CoreUnitTests.Infrastructure.Model; @@ -23,7 +24,7 @@ public class DeleteManyTests : TestMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteMany(It.IsAny>())) + .Setup(x => x.DeleteMany(It.IsAny>(), It.IsAny())) .Returns(count); // Act @@ -31,7 +32,28 @@ public class DeleteManyTests : TestMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteMany(documents), Times.Once); + 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] @@ -46,7 +68,7 @@ public class DeleteManyTests : TestMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteMany(It.IsAny>>(), null)) + .Setup(x => x.DeleteMany(It.IsAny>>(), It.IsAny(), It.IsAny())) .Returns(count); // Act @@ -54,7 +76,31 @@ public class DeleteManyTests : TestMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteMany(filter, null), Times.Once); + 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] @@ -70,7 +116,7 @@ public class DeleteManyTests : TestMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteMany(It.IsAny>>(), It.IsAny())) + .Setup(x => x.DeleteMany(It.IsAny>>(), It.IsAny(), It.IsAny())) .Returns(count); // Act @@ -78,7 +124,32 @@ public class DeleteManyTests : TestMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteMany(filter, partitionKey), Times.Once); + 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 @@ -92,7 +163,7 @@ public class DeleteManyTests : TestMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteMany, int>(It.IsAny>>())) + .Setup(x => x.DeleteMany, int>(It.IsAny>>(), It.IsAny())) .Returns(count); // Act @@ -100,7 +171,28 @@ public class DeleteManyTests : TestMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteMany, int>(documents), Times.Once); + 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] @@ -115,7 +207,7 @@ public class DeleteManyTests : TestMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteMany, int>(It.IsAny, bool>>>(), null)) + .Setup(x => x.DeleteMany, int>(It.IsAny, bool>>>(), null, CancellationToken.None)) .Returns(count); // Act @@ -123,7 +215,31 @@ public class DeleteManyTests : TestMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteMany, int>(filter, null), Times.Once); + 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] @@ -139,7 +255,7 @@ public class DeleteManyTests : TestMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteMany, int>(It.IsAny, bool>>>(), It.IsAny())) + .Setup(x => x.DeleteMany, int>(It.IsAny, bool>>>(), It.IsAny(), It.IsAny())) .Returns(count); // Act @@ -147,8 +263,32 @@ public class DeleteManyTests : TestMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteMany, int>(filter, partitionKey), Times.Once); + 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 -} \ No newline at end of file +} 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 index a3506be..155a0e5 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteOneTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteOneTests.cs @@ -15,36 +15,6 @@ namespace CoreUnitTests.DataAccessTests.MongoDbEraserTests; public class DeleteOneTests : GenericTestContext { - [Fact] - public void WithDocument_DeletesOne() - { - // Arrange - var count = Fixture.Create(); - var document = Fixture.Create(); - 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(Fixture.Create>()); - - // Act - var result = Sut.DeleteOne(document); - - // Assert - result.Should().Be(count); - - var expectedFilter = Builders.Filter.Eq("Id", document.Id); - collection.Verify( - x => x.DeleteOne( - It.Is>(f => f.EquivalentTo(expectedFilter)), - CancellationToken.None)); - } - [Fact] public void WithDocumentAndCancellationToken_DeletesOne() { @@ -61,7 +31,7 @@ public class DeleteOneTests : GenericTestContext var dbContext = MockOf(); dbContext .Setup(x => x.GetCollection(null)) - .Returns(Fixture.Create>()); + .Returns(collection.Object); // Act var result = Sut.DeleteOne(document, token); @@ -73,98 +43,8 @@ public class DeleteOneTests : GenericTestContext collection.Verify( x => x.DeleteOne( It.Is>(f => f.EquivalentTo(expectedFilter)), - token)); - } - - [Fact] - public void WithFilter_DeletesOne() - { - // Arrange - var count = Fixture.Create(); - var document = Fixture.Create(); - - 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(Fixture.Create>()); - - Expression> filter = d => d.SomeContent == document.SomeContent; - - // Act - var result = Sut.DeleteOne(filter); - - // Assert - result.Should().Be(count); - collection.Verify( - x => x.DeleteOne( - It.Is>(f => f.EquivalentTo(filter)), CancellationToken.None)); - } - - [Fact] - public void WithFilterAndCancellationToken_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(Fixture.Create>()); - - Expression> filter = d => d.Id == document.Id; - - // Act - var result = Sut.DeleteOne(filter, cancellationToken: token); - - // Assert - result.Should().Be(count); - collection.Verify( - x => x.DeleteOne( - It.Is>(f => f.EquivalentTo(filter)), token)); - } - - [Fact] - public void WithFilterAndPartitionKey_DeletesOne() - { - // Arrange - var count = Fixture.Create(); - var document = Fixture.Create(); - var partitionKey = Fixture.Create(); - - 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(Fixture.Create>()); - - Expression> filter = d => d.Id == document.Id; - - // Act - var result = Sut.DeleteOne(filter, partitionKey); - - // Assert - result.Should().Be(count); - collection.Verify( - x => x.DeleteOne( - It.Is>(f => f.EquivalentTo(filter)), CancellationToken.None)); - - dbContext.Verify(x => x.GetCollection(partitionKey)); + token), + Times.Once()); } [Fact] @@ -184,7 +64,7 @@ public class DeleteOneTests : GenericTestContext var dbContext = MockOf(); dbContext .Setup(x => x.GetCollection(It.IsAny())) - .Returns(Fixture.Create>()); + .Returns(collection.Object); Expression> filter = d => d.Id == document.Id; @@ -195,7 +75,9 @@ public class DeleteOneTests : GenericTestContext result.Should().Be(count); collection.Verify( x => x.DeleteOne( - It.Is>(f => f.EquivalentTo(filter)), token)); + It.Is>(f => f.EquivalentTo(filter)), + token), + Times.Once()); dbContext.Verify(x => x.GetCollection(partitionKey)); } diff --git a/CoreUnitTests/Infrastructure/Model/PartitionedTestDocument.cs b/CoreUnitTests/Infrastructure/Model/PartitionedTestDocument.cs index da6e69f..082e5aa 100644 --- a/CoreUnitTests/Infrastructure/Model/PartitionedTestDocument.cs +++ b/CoreUnitTests/Infrastructure/Model/PartitionedTestDocument.cs @@ -6,4 +6,4 @@ public class PartitionedTestDocument : TestDocument, IPartitionedDocument { /// public string PartitionKey { get; set; } = "PartitionedTestDocument"; -} \ No newline at end of file +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyTests.cs index 90e3b85..8bcddfa 100644 --- a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyTests.cs +++ b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteManyTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Threading; using AutoFixture; using CoreUnitTests.Infrastructure; using CoreUnitTests.Infrastructure.Model; @@ -23,7 +24,7 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteMany, int>(It.IsAny>>())) + .Setup(x => x.DeleteMany, int>(It.IsAny>>(), CancellationToken.None)) .Returns(count); // Act @@ -31,7 +32,29 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteMany, int>(documents), Times.Once); + 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] @@ -46,7 +69,7 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteMany, int>(It.IsAny, bool>>>(), null)) + .Setup(x => x.DeleteMany, int>(It.IsAny, bool>>>(), null, CancellationToken.None)) .Returns(count); // Act @@ -54,7 +77,35 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteMany, int>(filter, null), Times.Once); + 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] @@ -70,7 +121,11 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext Eraser = new Mock(); Eraser - .Setup(x => x.DeleteMany, int>(It.IsAny, bool>>>(), It.IsAny())) + .Setup( + x => x.DeleteMany, int>( + It.IsAny, bool>>>(), + It.IsAny(), + CancellationToken.None)) .Returns(count); // Act @@ -78,6 +133,34 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext // Assert result.Should().Be(count); - Eraser.Verify(x => x.DeleteMany, int>(filter, partitionKey), Times.Once); + Eraser.Verify(x => x.DeleteMany, int>(filter, partitionKey, CancellationToken.None), Times.Once); } -} \ No newline at end of file + + [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/MongoDbGenericRepository/BaseMongoRepository.Delete.cs b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs index 76a17a1..ebb8fd8 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs @@ -47,10 +47,9 @@ namespace MongoDbGenericRepository public virtual long DeleteOne(TDocument document, CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbEraser.DeleteOne(document, cancellationToken); + return DeleteOne(document, cancellationToken); } - /// public virtual long DeleteOne(Expression> filter) where TDocument : IDocument @@ -76,7 +75,7 @@ namespace MongoDbGenericRepository public virtual long DeleteOne(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbEraser.DeleteOne(filter, partitionKey, cancellationToken); + return DeleteOne(filter, partitionKey, cancellationToken); } /// @@ -90,10 +89,9 @@ namespace MongoDbGenericRepository public virtual async Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbEraser.DeleteOneAsync(document, cancellationToken); + return await DeleteOneAsync(document, cancellationToken); } - /// public virtual async Task DeleteOneAsync(Expression> filter) where TDocument : IDocument @@ -119,7 +117,7 @@ namespace MongoDbGenericRepository public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbEraser.DeleteOneAsync(filter, partitionKey, cancellationToken); + return await DeleteOneAsync(filter, partitionKey, cancellationToken); } /// @@ -147,7 +145,7 @@ namespace MongoDbGenericRepository public async Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbEraser.DeleteManyAsync(filter, partitionKey, cancellationToken); + return await DeleteManyAsync(filter, partitionKey, cancellationToken); } /// @@ -168,14 +166,42 @@ namespace MongoDbGenericRepository public virtual long DeleteMany(IEnumerable documents) where TDocument : IDocument { - return DeleteMany(documents); + return DeleteMany(documents, CancellationToken.None); } /// - 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 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 @@ -331,15 +357,47 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable { - return MongoDbEraser.DeleteMany(documents); + return DeleteMany(documents, CancellationToken.None); } /// - 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 diff --git a/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs b/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs index 23a1eaf..f56873b 100644 --- a/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs +++ b/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs @@ -21,7 +21,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// The document you want to delete. /// An optional cancellation token /// The number of documents deleted. - long DeleteOne(TDocument document, CancellationToken cancellationToken = default) + long DeleteOne(TDocument document, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -34,7 +34,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// An optional partition key. /// An optional cancellation token /// The number of documents deleted. - long DeleteOne(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) + long DeleteOne(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -94,8 +94,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// 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) + long DeleteMany(IEnumerable documents, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -106,8 +107,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// 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 = null) + long DeleteMany(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; } diff --git a/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs b/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs index 902b107..7136261 100644 --- a/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs +++ b/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs @@ -24,7 +24,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete #region Delete TKey /// - public virtual long DeleteOne(TDocument document, CancellationToken cancellationToken = default) + public virtual long DeleteOne(TDocument document, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -33,7 +33,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete } /// - public virtual long DeleteOne(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) + public virtual long DeleteOne(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -41,7 +41,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete } /// - public virtual async Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) + public virtual async Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { @@ -68,7 +68,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete where TDocument : IDocument where TKey : IEquatable { - return (await HandlePartitioned(partitionKey).DeleteManyAsync(filter)).DeletedCount; + return (await HandlePartitioned(partitionKey).DeleteManyAsync(filter, cancellationToken)).DeletedCount; } /// @@ -103,14 +103,8 @@ namespace MongoDbGenericRepository.DataAccess.Delete .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 { @@ -128,29 +122,22 @@ namespace MongoDbGenericRepository.DataAccess.Delete 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; + deleteCount += HandlePartitioned(group.FirstOrDefault()).DeleteMany(x => groupIdsToDelete.Contains(x.Id), cancellationToken).DeletedCount; } return deleteCount; } var idsToDelete = documentList.Select(e => e.Id).ToArray(); - return HandlePartitioned(documentList.FirstOrDefault()).DeleteMany(x => idsToDelete.Contains(x.Id)).DeletedCount; + 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 diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs b/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs index 930870f..9941b6d 100644 --- a/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs +++ b/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs @@ -236,6 +236,42 @@ namespace MongoDbGenericRepository 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. /// @@ -244,8 +280,22 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. - long DeleteMany(Expression> filter, string partitionKey = null) + 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/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs index 839a92b..e2d8f39 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs @@ -164,14 +164,42 @@ namespace MongoDbGenericRepository public virtual long DeleteMany(IEnumerable documents) where TDocument : IDocument { - return MongoDbEraser.DeleteMany(documents); + return DeleteMany(documents, CancellationToken.None); } /// - 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); } } } diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs index c747d91..883d7c9 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs @@ -201,6 +201,35 @@ namespace MongoDbGenericRepository 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. /// @@ -208,7 +237,18 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. - long DeleteMany(Expression> filter, string partitionKey = null) + 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 From 86d9ea6990ac43f7c00cc9a33a28313efa05c5da Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sat, 17 Jun 2023 23:42:10 +0100 Subject: [PATCH 11/40] added unit tests for MongoDBCreator and missing CancellationTokens --- .../MongoDbCreatorTests/AddManyAsyncTests.cs | 161 ++++++++++++++++++ .../MongoDbCreatorTests/AddManyTests.cs | 161 ++++++++++++++++++ .../MongoDbCreatorTests/AddOneAsyncTests.cs | 153 +++++++++++++++++ .../MongoDbCreatorTests/AddOneTests.cs | 152 +++++++++++++++++ .../DataAccess/Create/IMongoDbCreator.cs | 6 +- .../DataAccess/Create/MongoDbCreator.cs | 77 +++------ .../DataAccess/Delete/IMongoDbEraser.cs | 16 +- .../DataAccess/Delete/MongoDbEraser.cs | 2 +- 8 files changed, 665 insertions(+), 63 deletions(-) create mode 100644 CoreUnitTests/DataAccessTests/MongoDbCreatorTests/AddManyAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbCreatorTests/AddManyTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbCreatorTests/AddOneAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbCreatorTests/AddOneTests.cs 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/MongoDbGenericRepository/DataAccess/Create/IMongoDbCreator.cs b/MongoDbGenericRepository/DataAccess/Create/IMongoDbCreator.cs index 00b299e..b90bfea 100644 --- a/MongoDbGenericRepository/DataAccess/Create/IMongoDbCreator.cs +++ b/MongoDbGenericRepository/DataAccess/Create/IMongoDbCreator.cs @@ -31,7 +31,8 @@ namespace MongoDbGenericRepository.DataAccess.Create /// The type representing a Document. /// The type of the primary key for a Document. /// The document you want to add. - void AddOne(TDocument document) + /// An optional cancellation Token. + void AddOne(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -54,7 +55,8 @@ namespace MongoDbGenericRepository.DataAccess.Create /// The type representing a Document. /// The type of the primary key for a Document. /// The documents you want to add. - void AddMany(IEnumerable documents) + /// An optional cancellation Token. + void AddMany(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; } diff --git a/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs b/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs index 30f8693..4be9bf1 100644 --- a/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs +++ b/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs @@ -1,74 +1,54 @@ -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, 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 { var documentsList = documents.ToList(); - + if (!documentsList.Any()) { return; @@ -82,7 +62,7 @@ namespace MongoDbGenericRepository.DataAccess.Create // cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5 if (documentsList.Any(e => e is IPartitionedDocument)) { - foreach (var group in documentsList.GroupBy(e => ((IPartitionedDocument)e).PartitionKey)) + foreach (var group in documentsList.GroupBy(e => ((IPartitionedDocument) e).PartitionKey)) { await HandlePartitioned(group.FirstOrDefault()).InsertManyAsync(group.ToList(), null, cancellationToken); } @@ -93,14 +73,8 @@ namespace MongoDbGenericRepository.DataAccess.Create } } - /// - /// 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 { @@ -119,21 +93,19 @@ namespace MongoDbGenericRepository.DataAccess.Create // cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5 if (documentList.Any(e => e is IPartitionedDocument)) { - foreach (var group in documentList.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(documentList.ToList()); + GetCollection().InsertMany(documentList.ToList(), 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. @@ -146,6 +118,7 @@ namespace MongoDbGenericRepository.DataAccess.Create { throw new ArgumentNullException(nameof(document)); } + var defaultTKey = default(TKey); if (document.Id == null || (defaultTKey != null @@ -155,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 index f56873b..c1fc2f5 100644 --- a/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs +++ b/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs @@ -21,7 +21,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// The document you want to delete. /// An optional cancellation token /// The number of documents deleted. - long DeleteOne(TDocument document, CancellationToken cancellationToken) + long DeleteOne(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -34,7 +34,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// An optional partition key. /// An optional cancellation token /// The number of documents deleted. - long DeleteOne(Expression> filter, string partitionKey, CancellationToken cancellationToken) + long DeleteOne(Expression> filter, string partitionKey = default, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -46,7 +46,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// The document you want to delete. /// The cancellation token /// The number of documents deleted. - Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) + Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -59,7 +59,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// An optional partition key. /// The cancellation token /// The number of documents deleted. - Task DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + Task DeleteOneAsync(Expression> filter, string partitionKey = default, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -72,7 +72,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// An optional partition key. /// The cancellation token /// The number of documents deleted. - Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + Task DeleteManyAsync(Expression> filter, string partitionKey = default, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -84,7 +84,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// The list of documents to delete. /// The cancellation token. /// The number of documents deleted. - Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) + Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -96,7 +96,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// The list of documents to delete. /// The cancellation token /// The number of documents deleted. - long DeleteMany(IEnumerable documents, CancellationToken cancellationToken) + long DeleteMany(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -109,7 +109,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// An optional partition key. /// The Cancellation Token /// The number of documents deleted. - long DeleteMany(Expression> filter, string partitionKey, CancellationToken cancellationToken) + long DeleteMany(Expression> filter, string partitionKey = default, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; } diff --git a/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs b/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs index 7136261..27ff63a 100644 --- a/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs +++ b/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs @@ -24,7 +24,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete #region Delete TKey /// - public virtual long DeleteOne(TDocument document, CancellationToken cancellationToken) + public virtual long DeleteOne(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { From cae2b01dd91dbd01c889cbc9f70e1e65c1ea8fbd Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sat, 17 Jun 2023 23:46:02 +0100 Subject: [PATCH 12/40] removing ignored file --- .vs/MongoDbGenericRepository/v14/.suo | Bin 28672 -> 0 bytes .vs/config/applicationhost.config | 1038 ------------------------- 2 files changed, 1038 deletions(-) delete mode 100644 .vs/MongoDbGenericRepository/v14/.suo delete mode 100644 .vs/config/applicationhost.config diff --git a/.vs/MongoDbGenericRepository/v14/.suo b/.vs/MongoDbGenericRepository/v14/.suo deleted file mode 100644 index 424bfa89768302a8ee99083aa1a6683bf9021f4f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 28672 zcmeHQTWlN067IdPgt^RAn=Xi>BuMH8TCn*jMtkdHPW1qu{!(#KDM0_jK4qUa+9f~G)`xaaJ0 z$R)+clA=Xf(Flh#yF0sgcIM8^oqO-B-oL#5!ymnM`=6zPuv==DKEJYBy54ZU$&i-T zNzw)!^W5iGu3X_I3*ZEBxE!vL8aON^@RyRTQb39$PD(RuO4DJZbX~T+S5>&n`b6@h zp)(&?HX+}h7CrL-C?=%S(mqg+A{CIPD?BCt@LLWl#UGJ9+kzfoZ0hWVGKm1L{Wf#`kdy`$MT+75i7^9ZKh7k}JcpM+ z-@<+;%1|M@cKIlNn=*}c3(}v7o6E%P;|w<-KhtXw-wJF5z6`7f*akNNy#6J`Hv_i; zw*z+oY^SdPn}IEW6}Su73OoRiCGG~c0rvpgfqQ}bfct?~zy{a>(r_Sd2avqtLd^GY zBlZAmGUs`1Ih3w}diMX-sPl5+WXtn!u>bEwri=CK|5@JWTb83Qu6+{;@+w0;`ak*i zI>0P_Kk{D6Or~|{|5d;LDbN@FUvLJ?&;G-H)rs^DfbGd+4i$`fo&6utq6#a@l!@|- z7w7_30y3~0co5hFbOSwr4`90lfL>rP&3Q2Y`OyAaDp603HSo13_RA7y^cY zBLK(QM}S9xW55V-9C!>k0h|Ovz$g#~A^__-ig*lQJH>$0z&M})6F?kD0FMJ_0L~FA zkOI=c6mS;cOv>5(9B>|(1ug)j@g!n4BIB2Jj(L{DwOIo_;G_h&YXY2@L~K^>LDOsDPRKu;*EycD2K2M#f63e1Gg$^fR|VYz z;B*xn90b=>21}twP%5y+-yG}PG7k-EZ%a;#Ar;ToGfSgjl%LFA$#6K|D5>>KQ92`KJ?%jKlb1ywn44qn8Dsm$L2nFg&AFqNum%Pv_afDXKR=F7Wc$M0(TV&o-rX$yT_8Qp2^_&3 z{XyjGNBKhFhkQQpfc_nL&KAHd{U+q4EWvODd`F&7V8-sp^EnEzJ(5`E3~1NlIEsLN z5?3aWoHc@se7iwAmzJv#^VPeeh2!IJH&`9QqqM{@)Dx4u@XMMvebM{%iF3&o(!% z|AZV{$QR4UOS1+lcGe0RIE)eL@Q#tRDks&EkRnfyL=xfjgd9()BU3Ro9a5}nDjkU> ztnH3A_sBppaas=JT7M!wmhg}6mE&?U7CtObCe&Cek(?=gVKNjR4~@yHJ)DR~V_Zl| z*YAv{6@@yTTEgEfVd%O>G^tx(@r+Ve{MpJfm<4&Ph)a7~=XZp4E#Tb75SZVIno=A$Ec@N`g5?46qrV!H#Q_CHbdGm-0 z88WOoVR}J_kGybMU|iQB*6a9HNn$}ssCY`C>Dz8e~RT>K6h=@ zKpob9c`^jpr(z)EHFEyv+$-kaM$P|=W&O7l~VD5T$mGRP>yxF!!#H&#*qV6oyk^fNH~{tqJWPXLBO`qX(*?_Wvp ztKUsug(k~-9MA{CEPcwVLjO@npZb651Id@S6!83C|OHlgw+Cjp!pFUaW7*|?Tk4laa^W-k~R|BiCNCg0PQ7K;4WY*K$(Ct z+}*%7;2wZHaxZWna6ixr*Z@2506-NU`*Azq1YCd{@BkeEZ5N%u4xkIz3DEW<%DM;X zZo{!S--~m5fxgVOhcYo`_I}_Xa0nOx9tI8rL0}LN&k*q=IQJ+(n{O024m<{&08RqT zH;On6L;yK+ZVd4$AO@TU#sLMG0OCLbm;}xMNq}>83P=M}z*%4#m;ufK=Yd(^0`NHS z1aJ|kr2l3emJ8HBv;DZHV;?M+S6q7uiDmnbsEzr@talNreVPwkMGp)W5@8J7cXo~x zTK|LoYyGL+A)UnS3rd_&-7dfn3APdjI!-b~~x6~q7Fs?z89TrN=l;@HJ8@@l+4 z<-;kAava-=1*|{j_5P*h&l!w0^oZf8Q##b*e_?F@Li+u9-w9Bq2GqC~EwvV@<^8!{ z<5~C&S_@be*^MzZe=K>uT!E{S@%TO^)tHeSm=iJJM~}q zA5`7`EU%D1jL)hiy#_Z~YPm6UPfvgEAY}qOX4L>%CFkdGR;z(=%=*LN3Hl=GT8Lff z4=&_)1MM1Z(huM{+9TIc8XP#7-Yci^ zBjgNS$H#Bh-fB1)m`=%YzL8x!5tLJ@SbR)nmMuGv#Nv^}S#fUn_=yueu$ka`Z%kGq z&6k>jGipknu=^5<0wWBbqS`YC7`s2Fod_joPPQLAmdO}QC2@DFbwUj%l1gkeSD!A_ zCjJbyS?X;%IzsNS+tb$Rbjc2H=keBKdIJsU=A8ae97Y}5i|j-D>_adkiSoBvOJ~d# zq*%k2D2lUfH0t!aJ)wxxN&m6e$;w91#5m<%Q5=Ih2gV;vprQ2RBNq#Xz%yi4r-&hN}@zDtZUhB-)?xju~hWGgYSxQPh{v^hp;3PQ0Z{Ax^mV(sUTk`;l=@})3&Y3aBn=T2 z!p*o&Fz~il-}U**-*5D>vru-wL3;l$gCFet{?B}`T>3|~_f!AQwF+okBuBMAel^@; zRJhQP#Fy@KKqfc-*|JCFQ^tFk)`ISQNF^LUN05L}GO9TTa3hNDu1FJo#$7QewcPkw z_x&&bvhS@w{`B%KuOC@^Kzje3e;<8r>)U~!@AyObd!J5Iy@L>xywMHseg59&1F!us z^5%Cp|Esf9PGqL4$LLNu3CjFTM9!TuQ$DVH`Z0nk+6cVzf8iPeG)XPrp*AmzpNPVH&{xjLb3>Ol@(Cwg(^0#-v2 z=*;u!>i+vWRla$bW-ZIlldtDy-c_32tDa@+(yUwce4<_RRHb(;Z3Z$dO5I?R88np- zwLOH|h@y@-QqqH@2dxsp`?L1-dUy~~ean;6pH^EIYV5mFPR_hjco!eaPOBSR;wHqU zpWJ2O4P-3I8)oYN=w+6Jc1?5KMo(d5{kNc&^LOy7XpLd;COwh4R&Udu&z~-B*L!~b z(+`XK;FZ#>e}@Bq=N*|~V_fhfu3ebj8B8c?Y7ckMmh$4Zt4e%!*4*=6Zpu*ix4B(` z4!^_J+3ob%T+Z$un>Wzuum${`?L9u1)8*`STqsGhq~o|ftWLt~e6|WNb>`Q+)Fh{_k0av%p)$8-vyk1YY z&Eavidz}uq%hTby@Z6HBj!Zq}YOac!cz#LM#8gZ^JCTUUiaL^%RoEb~k-;=mEUl`D zmEekyCyf3PLjRCmA)6~2*7`>%zkhsUSxUo&*+KG|CDj))?T?K-DS0|oML&SVt9|l{ zQ(A0KL#M5klE0z!*J;)gE2G1&|0$lqn0sp&bC3BZV6pB06lJ9)dJiCuMI6An zUSKc4{h#}Qhk*US0e~LY2La~i{!hln{hxHn7zVh<`6wXve~Rau-Tx`_$MB5Pz&M}) z6F?lO*Z$9Xe03^+jLv<pVL?fA(e;{N%+{XWbH!cmUzTsWSJ@sIQC znk6;<%}-X+rHub9DPG8N0>AKBjvFXtNlS_wg5dom{OI)mnm~EBPa6v+we$H`MW3LI sNzdhCHD&hemtrZtSiV0h_U;*7RrC2G|I43+-%qaf%b)o*Y`*^g4~&4CQvd(} 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 @@ - - - - - - - - -
-
-
-
-
-
-
-
- - - -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
- -
-
-
-
-
-
- -
-
-
-
-
- -
-
-
- -
-
- -
-
- -
-
-
- - -
-
-
-
-
-
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From 39e93dc968eea4605258fdb35b2747fc19752406 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sun, 18 Jun 2023 18:08:50 +0100 Subject: [PATCH 13/40] added cancellation tokens to 'Create' methods --- .../BaseMongoRepository.Create.cs | 171 ++++++++++-------- .../IBaseMongoRepository.Create.cs | 52 +++++- .../BaseMongoRepository.TKey.Create.cs | 93 +++++----- .../IBaseMongoRepository.TKey.Create.cs | 74 ++++++-- 4 files changed, 253 insertions(+), 137 deletions(-) diff --git a/MongoDbGenericRepository/BaseMongoRepository.Create.cs b/MongoDbGenericRepository/BaseMongoRepository.Create.cs index 53e37fa..5b2446f 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Create.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Create.cs @@ -1,15 +1,15 @@ -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 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 { @@ -17,13 +17,16 @@ namespace MongoDbGenericRepository private IMongoDbCreator _mongoDbCreator; /// - /// The MongoDbCreator field. + /// The MongoDbCreator field. /// protected virtual IMongoDbCreator MongoDbCreator { get { - if (_mongoDbCreator != null) { return _mongoDbCreator; } + if (_mongoDbCreator != null) + { + return _mongoDbCreator; + } lock (_initLock) { @@ -35,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/IBaseMongoRepository.Create.cs b/MongoDbGenericRepository/IBaseMongoRepository.Create.cs index 8dc50e3..1b7243d 100644 --- a/MongoDbGenericRepository/IBaseMongoRepository.Create.cs +++ b/MongoDbGenericRepository/IBaseMongoRepository.Create.cs @@ -7,11 +7,22 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The IBaseMongoRepository_Create interface to expose document creation functionality + /// 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. @@ -20,7 +31,7 @@ namespace MongoDbGenericRepository /// 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) + Task AddOneAsync(TDocument document, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -35,6 +46,29 @@ namespace MongoDbGenericRepository 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. @@ -43,7 +77,7 @@ namespace MongoDbGenericRepository /// 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) + Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -57,5 +91,17 @@ namespace MongoDbGenericRepository 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/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs index abd4833..c51dc35 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs @@ -1,23 +1,23 @@ -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 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 + public abstract partial class BaseMongoRepository : IBaseMongoRepository_Create where TKey : IEquatable { private IMongoDbCreator _mongoDbCreator; /// - /// The MongoDb accessor to insert data. + /// The MongoDb accessor to insert data. /// protected virtual IMongoDbCreator MongoDbCreator { @@ -33,56 +33,67 @@ 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 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/IBaseMongoRepository.TKey.Create.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Create.cs index 8994fde..bce8e62 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Create.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Create.cs @@ -7,43 +7,87 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The interface exposing data insertion functionality for Key typed repositories. + /// The interface exposing data insertion functionality for Key typed repositories. /// /// - public interface IBaseMongoRepository_Create where TKey : IEquatable + public interface IBaseMongoRepository_Create + where TKey : IEquatable { /// - /// Asynchronously adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. + /// 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; + Task AddOneAsync(TDocument document) + where TDocument : IDocument; /// - /// Adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. + /// 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. - void AddOne(TDocument document) where TDocument : IDocument; + /// The cancellation Token. + Task AddOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument; /// - /// Asynchronously adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. + /// 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 = default) where TDocument : IDocument; + 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. + /// 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; + 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 From ed2eddd33e51284ff34c45a82941a7cc54956734 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sun, 18 Jun 2023 18:37:30 +0100 Subject: [PATCH 14/40] unit tests for add --- .../AddTests/AddManyAsyncTests.cs | 77 +++++++++++++++++++ .../AddTests/AddManyTests.cs | 64 ++++++++++++--- .../AddTests/AddOneAsyncTests.cs | 76 ++++++++++++++++++ .../AddTests/AddOneTests.cs | 59 ++++++++++++-- .../AddTests/AddManyAsyncTests.cs | 43 +++++++++++ .../AddTests/AddManyTests.cs | 43 +++++++++++ .../AddTests/AddOneAsyncTests.cs | 42 ++++++++++ .../AddTests/AddOneTests.cs | 41 ++++++++++ .../BaseMongoRepository.Delete.cs | 33 +++++--- 9 files changed, 451 insertions(+), 27 deletions(-) create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyAsyncTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneAsyncTests.cs create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/AddTests/AddManyAsyncTests.cs create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/AddTests/AddManyTests.cs create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/AddTests/AddOneAsyncTests.cs create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/AddTests/AddOneTests.cs 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 index 3fd2788..1fb839a 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs @@ -1,7 +1,7 @@ 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 MongoDbGenericRepository.DataAccess.Create; @@ -14,20 +14,64 @@ public class AddManyTests : TestMongoRepositoryContext { [Fact] - public async Task AddManyAsync_EnsureTokenPassed() + public void WithDocument_ShouldAddOne() { // Arrange + var documents = Fixture.CreateMany().ToList(); Creator = new Mock(); - var token = new CancellationToken(true); - var documents = new List - { - new(), new(), new() - }; // Act - await Sut.AddManyAsync(documents, token); + Sut.AddMany(documents); // Assert - Creator.Verify(x => x.AddManyAsync(documents, token)); + 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 index 6396c7f..f3b419c 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs @@ -1,6 +1,6 @@ using System; using System.Threading; -using System.Threading.Tasks; +using AutoFixture; using CoreUnitTests.Infrastructure; using CoreUnitTests.Infrastructure.Model; using MongoDbGenericRepository.DataAccess.Create; @@ -12,17 +12,64 @@ namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests; public class AddOneTests : TestMongoRepositoryContext { [Fact] - public async Task AddOneAsync_EnsureTokenPassed() + public void WithDocument_ShouldAddOne() { // Arrange + var document = Fixture.Create(); Creator = new Mock(); - var token = new CancellationToken(true); - var document = new TestDocument(); // Act - await Sut.AddOneAsync(document, token); + Sut.AddOne(document); // Assert - Creator.Verify(x => x.AddOneAsync(document, token)); + 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/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/MongoDbGenericRepository/BaseMongoRepository.Delete.cs b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs index ebb8fd8..692c10f 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs @@ -1,10 +1,10 @@ -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 { @@ -13,13 +13,16 @@ namespace MongoDbGenericRepository private IMongoDbEraser _mongoDbEraser; /// - /// The MongoDbEraser used to delete documents. + /// The MongoDbEraser used to delete documents. /// protected virtual IMongoDbEraser MongoDbEraser { get { - if (_mongoDbEraser != null) { return _mongoDbEraser; } + if (_mongoDbEraser != null) + { + return _mongoDbEraser; + } lock (_initLock) { @@ -31,7 +34,7 @@ namespace MongoDbGenericRepository return _mongoDbEraser; } - set { _mongoDbEraser = value; } + set => _mongoDbEraser = value; } #region Delete @@ -114,7 +117,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + public virtual async Task DeleteOneAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await DeleteOneAsync(filter, partitionKey, cancellationToken); @@ -297,7 +303,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + public virtual async Task DeleteOneAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -329,7 +338,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + public virtual async Task DeleteManyAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -401,6 +413,5 @@ namespace MongoDbGenericRepository } #endregion - } -} +} \ No newline at end of file From 25766febd487fc15d91ecc15f9e14c93bed40c03 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Tue, 20 Jun 2023 22:06:30 +0100 Subject: [PATCH 15/40] Index tests --- .../IndexTests/CreateTextIndexTests.cs | 24 +-- .../IndexTests/GetIndexNamesTests.cs | 6 +- .../BaseIndexTests.cs | 45 +++++ .../CreateTextIndexAsyncTests.cs | 180 ++++++++++++++++++ .../GetIndexNamesAsyncTests.cs | 129 +++++++++++++ .../Infrastructure/IndexExtensions.cs | 57 ++++++ .../DataAccess/Index/IMongoDbIndexHandler.cs | 78 +++++--- .../DataAccess/Index/MongoDbIndexHandler.cs | 180 +++++++----------- 8 files changed, 553 insertions(+), 146 deletions(-) create mode 100644 CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/BaseIndexTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateTextIndexAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/GetIndexNamesAsyncTests.cs create mode 100644 CoreUnitTests/Infrastructure/IndexExtensions.cs diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs index e7dd3be..c3584ec 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs @@ -12,20 +12,20 @@ namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; public class CreateTextIndexTests : BaseIndexTests { - private readonly Expression> _fieldExpression = t => t.SomeContent2; + private readonly Expression> fieldExpression = t => t.SomeContent2; [Fact] - public async Task Ensure_Creates_Index() + public async Task WhenFieldExpression_CreatesIndex() { // Arrange IndexHandler = new Mock(); // Act - await Sut.CreateTextIndexAsync(_fieldExpression); + await Sut.CreateTextIndexAsync(fieldExpression); // Assert IndexHandler.Verify( - x => x.CreateTextIndexAsync(_fieldExpression, null, null)); + x => x.CreateTextIndexAsync(fieldExpression, null, null, CancellationToken.None)); } [Fact] @@ -36,12 +36,12 @@ public class CreateTextIndexTests : BaseIndexTests var options = new IndexCreationOptions { Name = "theIndexName" }; // Act - await Sut.CreateTextIndexAsync(_fieldExpression, options); + await Sut.CreateTextIndexAsync(fieldExpression, options); // Assert IndexHandler.Verify( x => x.CreateTextIndexAsync( - _fieldExpression, options, null)); + fieldExpression, options, null, CancellationToken.None)); } [Fact] @@ -53,16 +53,15 @@ public class CreateTextIndexTests : BaseIndexTests IndexHandler = new Mock(); // Act - await Sut.CreateTextIndexAsync(_fieldExpression, partitionKey: partitionKey); + await Sut.CreateTextIndexAsync(fieldExpression, partitionKey: partitionKey); // Assert IndexHandler.Verify( x => x.CreateTextIndexAsync( - _fieldExpression, null, partitionKey)); + fieldExpression, null, partitionKey, CancellationToken.None)); } - /* - [Fact] + /*[Fact] public async Task Ensure_Creates_Index_With_CancellationToken() { // Arrange @@ -70,13 +69,14 @@ public class CreateTextIndexTests : BaseIndexTests var token = new CancellationToken(true); // Act - await Sut.CreateTextIndexAsync(_fieldExpression, token); + await Sut.CreateTextIndexAsync(fieldExpression, token); // Assert IndexHandler.Verify( x => x.CreateTextIndexAsync(_fieldExpression, null, null, token)); - } + }*/ + /* [Fact] public async Task Ensure_Passes_Options_With_CancellationToken() { diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs index a23350a..c73e7ae 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs @@ -19,7 +19,7 @@ public class GetIndexNamesTests : BaseIndexTests const string indexName = "theIndexName"; IndexHandler - .Setup(x => x.GetIndexesNamesAsync(null)) + .Setup(x => x.GetIndexesNamesAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { indexName }); // Act @@ -28,7 +28,7 @@ public class GetIndexNamesTests : BaseIndexTests // Assert Assert.NotNull(result); Assert.Contains(result, x => x == indexName); - IndexHandler.Verify(x => x.GetIndexesNamesAsync(null), Times.Once()); + IndexHandler.Verify(x => x.GetIndexesNamesAsync(null, CancellationToken.None), Times.Once()); } /* @@ -61,7 +61,7 @@ public class GetIndexNamesTests : BaseIndexTests IndexHandler = new Mock(); IndexHandler - .Setup(x => x.GetIndexesNamesAsync(partitionKey)) + .Setup(x => x.GetIndexesNamesAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { indexName }); // Act diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/BaseIndexTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/BaseIndexTests.cs new file mode 100644 index 0000000..566aa75 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/BaseIndexTests.cs @@ -0,0 +1,45 @@ +using System.Collections.Generic; +using System.Threading; +using CoreUnitTests.Infrastructure; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +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/CreateTextIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateTextIndexAsyncTests.cs new file mode 100644 index 0000000..f54b2ff --- /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 WhenFieldExpression_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndOptions_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndPartitionKey_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndCancellationToken_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex() + { + // 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/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/Infrastructure/IndexExtensions.cs b/CoreUnitTests/Infrastructure/IndexExtensions.cs new file mode 100644 index 0000000..7daa789 --- /dev/null +++ b/CoreUnitTests/Infrastructure/IndexExtensions.cs @@ -0,0 +1,57 @@ +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Driver; +using MongoDbGenericRepository.Models; + +namespace CoreUnitTests.Infrastructure; + +public static class IndexExtensions +{ + public static bool EqualToJson(this IndexKeysDefinition keys, string json) + { + var indexModelRendered = RenderIndexModelKeys(keys); + return indexModelRendered.Equals(json, System.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/MongoDbGenericRepository/DataAccess/Index/IMongoDbIndexHandler.cs b/MongoDbGenericRepository/DataAccess/Index/IMongoDbIndexHandler.cs index 247ad6a..9c79ab5 100644 --- a/MongoDbGenericRepository/DataAccess/Index/IMongoDbIndexHandler.cs +++ b/MongoDbGenericRepository/DataAccess/Index/IMongoDbIndexHandler.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using MongoDbGenericRepository.DataAccess.Base; using MongoDbGenericRepository.Models; @@ -8,104 +9,131 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository.DataAccess.Index { /// - /// The MongoDbIndexHandler interface. used to create indexes on collections. + /// The MongoDbIndexHandler interface. used to create indexes on collections. /// public interface IMongoDbIndexHandler : IDataAccessBase { /// - /// Returns the names of the indexes present on a collection. + /// 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) + 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. + /// 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) + 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. + /// 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) + 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. + /// 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) + 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. + /// 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) + 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. + /// 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) + 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 + /// 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) + /// An optional cancellation token, + Task DropIndexAsync(string indexName, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; } diff --git a/MongoDbGenericRepository/DataAccess/Index/MongoDbIndexHandler.cs b/MongoDbGenericRepository/DataAccess/Index/MongoDbIndexHandler.cs index a4bf5e8..0bfe00b 100644 --- a/MongoDbGenericRepository/DataAccess/Index/MongoDbIndexHandler.cs +++ b/MongoDbGenericRepository/DataAccess/Index/MongoDbIndexHandler.cs @@ -1,11 +1,12 @@ -using MongoDB.Driver; -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 { @@ -13,64 +14,67 @@ namespace MongoDbGenericRepository.DataAccess.Index public class MongoDbIndexHandler : DataAccessBase, IMongoDbIndexHandler { /// - /// The MongoDbIndexHandler constructor. + /// 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 virtual async 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 virtual async 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 virtual async 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 { @@ -78,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 virtual async 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 { @@ -101,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 virtual async 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 virtual async 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 { @@ -150,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 virtual async 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 From b7b23e7b920a7f2c7461fb6d991dfdfbae38dc06 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Thu, 22 Jun 2023 22:06:27 +0100 Subject: [PATCH 16/40] added tests for index handler --- .../CreateAscendingIndexAsyncTests.cs | 180 +++++++++++++++++ .../CreateCombinedTextIndexAsyncTests.cs | 190 ++++++++++++++++++ .../CreateDescendingIndexAsyncTests.cs | 180 +++++++++++++++++ .../CreateHashedIndexAsyncTests.cs | 185 +++++++++++++++++ .../DropIndexAsyncTests.cs | 119 +++++++++++ .../Infrastructure/IndexExtensions.cs | 22 +- 6 files changed, 871 insertions(+), 5 deletions(-) create mode 100644 CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateAscendingIndexAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateCombinedTextIndexAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateDescendingIndexAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateHashedIndexAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/DropIndexAsyncTests.cs diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateAscendingIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateAscendingIndexAsyncTests.cs new file mode 100644 index 0000000..e3040e8 --- /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 WhenFieldExpression_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndOptions_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndPartitionKey_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndCancellationToken_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex() + { + // 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..5f1ed30 --- /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 WhenFieldExpression_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndOptions_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndPartitionKey_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndCancellationToken_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex() + { + // 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..d0bb876 --- /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 WhenFieldExpression_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndOptions_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndPartitionKey_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndCancellationToken_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex() + { + // 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..f1172c7 --- /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 WhenFieldExpression_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndOptions_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndPartitionKey_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndCancellationToken_ThenCreatesIndex() + { + // 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 WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex() + { + // 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/DropIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/DropIndexAsyncTests.cs new file mode 100644 index 0000000..6485411 --- /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 WhenIndexName_ThenDropsIndex() + { + // 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 WhenIndexNameAndPartitionKey_ThenDropsIndex() + { + // 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 WhenIndexNameAndCancellationToken_ThenDropsIndex() + { + // 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 WhenIndexNameAndPartitionKeyAndCancellationToken_ThenDropsIndex() + { + // 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/Infrastructure/IndexExtensions.cs b/CoreUnitTests/Infrastructure/IndexExtensions.cs index 7daa789..7ff4fff 100644 --- a/CoreUnitTests/Infrastructure/IndexExtensions.cs +++ b/CoreUnitTests/Infrastructure/IndexExtensions.cs @@ -1,16 +1,30 @@ -using MongoDB.Bson; +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, System.StringComparison.Ordinal); + return indexModelRendered.Equals(json, StringComparison.Ordinal); } public static bool EqualTo(this IndexCreationOptions x, CreateIndexOptions y) => @@ -28,7 +42,7 @@ public static class IndexExtensions x.Background == y.Background && x.Version == y.Version; - public static bool EqualTo(this CreateIndexOptions x, IndexCreationOptions y) => + public static bool EqualTo(this CreateIndexOptions x, IndexCreationOptions y) => x.Unique == y.Unique && x.TextIndexVersion == y.TextIndexVersion && x.SphereIndexVersion == y.SphereIndexVersion && @@ -52,6 +66,4 @@ public static class IndexExtensions var result = indexModelRendered.ToString(); return result.Replace(" ", ""); } - - } From aaf80a7ae29f73faec08cfd2fe49df7ff6544efb Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sat, 24 Jun 2023 12:41:17 +0100 Subject: [PATCH 17/40] Cancellation Tokens for Text Index and GetIndex --- .../IndexTests/GetIndexNamesAsyncTests.cs | 8 + .../BaseMongoRepository.Index.cs | 291 +++++++++++------- .../IBaseMongoRepository.Index.cs | 151 ++++++++- .../BaseMongoRepository.TKey.Index.cs | 146 +++++---- .../IBaseMongoRepository.TKey.Index.cs | 129 +++++++- 5 files changed, 535 insertions(+), 190 deletions(-) create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs diff --git a/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs new file mode 100644 index 0000000..087cfc6 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs @@ -0,0 +1,8 @@ +using CoreUnitTests.Infrastructure; + +namespace CoreUnitTests.KeyTypedRepositoryTests.IndexTests; + +public class GetIndexNamesAsyncTests : TestKeyedMongoRepositoryContext +{ + +} diff --git a/MongoDbGenericRepository/BaseMongoRepository.Index.cs b/MongoDbGenericRepository/BaseMongoRepository.Index.cs index 90741a6..c32dde0 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Index.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Linq.Expressions; +using System.Threading; using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository @@ -36,93 +37,195 @@ namespace MongoDbGenericRepository 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 Task> GetIndexesNamesAsync(string partitionKey = null) + /// + public async Task> GetIndexesNamesAsync() where TDocument : IDocument { - return await MongoDbIndexHandler.GetIndexesNamesAsync(partitionKey); + return await GetIndexesNamesAsync(null, CancellationToken.None); } - /// - /// Returns the names of the indexes present on a collection. - /// - /// The type representing a Document. - /// The type for the key of the document. - /// An optional partition key - /// A list containing the names of the indexes on on the concerned collection. - public virtual async Task> GetIndexesNamesAsync(string partitionKey = null) + + /// + public async Task> GetIndexesNamesAsync(CancellationToken cancellationToken) + where TDocument : IDocument + { + return await GetIndexesNamesAsync(null, cancellationToken); + } + + /// + public async Task> GetIndexesNamesAsync(string partitionKey) + where TDocument : IDocument + { + return await GetIndexesNamesAsync(partitionKey); + } + + /// + public async Task> GetIndexesNamesAsync(string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await GetIndexesNamesAsync(partitionKey, cancellationToken); + } + + /// + public virtual async Task> GetIndexesNamesAsync() where TDocument : IDocument where TKey : IEquatable { - return await MongoDbIndexHandler.GetIndexesNamesAsync(partitionKey); + return await GetIndexesNamesAsync(null, CancellationToken.None); } - /// - /// 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 virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + /// + public virtual async Task> GetIndexesNamesAsync(CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbIndexHandler.CreateTextIndexAsync(field, indexCreationOptions, partitionKey); + return await GetIndexesNamesAsync(null, 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 Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + /// + 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 MongoDbIndexHandler.CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey); + return await CreateTextIndexAsync(field, null, null, CancellationToken.None); } - /// - /// 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 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, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey); + } + + /// public virtual async Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable @@ -130,20 +233,11 @@ namespace MongoDbGenericRepository 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); + return await CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey); } /// @@ -154,20 +248,11 @@ namespace MongoDbGenericRepository 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) where TDocument : IDocument { - return await MongoDbIndexHandler.CreateHashedIndexAsync(field, indexCreationOptions, partitionKey); + return await CreateHashedIndexAsync(field, indexCreationOptions, partitionKey); } /// @@ -178,20 +263,11 @@ namespace MongoDbGenericRepository 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 CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument { - return await MongoDbIndexHandler.CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey); + return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey); } /// @@ -202,16 +278,11 @@ namespace MongoDbGenericRepository 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) where TDocument : IDocument { - await MongoDbIndexHandler.DropIndexAsync(indexName, partitionKey); + await DropIndexAsync(indexName, partitionKey); } /// diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Index.cs b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs index 3db3ecf..b3de813 100644 --- a/MongoDbGenericRepository/IBaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs @@ -1,16 +1,38 @@ 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. + /// 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. /// @@ -18,13 +40,110 @@ namespace MongoDbGenericRepository /// 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) + 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 + /// 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. @@ -33,13 +152,29 @@ namespace MongoDbGenericRepository /// 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) + 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 + /// IndexCreationOptions can be supplied to further specify /// how the creation should be done. /// /// The type representing a Document. @@ -54,7 +189,7 @@ namespace MongoDbGenericRepository /// /// Creates an index on the given field in descending order. - /// IndexCreationOptions can be supplied to further specify + /// IndexCreationOptions can be supplied to further specify /// how the creation should be done. /// /// The type representing a Document. @@ -69,7 +204,7 @@ namespace MongoDbGenericRepository /// /// Creates a hashed index on the given field. - /// IndexCreationOptions can be supplied to further specify + /// IndexCreationOptions can be supplied to further specify /// how the creation should be done. /// /// The type representing a Document. @@ -84,7 +219,7 @@ namespace MongoDbGenericRepository /// /// Creates a combined text index. - /// IndexCreationOptions can be supplied to further specify + /// IndexCreationOptions can be supplied to further specify /// how the creation should be done. /// /// The type representing a Document. diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs index b7e56ce..e24022c 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; +using CancellationToken = System.Threading.CancellationToken; namespace MongoDbGenericRepository { @@ -38,104 +39,119 @@ namespace MongoDbGenericRepository 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 virtual async 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 virtual async 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 virtual async Task CreateTextIndexAsync(Expression> field) + where TDocument : IDocument + { + return await CreateTextIndexAsync(field, null, null, CancellationToken.None); + } + + /// + public virtual async Task CreateTextIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateTextIndexAsync(field, null, null, cancellationToken); + } + + /// + public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + { + return await CreateTextIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + /// + public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateTextIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + where TDocument : IDocument + { + 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, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument { 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 virtual async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument { 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 virtual async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument { 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 virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument { 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 virtual async Task DropIndexAsync(string indexName, string partitionKey = null) where TDocument : IDocument { diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs index 951904b..cb97145 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using MongoDbGenericRepository.Models; @@ -12,18 +13,118 @@ namespace MongoDbGenericRepository /// 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 = null) + 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 + /// 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. @@ -31,12 +132,26 @@ namespace MongoDbGenericRepository /// 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) + 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 + /// IndexCreationOptions can be supplied to further specify /// how the creation should be done. /// /// The type representing a Document. @@ -49,7 +164,7 @@ namespace MongoDbGenericRepository /// /// Creates an index on the given field in descending order. - /// IndexCreationOptions can be supplied to further specify + /// IndexCreationOptions can be supplied to further specify /// how the creation should be done. /// /// The type representing a Document. @@ -62,7 +177,7 @@ namespace MongoDbGenericRepository /// /// Creates a hashed index on the given field. - /// IndexCreationOptions can be supplied to further specify + /// IndexCreationOptions can be supplied to further specify /// how the creation should be done. /// /// The type representing a Document. @@ -75,7 +190,7 @@ namespace MongoDbGenericRepository /// /// Creates a combined text index. - /// IndexCreationOptions can be supplied to further specify + /// IndexCreationOptions can be supplied to further specify /// how the creation should be done. /// /// The type representing a Document. From 251ffdacfa2647f4246f48ea6ee9b29d7bfe88b6 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sat, 24 Jun 2023 14:48:35 +0100 Subject: [PATCH 18/40] unit tests for Create and Get Indexes --- .../IndexTests/CreateTextIndexTests.cs | 263 ++++++++++++------ .../IndexTests/GetIndexNamesTests.cs | 154 +++++----- .../IndexTests/CreateTextIndexAsyncTests.cs | 154 ++++++++++ .../IndexTests/GetIndexNamesAsyncTests.cs | 90 ++++++ 4 files changed, 506 insertions(+), 155 deletions(-) create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateTextIndexAsyncTests.cs diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs index c3584ec..485fa54 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs @@ -1,21 +1,23 @@ 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; +using CancellationToken = System.Threading.CancellationToken; namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; public class CreateTextIndexTests : BaseIndexTests { private readonly Expression> fieldExpression = t => t.SomeContent2; + private readonly Expression, object>> keyedFieldExpression = t => t.SomeContent2; [Fact] - public async Task WhenFieldExpression_CreatesIndex() + public async Task WithFieldExpression_CreatesIndex() { // Arrange IndexHandler = new Mock(); @@ -29,11 +31,27 @@ public class CreateTextIndexTests : BaseIndexTests } [Fact] - public async Task Ensure_Passes_Options() + 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(); - var options = new IndexCreationOptions { Name = "theIndexName" }; // Act await Sut.CreateTextIndexAsync(fieldExpression, options); @@ -45,15 +63,32 @@ public class CreateTextIndexTests : BaseIndexTests } [Fact] - public async Task Ensure_Passes_PartitionKey() + public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() { // Arrange - const string partitionKey = "thePartitionKey"; - + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); IndexHandler = new Mock(); // Act - await Sut.CreateTextIndexAsync(fieldExpression, partitionKey: partitionKey); + 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( @@ -61,135 +96,199 @@ public class CreateTextIndexTests : BaseIndexTests fieldExpression, null, partitionKey, CancellationToken.None)); } - /*[Fact] - public async Task Ensure_Creates_Index_With_CancellationToken() - { - // Arrange - IndexHandler = new Mock(); - var token = new CancellationToken(true); - - // Act - await Sut.CreateTextIndexAsync(fieldExpression, token); - - // Assert - IndexHandler.Verify( - x => x.CreateTextIndexAsync(_fieldExpression, null, null, token)); - }*/ - - /* [Fact] - public async Task Ensure_Passes_Options_With_CancellationToken() + public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() { // Arrange - IndexHandler = new Mock(); + var partitionKey = Fixture.Create(); var token = new CancellationToken(true); - var options = new IndexCreationOptions { Name = "theIndexName" }; + IndexHandler = new Mock(); // Act - await Sut.CreateTextIndexAsync(_fieldExpression, token, options); + await Sut.CreateTextIndexAsync(fieldExpression, partitionKey, token); // Assert IndexHandler.Verify( x => x.CreateTextIndexAsync( - _fieldExpression, options, null, token)); + fieldExpression, null, partitionKey, token)); } [Fact] - public async Task Ensure_Passes_PartitionKey_With_CancellationToken() + public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() { // Arrange - const string partitionKey = "thePartitionKey"; - var token = new CancellationToken(true); + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); // Act - await Sut.CreateTextIndexAsync(_fieldExpression, token, partitionKey: partitionKey); + await Sut.CreateTextIndexAsync(fieldExpression, options, partitionKey); // Assert IndexHandler.Verify( x => x.CreateTextIndexAsync( - _fieldExpression, null, partitionKey, token)); + fieldExpression, options, partitionKey, CancellationToken.None)); } [Fact] - public async Task Ensure_Creates_Index_Custom_Key() + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() { // Arrange - IndexHandler = new Mock(); - Expression> fieldExpression = t => t.SomeContent2; - - // Act - await Sut.CreateTextIndexAsync(fieldExpression); - - // Assert - IndexHandler.Verify(x => x.CreateTextIndexAsync(fieldExpression, null, null, default)); - } - - [Fact] - public async Task Ensure_Passes_CancellationToken_Custom_Key() - { - // Arrange - IndexHandler = new Mock(); + var 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(t => t.SomeContent2, token); + await Sut.CreateTextIndexAsync(fieldExpression, options, partitionKey, token); // Assert IndexHandler.Verify( - x => x.CreateTextIndexAsync( - t => t.SomeContent2, null, null, token)); + x => x.CreateTextIndexAsync( + fieldExpression, options, partitionKey, token)); } + #region Keyed + [Fact] - public async Task Ensure_Passes_Options_Custom_Key() + public async Task Keyed_WithKeyedFieldExpression_CreatesIndex() { // Arrange IndexHandler = new Mock(); - var options = new IndexCreationOptions { Name = "theIndexName" }; // Act - await Sut.CreateTextIndexAsync(t => t.SomeContent2, options); - - // Assert - IndexHandler.Verify(x => x.CreateTextIndexAsync(t => t.SomeContent2, options, null, default)); - } - - [Fact] - public async Task Ensure_Passes_PartitionKey_Custom_Key() - { - // Arrange - const string partitionKey = "thePartitionKey"; - - IndexHandler = new Mock(); - var options = new IndexCreationOptions { Name = "theIndexName" }; - - // Act - await Sut.CreateTextIndexAsync(t => t.SomeContent2, options, partitionKey); + await Sut.CreateTextIndexAsync, int>(keyedFieldExpression); // Assert IndexHandler.Verify( - x => x.CreateTextIndexAsync( - t => t.SomeContent2, options, partitionKey, default)); + x => x.CreateTextIndexAsync, int>(keyedFieldExpression, null, null, CancellationToken.None)); } [Fact] - public async Task Ensure_Passes_PartitionKey_And_CancellationToken_Custom_Key() + public async Task Keyed_WithKeyedFieldExpressionAndCancellationToken_CreatesIndex() { // Arrange - const string partitionKey = "thePartitionKey"; - const string indexName = "theIndexName"; 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(t => t.SomeContent2, token, options, partitionKey); + await Sut.CreateTextIndexAsync, int>(keyedFieldExpression, options); // Assert - IndexHandler - .Verify(x => x.CreateTextIndexAsync( - t => t.SomeContent2, options, partitionKey, token)); + 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/GetIndexNamesTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs index c73e7ae..78b2b77 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs @@ -2,6 +2,7 @@ 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; @@ -12,11 +13,11 @@ namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; public class GetIndexNamesTests : BaseIndexTests { [Fact] - public async Task Ensure_Returns_IndexNames() + public async Task WithNoParameters_ReturnsIndexNames() { // Arrange IndexHandler = new Mock(); - const string indexName = "theIndexName"; + var indexName = Fixture.Create(); IndexHandler .Setup(x => x.GetIndexesNamesAsync(It.IsAny(), It.IsAny())) @@ -31,16 +32,16 @@ public class GetIndexNamesTests : BaseIndexTests IndexHandler.Verify(x => x.GetIndexesNamesAsync(null, CancellationToken.None), Times.Once()); } - /* [Fact] - public async Task Ensure_Passes_Provided_CancellationToken() + public async Task WithCancellationToken_ReturnsIndexNames() { // Arrange - const string indexName = "theIndexName"; - var token = new CancellationToken(true); IndexHandler = new Mock(); + var indexName = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler - .Setup(x => x.GetIndexesNamesAsync(null, token)) + .Setup(x => x.GetIndexesNamesAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { indexName }); // Act @@ -49,17 +50,17 @@ public class GetIndexNamesTests : BaseIndexTests // Assert Assert.NotNull(result); Assert.Contains(result, x => x == indexName); + IndexHandler.Verify(x => x.GetIndexesNamesAsync(null, token), Times.Once()); } - */ [Fact] - public async Task Ensure_Handles_PartitionKey() + public async Task WithPartitionKey_ReturnsIndexNames() { // Arrange - const string partitionKey = "thePartitionKey"; - const string indexName = "theIndexName"; - IndexHandler = new Mock(); + var indexName = Fixture.Create(); + var partitionKey = Fixture.Create(); + IndexHandler .Setup(x => x.GetIndexesNamesAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { indexName }); @@ -70,105 +71,112 @@ public class GetIndexNamesTests : BaseIndexTests // Assert Assert.NotNull(result); Assert.Contains(result, x => x == indexName); + IndexHandler.Verify(x => x.GetIndexesNamesAsync(partitionKey, CancellationToken.None), Times.Once()); } - /*[Fact] - public async Task Ensure_Passes_Provided_CancellationToken_And_Handles_Partition_Key() + [Fact] + public async Task WithPartitionKeyAndCancellationToken_ReturnsIndexNames() { // Arrange - const string partitionKey = "thePartitionKey"; - const string indexName = "theIndexName"; + IndexHandler = new Mock(); + var indexName = Fixture.Create(); + var partitionKey = Fixture.Create(); var token = new CancellationToken(true); - IndexHandler = new Mock(); IndexHandler - .Setup(x => x.GetIndexesNamesAsync(partitionKey, token)) + .Setup(x => x.GetIndexesNamesAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { indexName }); // Act - var result = await Sut.GetIndexesNamesAsync(token, partitionKey); + 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 Ensure_Returns_IndexNames_Custom_Primary_Key() + [Fact] + public async Task Keyed_WithNoParameters_ReturnsIndexNames() { // Arrange - const string indexName = "theIndexName"; - IndexHandler = new Mock(); + var indexName = Fixture.Create(); + IndexHandler - .Setup(x => x.GetIndexesNamesAsync(null)) + .Setup(x => x.GetIndexesNamesAsync, int>(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { indexName }); // Act - var result = await Sut.GetIndexesNamesAsync(); + 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 Ensure_Passes_Provided_CancellationToken_Custom_Primary_Key() + [Fact] + public async Task Keyed_WithCancellationToken_ReturnsIndexNames() { // Arrange - const string indexName = "theIndexName"; - var token = new CancellationToken(true); IndexHandler = new Mock(); - IndexHandler - .Setup(x => x.GetIndexesNamesAsync(null, token)) - .ReturnsAsync(new List { indexName }); - - // Act - var result = await Sut.GetIndexesNamesAsync(token); - - // Assert - Assert.NotNull(result); - Assert.Contains(result, x => x == indexName); - }*/ - - /*[Fact] - public async Task Ensure_Handles_PartitionKey_Custom_Primary_Key() - { - // Arrange - const string indexName = "theIndexName"; - const string partitionKey = "thePartitionKey"; - - IndexHandler = new Mock(); - IndexHandler - .Setup(x => x.GetIndexesNamesAsync(partitionKey)) - .ReturnsAsync(new List { indexName }); - - // Act - var result = await Sut.GetIndexesNamesAsync(partitionKey); - - // Assert - Assert.NotNull(result); - Assert.Contains(result, x => x == indexName); - }*/ - - /*[Fact] - public async Task Ensure_Passes_Provided_CancellationToken_And_Handles_Partition_Key_Custom_Primary_Key() - { - // Arrange - const string indexName = "theIndexName"; - const string partitionKey = "thePartitionKey"; + var indexName = Fixture.Create(); var token = new CancellationToken(true); - IndexHandler = new Mock(); IndexHandler - .Setup(x => x.GetIndexesNamesAsync(partitionKey, token)) + .Setup(x => x.GetIndexesNamesAsync, int>(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { indexName }); // Act - var result = await Sut.GetIndexesNamesAsync(token, partitionKey); + 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/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/GetIndexNamesAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs index 087cfc6..2d385bf 100644 --- a/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs +++ b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs @@ -1,8 +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()); + } } From 807a4b17ce8ec6c5fa86a68f3b84766c5a5e9d4c Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sat, 24 Jun 2023 17:08:26 +0100 Subject: [PATCH 19/40] Ascending index tests and Descending Index and tests --- .../CreateAscendingIndexAsyncTests.cs | 294 +++++++++++++++ .../IndexTests/CreateAscendingIndexTests.cs | 29 -- .../CreateDescendingIndexAsyncTests.cs | 294 +++++++++++++++ ...xTests.cs => CreateTextIndexAsyncTests.cs} | 2 +- ...mesTests.cs => GetIndexNamesAsyncTests.cs} | 2 +- .../Infrastructure/GenericTestContext.cs | 15 +- .../CreateAscendingIndexAsyncTests.cs | 154 ++++++++ .../CreateDescendingIndexAsyncTests.cs | 154 ++++++++ .../BaseMongoRepository.Index.cs | 355 ++++++++++++++++-- .../IBaseMongoRepository.Index.cs | 206 +++++++++- .../BaseMongoRepository.TKey.Index.cs | 182 ++++++++- .../IBaseMongoRepository.TKey.Index.cs | 179 ++++++++- 12 files changed, 1768 insertions(+), 98 deletions(-) create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexAsyncTests.cs delete mode 100644 CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateDescendingIndexAsyncTests.cs rename CoreUnitTests/BaseMongoRepositoryTests/IndexTests/{CreateTextIndexTests.cs => CreateTextIndexAsyncTests.cs} (99%) rename CoreUnitTests/BaseMongoRepositoryTests/IndexTests/{GetIndexNamesTests.cs => GetIndexNamesAsyncTests.cs} (99%) create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateAscendingIndexAsyncTests.cs create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateDescendingIndexAsyncTests.cs 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/CreateAscendingIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs deleted file mode 100644 index 85b5b70..0000000 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; -using CoreUnitTests.Infrastructure.Model; -using MongoDbGenericRepository.DataAccess.Index; -using Moq; -using Xunit; - -namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; - -public class CreateAscendingIndexTests : BaseIndexTests -{ - /*[Fact] - public async Task CreateAscendingIndexAsync_EnsureTokenPassed() - { - // Arrange - IndexHandler = new Mock(); - var token = new CancellationToken(true); - - // Act - Expression> fieldExpression = t => t.SomeContent2; - // await Sut.CreateAscendingIndexAsync(fieldExpression, token); - - // Assert - IndexHandler.Verify(x => x.CreateAscendingIndexAsync( - fieldExpression, null, null, token)); - }*/ -} 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/CreateTextIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexAsyncTests.cs similarity index 99% rename from CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs rename to CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexAsyncTests.cs index 485fa54..7e1f651 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexAsyncTests.cs @@ -11,7 +11,7 @@ using CancellationToken = System.Threading.CancellationToken; namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; -public class CreateTextIndexTests : BaseIndexTests +public class CreateTextIndexAsyncTests : BaseIndexTests { private readonly Expression> fieldExpression = t => t.SomeContent2; private readonly Expression, object>> keyedFieldExpression = t => t.SomeContent2; diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs similarity index 99% rename from CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs rename to CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs index 78b2b77..6c9afb8 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs @@ -10,7 +10,7 @@ using Xunit; namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; -public class GetIndexNamesTests : BaseIndexTests +public class GetIndexNamesAsyncTests : BaseIndexTests { [Fact] public async Task WithNoParameters_ReturnsIndexNames() diff --git a/CoreUnitTests/Infrastructure/GenericTestContext.cs b/CoreUnitTests/Infrastructure/GenericTestContext.cs index 9b0a708..9c494d6 100644 --- a/CoreUnitTests/Infrastructure/GenericTestContext.cs +++ b/CoreUnitTests/Infrastructure/GenericTestContext.cs @@ -6,18 +6,13 @@ namespace CoreUnitTests.Infrastructure; public class GenericTestContext { - public GenericTestContext() - { - Fixture = new Fixture().Customize(new AutoMoqCustomization()); - } + public GenericTestContext() => Fixture = new Fixture().Customize(new AutoMoqCustomization()); protected Mock MockOf() - where T : class - { - return Fixture.Freeze>(); - } + where T : class => + Fixture.Freeze>(); protected IFixture Fixture { get; set; } - protected TSut Sut { get => Fixture.Create(); } -} \ No newline at end of file + protected TSut Sut => Fixture.Create(); +} 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/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/MongoDbGenericRepository/BaseMongoRepository.Index.cs b/MongoDbGenericRepository/BaseMongoRepository.Index.cs index c32dde0..c8192fd 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Index.cs @@ -1,29 +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 { /// - /// 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 IMongoDbIndexHandler _mongoDbIndexHandler; /// - /// The MongoDb accessor to manage indexes. + /// The MongoDb accessor to manage indexes. /// protected virtual IMongoDbIndexHandler MongoDbIndexHandler { get { - if (_mongoDbIndexHandler != null) { return _mongoDbIndexHandler; } + if (_mongoDbIndexHandler != null) + { + return _mongoDbIndexHandler; + } lock (_initLock) { @@ -32,9 +35,10 @@ namespace MongoDbGenericRepository _mongoDbIndexHandler = new MongoDbIndexHandler(MongoDbContext); } } + return _mongoDbIndexHandler; } - set { _mongoDbIndexHandler = value; } + set => _mongoDbIndexHandler = value; } /// @@ -120,7 +124,10 @@ namespace MongoDbGenericRepository } /// - public async Task CreateTextIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + public async Task CreateTextIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await CreateTextIndexAsync(field, null, partitionKey, cancellationToken); @@ -134,21 +141,31 @@ namespace MongoDbGenericRepository } /// - public async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + 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) + 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) + public async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await CreateTextIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); @@ -179,7 +196,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateTextIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + public virtual async Task CreateTextIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -187,7 +207,9 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions) where TDocument : IDocument where TKey : IEquatable { @@ -195,7 +217,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -203,7 +228,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -211,7 +239,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -219,44 +251,291 @@ namespace MongoDbGenericRepository } /// - public async Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public async Task CreateAscendingIndexAsync(Expression> field) where TDocument : IDocument { - return await CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey); + return await CreateAscendingIndexAsync(field, null, null, CancellationToken.None); } /// - public virtual async Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + 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 MongoDbIndexHandler.CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey); + return await CreateAscendingIndexAsync(field, null, null, CancellationToken.None); } /// - public async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument - { - return await CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey); - } - - /// - public virtual async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbIndexHandler.CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey); + return await CreateAscendingIndexAsync(field, null, null, cancellationToken); } /// - public async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + 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, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null) where TDocument : IDocument { return await CreateHashedIndexAsync(field, indexCreationOptions, partitionKey); } /// - public virtual async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { @@ -264,14 +543,20 @@ namespace MongoDbGenericRepository } /// - public async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null) where TDocument : IDocument { return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey); } /// - public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { @@ -293,4 +578,4 @@ namespace MongoDbGenericRepository await MongoDbIndexHandler.DropIndexAsync(indexName, partitionKey); } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Index.cs b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs index b3de813..6d44731 100644 --- a/MongoDbGenericRepository/IBaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs @@ -172,6 +172,91 @@ namespace MongoDbGenericRepository 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 @@ -183,7 +268,108 @@ namespace MongoDbGenericRepository /// 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) + 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; @@ -198,7 +384,23 @@ namespace MongoDbGenericRepository /// 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) + 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; diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs index e24022c..7f67270 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs @@ -1,16 +1,16 @@ -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 CancellationToken = System.Threading.CancellationToken; +using MongoDbGenericRepository.DataAccess.Index; +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 BaseMongoRepository : IBaseMongoRepository_Index where TKey : IEquatable @@ -18,13 +18,16 @@ namespace MongoDbGenericRepository private IMongoDbIndexHandler _mongoDbIndexHandler; /// - /// The MongoDb accessor to manage indexes. + /// The MongoDb accessor to manage indexes. /// protected virtual IMongoDbIndexHandler MongoDbIndexHandler { get { - if (_mongoDbIndexHandler != null) { return _mongoDbIndexHandler; } + if (_mongoDbIndexHandler != null) + { + return _mongoDbIndexHandler; + } lock (_initLock) { @@ -36,7 +39,7 @@ namespace MongoDbGenericRepository return _mongoDbIndexHandler; } - set { _mongoDbIndexHandler = value; } + set => _mongoDbIndexHandler = value; } /// @@ -89,14 +92,20 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) where TDocument : IDocument { return await CreateTextIndexAsync(field, indexCreationOptions, null, cancellationToken); } /// - public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) where TDocument : IDocument { return await CreateTextIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); @@ -110,31 +119,168 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateTextIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + 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) + 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, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public virtual async Task CreateAscendingIndexAsync(Expression> field) where TDocument : IDocument { - return await MongoDbIndexHandler.CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey); + return await CreateAscendingIndexAsync(field, null, null, CancellationToken.None); } /// - public virtual async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbIndexHandler.CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey); + 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); } /// @@ -158,4 +304,4 @@ namespace MongoDbGenericRepository await MongoDbIndexHandler.DropIndexAsync(indexName, partitionKey); } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs index cb97145..7b0678f 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs @@ -149,6 +149,80 @@ namespace MongoDbGenericRepository 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 @@ -159,7 +233,94 @@ namespace MongoDbGenericRepository /// 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) + 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; /// @@ -172,7 +333,21 @@ namespace MongoDbGenericRepository /// 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) + 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; /// From 387bf6beecffab9c5a7d707ce59599d3ef8fccb2 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sat, 24 Jun 2023 18:17:55 +0100 Subject: [PATCH 20/40] Cancellation tokens on Create Hashed Index --- .../IndexTests/CreateHashedIndexAsyncTests.cs | 294 ++++++++++++++++++ .../IndexTests/CreateHashedIndexAsyncTests.cs | 154 +++++++++ .../BaseMongoRepository.Index.cs | 119 ++++++- .../IBaseMongoRepository.Index.cs | 103 +++++- .../BaseMongoRepository.TKey.Index.cs | 53 +++- .../IBaseMongoRepository.TKey.Index.cs | 89 +++++- 6 files changed, 798 insertions(+), 14 deletions(-) create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateHashedIndexAsyncTests.cs create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateHashedIndexAsyncTests.cs 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/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/MongoDbGenericRepository/BaseMongoRepository.Index.cs b/MongoDbGenericRepository/BaseMongoRepository.Index.cs index c8192fd..a3eaee0 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Index.cs @@ -522,24 +522,123 @@ namespace MongoDbGenericRepository } /// - public async Task CreateHashedIndexAsync( - Expression> field, - IndexCreationOptions indexCreationOptions = null, - string partitionKey = null) + public async Task CreateHashedIndexAsync(Expression> field) where TDocument : IDocument { - return await CreateHashedIndexAsync(field, indexCreationOptions, partitionKey); + return await CreateHashedIndexAsync(field, null, null, CancellationToken.None); } /// - public virtual async Task CreateHashedIndexAsync( - Expression> field, - IndexCreationOptions indexCreationOptions = null, - string partitionKey = null) + 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 MongoDbIndexHandler.CreateHashedIndexAsync(field, indexCreationOptions, partitionKey); + 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); } /// diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Index.cs b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs index 6d44731..eaa6372 100644 --- a/MongoDbGenericRepository/IBaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs @@ -404,6 +404,91 @@ namespace MongoDbGenericRepository 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 @@ -415,7 +500,23 @@ namespace MongoDbGenericRepository /// 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) + 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; diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs index 7f67270..145d497 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs @@ -284,10 +284,59 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public virtual async Task CreateHashedIndexAsync(Expression> field) where TDocument : IDocument { - return await MongoDbIndexHandler.CreateHashedIndexAsync(field, indexCreationOptions, partitionKey); + 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); } /// diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs index 7b0678f..9b51ceb 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs @@ -350,6 +350,79 @@ namespace MongoDbGenericRepository 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 @@ -360,7 +433,21 @@ namespace MongoDbGenericRepository /// 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) + 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; /// From 11ce19cb5c6ef3a387067dbe7842e53e5536945a Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sat, 24 Jun 2023 19:10:12 +0100 Subject: [PATCH 21/40] Cancellation tokens for CombinedTextIndex and DropIndex --- .../CreateCombinedTextIndexAsyncTests.cs | 295 ++++++++++++++++++ .../IndexTests/DropIndexAsyncTests.cs | 145 +++++++++ .../CreateCombinedTextIndexAsyncTests.cs | 155 +++++++++ .../IndexTests/DropIndexAsyncTests.cs | 77 +++++ .../BaseMongoRepository.Index.cs | 182 +++++++++-- .../IBaseMongoRepository.Index.cs | 138 +++++++- .../BaseMongoRepository.TKey.Index.cs | 78 ++++- .../IBaseMongoRepository.TKey.Index.cs | 118 ++++++- 8 files changed, 1161 insertions(+), 27 deletions(-) create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateCombinedTextIndexAsyncTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/IndexTests/DropIndexAsyncTests.cs create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateCombinedTextIndexAsyncTests.cs create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/IndexTests/DropIndexAsyncTests.cs 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/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/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/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/MongoDbGenericRepository/BaseMongoRepository.Index.cs b/MongoDbGenericRepository/BaseMongoRepository.Index.cs index a3eaee0..9ed7e9d 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Index.cs @@ -642,39 +642,183 @@ namespace MongoDbGenericRepository } /// - public async Task CreateCombinedTextIndexAsync( - IEnumerable>> fields, - IndexCreationOptions indexCreationOptions = null, - string partitionKey = null) + public async Task CreateCombinedTextIndexAsync(IEnumerable>> fields) where TDocument : IDocument { - return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey); + return await CreateCombinedTextIndexAsync(fields, null, null, CancellationToken.None); } /// - public virtual async Task CreateCombinedTextIndexAsync( - IEnumerable>> fields, - IndexCreationOptions indexCreationOptions = null, - string partitionKey = null) + 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 MongoDbIndexHandler.CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey); + return await CreateCombinedTextIndexAsync(fields, null, null, CancellationToken.None); } /// - public async Task DropIndexAsync(string indexName, string partitionKey = null) - where TDocument : IDocument - { - await DropIndexAsync(indexName, partitionKey); - } - - /// - public virtual async Task DropIndexAsync(string indexName, string partitionKey = null) + public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - await MongoDbIndexHandler.DropIndexAsync(indexName, partitionKey); + 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/IBaseMongoRepository.Index.cs b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs index eaa6372..84abebf 100644 --- a/MongoDbGenericRepository/IBaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs @@ -520,6 +520,91 @@ namespace MongoDbGenericRepository 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 @@ -531,7 +616,44 @@ namespace MongoDbGenericRepository /// 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) + 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; @@ -542,7 +664,19 @@ namespace MongoDbGenericRepository /// 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) + 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; } diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs index 145d497..a327965 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs @@ -340,17 +340,87 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields) where TDocument : IDocument { - return await MongoDbIndexHandler.CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey); + return await CreateCombinedTextIndexAsync(fields, null, null, CancellationToken.None); } /// - public virtual async Task DropIndexAsync(string indexName, string partitionKey = null) + public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, CancellationToken cancellationToken) where TDocument : IDocument { - await MongoDbIndexHandler.DropIndexAsync(indexName, partitionKey); + 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/IBaseMongoRepository.TKey.Index.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs index 9b51ceb..cce8c25 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs @@ -450,6 +450,79 @@ namespace MongoDbGenericRepository 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 @@ -460,7 +533,38 @@ namespace MongoDbGenericRepository /// 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) + 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; /// @@ -469,7 +573,17 @@ namespace MongoDbGenericRepository /// The type representing a Document. /// The name of the index /// An optional partition key - Task DropIndexAsync(string indexName, string partitionKey = null) + 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 From dee1376a41535bb497fe529878b8bf9ea5ce5909 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sun, 25 Jun 2023 15:03:52 +0100 Subject: [PATCH 22/40] MongodbReader cancellation tokens and some unit tests --- .../BaseIndexTests.cs | 2 - .../CreateAscendingIndexAsyncTests.cs | 10 +- .../CreateCombinedTextIndexAsyncTests.cs | 10 +- .../CreateDescendingIndexAsyncTests.cs | 10 +- .../CreateHashedIndexAsyncTests.cs | 10 +- .../CreateTextIndexAsyncTests.cs | 10 +- .../DropIndexAsyncTests.cs | 8 +- .../MongoDbReaderTests/BaseReaderTests.cs | 69 ++ .../MongoDbReaderTests/GetByIdAsyncTests.cs | 115 +++ .../MongoDbReaderTests/GetByIdTests.cs | 114 +++ .../MongoDbReaderTests/GetOneAsyncTests.cs | 283 +++++++ .../MongoDbReaderTests/GetOneTests.cs | 282 +++++++ .../ProjectManyAsyncTests.cs | 123 ++++ .../MongoDbReaderTests/ProjectManyTests.cs | 117 +++ .../ProjectOneAsyncTests.cs | 121 +++ .../MongoDbReaderTests/ProjectOneTests.cs | 121 +++ .../Infrastructure/Model/TestProjection.cs | 10 + .../Abstractions/IMongoDbContext.cs | 2 +- .../DataAccess/Read/IMongoDbReader.cs | 48 +- .../DataAccess/Read/MongoDbReader.GroupBy.cs | 108 +++ .../DataAccess/Read/MongoDbReader.Main.cs | 692 +++++------------- .../DataAccess/Read/MongoDbReader.Project.cs | 107 +-- 22 files changed, 1758 insertions(+), 614 deletions(-) create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/BaseReaderTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByIdAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByIdTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetOneAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetOneTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/ProjectManyAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/ProjectManyTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/ProjectOneAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/ProjectOneTests.cs create mode 100644 CoreUnitTests/Infrastructure/Model/TestProjection.cs create mode 100644 MongoDbGenericRepository/DataAccess/Read/MongoDbReader.GroupBy.cs diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/BaseIndexTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/BaseIndexTests.cs index 566aa75..ba9ce13 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/BaseIndexTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/BaseIndexTests.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Threading; using CoreUnitTests.Infrastructure; using MongoDB.Bson; -using MongoDB.Bson.Serialization; using MongoDB.Driver; using MongoDbGenericRepository.DataAccess.Index; using Moq; @@ -41,5 +40,4 @@ public class BaseIndexTests : GenericTestContext return (asyncCursor, indexManager); } - } diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateAscendingIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateAscendingIndexAsyncTests.cs index e3040e8..01f5ebc 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateAscendingIndexAsyncTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateAscendingIndexAsyncTests.cs @@ -19,7 +19,7 @@ public class CreateAscendingIndexAsyncTests : BaseIndexTests private readonly Expression> fieldExpression = t => t.SomeContent2; [Fact] - public async Task WhenFieldExpression_ThenCreatesIndex() + public async Task WithFieldExpression_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -42,7 +42,7 @@ public class CreateAscendingIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndOptions_ThenCreatesIndex() + public async Task WithFieldExpressionAndOptions_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -68,7 +68,7 @@ public class CreateAscendingIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndPartitionKey_ThenCreatesIndex() + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -95,7 +95,7 @@ public class CreateAscendingIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndCancellationToken_ThenCreatesIndex() + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -121,7 +121,7 @@ public class CreateAscendingIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex() + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateCombinedTextIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateCombinedTextIndexAsyncTests.cs index 5f1ed30..d801586 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateCombinedTextIndexAsyncTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateCombinedTextIndexAsyncTests.cs @@ -29,7 +29,7 @@ public class CreateCombinedTextIndexAsyncTests : BaseIndexTests => this.testOutputHelper = testOutputHelper; [Fact] - public async Task WhenFieldExpression_ThenCreatesIndex() + public async Task WithFieldExpression_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -52,7 +52,7 @@ public class CreateCombinedTextIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndOptions_ThenCreatesIndex() + public async Task WithFieldExpressionAndOptions_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -78,7 +78,7 @@ public class CreateCombinedTextIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndPartitionKey_ThenCreatesIndex() + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -105,7 +105,7 @@ public class CreateCombinedTextIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndCancellationToken_ThenCreatesIndex() + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -131,7 +131,7 @@ public class CreateCombinedTextIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex() + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateDescendingIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateDescendingIndexAsyncTests.cs index d0bb876..8ad433a 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateDescendingIndexAsyncTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateDescendingIndexAsyncTests.cs @@ -19,7 +19,7 @@ public class CreateDescendingIndexAsyncTests : BaseIndexTests private readonly Expression> fieldExpression = t => t.SomeContent2; [Fact] - public async Task WhenFieldExpression_ThenCreatesIndex() + public async Task WithFieldExpression_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -42,7 +42,7 @@ public class CreateDescendingIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndOptions_ThenCreatesIndex() + public async Task WithFieldExpressionAndOptions_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -68,7 +68,7 @@ public class CreateDescendingIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndPartitionKey_ThenCreatesIndex() + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -95,7 +95,7 @@ public class CreateDescendingIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndCancellationToken_ThenCreatesIndex() + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -121,7 +121,7 @@ public class CreateDescendingIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex() + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateHashedIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateHashedIndexAsyncTests.cs index f1172c7..74c576d 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateHashedIndexAsyncTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateHashedIndexAsyncTests.cs @@ -24,7 +24,7 @@ public class CreateHashedIndexAsyncTests : BaseIndexTests => this.testOutputHelper = testOutputHelper; [Fact] - public async Task WhenFieldExpression_ThenCreatesIndex() + public async Task WithFieldExpression_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -47,7 +47,7 @@ public class CreateHashedIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndOptions_ThenCreatesIndex() + public async Task WithFieldExpressionAndOptions_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -73,7 +73,7 @@ public class CreateHashedIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndPartitionKey_ThenCreatesIndex() + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -100,7 +100,7 @@ public class CreateHashedIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndCancellationToken_ThenCreatesIndex() + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -126,7 +126,7 @@ public class CreateHashedIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex() + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateTextIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateTextIndexAsyncTests.cs index f54b2ff..3c482dd 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateTextIndexAsyncTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateTextIndexAsyncTests.cs @@ -19,7 +19,7 @@ public class CreateTextIndexAsyncTests : BaseIndexTests private readonly Expression> fieldExpression = t => t.SomeContent2; [Fact] - public async Task WhenFieldExpression_ThenCreatesIndex() + public async Task WithFieldExpression_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -42,7 +42,7 @@ public class CreateTextIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndOptions_ThenCreatesIndex() + public async Task WithFieldExpressionAndOptions_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -68,7 +68,7 @@ public class CreateTextIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndPartitionKey_ThenCreatesIndex() + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -95,7 +95,7 @@ public class CreateTextIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndCancellationToken_ThenCreatesIndex() + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -121,7 +121,7 @@ public class CreateTextIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex() + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/DropIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/DropIndexAsyncTests.cs index 6485411..b8b30ac 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/DropIndexAsyncTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/DropIndexAsyncTests.cs @@ -13,7 +13,7 @@ namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests; public class DropIndexAsyncTests : BaseIndexTests { [Fact] - public async Task WhenIndexName_ThenDropsIndex() + public async Task WithIndexName_DropsIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -30,7 +30,7 @@ public class DropIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenIndexNameAndPartitionKey_ThenDropsIndex() + public async Task WithIndexNameAndPartitionKey_DropsIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -50,7 +50,7 @@ public class DropIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenIndexNameAndCancellationToken_ThenDropsIndex() + public async Task WithIndexNameAndCancellationToken_DropsIndex() { // Arrange var expectedIndexName = Fixture.Create(); @@ -69,7 +69,7 @@ public class DropIndexAsyncTests : BaseIndexTests } [Fact] - public async Task WhenIndexNameAndPartitionKeyAndCancellationToken_ThenDropsIndex() + public async Task WithIndexNameAndPartitionKeyAndCancellationToken_DropsIndex() { // Arrange var expectedIndexName = Fixture.Create(); 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/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/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/Infrastructure/Model/TestProjection.cs b/CoreUnitTests/Infrastructure/Model/TestProjection.cs new file mode 100644 index 0000000..7d278a1 --- /dev/null +++ b/CoreUnitTests/Infrastructure/Model/TestProjection.cs @@ -0,0 +1,10 @@ +using System; + +namespace CoreUnitTests.Infrastructure.Model; + +public class TestProjection +{ + public Guid TestDocumentId { get; set; } + + public DateTime NestedData { get; set; } +} diff --git a/MongoDbGenericRepository/Abstractions/IMongoDbContext.cs b/MongoDbGenericRepository/Abstractions/IMongoDbContext.cs index bca34b4..e3c70b5 100644 --- a/MongoDbGenericRepository/Abstractions/IMongoDbContext.cs +++ b/MongoDbGenericRepository/Abstractions/IMongoDbContext.cs @@ -25,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/DataAccess/Read/IMongoDbReader.cs b/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs index 8665bd8..7230aa3 100644 --- a/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs +++ b/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs @@ -26,8 +26,8 @@ namespace MongoDbGenericRepository.DataAccess.Read /// An optional partition key. /// An optional cancellation Token. Task ProjectOneAsync( - Expression> filter, - Expression> projection, + Expression> filter, + Expression> projection, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument @@ -43,7 +43,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey = null) + TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -59,8 +59,8 @@ namespace MongoDbGenericRepository.DataAccess.Read /// An optional partition key. /// An optional cancellation Token. Task> ProjectManyAsync( - Expression> filter, - Expression> projection, + Expression> filter, + Expression> projection, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument @@ -76,13 +76,13 @@ namespace MongoDbGenericRepository.DataAccess.Read /// The document filter. /// The projection expression. /// An optional partition key. - List ProjectMany(Expression> filter, Expression> projection, string partitionKey = null) + 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, + /// 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. @@ -95,13 +95,13 @@ namespace MongoDbGenericRepository.DataAccess.Read List GroupBy( Expression> groupingCriteria, Expression, TProjection>> groupProjection, - string partitionKey = null) + 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, + /// 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. @@ -116,13 +116,13 @@ namespace MongoDbGenericRepository.DataAccess.Read Expression> filter, Expression> selector, Expression, TProjection>> projection, - string partitionKey = null) + 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, + /// 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. @@ -207,7 +207,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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 = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -246,7 +246,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A mongodb filter definition. /// A mongodb filter option. /// An optional partition key. - TDocument GetOne(FilterDefinition condition, FindOptions findOption = null, string partitionKey = null) + TDocument GetOne(FilterDefinition condition, FindOptions findOption = null, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -257,7 +257,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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 = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -307,7 +307,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A mongodb counting option. /// An optional partition key. bool Any(FilterDefinition condition, CountOptions countOption = null, - string partitionKey = null) + string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -318,7 +318,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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 = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -357,7 +357,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A mongodb filter option. /// An optional partition key. List GetAll(FilterDefinition condition, FindOptions findOption = null, - string partitionKey = null) + string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -368,7 +368,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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 = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -407,7 +407,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A mongodb counting option. /// An optional partitionKey long Count(FilterDefinition condition, CountOptions countOption = null, - string partitionKey = null) + string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -418,7 +418,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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 = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -443,7 +443,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A LINQ expression filter. /// A property selector to order by descending. /// An optional partitionKey. - TDocument GetByMax(Expression> filter, Expression> maxValueSelector, string partitionKey = null) + TDocument GetByMax(Expression> filter, Expression> maxValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -468,7 +468,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partitionKey. - TDocument GetByMin(Expression> filter, Expression> minValueSelector, string partitionKey = null) + TDocument GetByMin(Expression> filter, Expression> minValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -495,7 +495,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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 = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -522,7 +522,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partition key. - TValue GetMinValue(Expression> filter, Expression> minValueSelector, string partitionKey = null) + TValue GetMinValue(Expression> filter, Expression> minValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; diff --git a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.GroupBy.cs b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.GroupBy.cs new file mode 100644 index 0000000..35185b3 --- /dev/null +++ b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.GroupBy.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 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: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 7252cc4..05ab62d 100644 --- a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs +++ b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs @@ -1,39 +1,32 @@ -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 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 virtual async 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 virtual async 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 virtual async 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 virtual async 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 virtual async 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 virtual async 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 virtual async 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 61639ee..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 : IMongoDbReader + 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 From 03b8b273f8f619f7f42a735a341e21c706389692 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sun, 25 Jun 2023 16:55:44 +0100 Subject: [PATCH 23/40] any and count tests --- .../MongoDbReaderTests/AnyAsyncTests.cs | 282 ++++++++++++++++++ .../MongoDbReaderTests/AnyTests.cs | 281 +++++++++++++++++ .../MongoDbReaderTests/CountAsyncTests.cs | 282 ++++++++++++++++++ .../MongoDbReaderTests/CountTests.cs | 281 +++++++++++++++++ 4 files changed, 1126 insertions(+) create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/AnyAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/AnyTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/CountAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/CountTests.cs 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/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); + } +} From 06991307338a2768a3e29b349a8d3d06e7aed328 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sun, 25 Jun 2023 21:12:52 +0100 Subject: [PATCH 24/40] Reader GetAll, GetByMin and getByMax test --- .../MongoDbReaderTests/GetAllAsyncTests.cs | 283 ++++++++++++++++++ .../MongoDbReaderTests/GetAllTests.cs | 283 ++++++++++++++++++ .../MongoDbReaderTests/GetByMaxAsyncTests.cs | 115 +++++++ .../MongoDbReaderTests/GetByMaxTests.cs | 119 ++++++++ .../MongoDbReaderTests/GetByMinAsyncTests.cs | 119 ++++++++ .../MongoDbReaderTests/GetByMinTests.cs | 119 ++++++++ 6 files changed, 1038 insertions(+) create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinTests.cs diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllAsyncTests.cs new file mode 100644 index 0000000..51818de --- /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 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/GetAllTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllTests.cs new file mode 100644 index 0000000..db168c5 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllTests.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/GetByMaxAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxAsyncTests.cs new file mode 100644 index 0000000..d9af227 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxAsyncTests.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/GetByMaxTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxTests.cs new file mode 100644 index 0000000..000ea7b --- /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 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/GetByMinAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinAsyncTests.cs new file mode 100644 index 0000000..000ea7b --- /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 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/GetByMinTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinTests.cs new file mode 100644 index 0000000..500afc2 --- /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 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); + } +} From f7ba046f2039eb7c828fed6d85011d60174cf5da Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sun, 25 Jun 2023 21:13:17 +0100 Subject: [PATCH 25/40] Reader GetAll, GetByMin and getByMax test --- .../MongoDbReaderTests/GetAllAsyncTests.cs | 102 +++++++++--------- .../MongoDbReaderTests/GetAllTests.cs | 97 +++++++++-------- .../MongoDbReaderTests/GetByMaxAsyncTests.cs | 22 ++-- .../MongoDbReaderTests/GetByMaxTests.cs | 40 +++---- .../MongoDbReaderTests/GetByMinAsyncTests.cs | 10 +- .../MongoDbReaderTests/GetByMinTests.cs | 10 +- 6 files changed, 142 insertions(+), 139 deletions(-) diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllAsyncTests.cs index 51818de..db168c5 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllAsyncTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllAsyncTests.cs @@ -14,7 +14,7 @@ using Xunit; namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; -public class GetOneAsyncTests : BaseReaderTests +public class GetAllAsyncTests : BaseReaderTests { [Fact] public async Task WithFilter_GetsMatchingDocuments() @@ -27,14 +27,14 @@ public class GetOneAsyncTests : BaseReaderTests // Act - var result = await Sut.GetOneAsync(filter); + var result = await Sut.GetAllAsync(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); + 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().Be(documents[0]); + result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] @@ -43,19 +43,19 @@ public class GetOneAsyncTests : BaseReaderTests // Arrange var collection = MockOf>(); var documents = Fixture.CreateMany().ToList(); - var token = new CancellationToken(true); + var token = new CancellationToken(false); Expression> filter = x => x.Id == documents[0].Id; var (context, cursor) = SetupAsyncGet(documents, collection); // Act - var result = await Sut.GetOneAsync(filter, cancellationToken: token); + var result = await Sut.GetAllAsync(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); + 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().Be(documents[0]); + result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] @@ -69,14 +69,14 @@ public class GetOneAsyncTests : BaseReaderTests var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); // Act - var result = await Sut.GetOneAsync(filter, partitionKey); + var result = await Sut.GetAllAsync(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); + 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().Be(documents[0]); + result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] @@ -86,19 +86,19 @@ public class GetOneAsyncTests : BaseReaderTests var collection = MockOf>(); var documents = Fixture.CreateMany().ToList(); var partitionKey = Fixture.Create(); - var token = new CancellationToken(true); + 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.GetOneAsync(filter, partitionKey, token); + var result = await Sut.GetAllAsync(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); + 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().Be(documents[0]); + result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] @@ -111,14 +111,14 @@ public class GetOneAsyncTests : BaseReaderTests var (context, cursor) = SetupAsyncGet(documents, collection); // Act - var result = await Sut.GetOneAsync(condition); + var result = await Sut.GetAllAsync(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); + 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().Be(documents[0]); + result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] @@ -128,18 +128,18 @@ public class GetOneAsyncTests : BaseReaderTests var collection = MockOf>(); var documents = Fixture.CreateMany().ToList(); var condition = Builders.Filter.Eq("Id", documents[0].Id); - var token = new CancellationToken(true); + var token = new CancellationToken(false); var (context, cursor) = SetupAsyncGet(documents, collection); // Act - var result = await Sut.GetOneAsync(condition, cancellationToken: token); + var result = await Sut.GetAllAsync(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); + 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().Be(documents[0]); + result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] @@ -153,14 +153,14 @@ public class GetOneAsyncTests : BaseReaderTests var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); // Act - var result = await Sut.GetOneAsync(condition, partitionKey: partitionKey); + 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.Once); - cursor.Verify(x => x.Current, 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().Be(documents[0]); + result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] @@ -171,18 +171,18 @@ public class GetOneAsyncTests : BaseReaderTests var documents = Fixture.CreateMany().ToList(); var condition = Builders.Filter.Eq("Id", documents[0].Id); var partitionKey = Fixture.Create(); - var token = new CancellationToken(true); + var token = new CancellationToken(false); var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); // Act - var result = await Sut.GetOneAsync(condition, partitionKey: partitionKey, cancellationToken: token); + 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.Once); - cursor.Verify(x => x.MoveNextAsync(token), 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().Be(documents[0]); + result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] @@ -200,14 +200,14 @@ public class GetOneAsyncTests : BaseReaderTests var (context, cursor) = SetupAsyncGet(documents, collection); // Act - var result = await Sut.GetOneAsync(condition, options); + var result = await Sut.GetAllAsync(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); + 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().Be(documents[0]); + result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] @@ -222,18 +222,18 @@ public class GetOneAsyncTests : BaseReaderTests .Without(x => x.Hint) .Create(); var condition = Builders.Filter.Eq("Id", documents[0].Id); - var token = new CancellationToken(true); + var token = new CancellationToken(false); var (context, cursor) = SetupAsyncGet(documents, collection); // Act - var result = await Sut.GetOneAsync(condition, options, cancellationToken: token); + var result = await Sut.GetAllAsync(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); + 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().Be(documents[0]); + result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] @@ -248,19 +248,19 @@ public class GetOneAsyncTests : BaseReaderTests .Without(x => x.Hint) .Create(); var condition = Builders.Filter.Eq("Id", documents[0].Id); - var token = new CancellationToken(true); + var token = new CancellationToken(false); var partitionKey = Fixture.Create(); var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); // Act - var result = await Sut.GetOneAsync(condition, options, partitionKey, token); + var result = await Sut.GetAllAsync(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); + 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().Be(documents[0]); + result.Should().OnlyContain(x => documents.Contains(x)); } private (Mock, Mock>) SetupAsyncGet( diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllTests.cs index db168c5..2d679ab 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetAllTests.cs @@ -3,7 +3,6 @@ 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; @@ -14,73 +13,73 @@ using Xunit; namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; -public class GetAllAsyncTests : BaseReaderTests +public class GetAllTests : BaseReaderTests { [Fact] - public async Task WithFilter_GetsMatchingDocuments() + public void 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); + var (context, cursor) = SetupGet(documents, collection); // Act - var result = await Sut.GetAllAsync(filter); + 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.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1)); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1)); result.Should().NotBeNull(); result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] - public async Task WithFilterAndCancellationToken_GetsMatchingDocuments() + 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) = SetupAsyncGet(documents, collection); + var (context, cursor) = SetupGet(documents, collection); // Act - var result = await Sut.GetAllAsync(filter, cancellationToken: token); + 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.MoveNextAsync(token), Times.Exactly(documents.Count + 1)); + cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1)); result.Should().NotBeNull(); result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] - public async Task WithFilterAndPartitionKey_GetsMatchingDocuments() + 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) = SetupAsyncGet(documents, collection, partitionKey); + var (context, cursor) = SetupGet(documents, collection, partitionKey); // Act - var result = await Sut.GetAllAsync(filter, partitionKey); + 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.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1)); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1)); result.Should().NotBeNull(); result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] - public async Task WithFilterAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() + public void WithFilterAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() { // Arrange var collection = MockOf>(); @@ -88,83 +87,83 @@ public class GetAllAsyncTests : BaseReaderTests var partitionKey = Fixture.Create(); var token = new CancellationToken(false); Expression> filter = x => x.Id == documents[0].Id; - var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + var (context, cursor) = SetupGet(documents, collection, partitionKey); // Act - var result = await Sut.GetAllAsync(filter, partitionKey, token); + 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.MoveNextAsync(token), Times.Exactly(documents.Count + 1)); + cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1)); result.Should().NotBeNull(); result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] - public async Task WithCondition_GetsMatchingDocuments() + 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) = SetupAsyncGet(documents, collection); + var (context, cursor) = SetupGet(documents, collection); // Act - var result = await Sut.GetAllAsync(condition); + 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.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1)); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1)); result.Should().NotBeNull(); result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] - public async Task WithConditionAndCancellationToken_GetsMatchingDocuments() + 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) = SetupAsyncGet(documents, collection); + var (context, cursor) = SetupGet(documents, collection); // Act - var result = await Sut.GetAllAsync(condition, cancellationToken: token); + 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.MoveNextAsync(token), Times.Exactly(documents.Count + 1)); + cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1)); result.Should().NotBeNull(); result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] - public async Task WithConditionAndPartitionKey_GetsMatchingDocuments() + 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) = SetupAsyncGet(documents, collection, partitionKey); + var (context, cursor) = SetupGet(documents, collection, partitionKey); // Act - var result = await Sut.GetAllAsync(condition, partitionKey: partitionKey); + var result = Sut.GetAll(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.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 async Task WithConditionAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() + public void WithConditionAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() { // Arrange var collection = MockOf>(); @@ -172,21 +171,21 @@ public class GetAllAsyncTests : BaseReaderTests 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); + var (context, cursor) = SetupGet(documents, collection, partitionKey); // Act - var result = await Sut.GetAllAsync(condition, partitionKey: partitionKey, cancellationToken: token); + 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.MoveNextAsync(token), Times.Exactly(documents.Count + 1)); + cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1)); result.Should().NotBeNull(); result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] - public async Task WithConditionAndFindOptions_GetsMatchingDocuments() + public void WithConditionAndFindOptions_GetsMatchingDocuments() { // Arrange var collection = MockOf>(); @@ -197,21 +196,21 @@ public class GetAllAsyncTests : BaseReaderTests .Without(x => x.Hint) .Create(); var condition = Builders.Filter.Eq("Id", documents[0].Id); - var (context, cursor) = SetupAsyncGet(documents, collection); + var (context, cursor) = SetupGet(documents, collection); // Act - var result = await Sut.GetAllAsync(condition, options); + 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.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1)); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1)); result.Should().NotBeNull(); result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] - public async Task WithConditionAndFindOptionsAndCancellationToken_GetsMatchingDocuments() + public void WithConditionAndFindOptionsAndCancellationToken_GetsMatchingDocuments() { // Arrange var collection = MockOf>(); @@ -223,21 +222,21 @@ public class GetAllAsyncTests : BaseReaderTests .Create(); var condition = Builders.Filter.Eq("Id", documents[0].Id); var token = new CancellationToken(false); - var (context, cursor) = SetupAsyncGet(documents, collection); + var (context, cursor) = SetupGet(documents, collection); // Act - var result = await Sut.GetAllAsync(condition, options, cancellationToken: token); + 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.MoveNextAsync(token), Times.Exactly(documents.Count + 1)); + cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1)); result.Should().NotBeNull(); result.Should().OnlyContain(x => documents.Contains(x)); } [Fact] - public async Task WithConditionAndFindOptionsAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() + public void WithConditionAndFindOptionsAndPartitionKeyAndCancellationToken_GetsMatchingDocuments() { // Arrange var collection = MockOf>(); @@ -250,27 +249,27 @@ public class GetAllAsyncTests : BaseReaderTests 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); + var (context, cursor) = SetupGet(documents, collection, partitionKey); // Act - var result = await Sut.GetAllAsync(condition, options, partitionKey, token); + 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.MoveNextAsync(token), Times.Exactly(documents.Count + 1)); + cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1)); result.Should().NotBeNull(); result.Should().OnlyContain(x => documents.Contains(x)); } - private (Mock, Mock>) SetupAsyncGet( + private (Mock, Mock>) SetupGet( List documents, Mock> collection, string partitionKey = null) { - var asyncCursor = SetupAsyncCursor(documents); + var asyncCursor = SetupSyncCursor(documents); - SetupFindAsync(collection, asyncCursor); + SetupFindSync(collection, asyncCursor); var context = MockOf(); diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxAsyncTests.cs index d9af227..000ea7b 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxAsyncTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxAsyncTests.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; using AutoFixture; @@ -13,10 +14,13 @@ using Xunit; namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; -public class GetByIdAsyncTests : BaseReaderTests +public class GetByMaxAsyncTests : BaseReaderTests { + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + private readonly Expression> selector = x => x.SomeValue; + [Fact] - public async Task WithId_GetsMatchingDocument() + public async Task WithFilterAndSelector_GetsMatchingDocument() { // Arrange var collection = MockOf>(); @@ -24,7 +28,7 @@ public class GetByIdAsyncTests : BaseReaderTests var (context, cursor) = SetupAsyncGet(documents, collection); // Act - var result = await Sut.GetByIdAsync(documents[0].Id); + var result = await Sut.GetByMaxAsync(filter, selector); // Assert context.Verify(x => x.GetCollection(null), Times.Once); @@ -35,7 +39,7 @@ public class GetByIdAsyncTests : BaseReaderTests } [Fact] - public async Task WithIdAndCancellationToken_GetsMatchingDocument() + public async Task WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument() { // Arrange var collection = MockOf>(); @@ -44,7 +48,7 @@ public class GetByIdAsyncTests : BaseReaderTests var (context, cursor) = SetupAsyncGet(documents, collection); // Act - var result = await Sut.GetByIdAsync(documents[0].Id, cancellationToken: token); + var result = await Sut.GetByMaxAsync(filter, selector, cancellationToken: token); // Assert context.Verify(x => x.GetCollection(null), Times.Once); @@ -55,7 +59,7 @@ public class GetByIdAsyncTests : BaseReaderTests } [Fact] - public async Task WithIdAndPartitionKey_GetsMatchingDocument() + public async Task WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument() { // Arrange var collection = MockOf>(); @@ -64,7 +68,7 @@ public class GetByIdAsyncTests : BaseReaderTests var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); // Act - var result = await Sut.GetByIdAsync(documents[0].Id, partitionKey); + var result = await Sut.GetByMaxAsync(filter, selector, partitionKey); // Assert context.Verify(x => x.GetCollection(partitionKey), Times.Once); @@ -75,7 +79,7 @@ public class GetByIdAsyncTests : BaseReaderTests } [Fact] - public async Task WithIdAndPartitionKeyAndCancellationToken_GetsMatchingDocument() + public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument() { // Arrange var collection = MockOf>(); @@ -85,7 +89,7 @@ public class GetByIdAsyncTests : BaseReaderTests var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); // Act - var result = await Sut.GetByIdAsync(documents[0].Id, partitionKey, token); + var result = await Sut.GetByMaxAsync(filter, selector, partitionKey, token); // Assert context.Verify(x => x.GetCollection(partitionKey), Times.Once); diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxTests.cs index 000ea7b..500afc2 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMaxTests.cs @@ -14,99 +14,99 @@ using Xunit; namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; -public class GetByMaxAsyncTests : BaseReaderTests +public class GetByMaxTests : BaseReaderTests { private readonly Expression> filter = x => x.SomeContent == "SomeContent"; private readonly Expression> selector = x => x.SomeValue; [Fact] - public async Task WithFilterAndSelector_GetsMatchingDocument() + public void WithFilterAndSelector_GetsMatchingDocument() { // Arrange var collection = MockOf>(); var documents = Fixture.CreateMany().ToList(); - var (context, cursor) = SetupAsyncGet(documents, collection); + var (context, cursor) = SetupSyncGet(documents, collection); // Act - var result = await Sut.GetByMaxAsync(filter, selector); + 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.MoveNextAsync(CancellationToken.None), Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); result.Should().NotBeNull(); result.Should().Be(documents[0]); } [Fact] - public async Task WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument() + public void WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument() { // Arrange var collection = MockOf>(); var documents = Fixture.CreateMany().ToList(); var token = new CancellationToken(true); - var (context, cursor) = SetupAsyncGet(documents, collection); + var (context, cursor) = SetupSyncGet(documents, collection); // Act - var result = await Sut.GetByMaxAsync(filter, selector, cancellationToken: token); + 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.MoveNextAsync(token), Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); result.Should().NotBeNull(); result.Should().Be(documents[0]); } [Fact] - public async Task WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument() + public void WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument() { // Arrange var collection = MockOf>(); var documents = Fixture.CreateMany().ToList(); var partitionKey = Fixture.Create(); - var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); + var (context, cursor) = SetupSyncGet(documents, collection, partitionKey); // Act - var result = await Sut.GetByMaxAsync(filter, selector, partitionKey); + 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.MoveNextAsync(CancellationToken.None), Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); result.Should().NotBeNull(); result.Should().Be(documents[0]); } [Fact] - public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument() + 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) = SetupAsyncGet(documents, collection, partitionKey); + var (context, cursor) = SetupSyncGet(documents, collection, partitionKey); // Act - var result = await Sut.GetByMaxAsync(filter, selector, partitionKey, token); + 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.MoveNextAsync(token), Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); result.Should().NotBeNull(); result.Should().Be(documents[0]); } - private (Mock, Mock>) SetupAsyncGet( + private (Mock, Mock>) SetupSyncGet( List documents, Mock> collection, string partitionKey = null) { - var asyncCursor = SetupAsyncCursor(documents); + var asyncCursor = SetupSyncCursor(documents); - SetupFindAsync(collection, asyncCursor); + SetupFindSync(collection, asyncCursor); var context = MockOf(); diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinAsyncTests.cs index 000ea7b..5521897 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinAsyncTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinAsyncTests.cs @@ -14,7 +14,7 @@ using Xunit; namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; -public class GetByMaxAsyncTests : BaseReaderTests +public class GetByMinAsyncTests : BaseReaderTests { private readonly Expression> filter = x => x.SomeContent == "SomeContent"; private readonly Expression> selector = x => x.SomeValue; @@ -28,7 +28,7 @@ public class GetByMaxAsyncTests : BaseReaderTests var (context, cursor) = SetupAsyncGet(documents, collection); // Act - var result = await Sut.GetByMaxAsync(filter, selector); + var result = await Sut.GetByMinAsync(filter, selector); // Assert context.Verify(x => x.GetCollection(null), Times.Once); @@ -48,7 +48,7 @@ public class GetByMaxAsyncTests : BaseReaderTests var (context, cursor) = SetupAsyncGet(documents, collection); // Act - var result = await Sut.GetByMaxAsync(filter, selector, cancellationToken: token); + var result = await Sut.GetByMinAsync(filter, selector, cancellationToken: token); // Assert context.Verify(x => x.GetCollection(null), Times.Once); @@ -68,7 +68,7 @@ public class GetByMaxAsyncTests : BaseReaderTests var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); // Act - var result = await Sut.GetByMaxAsync(filter, selector, partitionKey); + var result = await Sut.GetByMinAsync(filter, selector, partitionKey); // Assert context.Verify(x => x.GetCollection(partitionKey), Times.Once); @@ -89,7 +89,7 @@ public class GetByMaxAsyncTests : BaseReaderTests var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey); // Act - var result = await Sut.GetByMaxAsync(filter, selector, partitionKey, token); + var result = await Sut.GetByMinAsync(filter, selector, partitionKey, token); // Assert context.Verify(x => x.GetCollection(partitionKey), Times.Once); diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinTests.cs index 500afc2..0cd9d9c 100644 --- a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinTests.cs +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetByMinTests.cs @@ -14,7 +14,7 @@ using Xunit; namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; -public class GetByMaxTests : BaseReaderTests +public class GetByMinTests : BaseReaderTests { private readonly Expression> filter = x => x.SomeContent == "SomeContent"; private readonly Expression> selector = x => x.SomeValue; @@ -28,7 +28,7 @@ public class GetByMaxTests : BaseReaderTests var (context, cursor) = SetupSyncGet(documents, collection); // Act - var result = Sut.GetByMax(filter, selector); + var result = Sut.GetByMin(filter, selector); // Assert context.Verify(x => x.GetCollection(null), Times.Once); @@ -48,7 +48,7 @@ public class GetByMaxTests : BaseReaderTests var (context, cursor) = SetupSyncGet(documents, collection); // Act - var result = Sut.GetByMax(filter, selector, cancellationToken: token); + var result = Sut.GetByMin(filter, selector, cancellationToken: token); // Assert context.Verify(x => x.GetCollection(null), Times.Once); @@ -68,7 +68,7 @@ public class GetByMaxTests : BaseReaderTests var (context, cursor) = SetupSyncGet(documents, collection, partitionKey); // Act - var result = Sut.GetByMax(filter, selector, partitionKey); + var result = Sut.GetByMin(filter, selector, partitionKey); // Assert context.Verify(x => x.GetCollection(partitionKey), Times.Once); @@ -89,7 +89,7 @@ public class GetByMaxTests : BaseReaderTests var (context, cursor) = SetupSyncGet(documents, collection, partitionKey); // Act - var result = Sut.GetByMax(filter, selector, partitionKey, token); + var result = Sut.GetByMin(filter, selector, partitionKey, token); // Assert context.Verify(x => x.GetCollection(partitionKey), Times.Once); From 240b61d1d2d9204b58c94c332872a0aa06c98418 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sun, 25 Jun 2023 22:27:40 +0100 Subject: [PATCH 26/40] tests for getMaxVValue and GetMinValue --- .../GetMaxValueAsyncTests .cs | 114 ++++++++++++++++++ .../MongoDbReaderTests/GetMaxValueTests .cs | 114 ++++++++++++++++++ .../GetMinValueAsyncTests .cs | 114 ++++++++++++++++++ .../MongoDbReaderTests/GetMinValueTests .cs | 114 ++++++++++++++++++ .../DataAccess/Base/DataAccessBase.cs | 1 - 5 files changed, 456 insertions(+), 1 deletion(-) create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMaxValueAsyncTests .cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMaxValueTests .cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMinValueAsyncTests .cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMinValueTests .cs 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/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs b/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs index 1875161..3e55c59 100644 --- a/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs +++ b/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs @@ -168,7 +168,6 @@ namespace MongoDbGenericRepository.DataAccess.Base .Limit(1); } - #endregion } } From 40e287418b7f37ae898e79bcfa8b92f4474c879a Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sat, 1 Jul 2023 22:27:11 +0100 Subject: [PATCH 27/40] read only and tests --- .../Infrastructure/Model/TestDocument.cs | 9 +- .../Infrastructure/Model/TestProjection.cs | 2 + .../TestKeyedMongoRepository.cs | 22 +- .../TestKeyedMongoRepositoryContext.cs | 24 +- .../TestKeyedReadOnlyMongoRepository.cs | 30 + ...TestKeyedReadOnlyMongoRepositoryContext.cs | 45 + .../GetSortedPaginatedAsyncTests.cs | 246 +++ .../GroupByTests.cs | 243 +++ .../IReadOnlyMongoRepository.TKey.cs | 1018 +++++++++++-- .../BaseMongoRepository.TKey.ReadOnly.cs | 1319 ++++++++++++----- 10 files changed, 2466 insertions(+), 492 deletions(-) create mode 100644 CoreUnitTests/Infrastructure/TestKeyedReadOnlyMongoRepository.cs create mode 100644 CoreUnitTests/Infrastructure/TestKeyedReadOnlyMongoRepositoryContext.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GroupByTests.cs diff --git a/CoreUnitTests/Infrastructure/Model/TestDocument.cs b/CoreUnitTests/Infrastructure/Model/TestDocument.cs index 51f87f7..5f92e89 100644 --- a/CoreUnitTests/Infrastructure/Model/TestDocument.cs +++ b/CoreUnitTests/Infrastructure/Model/TestDocument.cs @@ -4,23 +4,21 @@ using MongoDbGenericRepository.Models; namespace CoreUnitTests.Infrastructure.Model; - public class TestDocument : Document { public TestDocument() { Version = 2; - Nested = new Nested - { - SomeDate = DateTime.UtcNow - }; + Nested = new Nested {SomeDate = DateTime.UtcNow}; Children = new List(); } public int SomeValue { get; set; } public string SomeContent { get; set; } + public string SomeContent2 { get; set; } + public string SomeContent3 { get; set; } public int GroupingKey { get; set; } @@ -29,4 +27,3 @@ public class TestDocument : Document public List Children { get; set; } } - diff --git a/CoreUnitTests/Infrastructure/Model/TestProjection.cs b/CoreUnitTests/Infrastructure/Model/TestProjection.cs index 7d278a1..c1c8714 100644 --- a/CoreUnitTests/Infrastructure/Model/TestProjection.cs +++ b/CoreUnitTests/Infrastructure/Model/TestProjection.cs @@ -7,4 +7,6 @@ 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 index 8780599..69f219d 100644 --- a/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs +++ b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs @@ -16,23 +16,11 @@ public class TestKeyedMongoRepository : BaseMongoRepository { } - public void SetIndexHandler(IMongoDbIndexHandler indexHandler) - { - MongoDbIndexHandler = indexHandler; - } + public void SetIndexHandler(IMongoDbIndexHandler indexHandler) => MongoDbIndexHandler = indexHandler; - public void SetDbCreator(IMongoDbCreator creator) - { - MongoDbCreator = creator; - } + public void SetDbCreator(IMongoDbCreator creator) => MongoDbCreator = creator; - public void SetReader(IMongoDbReader reader) - { - MongoDbReader = reader; - } + public void SetReader(IMongoDbReader reader) => MongoDbReader = reader; - public void SetEraser(IMongoDbEraser eraser) - { - MongoDbEraser = eraser; - } -} \ No newline at end of file + public void SetEraser(IMongoDbEraser eraser) => MongoDbEraser = eraser; +} diff --git a/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs b/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs index 55b4bd5..2b86c1a 100644 --- a/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs +++ b/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs @@ -12,13 +12,13 @@ namespace CoreUnitTests.Infrastructure; public class TestKeyedMongoRepositoryContext where TKey : IEquatable { - private readonly Mock _mongoDatabase; + private readonly Mock mongoDatabase; - private TestKeyedMongoRepository _sut; + private TestKeyedMongoRepository sut; protected TestKeyedMongoRepositoryContext() { - _mongoDatabase = new Mock(); + mongoDatabase = new Mock(); Fixture = new Fixture(); } @@ -28,33 +28,33 @@ public class TestKeyedMongoRepositoryContext { get { - if (_sut != null) + if (sut != null) { - return _sut; + return sut; } - _sut = new TestKeyedMongoRepository(_mongoDatabase.Object); + sut = new TestKeyedMongoRepository(mongoDatabase.Object); if (IndexHandler != null) { - _sut.SetIndexHandler(IndexHandler.Object); + sut.SetIndexHandler(IndexHandler.Object); } if (Creator != null) { - _sut.SetDbCreator(Creator.Object); + sut.SetDbCreator(Creator.Object); } if (Reader != null) { - _sut.SetReader(Reader.Object); + sut.SetReader(Reader.Object); } if (Eraser != null) { - _sut.SetEraser(Eraser.Object); + sut.SetEraser(Eraser.Object); } - return _sut; + return sut; } } @@ -65,4 +65,4 @@ public class TestKeyedMongoRepositoryContext protected Mock Reader { get; set; } protected Mock Eraser { get; set; } -} \ No newline at end of file +} 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/ReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs b/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs new file mode 100644 index 0000000..3fe29a1 --- /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 : TestKeyedReadOnlyMongoRepositoryContext +{ + 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..abbdca7 --- /dev/null +++ b/CoreUnitTests/ReadOnlyMongoRepositoryTests/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.ReadOnlyMongoRepositoryTests; + +public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext +{ + 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); + } +} diff --git a/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs index 6e93142..d7d2124 100644 --- a/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs +++ b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using MongoDB.Driver; using MongoDbGenericRepository.Models; @@ -9,188 +10,746 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// read only repository interface + /// read only repository interface /// /// The key type - public interface IReadOnlyMongoRepository where TKey : IEquatable + 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 @@ -198,51 +757,109 @@ 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 @@ -250,50 +867,193 @@ 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 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; @@ -302,8 +1062,35 @@ 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. @@ -311,16 +1098,57 @@ namespace MongoDbGenericRepository /// The grouping criteria. /// The projected group result. /// The partition key of your document, if any. - List GroupBy( - Expression> groupingCriteria, - Expression, TProjection>> groupProjection, - string partitionKey = null) + List GroupBy(Expression> groupingCriteria, Expression, TProjection>> groupProjection, 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. @@ -329,20 +1157,32 @@ namespace MongoDbGenericRepository /// The grouping criteria. /// The projected group result. /// The partition key of your document, if any. - List GroupBy( - Expression> filter, - Expression> groupingCriteria, - Expression, TProjection>> groupProjection, - string partitionKey = null) - where TDocument : IDocument - where TProjection : class, new(); + List GroupBy(Expression> filter, Expression> groupingCriteria, Expression, TProjection>> groupProjection, 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. /// A LINQ expression filter. @@ -351,17 +1191,19 @@ 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; /// - /// 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. /// A LINQ expression filter. @@ -369,12 +1211,14 @@ 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, 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/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs index 55691d4..8cbe59a 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs @@ -1,42 +1,34 @@ -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. + /// The MongoDbContext /// - public string ConnectionString { get; protected set; } + protected IMongoDbContext MongoDbContext { get; set; } /// - /// The database name. + /// A MongoDb Reader for read operations /// - public string DatabaseName { get; protected set; } + protected IMongoDbReader MongoDbReader { get; set; } /// - /// The MongoDbContext - /// - protected IMongoDbContext MongoDbContext; - - /// - /// A MongoDb Reader for read operations - /// - protected IMongoDbReader MongoDbReader; - - /// - /// 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. @@ -46,24 +38,167 @@ namespace MongoDbGenericRepository } /// - /// The constructor 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 constructor taking a . + /// The constructor taking a . /// - /// A mongodb context implementing + /// A mongodb context implementing protected ReadOnlyMongoRepository(IMongoDbContext mongoDbContext) { SetupMongoDbContext(mongoDbContext); } /// - /// Setups the repository with a . + /// 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) @@ -73,10 +208,13 @@ namespace MongoDbGenericRepository } /// - /// Setups the repository with a connection string and a database name. + /// 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 + /// + /// 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 (string.IsNullOrWhiteSpace(databaseName)) @@ -92,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 await AnyAsync(filter, null, cancellationToken); + } + + /// + public async Task AnyAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return await AnyAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public async Task AnyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbReader.AnyAsync(filter, partitionKey, cancellationToken); + } + + /// + public bool Any(Expression> filter) + where TDocument : IDocument + { + return Any(filter, null, CancellationToken.None); + } + + /// + public bool Any(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + { + return Any(filter, null, cancellationToken); + } + + /// + public bool Any(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return Any(filter, partitionKey, CancellationToken.None); + } + + /// + public bool Any(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + 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); } - /// - /// 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 GetByMinAsync(Expression> filter, Expression> minValueSelector) where TDocument : IDocument { - return await MongoDbReader.GetByMinAsync(filter, minValueSelector, partitionKey); + return await GetByMinAsync(filter, minValueSelector, 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 ascending. - /// An optional partitionKey. - public TDocument GetByMin(Expression> filter, Expression> minValueSelector, string partitionKey = null) + /// + public async Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbReader.GetByMin(filter, minValueSelector, partitionKey); + return await GetByMinAsync(filter, minValueSelector, null, 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 async Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey) where TDocument : IDocument { - return await MongoDbReader.GetMaxValueAsync(filter, maxValueSelector, partitionKey); + return await GetByMinAsync(filter, minValueSelector, partitionKey, 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 async Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbReader.GetMaxValue(filter, maxValueSelector, partitionKey); + return await MongoDbReader.GetByMinAsync(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 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 TDocument GetByMin(Expression> filter, Expression> minValueSelector) where TDocument : IDocument { - return await MongoDbReader.GetMinValueAsync(filter, minValueSelector, partitionKey); + return GetByMin(filter, minValueSelector, null, 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 TDocument GetByMin( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbReader.GetMinValue(filter, minValueSelector, partitionKey); + 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); @@ -390,144 +930,183 @@ namespace MongoDbGenericRepository #region Project - /// - 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 From f9ea630611e80445424b0f864c1bcd21a121a024 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sun, 2 Jul 2023 14:10:55 +0100 Subject: [PATCH 28/40] unit tests for keyed readonly repo --- .../AnyAsyncTests.cs | 101 ++++++++++++++++ .../AnyTests.cs | 100 ++++++++++++++++ .../CountAsyncTests.cs | 108 +++++++++++++++++ .../CountTests.cs | 105 ++++++++++++++++ .../GetAllAsyncTests.cs | 112 ++++++++++++++++++ .../GetAllTests.cs | 111 +++++++++++++++++ .../GetByIdAsyncTests.cs | 109 +++++++++++++++++ .../GetByIdTests.cs | 109 +++++++++++++++++ .../GetByMaxAsyncTests.cs | 112 ++++++++++++++++++ .../GetByMaxTests.cs | 111 +++++++++++++++++ .../GetByMinAsyncTests.cs | 112 ++++++++++++++++++ .../GetByMinTests.cs | 111 +++++++++++++++++ .../GetMaxValueAsyncTests.cs | 108 +++++++++++++++++ .../GetMaxValueTests.cs | 107 +++++++++++++++++ .../GetMinValueAsyncTests.cs | 108 +++++++++++++++++ .../GetMinValueTests.cs | 107 +++++++++++++++++ .../GetOneAsyncTests.cs | 110 +++++++++++++++++ .../GetOneTests.cs | 110 +++++++++++++++++ .../GetSortedPaginatedAsyncTests.cs | 2 +- .../GroupByTests.cs | 2 +- .../BaseMongoRepository.TKey.ReadOnly.cs | 2 +- 21 files changed, 1954 insertions(+), 3 deletions(-) create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyAsyncTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/CountAsyncTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/CountTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetAllAsyncTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetAllTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdAsyncTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxAsyncTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMinAsyncTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMinTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMaxValueAsyncTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMaxValueTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMinValueAsyncTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMinValueTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneTests.cs rename CoreUnitTests/{ReadOnlyMongoRepositoryTests => KeyedReadOnlyMongoRepositoryTests}/GetSortedPaginatedAsyncTests.cs (99%) rename CoreUnitTests/{ReadOnlyMongoRepositoryTests => KeyedReadOnlyMongoRepositoryTests}/GroupByTests.cs (99%) diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyAsyncTests.cs new file mode 100644 index 0000000..6ba8dae --- /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> 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(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(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(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(filter, partitionKey, token), + Times.Once); + } + + private void SetupReader() + { + Reader = new Mock(); + Reader + .Setup( + x => x.AnyAsync( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyTests.cs new file mode 100644 index 0000000..af13658 --- /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> 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(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(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(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(filter, partitionKey, token), + Times.Once); + } + + private void SetupReader() + { + Reader = new Mock(); + Reader + .Setup( + x => x.Any( + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/CountAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/CountAsyncTests.cs new file mode 100644 index 0000000..96723f7 --- /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> 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(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(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(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(filter, 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/CountTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/CountTests.cs new file mode 100644 index 0000000..a84fe1c --- /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> 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(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(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(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(filter, 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetAllAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetAllAsyncTests.cs new file mode 100644 index 0000000..a871c08 --- /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> 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(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(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(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(filter, 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetAllTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetAllTests.cs new file mode 100644 index 0000000..3ff29bb --- /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> 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(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(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(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(filter, 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdAsyncTests.cs new file mode 100644 index 0000000..d51f1be --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdAsyncTests.cs @@ -0,0 +1,109 @@ +using System; +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 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(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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdTests.cs new file mode 100644 index 0000000..c7e39a3 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdTests.cs @@ -0,0 +1,109 @@ +using System; +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 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(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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxAsyncTests.cs new file mode 100644 index 0000000..508e637 --- /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> 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxTests.cs new file mode 100644 index 0000000..987bca6 --- /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> 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMinAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMinAsyncTests.cs new file mode 100644 index 0000000..fc2e3c7 --- /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> 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMinTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMinTests.cs new file mode 100644 index 0000000..e3eacee --- /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> 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMaxValueAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMaxValueAsyncTests.cs new file mode 100644 index 0000000..f9fe0c8 --- /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> 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMaxValueTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMaxValueTests.cs new file mode 100644 index 0000000..06af072 --- /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> 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMinValueAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMinValueAsyncTests.cs new file mode 100644 index 0000000..4e0cc5d --- /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> 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMinValueTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMinValueTests.cs new file mode 100644 index 0000000..5b861a6 --- /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> 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs new file mode 100644 index 0000000..d817465 --- /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> filter = document => document.SomeContent == "SomeContent"; + + [Fact] + public async Task WithId_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(filter, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithIdAndCancellationToken_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(filter, null, token), + Times.Once); + } + + [Fact] + public async Task WithIdAndPartitionKey_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(filter, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithIdAndPartitionKeyAndCancellationToken_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(filter, 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneTests.cs new file mode 100644 index 0000000..289f1d0 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneTests.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 GetOneTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + + [Fact] + public void WithId_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(filter, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithIdAndCancellationToken_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(filter, null, token), + Times.Once); + } + + [Fact] + public void WithIdAndPartitionKey_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(filter, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithIdAndPartitionKeyAndCancellationToken_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(filter, 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); + } +} diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs similarity index 99% rename from CoreUnitTests/ReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs rename to CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs index 3fe29a1..1b199c8 100644 --- a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs @@ -13,7 +13,7 @@ using MongoDbGenericRepository.DataAccess.Read; using Moq; using Xunit; -namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; public class GetSortedPaginatedAsyncTests : TestKeyedReadOnlyMongoRepositoryContext { diff --git a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GroupByTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GroupByTests.cs similarity index 99% rename from CoreUnitTests/ReadOnlyMongoRepositoryTests/GroupByTests.cs rename to CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GroupByTests.cs index abbdca7..018c4bc 100644 --- a/CoreUnitTests/ReadOnlyMongoRepositoryTests/GroupByTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GroupByTests.cs @@ -10,7 +10,7 @@ using MongoDbGenericRepository.DataAccess.Read; using Moq; using Xunit; -namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext { diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs index 8cbe59a..4e5c741 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs @@ -594,7 +594,7 @@ namespace MongoDbGenericRepository CancellationToken cancellationToken) where TDocument : IDocument { - return MongoDbReader.GetByMax(filter, maxValueSelector, partitionKey); + return MongoDbReader.GetByMax(filter, maxValueSelector, partitionKey, cancellationToken); } /// From b09b359867f61408305dd7a00af483b1df48f680 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sun, 2 Jul 2023 17:54:30 +0100 Subject: [PATCH 29/40] rest of the unit tests for readonly repo --- .../Infrastructure/Model/TestDocument.cs | 2 + .../GetOneAsyncTests.cs | 8 +- .../ProjectManyAsyncTests.cs | 114 ++++++++++ .../ProjectManyTests.cs | 113 ++++++++++ .../ProjectOneAsyncTests.cs | 112 ++++++++++ .../ProjectOneTests.cs | 111 ++++++++++ .../SumByAsyncTests.cs | 198 ++++++++++++++++++ 7 files changed, 654 insertions(+), 4 deletions(-) create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyAsyncTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneAsyncTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/SumByAsyncTests.cs diff --git a/CoreUnitTests/Infrastructure/Model/TestDocument.cs b/CoreUnitTests/Infrastructure/Model/TestDocument.cs index 5f92e89..2985c16 100644 --- a/CoreUnitTests/Infrastructure/Model/TestDocument.cs +++ b/CoreUnitTests/Infrastructure/Model/TestDocument.cs @@ -15,6 +15,8 @@ public class TestDocument : Document public int SomeValue { get; set; } + public decimal SomeDecimalValue { get; set; } + public string SomeContent { get; set; } public string SomeContent2 { get; set; } diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs index d817465..9ae88f4 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs @@ -17,7 +17,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext private readonly Expression> filter = document => document.SomeContent == "SomeContent"; [Fact] - public async Task WithId_GetsOne() + public async Task WithFilter_GetsOne() { // Arrange var document = Fixture.Create(); @@ -36,7 +36,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext } [Fact] - public async Task WithIdAndCancellationToken_GetsOne() + public async Task WithFilterAndCancellationToken_GetsOne() { // Arrange var document = Fixture.Create(); @@ -56,7 +56,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext } [Fact] - public async Task WithIdAndPartitionKey_GetsOne() + public async Task WithFilterAndPartitionKey_GetsOne() { // Arrange var document = Fixture.Create(); @@ -76,7 +76,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext } [Fact] - public async Task WithIdAndPartitionKeyAndCancellationToken_GetsOne() + public async Task WithFilterAndPartitionKeyAndCancellationToken_GetsOne() { // Arrange var document = Fixture.Create(); diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyAsyncTests.cs new file mode 100644 index 0000000..53535b4 --- /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> 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyTests.cs new file mode 100644 index 0000000..47b9a09 --- /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> 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneAsyncTests.cs new file mode 100644 index 0000000..e027aa3 --- /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> 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneTests.cs new file mode 100644 index 0000000..280fcd0 --- /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> 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/SumByAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/SumByAsyncTests.cs new file mode 100644 index 0000000..9652042 --- /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> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> intSelector = document => document.SomeValue; + private readonly Expression> 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(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); + } +} From dc7a4dc67b428ce91b5467e74104d22875c47341 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Tue, 4 Jul 2023 22:38:01 +0100 Subject: [PATCH 30/40] tests for readonly repositories --- .../Infrastructure/Model/TestDocument.cs | 2 + .../Model/TestDocumentWithKey.cs | 4 +- .../TestReadOnlyMongoRepository.cs | 29 + .../TestReadOnlyMongoRepositoryContext.cs | 49 + .../AnyAsyncTests.cs | 16 +- .../AnyTests.cs | 16 +- .../CountAsyncTests.cs | 16 +- .../CountTests.cs | 16 +- .../GetAllAsyncTests.cs | 26 +- .../GetAllTests.cs | 26 +- .../GetByIdAsyncTests.cs | 35 +- .../GetByIdTests.cs | 36 +- .../GetByMaxAsyncTests.cs | 30 +- .../GetByMaxTests.cs | 30 +- .../GetByMinAsyncTests.cs | 30 +- .../GetByMinTests.cs | 30 +- .../GetMaxValueAsyncTests.cs | 20 +- .../GetMaxValueTests.cs | 20 +- .../GetMinValueAsyncTests.cs | 20 +- .../GetMinValueTests.cs | 20 +- .../GetOneAsyncTests.cs | 26 +- .../GetOneTests.cs | 35 +- .../GetSortedPaginatedAsyncTests.cs | 36 +- .../GroupByTests.cs | 80 +- .../ProjectManyAsyncTests.cs | 20 +- .../ProjectManyTests.cs | 20 +- .../ProjectOneAsyncTests.cs | 20 +- .../ProjectOneTests.cs | 20 +- .../SumByAsyncTests.cs | 36 +- .../AnyAsyncTests.cs | 349 +++ .../ReadOnlyMongoRepositoryTests/AnyTests.cs | 349 +++ .../CountAsyncTests.cs | 371 +++ .../CountTests.cs | 373 +++ .../GetAllAsyncTests.cs | 389 +++ .../GetAllTests.cs | 388 +++ .../GetByIdAsyncTests.cs | 203 ++ .../GetByIdTests.cs | 202 ++ .../GetByMaxAsyncTests.cs | 212 ++ .../GetByMaxTests.cs | 211 ++ .../GetByMinAsyncTests.cs | 212 ++ .../GetByMinTests.cs | 211 ++ .../GetMaxValueAsyncTests.cs | 204 ++ .../GetMaxValueTests.cs | 203 ++ .../GetMinValueAsyncTests.cs | 204 ++ .../GetMinValueTests.cs | 203 ++ .../GetOneAsyncTests.cs | 387 +++ .../GetOneTests.cs | 386 +++ .../GetSortedPaginatedAsyncTests.cs | 246 ++ .../GroupByTests.cs | 474 ++++ .../ProjectManyAsyncTests.cs | 214 ++ .../ProjectManyTests.cs | 214 ++ .../ProjectOneAsyncTests.cs | 212 ++ .../ProjectOneTests.cs | 211 ++ .../SumByAsyncTests.cs | 384 +++ .../Abstractions/IBaseReadOnlyRepository.cs | 1903 +++++++++++-- .../ReadOnlyMongoRepository.cs | 2410 ++++++++++++----- 56 files changed, 10877 insertions(+), 1212 deletions(-) create mode 100644 CoreUnitTests/Infrastructure/TestReadOnlyMongoRepository.cs create mode 100644 CoreUnitTests/Infrastructure/TestReadOnlyMongoRepositoryContext.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/AnyAsyncTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/AnyTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/CountAsyncTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/CountTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GetAllAsyncTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GetAllTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByIdAsyncTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByIdTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByMaxAsyncTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByMaxTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByMinAsyncTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GetByMinTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GetMaxValueAsyncTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GetMaxValueTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GetMinValueAsyncTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GetMinValueTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GetOneTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/GroupByTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/ProjectManyAsyncTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/ProjectManyTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/ProjectOneAsyncTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/ProjectOneTests.cs create mode 100644 CoreUnitTests/ReadOnlyMongoRepositoryTests/SumByAsyncTests.cs diff --git a/CoreUnitTests/Infrastructure/Model/TestDocument.cs b/CoreUnitTests/Infrastructure/Model/TestDocument.cs index 2985c16..9d09bf6 100644 --- a/CoreUnitTests/Infrastructure/Model/TestDocument.cs +++ b/CoreUnitTests/Infrastructure/Model/TestDocument.cs @@ -25,6 +25,8 @@ public class TestDocument : Document 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 index 2a76873..a189b86 100644 --- a/CoreUnitTests/Infrastructure/Model/TestDocumentWithKey.cs +++ b/CoreUnitTests/Infrastructure/Model/TestDocumentWithKey.cs @@ -22,6 +22,8 @@ public class TestDocumentWithKey : IDocument 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; } @@ -31,4 +33,4 @@ public class TestDocumentWithKey : IDocument public Nested Nested { get; set; } public List Children { get; set; } -} \ No newline at end of file +} 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/KeyedReadOnlyMongoRepositoryTests/AnyAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyAsyncTests.cs index 6ba8dae..a3a06fb 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyAsyncTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyAsyncTests.cs @@ -12,9 +12,9 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class AnyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +public class AnyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; [Fact] public async Task WithFilter_GetsResult() @@ -28,7 +28,7 @@ public class AnyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().BeTrue(); Reader.Verify( - x => x.AnyAsync(filter, null, CancellationToken.None), + x => x.AnyAsync, int>(filter, null, CancellationToken.None), Times.Once); } @@ -46,7 +46,7 @@ public class AnyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().BeTrue(); Reader.Verify( - x => x.AnyAsync(filter, null, token), + x => x.AnyAsync, int>(filter, null, token), Times.Once); } @@ -64,7 +64,7 @@ public class AnyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().BeTrue(); Reader.Verify( - x => x.AnyAsync(filter, partitionKey, CancellationToken.None), + x => x.AnyAsync, int>(filter, partitionKey, CancellationToken.None), Times.Once); } @@ -83,7 +83,7 @@ public class AnyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().BeTrue(); Reader.Verify( - x => x.AnyAsync(filter, partitionKey, token), + x => x.AnyAsync, int>(filter, partitionKey, token), Times.Once); } @@ -92,8 +92,8 @@ public class AnyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext Reader = new Mock(); Reader .Setup( - x => x.AnyAsync( - It.IsAny>>(), + 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 index af13658..2918a54 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/AnyTests.cs @@ -11,9 +11,9 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class AnyTests : TestKeyedReadOnlyMongoRepositoryContext +public class AnyTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; [Fact] public void WithFilter_GetsResult() @@ -27,7 +27,7 @@ public class AnyTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().BeTrue(); Reader.Verify( - x => x.Any(filter, null, CancellationToken.None), + x => x.Any, int>(filter, null, CancellationToken.None), Times.Once); } @@ -45,7 +45,7 @@ public class AnyTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().BeTrue(); Reader.Verify( - x => x.Any(filter, null, token), + x => x.Any, int>(filter, null, token), Times.Once); } @@ -63,7 +63,7 @@ public class AnyTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().BeTrue(); Reader.Verify( - x => x.Any(filter, partitionKey, CancellationToken.None), + x => x.Any, int>(filter, partitionKey, CancellationToken.None), Times.Once); } @@ -82,7 +82,7 @@ public class AnyTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().BeTrue(); Reader.Verify( - x => x.Any(filter, partitionKey, token), + x => x.Any, int>(filter, partitionKey, token), Times.Once); } @@ -91,8 +91,8 @@ public class AnyTests : TestKeyedReadOnlyMongoRepositoryContext Reader = new Mock(); Reader .Setup( - x => x.Any( - It.IsAny>>(), + 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 index 96723f7..902c905 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/CountAsyncTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/CountAsyncTests.cs @@ -14,9 +14,9 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class CountAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +public class CountAsyncTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; [Fact] public async Task WithFilter_GetsOne() @@ -32,7 +32,7 @@ public class CountAsyncTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(count); Reader.Verify( - x => x.CountAsync(filter, null, CancellationToken.None), + x => x.CountAsync, int>(filter, null, CancellationToken.None), Times.Once); } @@ -51,7 +51,7 @@ public class CountAsyncTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(count); Reader.Verify( - x => x.CountAsync(filter, null, token), + x => x.CountAsync, int>(filter, null, token), Times.Once); } @@ -70,7 +70,7 @@ public class CountAsyncTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(count); Reader.Verify( - x => x.CountAsync(filter, partitionKey, CancellationToken.None), + x => x.CountAsync, int>(filter, partitionKey, CancellationToken.None), Times.Once); } @@ -90,7 +90,7 @@ public class CountAsyncTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(count); Reader.Verify( - x => x.CountAsync(filter, partitionKey, token), + x => x.CountAsync, int>(filter, partitionKey, token), Times.Once); } @@ -99,8 +99,8 @@ public class CountAsyncTests : TestKeyedReadOnlyMongoRepositoryContext Reader = new Mock(); Reader .Setup( - x => x.CountAsync( - It.IsAny>>(), + 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 index a84fe1c..c1f0626 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/CountTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/CountTests.cs @@ -11,9 +11,9 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class CountTests : TestKeyedReadOnlyMongoRepositoryContext +public class CountTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; [Fact] public void WithFilter_GetsOne() @@ -29,7 +29,7 @@ public class CountTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(count); Reader.Verify( - x => x.Count(filter, null, CancellationToken.None), + x => x.Count, int>(filter, null, CancellationToken.None), Times.Once); } @@ -48,7 +48,7 @@ public class CountTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(count); Reader.Verify( - x => x.Count(filter, null, token), + x => x.Count, int>(filter, null, token), Times.Once); } @@ -67,7 +67,7 @@ public class CountTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(count); Reader.Verify( - x => x.Count(filter, partitionKey, CancellationToken.None), + x => x.Count, int>(filter, partitionKey, CancellationToken.None), Times.Once); } @@ -87,7 +87,7 @@ public class CountTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(count); Reader.Verify( - x => x.Count(filter, partitionKey, token), + x => x.Count, int>(filter, partitionKey, token), Times.Once); } @@ -96,8 +96,8 @@ public class CountTests : TestKeyedReadOnlyMongoRepositoryContext Reader = new Mock(); Reader .Setup( - x => x.Count( - It.IsAny>>(), + 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 index a871c08..049f5db 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetAllAsyncTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetAllAsyncTests.cs @@ -14,15 +14,15 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class GetAllAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +public class GetAllAsyncTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; [Fact] public async Task WithFilter_GetsOne() { // Arrange - var document = Fixture.CreateMany().ToList(); + var document = Fixture.CreateMany>().ToList(); SetupReader(document); @@ -33,7 +33,7 @@ public class GetAllAsyncTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetAllAsync(filter, null, CancellationToken.None), + x => x.GetAllAsync, int>(filter, null, CancellationToken.None), Times.Once); } @@ -41,7 +41,7 @@ public class GetAllAsyncTests : TestKeyedReadOnlyMongoRepositoryContext public async Task WithFilterAndCancellationToken_GetsOne() { // Arrange - var document = Fixture.CreateMany().ToList(); + var document = Fixture.CreateMany>().ToList(); var token = new CancellationToken(true); SetupReader(document); @@ -53,7 +53,7 @@ public class GetAllAsyncTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetAllAsync(filter, null, token), + x => x.GetAllAsync, int>(filter, null, token), Times.Once); } @@ -61,7 +61,7 @@ public class GetAllAsyncTests : TestKeyedReadOnlyMongoRepositoryContext public async Task WithFilterAndPartitionKey_GetsOne() { // Arrange - var document = Fixture.CreateMany().ToList(); + var document = Fixture.CreateMany>().ToList(); var partitionKey = Fixture.Create(); SetupReader(document); @@ -73,7 +73,7 @@ public class GetAllAsyncTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetAllAsync(filter, partitionKey, CancellationToken.None), + x => x.GetAllAsync, int>(filter, partitionKey, CancellationToken.None), Times.Once); } @@ -81,7 +81,7 @@ public class GetAllAsyncTests : TestKeyedReadOnlyMongoRepositoryContext public async Task WithFilterAndPartitionKeyAndCancellationToken_GetsOne() { // Arrange - var document = Fixture.CreateMany().ToList(); + var document = Fixture.CreateMany>().ToList(); var partitionKey = Fixture.Create(); var token = new CancellationToken(true); @@ -94,17 +94,17 @@ public class GetAllAsyncTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetAllAsync(filter, partitionKey, token), + x => x.GetAllAsync, int>(filter, partitionKey, token), Times.Once); } - private void SetupReader(List documents) + private void SetupReader(List> documents) { Reader = new Mock(); Reader .Setup( - x => x.GetAllAsync( - It.IsAny>>(), + 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 index 3ff29bb..82aac09 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetAllTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetAllTests.cs @@ -13,15 +13,15 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class GetAllTests : TestKeyedReadOnlyMongoRepositoryContext +public class GetAllTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; [Fact] public void WithFilter_GetsOne() { // Arrange - var document = Fixture.CreateMany().ToList(); + var document = Fixture.CreateMany>().ToList(); SetupReader(document); @@ -32,7 +32,7 @@ public class GetAllTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetAll(filter, null, CancellationToken.None), + x => x.GetAll, int>(filter, null, CancellationToken.None), Times.Once); } @@ -40,7 +40,7 @@ public class GetAllTests : TestKeyedReadOnlyMongoRepositoryContext public void WithFilterAndCancellationToken_GetsOne() { // Arrange - var document = Fixture.CreateMany().ToList(); + var document = Fixture.CreateMany>().ToList(); var token = new CancellationToken(true); SetupReader(document); @@ -52,7 +52,7 @@ public class GetAllTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetAll(filter, null, token), + x => x.GetAll, int>(filter, null, token), Times.Once); } @@ -60,7 +60,7 @@ public class GetAllTests : TestKeyedReadOnlyMongoRepositoryContext public void WithFilterAndPartitionKey_GetsOne() { // Arrange - var document = Fixture.CreateMany().ToList(); + var document = Fixture.CreateMany>().ToList(); var partitionKey = Fixture.Create(); SetupReader(document); @@ -72,7 +72,7 @@ public class GetAllTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetAll(filter, partitionKey, CancellationToken.None), + x => x.GetAll, int>(filter, partitionKey, CancellationToken.None), Times.Once); } @@ -80,7 +80,7 @@ public class GetAllTests : TestKeyedReadOnlyMongoRepositoryContext public void WithFilterAndPartitionKeyAndCancellationToken_GetsOne() { // Arrange - var document = Fixture.CreateMany().ToList(); + var document = Fixture.CreateMany>().ToList(); var partitionKey = Fixture.Create(); var token = new CancellationToken(true); @@ -93,17 +93,17 @@ public class GetAllTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetAll(filter, partitionKey, token), + x => x.GetAll, int>(filter, partitionKey, token), Times.Once); } - private void SetupReader(List documents) + private void SetupReader(List> documents) { Reader = new Mock(); Reader .Setup( - x => x.GetAll( - It.IsAny>>(), + 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 index d51f1be..6d9e8c2 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdAsyncTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdAsyncTests.cs @@ -1,6 +1,3 @@ -using System; -using System.Linq; -using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; using AutoFixture; @@ -13,24 +10,24 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class GetByIdAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +public class GetByIdAsyncTests : TestKeyedReadOnlyMongoRepositoryContext { [Fact] public async Task WithId_Gets() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); SetupReader(document); // Act - var result = await Sut.GetByIdAsync(document.Id); + 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), + x => x.GetByIdAsync, int>(document.Id, null, CancellationToken.None), Times.Once); } @@ -38,19 +35,19 @@ public class GetByIdAsyncTests : TestKeyedReadOnlyMongoRepositoryContext public async Task WithIdAndCancellationToken_Gets() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var token = new CancellationToken(true); SetupReader(document); // Act - var result = await Sut.GetByIdAsync(document.Id, token); + 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), + x => x.GetByIdAsync, int>(document.Id, null, token), Times.Once); } @@ -58,19 +55,19 @@ public class GetByIdAsyncTests : TestKeyedReadOnlyMongoRepositoryContext public async Task WithIdAndPartitionKey_Gets() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var partitionKey = Fixture.Create(); SetupReader(document); // Act - var result = await Sut.GetByIdAsync(document.Id, partitionKey); + 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), + x => x.GetByIdAsync, int>(document.Id, partitionKey, CancellationToken.None), Times.Once); } @@ -78,30 +75,30 @@ public class GetByIdAsyncTests : TestKeyedReadOnlyMongoRepositoryContext public async Task WithIdAndPartitionKeyAndCancellationToken_Gets() { // Arrange - var document = Fixture.Create(); + 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); + 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), + x => x.GetByIdAsync, int>(document.Id, partitionKey, token), Times.Once); } - private void SetupReader(TestDocument document) + private void SetupReader(TestDocumentWithKey document) { Reader = new Mock(); Reader .Setup( - x => x.GetByIdAsync( - It.IsAny(), + x => x.GetByIdAsync, int>( + It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(document); diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdTests.cs index c7e39a3..f9cef3d 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByIdTests.cs @@ -1,8 +1,4 @@ -using System; -using System.Linq; -using System.Linq.Expressions; using System.Threading; -using System.Threading.Tasks; using AutoFixture; using CoreUnitTests.Infrastructure; using CoreUnitTests.Infrastructure.Model; @@ -13,24 +9,24 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class GetByIdTests : TestKeyedReadOnlyMongoRepositoryContext +public class GetByIdTests : TestKeyedReadOnlyMongoRepositoryContext { [Fact] public void WithId_Gets() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); SetupReader(document); // Act - var result = Sut.GetById(document.Id); + var result = Sut.GetById>(document.Id); // Assert result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetById(document.Id, null, CancellationToken.None), + x => x.GetById, int>(document.Id, null, CancellationToken.None), Times.Once); } @@ -38,19 +34,19 @@ public class GetByIdTests : TestKeyedReadOnlyMongoRepositoryContext public void WithIdAndCancellationToken_Gets() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var token = new CancellationToken(true); SetupReader(document); // Act - var result = Sut.GetById(document.Id, token); + var result = Sut.GetById>(document.Id, token); // Assert result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetById(document.Id, null, token), + x => x.GetById, int>(document.Id, null, token), Times.Once); } @@ -58,19 +54,19 @@ public class GetByIdTests : TestKeyedReadOnlyMongoRepositoryContext public void WithIdAndPartitionKey_Gets() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var partitionKey = Fixture.Create(); SetupReader(document); // Act - var result = Sut.GetById(document.Id, partitionKey); + 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), + x => x.GetById, int>(document.Id, partitionKey, CancellationToken.None), Times.Once); } @@ -78,30 +74,30 @@ public class GetByIdTests : TestKeyedReadOnlyMongoRepositoryContext public void WithIdAndPartitionKeyAndCancellationToken_Gets() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var partitionKey = Fixture.Create(); var token = new CancellationToken(true); SetupReader(document); // Act - var result = Sut.GetById(document.Id, partitionKey, token); + 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), + x => x.GetById, int>(document.Id, partitionKey, token), Times.Once); } - private void SetupReader(TestDocument document) + private void SetupReader(TestDocumentWithKey document) { Reader = new Mock(); Reader .Setup( - x => x.GetById( - It.IsAny(), + x => x.GetById, int>( + It.IsAny(), It.IsAny(), It.IsAny())) .Returns(document); diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxAsyncTests.cs index 508e637..00f8fba 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxAsyncTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxAsyncTests.cs @@ -12,16 +12,16 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class GetByMaxAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +public class GetByMaxAsyncTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; - private readonly Expression> selector = document => document.SomeValue; + 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(); + var document = Fixture.Create>(); SetupReader(document); @@ -32,7 +32,7 @@ public class GetByMaxAsyncTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetByMaxAsync(filter, selector, null, CancellationToken.None), + x => x.GetByMaxAsync, int>(filter, selector, null, CancellationToken.None), Times.Once); } @@ -40,7 +40,7 @@ public class GetByMaxAsyncTests : TestKeyedReadOnlyMongoRepositoryContext public async Task WithFilterAndSelectorAndCancellationToken_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var token = new CancellationToken(true); SetupReader(document); @@ -52,7 +52,7 @@ public class GetByMaxAsyncTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetByMaxAsync(filter, selector, null, token), + x => x.GetByMaxAsync, int>(filter, selector, null, token), Times.Once); } @@ -60,7 +60,7 @@ public class GetByMaxAsyncTests : TestKeyedReadOnlyMongoRepositoryContext public async Task WithFilterAndSelectorAndPartitionKey_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var partitionKey = Fixture.Create(); SetupReader(document); @@ -72,7 +72,7 @@ public class GetByMaxAsyncTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetByMaxAsync(filter, selector, partitionKey, CancellationToken.None), + x => x.GetByMaxAsync, int>(filter, selector, partitionKey, CancellationToken.None), Times.Once); } @@ -80,7 +80,7 @@ public class GetByMaxAsyncTests : TestKeyedReadOnlyMongoRepositoryContext public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var partitionKey = Fixture.Create(); var token = new CancellationToken(true); @@ -93,18 +93,18 @@ public class GetByMaxAsyncTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetByMaxAsync(filter, selector, partitionKey, token), + x => x.GetByMaxAsync, int>(filter, selector, partitionKey, token), Times.Once); } - private void SetupReader(TestDocument document) + private void SetupReader(TestDocumentWithKey document) { Reader = new Mock(); Reader .Setup( - x => x.GetByMaxAsync( - It.IsAny>>(), - It.IsAny>>(), + 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 index 987bca6..b694cdb 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMaxTests.cs @@ -11,16 +11,16 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class GetByMaxTests : TestKeyedReadOnlyMongoRepositoryContext +public class GetByMaxTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; - private readonly Expression> selector = document => document.SomeValue; + 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(); + var document = Fixture.Create>(); SetupReader(document); @@ -31,7 +31,7 @@ public class GetByMaxTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetByMax(filter, selector, null, CancellationToken.None), + x => x.GetByMax, int>(filter, selector, null, CancellationToken.None), Times.Once); } @@ -39,7 +39,7 @@ public class GetByMaxTests : TestKeyedReadOnlyMongoRepositoryContext public void WithFilterAndSelectorAndCancellationToken_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var token = new CancellationToken(true); SetupReader(document); @@ -51,7 +51,7 @@ public class GetByMaxTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetByMax(filter, selector, null, token), + x => x.GetByMax, int>(filter, selector, null, token), Times.Once); } @@ -59,7 +59,7 @@ public class GetByMaxTests : TestKeyedReadOnlyMongoRepositoryContext public void WithFilterAndSelectorAndPartitionKey_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var partitionKey = Fixture.Create(); SetupReader(document); @@ -71,7 +71,7 @@ public class GetByMaxTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetByMax(filter, selector, partitionKey, CancellationToken.None), + x => x.GetByMax, int>(filter, selector, partitionKey, CancellationToken.None), Times.Once); } @@ -79,7 +79,7 @@ public class GetByMaxTests : TestKeyedReadOnlyMongoRepositoryContext public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var partitionKey = Fixture.Create(); var token = new CancellationToken(true); @@ -92,18 +92,18 @@ public class GetByMaxTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetByMax(filter, selector, partitionKey, token), + x => x.GetByMax, int>(filter, selector, partitionKey, token), Times.Once); } - private void SetupReader(TestDocument document) + private void SetupReader(TestDocumentWithKey document) { Reader = new Mock(); Reader .Setup( - x => x.GetByMax( - It.IsAny>>(), - It.IsAny>>(), + 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 index fc2e3c7..02032b1 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMinAsyncTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMinAsyncTests.cs @@ -12,16 +12,16 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class GetByMinAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +public class GetByMinAsyncTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; - private readonly Expression> selector = document => document.SomeValue; + 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(); + var document = Fixture.Create>(); SetupReader(document); @@ -32,7 +32,7 @@ public class GetByMinAsyncTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetByMinAsync(filter, selector, null, CancellationToken.None), + x => x.GetByMinAsync, int>(filter, selector, null, CancellationToken.None), Times.Once); } @@ -40,7 +40,7 @@ public class GetByMinAsyncTests : TestKeyedReadOnlyMongoRepositoryContext public async Task WithFilterAndSelectorAndCancellationToken_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var token = new CancellationToken(true); SetupReader(document); @@ -52,7 +52,7 @@ public class GetByMinAsyncTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetByMinAsync(filter, selector, null, token), + x => x.GetByMinAsync, int>(filter, selector, null, token), Times.Once); } @@ -60,7 +60,7 @@ public class GetByMinAsyncTests : TestKeyedReadOnlyMongoRepositoryContext public async Task WithFilterAndSelectorAndPartitionKey_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var partitionKey = Fixture.Create(); SetupReader(document); @@ -72,7 +72,7 @@ public class GetByMinAsyncTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetByMinAsync(filter, selector, partitionKey, CancellationToken.None), + x => x.GetByMinAsync, int>(filter, selector, partitionKey, CancellationToken.None), Times.Once); } @@ -80,7 +80,7 @@ public class GetByMinAsyncTests : TestKeyedReadOnlyMongoRepositoryContext public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var partitionKey = Fixture.Create(); var token = new CancellationToken(true); @@ -93,18 +93,18 @@ public class GetByMinAsyncTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetByMinAsync(filter, selector, partitionKey, token), + x => x.GetByMinAsync, int>(filter, selector, partitionKey, token), Times.Once); } - private void SetupReader(TestDocument document) + private void SetupReader(TestDocumentWithKey document) { Reader = new Mock(); Reader .Setup( - x => x.GetByMinAsync( - It.IsAny>>(), - It.IsAny>>(), + 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 index e3eacee..9efd7f2 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMinTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetByMinTests.cs @@ -11,16 +11,16 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class GetByMinTests : TestKeyedReadOnlyMongoRepositoryContext +public class GetByMinTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; - private readonly Expression> selector = document => document.SomeValue; + 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(); + var document = Fixture.Create>(); SetupReader(document); @@ -31,7 +31,7 @@ public class GetByMinTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetByMin(filter, selector, null, CancellationToken.None), + x => x.GetByMin, int>(filter, selector, null, CancellationToken.None), Times.Once); } @@ -39,7 +39,7 @@ public class GetByMinTests : TestKeyedReadOnlyMongoRepositoryContext public void WithFilterAndSelectorAndCancellationToken_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var token = new CancellationToken(true); SetupReader(document); @@ -51,7 +51,7 @@ public class GetByMinTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetByMin(filter, selector, null, token), + x => x.GetByMin, int>(filter, selector, null, token), Times.Once); } @@ -59,7 +59,7 @@ public class GetByMinTests : TestKeyedReadOnlyMongoRepositoryContext public void WithFilterAndSelectorAndPartitionKey_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var partitionKey = Fixture.Create(); SetupReader(document); @@ -71,7 +71,7 @@ public class GetByMinTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetByMin(filter, selector, partitionKey, CancellationToken.None), + x => x.GetByMin, int>(filter, selector, partitionKey, CancellationToken.None), Times.Once); } @@ -79,7 +79,7 @@ public class GetByMinTests : TestKeyedReadOnlyMongoRepositoryContext public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var partitionKey = Fixture.Create(); var token = new CancellationToken(true); @@ -92,18 +92,18 @@ public class GetByMinTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetByMin(filter, selector, partitionKey, token), + x => x.GetByMin, int>(filter, selector, partitionKey, token), Times.Once); } - private void SetupReader(TestDocument document) + private void SetupReader(TestDocumentWithKey document) { Reader = new Mock(); Reader .Setup( - x => x.GetByMin( - It.IsAny>>(), - It.IsAny>>(), + 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 index f9fe0c8..5b16f95 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMaxValueAsyncTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMaxValueAsyncTests.cs @@ -12,10 +12,10 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class GetMaxValueAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +public class GetMaxValueAsyncTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; - private readonly Expression> selector = document => document.SomeValue; + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, int>> selector = document => document.SomeValue; [Fact] public async Task WithFilterAndSelector_GetsMaxValue() @@ -31,7 +31,7 @@ public class GetMaxValueAsyncTests : TestKeyedReadOnlyMongoRepositoryContext x.GetMaxValueAsync(filter, selector, null, CancellationToken.None), + x => x.GetMaxValueAsync, int, int>(filter, selector, null, CancellationToken.None), Times.Once); } @@ -50,7 +50,7 @@ public class GetMaxValueAsyncTests : TestKeyedReadOnlyMongoRepositoryContext x.GetMaxValueAsync(filter, selector, null, token), + x => x.GetMaxValueAsync, int, int>(filter, selector, null, token), Times.Once); } @@ -69,7 +69,7 @@ public class GetMaxValueAsyncTests : TestKeyedReadOnlyMongoRepositoryContext x.GetMaxValueAsync(filter, selector, partitionKey, CancellationToken.None), + x => x.GetMaxValueAsync, int, int>(filter, selector, partitionKey, CancellationToken.None), Times.Once); } @@ -89,7 +89,7 @@ public class GetMaxValueAsyncTests : TestKeyedReadOnlyMongoRepositoryContext x.GetMaxValueAsync(filter, selector, partitionKey, token), + x => x.GetMaxValueAsync, int, int>(filter, selector, partitionKey, token), Times.Once); } @@ -98,9 +98,9 @@ public class GetMaxValueAsyncTests : TestKeyedReadOnlyMongoRepositoryContext(); Reader .Setup( - x => x.GetMaxValueAsync( - It.IsAny>>(), - It.IsAny>>(), + 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 index 06af072..ada314c 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMaxValueTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMaxValueTests.cs @@ -11,10 +11,10 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class GetMaxValueTests : TestKeyedReadOnlyMongoRepositoryContext +public class GetMaxValueTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; - private readonly Expression> selector = document => document.SomeValue; + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, int>> selector = document => document.SomeValue; [Fact] public void WithFilterAndSelector_GetsMaxValue() @@ -30,7 +30,7 @@ public class GetMaxValueTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(value); Reader.Verify( - x => x.GetMaxValue(filter, selector, null, CancellationToken.None), + x => x.GetMaxValue, int, int>(filter, selector, null, CancellationToken.None), Times.Once); } @@ -49,7 +49,7 @@ public class GetMaxValueTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(value); Reader.Verify( - x => x.GetMaxValue(filter, selector, null, token), + x => x.GetMaxValue, int, int>(filter, selector, null, token), Times.Once); } @@ -68,7 +68,7 @@ public class GetMaxValueTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(value); Reader.Verify( - x => x.GetMaxValue(filter, selector, partitionKey, CancellationToken.None), + x => x.GetMaxValue, int, int>(filter, selector, partitionKey, CancellationToken.None), Times.Once); } @@ -88,7 +88,7 @@ public class GetMaxValueTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(value); Reader.Verify( - x => x.GetMaxValue(filter, selector, partitionKey, token), + x => x.GetMaxValue, int, int>(filter, selector, partitionKey, token), Times.Once); } @@ -97,9 +97,9 @@ public class GetMaxValueTests : TestKeyedReadOnlyMongoRepositoryContext Reader = new Mock(); Reader .Setup( - x => x.GetMaxValue( - It.IsAny>>(), - It.IsAny>>(), + 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 index 4e0cc5d..375d672 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMinValueAsyncTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMinValueAsyncTests.cs @@ -12,10 +12,10 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class GetMinValueAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +public class GetMinValueAsyncTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; - private readonly Expression> selector = document => document.SomeValue; + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, int>> selector = document => document.SomeValue; [Fact] public async Task WithFilterAndSelector_GetsMaxValue() @@ -31,7 +31,7 @@ public class GetMinValueAsyncTests : TestKeyedReadOnlyMongoRepositoryContext x.GetMinValueAsync(filter, selector, null, CancellationToken.None), + x => x.GetMinValueAsync, int, int>(filter, selector, null, CancellationToken.None), Times.Once); } @@ -50,7 +50,7 @@ public class GetMinValueAsyncTests : TestKeyedReadOnlyMongoRepositoryContext x.GetMinValueAsync(filter, selector, null, token), + x => x.GetMinValueAsync, int, int>(filter, selector, null, token), Times.Once); } @@ -69,7 +69,7 @@ public class GetMinValueAsyncTests : TestKeyedReadOnlyMongoRepositoryContext x.GetMinValueAsync(filter, selector, partitionKey, CancellationToken.None), + x => x.GetMinValueAsync, int, int>(filter, selector, partitionKey, CancellationToken.None), Times.Once); } @@ -89,7 +89,7 @@ public class GetMinValueAsyncTests : TestKeyedReadOnlyMongoRepositoryContext x.GetMinValueAsync(filter, selector, partitionKey, token), + x => x.GetMinValueAsync, int, int>(filter, selector, partitionKey, token), Times.Once); } @@ -98,9 +98,9 @@ public class GetMinValueAsyncTests : TestKeyedReadOnlyMongoRepositoryContext(); Reader .Setup( - x => x.GetMinValueAsync( - It.IsAny>>(), - It.IsAny>>(), + 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 index 5b861a6..05a6c18 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMinValueTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetMinValueTests.cs @@ -11,10 +11,10 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class GetMinValueTests : TestKeyedReadOnlyMongoRepositoryContext +public class GetMinValueTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; - private readonly Expression> selector = document => document.SomeValue; + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, int>> selector = document => document.SomeValue; [Fact] public void WithFilterAndSelector_GetsMinValue() @@ -30,7 +30,7 @@ public class GetMinValueTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(value); Reader.Verify( - x => x.GetMinValue(filter, selector, null, CancellationToken.None), + x => x.GetMinValue, int, int>(filter, selector, null, CancellationToken.None), Times.Once); } @@ -49,7 +49,7 @@ public class GetMinValueTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(value); Reader.Verify( - x => x.GetMinValue(filter, selector, null, token), + x => x.GetMinValue, int, int>(filter, selector, null, token), Times.Once); } @@ -68,7 +68,7 @@ public class GetMinValueTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(value); Reader.Verify( - x => x.GetMinValue(filter, selector, partitionKey, CancellationToken.None), + x => x.GetMinValue, int, int>(filter, selector, partitionKey, CancellationToken.None), Times.Once); } @@ -88,7 +88,7 @@ public class GetMinValueTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(value); Reader.Verify( - x => x.GetMinValue(filter, selector, partitionKey, token), + x => x.GetMinValue, int, int>(filter, selector, partitionKey, token), Times.Once); } @@ -97,9 +97,9 @@ public class GetMinValueTests : TestKeyedReadOnlyMongoRepositoryContext Reader = new Mock(); Reader .Setup( - x => x.GetMinValue( - It.IsAny>>(), - It.IsAny>>(), + 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 index 9ae88f4..01efd4c 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs @@ -12,15 +12,15 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; [Fact] public async Task WithFilter_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); SetupReader(document); @@ -31,7 +31,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetOneAsync(filter, null, CancellationToken.None), + x => x.GetOneAsync, int>(filter, null, CancellationToken.None), Times.Once); } @@ -39,7 +39,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext public async Task WithFilterAndCancellationToken_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var token = new CancellationToken(true); SetupReader(document); @@ -51,7 +51,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetOneAsync(filter, null, token), + x => x.GetOneAsync, int>(filter, null, token), Times.Once); } @@ -59,7 +59,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext public async Task WithFilterAndPartitionKey_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var partitionKey = Fixture.Create(); SetupReader(document); @@ -71,7 +71,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetOneAsync(filter, partitionKey, CancellationToken.None), + x => x.GetOneAsync, int>(filter, partitionKey, CancellationToken.None), Times.Once); } @@ -79,7 +79,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext public async Task WithFilterAndPartitionKeyAndCancellationToken_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var partitionKey = Fixture.Create(); var token = new CancellationToken(true); @@ -92,17 +92,17 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetOneAsync(filter, partitionKey, token), + x => x.GetOneAsync, int>(filter, partitionKey, token), Times.Once); } - private void SetupReader(TestDocument document) + private void SetupReader(TestDocumentWithKey document) { Reader = new Mock(); Reader .Setup( - x => x.GetOneAsync( - It.IsAny>>(), + 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 index 289f1d0..803e999 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneTests.cs @@ -1,7 +1,6 @@ using System; using System.Linq.Expressions; using System.Threading; -using System.Threading.Tasks; using AutoFixture; using CoreUnitTests.Infrastructure; using CoreUnitTests.Infrastructure.Model; @@ -12,15 +11,15 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class GetOneTests : TestKeyedReadOnlyMongoRepositoryContext +public class GetOneTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression, bool>> filter = document => document.SomeContent == "SomeContent"; [Fact] - public void WithId_GetsOne() + public void WithFilter_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); SetupReader(document); @@ -31,15 +30,15 @@ public class GetOneTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetOne(filter, null, CancellationToken.None), + x => x.GetOne, int>(filter, null, CancellationToken.None), Times.Once); } [Fact] - public void WithIdAndCancellationToken_GetsOne() + public void WithFilterAndCancellationToken_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var token = new CancellationToken(true); SetupReader(document); @@ -51,15 +50,15 @@ public class GetOneTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetOne(filter, null, token), + x => x.GetOne, int>(filter, null, token), Times.Once); } [Fact] - public void WithIdAndPartitionKey_GetsOne() + public void WithFilterAndPartitionKey_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var partitionKey = Fixture.Create(); SetupReader(document); @@ -71,15 +70,15 @@ public class GetOneTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetOne(filter, partitionKey, CancellationToken.None), + x => x.GetOne, int>(filter, partitionKey, CancellationToken.None), Times.Once); } [Fact] - public void WithIdAndPartitionKeyAndCancellationToken_GetsOne() + public void WithFilterAndPartitionKeyAndCancellationToken_GetsOne() { // Arrange - var document = Fixture.Create(); + var document = Fixture.Create>(); var partitionKey = Fixture.Create(); var token = new CancellationToken(true); @@ -92,17 +91,17 @@ public class GetOneTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(document); Reader.Verify( - x => x.GetOne(filter, partitionKey, token), + x => x.GetOne, int>(filter, partitionKey, token), Times.Once); } - private void SetupReader(TestDocument document) + private void SetupReader(TestDocumentWithKey document) { Reader = new Mock(); Reader .Setup( - x => x.GetOne( - It.IsAny>>(), + 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 index 1b199c8..189b5b1 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetSortedPaginatedAsyncTests.cs @@ -15,11 +15,11 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class GetSortedPaginatedAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +public class GetSortedPaginatedAsyncTests : TestKeyedReadOnlyMongoRepositoryContext { - 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 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; @@ -178,15 +178,15 @@ public class GetSortedPaginatedAsyncTests : TestKeyedReadOnlyMongoRepositoryCont VerifyDefinition(result, documents, DefaultSkipNumber, DefaultTakeNumber, null, token); } - private List SetupReaderWithSortSelector() + private List> SetupReaderWithSortSelector() { - var documents = Fixture.CreateMany().ToList(); + var documents = Fixture.CreateMany>().ToList(); Reader = new Mock(); Reader.Setup( - x => x.GetSortedPaginatedAsync( - It.IsAny>>(), - It.IsAny>>(), + x => x.GetSortedPaginatedAsync, int>( + It.IsAny, bool>>>(), + It.IsAny, object>>>(), It.IsAny(), It.IsAny(), It.IsAny(), @@ -196,12 +196,12 @@ public class GetSortedPaginatedAsyncTests : TestKeyedReadOnlyMongoRepositoryCont return documents; } - private void VerifySelector(List result, List documents, bool ascending, int skipNumber, int takeNumber, string partitionKey, CancellationToken cancellationToken) + 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( + x => x.GetSortedPaginatedAsync, int>( filter, selector, ascending, @@ -212,15 +212,15 @@ public class GetSortedPaginatedAsyncTests : TestKeyedReadOnlyMongoRepositoryCont Times.Once); } - private List SetupReaderWithSortDefinition() + private List> SetupReaderWithSortDefinition() { - var documents = Fixture.CreateMany().ToList(); + var documents = Fixture.CreateMany>().ToList(); Reader = new Mock(); Reader.Setup( - x => x.GetSortedPaginatedAsync( - It.IsAny>>(), - It.IsAny>(), + x => x.GetSortedPaginatedAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), It.IsAny(), It.IsAny(), It.IsAny(), @@ -229,12 +229,12 @@ public class GetSortedPaginatedAsyncTests : TestKeyedReadOnlyMongoRepositoryCont return documents; } - private void VerifyDefinition(List result, List documents, int skipNumber, int takeNumber, string partitionKey, CancellationToken cancellationToken) + 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( + x => x.GetSortedPaginatedAsync, int>( filter, sortDefinition, skipNumber, diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GroupByTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GroupByTests.cs index 018c4bc..4c39155 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GroupByTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GroupByTests.cs @@ -12,11 +12,11 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext +public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext { - 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; + 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() @@ -26,9 +26,9 @@ public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext Reader = new Mock(); Reader.Setup( - x => x.GroupBy( - It.IsAny>>(), - It.IsAny, TestProjection>>>(), + x => x.GroupBy, int, TestProjection, int>( + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(projections); @@ -40,7 +40,7 @@ public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(projections); Reader.Verify( - x => x.GroupBy(grouping, projection, null, CancellationToken.None), + x => x.GroupBy, int, TestProjection, int>(grouping, projection, null, CancellationToken.None), Times.Once); } @@ -54,9 +54,9 @@ public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext Reader .Setup( - x => x.GroupBy( - It.IsAny>>(), - It.IsAny, TestProjection>>>(), + x => x.GroupBy, int, TestProjection, int>( + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(projections); @@ -68,7 +68,7 @@ public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(projections); Reader.Verify( - x => x.GroupBy(grouping, projection, null, token), + x => x.GroupBy, int, TestProjection, int>(grouping, projection, null, token), Times.Once); } @@ -82,9 +82,9 @@ public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext Reader .Setup( - x => x.GroupBy( - It.IsAny>>(), - It.IsAny, TestProjection>>>(), + x => x.GroupBy, int, TestProjection, int>( + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(projections); @@ -96,7 +96,7 @@ public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(projections); Reader.Verify( - x => x.GroupBy(grouping, projection, partitionKey, CancellationToken.None), + x => x.GroupBy, int, TestProjection, int>(grouping, projection, partitionKey, CancellationToken.None), Times.Once); } @@ -111,9 +111,9 @@ public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext Reader .Setup( - x => x.GroupBy( - It.IsAny>>(), - It.IsAny, TestProjection>>>(), + x => x.GroupBy, int, TestProjection, int>( + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(projections); @@ -125,7 +125,7 @@ public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(projections); Reader.Verify( - x => x.GroupBy(grouping, projection, partitionKey, token), + x => x.GroupBy, int, TestProjection, int>(grouping, projection, partitionKey, token), Times.Once); } @@ -137,10 +137,10 @@ public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext Reader = new Mock(); Reader.Setup( - x => x.GroupBy( - It.IsAny>>(), - It.IsAny>>(), - It.IsAny, TestProjection>>>(), + x => x.GroupBy, int, TestProjection, int>( + It.IsAny,bool>>>(), + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(projections); @@ -152,7 +152,7 @@ public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(projections); Reader.Verify( - x => x.GroupBy(filter, grouping, projection, null, CancellationToken.None), + x => x.GroupBy, int, TestProjection, int>(filter, grouping, projection, null, CancellationToken.None), Times.Once); } @@ -165,10 +165,10 @@ public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext Reader = new Mock(); Reader.Setup( - x => x.GroupBy( - It.IsAny>>(), - It.IsAny>>(), - It.IsAny, TestProjection>>>(), + x => x.GroupBy, int, TestProjection, int>( + It.IsAny,bool>>>(), + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(projections); @@ -180,7 +180,7 @@ public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(projections); Reader.Verify( - x => x.GroupBy(filter, grouping, projection, null, token), + x => x.GroupBy, int, TestProjection, int>(filter, grouping, projection, null, token), Times.Once); } @@ -193,10 +193,10 @@ public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext Reader = new Mock(); Reader.Setup( - x => x.GroupBy( - It.IsAny>>(), - It.IsAny>>(), - It.IsAny, TestProjection>>>(), + x => x.GroupBy, int, TestProjection, int>( + It.IsAny,bool>>>(), + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(projections); @@ -208,7 +208,7 @@ public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(projections); Reader.Verify( - x => x.GroupBy(filter, grouping, projection, partitionKey, CancellationToken.None), + x => x.GroupBy, int, TestProjection, int>(filter, grouping, projection, partitionKey, CancellationToken.None), Times.Once); } @@ -222,10 +222,10 @@ public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext Reader = new Mock(); Reader.Setup( - x => x.GroupBy( - It.IsAny>>(), - It.IsAny>>(), - It.IsAny, TestProjection>>>(), + x => x.GroupBy, int, TestProjection, int>( + It.IsAny,bool>>>(), + It.IsAny, int>>>(), + It.IsAny>, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(projections); @@ -237,7 +237,7 @@ public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext result.Should().NotBeNull(); result.Should().BeEquivalentTo(projections); Reader.Verify( - x => x.GroupBy(filter, grouping, projection, partitionKey, token), + 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 index 53535b4..1b2eca6 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyAsyncTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyAsyncTests.cs @@ -14,10 +14,10 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class ProjectManyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +public class ProjectManyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; - private readonly Expression> projection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + 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() @@ -33,7 +33,7 @@ public class ProjectManyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext projections.Contains(x)); Reader.Verify( - x => x.ProjectManyAsync( + x => x.ProjectManyAsync, TestProjection, int>( filter, projection, null, @@ -56,7 +56,7 @@ public class ProjectManyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext projections.Contains(x)); Reader.Verify( - x => x.ProjectManyAsync(filter, projection, null, token), + x => x.ProjectManyAsync, TestProjection, int>(filter, projection, null, token), Times.Once); } @@ -75,7 +75,7 @@ public class ProjectManyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext projections.Contains(x)); Reader.Verify( - x => x.ProjectManyAsync(filter, projection, partitionKey, CancellationToken.None), + x => x.ProjectManyAsync, TestProjection, int>(filter, projection, partitionKey, CancellationToken.None), Times.Once); } @@ -95,7 +95,7 @@ public class ProjectManyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext projections.Contains(x)); Reader.Verify( - x => x.ProjectManyAsync(filter, projection, partitionKey, token), + x => x.ProjectManyAsync, TestProjection, int>(filter, projection, partitionKey, token), Times.Once); } @@ -104,9 +104,9 @@ public class ProjectManyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext(); Reader .Setup( - x => x.ProjectManyAsync( - It.IsAny>>(), - It.IsAny>>(), + 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 index 47b9a09..324090e 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyTests.cs @@ -13,10 +13,10 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class ProjectManyTests : TestKeyedReadOnlyMongoRepositoryContext +public class ProjectManyTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; - private readonly Expression> projection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + 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() @@ -32,7 +32,7 @@ public class ProjectManyTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().OnlyContain(x => projections.Contains(x)); Reader.Verify( - x => x.ProjectMany( + x => x.ProjectMany, TestProjection, int>( filter, projection, null, @@ -55,7 +55,7 @@ public class ProjectManyTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().OnlyContain(x => projections.Contains(x)); Reader.Verify( - x => x.ProjectMany(filter, projection, null, token), + x => x.ProjectMany, TestProjection, int>(filter, projection, null, token), Times.Once); } @@ -74,7 +74,7 @@ public class ProjectManyTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().OnlyContain(x => projections.Contains(x)); Reader.Verify( - x => x.ProjectMany(filter, projection, partitionKey, CancellationToken.None), + x => x.ProjectMany, TestProjection, int>(filter, projection, partitionKey, CancellationToken.None), Times.Once); } @@ -94,7 +94,7 @@ public class ProjectManyTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().OnlyContain(x => projections.Contains(x)); Reader.Verify( - x => x.ProjectMany(filter, projection, partitionKey, token), + x => x.ProjectMany, TestProjection, int>(filter, projection, partitionKey, token), Times.Once); } @@ -103,9 +103,9 @@ public class ProjectManyTests : TestKeyedReadOnlyMongoRepositoryContext Reader = new Mock(); Reader .Setup( - x => x.ProjectMany( - It.IsAny>>(), - It.IsAny>>(), + 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 index e027aa3..4d160f9 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneAsyncTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneAsyncTests.cs @@ -12,10 +12,10 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class ProjectOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +public class ProjectOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; - private readonly Expression> projection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + 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() @@ -31,7 +31,7 @@ public class ProjectOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext x.ProjectOneAsync( + x => x.ProjectOneAsync, TestProjection, int>( filter, projection, null, @@ -54,7 +54,7 @@ public class ProjectOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext x.ProjectOneAsync(filter, projection, null, token), + x => x.ProjectOneAsync, TestProjection, int>(filter, projection, null, token), Times.Once); } @@ -73,7 +73,7 @@ public class ProjectOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext x.ProjectOneAsync(filter, projection, partitionKey, CancellationToken.None), + x => x.ProjectOneAsync, TestProjection, int>(filter, projection, partitionKey, CancellationToken.None), Times.Once); } @@ -93,7 +93,7 @@ public class ProjectOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext x.ProjectOneAsync(filter, projection, partitionKey, token), + x => x.ProjectOneAsync, TestProjection, int>(filter, projection, partitionKey, token), Times.Once); } @@ -102,9 +102,9 @@ public class ProjectOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext(); Reader .Setup( - x => x.ProjectOneAsync( - It.IsAny>>(), - It.IsAny>>(), + 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 index 280fcd0..d7b5eb3 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneTests.cs @@ -11,10 +11,10 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class ProjectOneTests : TestKeyedReadOnlyMongoRepositoryContext +public class ProjectOneTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; - private readonly Expression> projection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + 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() @@ -30,7 +30,7 @@ public class ProjectOneTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(expected); Reader.Verify( - x => x.ProjectOne( + x => x.ProjectOne, TestProjection, int>( filter, projection, null, @@ -53,7 +53,7 @@ public class ProjectOneTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(expected); Reader.Verify( - x => x.ProjectOne(filter, projection, null, token), + x => x.ProjectOne, TestProjection, int>(filter, projection, null, token), Times.Once); } @@ -72,7 +72,7 @@ public class ProjectOneTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(expected); Reader.Verify( - x => x.ProjectOne(filter, projection, partitionKey, CancellationToken.None), + x => x.ProjectOne, TestProjection, int>(filter, projection, partitionKey, CancellationToken.None), Times.Once); } @@ -92,7 +92,7 @@ public class ProjectOneTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(expected); Reader.Verify( - x => x.ProjectOne(filter, projection, partitionKey, token), + x => x.ProjectOne, TestProjection, int>(filter, projection, partitionKey, token), Times.Once); } @@ -101,9 +101,9 @@ public class ProjectOneTests : TestKeyedReadOnlyMongoRepositoryContext Reader = new Mock(); Reader .Setup( - x => x.ProjectOne( - It.IsAny>>(), - It.IsAny>>(), + 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 index 9652042..43f613f 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/SumByAsyncTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/SumByAsyncTests.cs @@ -12,11 +12,11 @@ using Xunit; namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; -public class SumByAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +public class SumByAsyncTests : TestKeyedReadOnlyMongoRepositoryContext { - private readonly Expression> filter = document => document.SomeContent == "SomeContent"; - private readonly Expression> intSelector = document => document.SomeValue; - private readonly Expression> decimalSelector = document => document.SomeDecimalValue; + 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() @@ -32,7 +32,7 @@ public class SumByAsyncTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(expected); Reader.Verify( - x => x.SumByAsync(filter, intSelector, null, CancellationToken.None), + x => x.SumByAsync, int>(filter, intSelector, null, CancellationToken.None), Times.Once); } @@ -51,7 +51,7 @@ public class SumByAsyncTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(expected); Reader.Verify( - x => x.SumByAsync(filter, intSelector, null, token), + x => x.SumByAsync, int>(filter, intSelector, null, token), Times.Once); } @@ -70,7 +70,7 @@ public class SumByAsyncTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(expected); Reader.Verify( - x => x.SumByAsync(filter, intSelector, partitionKey, CancellationToken.None), + x => x.SumByAsync, int>(filter, intSelector, partitionKey, CancellationToken.None), Times.Once); } @@ -90,7 +90,7 @@ public class SumByAsyncTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(expected); Reader.Verify( - x => x.SumByAsync(filter, intSelector, partitionKey, token), + x => x.SumByAsync, int>(filter, intSelector, partitionKey, token), Times.Once); } @@ -108,7 +108,7 @@ public class SumByAsyncTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(expected); Reader.Verify( - x => x.SumByAsync(filter, decimalSelector, null, CancellationToken.None), + x => x.SumByAsync, int>(filter, decimalSelector, null, CancellationToken.None), Times.Once); } @@ -127,7 +127,7 @@ public class SumByAsyncTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(expected); Reader.Verify( - x => x.SumByAsync(filter, decimalSelector, null, token), + x => x.SumByAsync, int>(filter, decimalSelector, null, token), Times.Once); } @@ -146,7 +146,7 @@ public class SumByAsyncTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(expected); Reader.Verify( - x => x.SumByAsync(filter, decimalSelector, partitionKey, CancellationToken.None), + x => x.SumByAsync, int>(filter, decimalSelector, partitionKey, CancellationToken.None), Times.Once); } @@ -166,7 +166,7 @@ public class SumByAsyncTests : TestKeyedReadOnlyMongoRepositoryContext // Assert result.Should().Be(expected); Reader.Verify( - x => x.SumByAsync(filter, decimalSelector, partitionKey, token), + x => x.SumByAsync, int>(filter, decimalSelector, partitionKey, token), Times.Once); } @@ -175,9 +175,9 @@ public class SumByAsyncTests : TestKeyedReadOnlyMongoRepositoryContext Reader = new Mock(); Reader .Setup( - x => x.SumByAsync( - It.IsAny>>(), - It.IsAny>>(), + x => x.SumByAsync, int>( + It.IsAny, bool>>>(), + It.IsAny,int>>>(), It.IsAny(), It.IsAny())) .ReturnsAsync(expected); @@ -188,9 +188,9 @@ public class SumByAsyncTests : TestKeyedReadOnlyMongoRepositoryContext Reader = new Mock(); Reader .Setup( - x => x.SumByAsync( - It.IsAny>>(), - It.IsAny>>(), + 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/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs b/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs index 185ef77..be4bbee 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; +using CancellationToken = System.Threading.CancellationToken; 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,271 @@ 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 +760,271 @@ 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 +1032,187 @@ 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 +1221,47 @@ 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. @@ -262,55 +1269,193 @@ namespace MongoDbGenericRepository /// 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) + Task GetByMaxAsync(Expression> filter, Expression> maxValueSelector, 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, - Expression> minValueSelector, - string partitionKey = null, - CancellationToken cancellationToken = default) + Task GetByMinAsync(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. + /// 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 +1463,51 @@ 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) + Task GetMaxValueAsync(Expression> filter, Expression> maxValueSelector, 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 +1515,51 @@ 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) + Task GetMinValueAsync(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. + /// 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 +1567,73 @@ 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 +1642,125 @@ 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,7 +1769,34 @@ 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 cancellation Token. + Task ProjectOneAsync(Expression> filter, Expression> projection, CancellationToken cancellationToken) + 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. @@ -446,32 +1804,13 @@ namespace MongoDbGenericRepository /// 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) + Task ProjectOneAsync(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. - 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 +1818,41 @@ namespace MongoDbGenericRepository /// 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) + /// An optional cancellation token. + Task ProjectOneAsync(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. + /// 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 +1860,134 @@ 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 +1997,39 @@ 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. @@ -518,17 +2038,64 @@ namespace MongoDbGenericRepository /// The grouping criteria. /// The projected group result. /// The partition key of your document, if any. - List GroupBy( - Expression> groupingCriteria, - Expression, TProjection>> groupProjection, - string partitionKey = null) + List GroupBy(Expression> groupingCriteria, Expression, TProjection>> groupProjection, 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. @@ -538,21 +2105,35 @@ namespace MongoDbGenericRepository /// The grouping criteria. /// The projected group result. /// The partition key of your document, if any. - List GroupBy( - Expression> filter, - Expression> groupingCriteria, - Expression, TProjection>> groupProjection, - string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - where TProjection : class, new(); + List GroupBy(Expression> filter, Expression> groupingCriteria, Expression, TProjection>> groupProjection, 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 +2143,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 +2178,4 @@ namespace MongoDbGenericRepository #endregion Pagination } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/ReadOnlyMongoRepository.cs b/MongoDbGenericRepository/ReadOnlyMongoRepository.cs index 7d8325a..54913fc 100644 --- a/MongoDbGenericRepository/ReadOnlyMongoRepository.cs +++ b/MongoDbGenericRepository/ReadOnlyMongoRepository.cs @@ -1,22 +1,22 @@ -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 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. @@ -25,717 +25,24 @@ namespace MongoDbGenericRepository } /// - /// The constructor taking a . + /// The constructor taking a . /// - /// A mongodb context implementing + /// A mongodb context implementing protected ReadOnlyMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } /// - /// The constructor 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 virtual async 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 virtual async 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 virtual async 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 virtual async 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 virtual async 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 virtual async 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 virtual async 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 virtual async 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. @@ -749,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. @@ -763,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. @@ -779,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. @@ -791,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 From be58460bf1ca919e50c963803dcb168c1b3adc96 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Wed, 5 Jul 2023 20:20:08 +0100 Subject: [PATCH 31/40] tokens for updater and unit tests --- CoreUnitTests/CoreUnitTests.csproj | 4 + .../UpdateManyAsyncTests.cs | 582 +++++++++++++ .../MongoDbUpdaterTests/UpdateManyTests.cs | 581 +++++++++++++ .../UpdateOneAsyncTests.cs | 806 ++++++++++++++++++ .../MongoDbUpdaterTests/UpdateOneTests.cs | 805 +++++++++++++++++ .../UpdateDefinitionExtensions.cs | 21 + .../DataAccess/Update/IMongoDbUpdater.cs | 75 +- .../Update/MongoDbUpdater.ClientSession.cs | 22 +- .../DataAccess/Update/MongoDbUpdater.cs | 180 ++-- 9 files changed, 2978 insertions(+), 98 deletions(-) create mode 100644 CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateManyAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateManyTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateOneAsyncTests.cs create mode 100644 CoreUnitTests/DataAccessTests/MongoDbUpdaterTests/UpdateOneTests.cs create mode 100644 CoreUnitTests/Infrastructure/UpdateDefinitionExtensions.cs diff --git a/CoreUnitTests/CoreUnitTests.csproj b/CoreUnitTests/CoreUnitTests.csproj index d417073..58aa65b 100644 --- a/CoreUnitTests/CoreUnitTests.csproj +++ b/CoreUnitTests/CoreUnitTests.csproj @@ -30,4 +30,8 @@ + + + + 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/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/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs b/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs index abc8124..1d96fca 100644 --- a/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs +++ b/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs @@ -19,7 +19,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The type representing a Document. /// The type of the primary key for a Document. /// The document with the modifications you want to persist. - Task UpdateOneAsync(TDocument modifiedDocument) + /// An optional cancellation token. + Task UpdateOneAsync(TDocument modifiedDocument, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -30,7 +31,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The type of the primary key for a Document. /// The document you want to modify. /// The update definition for the document. - Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update) + /// An optional cancellation token. + Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -43,7 +45,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The document you want to modify. /// The field selector. /// The new value of the property field. - Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value) + /// An optional cancellation token. + Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -57,7 +60,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + /// An optional cancellation token. + Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -71,7 +75,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + /// An optional cancellation token. + Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -84,7 +89,7 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The document with the modifications you want to persist. /// The optional cancellation token. /// - Task UpdateOneAsync(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) + Task UpdateOneAsync(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -98,7 +103,7 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The update definition. /// The optional cancellation token. /// - Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) + Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -108,13 +113,13 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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. /// The optional cancellation token. /// - 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 = default) where TDocument : IDocument where TKey : IEquatable; @@ -131,7 +136,7 @@ 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 = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -148,7 +153,7 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The optional partition key. /// The optional cancellation token. /// - 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 = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -158,7 +163,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The type representing a Document. /// The type of the primary key for a Document. /// The document with the modifications you want to persist. - bool UpdateOne(TDocument modifiedDocument) + /// The optional cancellation token. + bool UpdateOne(TDocument modifiedDocument, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -169,7 +175,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The type of the primary key for a Document. /// The document you want to modify. /// The update definition for the document. - bool UpdateOne(TDocument documentToModify, UpdateDefinition update) + /// The optional cancellation token. + bool UpdateOne(TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -182,7 +189,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The document you want to modify. /// The field selector. /// The new value of the property field. - bool UpdateOne(TDocument documentToModify, Expression> field, TField value) + /// The optional cancellation token. + bool UpdateOne(TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -196,7 +204,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + /// The optional cancellation token. + bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -210,7 +219,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + /// The optional cancellation token. + bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -223,7 +233,7 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The document with the modifications you want to persist. /// The optional cancellation token. /// - bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) + bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -237,7 +247,7 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The update definition. /// The optional cancellation token. /// - bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) + bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -253,7 +263,7 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The value of the field. /// The optional cancellation token. /// - 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 = default) where TDocument : IDocument where TKey : IEquatable; @@ -270,7 +280,7 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The optional partition key. /// The optional cancellation token. /// - 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 = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -287,7 +297,7 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The optional partition key. /// The optional cancellation token. /// - 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 = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -301,7 +311,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + /// The optional cancellation token. + Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -315,7 +326,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + /// The optional cancellation token. + Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -325,9 +337,10 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The type representing a Document. /// The type of the primary key for a Document. /// The document filter. - /// the update definiton + /// the update definition /// The value of the partition key. - Task UpdateManyAsync(Expression> filter, UpdateDefinition update, string partitionKey = null) + /// The optional cancellation token. + Task UpdateManyAsync(Expression> filter, UpdateDefinition update, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -339,7 +352,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The document filter. /// The update definition. /// The value of the partition key. - Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + /// The optional cancellation token. + Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -353,7 +367,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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 optional cancellation token. + long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -367,7 +382,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + /// The optional cancellation token. + long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -379,7 +395,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The document filter. /// The update definition. /// The value of the partition key. - long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + /// The optional cancellation token. + long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; } diff --git a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs index edc0c5c..8726c6f 100644 --- a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs +++ b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs @@ -10,7 +10,7 @@ namespace MongoDbGenericRepository.DataAccess.Update public partial class MongoDbUpdater { /// - 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 { @@ -22,7 +22,7 @@ namespace MongoDbGenericRepository.DataAccess.Update } /// - 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 { @@ -32,7 +32,7 @@ namespace MongoDbGenericRepository.DataAccess.Update } /// - 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 { @@ -42,7 +42,7 @@ namespace MongoDbGenericRepository.DataAccess.Update } /// - 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 { @@ -52,7 +52,7 @@ namespace MongoDbGenericRepository.DataAccess.Update } /// - 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 { @@ -65,7 +65,7 @@ namespace MongoDbGenericRepository.DataAccess.Update } /// - 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 { @@ -75,7 +75,7 @@ namespace MongoDbGenericRepository.DataAccess.Update } /// - 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 { @@ -85,7 +85,7 @@ namespace MongoDbGenericRepository.DataAccess.Update } /// - 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 { @@ -93,7 +93,7 @@ namespace MongoDbGenericRepository.DataAccess.Update } /// - 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 { @@ -103,11 +103,11 @@ namespace MongoDbGenericRepository.DataAccess.Update } /// - 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 2d59aba..b5ffa7b 100644 --- a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs +++ b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs @@ -1,183 +1,247 @@ -using MongoDB.Driver; +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using MongoDB.Driver; using MongoDbGenericRepository.DataAccess.Base; using MongoDbGenericRepository.Models; -using System; -using System.Linq.Expressions; -using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Update { /// - /// The MongoDb updater. + /// The MongoDb updater. /// public partial class MongoDbUpdater : DataAccessBase, IMongoDbUpdater { /// - /// Constructor + /// Constructor /// /// public MongoDbUpdater(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } - /// - 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; } - /// - 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; } - /// - 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; } - /// - 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; } - /// - 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; } - /// - 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; } - /// - 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; } - /// - 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); } - /// - 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; } - /// - 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); } - /// - 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); } - /// - 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; } - /// - 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); } - /// - 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; } - /// - 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); } - /// - 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; } - /// - 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; } } -} +} \ No newline at end of file From c70727212ee41a69a484ad55ee86691bdb34cc68 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Thu, 6 Jul 2023 11:49:45 +0100 Subject: [PATCH 32/40] cancellation tokens on repository update methods --- ...aseMongoRepository_Update_ClientSession.cs | 293 ++++- ...aseMongoRepository.Update.ClientSession.cs | 411 +++++-- .../BaseMongoRepository.Update.cs | 1092 +++++++++++------ .../BaseMongoRepository.TKey.Update.cs | 520 +++++--- .../IBaseMongoRepository.TKey.Update.cs | 757 ++++++++++-- 5 files changed, 2332 insertions(+), 741 deletions(-) diff --git a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs index 9e9223b..60dc065 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs @@ -12,6 +12,38 @@ namespace MongoDbGenericRepository.DataAccess.Update /// public interface IBaseMongoRepository_Update_ClientSession { + + /// + /// 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. /// @@ -23,9 +55,8 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The field to update. /// The value of the field. /// 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) where TDocument : IDocument where TKey : IEquatable; @@ -42,7 +73,86 @@ 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, Expression> filter, Expression> field, TField value, string partitionKey, 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. + /// 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. + /// 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. + bool UpdateOne(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey, 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 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; @@ -58,7 +168,19 @@ 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. + /// + /// 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; @@ -71,7 +193,20 @@ 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. + /// + /// 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; @@ -85,7 +220,54 @@ 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. + /// + /// 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; @@ -102,7 +284,54 @@ 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. + /// + /// 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; @@ -119,7 +348,7 @@ 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; @@ -129,13 +358,40 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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. + /// + /// 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; @@ -148,7 +404,20 @@ 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. + /// + /// 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; @@ -162,7 +431,7 @@ 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/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 6e96164..3d4ccb7 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Update.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Update.cs @@ -3,6 +3,7 @@ using MongoDbGenericRepository.DataAccess.Update; using MongoDbGenericRepository.Models; using System; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository @@ -39,538 +40,911 @@ namespace MongoDbGenericRepository #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 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 bool UpdateOne(TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbUpdater.UpdateOne(documentToModify, field, value, cancellationToken); + } + + /// public virtual async Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbUpdater.UpdateOneAsync(documentToModify, field, value); + return await UpdateOneAsync(documentToModify, field, value, CancellationToken.None); } - /// - /// 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, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - 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 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(FilterDefinition filter, Expression> field, TField value) where TDocument : IDocument where TKey : IEquatable { - 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 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, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - 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 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, string partitionKey) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbUpdater.UpdateOneAsync(Builders.Filter.Where(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 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 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, 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(Expression> filter, Expression> field, TField value) where TDocument : IDocument where TKey : IEquatable { - 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 where TKey : IEquatable { - 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 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, string partitionKey) where TDocument : IDocument where TKey : IEquatable { - 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 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, 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, 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 async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value) where TDocument : IDocument where TKey : IEquatable { - 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 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, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - 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 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, string partitionKey) where TDocument : IDocument where TKey : IEquatable { - 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 + 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/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs index 53015c1..3d66778 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs @@ -3,6 +3,7 @@ using MongoDbGenericRepository.DataAccess.Update; using MongoDbGenericRepository.Models; using System; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository @@ -34,247 +35,424 @@ namespace MongoDbGenericRepository 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.Update.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Update.cs index 45a9ebd..b7c4220 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Update.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Update.cs @@ -1,5 +1,6 @@ using System; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using MongoDB.Driver; using MongoDbGenericRepository.Models; @@ -7,27 +8,48 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The base Mongo Repository Update interface. used to update documents in the collections. + /// The base Mongo Repository Update interface. used to update documents in the collections. /// /// - public interface IBaseMongoRepository_Update where TKey : IEquatable + public interface IBaseMongoRepository_Update + where TKey : IEquatable { /// - /// Asynchronously Updates a document. + /// 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; + Task UpdateOneAsync(TDocument modifiedDocument) + where TDocument : IDocument; /// - /// Updates a document. + /// Asynchronously Updates a document. /// /// The type representing a Document. /// The document with the modifications you want to persist. - bool UpdateOne(TDocument modifiedDocument) where TDocument : IDocument; + /// The cancellation token. + Task UpdateOneAsync(TDocument modifiedDocument, CancellationToken cancellationToken) + where TDocument : IDocument; /// - /// Takes a document you want to modify and applies the update you have defined in MongoDb. + /// 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. @@ -36,7 +58,17 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Takes a document you want to modify and applies the update you have defined in MongoDb. + /// 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. @@ -45,7 +77,17 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Updates the property field with the given value update a property field in entities. + /// 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. @@ -56,7 +98,23 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Updates the property field with the given value update a property field in entities. + /// 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. @@ -67,7 +125,50 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Updates the property field with the given value update a property field in entities. + /// 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. @@ -75,11 +176,63 @@ 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) + bool UpdateOne( + FilterDefinition 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. + /// 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. @@ -87,23 +240,15 @@ 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; /// - /// 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. + /// 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. @@ -111,11 +256,105 @@ 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) + /// The cancellation token. + bool UpdateOne( + 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. + /// 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. @@ -123,43 +362,15 @@ 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) + Task UpdateOneAsync( + 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 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. + /// 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. @@ -167,11 +378,105 @@ 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 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. + /// 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. @@ -179,27 +484,339 @@ 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; /// - /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// 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. - long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + 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. + /// 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) + /// 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 From 26b71ff76e5782fed8cb21c71e7c20fa73663510 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Thu, 6 Jul 2023 13:00:55 +0100 Subject: [PATCH 33/40] reformatting --- .../Abstractions/IBaseMongoRepository.cs | 22 +- .../IBaseMongoRepository_Update.cs | 653 ++++++++++++++++-- ...aseMongoRepository_Update_ClientSession.cs | 209 ++++-- .../Abstractions/IBaseReadOnlyRepository.cs | 291 ++++++-- .../IReadOnlyMongoRepository.TKey.cs | 253 +++++-- .../Abstractions/IReadOnlyMongoRepository.cs | 3 +- .../BaseMongoRepository.Index.cs | 162 ++++- .../BaseMongoRepository.Main.cs | 121 ++-- .../BaseMongoRepository.Update.cs | 536 +++++++++++--- .../DataAccess/Read/IMongoDbReader.cs | 265 ++++--- .../DataAccess/Read/MongoDbReader.GroupBy.cs | 5 +- .../DataAccess/Update/IMongoDbUpdater.cs | 205 ++++-- .../Update/MongoDbUpdater.ClientSession.cs | 92 ++- .../DataAccess/Update/MongoDbUpdater.cs | 16 +- .../IBaseMongoRepository.Create.cs | 36 +- .../IBaseMongoRepository.Delete.cs | 1 - .../IBaseMongoRepository.Index.cs | 362 ++++++---- .../BaseMongoRepository.TKey.Delete.cs | 34 +- .../BaseMongoRepository.TKey.Index.cs | 54 +- .../BaseMongoRepository.TKey.Main.cs | 30 +- .../BaseMongoRepository.TKey.ReadOnly.cs | 20 +- .../BaseMongoRepository.TKey.Update.cs | 260 +++++-- .../IBaseMongoRepository.TKey.Delete.cs | 50 +- .../IBaseMongoRepository.TKey.Index.cs | 341 +++++---- 24 files changed, 3041 insertions(+), 980 deletions(-) diff --git a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs index ead6f9f..72009fa 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs @@ -25,7 +25,11 @@ 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) + Task> GetPaginatedAsync( + Expression> filter, + int skipNumber = 0, + int takeNumber = 50, + string partitionKey = null) where TDocument : IDocument; /// @@ -37,7 +41,11 @@ 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) + Task> GetPaginatedAsync( + Expression> filter, + int skipNumber = 0, + int takeNumber = 50, + string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; @@ -49,7 +57,10 @@ namespace MongoDbGenericRepository /// /// /// - Task GetAndUpdateOne(FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options) + Task GetAndUpdateOne( + FilterDefinition filter, + UpdateDefinition update, + FindOneAndUpdateOptions options) where TDocument : IDocument; /// @@ -61,7 +72,10 @@ namespace MongoDbGenericRepository /// /// /// - Task GetAndUpdateOne(FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options) + Task GetAndUpdateOne( + FilterDefinition filter, + UpdateDefinition update, + FindOneAndUpdateOptions options) where TDocument : IDocument where TKey : IEquatable; } 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 60dc065..13cd40f 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs @@ -8,13 +8,12 @@ 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. @@ -24,12 +23,16 @@ namespace MongoDbGenericRepository.DataAccess.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) + bool UpdateOne( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value) 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. @@ -40,12 +43,17 @@ namespace MongoDbGenericRepository.DataAccess.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) + bool UpdateOne( + IClientSessionHandle session, + Expression> filter, + 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. @@ -56,12 +64,17 @@ namespace MongoDbGenericRepository.DataAccess.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) + bool UpdateOne( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + string partitionKey) 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. @@ -73,12 +86,18 @@ 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, CancellationToken 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. @@ -88,12 +107,16 @@ namespace MongoDbGenericRepository.DataAccess.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) + bool UpdateOne( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value) 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. @@ -104,12 +127,17 @@ namespace MongoDbGenericRepository.DataAccess.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) + bool UpdateOne( + IClientSessionHandle session, + FilterDefinition filter, + 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. @@ -120,12 +148,17 @@ namespace MongoDbGenericRepository.DataAccess.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) + bool UpdateOne( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) 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. @@ -137,12 +170,18 @@ 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, CancellationToken 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. @@ -152,12 +191,16 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The field to update. /// The value of the field. /// A boolean value indicating success. - bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value) + bool UpdateOne( + IClientSessionHandle session, + TDocument documentToModify, + Expression> field, + TField value) 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. @@ -168,12 +211,17 @@ 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) + 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. @@ -185,7 +233,7 @@ namespace MongoDbGenericRepository.DataAccess.Update where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -198,7 +246,7 @@ namespace MongoDbGenericRepository.DataAccess.Update where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -211,7 +259,7 @@ namespace MongoDbGenericRepository.DataAccess.Update where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -220,12 +268,16 @@ 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) + 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. @@ -235,12 +287,16 @@ namespace MongoDbGenericRepository.DataAccess.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) + Task UpdateOneAsync( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value) 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. @@ -251,12 +307,17 @@ namespace MongoDbGenericRepository.DataAccess.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) + Task UpdateOneAsync( + IClientSessionHandle session, + Expression> filter, + 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. @@ -267,12 +328,17 @@ namespace MongoDbGenericRepository.DataAccess.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) + Task UpdateOneAsync( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + string partitionKey) 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. @@ -284,12 +350,18 @@ 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, CancellationToken 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. @@ -299,12 +371,16 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The field to update. /// The value of the field. /// - Task UpdateOneAsync(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value) + Task UpdateOneAsync( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value) 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. @@ -315,12 +391,17 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The value of the field. /// The optional cancellation token. /// - Task UpdateOneAsync(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, CancellationToken cancellationToken) + Task UpdateOneAsync( + IClientSessionHandle session, + FilterDefinition filter, + 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. @@ -331,12 +412,17 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The value of the field. /// The optional partition key. /// - Task UpdateOneAsync(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey) + Task UpdateOneAsync( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) 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. @@ -348,12 +434,18 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The optional partition key. /// The optional cancellation token. /// - Task UpdateOneAsync(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey, CancellationToken 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. @@ -363,12 +455,16 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The field to update. /// The value of the field. /// A boolean value indicating success. - Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value) + Task UpdateOneAsync( + IClientSessionHandle session, + TDocument documentToModify, + Expression> field, + TField value) 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. @@ -379,12 +475,17 @@ namespace MongoDbGenericRepository.DataAccess.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) + 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. @@ -396,7 +497,7 @@ namespace MongoDbGenericRepository.DataAccess.Update where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -409,7 +510,7 @@ namespace MongoDbGenericRepository.DataAccess.Update where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -422,7 +523,7 @@ namespace MongoDbGenericRepository.DataAccess.Update where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -431,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) + 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 be4bbee..94000b1 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs @@ -2,10 +2,10 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using MongoDB.Driver; using MongoDbGenericRepository.Models; -using CancellationToken = System.Threading.CancellationToken; namespace MongoDbGenericRepository { @@ -488,7 +488,11 @@ namespace MongoDbGenericRepository /// A mongodb counting option. /// An optional partition key. /// An optional cancellation Token. - Task AnyAsync(FilterDefinition condition, CountOptions countOption, string partitionKey, CancellationToken cancellationToken) + Task AnyAsync( + FilterDefinition condition, + CountOptions countOption, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -760,7 +764,11 @@ namespace MongoDbGenericRepository /// A mongodb filter option. /// An optional partition key. /// An optional cancellation Token. - Task> GetAllAsync(FilterDefinition condition, FindOptions findOption, string partitionKey, CancellationToken cancellationToken) + Task> GetAllAsync( + FilterDefinition condition, + FindOptions findOption, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -852,7 +860,11 @@ namespace MongoDbGenericRepository /// A mongodb filter option. /// An optional partition key. /// An optional cancellation Token. - List GetAll(FilterDefinition condition, FindOptions findOption, string partitionKey, CancellationToken cancellationToken) + List GetAll( + FilterDefinition condition, + FindOptions findOption, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1032,7 +1044,11 @@ namespace MongoDbGenericRepository /// A mongodb counting option. /// An optional partitionKey /// An optional cancellation Token. - Task CountAsync(FilterDefinition condition, CountOptions countOption, string partitionKey, CancellationToken cancellationToken) + Task CountAsync( + FilterDefinition condition, + CountOptions countOption, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1241,7 +1257,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by descending. /// An optional cancellation Token. - Task GetByMaxAsync(Expression> filter, Expression> maxValueSelector, CancellationToken cancellationToken) + Task GetByMaxAsync( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1255,7 +1274,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by descending. /// An optional partitionKey. - Task GetByMaxAsync(Expression> filter, Expression> maxValueSelector, string partitionKey) + Task GetByMaxAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) where TDocument : IDocument where TKey : IEquatable; @@ -1269,7 +1291,11 @@ namespace MongoDbGenericRepository /// A property selector to order by descending. /// An optional partitionKey. /// An optional cancellation Token. - Task GetByMaxAsync(Expression> filter, Expression> maxValueSelector, string partitionKey, CancellationToken cancellationToken) + Task GetByMaxAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1294,7 +1320,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by descending. /// An optional cancellation token. - TDocument GetByMax(Expression> filter, Expression> orderByDescending, CancellationToken cancellationToken) + TDocument GetByMax( + Expression> filter, + Expression> orderByDescending, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1307,7 +1336,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by descending. /// An optional partitionKey. - TDocument GetByMax(Expression> filter, Expression> orderByDescending, string partitionKey) + TDocument GetByMax( + Expression> filter, + Expression> orderByDescending, + string partitionKey) where TDocument : IDocument where TKey : IEquatable; @@ -1321,7 +1353,11 @@ namespace MongoDbGenericRepository /// A property selector to order by descending. /// An optional partitionKey. /// An optional cancellation token. - TDocument GetByMax(Expression> filter, Expression> orderByDescending, string partitionKey, CancellationToken cancellationToken) + TDocument GetByMax( + Expression> filter, + Expression> orderByDescending, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1346,7 +1382,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector for the minimum value you are looking for. /// An optional cancellation Token. - Task GetByMinAsync(Expression> filter, Expression> minValueSelector, CancellationToken cancellationToken) + Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1359,7 +1398,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector for the minimum value you are looking for. /// An optional partitionKey. - Task GetByMinAsync(Expression> filter, Expression> minValueSelector, string partitionKey) + Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey) where TDocument : IDocument where TKey : IEquatable; @@ -1373,7 +1415,11 @@ namespace MongoDbGenericRepository /// 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) + Task GetByMinAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1398,7 +1444,10 @@ namespace MongoDbGenericRepository /// 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) + TDocument GetByMin( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1425,7 +1474,11 @@ namespace MongoDbGenericRepository /// 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) + TDocument GetByMin( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1450,7 +1503,10 @@ namespace MongoDbGenericRepository /// 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) + Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1463,7 +1519,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector for the maximum value you are looking for. /// An optional partitionKey. - Task GetMaxValueAsync(Expression> filter, Expression> maxValueSelector, string partitionKey) + Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) where TDocument : IDocument where TKey : IEquatable; @@ -1477,7 +1536,11 @@ namespace MongoDbGenericRepository /// 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) + Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1502,7 +1565,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional cancellation token. - TValue GetMaxValue(Expression> filter, Expression> maxValueSelector, CancellationToken cancellationToken) + TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1515,7 +1581,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partitionKey. - TValue GetMaxValue(Expression> filter, Expression> maxValueSelector, string partitionKey) + TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) where TDocument : IDocument where TKey : IEquatable; @@ -1529,7 +1598,11 @@ namespace MongoDbGenericRepository /// A property selector to order by ascending. /// An optional partitionKey. /// An optional cancellation token. - TValue GetMaxValue(Expression> filter, Expression> maxValueSelector, string partitionKey, CancellationToken cancellationToken) + TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1554,7 +1627,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional cancellation Token. - Task GetMinValueAsync(Expression> filter, Expression> minValueSelector, CancellationToken cancellationToken) + Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1567,7 +1643,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partition key. - Task GetMinValueAsync(Expression> filter, Expression> minValueSelector, string partitionKey) + Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey) where TDocument : IDocument where TKey : IEquatable; @@ -1581,7 +1660,11 @@ namespace MongoDbGenericRepository /// 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) + Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1606,7 +1689,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional cancellation token. - TValue GetMinValue(Expression> filter, Expression> minValueSelector, CancellationToken cancellationToken) + TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1619,7 +1705,10 @@ 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) + TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + string partitionKey) where TDocument : IDocument where TKey : IEquatable; @@ -1633,7 +1722,11 @@ namespace MongoDbGenericRepository /// 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) + TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1660,7 +1753,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The field you want to sum. /// An optional cancellation Token. - Task SumByAsync(Expression> filter, Expression> selector, CancellationToken cancellationToken) + Task SumByAsync( + Expression> filter, + Expression> selector, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1685,7 +1781,11 @@ namespace MongoDbGenericRepository /// 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) + Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1720,7 +1820,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The field you want to sum. /// An optional cancellation token. - Task SumByAsync(Expression> filter, Expression> selector, CancellationToken cancellationToken) + Task SumByAsync( + Expression> filter, + Expression> selector, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1745,7 +1848,11 @@ namespace MongoDbGenericRepository /// 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) + Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -1776,7 +1883,9 @@ namespace MongoDbGenericRepository /// The type representing the model you want to project to. /// A LINQ expression filter. /// The projection expression. - Task ProjectOneAsync(Expression> filter, Expression> projection) + Task ProjectOneAsync( + Expression> filter, + Expression> projection) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -1790,7 +1899,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The projection expression. /// An optional cancellation Token. - Task ProjectOneAsync(Expression> filter, Expression> projection, CancellationToken cancellationToken) + Task ProjectOneAsync( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -1804,7 +1916,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - Task ProjectOneAsync(Expression> filter, Expression> projection, string partitionKey) + Task ProjectOneAsync( + Expression> filter, + Expression> projection, + string partitionKey) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -1819,7 +1934,11 @@ namespace MongoDbGenericRepository /// The projection expression. /// An optional partition key. /// An optional cancellation token. - Task ProjectOneAsync(Expression> filter, Expression> projection, string partitionKey, CancellationToken cancellationToken) + Task ProjectOneAsync( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -1846,7 +1965,10 @@ namespace MongoDbGenericRepository /// /// The projection expression. /// An optional cancellation token. - TProjection ProjectOne(Expression> filter, Expression> projection, CancellationToken cancellationToken) + TProjection ProjectOne( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -1860,7 +1982,10 @@ namespace MongoDbGenericRepository /// /// The projection expression. /// An optional partition key. - TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey) + TProjection ProjectOne( + Expression> filter, + Expression> projection, + string partitionKey) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -1875,7 +2000,11 @@ namespace MongoDbGenericRepository /// The projection expression. /// An optional partition key. /// An optional cancellation token. - TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey, CancellationToken cancellationToken) + TProjection ProjectOne( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -1888,7 +2017,9 @@ namespace MongoDbGenericRepository /// The type representing the model you want to project to. /// A LINQ expression filter. /// The projection expression. - Task> ProjectManyAsync(Expression> filter, Expression> projection) + Task> ProjectManyAsync( + Expression> filter, + Expression> projection) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -1902,7 +2033,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The projection expression. /// An optional cancellation Token. - Task> ProjectManyAsync(Expression> filter, Expression> projection, CancellationToken cancellationToken) + Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -1916,7 +2050,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - Task> ProjectManyAsync(Expression> filter, Expression> projection, string partitionKey) + Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + string partitionKey) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -1931,7 +2068,11 @@ namespace MongoDbGenericRepository /// The projection expression. /// An optional partition key. /// An optional cancellation token. - Task> ProjectManyAsync(Expression> filter, Expression> projection, string partitionKey, CancellationToken cancellationToken) + Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -1944,7 +2085,9 @@ namespace MongoDbGenericRepository /// The type representing the model you want to project to. /// /// The projection expression. - List ProjectMany(Expression> filter, Expression> projection) + List ProjectMany( + Expression> filter, + Expression> projection) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -1958,7 +2101,10 @@ namespace MongoDbGenericRepository /// /// The projection expression. /// An optional cancellation token. - List ProjectMany(Expression> filter, Expression> projection, CancellationToken cancellationToken) + List ProjectMany( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -1972,7 +2118,10 @@ namespace MongoDbGenericRepository /// /// The projection expression. /// An optional partition key. - List ProjectMany(Expression> filter, Expression> projection, string partitionKey) + List ProjectMany( + Expression> filter, + Expression> projection, + string partitionKey) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -1987,7 +2136,11 @@ namespace MongoDbGenericRepository /// The projection expression. /// An optional partition key. /// An optional cancellation token. - List ProjectMany(Expression> filter, Expression> projection, string partitionKey, CancellationToken cancellationToken) + List ProjectMany( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -2006,7 +2159,9 @@ namespace MongoDbGenericRepository /// The type of the primary key. /// The grouping criteria. /// The projected group result. - List GroupBy(Expression> groupingCriteria, Expression, TProjection>> groupProjection) + List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection) where TDocument : IDocument where TKey : IEquatable where TProjection : class, new(); @@ -2022,7 +2177,10 @@ namespace MongoDbGenericRepository /// The grouping criteria. /// The projected group result. /// An optional cancellation token. - List GroupBy(Expression> groupingCriteria, Expression, TProjection>> groupProjection, CancellationToken cancellationToken) + List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable where TProjection : class, new(); @@ -2038,7 +2196,10 @@ namespace MongoDbGenericRepository /// The grouping criteria. /// The projected group result. /// The partition key of your document, if any. - List GroupBy(Expression> groupingCriteria, Expression, TProjection>> groupProjection, string partitionKey) + List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey) where TDocument : IDocument where TKey : IEquatable where TProjection : class, new(); @@ -2055,7 +2216,11 @@ namespace MongoDbGenericRepository /// 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) + List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable where TProjection : class, new(); @@ -2071,7 +2236,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The grouping criteria. /// The projected group result. - List GroupBy(Expression> filter, Expression> groupingCriteria, Expression, TProjection>> groupProjection) + List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection) where TDocument : IDocument where TKey : IEquatable where TProjection : class, new(); @@ -2088,7 +2256,11 @@ namespace MongoDbGenericRepository /// The grouping criteria. /// The projected group result. /// An optional cancellation token. - List GroupBy(Expression> filter, Expression> groupingCriteria, Expression, TProjection>> groupProjection, CancellationToken cancellationToken) + List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable where TProjection : class, new(); @@ -2105,7 +2277,11 @@ namespace MongoDbGenericRepository /// The grouping criteria. /// The projected group result. /// The partition key of your document, if any. - List GroupBy(Expression> filter, Expression> groupingCriteria, Expression, TProjection>> groupProjection, string partitionKey) + List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey) where TDocument : IDocument where TKey : IEquatable where TProjection : class, new(); @@ -2123,7 +2299,12 @@ namespace MongoDbGenericRepository /// 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) + List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable where TProjection : class, new(); diff --git a/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs index d7d2124..d518853 100644 --- a/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs +++ b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs @@ -410,7 +410,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by descending. /// The cancellation token. - Task GetByMaxAsync(Expression> filter, Expression> orderByDescending, CancellationToken cancellationToken) + Task GetByMaxAsync( + Expression> filter, + Expression> orderByDescending, + CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -421,7 +424,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by descending. /// An optional partitionKey. - Task GetByMaxAsync(Expression> filter, Expression> orderByDescending, string partitionKey) + Task GetByMaxAsync( + Expression> filter, + Expression> orderByDescending, + string partitionKey) where TDocument : IDocument; /// @@ -433,7 +439,11 @@ namespace MongoDbGenericRepository /// A property selector to order by descending. /// An optional partitionKey. /// The cancellation token. - Task GetByMaxAsync(Expression> filter, Expression> orderByDescending, string partitionKey, CancellationToken cancellationToken) + Task GetByMaxAsync( + Expression> filter, + Expression> orderByDescending, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -456,7 +466,10 @@ namespace MongoDbGenericRepository /// A property selector to order by descending. /// The cancellation token. /// - TDocument GetByMax(Expression> filter, Expression> orderByDescending, CancellationToken cancellationToken) + TDocument GetByMax( + Expression> filter, + Expression> orderByDescending, + CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -481,7 +494,11 @@ namespace MongoDbGenericRepository /// An optional partitionKey. /// The cancellation token. /// - TDocument GetByMax(Expression> filter, Expression> orderByDescending, string partitionKey, CancellationToken cancellationToken) + TDocument GetByMax( + Expression> filter, + Expression> orderByDescending, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -502,7 +519,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// The cancellation token. - Task GetByMinAsync(Expression> filter, Expression> orderByAscending, CancellationToken cancellationToken) + Task GetByMinAsync( + Expression> filter, + Expression> orderByAscending, + CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -513,7 +533,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partitionKey. - Task GetByMinAsync(Expression> filter, Expression> orderByAscending, string partitionKey) + Task GetByMinAsync( + Expression> filter, + Expression> orderByAscending, + string partitionKey) where TDocument : IDocument; /// @@ -525,7 +548,11 @@ namespace MongoDbGenericRepository /// A property selector to order by ascending. /// An optional partitionKey. /// The cancellation token. - Task GetByMinAsync(Expression> filter, Expression> orderByAscending, string partitionKey, CancellationToken cancellationToken) + Task GetByMinAsync( + Expression> filter, + Expression> orderByAscending, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -546,7 +573,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// The cancellation token. - TDocument GetByMin(Expression> filter, Expression> orderByAscending, CancellationToken cancellationToken) + TDocument GetByMin( + Expression> filter, + Expression> orderByAscending, + CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -569,7 +599,11 @@ namespace MongoDbGenericRepository /// A property selector to order by ascending. /// An optional partitionKey. /// The cancellation token. - TDocument GetByMin(Expression> filter, Expression> orderByAscending, string partitionKey, CancellationToken cancellationToken) + TDocument GetByMin( + Expression> filter, + Expression> orderByAscending, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -590,7 +624,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// The cancellation token. - Task GetMaxValueAsync(Expression> filter, Expression> maxValueSelector, CancellationToken cancellationToken) + Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -601,7 +638,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partitionKey. - Task GetMaxValueAsync(Expression> filter, Expression> maxValueSelector, string partitionKey) + Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) where TDocument : IDocument; /// @@ -613,7 +653,11 @@ namespace MongoDbGenericRepository /// A property selector to order by ascending. /// An optional partitionKey. /// The cancellation token. - Task GetMaxValueAsync(Expression> filter, Expression> maxValueSelector, string partitionKey, CancellationToken cancellationToken) + Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -634,7 +678,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// The cancellation token. - TValue GetMaxValue(Expression> filter, Expression> maxValueSelector, CancellationToken cancellationToken) + TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + CancellationToken cancellationToken) where TDocument : IDocument; @@ -646,7 +693,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partitionKey. - TValue GetMaxValue(Expression> filter, Expression> maxValueSelector, string partitionKey) + TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + string partitionKey) where TDocument : IDocument; /// @@ -658,7 +708,11 @@ namespace MongoDbGenericRepository /// A property selector to order by ascending. /// An optional partitionKey. /// The cancellation token. - TValue GetMaxValue(Expression> filter, Expression> maxValueSelector, string partitionKey, CancellationToken cancellationToken) + TValue GetMaxValue( + Expression> filter, + Expression> maxValueSelector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -680,7 +734,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// The cancellation token. - Task GetMinValueAsync(Expression> filter, Expression> minValueSelector, CancellationToken cancellationToken) + Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) where TDocument : IDocument; @@ -692,7 +749,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partition key. - Task GetMinValueAsync(Expression> filter, Expression> minValueSelector, string partitionKey) + Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey) where TDocument : IDocument; /// @@ -704,7 +764,11 @@ namespace MongoDbGenericRepository /// A property selector to order by ascending. /// An optional partition key. /// The cancellation token. - Task GetMinValueAsync(Expression> filter, Expression> minValueSelector, string partitionKey, CancellationToken cancellationToken) + Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -725,7 +789,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// The cancellation token. - TValue GetMinValue(Expression> filter, Expression> minValueSelector, CancellationToken cancellationToken) + TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + CancellationToken cancellationToken) where TDocument : IDocument; @@ -737,7 +804,10 @@ 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) + TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + string partitionKey) where TDocument : IDocument; /// @@ -749,7 +819,11 @@ namespace MongoDbGenericRepository /// A property selector to order by ascending. /// An optional partition key. /// The cancellation token. - TValue GetMinValue(Expression> filter, Expression> minValueSelector, string partitionKey, CancellationToken cancellationToken) + TValue GetMinValue( + Expression> filter, + Expression> minValueSelector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument; #endregion @@ -772,7 +846,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The field you want to sum. /// The cancellation token. - Task SumByAsync(Expression> filter, Expression> selector, CancellationToken cancellationToken) + Task SumByAsync( + Expression> filter, + Expression> selector, + CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -793,7 +870,11 @@ namespace MongoDbGenericRepository /// 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) + Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -812,7 +893,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The field you want to sum. /// The cancellation token. - Task SumByAsync(Expression> filter, Expression> selector, CancellationToken cancellationToken) + Task SumByAsync( + Expression> filter, + Expression> selector, + CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -833,7 +917,11 @@ namespace MongoDbGenericRepository /// 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) + Task SumByAsync( + Expression> filter, + Expression> selector, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -885,7 +973,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The projection expression. /// The cancellation token - Task ProjectOneAsync(Expression> filter, Expression> projection, CancellationToken cancellationToken) + Task ProjectOneAsync( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) where TDocument : IDocument where TProjection : class; @@ -897,7 +988,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - Task ProjectOneAsync(Expression> filter, Expression> projection, string partitionKey) + Task ProjectOneAsync( + Expression> filter, + Expression> projection, + string partitionKey) where TDocument : IDocument where TProjection : class; @@ -910,9 +1004,14 @@ namespace MongoDbGenericRepository /// The projection expression. /// An optional partition key. /// The cancellation token - Task ProjectOneAsync(Expression> filter, Expression> projection, string partitionKey, CancellationToken cancellationToken) + Task ProjectOneAsync( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TProjection : class; + /// /// Returns a projected document matching the filter condition. /// @@ -932,7 +1031,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The projection expression. /// The cancellation token. - TProjection ProjectOne(Expression> filter, Expression> projection, CancellationToken cancellationToken) + TProjection ProjectOne( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) where TDocument : IDocument where TProjection : class; @@ -944,7 +1046,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey) + TProjection ProjectOne( + Expression> filter, + Expression> projection, + string partitionKey) where TDocument : IDocument where TProjection : class; @@ -957,7 +1062,11 @@ namespace MongoDbGenericRepository /// The projection expression. /// An optional partition key. /// The cancellation token. - TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey, CancellationToken cancellationToken) + TProjection ProjectOne( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TProjection : class; @@ -968,7 +1077,9 @@ namespace MongoDbGenericRepository /// The type representing the model you want to project to. /// A LINQ expression filter. /// The projection expression. - Task> ProjectManyAsync(Expression> filter, Expression> projection) + Task> ProjectManyAsync( + Expression> filter, + Expression> projection) where TDocument : IDocument where TProjection : class; @@ -980,7 +1091,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The projection expression. /// The cancellation token. - Task> ProjectManyAsync(Expression> filter, Expression> projection, CancellationToken cancellationToken) + Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) where TDocument : IDocument where TProjection : class; @@ -992,7 +1106,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - Task> ProjectManyAsync(Expression> filter, Expression> projection, string partitionKey) + Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + string partitionKey) where TDocument : IDocument where TProjection : class; @@ -1005,7 +1122,11 @@ namespace MongoDbGenericRepository /// The projection expression. /// An optional partition key. /// The cancellation token. - Task> ProjectManyAsync(Expression> filter, Expression> projection, string partitionKey, CancellationToken cancellationToken) + Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TProjection : class; @@ -1028,7 +1149,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The projection expression. /// The cancellation token - List ProjectMany(Expression> filter, Expression> projection, CancellationToken cancellationToken) + List ProjectMany( + Expression> filter, + Expression> projection, + CancellationToken cancellationToken) where TDocument : IDocument where TProjection : class; @@ -1040,7 +1164,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - List ProjectMany(Expression> filter, Expression> projection, string partitionKey) + List ProjectMany( + Expression> filter, + Expression> projection, + string partitionKey) where TDocument : IDocument where TProjection : class; @@ -1053,7 +1180,11 @@ namespace MongoDbGenericRepository /// The projection expression. /// An optional partition key. /// The cancellation token - List ProjectMany(Expression> filter, Expression> projection, string partitionKey, CancellationToken cancellationToken) + List ProjectMany( + Expression> filter, + Expression> projection, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TProjection : class; @@ -1070,7 +1201,9 @@ namespace MongoDbGenericRepository /// The type of the projected group. /// The grouping criteria. /// The projected group result. - List GroupBy(Expression> groupingCriteria, Expression, TProjection>> groupProjection) + List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection) where TDocument : IDocument where TProjection : class, new(); @@ -1084,7 +1217,10 @@ namespace MongoDbGenericRepository /// The grouping criteria. /// The projected group result. /// The cancellation token. - List GroupBy(Expression> groupingCriteria, Expression, TProjection>> groupProjection, CancellationToken cancellationToken) + List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + CancellationToken cancellationToken) where TDocument : IDocument where TProjection : class, new(); @@ -1098,7 +1234,10 @@ namespace MongoDbGenericRepository /// The grouping criteria. /// The projected group result. /// The partition key of your document, if any. - List GroupBy(Expression> groupingCriteria, Expression, TProjection>> groupProjection, string partitionKey) + List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey) where TDocument : IDocument where TProjection : class, new(); @@ -1113,7 +1252,11 @@ namespace MongoDbGenericRepository /// 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) + List GroupBy( + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TProjection : class, new(); @@ -1127,7 +1270,10 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The grouping criteria. /// The projected group result. - List GroupBy(Expression> filter, Expression> groupingCriteria, Expression, TProjection>> groupProjection) + List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection) where TDocument : IDocument where TProjection : class, new(); @@ -1142,7 +1288,11 @@ namespace MongoDbGenericRepository /// The grouping criteria. /// The projected group result. /// The cancellation token. - List GroupBy(Expression> filter, Expression> groupingCriteria, Expression, TProjection>> groupProjection, CancellationToken cancellationToken) + List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + CancellationToken cancellationToken) where TDocument : IDocument where TProjection : class, new(); @@ -1157,7 +1307,11 @@ namespace MongoDbGenericRepository /// The grouping criteria. /// The projected group result. /// The partition key of your document, if any. - List GroupBy(Expression> filter, Expression> groupingCriteria, Expression, TProjection>> groupProjection, string partitionKey) + List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey) where TDocument : IDocument where TProjection : class, new(); @@ -1173,7 +1327,12 @@ namespace MongoDbGenericRepository /// 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) + List GroupBy( + Expression> filter, + Expression> groupingCriteria, + Expression, TProjection>> groupProjection, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TProjection : class, new(); 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.Index.cs b/MongoDbGenericRepository/BaseMongoRepository.Index.cs index 9ed7e9d..b56b60f 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Index.cs @@ -422,7 +422,10 @@ namespace MongoDbGenericRepository } /// - public async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + public async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) where TDocument : IDocument { return await CreateDescendingIndexAsync(field, indexCreationOptions, null, cancellationToken); @@ -436,21 +439,31 @@ namespace MongoDbGenericRepository } /// - public async Task CreateDescendingIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + 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) + 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) + public async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); @@ -465,7 +478,9 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateDescendingIndexAsync(Expression> field, CancellationToken cancellationToken) + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -473,7 +488,9 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions) where TDocument : IDocument where TKey : IEquatable { @@ -482,7 +499,10 @@ namespace MongoDbGenericRepository /// - public virtual async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -498,7 +518,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateDescendingIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -506,7 +529,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -514,7 +540,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -543,7 +573,10 @@ namespace MongoDbGenericRepository } /// - public async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + public async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) where TDocument : IDocument { return await CreateHashedIndexAsync(field, indexCreationOptions, null, cancellationToken); @@ -557,21 +590,31 @@ namespace MongoDbGenericRepository } /// - public async Task CreateHashedIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + 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) + 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) + public async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await CreateHashedIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); @@ -586,7 +629,9 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateHashedIndexAsync(Expression> field, CancellationToken cancellationToken) + public virtual async Task CreateHashedIndexAsync( + Expression> field, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -594,7 +639,9 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions) where TDocument : IDocument where TKey : IEquatable { @@ -602,7 +649,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -618,7 +668,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateHashedIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + public virtual async Task CreateHashedIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -626,7 +679,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -634,7 +690,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -649,21 +709,28 @@ namespace MongoDbGenericRepository } /// - public async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, CancellationToken cancellationToken) + 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) + 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) + public async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) where TDocument : IDocument { return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, null, cancellationToken); @@ -677,21 +744,31 @@ namespace MongoDbGenericRepository } /// - public async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, string partitionKey, CancellationToken cancellationToken) + 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) + 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) + public async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey, cancellationToken); @@ -706,7 +783,9 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, CancellationToken cancellationToken) + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -714,7 +793,9 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions) + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions) where TDocument : IDocument where TKey : IEquatable { @@ -722,7 +803,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -730,7 +814,9 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, string partitionKey) + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -738,7 +824,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, string partitionKey, CancellationToken cancellationToken) + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -746,7 +835,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions, string partitionKey) + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -754,7 +846,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { diff --git a/MongoDbGenericRepository/BaseMongoRepository.Main.cs b/MongoDbGenericRepository/BaseMongoRepository.Main.cs index f86f9c7..fdbf67f 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Main.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Main.cs @@ -1,21 +1,21 @@ -using MongoDB.Driver; -using MongoDbGenericRepository.Models; -using MongoDbGenericRepository.Utils; -using System; +using System; using System.Collections.Generic; using System.Linq.Expressions; 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,23 +24,23 @@ namespace MongoDbGenericRepository } /// - /// The constructor taking a . + /// The constructor taking a . /// - /// A mongodb context implementing + /// A mongodb context implementing protected BaseMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } /// - /// The constructor 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. + /// Asynchronously returns a paginated list of the documents matching the filter condition. /// /// The type representing a Document. /// A LINQ expression filter. @@ -58,7 +58,7 @@ namespace MongoDbGenericRepository } /// - /// 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. @@ -66,48 +66,19 @@ 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. - 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) 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); - } - - /// - /// 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 +91,7 @@ namespace MongoDbGenericRepository { throw new ArgumentNullException(nameof(document)); } + var defaultTKey = default(TKey); if (document.Id == null || (defaultTKey != null @@ -130,24 +102,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 +133,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 +148,45 @@ namespace MongoDbGenericRepository { return MongoDbContext.GetCollection(partitionKey); } + + #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); + } + + /// + /// 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 } } \ No newline at end of file diff --git a/MongoDbGenericRepository/BaseMongoRepository.Update.cs b/MongoDbGenericRepository/BaseMongoRepository.Update.cs index 3d4ccb7..9b22fd7 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Update.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Update.cs @@ -1,29 +1,32 @@ -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 { /// - /// 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 IMongoDbUpdater _mongoDbUpdater; /// - /// The MongoDb accessor to update data. + /// The MongoDb accessor to update data. /// protected virtual IMongoDbUpdater MongoDbUpdater { get { - if (_mongoDbUpdater != null) { return _mongoDbUpdater; } + if (_mongoDbUpdater != null) + { + return _mongoDbUpdater; + } lock (_initLock) { @@ -35,7 +38,7 @@ namespace MongoDbGenericRepository return _mongoDbUpdater; } - set { _mongoDbUpdater = value; } + set => _mongoDbUpdater = value; } #region Update @@ -76,7 +79,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken) + public virtual async Task UpdateOneAsync( + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken) where TDocument : IDocument { return await MongoDbUpdater.UpdateOneAsync(documentToModify, update, cancellationToken); @@ -104,7 +110,11 @@ namespace MongoDbGenericRepository } /// - public virtual bool UpdateOne(TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken) + public virtual bool UpdateOne( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { return MongoDbUpdater.UpdateOne(documentToModify, field, value, cancellationToken); @@ -116,8 +126,13 @@ namespace MongoDbGenericRepository { return await UpdateOneAsync(documentToModify, field, value, CancellationToken.None); } + /// - public virtual async Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken) + public virtual async Task UpdateOneAsync( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { return await MongoDbUpdater.UpdateOneAsync(documentToModify, field, value, cancellationToken); @@ -131,21 +146,34 @@ namespace MongoDbGenericRepository } /// - public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, CancellationToken cancellationToken) + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { return UpdateOne(filter, field, value, null, cancellationToken); } /// - public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey) + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument { return UpdateOne(filter, field, value, partitionKey, CancellationToken.None); } /// - public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey, cancellationToken); @@ -159,119 +187,187 @@ namespace MongoDbGenericRepository } /// - public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, CancellationToken cancellationToken) + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { return UpdateOne(filter, field, value, null, cancellationToken); } /// - public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey) + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument { return UpdateOne(filter, field, value, partitionKey, CancellationToken.None); } /// - public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey, cancellationToken); } /// - public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value) + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value) where TDocument : IDocument { return await UpdateOneAsync(filter, field, value, null, CancellationToken.None); } /// - public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, CancellationToken cancellationToken) + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { return await UpdateOneAsync(filter, field, value, null, cancellationToken); } /// - public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey) + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument { return await UpdateOneAsync(filter, field, value, partitionKey, CancellationToken.None); } /// - public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + 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) + 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) + 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) + 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) + 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) + 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) + 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) + 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) + 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) + 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) + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { return await UpdateManyAsync(filter, field, value, null, cancellationToken); @@ -279,14 +375,23 @@ namespace MongoDbGenericRepository /// - public virtual async Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey) + 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) + 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); @@ -300,21 +405,31 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + 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) + 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) + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey, cancellationToken); @@ -328,21 +443,31 @@ namespace MongoDbGenericRepository } /// - public async Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + 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) + 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) + public async Task UpdateManyAsync( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey, cancellationToken); @@ -356,21 +481,34 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value, CancellationToken cancellationToken) + 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) + 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) + 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); @@ -384,21 +522,34 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(Expression> filter, Expression> field, TField value, CancellationToken cancellationToken) + 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) + 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) + 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); @@ -412,7 +563,10 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + public virtual long UpdateMany( + FilterDefinition filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) where TDocument : IDocument { return UpdateMany(filter, updateDefinition, null, cancellationToken); @@ -426,7 +580,11 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey, CancellationToken cancellationToken) + public virtual long UpdateMany( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey, cancellationToken); @@ -440,7 +598,10 @@ namespace MongoDbGenericRepository } /// - public long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + public long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) where TDocument : IDocument { return UpdateMany(filter, updateDefinition, null, cancellationToken); @@ -454,7 +615,11 @@ namespace MongoDbGenericRepository } /// - public long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey, CancellationToken cancellationToken) + public long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey, cancellationToken); @@ -505,7 +670,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken) + public virtual async Task UpdateOneAsync( + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -537,7 +705,11 @@ namespace MongoDbGenericRepository } /// - public virtual bool UpdateOne(TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken) + public virtual bool UpdateOne( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -545,7 +717,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value) + public virtual async Task UpdateOneAsync( + TDocument documentToModify, + Expression> field, + TField value) where TDocument : IDocument where TKey : IEquatable { @@ -553,7 +728,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken) + public virtual async Task UpdateOneAsync( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -569,7 +748,11 @@ namespace MongoDbGenericRepository } /// - public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, CancellationToken cancellationToken) + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -577,7 +760,11 @@ namespace MongoDbGenericRepository } /// - public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey) + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -585,7 +772,12 @@ namespace MongoDbGenericRepository } /// - public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -593,7 +785,10 @@ namespace MongoDbGenericRepository } /// - public virtual bool UpdateOne(Expression> filter, Expression> field, TField value) + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value) where TDocument : IDocument where TKey : IEquatable { @@ -601,7 +796,11 @@ namespace MongoDbGenericRepository } /// - public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, CancellationToken cancellationToken) + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -609,7 +808,11 @@ namespace MongoDbGenericRepository } /// - public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey) + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -617,7 +820,12 @@ namespace MongoDbGenericRepository } /// - public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -625,7 +833,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value) + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value) where TDocument : IDocument where TKey : IEquatable { @@ -633,7 +844,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, CancellationToken cancellationToken) + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -641,7 +856,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey) + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -649,7 +868,12 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -657,7 +881,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateOneAsync(Expression> filter, Expression> field, TField value) + public virtual async Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value) where TDocument : IDocument where TKey : IEquatable { @@ -665,7 +892,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateOneAsync(Expression> filter, Expression> field, TField value, CancellationToken cancellationToken) + public virtual async Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -673,7 +904,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey) + public virtual async Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -681,15 +916,28 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + 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); + return await MongoDbUpdater.UpdateOneAsync( + Builders.Filter.Where(filter), + field, + value, + partitionKey, + cancellationToken); } /// - public virtual async Task UpdateManyAsync(Expression> filter, Expression> field, TField value) + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value) where TDocument : IDocument where TKey : IEquatable { @@ -697,7 +945,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateManyAsync(Expression> filter, Expression> field, TField value, CancellationToken cancellationToken) + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -705,7 +957,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey) + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -713,7 +969,12 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + public virtual async Task UpdateManyAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -721,7 +982,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value) + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value) where TDocument : IDocument where TKey : IEquatable { @@ -729,7 +993,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, CancellationToken cancellationToken) + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -737,7 +1005,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey) + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -745,7 +1017,12 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -761,7 +1038,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -769,7 +1049,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey) + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -777,7 +1060,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey, CancellationToken cancellationToken) + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -793,7 +1080,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + public virtual async Task UpdateManyAsync( + Expression> filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -801,7 +1091,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, string partitionKey) + public virtual async Task UpdateManyAsync( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -809,7 +1102,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, string partitionKey, CancellationToken cancellationToken) + public virtual async Task UpdateManyAsync( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -817,7 +1114,10 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(Expression> filter, Expression> field, TField value) + public virtual long UpdateMany( + Expression> filter, + Expression> field, + TField value) where TDocument : IDocument where TKey : IEquatable { @@ -825,7 +1125,11 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(Expression> filter, Expression> field, TField value, CancellationToken cancellationToken) + public virtual long UpdateMany( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -833,7 +1137,11 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey) + public virtual long UpdateMany( + Expression> filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -841,7 +1149,12 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + public virtual long UpdateMany( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -857,7 +1170,11 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value, CancellationToken cancellationToken) + public virtual long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -865,7 +1182,11 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey) + public virtual long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -873,7 +1194,12 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + public virtual long UpdateMany( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -889,7 +1215,10 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + public virtual long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -897,7 +1226,10 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey) + public virtual long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -905,7 +1237,11 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey, CancellationToken cancellationToken) + public virtual long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -921,7 +1257,10 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + public virtual long UpdateMany( + FilterDefinition filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -937,7 +1276,11 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey, CancellationToken cancellationToken) + public virtual long UpdateMany( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -946,5 +1289,4 @@ namespace MongoDbGenericRepository #endregion Update } - } \ No newline at end of file diff --git a/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs b/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs index 7230aa3..db14f81 100644 --- a/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs +++ b/MongoDbGenericRepository/DataAccess/Read/IMongoDbReader.cs @@ -11,12 +11,12 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository.DataAccess.Read { /// - /// A interface for accessing the Database and its Collections. + /// A interface for accessing the Database and its Collections. /// public interface IMongoDbReader : IDataAccessBase { /// - /// 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. @@ -35,7 +35,7 @@ namespace MongoDbGenericRepository.DataAccess.Read where TProjection : class; /// - /// Returns a projected document matching the filter condition. + /// Returns a projected document matching the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -43,13 +43,18 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey = null, CancellationToken cancellationToken = default) + /// 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. + /// 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. @@ -68,7 +73,7 @@ namespace MongoDbGenericRepository.DataAccess.Read where TProjection : class; /// - /// Asynchronously returns a list of projected documents matching the filter condition. + /// 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. @@ -76,14 +81,19 @@ namespace MongoDbGenericRepository.DataAccess.Read /// The document filter. /// The projection expression. /// An optional partition key. - List ProjectMany(Expression> filter, Expression> projection, string partitionKey = null, CancellationToken cancellationToken = default) + /// 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. + /// 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. @@ -92,17 +102,19 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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) + 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. + /// 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. @@ -112,18 +124,20 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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) + 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. + /// 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. @@ -145,7 +159,7 @@ namespace MongoDbGenericRepository.DataAccess.Read where TProjection : class, new(); /// - /// 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. @@ -168,7 +182,7 @@ namespace MongoDbGenericRepository.DataAccess.Read 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. @@ -189,7 +203,7 @@ namespace MongoDbGenericRepository.DataAccess.Read where TKey : IEquatable; /// - /// 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. @@ -201,18 +215,19 @@ namespace MongoDbGenericRepository.DataAccess.Read 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. /// 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. + /// Asynchronously returns one document given filter definition. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -220,49 +235,60 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A mongodb filter option. /// An optional partition key. /// An optional cancellation Token. - Task GetOneAsync(FilterDefinition condition, FindOptions findOption = null, + 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. + /// 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 = null, + CancellationToken cancellationToken = default) 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. /// A mongodb filter option. /// An optional partition key. - TDocument GetOne(FilterDefinition condition, FindOptions findOption = null, string partitionKey = null, CancellationToken cancellationToken = default) + /// 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. + /// 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. + /// Returns a collection cursor. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -273,7 +299,7 @@ namespace MongoDbGenericRepository.DataAccess.Read 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. @@ -281,49 +307,60 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A mongodb counting option. /// An optional partition key. /// An optional cancellation Token. - Task AnyAsync(FilterDefinition condition, CountOptions countOption = null, string partitionKey = null, + 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. + /// 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 = null, + CancellationToken cancellationToken = default) 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. /// A mongodb counting option. /// An optional partition key. - bool Any(FilterDefinition condition, CountOptions countOption = null, - string partitionKey = null, CancellationToken cancellationToken = default) + /// 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. + /// 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. + /// 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. @@ -331,49 +368,63 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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) + 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. + /// 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 = null, + CancellationToken cancellationToken = default) 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. /// A mongodb filter option. /// An optional partition key. - List GetAll(FilterDefinition condition, FindOptions findOption = null, - string partitionKey = null, CancellationToken cancellationToken = default) + /// 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. + /// 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, CancellationToken cancellationToken = default) + /// 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. + /// Asynchronously counts how many documents match the filter condition. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -381,49 +432,61 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A mongodb counting option. /// An optional partitionKey /// An optional cancellation Token. - Task CountAsync(FilterDefinition condition, CountOptions countOption = null, - string partitionKey = null, CancellationToken cancellationToken = default) + 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. + /// 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 = null, + CancellationToken cancellationToken = default) 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. /// A mongodb counting option. /// An optional partitionKey - long Count(FilterDefinition condition, CountOptions countOption = null, - string partitionKey = null, CancellationToken cancellationToken = default) + /// 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. + /// 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. + /// 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. @@ -431,24 +494,35 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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) + 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. + /// 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> maxValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) + /// 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. + /// 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. @@ -456,24 +530,34 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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) + 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. + /// 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. - TDocument GetByMin(Expression> filter, Expression> minValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) + /// 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. + /// 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. @@ -482,12 +566,16 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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) + 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. + /// 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. @@ -495,12 +583,17 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partitionKey. - TValue GetMaxValue(Expression> filter, Expression> maxValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) + /// 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. + /// 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. @@ -509,12 +602,16 @@ namespace MongoDbGenericRepository.DataAccess.Read /// 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) + 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. + /// 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. @@ -522,12 +619,17 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partition key. - TValue GetMinValue(Expression> filter, Expression> minValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) + /// 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. + /// 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. @@ -535,7 +637,8 @@ namespace MongoDbGenericRepository.DataAccess.Read /// The field you want to sum. /// The partition key of your document, if any. /// An optional cancellation Token. - Task SumByAsync(Expression> filter, + Task SumByAsync( + Expression> filter, Expression> selector, string partitionKey = null, CancellationToken cancellationToken = default) @@ -543,7 +646,7 @@ namespace MongoDbGenericRepository.DataAccess.Read 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 for a Document. @@ -551,35 +654,39 @@ namespace MongoDbGenericRepository.DataAccess.Read /// The field you want to sum. /// The partition key of your document, if any. /// An optional cancellation Token. - Task SumByAsync(Expression> filter, + Task SumByAsync( + Expression> filter, Expression> selector, - string partitionKey = null, CancellationToken cancellationToken = default) + 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. + /// 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, + 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. + /// 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, + decimal SumBy( + Expression> filter, Expression> selector, string partitionKey = null) where TDocument : IDocument diff --git a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.GroupBy.cs b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.GroupBy.cs index 35185b3..c808147 100644 --- a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.GroupBy.cs +++ b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.GroupBy.cs @@ -15,7 +15,8 @@ namespace MongoDbGenericRepository.DataAccess.Read public virtual List GroupBy( Expression> groupingCriteria, Expression, TProjection>> groupProjection, - string partitionKey = null, CancellationToken cancellationToken = default) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable where TProjection : class, new() @@ -23,7 +24,7 @@ namespace MongoDbGenericRepository.DataAccess.Read return HandlePartitioned(partitionKey) .Aggregate() .Group(groupingCriteria, groupProjection) - .ToList(cancellationToken:cancellationToken); + .ToList(cancellationToken); } /// diff --git a/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs b/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs index 1d96fca..fa15af7 100644 --- a/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs +++ b/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs @@ -9,12 +9,12 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository.DataAccess.Update { /// - /// A interface for updating documents in MongoDb. + /// A interface for updating documents in MongoDb. /// public interface IMongoDbUpdater : IDataAccessBase { /// - /// Asynchronously Updates a document. + /// Asynchronously Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -25,19 +25,22 @@ namespace MongoDbGenericRepository.DataAccess.Update where TKey : IEquatable; /// - /// Takes a document you want to modify and applies the update you have defined in MongoDb. + /// 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) + 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. + /// 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. @@ -46,12 +49,16 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + 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. + /// 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. @@ -61,12 +68,17 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + 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. + /// 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. @@ -76,12 +88,17 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + Task UpdateOneAsync( + Expression> filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) 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. @@ -94,7 +111,7 @@ namespace MongoDbGenericRepository.DataAccess.Update where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -103,12 +120,16 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The update definition. /// The optional cancellation token. /// - Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default) + Task UpdateOneAsync( + IClientSessionHandle session, + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken = default) 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. @@ -119,12 +140,17 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The value of the field. /// The optional cancellation token. /// - Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default) + Task UpdateOneAsync( + IClientSessionHandle session, + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken = default) 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. @@ -136,12 +162,18 @@ 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) + 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. + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -153,12 +185,18 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The optional partition key. /// The optional cancellation token. /// - Task UpdateOneAsync(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) + 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. + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -169,7 +207,7 @@ namespace MongoDbGenericRepository.DataAccess.Update where TKey : IEquatable; /// - /// Takes a document you want to modify and applies the update you have defined in MongoDb. + /// 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. @@ -181,7 +219,7 @@ namespace MongoDbGenericRepository.DataAccess.Update where TKey : IEquatable; /// - /// Updates the property field with the given value update a property field in entities. + /// 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. @@ -190,12 +228,16 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + 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. + /// Updates the property field with the given value. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -205,12 +247,17 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + 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. + /// 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. @@ -220,12 +267,17 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) 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. @@ -238,7 +290,7 @@ namespace MongoDbGenericRepository.DataAccess.Update where TKey : IEquatable; /// - /// Updates a document. + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -247,12 +299,16 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The update definition. /// The optional cancellation token. /// - bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default) + bool UpdateOne( + IClientSessionHandle session, + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken = default) 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. @@ -263,12 +319,17 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The value of the field. /// The optional cancellation token. /// - bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default) + bool UpdateOne( + IClientSessionHandle session, + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken = default) 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. @@ -280,12 +341,18 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The optional partition key. /// The optional cancellation token. /// - bool UpdateOne(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) + 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. + /// Updates a document. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -297,12 +364,18 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The optional partition key. /// The optional cancellation token. /// - bool UpdateOne(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) + 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. + /// 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. @@ -312,12 +385,17 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + 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. + /// 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. @@ -327,12 +405,17 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + 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. + /// 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. @@ -340,12 +423,16 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + 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. + /// 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. @@ -353,12 +440,16 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + 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. + /// 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. @@ -368,12 +459,17 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + 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. + /// 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. @@ -383,12 +479,17 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + 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. + /// 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. @@ -396,7 +497,11 @@ namespace MongoDbGenericRepository.DataAccess.Update /// 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) + long UpdateMany( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; } diff --git a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs index 8726c6f..ff78746 100644 --- a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs +++ b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs @@ -1,23 +1,26 @@ -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 { /// - public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default) + 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; } @@ -27,22 +30,32 @@ namespace MongoDbGenericRepository.DataAccess.Update 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; } /// - public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default) + 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; } /// - public virtual bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default) + public virtual bool UpdateOne( + IClientSessionHandle session, + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { @@ -52,40 +65,67 @@ namespace MongoDbGenericRepository.DataAccess.Update } /// - public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default) + 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; } /// - public virtual bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default) + 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; } /// - public virtual async Task UpdateOneAsync(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) + 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; } /// - public virtual Task UpdateOneAsync(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) + public virtual Task UpdateOneAsync( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { @@ -93,7 +133,13 @@ namespace MongoDbGenericRepository.DataAccess.Update } /// - public virtual bool UpdateOne(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) + public virtual bool UpdateOne( + IClientSessionHandle session, + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { @@ -103,7 +149,13 @@ namespace MongoDbGenericRepository.DataAccess.Update } /// - public virtual bool UpdateOne(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default) + public virtual bool UpdateOne( + IClientSessionHandle session, + Expression> filter, + Expression> field, + TField value, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { diff --git a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs index b5ffa7b..ebb74ef 100644 --- a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs +++ b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs @@ -56,7 +56,10 @@ namespace MongoDbGenericRepository.DataAccess.Update } /// - public virtual bool UpdateOne(TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default) + public virtual bool UpdateOne( + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { @@ -83,12 +86,19 @@ namespace MongoDbGenericRepository.DataAccess.Update } /// - public virtual bool UpdateOne(TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default) + 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), cancellationToken: cancellationToken); + var updateRes = HandlePartitioned(documentToModify).UpdateOne( + filter, + Builders.Update.Set(field, value), + cancellationToken: cancellationToken); return updateRes.ModifiedCount == 1; } diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Create.cs b/MongoDbGenericRepository/IBaseMongoRepository.Create.cs index 1b7243d..56be005 100644 --- a/MongoDbGenericRepository/IBaseMongoRepository.Create.cs +++ b/MongoDbGenericRepository/IBaseMongoRepository.Create.cs @@ -7,14 +7,14 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The IBaseMongoRepository_Create interface to expose document creation functionality - /// with document having an Id of type Guid. + /// 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. + /// 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. @@ -24,8 +24,8 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Asynchronously adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. + /// 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. @@ -36,8 +36,8 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. + /// 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. @@ -47,8 +47,8 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. + /// 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. @@ -59,8 +59,8 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Asynchronously adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. + /// 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. @@ -70,8 +70,8 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Asynchronously adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. + /// 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. @@ -82,8 +82,8 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. + /// 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. @@ -93,8 +93,8 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. + /// 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. diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs b/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs index 9941b6d..1407d5f 100644 --- a/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs +++ b/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs @@ -296,6 +296,5 @@ namespace MongoDbGenericRepository 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 index 84abebf..cccdc08 100644 --- a/MongoDbGenericRepository/IBaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs @@ -8,12 +8,12 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The interface exposing index management functionality for Guid Keyed repositories. + /// 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. + /// Returns the names of the indexes present on a collection. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -23,7 +23,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Returns the names of the indexes present on a collection. + /// Returns the names of the indexes present on a collection. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -34,7 +34,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Returns the names of the indexes present on a collection. + /// Returns the names of the indexes present on a collection. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -45,7 +45,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Returns the names of the indexes present on a collection. + /// Returns the names of the indexes present on a collection. /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -57,9 +57,9 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -70,9 +70,9 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -84,9 +84,9 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -98,9 +98,9 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -113,9 +113,9 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -127,9 +127,9 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -137,14 +137,17 @@ namespace MongoDbGenericRepository /// Options for creating an index. /// The cancellation token /// The result of the create index operation. - Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + 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. + /// 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. @@ -152,14 +155,17 @@ namespace MongoDbGenericRepository /// Options for creating an index. /// An optional partition key. /// The result of the create index operation. - Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + 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. + /// 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. @@ -168,14 +174,18 @@ namespace MongoDbGenericRepository /// An optional partition key. /// The cancellation token. /// The result of the create index operation. - Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + 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. + /// 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. @@ -186,9 +196,9 @@ namespace MongoDbGenericRepository 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. + /// 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. @@ -200,9 +210,9 @@ namespace MongoDbGenericRepository 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. + /// 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. @@ -214,9 +224,9 @@ namespace MongoDbGenericRepository 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. + /// 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. @@ -224,14 +234,17 @@ namespace MongoDbGenericRepository /// Options for creating an index. /// The cancellation token. /// The result of the create index operation. - Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + 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. + /// 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. @@ -243,9 +256,9 @@ namespace MongoDbGenericRepository 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. + /// 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. @@ -253,14 +266,17 @@ namespace MongoDbGenericRepository /// An optional partition key. /// The cancellation token. /// The result of the create index operation. - Task CreateAscendingIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + 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. + /// 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. @@ -268,14 +284,17 @@ namespace MongoDbGenericRepository /// Options for creating an index. /// An optional partition key. /// The result of the create index operation. - Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + 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. + /// 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. @@ -284,14 +303,18 @@ namespace MongoDbGenericRepository /// An optional partition key. /// The cancellation token. /// The result of the create index operation. - Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + 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. + /// 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. @@ -302,9 +325,9 @@ namespace MongoDbGenericRepository 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. + /// 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. @@ -316,9 +339,9 @@ namespace MongoDbGenericRepository 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. + /// 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. @@ -330,9 +353,9 @@ namespace MongoDbGenericRepository 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. + /// 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. @@ -340,14 +363,17 @@ namespace MongoDbGenericRepository /// Options for creating an index. /// The cancellation token. /// The result of the create index operation. - Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + 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. + /// 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. @@ -359,9 +385,9 @@ namespace MongoDbGenericRepository 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. + /// 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. @@ -369,14 +395,17 @@ namespace MongoDbGenericRepository /// An optional partition key. /// The cancellation token. /// The result of the create index operation. - Task CreateDescendingIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + 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. + /// 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. @@ -384,14 +413,17 @@ namespace MongoDbGenericRepository /// Options for creating an index. /// An optional partition key. /// The result of the create index operation. - Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + 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. + /// 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. @@ -400,14 +432,18 @@ namespace MongoDbGenericRepository /// An optional partition key. /// The cancellation token. /// The result of the create index operation. - Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + 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. + /// 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. @@ -418,9 +454,9 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Creates a hashed index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -432,9 +468,9 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Creates a hashed index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -446,9 +482,9 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Creates a hashed index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -456,14 +492,17 @@ namespace MongoDbGenericRepository /// Options for creating an index. /// The cancellation token. /// The result of the create index operation. - Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + 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. + /// 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. @@ -475,9 +514,9 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Creates a hashed index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -485,14 +524,17 @@ namespace MongoDbGenericRepository /// An optional partition key. /// The cancellation token. /// The result of the create index operation. - Task CreateHashedIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + 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. + /// 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. @@ -500,14 +542,17 @@ namespace MongoDbGenericRepository /// Options for creating an index. /// An optional partition key. /// The result of the create index operation. - Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + 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. + /// 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. @@ -516,14 +561,18 @@ namespace MongoDbGenericRepository /// An optional partition key. /// The cancellation token. /// The result of the create index operation. - Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + 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. + /// 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. @@ -534,9 +583,9 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Creates a combined text index. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -548,23 +597,25 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Creates a combined text index. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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) + 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. + /// 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. @@ -572,14 +623,17 @@ namespace MongoDbGenericRepository /// Options for creating an index. /// The cancellation token. /// The result of the create index operation. - Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + 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. + /// 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. @@ -591,9 +645,9 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Creates a combined text index. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -601,14 +655,17 @@ namespace MongoDbGenericRepository /// An optional partition key. /// The cancellation token. /// The result of the create index operation. - Task CreateCombinedTextIndexAsync(IEnumerable>> fields, string partitionKey, CancellationToken cancellationToken) + 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. + /// 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. @@ -616,14 +673,17 @@ namespace MongoDbGenericRepository /// Options for creating an index. /// An optional partition key. /// The result of the create index operation. - Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions, string partitionKey) + 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. + /// 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. @@ -632,12 +692,16 @@ namespace MongoDbGenericRepository /// An optional partition key. /// The cancellation token. /// The result of the create index operation. - Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; /// - /// Drops the index given a field name + /// Drops the index given a field name /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -647,7 +711,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Drops the index given a field name + /// Drops the index given a field name /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -658,7 +722,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Drops the index given a field name + /// Drops the index given a field name /// /// The type representing a Document. /// The type of the primary key for a Document. @@ -669,7 +733,7 @@ namespace MongoDbGenericRepository where TKey : IEquatable; /// - /// Drops the index given a field name + /// Drops the index given a field name /// /// The type representing a Document. /// The type of the primary key for a Document. diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs index e2d8f39..5010c08 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs @@ -1,10 +1,10 @@ -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 { @@ -14,13 +14,16 @@ namespace MongoDbGenericRepository private IMongoDbEraser _mongoDbEraser; /// - /// The MongoDb accessor to delete data. + /// The MongoDb accessor to delete data. /// protected virtual IMongoDbEraser MongoDbEraser { get { - if (_mongoDbEraser != null) { return _mongoDbEraser; } + if (_mongoDbEraser != null) + { + return _mongoDbEraser; + } lock (_initLock) { @@ -29,9 +32,10 @@ namespace MongoDbGenericRepository _mongoDbEraser = new MongoDbEraser(MongoDbContext); } } + return _mongoDbEraser; } - set { _mongoDbEraser = value; } + set => _mongoDbEraser = value; } /// @@ -112,7 +116,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + public virtual async Task DeleteOneAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await MongoDbEraser.DeleteOneAsync(filter, partitionKey, cancellationToken); @@ -154,7 +161,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + public virtual async Task DeleteManyAsync( + Expression> filter, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await MongoDbEraser.DeleteManyAsync(filter, partitionKey, cancellationToken); @@ -178,21 +188,21 @@ namespace MongoDbGenericRepository public virtual long DeleteMany(Expression> filter) where TDocument : IDocument { - return DeleteMany(filter, null, CancellationToken.None); + return DeleteMany(filter, null, CancellationToken.None); } /// public virtual long DeleteMany(Expression> filter, CancellationToken cancellationToken) where TDocument : IDocument { - return DeleteMany(filter, null, cancellationToken); + return DeleteMany(filter, null, cancellationToken); } /// public virtual long DeleteMany(Expression> filter, string partitionKey) where TDocument : IDocument { - return DeleteMany(filter, partitionKey, CancellationToken.None); + return DeleteMany(filter, partitionKey, CancellationToken.None); } /// @@ -202,4 +212,4 @@ namespace MongoDbGenericRepository 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 a327965..485d899 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs @@ -298,14 +298,19 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + 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) + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) where TDocument : IDocument { return await CreateHashedIndexAsync(field, indexCreationOptions, null, cancellationToken); @@ -319,21 +324,31 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateHashedIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + 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) + 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) + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await MongoDbIndexHandler.CreateHashedIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); @@ -347,21 +362,28 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, CancellationToken cancellationToken) + 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) + 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) + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) where TDocument : IDocument { return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, null, cancellationToken); @@ -375,21 +397,31 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, string partitionKey, CancellationToken cancellationToken) + 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) + 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) + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await MongoDbIndexHandler.CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey, cancellationToken); diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Main.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Main.cs index eeca6ab..12b41fa 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Main.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Main.cs @@ -1,24 +1,23 @@ -using MongoDB.Driver; +using System; +using MongoDB.Driver; using MongoDbGenericRepository.Models; -using System; 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. /// /// 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. @@ -27,23 +26,23 @@ namespace MongoDbGenericRepository } /// - /// The constructor taking a . + /// The constructor taking a . /// - /// A mongodb context implementing + /// A mongodb context implementing protected BaseMongoRepository(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } /// - /// The constructor 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. @@ -55,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. @@ -70,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 4e5c741..2c670c5 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.ReadOnly.cs @@ -17,16 +17,6 @@ namespace MongoDbGenericRepository public abstract class ReadOnlyMongoRepository : IReadOnlyMongoRepository where TKey : IEquatable { - /// - /// The MongoDbContext - /// - protected IMongoDbContext MongoDbContext { get; set; } - - /// - /// A MongoDb Reader for read operations - /// - protected IMongoDbReader MongoDbReader { get; set; } - /// /// The constructor taking a connection string and a database name. /// @@ -55,6 +45,16 @@ namespace MongoDbGenericRepository SetupMongoDbContext(mongoDbContext); } + /// + /// The MongoDbContext + /// + protected IMongoDbContext MongoDbContext { get; set; } + + /// + /// A MongoDb Reader for read operations + /// + protected IMongoDbReader MongoDbReader { get; set; } + /// /// The connection string. /// diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs index 3d66778..1dd1870 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs @@ -1,10 +1,10 @@ -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 { @@ -14,13 +14,16 @@ namespace MongoDbGenericRepository private IMongoDbUpdater _mongoDbUpdater; /// - /// The MongoDb accessor to update data. + /// The MongoDb accessor to update data. /// protected virtual IMongoDbUpdater MongoDbUpdater { get { - if (_mongoDbUpdater != null) { return _mongoDbUpdater; } + if (_mongoDbUpdater != null) + { + return _mongoDbUpdater; + } lock (_initLock) { @@ -32,7 +35,7 @@ namespace MongoDbGenericRepository return _mongoDbUpdater; } - set { _mongoDbUpdater = value; } + set => _mongoDbUpdater = value; } /// @@ -71,7 +74,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken) + public virtual async Task UpdateOneAsync( + TDocument documentToModify, + UpdateDefinition update, + CancellationToken cancellationToken) where TDocument : IDocument { return await MongoDbUpdater.UpdateOneAsync(documentToModify, update, cancellationToken); @@ -99,7 +105,11 @@ namespace MongoDbGenericRepository } /// - public virtual bool UpdateOne(TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken) + public virtual bool UpdateOne( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { return MongoDbUpdater.UpdateOne(documentToModify, field, value, cancellationToken); @@ -113,7 +123,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken) + public virtual async Task UpdateOneAsync( + TDocument documentToModify, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { return await MongoDbUpdater.UpdateOneAsync(documentToModify, field, value, cancellationToken); @@ -127,21 +141,34 @@ namespace MongoDbGenericRepository } /// - public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, CancellationToken cancellationToken) + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { return UpdateOne(filter, field, value, null, cancellationToken); } /// - public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey) + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument { return UpdateOne(filter, field, value, partitionKey, CancellationToken.None); } /// - public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + public virtual bool UpdateOne( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey, cancellationToken); @@ -155,133 +182,210 @@ namespace MongoDbGenericRepository } /// - public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, CancellationToken cancellationToken) + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { return UpdateOne(filter, field, value, null, cancellationToken); } /// - public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey) + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument { return UpdateOne(filter, field, value, partitionKey, CancellationToken.None); } /// - public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + public virtual bool UpdateOne( + Expression> filter, + Expression> field, + TField value, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey, cancellationToken); } /// - public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value) + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value) where TDocument : IDocument { return await UpdateOneAsync(filter, field, value, null, CancellationToken.None); } /// - public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, CancellationToken cancellationToken) + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + CancellationToken cancellationToken) where TDocument : IDocument { return await UpdateOneAsync(filter, field, value, null, cancellationToken); } /// - public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey) + public virtual async Task UpdateOneAsync( + FilterDefinition filter, + Expression> field, + TField value, + string partitionKey) where TDocument : IDocument { return await UpdateOneAsync(filter, field, value, partitionKey, CancellationToken.None); } /// - public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey, CancellationToken cancellationToken) + 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) + 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) + 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) + 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) + 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) + 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) + 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) + 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) + 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) + 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) + 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) + 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) + 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); @@ -295,21 +399,31 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + 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) + 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) + public virtual async Task UpdateManyAsync( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey, cancellationToken); @@ -323,21 +437,31 @@ namespace MongoDbGenericRepository } /// - public virtual async Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + 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) + 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) + public virtual async Task UpdateManyAsync( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey, cancellationToken); @@ -351,21 +475,34 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(Expression> filter, Expression> field, TField value, CancellationToken cancellationToken) + 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) + 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) + 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); @@ -379,21 +516,34 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value, CancellationToken cancellationToken) + 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) + 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) + 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); @@ -407,7 +557,10 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + public virtual long UpdateMany( + FilterDefinition filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) where TDocument : IDocument { return UpdateMany(filter, updateDefinition, null, cancellationToken); @@ -421,7 +574,11 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey, CancellationToken cancellationToken) + public virtual long UpdateMany( + FilterDefinition filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey, cancellationToken); @@ -435,7 +592,10 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, CancellationToken cancellationToken) + public virtual long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + CancellationToken cancellationToken) where TDocument : IDocument { return UpdateMany(filter, updateDefinition, null, cancellationToken); @@ -449,7 +609,11 @@ namespace MongoDbGenericRepository } /// - public virtual long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey, CancellationToken cancellationToken) + public virtual long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey, cancellationToken); diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs index 883d7c9..32d269d 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs @@ -8,14 +8,14 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The interface exposing deletion functionality for Key typed repositories. + /// 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. + /// Deletes a document. /// /// The type representing a Document. /// The document you want to delete. @@ -24,7 +24,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Deletes a document. + /// Deletes a document. /// /// The type representing a Document. /// The document you want to delete. @@ -34,7 +34,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Deletes a document matching the condition of the LINQ expression filter. + /// Deletes a document matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. @@ -43,7 +43,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Deletes a document matching the condition of the LINQ expression filter. + /// Deletes a document matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. @@ -53,7 +53,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Deletes a document matching the condition of the LINQ expression filter. + /// Deletes a document matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. @@ -63,7 +63,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Deletes a document matching the condition of the LINQ expression filter. + /// Deletes a document matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. @@ -74,7 +74,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// The document you want to delete. @@ -83,7 +83,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// The document you want to delete. @@ -94,7 +94,7 @@ namespace MongoDbGenericRepository /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. @@ -103,7 +103,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. @@ -113,7 +113,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. @@ -123,7 +123,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. @@ -134,7 +134,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. @@ -143,7 +143,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. @@ -153,7 +153,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. @@ -163,7 +163,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. @@ -174,7 +174,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Asynchronously deletes a list of documents. + /// Asynchronously deletes a list of documents. /// /// The type representing a Document. /// The list of documents to delete. @@ -183,7 +183,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Asynchronously deletes a list of documents. + /// Asynchronously deletes a list of documents. /// /// The type representing a Document. /// The list of documents to delete. @@ -193,7 +193,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Deletes a list of documents. + /// Deletes a list of documents. /// /// The type representing a Document. /// The list of documents to delete. @@ -202,7 +202,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Deletes a list of documents. + /// Deletes a list of documents. /// /// The type representing a Document. /// The list of documents to delete. @@ -212,7 +212,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Deletes the documents matching the condition of the LINQ expression filter. + /// Deletes the documents matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. @@ -221,7 +221,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Deletes the documents matching the condition of the LINQ expression filter. + /// Deletes the documents matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. @@ -231,7 +231,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Deletes the documents matching the condition of the LINQ expression filter. + /// Deletes the documents matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. @@ -241,7 +241,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Deletes the documents matching the condition of the LINQ expression filter. + /// Deletes the documents matching the condition of the LINQ expression filter. /// /// The type representing a Document. /// A LINQ expression filter. diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs index cce8c25..f82e6fa 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs @@ -8,13 +8,14 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The interface exposing index management functionality for Key typed repositories. + /// The interface exposing index management functionality for Key typed repositories. /// /// - public interface IBaseMongoRepository_Index where TKey : IEquatable + public interface IBaseMongoRepository_Index + where TKey : IEquatable { /// - /// Returns the names of the indexes present on a collection. + /// 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. @@ -22,7 +23,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Returns the names of the indexes present on a collection. + /// Returns the names of the indexes present on a collection. /// /// The type representing a Document. /// The cancellation token. @@ -31,7 +32,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Returns the names of the indexes present on a collection. + /// Returns the names of the indexes present on a collection. /// /// The type representing a Document. /// An optional partition key @@ -40,7 +41,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Returns the names of the indexes present on a collection. + /// Returns the names of the indexes present on a collection. /// /// The type representing a Document. /// An optional partition key @@ -50,9 +51,9 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -61,9 +62,9 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -73,9 +74,9 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -85,22 +86,25 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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) + 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. + /// 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. @@ -110,9 +114,9 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -123,9 +127,9 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -136,9 +140,9 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Create a text index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -146,14 +150,18 @@ namespace MongoDbGenericRepository /// An optional partition key. /// The cancellation token. /// The result of the create index operation. - Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + 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. + /// 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. @@ -162,9 +170,9 @@ namespace MongoDbGenericRepository 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. + /// 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. @@ -174,9 +182,9 @@ namespace MongoDbGenericRepository 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. + /// 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. @@ -186,22 +194,25 @@ namespace MongoDbGenericRepository 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. + /// 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) + 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. + /// 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. @@ -211,9 +222,9 @@ namespace MongoDbGenericRepository 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. + /// 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. @@ -224,22 +235,25 @@ namespace MongoDbGenericRepository 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. + /// 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) + 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. + /// 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. @@ -247,13 +261,17 @@ namespace MongoDbGenericRepository /// An optional partition key. /// The cancellation token. /// The result of the create index operation. - Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + 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. + /// 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. @@ -262,9 +280,9 @@ namespace MongoDbGenericRepository 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. + /// 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. @@ -274,9 +292,9 @@ namespace MongoDbGenericRepository 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. + /// 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. @@ -286,22 +304,25 @@ namespace MongoDbGenericRepository 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. + /// 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) + 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. + /// 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. @@ -311,9 +332,9 @@ namespace MongoDbGenericRepository 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. + /// 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. @@ -324,22 +345,25 @@ namespace MongoDbGenericRepository 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. + /// 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) + 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. + /// 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. @@ -347,13 +371,17 @@ namespace MongoDbGenericRepository /// An optional partition key. /// The cancellation token. /// The result of the create index operation. - Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + 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. + /// 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. @@ -362,9 +390,9 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Creates a hashed index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -374,9 +402,9 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Creates a hashed index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -386,22 +414,25 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Creates a hashed index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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) + 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. + /// 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. @@ -411,9 +442,9 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Creates a hashed index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -424,22 +455,25 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Creates a hashed index on the given field. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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) + 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. + /// 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. @@ -447,13 +481,17 @@ namespace MongoDbGenericRepository /// An optional partition key. /// The cancellation token. /// The result of the create index operation. - Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + 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. + /// 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. @@ -462,9 +500,9 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Creates a combined text index. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -474,9 +512,9 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Creates a combined text index. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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. @@ -486,22 +524,25 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Creates a combined text index. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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) + 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. + /// 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. @@ -511,35 +552,41 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Creates a combined text index. - /// IndexCreationOptions can be supplied to further specify - /// how the creation should be done. + /// 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) + 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. + /// 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) + 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. + /// 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. @@ -547,11 +594,15 @@ namespace MongoDbGenericRepository /// An optional partition key. /// The cancellation token. /// The result of the create index operation. - Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument; /// - /// Drops the index given a field name + /// Drops the index given a field name /// /// The type representing a Document. /// The name of the index @@ -559,7 +610,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Drops the index given a field name + /// Drops the index given a field name /// /// The type representing a Document. /// The name of the index @@ -568,7 +619,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Drops the index given a field name + /// Drops the index given a field name /// /// The type representing a Document. /// The name of the index @@ -577,7 +628,7 @@ namespace MongoDbGenericRepository where TDocument : IDocument; /// - /// Drops the index given a field name + /// Drops the index given a field name /// /// The type representing a Document. /// The name of the index From 3a7b24262e2de342248099441b83d49ad6dbe8c5 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Thu, 6 Jul 2023 18:32:00 +0100 Subject: [PATCH 34/40] unit tests for repo update methods --- .../UpdateTests/UpdateManyAsyncTests.cs | 1055 +++++++++++++ .../UpdateTests/UpdateManyTests.cs | 1053 +++++++++++++ .../UpdateTests/UpdateOneAsyncTests.cs | 1318 +++++++++++++++++ .../UpdateTests/UpdateOneTests.cs | 1315 ++++++++++++++++ CoreUnitTests/CoreUnitTests.csproj | 4 - .../TestKeyedMongoRepository.cs | 3 + .../TestKeyedMongoRepositoryContext.cs | 9 + .../Infrastructure/TestMongoRepository.cs | 25 +- .../TestMongoRepositoryContext.cs | 10 +- .../UpdateTests/UpdateManyAsyncTests.cs | 533 +++++++ .../UpdateTests/UpdateManyTests.cs | 532 +++++++ .../UpdateTests/UpdateOneAsyncTests.cs | 432 ++++++ .../UpdateTests/UpdateOneTests.cs | 432 ++++++ .../DataAccess/Update/IMongoDbUpdater.cs | 17 + .../DataAccess/Update/MongoDbUpdater.cs | 14 + 15 files changed, 6730 insertions(+), 22 deletions(-) create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateManyAsyncTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateManyTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateOneAsyncTests.cs create mode 100644 CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateOneTests.cs create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateManyAsyncTests.cs create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateManyTests.cs create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateOneAsyncTests.cs create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateOneTests.cs 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 index 58aa65b..d417073 100644 --- a/CoreUnitTests/CoreUnitTests.csproj +++ b/CoreUnitTests/CoreUnitTests.csproj @@ -30,8 +30,4 @@ - - - - diff --git a/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs index 69f219d..b6e1377 100644 --- a/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs +++ b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs @@ -5,6 +5,7 @@ using MongoDbGenericRepository.DataAccess.Create; using MongoDbGenericRepository.DataAccess.Delete; using MongoDbGenericRepository.DataAccess.Index; using MongoDbGenericRepository.DataAccess.Read; +using MongoDbGenericRepository.DataAccess.Update; namespace CoreUnitTests.Infrastructure; @@ -23,4 +24,6 @@ public class TestKeyedMongoRepository : BaseMongoRepository 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 index 2b86c1a..91a988d 100644 --- a/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs +++ b/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs @@ -5,6 +5,7 @@ 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; @@ -54,6 +55,11 @@ public class TestKeyedMongoRepositoryContext sut.SetEraser(Eraser.Object); } + if (Updater != null) + { + sut.SetUpdater(Updater.Object); + } + return sut; } } @@ -65,4 +71,7 @@ public class TestKeyedMongoRepositoryContext protected Mock Reader { get; set; } protected Mock Eraser { get; set; } + + protected Mock Updater { get; set; } + } diff --git a/CoreUnitTests/Infrastructure/TestMongoRepository.cs b/CoreUnitTests/Infrastructure/TestMongoRepository.cs index b204f07..4252a4f 100644 --- a/CoreUnitTests/Infrastructure/TestMongoRepository.cs +++ b/CoreUnitTests/Infrastructure/TestMongoRepository.cs @@ -4,6 +4,7 @@ using MongoDbGenericRepository.DataAccess.Create; using MongoDbGenericRepository.DataAccess.Delete; using MongoDbGenericRepository.DataAccess.Index; using MongoDbGenericRepository.DataAccess.Read; +using MongoDbGenericRepository.DataAccess.Update; namespace CoreUnitTests.Infrastructure; @@ -14,23 +15,13 @@ public class TestMongoRepository : BaseMongoRepository { } - public void SetIndexHandler(IMongoDbIndexHandler indexHandler) - { - MongoDbIndexHandler = indexHandler; - } + public void SetIndexHandler(IMongoDbIndexHandler indexHandler) => MongoDbIndexHandler = indexHandler; - public void SetDbCreator(IMongoDbCreator creator) - { - MongoDbCreator = creator; - } + public void SetDbCreator(IMongoDbCreator creator) => MongoDbCreator = creator; - public void SetReader(IMongoDbReader reader) - { - MongoDbReader = reader; - } + public void SetReader(IMongoDbReader reader) => MongoDbReader = reader; - public void SetEraser(IMongoDbEraser eraser) - { - MongoDbEraser = eraser; - } -} \ No newline at end of file + 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 index 6dea5f0..c335e13 100644 --- a/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs +++ b/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs @@ -4,6 +4,7 @@ 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; @@ -48,6 +49,11 @@ public class TestMongoRepositoryContext { _sut.SetEraser(Eraser.Object); } + + if (Updater != null) + { + _sut.SetUpdater(Updater.Object); + } } return _sut; @@ -61,4 +67,6 @@ public class TestMongoRepositoryContext protected Mock Reader { get; set; } protected Mock Eraser { get; set; } -} \ No newline at end of file + + protected Mock Updater { get; set; } +} 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/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs b/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs index fa15af7..757606d 100644 --- a/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs +++ b/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs @@ -504,5 +504,22 @@ namespace MongoDbGenericRepository.DataAccess.Update 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.cs b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs index ebb74ef..7529b16 100644 --- a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs +++ b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs @@ -253,5 +253,19 @@ namespace MongoDbGenericRepository.DataAccess.Update 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 From 67784e64eebe1ba23d2caffb7ee00d21026b3ccb Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Thu, 6 Jul 2023 19:15:10 +0100 Subject: [PATCH 35/40] getandupdate and getpaginated --- .../Abstractions/IBaseMongoRepository.cs | 43 ++++++++++- .../BaseMongoRepository.Main.cs | 77 +++++++++---------- 2 files changed, 77 insertions(+), 43 deletions(-) diff --git a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs index 72009fa..a35c5c1 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using MongoDB.Driver; using MongoDbGenericRepository.Models; @@ -25,11 +26,13 @@ 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> GetPaginatedAsync( Expression> filter, int skipNumber = 0, int takeNumber = 50, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument; /// @@ -41,11 +44,13 @@ 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> GetPaginatedAsync( Expression> filter, int skipNumber = 0, int takeNumber = 50, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -63,6 +68,22 @@ namespace MongoDbGenericRepository FindOneAndUpdateOptions options) where TDocument : IDocument; + /// + /// 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 /// @@ -78,5 +99,23 @@ namespace MongoDbGenericRepository 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/BaseMongoRepository.Main.cs b/MongoDbGenericRepository/BaseMongoRepository.Main.cs index fdbf67f..8595cb4 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Main.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Main.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using MongoDB.Driver; using MongoDbGenericRepository.Models; @@ -39,42 +40,29 @@ namespace MongoDbGenericRepository { } - /// - /// 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) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await HandlePartitioned(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(); + return await HandlePartitioned(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(cancellationToken); } /// @@ -150,33 +138,28 @@ namespace MongoDbGenericRepository } #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 GetAndUpdateOne(filter, update, options, CancellationToken.None); } - /// - /// 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, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await GetCollection().FindOneAndUpdateAsync(filter, update, options, cancellationToken); + } + + /// public virtual async Task GetAndUpdateOne( FilterDefinition filter, UpdateDefinition update, @@ -184,7 +167,19 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable { - return await GetCollection().FindOneAndUpdateAsync(filter, update, options); + 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 From cb55d3bffa66cfa192aee3904f2b1af328cf09ba Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Mon, 17 Jul 2023 18:37:17 +0100 Subject: [PATCH 36/40] removing empty test classes --- .../MainTests/AnyTests.cs | 34 ------------------- .../MainTests/CountTests.cs | 34 ------------------- 2 files changed, 68 deletions(-) delete mode 100644 CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs delete mode 100644 CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs diff --git a/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs deleted file mode 100644 index 7d8c18d..0000000 --- a/CoreUnitTests/BaseMongoRepositoryTests/MainTests/AnyTests.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using CoreUnitTests.Infrastructure; -using CoreUnitTests.Infrastructure.Model; -using MongoDB.Driver; -using MongoDbGenericRepository.DataAccess.Read; -using Moq; -using Xunit; - -namespace CoreUnitTests.BaseMongoRepositoryTests.MainTests; - -public class AnyTests : TestMongoRepositoryContext -{ - /*[Fact] - public async Task AnyAsync_EnsureTokenPassed() - { - // Arrange - var token = new CancellationToken(true); - - Reader = new Mock(); - Reader - .Setup(x => x.AnyAsync(It.IsAny>(), null, null, token)) - .ReturnsAsync(true); - - // Act - await Sut.AnyAsync(t => string.IsNullOrWhiteSpace(t.SomeContent2), token); - - // Assert - Reader - .Verify(x => x.AnyAsync( - t => string.IsNullOrWhiteSpace(t.SomeContent2), null, token)); - }*/ -} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs deleted file mode 100644 index 7c5c0c2..0000000 --- a/CoreUnitTests/BaseMongoRepositoryTests/MainTests/CountTests.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; -using CoreUnitTests.Infrastructure; -using CoreUnitTests.Infrastructure.Model; -using MongoDbGenericRepository.DataAccess.Read; -using Moq; -using Xunit; - -namespace CoreUnitTests.BaseMongoRepositoryTests.MainTests; - -public class CountTests : TestMongoRepositoryContext -{ - /*[Fact] - public async Task CountAsync_EnsureTokenPassed() - { - // Arrange - var token = new CancellationToken(true); - - Reader = new Mock(); - Reader - .Setup(x => x.CountAsync(It.IsAny>>(), null, token)) - .ReturnsAsync(10); - - // Act - var result = await Sut.CountAsync(t => string.IsNullOrWhiteSpace(t.SomeContent2), token); - - // Assert - Assert.Equal(10, result); - Reader.Verify(x => x.CountAsync( - t => string.IsNullOrWhiteSpace(t.SomeContent2), null, token)); - }*/ -} From 7bbbd7bd714fb71d5c4b22c454b4e3569a612526 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Tue, 18 Jul 2023 08:55:06 +0100 Subject: [PATCH 37/40] reverting target frameworks change --- MongoDbGenericRepository/MongoDbGenericRepository.csproj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MongoDbGenericRepository/MongoDbGenericRepository.csproj b/MongoDbGenericRepository/MongoDbGenericRepository.csproj index 45c60c1..a28fe63 100644 --- a/MongoDbGenericRepository/MongoDbGenericRepository.csproj +++ b/MongoDbGenericRepository/MongoDbGenericRepository.csproj @@ -1,7 +1,8 @@  - netstandard2.1;net48 + + net472;netstandard2.0; true MongoDbGenericRepository 1.4.8 From 091ee9c20460d186045b95a9469456c2abce98f9 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Tue, 18 Jul 2023 08:56:03 +0100 Subject: [PATCH 38/40] removing comment --- MongoDbGenericRepository/MongoDbGenericRepository.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/MongoDbGenericRepository/MongoDbGenericRepository.csproj b/MongoDbGenericRepository/MongoDbGenericRepository.csproj index a28fe63..aeeb853 100644 --- a/MongoDbGenericRepository/MongoDbGenericRepository.csproj +++ b/MongoDbGenericRepository/MongoDbGenericRepository.csproj @@ -1,7 +1,6 @@  - net472;netstandard2.0; true MongoDbGenericRepository From 8f2f92f9d36a18ffb7b936fc226bca850068bd70 Mon Sep 17 00:00:00 2001 From: Alexandre SPIESER Date: Tue, 18 Jul 2023 23:01:21 +0100 Subject: [PATCH 39/40] remove dotnet-xunit bet nuget package, update xunit packages --- .../CoreIntegrationTests.csproj | 17 +++++++---------- CoreUnitTests/CoreUnitTests.csproj | 12 ++++++++---- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/CoreIntegrationTests/CoreIntegrationTests.csproj b/CoreIntegrationTests/CoreIntegrationTests.csproj index 49ae519..dbb6dac 100644 --- a/CoreIntegrationTests/CoreIntegrationTests.csproj +++ b/CoreIntegrationTests/CoreIntegrationTests.csproj @@ -8,19 +8,16 @@ - - + + 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/CoreUnitTests/CoreUnitTests.csproj b/CoreUnitTests/CoreUnitTests.csproj index d417073..54fd87a 100644 --- a/CoreUnitTests/CoreUnitTests.csproj +++ b/CoreUnitTests/CoreUnitTests.csproj @@ -13,12 +13,16 @@ - + - - - runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive runtime; build; native; contentfiles; analyzers; buildtransitive From 5ff1c217807f3d25af35d24fa7f8d4fd83116677 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Wed, 19 Jul 2023 11:21:46 +0100 Subject: [PATCH 40/40] removed redundant ToList() calls and added line break after region --- MongoDbGenericRepository/BaseMongoRepository.Main.cs | 1 + MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/MongoDbGenericRepository/BaseMongoRepository.Main.cs b/MongoDbGenericRepository/BaseMongoRepository.Main.cs index 8595cb4..b382c23 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Main.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Main.cs @@ -138,6 +138,7 @@ namespace MongoDbGenericRepository } #region Find And Update + /// public virtual async Task GetAndUpdateOne( FilterDefinition filter, diff --git a/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs b/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs index 4be9bf1..b09b505 100644 --- a/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs +++ b/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs @@ -69,7 +69,7 @@ namespace MongoDbGenericRepository.DataAccess.Create } else { - await GetCollection().InsertManyAsync(documentsList.ToList(), null, cancellationToken); + await GetCollection().InsertManyAsync(documentsList, null, cancellationToken); } } @@ -100,7 +100,7 @@ namespace MongoDbGenericRepository.DataAccess.Create } else { - GetCollection().InsertMany(documentList.ToList(), cancellationToken: cancellationToken); + GetCollection().InsertMany(documentList, cancellationToken: cancellationToken); } }