├── .gitignore
├── CoreIntegrationTests
├── App.config
├── CRUDObjectIdTests.cs
├── CRUDPartitionedCollectionNameAttributeTests.cs
├── CRUDPartitionedTests.cs
├── CRUDTKeyPartitionedCollectionNameAttributeTests.cs
├── CRUDTKeyPartitionedTests.cs
├── CRUDTKeyTests.cs
├── CRUDTests.cs
├── CoreIntegrationTests.csproj
├── IdentityUserTests.cs
└── Infrastructure
│ ├── BaseMongoDbRepositoryTests.cs
│ ├── GlobalVariables.cs
│ ├── ITestRepository.cs
│ ├── MongoDbConfig.cs
│ ├── MongoDbDocumentTestBase.Main.cs
│ ├── MongoDbDocumentTestBase.Update.cs
│ ├── MongoDbTKeyDocumentTestBase.Main.cs
│ ├── MongoDbTKeyDocumentTestBase.Update.cs
│ ├── MongoDbTestFixture.cs
│ ├── RandomExtensions.cs
│ ├── TestClasses.cs
│ └── TestRepository.cs
├── CoreUnitTests
├── .editorconfig
├── BaseMongoRepositoryTests
│ ├── AddTests
│ │ ├── AddManyAsyncTests.cs
│ │ ├── AddManyTests.cs
│ │ ├── AddOneAsyncTests.cs
│ │ └── AddOneTests.cs
│ ├── DeleteTests
│ │ ├── DeleteManyAsyncTests.cs
│ │ ├── DeleteManyTests.cs
│ │ ├── DeleteOneAsyncTests.cs
│ │ └── DeleteOneTests.cs
│ ├── IndexTests
│ │ ├── BaseIndexTests.cs
│ │ ├── CreateAscendingIndexAsyncTests.cs
│ │ ├── CreateCombinedTextIndexAsyncTests.cs
│ │ ├── CreateDescendingIndexAsyncTests.cs
│ │ ├── CreateHashedIndexAsyncTests.cs
│ │ ├── CreateTextIndexAsyncTests.cs
│ │ ├── DropIndexAsyncTests.cs
│ │ └── GetIndexNamesAsyncTests.cs
│ └── UpdateTests
│ │ ├── UpdateManyAsyncTests.cs
│ │ ├── UpdateManyTests.cs
│ │ ├── UpdateOneAsyncTests.cs
│ │ └── UpdateOneTests.cs
├── CoreUnitTests.csproj
├── DataAccessTests
│ ├── MongoDbCreatorTests
│ │ ├── AddManyAsyncTests.cs
│ │ ├── AddManyTests.cs
│ │ ├── AddOneAsyncTests.cs
│ │ └── AddOneTests.cs
│ ├── MongoDbEraserTests
│ │ ├── DeleteManyAsyncTests.cs
│ │ ├── DeleteManyTests.cs
│ │ ├── DeleteOneAsyncTests.cs
│ │ └── DeleteOneTests.cs
│ ├── MongoDbIndexHandlerTests
│ │ ├── BaseIndexTests.cs
│ │ ├── CreateAscendingIndexAsyncTests.cs
│ │ ├── CreateCombinedTextIndexAsyncTests.cs
│ │ ├── CreateDescendingIndexAsyncTests.cs
│ │ ├── CreateHashedIndexAsyncTests.cs
│ │ ├── CreateTextIndexAsyncTests.cs
│ │ ├── DropIndexAsyncTests.cs
│ │ └── GetIndexNamesAsyncTests.cs
│ ├── MongoDbReaderTests
│ │ ├── AnyAsyncTests.cs
│ │ ├── AnyTests.cs
│ │ ├── BaseReaderTests.cs
│ │ ├── CountAsyncTests.cs
│ │ ├── CountTests.cs
│ │ ├── GetAllAsyncTests.cs
│ │ ├── GetAllTests.cs
│ │ ├── GetByIdAsyncTests.cs
│ │ ├── GetByIdTests.cs
│ │ ├── GetByMaxAsyncTests.cs
│ │ ├── GetByMaxTests.cs
│ │ ├── GetByMinAsyncTests.cs
│ │ ├── GetByMinTests.cs
│ │ ├── GetMaxValueAsyncTests .cs
│ │ ├── GetMaxValueTests .cs
│ │ ├── GetMinValueAsyncTests .cs
│ │ ├── GetMinValueTests .cs
│ │ ├── GetOneAsyncTests.cs
│ │ ├── GetOneTests.cs
│ │ ├── ProjectManyAsyncTests.cs
│ │ ├── ProjectManyTests.cs
│ │ ├── ProjectOneAsyncTests.cs
│ │ └── ProjectOneTests.cs
│ └── MongoDbUpdaterTests
│ │ ├── UpdateManyAsyncTests.cs
│ │ ├── UpdateManyTests.cs
│ │ ├── UpdateOneAsyncTests.cs
│ │ └── UpdateOneTests.cs
├── Infrastructure
│ ├── FilterDefinitionExtensions.cs
│ ├── GenericTestContext.cs
│ ├── IndexExtensions.cs
│ ├── Model
│ │ ├── Child.cs
│ │ ├── Nested.cs
│ │ ├── PartitionedTestDocument.cs
│ │ ├── TestDocument.cs
│ │ ├── TestDocumentWithKey.cs
│ │ └── TestProjection.cs
│ ├── TestKeyedMongoRepository.cs
│ ├── TestKeyedMongoRepositoryContext.cs
│ ├── TestKeyedReadOnlyMongoRepository.cs
│ ├── TestKeyedReadOnlyMongoRepositoryContext.cs
│ ├── TestMongoRepository.cs
│ ├── TestMongoRepositoryContext.cs
│ ├── TestReadOnlyMongoRepository.cs
│ ├── TestReadOnlyMongoRepositoryContext.cs
│ └── UpdateDefinitionExtensions.cs
├── KeyTypedRepositoryTests
│ ├── AddTests
│ │ ├── AddManyAsyncTests.cs
│ │ ├── AddManyTests.cs
│ │ ├── AddOneAsyncTests.cs
│ │ └── AddOneTests.cs
│ ├── DeleteTests
│ │ ├── DeleteManyAsyncTests.cs
│ │ ├── DeleteManyTests.cs
│ │ ├── DeleteOneAsyncTests.cs
│ │ └── DeleteOneTests.cs
│ ├── IndexTests
│ │ ├── CreateAscendingIndexAsyncTests.cs
│ │ ├── CreateCombinedTextIndexAsyncTests.cs
│ │ ├── CreateDescendingIndexAsyncTests.cs
│ │ ├── CreateHashedIndexAsyncTests.cs
│ │ ├── CreateTextIndexAsyncTests.cs
│ │ ├── DropIndexAsyncTests.cs
│ │ └── GetIndexNamesAsyncTests.cs
│ └── UpdateTests
│ │ ├── UpdateManyAsyncTests.cs
│ │ ├── UpdateManyTests.cs
│ │ ├── UpdateOneAsyncTests.cs
│ │ └── UpdateOneTests.cs
├── KeyedReadOnlyMongoRepositoryTests
│ ├── AnyAsyncTests.cs
│ ├── AnyTests.cs
│ ├── CountAsyncTests.cs
│ ├── CountTests.cs
│ ├── GetAllAsyncTests.cs
│ ├── GetAllTests.cs
│ ├── GetByIdAsyncTests.cs
│ ├── GetByIdTests.cs
│ ├── GetByMaxAsyncTests.cs
│ ├── GetByMaxTests.cs
│ ├── GetByMinAsyncTests.cs
│ ├── GetByMinTests.cs
│ ├── GetMaxValueAsyncTests.cs
│ ├── GetMaxValueTests.cs
│ ├── GetMinValueAsyncTests.cs
│ ├── GetMinValueTests.cs
│ ├── GetOneAsyncTests.cs
│ ├── GetOneTests.cs
│ ├── GetSortedPaginatedAsyncTests.cs
│ ├── GroupByTests.cs
│ ├── ProjectManyAsyncTests.cs
│ ├── ProjectManyTests.cs
│ ├── ProjectOneAsyncTests.cs
│ ├── ProjectOneTests.cs
│ └── SumByAsyncTests.cs
└── ReadOnlyMongoRepositoryTests
│ ├── AnyAsyncTests.cs
│ ├── AnyTests.cs
│ ├── CountAsyncTests.cs
│ ├── CountTests.cs
│ ├── GetAllAsyncTests.cs
│ ├── GetAllTests.cs
│ ├── GetByIdAsyncTests.cs
│ ├── GetByIdTests.cs
│ ├── GetByMaxAsyncTests.cs
│ ├── GetByMaxTests.cs
│ ├── GetByMinAsyncTests.cs
│ ├── GetByMinTests.cs
│ ├── GetMaxValueAsyncTests.cs
│ ├── GetMaxValueTests.cs
│ ├── GetMinValueAsyncTests.cs
│ ├── GetMinValueTests.cs
│ ├── GetOneAsyncTests.cs
│ ├── GetOneTests.cs
│ ├── GetSortedPaginatedAsyncTests.cs
│ ├── GroupByTests.cs
│ ├── ProjectManyAsyncTests.cs
│ ├── ProjectManyTests.cs
│ ├── ProjectOneAsyncTests.cs
│ ├── ProjectOneTests.cs
│ └── SumByAsyncTests.cs
├── IntegrationTests
├── App.config
├── CRUDObjectIdTests.cs
├── CRUDPartitionedCollectionNameAttributeTests.cs
├── CRUDPartitionedTests.cs
├── CRUDTKeyPartitionedCollectionNameAttributeTests.cs
├── CRUDTKeyPartitionedTests.cs
├── CRUDTKeyTests.cs
├── CRUDTests.cs
├── Infrastructure
│ ├── BaseMongoDbRepositoryTests.cs
│ ├── GlobalVariables.cs
│ ├── ITestRepository.cs
│ ├── MongoDBDocumentTestBase.cs
│ ├── MongoDbTKeyDocumentTestBase.cs
│ ├── RandomExtensions.cs
│ ├── TestClasses.cs
│ └── TestRepository.cs
├── IntegrationTests.csproj
├── Properties
│ └── AssemblyInfo.cs
├── libmongocrypt.dylib
├── libmongocrypt.so
├── mongocrypt.dll
└── packages.config
├── LICENSE
├── MongoDbGenericRepository.sln
├── MongoDbGenericRepository
├── Abstractions
│ ├── IBaseMongoRepository.cs
│ ├── IBaseMongoRepository_Update.cs
│ ├── IBaseMongoRepository_Update_ClientSession.cs
│ ├── IBaseReadOnlyRepository.cs
│ ├── IMongoDbContext.cs
│ ├── IReadOnlyMongoRepository.TKey.cs
│ └── IReadOnlyMongoRepository.cs
├── Attributes
│ └── CollectionNameAttribute.cs
├── BaseMongoRepository.Create.cs
├── BaseMongoRepository.Delete.cs
├── BaseMongoRepository.Index.cs
├── BaseMongoRepository.Main.cs
├── BaseMongoRepository.Update.ClientSession.cs
├── BaseMongoRepository.Update.cs
├── DataAccess
│ ├── Base
│ │ ├── DataAccessBase.cs
│ │ └── IDataAccessBase.cs
│ ├── Create
│ │ ├── IMongoDbCreator.cs
│ │ └── MongoDbCreator.cs
│ ├── Delete
│ │ ├── IMongoDbEraser.cs
│ │ └── MongoDbEraser.cs
│ ├── Index
│ │ ├── IMongoDbIndexHandler.cs
│ │ └── MongoDbIndexHandler.cs
│ ├── Read
│ │ ├── IMongoDbReader.cs
│ │ ├── MongoDbReader.GroupBy.cs
│ │ ├── MongoDbReader.Main.cs
│ │ └── MongoDbReader.Project.cs
│ └── Update
│ │ ├── IMongoDbUpdater.cs
│ │ ├── MongoDbUpdater.ClientSession.cs
│ │ └── MongoDbUpdater.cs
├── IBaseMongoRepository.Create.cs
├── IBaseMongoRepository.Delete.cs
├── IBaseMongoRepository.Index.cs
├── Internals
│ └── RepositorySerializationProvider.cs
├── KeyTypedRepository
│ ├── BaseMongoRepository.TKey.Create.cs
│ ├── BaseMongoRepository.TKey.Delete.cs
│ ├── BaseMongoRepository.TKey.Index.cs
│ ├── BaseMongoRepository.TKey.Main.cs
│ ├── BaseMongoRepository.TKey.ReadOnly.cs
│ ├── BaseMongoRepository.TKey.Update.cs
│ ├── IBaseMongoRepository.TKey.Create.cs
│ ├── IBaseMongoRepository.TKey.Delete.cs
│ ├── IBaseMongoRepository.TKey.Index.cs
│ ├── IBaseMongoRepository.TKey.Update.cs
│ └── IBaseMongoRepository.TKey.cs
├── Models
│ ├── Document.cs
│ ├── IDocument.cs
│ ├── IPartitionedDocument.cs
│ ├── IndexCreationOptions.cs
│ └── PartitionedDocument.cs
├── MongoDbContext.cs
├── MongoDbGenericRepository.csproj
├── MongoDbGenericRepository.csproj.user
├── MongoDbGenericRepository.xml
├── ReadOnlyMongoRepository.cs
├── Utils
│ ├── IdGenerator.cs
│ ├── Pluralization.cs
│ └── RandomExtensions.cs
└── _rels
│ └── .rels
└── README.md
/CoreIntegrationTests/App.config:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CoreIntegrationTests/CRUDObjectIdTests.cs:
--------------------------------------------------------------------------------
1 | using CoreIntegrationTests.Infrastructure;
2 | using MongoDB.Bson;
3 |
4 | namespace CoreIntegrationTests
5 | {
6 | public class CoreObjectIdTestDocument : TestDoc
7 | {
8 | }
9 |
10 | public class CRUDObjectIdTests : MongoDbTKeyDocumentTestBase
11 | {
12 | public CRUDObjectIdTests(MongoDbTestFixture fixture) : base(fixture)
13 | {
14 |
15 | }
16 |
17 | public override string GetClassName()
18 | {
19 | return "CRUDObjectIdTests";
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/CoreIntegrationTests/CRUDPartitionedCollectionNameAttributeTests.cs:
--------------------------------------------------------------------------------
1 | using CoreIntegrationTests.Infrastructure;
2 | using MongoDbGenericRepository.Attributes;
3 | using MongoDbGenericRepository.Models;
4 | using System;
5 |
6 | namespace CoreIntegrationTests
7 | {
8 | [CollectionName("CoreTestingCNameAttrPart")]
9 | public class CorePartitionedCollectionNameDoc : TestDoc, IPartitionedDocument
10 | {
11 | public CorePartitionedCollectionNameDoc()
12 | {
13 | PartitionKey = "CoreTestPartitionKeyCollectionName";
14 | }
15 |
16 | public string PartitionKey { get; set; }
17 | }
18 |
19 | public class CRUDPartitionedCollectionNameAttributeTests : MongoDbDocumentTestBase
20 | {
21 | public CRUDPartitionedCollectionNameAttributeTests(MongoDbTestFixture fixture) : base(fixture)
22 | {
23 |
24 | }
25 |
26 | public override string GetClassName()
27 | {
28 | return "CoreCRUDPartitionedCollectionNameAttributeTests";
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/CoreIntegrationTests/CRUDPartitionedTests.cs:
--------------------------------------------------------------------------------
1 | using CoreIntegrationTests.Infrastructure;
2 | using MongoDbGenericRepository.Models;
3 | using System;
4 |
5 | namespace CoreIntegrationTests
6 | {
7 | public class CorePartitionedDoc : TestDoc, IPartitionedDocument
8 | {
9 | public CorePartitionedDoc()
10 | {
11 | PartitionKey = "CoreTestPartitionKey";
12 | }
13 |
14 | public string PartitionKey { get; set; }
15 | }
16 |
17 | public class CRUDPartitionedTests : MongoDbDocumentTestBase
18 | {
19 | public CRUDPartitionedTests(MongoDbTestFixture fixture) : base(fixture)
20 | {
21 | }
22 |
23 | public override string GetClassName()
24 | {
25 | return "CoreCRUDPartitionedTests";
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/CoreIntegrationTests/CRUDTKeyPartitionedCollectionNameAttributeTests.cs:
--------------------------------------------------------------------------------
1 | using CoreIntegrationTests.Infrastructure;
2 | using MongoDB.Bson;
3 | using MongoDbGenericRepository.Attributes;
4 | using MongoDbGenericRepository.Models;
5 | using System;
6 |
7 | namespace CoreIntegrationTests
8 | {
9 | #region Guid Type
10 |
11 | [CollectionName("TestingCNameAttrPartTKey")]
12 | public class CoreTKeyPartitionedCollectionNameDoc : TestDoc, IPartitionedDocument
13 | {
14 | public CoreTKeyPartitionedCollectionNameDoc()
15 | {
16 | PartitionKey = "CoreTestPartitionKey";
17 | }
18 |
19 | public string PartitionKey { get; set; }
20 | }
21 |
22 | public class CRUDTKeyPartitionedCollectionNameAttributeTests : MongoDbTKeyDocumentTestBase
23 | {
24 | public CRUDTKeyPartitionedCollectionNameAttributeTests(MongoDbTestFixture fixture) : base(fixture)
25 | {
26 |
27 | }
28 |
29 | public override string GetClassName()
30 | {
31 | return "CoreCRUDTKeyPartitionedCollectionNameAttributeTests";
32 | }
33 | }
34 |
35 | #endregion Guid Type
36 |
37 |
38 | #region ObjectId Type
39 |
40 | [CollectionName("TestingCNameAttrPartObjectId")]
41 | public class CoreObjectIdPartitionedCollectionNameDoc : TestDoc, IPartitionedDocument
42 | {
43 | public CoreObjectIdPartitionedCollectionNameDoc()
44 | {
45 | PartitionKey = "CoreTestPartitionKeyObjectId";
46 | }
47 |
48 | public string PartitionKey { get; set; }
49 | }
50 |
51 | public class CRUDObjectIdPartitionedCollectionNameAttributeTests : MongoDbTKeyDocumentTestBase
52 | {
53 | public CRUDObjectIdPartitionedCollectionNameAttributeTests(MongoDbTestFixture fixture) : base(fixture)
54 | {
55 |
56 | }
57 |
58 | public override string GetClassName()
59 | {
60 | return "CoreCRUDTKeyPartitionedCollectionNameAttributeTests";
61 | }
62 | }
63 |
64 | #endregion ObjectId Type
65 | }
66 |
--------------------------------------------------------------------------------
/CoreIntegrationTests/CRUDTKeyPartitionedTests.cs:
--------------------------------------------------------------------------------
1 | using CoreIntegrationTests.Infrastructure;
2 | using MongoDbGenericRepository.Models;
3 | using System;
4 |
5 | namespace CoreIntegrationTests
6 | {
7 | public class CorePartitionedTKeyTestDocument : TestDoc, IPartitionedDocument
8 | {
9 | public CorePartitionedTKeyTestDocument()
10 | {
11 | PartitionKey = "CoreTestPartitionKey";
12 | }
13 | public string PartitionKey { get; set; }
14 | }
15 |
16 | public class CRUDTKeyPartitionedTests : MongoDbTKeyDocumentTestBase
17 | {
18 | public CRUDTKeyPartitionedTests(MongoDbTestFixture fixture) : base(fixture)
19 | {
20 |
21 | }
22 |
23 | public override string GetClassName()
24 | {
25 | return "CoreCRUDTKeyPartitionedTests";
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/CoreIntegrationTests/CRUDTKeyTests.cs:
--------------------------------------------------------------------------------
1 | using CoreIntegrationTests.Infrastructure;
2 | using System;
3 |
4 | namespace CoreIntegrationTests
5 | {
6 | public class CoreTKeyTestDocument : TestDoc
7 | {
8 | }
9 |
10 | public class CRUDTKeyTests : MongoDbTKeyDocumentTestBase
11 | {
12 | public CRUDTKeyTests(MongoDbTestFixture fixture) : base(fixture)
13 | {
14 |
15 | }
16 |
17 | public override string GetClassName()
18 | {
19 | return "CreateTKeyTests";
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/CoreIntegrationTests/CRUDTests.cs:
--------------------------------------------------------------------------------
1 | using CoreIntegrationTests.Infrastructure;
2 | using System;
3 |
4 | namespace CoreIntegrationTests
5 | {
6 | public class CoreTestDocument : TestDoc
7 | {
8 | }
9 |
10 | public class CRUDTests : MongoDbDocumentTestBase
11 | {
12 | public CRUDTests(MongoDbTestFixture fixture) : base(fixture)
13 | {
14 | }
15 |
16 | public override string GetClassName()
17 | {
18 | return "CRUDTests";
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/CoreIntegrationTests/CoreIntegrationTests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net8.0
5 | disable
6 | enable
7 |
8 |
9 |
10 |
11 |
12 |
13 | all
14 | runtime; build; native; contentfiles; analyzers
15 |
16 |
17 | all
18 | runtime; build; native; contentfiles; analyzers; buildtransitive
19 |
20 |
21 |
22 |
23 |
24 |
25 | Always
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/CoreIntegrationTests/IdentityUserTests.cs:
--------------------------------------------------------------------------------
1 | using CoreIntegrationTests.Infrastructure;
2 | using MongoDbGenericRepository.Models;
3 | using System;
4 | using System.Collections.Generic;
5 | using Xunit;
6 | using Microsoft.AspNetCore.Identity;
7 | using System.Threading.Tasks;
8 |
9 | namespace CoreIntegrationTests
10 | {
11 | public class MongoIdentityUser : IdentityUser, IDocument
12 | where TKey : IEquatable
13 | {
14 | public int Version { get; set; }
15 | }
16 |
17 | public class IdentityUserTest : MongoIdentityUser, IDocument
18 | {
19 | public IdentityUserTest()
20 | {
21 | Id = Guid.NewGuid();
22 | Version = 2;
23 | }
24 | public string SomeContent { get; set; }
25 | }
26 |
27 | public class IdentityUserTests : BaseMongoDbRepositoryTests
28 | {
29 | [Fact]
30 | public void AddOne()
31 | {
32 | // Arrange
33 | var document = new IdentityUserTest();
34 | // Act
35 | SUT.AddOne(document);
36 | // Assert
37 | long count = SUT.Count(e => e.Id == document.Id);
38 | Assert.Equal(1, count);
39 | }
40 |
41 | [Fact]
42 | public async Task AddOneAsync()
43 | {
44 | // Arrange
45 | var document = new IdentityUserTest();
46 | // Act
47 | await SUT.AddOneAsync(document);
48 | // Assert
49 | long count = SUT.Count(e => e.Id == document.Id);
50 | Assert.Equal(1, count);
51 | }
52 |
53 | [Fact]
54 | public void AddMany()
55 | {
56 | // Arrange
57 | var documents = new List { new IdentityUserTest(), new IdentityUserTest() };
58 | // Act
59 | SUT.AddMany(documents);
60 | // Assert
61 | long count = SUT.Count(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
62 | Assert.Equal(2, count);
63 | }
64 |
65 | [Fact]
66 | public async Task AddManyAsync()
67 | {
68 | // Arrange
69 | var documents = new List { new IdentityUserTest(), new IdentityUserTest() };
70 | // Act
71 | await SUT.AddManyAsync(documents);
72 | // Assert
73 | long count = SUT.Count(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
74 | Assert.Equal(2, count);
75 | }
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/CoreIntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs:
--------------------------------------------------------------------------------
1 | using MongoDbGenericRepository.Models;
2 | using System.Collections.Generic;
3 | using System;
4 |
5 | namespace CoreIntegrationTests.Infrastructure
6 | {
7 |
8 | public class BaseMongoDbRepositoryTests : IDisposable where T : new()
9 | {
10 | public T CreateTestDocument()
11 | {
12 | return new T();
13 | }
14 |
15 | public List CreateTestDocuments(int numberOfDocumentsToCreate)
16 | {
17 | var docs = new List();
18 | for(var i = 0; i < numberOfDocumentsToCreate; i++)
19 | {
20 | docs.Add(new T());
21 | }
22 | return docs;
23 | }
24 |
25 | ///
26 | /// Constructor, init code
27 | ///
28 | public BaseMongoDbRepositoryTests()
29 | {
30 | Init();
31 | var type = CreateTestDocument();
32 | if (type is IPartitionedDocument)
33 | {
34 | PartitionKey = ((IPartitionedDocument)type).PartitionKey;
35 | }
36 | }
37 |
38 | public string PartitionKey { get; set; }
39 |
40 | ///
41 | /// SUT: System Under Test
42 | ///
43 | protected static ITestRepository SUT { get; set; }
44 |
45 | public void Init()
46 | {
47 | MongoDbConfig.EnsureConfigured();
48 | SUT = TestRepository.Instance;
49 | }
50 |
51 | public void Cleanup()
52 | {
53 | // We drop the collection at the end of each test session.
54 | if (!string.IsNullOrEmpty(PartitionKey))
55 | {
56 | SUT.DropTestCollection(PartitionKey);
57 | }
58 | else
59 | {
60 | SUT.DropTestCollection();
61 | }
62 | }
63 |
64 | #region IDisposable Support
65 | private bool _disposedValue; // Pour détecter les appels redondants
66 |
67 | protected virtual void Dispose(bool disposing)
68 | {
69 | if (!_disposedValue)
70 | {
71 | if (disposing)
72 | {
73 | Cleanup();
74 | }
75 |
76 | _disposedValue = true;
77 | }
78 | }
79 |
80 | // Ce code est ajouté pour implémenter correctement le modèle supprimable.
81 | public void Dispose()
82 | {
83 | // Ne modifiez pas ce code. Placez le code de nettoyage dans Dispose(bool disposing) ci-dessus.
84 | Dispose(true);
85 | }
86 | #endregion
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/CoreIntegrationTests/Infrastructure/GlobalVariables.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace CoreIntegrationTests.Infrastructure
4 | {
5 | ///
6 | /// A class holding global variables.
7 | ///
8 | public static class GlobalVariables
9 | {
10 | ///
11 | /// A random number generator.
12 | ///
13 | public static Random Random = new Random();
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/CoreIntegrationTests/Infrastructure/ITestRepository.cs:
--------------------------------------------------------------------------------
1 | using MongoDbGenericRepository;
2 |
3 | namespace CoreIntegrationTests
4 | {
5 | public interface ITestRepository : IBaseMongoRepository
6 | {
7 | void DropTestCollection();
8 | void DropTestCollection(string partitionKey);
9 | }
10 | }
--------------------------------------------------------------------------------
/CoreIntegrationTests/Infrastructure/MongoDbConfig.cs:
--------------------------------------------------------------------------------
1 | using MongoDB.Bson.Serialization.Conventions;
2 | using System.Threading;
3 |
4 | namespace CoreIntegrationTests.Infrastructure
5 | {
6 | internal static class MongoDbConfig
7 | {
8 | private static bool _initialized;
9 | private static object _initializationLock = new();
10 | private static object _initializationTarget;
11 |
12 | public static void EnsureConfigured()
13 | {
14 | EnsureConfiguredImpl();
15 | }
16 |
17 | private static void EnsureConfiguredImpl()
18 | {
19 | LazyInitializer.EnsureInitialized(ref _initializationTarget, ref _initialized, ref _initializationLock, () =>
20 | {
21 | Configure();
22 | return null;
23 | });
24 | }
25 |
26 | private static void Configure()
27 | {
28 | RegisterConventions();
29 | }
30 |
31 | private static void RegisterConventions()
32 | {
33 | var pack = new ConventionPack
34 | {
35 | new IgnoreIfNullConvention(false),
36 | new CamelCaseElementNameConvention(),
37 | };
38 | }
39 |
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/CoreIntegrationTests/Infrastructure/MongoDbTestFixture.cs:
--------------------------------------------------------------------------------
1 | using MongoDbGenericRepository;
2 | using MongoDbGenericRepository.Models;
3 | using System;
4 | using System.Collections.Concurrent;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 |
8 | namespace CoreIntegrationTests.Infrastructure
9 | {
10 | public class MongoDbTestFixture : IDisposable
11 | where T : IDocument, new()
12 | where TKey : IEquatable
13 | {
14 |
15 | public IMongoDbContext Context;
16 |
17 | public MongoDbTestFixture()
18 | {
19 | }
20 |
21 | public string PartitionKey { get; set; }
22 |
23 | public static ConcurrentBag DocsToDelete { get; set; } = new ConcurrentBag();
24 |
25 | public virtual void Dispose()
26 | {
27 |
28 | if (DocsToDelete.Any())
29 | {
30 | TestRepository.Instance.DeleteMany(DocsToDelete.ToList());
31 | }
32 | }
33 |
34 | public T CreateTestDocument()
35 | {
36 | var doc = new T();
37 | DocsToDelete.Add(doc);
38 | return doc;
39 | }
40 |
41 | public List CreateTestDocuments(int numberOfDocumentsToCreate)
42 | {
43 | var docs = new List();
44 | for (var i = 0; i < numberOfDocumentsToCreate; i++)
45 | {
46 | var doc = new T();
47 | docs.Add(doc);
48 | DocsToDelete.Add(doc);
49 | }
50 | return docs;
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/CoreIntegrationTests/Infrastructure/RandomExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace CoreIntegrationTests.Infrastructure
4 | {
5 | // Thanks BlueRaja - Danny Pflughoeft https://stackoverflow.com/a/13095144/5103354
6 | ///
7 | /// Extensions for the random number generator
8 | ///
9 | public static class RandomExtensions
10 | {
11 |
12 | ///
13 | /// Returns a random long from min (inclusive) to max (exclusive)
14 | ///
15 | /// The given random instance
16 | /// The inclusive minimum bound
17 | /// The exclusive maximum bound. Must be greater than min
18 | public static long NextLong(this Random random, long min, long max)
19 | {
20 | if (max <= min)
21 | throw new ArgumentOutOfRangeException("max", "max must be > min!");
22 |
23 | //Working with ulong so that modulo works correctly with values > long.MaxValue
24 | ulong uRange = (ulong)(max - min);
25 |
26 | //Prevent a modulo bias; see https://stackoverflow.com/a/10984975/238419
27 | //for more information.
28 | //In the worst case, the expected number of calls is 2 (though usually it's
29 | //much closer to 1) so this loop doesn't really hurt performance at all.
30 | ulong ulongRand;
31 | do
32 | {
33 | byte[] buf = new byte[8];
34 | random.NextBytes(buf);
35 | ulongRand = (ulong)BitConverter.ToInt64(buf, 0);
36 | } while (ulongRand > ulong.MaxValue - ((ulong.MaxValue % uRange) + 1) % uRange);
37 |
38 | return (long)(ulongRand % uRange) + min;
39 | }
40 |
41 | ///
42 | /// Returns a random long from 0 (inclusive) to max (exclusive)
43 | ///
44 | /// The given random instance
45 | /// The exclusive maximum bound. Must be greater than 0
46 | public static long NextLong(this Random random, long max)
47 | {
48 | return random.NextLong(0, max);
49 | }
50 |
51 | ///
52 | /// Returns a random long over all possible values of long (except long.MaxValue, similar to
53 | /// random.Next())
54 | ///
55 | /// The given random instance
56 | public static long NextLong(this Random random)
57 | {
58 | return random.NextLong(long.MinValue, long.MaxValue);
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/CoreIntegrationTests/Infrastructure/TestClasses.cs:
--------------------------------------------------------------------------------
1 | using MongoDB.Bson;
2 | using MongoDB.Bson.Serialization.Attributes;
3 | using MongoDbGenericRepository.Models;
4 | using MongoDbGenericRepository.Utils;
5 | using System;
6 | using System.Collections.Generic;
7 |
8 | namespace CoreIntegrationTests.Infrastructure
9 | {
10 | public class ProjectedGroup
11 | {
12 | public int Key { get; set; }
13 | public List Content { get; set; }
14 | }
15 |
16 | public class MyTestProjection
17 | {
18 | public string SomeContent { get; set; }
19 | public DateTime SomeDate { get; set; }
20 | }
21 |
22 | public class Nested
23 | {
24 | public DateTime SomeDate { get; set; }
25 | [BsonRepresentation(BsonType.Decimal128)]
26 | public decimal SomeAmount { get; set; }
27 | }
28 |
29 | public class Child
30 | {
31 | public Child(string type, string value)
32 | {
33 | Type = type;
34 | Value = value;
35 | }
36 |
37 | public string Type { get; set; }
38 | public string Value { get; set; }
39 | }
40 |
41 | public class TestDoc : Document
42 | {
43 | public TestDoc()
44 | {
45 | Version = 2;
46 | Nested = new Nested
47 | {
48 | SomeDate = DateTime.UtcNow
49 | };
50 | Children = new List();
51 | }
52 |
53 | public int SomeValue { get; set; }
54 |
55 | public string SomeContent { get; set; }
56 | public string SomeContent2 { get; set; }
57 | public string SomeContent3 { get; set; }
58 |
59 | public int GroupingKey { get; set; }
60 |
61 | public Nested Nested { get; set; }
62 |
63 | public List Children { get; set; }
64 |
65 | }
66 |
67 | public class TestDoc : IDocument
68 | where TKey : IEquatable
69 | {
70 | [BsonId]
71 | public TKey Id { get; set; }
72 | public int Version { get; set; }
73 |
74 | public TestDoc()
75 | {
76 | InitializeFields();
77 | Version = 2;
78 | Nested = new Nested
79 | {
80 | SomeDate = DateTime.UtcNow
81 | };
82 | Children = new List();
83 | }
84 |
85 | public int GroupingKey { get; set; }
86 | public string SomeContent { get; set; }
87 | public string SomeContent4 { get; set; }
88 | public string SomeContent5 { get; set; }
89 |
90 | public Nested Nested { get; set; }
91 |
92 | public List Children { get; set; }
93 |
94 | public TId Init()
95 | {
96 | return IdGenerator.GetId();
97 | }
98 |
99 | private void InitializeFields()
100 | {
101 | Id = Init();
102 | }
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/CoreIntegrationTests/Infrastructure/TestRepository.cs:
--------------------------------------------------------------------------------
1 | using MongoDB.Bson;
2 | using MongoDbGenericRepository;
3 | using System;
4 |
5 | namespace CoreIntegrationTests.Infrastructure
6 | {
7 | public interface ITestRepository : IBaseMongoRepository where TKey : IEquatable
8 | {
9 | void DropTestCollection();
10 | void DropTestCollection(string partitionKey);
11 | }
12 |
13 | public class TestTKeyRepository : BaseMongoRepository, ITestRepository where TKey : IEquatable
14 | {
15 | const string connectionString = "mongodb://localhost:27017/MongoDbTests";
16 | private static readonly ITestRepository _instance = new TestTKeyRepository(connectionString);
17 | ///
18 | private TestTKeyRepository(string connectionString) : base(connectionString)
19 | {
20 | }
21 |
22 | public static ITestRepository Instance
23 | {
24 | get
25 | {
26 | return _instance;
27 | }
28 | }
29 |
30 | public void DropTestCollection()
31 | {
32 | MongoDbContext.DropCollection();
33 | }
34 |
35 | public void DropTestCollection(string partitionKey)
36 | {
37 | MongoDbContext.DropCollection(partitionKey);
38 | }
39 | }
40 |
41 | ///
42 | /// A singleton implementation of the TestRepository
43 | ///
44 | public sealed class TestRepository : BaseMongoRepository, ITestRepository
45 | {
46 |
47 | const string connectionString = "mongodb://localhost:27017";
48 | private static readonly ITestRepository _instance = new TestRepository(connectionString, "MongoDbTests");
49 |
50 | // Explicit static constructor to tell C# compiler
51 | // not to mark type as beforefieldinit
52 | static TestRepository()
53 | {
54 | }
55 |
56 | ///
57 | private TestRepository(string connectionString, string databaseName) : base(connectionString, databaseName)
58 | {
59 | }
60 |
61 | public static ITestRepository Instance
62 | {
63 | get
64 | {
65 | return _instance;
66 | }
67 | }
68 |
69 | public void DropTestCollection()
70 | {
71 | MongoDbContext.DropCollection();
72 | }
73 |
74 | public void DropTestCollection(string partitionKey)
75 | {
76 | MongoDbContext.DropCollection(partitionKey);
77 | }
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyAsyncTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 | using AutoFixture;
6 | using CoreUnitTests.Infrastructure;
7 | using CoreUnitTests.Infrastructure.Model;
8 | using MongoDbGenericRepository.DataAccess.Create;
9 | using Moq;
10 | using Xunit;
11 |
12 | namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests;
13 |
14 | public class AddManyAsyncTests : TestMongoRepositoryContext
15 | {
16 | [Fact]
17 | public async Task WithDocument_ShouldAddOne()
18 | {
19 | // Arrange
20 | var documents = Fixture.CreateMany().ToList();
21 | Creator = new Mock();
22 |
23 | // Act
24 | await Sut.AddManyAsync(documents);
25 |
26 | // Assert
27 | Creator.Verify(x => x.AddManyAsync(documents, CancellationToken.None), Times.Once);
28 | }
29 |
30 | [Fact]
31 | public async Task WithDocumentAndCancellationToken_ShouldAddOne()
32 | {
33 | // Arrange
34 | var documents = Fixture.CreateMany().ToList();
35 | var token = new CancellationToken(true);
36 | Creator = new Mock();
37 |
38 | // Act
39 | await Sut.AddManyAsync(documents, token);
40 |
41 | // Assert
42 | Creator.Verify(x => x.AddManyAsync(documents, token), Times.Once);
43 | }
44 |
45 | #region Keyed
46 |
47 | [Fact]
48 | public async Task Keyed_WithDocument_ShouldAddOne()
49 | {
50 | // Arrange
51 | var documents = Fixture.CreateMany>().ToList();
52 | Creator = new Mock();
53 |
54 | // Act
55 | await Sut.AddManyAsync, int>(documents);
56 |
57 | // Assert
58 | Creator.Verify(x => x.AddManyAsync, int>(documents, CancellationToken.None), Times.Once);
59 | }
60 |
61 | [Fact]
62 | public async Task Keyed_WithDocumentAndCancellationToken_ShouldAddOne()
63 | {
64 | // Arrange
65 | var documents = Fixture.CreateMany>().ToList();
66 | var token = new CancellationToken(true);
67 | Creator = new Mock();
68 |
69 | // Act
70 | await Sut.AddManyAsync, int>(documents, token);
71 |
72 | // Assert
73 | Creator.Verify(x => x.AddManyAsync, int>(documents, token), Times.Once);
74 | }
75 |
76 | #endregion
77 | }
78 |
--------------------------------------------------------------------------------
/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddManyTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 | using System.Threading;
4 | using AutoFixture;
5 | using CoreUnitTests.Infrastructure;
6 | using CoreUnitTests.Infrastructure.Model;
7 | using MongoDbGenericRepository.DataAccess.Create;
8 | using Moq;
9 | using Xunit;
10 |
11 | namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests;
12 |
13 | public class AddManyTests : TestMongoRepositoryContext
14 | {
15 |
16 | [Fact]
17 | public void WithDocument_ShouldAddOne()
18 | {
19 | // Arrange
20 | var documents = Fixture.CreateMany().ToList();
21 | Creator = new Mock();
22 |
23 | // Act
24 | Sut.AddMany(documents);
25 |
26 | // Assert
27 | Creator.Verify(x => x.AddMany(documents, CancellationToken.None), Times.Once);
28 | }
29 |
30 | [Fact]
31 | public void WithDocumentAndCancellationToken_ShouldAddOne()
32 | {
33 | // Arrange
34 | var documents = Fixture.CreateMany().ToList();
35 | var token = new CancellationToken(true);
36 | Creator = new Mock();
37 |
38 | // Act
39 | Sut.AddMany(documents, token);
40 |
41 | // Assert
42 | Creator.Verify(x => x.AddMany(documents, token), Times.Once);
43 | }
44 |
45 | #region Keyed
46 |
47 | [Fact]
48 | public void Keyed_WithDocument_ShouldAddOne()
49 | {
50 | // Arrange
51 | var documents = Fixture.CreateMany>().ToList();
52 | Creator = new Mock();
53 |
54 | // Act
55 | Sut.AddMany, int>(documents);
56 |
57 | // Assert
58 | Creator.Verify(x => x.AddMany, int>(documents, CancellationToken.None), Times.Once);
59 | }
60 |
61 | [Fact]
62 | public void Keyed_WithDocumentAndCancellationToken_ShouldAddOne()
63 | {
64 | // Arrange
65 | var documents = Fixture.CreateMany>().ToList();
66 | var token = new CancellationToken(true);
67 | Creator = new Mock();
68 |
69 | // Act
70 | Sut.AddMany, int>(documents, token);
71 |
72 | // Assert
73 | Creator.Verify(x => x.AddMany, int>(documents, token), Times.Once);
74 | }
75 |
76 | #endregion
77 | }
78 |
--------------------------------------------------------------------------------
/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneAsyncTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using System.Threading.Tasks;
4 | using AutoFixture;
5 | using CoreUnitTests.Infrastructure;
6 | using CoreUnitTests.Infrastructure.Model;
7 | using MongoDbGenericRepository.DataAccess.Create;
8 | using Moq;
9 | using Xunit;
10 |
11 | namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests;
12 |
13 | public class AddOneAsyncTests : TestMongoRepositoryContext
14 | {
15 | [Fact]
16 | public async Task WithDocument_ShouldAddOne()
17 | {
18 | // Arrange
19 | var document = Fixture.Create();
20 | Creator = new Mock();
21 |
22 | // Act
23 | await Sut.AddOneAsync(document);
24 |
25 | // Assert
26 | Creator.Verify(x => x.AddOneAsync(document, CancellationToken.None), Times.Once);
27 | }
28 |
29 | [Fact]
30 | public async Task WithDocumentAndCancellationToken_ShouldAddOne()
31 | {
32 | // Arrange
33 | var document = Fixture.Create();
34 | var token = new CancellationToken(true);
35 | Creator = new Mock();
36 |
37 | // Act
38 | await Sut.AddOneAsync(document, token);
39 |
40 | // Assert
41 | Creator.Verify(x => x.AddOneAsync(document, token), Times.Once);
42 | }
43 |
44 | #region Keyed
45 |
46 | [Fact]
47 | public async Task Keyed_WithDocument_ShouldAddOne()
48 | {
49 | // Arrange
50 | var document = Fixture.Create>();
51 | Creator = new Mock();
52 |
53 | // Act
54 | await Sut.AddOneAsync, int>(document);
55 |
56 | // Assert
57 | Creator.Verify(x => x.AddOneAsync, int>(document, CancellationToken.None), Times.Once);
58 | }
59 |
60 | [Fact]
61 | public async Task Keyed_WithDocumentAndCancellationToken_ShouldAddOne()
62 | {
63 | // Arrange
64 | var document = Fixture.Create>();
65 | var token = new CancellationToken(true);
66 | Creator = new Mock();
67 |
68 | // Act
69 | await Sut.AddOneAsync, int>(document, token);
70 |
71 | // Assert
72 | Creator.Verify(x => x.AddOneAsync, int>(document, token), Times.Once);
73 | }
74 |
75 | #endregion
76 | }
77 |
--------------------------------------------------------------------------------
/CoreUnitTests/BaseMongoRepositoryTests/AddTests/AddOneTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using AutoFixture;
4 | using CoreUnitTests.Infrastructure;
5 | using CoreUnitTests.Infrastructure.Model;
6 | using MongoDbGenericRepository.DataAccess.Create;
7 | using Moq;
8 | using Xunit;
9 |
10 | namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests;
11 |
12 | public class AddOneTests : TestMongoRepositoryContext
13 | {
14 | [Fact]
15 | public void WithDocument_ShouldAddOne()
16 | {
17 | // Arrange
18 | var document = Fixture.Create();
19 | Creator = new Mock();
20 |
21 | // Act
22 | Sut.AddOne(document);
23 |
24 | // Assert
25 | Creator.Verify(x => x.AddOne(document, CancellationToken.None), Times.Once);
26 | }
27 |
28 | [Fact]
29 | public void WithDocumentAndCancellationToken_ShouldAddOne()
30 | {
31 | // Arrange
32 | var document = Fixture.Create();
33 | var token = new CancellationToken(true);
34 | Creator = new Mock();
35 |
36 | // Act
37 | Sut.AddOne(document, token);
38 |
39 | // Assert
40 | Creator.Verify(x => x.AddOne(document, token), Times.Once);
41 | }
42 |
43 | #region Keyed
44 |
45 | [Fact]
46 | public void Keyed_WithDocument_ShouldAddOne()
47 | {
48 | // Arrange
49 | var document = Fixture.Create>();
50 | Creator = new Mock();
51 |
52 | // Act
53 | Sut.AddOne, int>(document);
54 |
55 | // Assert
56 | Creator.Verify(x => x.AddOne, int>(document, CancellationToken.None), Times.Once);
57 | }
58 |
59 | [Fact]
60 | public void Keyed_WithDocumentAndCancellationToken_ShouldAddOne()
61 | {
62 | // Arrange
63 | var document = Fixture.Create>();
64 | var token = new CancellationToken(true);
65 | Creator = new Mock();
66 |
67 | // Act
68 | Sut.AddOne, int>(document, token);
69 |
70 | // Assert
71 | Creator.Verify(x => x.AddOne, int>(document, token), Times.Once);
72 | }
73 |
74 | #endregion
75 | }
76 |
--------------------------------------------------------------------------------
/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/BaseIndexTests.cs:
--------------------------------------------------------------------------------
1 | using CoreUnitTests.Infrastructure;
2 | using MongoDB.Bson;
3 | using MongoDB.Driver;
4 | using Moq;
5 |
6 | namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests;
7 |
8 | public class BaseIndexTests : TestMongoRepositoryContext
9 | {
10 | protected static Mock> SetupIndex(BsonDocument index, Mock> collection)
11 | {
12 | var asyncCursor = new Mock>();
13 | asyncCursor
14 | .SetupSequence(x => x.MoveNextAsync(It.IsAny()))
15 | .ReturnsAsync(true)
16 | .ReturnsAsync(false);
17 |
18 | asyncCursor
19 | .SetupGet(x => x.Current)
20 | .Returns([index]);
21 |
22 | var indexManager = new Mock>();
23 | indexManager
24 | .Setup(x => x.ListAsync(It.IsAny()))
25 | .ReturnsAsync(asyncCursor.Object);
26 |
27 | collection
28 | .SetupGet(x => x.Indexes)
29 | .Returns(indexManager.Object);
30 |
31 | return asyncCursor;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/CoreUnitTests/CoreUnitTests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net8.0
5 | warnings
6 | enable
7 | false
8 | true
9 | latest
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | all
23 | runtime; build; native; contentfiles; analyzers
24 |
25 |
26 | all
27 | runtime; build; native; contentfiles; analyzers; buildtransitive
28 |
29 |
30 | runtime; build; native; contentfiles; analyzers; buildtransitive
31 | all
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteOneAsyncTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq.Expressions;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 | using AutoFixture;
6 | using CoreUnitTests.Infrastructure;
7 | using CoreUnitTests.Infrastructure.Model;
8 | using FluentAssertions;
9 | using MongoDB.Driver;
10 | using MongoDbGenericRepository;
11 | using MongoDbGenericRepository.DataAccess.Delete;
12 | using Moq;
13 | using Xunit;
14 |
15 | namespace CoreUnitTests.DataAccessTests.MongoDbEraserTests;
16 |
17 | public class DeleteOneAsyncTests : GenericTestContext
18 | {
19 | [Fact]
20 | public async Task WithDocumentAndCancellationToken_DeletesOne()
21 | {
22 | // Arrange
23 | var count = Fixture.Create();
24 | var document = Fixture.Create();
25 | var token = new CancellationToken(true);
26 |
27 | var collection = MockOf>();
28 | collection
29 | .Setup(x => x.DeleteOneAsync(It.IsAny>(), It.IsAny()))
30 | .ReturnsAsync(new DeleteResult.Acknowledged(count));
31 |
32 | var dbContext = MockOf();
33 | dbContext
34 | .Setup(x => x.GetCollection(null))
35 | .Returns(collection.Object);
36 |
37 | // Act
38 | var result = await Sut.DeleteOneAsync(document, token);
39 |
40 | // Assert
41 | result.Should().Be(count);
42 |
43 | var expectedFilter = Builders.Filter.Eq("Id", document.Id);
44 | collection.Verify(
45 | x => x.DeleteOneAsync(
46 | It.Is>(f => f.EquivalentTo(expectedFilter)),
47 | token),
48 | Times.Once());
49 | }
50 |
51 | [Fact]
52 | public async Task WithFilterAndPartitionKeyAndCancellationToken_DeletesOne()
53 | {
54 | // Arrange
55 | var count = Fixture.Create();
56 | var document = Fixture.Create();
57 | var partitionKey = Fixture.Create();
58 | var token = new CancellationToken(true);
59 |
60 | var collection = MockOf>();
61 | collection
62 | .Setup(x => x.DeleteOneAsync(It.IsAny>(), It.IsAny()))
63 | .ReturnsAsync(new DeleteResult.Acknowledged(count));
64 |
65 | var dbContext = MockOf();
66 | dbContext
67 | .Setup(x => x.GetCollection(It.IsAny()))
68 | .Returns(collection.Object);
69 |
70 | Expression> filter = d => d.Id == document.Id;
71 |
72 | // Act
73 | var result = await Sut.DeleteOneAsync(filter, partitionKey, token);
74 |
75 | // Assert
76 | result.Should().Be(count);
77 | collection.Verify(
78 | x => x.DeleteOneAsync(
79 | It.Is>(f => f.EquivalentTo(filter)),
80 | token),
81 | Times.Once());
82 |
83 | dbContext.Verify(x => x.GetCollection(partitionKey));
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/CoreUnitTests/DataAccessTests/MongoDbEraserTests/DeleteOneTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq.Expressions;
3 | using System.Threading;
4 | using AutoFixture;
5 | using CoreUnitTests.Infrastructure;
6 | using CoreUnitTests.Infrastructure.Model;
7 | using FluentAssertions;
8 | using MongoDB.Driver;
9 | using MongoDbGenericRepository;
10 | using MongoDbGenericRepository.DataAccess.Delete;
11 | using Moq;
12 | using Xunit;
13 |
14 | namespace CoreUnitTests.DataAccessTests.MongoDbEraserTests;
15 |
16 | public class DeleteOneTests : GenericTestContext
17 | {
18 | [Fact]
19 | public void WithDocumentAndCancellationToken_DeletesOne()
20 | {
21 | // Arrange
22 | var count = Fixture.Create();
23 | var document = Fixture.Create();
24 | var token = new CancellationToken(true);
25 |
26 | var collection = MockOf>();
27 | collection
28 | .Setup(x => x.DeleteOne(It.IsAny>(), It.IsAny()))
29 | .Returns(new DeleteResult.Acknowledged(count));
30 |
31 | var dbContext = MockOf();
32 | dbContext
33 | .Setup(x => x.GetCollection(null))
34 | .Returns(collection.Object);
35 |
36 | // Act
37 | var result = Sut.DeleteOne(document, token);
38 |
39 | // Assert
40 | result.Should().Be(count);
41 |
42 | var expectedFilter = Builders.Filter.Eq("Id", document.Id);
43 | collection.Verify(
44 | x => x.DeleteOne(
45 | It.Is>(f => f.EquivalentTo(expectedFilter)),
46 | token),
47 | Times.Once());
48 | }
49 |
50 | [Fact]
51 | public void WithFilterAndPartitionKeyAndCancellationToken_DeletesOne()
52 | {
53 | // Arrange
54 | var count = Fixture.Create();
55 | var document = Fixture.Create();
56 | var partitionKey = Fixture.Create();
57 | var token = new CancellationToken(true);
58 |
59 | var collection = MockOf>();
60 | collection
61 | .Setup(x => x.DeleteOne(It.IsAny>(), It.IsAny()))
62 | .Returns(new DeleteResult.Acknowledged(count));
63 |
64 | var dbContext = MockOf();
65 | dbContext
66 | .Setup(x => x.GetCollection(It.IsAny()))
67 | .Returns(collection.Object);
68 |
69 | Expression> filter = d => d.Id == document.Id;
70 |
71 | // Act
72 | var result = Sut.DeleteOne(filter, partitionKey, token);
73 |
74 | // Assert
75 | result.Should().Be(count);
76 | collection.Verify(
77 | x => x.DeleteOne(
78 | It.Is>(f => f.EquivalentTo(filter)),
79 | token),
80 | Times.Once());
81 |
82 | dbContext.Verify(x => x.GetCollection(partitionKey));
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/BaseIndexTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Threading;
3 | using CoreUnitTests.Infrastructure;
4 | using MongoDB.Bson;
5 | using MongoDB.Driver;
6 | using MongoDbGenericRepository.DataAccess.Index;
7 | using Moq;
8 |
9 | namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests;
10 |
11 | public class BaseIndexTests : GenericTestContext
12 | {
13 | protected (Mock>, Mock>) SetupIndexes(
14 | List indexes,
15 | Mock> collection)
16 | {
17 | var asyncCursor = MockOf>();
18 | var moveNextSequence = asyncCursor
19 | .SetupSequence(x => x.MoveNextAsync(It.IsAny()));
20 |
21 | var currentSequence = asyncCursor
22 | .SetupSequence(x => x.Current);
23 |
24 | foreach (var bsonDocument in indexes)
25 | {
26 | moveNextSequence.ReturnsAsync(true);
27 | currentSequence.Returns(new[] {bsonDocument});
28 | }
29 |
30 | moveNextSequence.ReturnsAsync(false);
31 |
32 | var indexManager = MockOf>();
33 | indexManager
34 | .Setup(x => x.ListAsync(It.IsAny()))
35 | .ReturnsAsync(asyncCursor.Object);
36 |
37 | collection
38 | .SetupGet(x => x.Indexes)
39 | .Returns(indexManager.Object);
40 |
41 | return (asyncCursor, indexManager);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/CoreUnitTests/DataAccessTests/MongoDbReaderTests/BaseReaderTests.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Threading;
3 | using CoreUnitTests.Infrastructure;
4 | using MongoDB.Driver;
5 | using MongoDbGenericRepository.DataAccess.Read;
6 | using Moq;
7 |
8 | namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
9 |
10 | public class BaseReaderTests : GenericTestContext
11 | {
12 | protected Mock> SetupSyncCursor(List documents)
13 | {
14 | var asyncCursor = MockOf>();
15 |
16 | var moveNextSequence = asyncCursor
17 | .SetupSequence(x => x.MoveNext(It.IsAny()));
18 |
19 | var currentSequence = asyncCursor
20 | .SetupSequence(x => x.Current);
21 |
22 | foreach (var projection in documents)
23 | {
24 | moveNextSequence.Returns(true);
25 | currentSequence.Returns(new[] {projection});
26 | }
27 |
28 | moveNextSequence.Returns(false);
29 | return asyncCursor;
30 | }
31 |
32 | protected Mock> SetupAsyncCursor(List documents)
33 | {
34 | var asyncCursor = MockOf>();
35 |
36 | var moveNextSequence = asyncCursor
37 | .SetupSequence(x => x.MoveNextAsync(It.IsAny()));
38 |
39 | var currentSequence = asyncCursor
40 | .SetupSequence(x => x.Current);
41 |
42 | foreach (var projection in documents)
43 | {
44 | moveNextSequence.ReturnsAsync(true);
45 | currentSequence.Returns(new[] {projection});
46 | }
47 |
48 | moveNextSequence.ReturnsAsync(false);
49 | return asyncCursor;
50 | }
51 |
52 | protected static void SetupFindAsync(Mock> collection, Mock> asyncCursor) =>
53 | collection
54 | .Setup(
55 | x => x.FindAsync(
56 | It.IsAny>(),
57 | It.IsAny>(),
58 | It.IsAny()))
59 | .ReturnsAsync(asyncCursor.Object);
60 |
61 | protected static void SetupFindSync(Mock> collection, Mock> asyncCursor) =>
62 | collection
63 | .Setup(
64 | x => x.FindSync(
65 | It.IsAny>(),
66 | It.IsAny>(),
67 | It.IsAny()))
68 | .Returns(asyncCursor.Object);
69 | }
70 |
--------------------------------------------------------------------------------
/CoreUnitTests/Infrastructure/FilterDefinitionExtensions.cs:
--------------------------------------------------------------------------------
1 | namespace CoreUnitTests.Infrastructure;
2 |
3 | using MongoDB.Bson;
4 | using MongoDB.Bson.Serialization;
5 | using MongoDB.Driver;
6 |
7 | public static class FilterDefinitionExtensions
8 | {
9 | public static string RenderToJson(this FilterDefinition filter)
10 | => filter.Render(new RenderArgs(BsonSerializer.SerializerRegistry.GetSerializer(), BsonSerializer.SerializerRegistry)).ToJson();
11 |
12 | public static bool EquivalentTo(this FilterDefinition filter, FilterDefinition other)
13 | => filter.RenderToJson() == other.RenderToJson();
14 | }
15 |
--------------------------------------------------------------------------------
/CoreUnitTests/Infrastructure/GenericTestContext.cs:
--------------------------------------------------------------------------------
1 | using AutoFixture;
2 | using AutoFixture.AutoMoq;
3 | using Moq;
4 |
5 | namespace CoreUnitTests.Infrastructure;
6 |
7 | public class GenericTestContext
8 | {
9 | public GenericTestContext() => Fixture = new Fixture().Customize(new AutoMoqCustomization());
10 |
11 | protected Mock MockOf()
12 | where T : class =>
13 | Fixture.Freeze>();
14 |
15 | protected IFixture Fixture { get; set; }
16 |
17 | protected TSut Sut => Fixture.Create();
18 | }
19 |
--------------------------------------------------------------------------------
/CoreUnitTests/Infrastructure/IndexExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using MongoDB.Bson.Serialization;
3 | using MongoDB.Driver;
4 | using MongoDbGenericRepository.Models;
5 | using Xunit.Abstractions;
6 |
7 | namespace CoreUnitTests.Infrastructure;
8 |
9 | public static class IndexExtensions
10 | {
11 | public static bool EqualToJson(this IndexKeysDefinition keys, string json, ITestOutputHelper output)
12 | {
13 | var indexModelRendered = RenderIndexModelKeys(keys);
14 | var result = indexModelRendered.Equals(json, StringComparison.Ordinal);
15 | if (!result && output != null)
16 | {
17 | output.WriteLine($"Expected: {json}");
18 | output.WriteLine($"Actual: {indexModelRendered}");
19 | }
20 |
21 | return result;
22 | }
23 |
24 | public static bool EqualToJson(this IndexKeysDefinition keys, string json)
25 | {
26 | var indexModelRendered = RenderIndexModelKeys(keys);
27 | return indexModelRendered.Equals(json, StringComparison.Ordinal);
28 | }
29 |
30 | public static bool EqualTo(this IndexCreationOptions x, CreateIndexOptions y) =>
31 | x.Unique == y.Unique &&
32 | x.TextIndexVersion == y.TextIndexVersion &&
33 | x.SphereIndexVersion == y.SphereIndexVersion &&
34 | x.Sparse == y.Sparse &&
35 | x.Name == y.Name &&
36 | x.Min == y.Min &&
37 | x.Max == y.Max &&
38 | x.LanguageOverride == y.LanguageOverride &&
39 | x.ExpireAfter == y.ExpireAfter &&
40 | x.DefaultLanguage == y.DefaultLanguage &&
41 | x.Bits == y.Bits &&
42 | x.Background == y.Background &&
43 | x.Version == y.Version;
44 |
45 | public static bool EqualTo(this CreateIndexOptions x, IndexCreationOptions y) =>
46 | x.Unique == y.Unique &&
47 | x.TextIndexVersion == y.TextIndexVersion &&
48 | x.SphereIndexVersion == y.SphereIndexVersion &&
49 | x.Sparse == y.Sparse &&
50 | x.Name == y.Name &&
51 | x.Min == y.Min &&
52 | x.Max == y.Max &&
53 | x.LanguageOverride == y.LanguageOverride &&
54 | x.ExpireAfter == y.ExpireAfter &&
55 | x.DefaultLanguage == y.DefaultLanguage &&
56 | x.Bits == y.Bits &&
57 | x.Background == y.Background &&
58 | x.Version == y.Version;
59 |
60 | private static string RenderIndexModelKeys(IndexKeysDefinition keys)
61 | {
62 | var indexModelRendered = keys.Render(new RenderArgs(
63 | BsonSerializer.SerializerRegistry.GetSerializer(), BsonSerializer.SerializerRegistry));
64 |
65 | var result = indexModelRendered.ToString();
66 | return result.Replace(" ", "");
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/CoreUnitTests/Infrastructure/Model/Child.cs:
--------------------------------------------------------------------------------
1 | namespace CoreUnitTests.Infrastructure.Model;
2 |
3 | public class Child
4 | {
5 | public Child(string type, string value)
6 | {
7 | Type = type;
8 | Value = value;
9 | }
10 |
11 | public string Type { get; set; }
12 | public string Value { get; set; }
13 | }
--------------------------------------------------------------------------------
/CoreUnitTests/Infrastructure/Model/Nested.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using MongoDB.Bson;
3 | using MongoDB.Bson.Serialization.Attributes;
4 |
5 | namespace CoreUnitTests.Infrastructure.Model;
6 |
7 | public class Nested
8 | {
9 | public DateTime SomeDate { get; set; }
10 |
11 | [BsonRepresentation(BsonType.Decimal128)]
12 | public decimal SomeAmount { get; set; }
13 | }
--------------------------------------------------------------------------------
/CoreUnitTests/Infrastructure/Model/PartitionedTestDocument.cs:
--------------------------------------------------------------------------------
1 | using MongoDbGenericRepository.Models;
2 |
3 | namespace CoreUnitTests.Infrastructure.Model;
4 |
5 | public class PartitionedTestDocument : TestDocument, IPartitionedDocument
6 | {
7 | ///
8 | public string PartitionKey { get; set; } = "PartitionedTestDocument";
9 | }
10 |
--------------------------------------------------------------------------------
/CoreUnitTests/Infrastructure/Model/TestDocument.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using MongoDbGenericRepository.Models;
4 |
5 | namespace CoreUnitTests.Infrastructure.Model;
6 |
7 | public class TestDocument : Document
8 | {
9 | public TestDocument()
10 | {
11 | Version = 2;
12 | Nested = new Nested {SomeDate = DateTime.UtcNow};
13 | Children = new List();
14 | }
15 |
16 | public int SomeValue { get; set; }
17 |
18 | public decimal SomeDecimalValue { get; set; }
19 |
20 | public string SomeContent { get; set; }
21 |
22 | public string SomeContent2 { get; set; }
23 |
24 | public string SomeContent3 { get; set; }
25 |
26 | public int GroupingKey { get; set; }
27 |
28 | public Guid OtherGroupingKey { get; set; }
29 |
30 | public Nested Nested { get; set; }
31 |
32 | public List Children { get; set; }
33 | }
34 |
--------------------------------------------------------------------------------
/CoreUnitTests/Infrastructure/Model/TestDocumentWithKey.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using MongoDbGenericRepository.Models;
4 |
5 | namespace CoreUnitTests.Infrastructure.Model;
6 |
7 | public class TestDocumentWithKey : IDocument
8 | where TKey : IEquatable
9 | {
10 | public TKey Id { get; set; }
11 | public int Version { get; set; }
12 |
13 | public TestDocumentWithKey()
14 | {
15 | Version = 2;
16 | Nested = new Nested
17 | {
18 | SomeDate = DateTime.UtcNow
19 | };
20 | Children = new List();
21 | }
22 |
23 | public int SomeValue { get; set; }
24 |
25 | public int SomeDecimalValue { get; set; }
26 |
27 | public string SomeContent { get; set; }
28 | public string SomeContent2 { get; set; }
29 | public string SomeContent3 { get; set; }
30 |
31 | public int GroupingKey { get; set; }
32 |
33 | public Nested Nested { get; set; }
34 |
35 | public List