Repository: alexandre-spieser/mongodb-generic-repository
Branch: master
Commit: 0c20dfca3894
Files: 254
Total size: 2.6 MB
Directory structure:
gitextract_61u1jiz3/
├── .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
│ └── packages.config
├── LICENSE
├── 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.xml
│ ├── ReadOnlyMongoRepository.cs
│ ├── Utils/
│ │ ├── IdGenerator.cs
│ │ ├── Pluralization.cs
│ │ └── RandomExtensions.cs
│ └── _rels/
│ └── .rels
├── MongoDbGenericRepository.sln
└── README.md
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
**/Properties/launchSettings.json
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Typescript v1 declaration files
typings/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml
# CodeRush
.cr/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
================================================
FILE: CoreIntegrationTests/App.config
================================================
================================================
FILE: CoreIntegrationTests/CRUDObjectIdTests.cs
================================================
using CoreIntegrationTests.Infrastructure;
using MongoDB.Bson;
namespace CoreIntegrationTests
{
public class CoreObjectIdTestDocument : TestDoc
{
}
public class CRUDObjectIdTests : MongoDbTKeyDocumentTestBase
{
public CRUDObjectIdTests(MongoDbTestFixture fixture) : base(fixture)
{
}
public override string GetClassName()
{
return "CRUDObjectIdTests";
}
}
}
================================================
FILE: CoreIntegrationTests/CRUDPartitionedCollectionNameAttributeTests.cs
================================================
using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Attributes;
using MongoDbGenericRepository.Models;
using System;
namespace CoreIntegrationTests
{
[CollectionName("CoreTestingCNameAttrPart")]
public class CorePartitionedCollectionNameDoc : TestDoc, IPartitionedDocument
{
public CorePartitionedCollectionNameDoc()
{
PartitionKey = "CoreTestPartitionKeyCollectionName";
}
public string PartitionKey { get; set; }
}
public class CRUDPartitionedCollectionNameAttributeTests : MongoDbDocumentTestBase
{
public CRUDPartitionedCollectionNameAttributeTests(MongoDbTestFixture fixture) : base(fixture)
{
}
public override string GetClassName()
{
return "CoreCRUDPartitionedCollectionNameAttributeTests";
}
}
}
================================================
FILE: CoreIntegrationTests/CRUDPartitionedTests.cs
================================================
using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models;
using System;
namespace CoreIntegrationTests
{
public class CorePartitionedDoc : TestDoc, IPartitionedDocument
{
public CorePartitionedDoc()
{
PartitionKey = "CoreTestPartitionKey";
}
public string PartitionKey { get; set; }
}
public class CRUDPartitionedTests : MongoDbDocumentTestBase
{
public CRUDPartitionedTests(MongoDbTestFixture fixture) : base(fixture)
{
}
public override string GetClassName()
{
return "CoreCRUDPartitionedTests";
}
}
}
================================================
FILE: CoreIntegrationTests/CRUDTKeyPartitionedCollectionNameAttributeTests.cs
================================================
using CoreIntegrationTests.Infrastructure;
using MongoDB.Bson;
using MongoDbGenericRepository.Attributes;
using MongoDbGenericRepository.Models;
using System;
namespace CoreIntegrationTests
{
#region Guid Type
[CollectionName("TestingCNameAttrPartTKey")]
public class CoreTKeyPartitionedCollectionNameDoc : TestDoc, IPartitionedDocument
{
public CoreTKeyPartitionedCollectionNameDoc()
{
PartitionKey = "CoreTestPartitionKey";
}
public string PartitionKey { get; set; }
}
public class CRUDTKeyPartitionedCollectionNameAttributeTests : MongoDbTKeyDocumentTestBase
{
public CRUDTKeyPartitionedCollectionNameAttributeTests(MongoDbTestFixture fixture) : base(fixture)
{
}
public override string GetClassName()
{
return "CoreCRUDTKeyPartitionedCollectionNameAttributeTests";
}
}
#endregion Guid Type
#region ObjectId Type
[CollectionName("TestingCNameAttrPartObjectId")]
public class CoreObjectIdPartitionedCollectionNameDoc : TestDoc, IPartitionedDocument
{
public CoreObjectIdPartitionedCollectionNameDoc()
{
PartitionKey = "CoreTestPartitionKeyObjectId";
}
public string PartitionKey { get; set; }
}
public class CRUDObjectIdPartitionedCollectionNameAttributeTests : MongoDbTKeyDocumentTestBase
{
public CRUDObjectIdPartitionedCollectionNameAttributeTests(MongoDbTestFixture fixture) : base(fixture)
{
}
public override string GetClassName()
{
return "CoreCRUDTKeyPartitionedCollectionNameAttributeTests";
}
}
#endregion ObjectId Type
}
================================================
FILE: CoreIntegrationTests/CRUDTKeyPartitionedTests.cs
================================================
using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models;
using System;
namespace CoreIntegrationTests
{
public class CorePartitionedTKeyTestDocument : TestDoc, IPartitionedDocument
{
public CorePartitionedTKeyTestDocument()
{
PartitionKey = "CoreTestPartitionKey";
}
public string PartitionKey { get; set; }
}
public class CRUDTKeyPartitionedTests : MongoDbTKeyDocumentTestBase
{
public CRUDTKeyPartitionedTests(MongoDbTestFixture fixture) : base(fixture)
{
}
public override string GetClassName()
{
return "CoreCRUDTKeyPartitionedTests";
}
}
}
================================================
FILE: CoreIntegrationTests/CRUDTKeyTests.cs
================================================
using CoreIntegrationTests.Infrastructure;
using System;
namespace CoreIntegrationTests
{
public class CoreTKeyTestDocument : TestDoc
{
}
public class CRUDTKeyTests : MongoDbTKeyDocumentTestBase
{
public CRUDTKeyTests(MongoDbTestFixture fixture) : base(fixture)
{
}
public override string GetClassName()
{
return "CreateTKeyTests";
}
}
}
================================================
FILE: CoreIntegrationTests/CRUDTests.cs
================================================
using CoreIntegrationTests.Infrastructure;
using System;
namespace CoreIntegrationTests
{
public class CoreTestDocument : TestDoc
{
}
public class CRUDTests : MongoDbDocumentTestBase
{
public CRUDTests(MongoDbTestFixture fixture) : base(fixture)
{
}
public override string GetClassName()
{
return "CRUDTests";
}
}
}
================================================
FILE: CoreIntegrationTests/CoreIntegrationTests.csproj
================================================
net8.0
disable
enable
all
runtime; build; native; contentfiles; analyzers
all
runtime; build; native; contentfiles; analyzers; buildtransitive
Always
================================================
FILE: CoreIntegrationTests/IdentityUserTests.cs
================================================
using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models;
using System;
using System.Collections.Generic;
using Xunit;
using Microsoft.AspNetCore.Identity;
using System.Threading.Tasks;
namespace CoreIntegrationTests
{
public class MongoIdentityUser : IdentityUser, IDocument
where TKey : IEquatable
{
public int Version { get; set; }
}
public class IdentityUserTest : MongoIdentityUser, IDocument
{
public IdentityUserTest()
{
Id = Guid.NewGuid();
Version = 2;
}
public string SomeContent { get; set; }
}
public class IdentityUserTests : BaseMongoDbRepositoryTests
{
[Fact]
public void AddOne()
{
// Arrange
var document = new IdentityUserTest();
// Act
SUT.AddOne(document);
// Assert
long count = SUT.Count(e => e.Id == document.Id);
Assert.Equal(1, count);
}
[Fact]
public async Task AddOneAsync()
{
// Arrange
var document = new IdentityUserTest();
// Act
await SUT.AddOneAsync(document);
// Assert
long count = SUT.Count(e => e.Id == document.Id);
Assert.Equal(1, count);
}
[Fact]
public void AddMany()
{
// Arrange
var documents = new List { new IdentityUserTest(), new IdentityUserTest() };
// Act
SUT.AddMany(documents);
// Assert
long count = SUT.Count(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
Assert.Equal(2, count);
}
[Fact]
public async Task AddManyAsync()
{
// Arrange
var documents = new List { new IdentityUserTest(), new IdentityUserTest() };
// Act
await SUT.AddManyAsync(documents);
// Assert
long count = SUT.Count(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
Assert.Equal(2, count);
}
}
}
================================================
FILE: CoreIntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs
================================================
using MongoDbGenericRepository.Models;
using System.Collections.Generic;
using System;
namespace CoreIntegrationTests.Infrastructure
{
public class BaseMongoDbRepositoryTests : IDisposable where T : new()
{
public T CreateTestDocument()
{
return new T();
}
public List CreateTestDocuments(int numberOfDocumentsToCreate)
{
var docs = new List();
for(var i = 0; i < numberOfDocumentsToCreate; i++)
{
docs.Add(new T());
}
return docs;
}
///
/// Constructor, init code
///
public BaseMongoDbRepositoryTests()
{
Init();
var type = CreateTestDocument();
if (type is IPartitionedDocument)
{
PartitionKey = ((IPartitionedDocument)type).PartitionKey;
}
}
public string PartitionKey { get; set; }
///
/// SUT: System Under Test
///
protected static ITestRepository SUT { get; set; }
public void Init()
{
MongoDbConfig.EnsureConfigured();
SUT = TestRepository.Instance;
}
public void Cleanup()
{
// We drop the collection at the end of each test session.
if (!string.IsNullOrEmpty(PartitionKey))
{
SUT.DropTestCollection(PartitionKey);
}
else
{
SUT.DropTestCollection();
}
}
#region IDisposable Support
private bool _disposedValue; // Pour détecter les appels redondants
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
Cleanup();
}
_disposedValue = true;
}
}
// Ce code est ajouté pour implémenter correctement le modèle supprimable.
public void Dispose()
{
// Ne modifiez pas ce code. Placez le code de nettoyage dans Dispose(bool disposing) ci-dessus.
Dispose(true);
}
#endregion
}
}
================================================
FILE: CoreIntegrationTests/Infrastructure/GlobalVariables.cs
================================================
using System;
namespace CoreIntegrationTests.Infrastructure
{
///
/// A class holding global variables.
///
public static class GlobalVariables
{
///
/// A random number generator.
///
public static Random Random = new Random();
}
}
================================================
FILE: CoreIntegrationTests/Infrastructure/ITestRepository.cs
================================================
using MongoDbGenericRepository;
namespace CoreIntegrationTests
{
public interface ITestRepository : IBaseMongoRepository
{
void DropTestCollection();
void DropTestCollection(string partitionKey);
}
}
================================================
FILE: CoreIntegrationTests/Infrastructure/MongoDbConfig.cs
================================================
using MongoDB.Bson.Serialization.Conventions;
using System.Threading;
namespace CoreIntegrationTests.Infrastructure
{
internal static class MongoDbConfig
{
private static bool _initialized;
private static object _initializationLock = new();
private static object _initializationTarget;
public static void EnsureConfigured()
{
EnsureConfiguredImpl();
}
private static void EnsureConfiguredImpl()
{
LazyInitializer.EnsureInitialized(ref _initializationTarget, ref _initialized, ref _initializationLock, () =>
{
Configure();
return null;
});
}
private static void Configure()
{
RegisterConventions();
}
private static void RegisterConventions()
{
var pack = new ConventionPack
{
new IgnoreIfNullConvention(false),
new CamelCaseElementNameConvention(),
};
}
}
}
================================================
FILE: CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.Main.cs
================================================
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
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
where T : TestDoc, new()
{
private readonly MongoDbTestFixture _fixture;
protected MongoDbDocumentTestBase(MongoDbTestFixture fixture)
{
_fixture = fixture;
var type = CreateTestDocument();
DocumentTypeName = type.GetType().FullName;
if (type is IPartitionedDocument)
{
PartitionKey = ((IPartitionedDocument)type).PartitionKey;
}
_fixture.PartitionKey = PartitionKey;
TestClassName = GetClassName();
MongoDbConfig.EnsureConfigured();
SUT = TestTKeyRepository.Instance;
}
protected T CreateTestDocument()
{
return _fixture.CreateTestDocument();
}
public abstract string GetClassName();
protected List CreateTestDocuments(int numberOfDocumentsToCreate)
{
return _fixture.CreateTestDocuments(numberOfDocumentsToCreate);
}
///
/// The partition key for the collection, if any
///
protected string PartitionKey { get; set; }
///
/// the name of the test class
///
protected string TestClassName { get; set; }
///
/// The name of the document used for tests
///
protected string DocumentTypeName { get; set; }
///
/// SUT: System Under Test
///
protected static ITestRepository SUT { get; set; }
#region Add
[Fact]
public void AddOne()
{
// Arrange
var document = CreateTestDocument();
// Act
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);
Assert.True(1 == count, GetTestName());
}
[Fact]
public async Task AddOneAsync()
{
// Arrange
var document = CreateTestDocument();
// Act
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);
Assert.True(1 == count, GetTestName());
}
[Fact]
public void AddMany()
{
// Arrange
var documents = CreateTestDocuments(2);
// Act
SUT.AddMany(documents);
// Assert
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count(e => e.Id.Equals(documents[0].Id)
|| e.Id.Equals(documents[1].Id))
: SUT.Count(e => e.Id.Equals(documents[0].Id)
|| e.Id.Equals(documents[1].Id), PartitionKey);
Assert.True(2 == count, GetTestName());
}
[Fact]
public void AddManyWithDifferentPartitionKey()
{
// only run this test for tests on documents with partition key
if (!string.IsNullOrEmpty(PartitionKey))
{
// Arrange
var documents = CreateTestDocuments(4);
if (documents.Any(e => e is IPartitionedDocument))
{
var secondPartitionKey = $"{PartitionKey}-2";
((IPartitionedDocument)documents[2]).PartitionKey = secondPartitionKey;
((IPartitionedDocument)documents[3]).PartitionKey = secondPartitionKey;
// Act
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);
// Cleanup second partition
SUT.DeleteMany(e => e.Id.Equals(documents[2].Id) || e.Id.Equals(documents[3].Id), secondPartitionKey);
Assert.True(2 == count, GetTestName());
Assert.True(2 == secondPartitionCount, GetTestName());
}
}
}
[Fact]
public async Task AddManyAsync()
{
// Arrange
var documents = CreateTestDocuments(2);
// Act
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))
: SUT.Count(e => e.Id.Equals(documents[0].Id)
|| e.Id.Equals(documents[1].Id), PartitionKey);
Assert.True(2 == count, GetTestName());
}
[Fact]
public async Task AddManyAsyncWithDifferentPartitionKey()
{
// only run this test for tests on documents with partition key
if (!string.IsNullOrEmpty(PartitionKey))
{
// Arrange
var documents = CreateTestDocuments(4);
if (documents.Any(e => e is IPartitionedDocument))
{
var secondPartitionKey = $"{PartitionKey}-2";
((IPartitionedDocument)documents[2]).PartitionKey = secondPartitionKey;
((IPartitionedDocument)documents[3]).PartitionKey = secondPartitionKey;
// Act
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);
// Cleanup second partition
SUT.DeleteMany(e => e.Id.Equals(documents[2].Id) || e.Id.Equals(documents[3].Id), secondPartitionKey);
Assert.True(2 == count, GetTestName());
Assert.True(2 == secondPartitionCount, GetTestName());
}
}
}
#endregion Add
#region Read
[Fact]
public async Task GetByIdAsync()
{
// Arrange
var document = CreateTestDocument();
await SUT.AddOneAsync(document);
// Act
var result = await SUT.GetByIdAsync(document.Id, PartitionKey);
// Assert
Assert.True(null != result, GetTestName());
}
[Fact]
public void GetById()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = SUT.GetById(document.Id, PartitionKey);
// Assert
Assert.True(null != result, GetTestName());
}
[Fact]
public async Task GetOneAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = await SUT.GetOneAsync(x => x.Id.Equals(document.Id), PartitionKey);
// Assert
Assert.True(null != result, GetTestName());
}
[Fact]
public void GetOne()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = SUT.GetOne(x => x.Id.Equals(document.Id), PartitionKey);
// Assert
Assert.True(null != result, GetTestName());
}
[Fact]
public void GetCursor()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var cursor = SUT.GetCursor(x => x.Id.Equals(document.Id), PartitionKey);
var count = cursor.CountDocuments();
// Assert
Assert.True(1 == count, GetTestName());
}
[Fact]
public async Task AnyAsyncReturnsTrue()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = await SUT.AnyAsync(x => x.Id.Equals(document.Id), PartitionKey);
// Assert
Assert.True(result, GetTestName());
}
[Fact]
public async Task AnyAsyncReturnsFalse()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = await SUT.AnyAsync(x => x.Id.Equals(Guid.NewGuid()), PartitionKey);
// Assert
Assert.False(result, GetTestName());
}
[Fact]
public void AnyReturnsTrue()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = SUT.Any(x => x.Id.Equals(document.Id), PartitionKey);
// Assert
Assert.True(result, GetTestName());
}
[Fact]
public void AnyReturnsFalse()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = SUT.Any(x => x.Id.Equals(Guid.NewGuid()), PartitionKey);
// Assert
Assert.False(result, GetTestName());
}
[Fact]
public async Task GetAllAsync()
{
// Arrange
var documents = CreateTestDocuments(5);
var content = GetContent();
documents.ForEach(e => e.SomeContent = content);
SUT.AddMany(documents);
// Act
var result = await SUT.GetAllAsync(x => x.SomeContent == content, PartitionKey);
// Assert
Assert.True(5 == result.Count, GetTestName());
}
[Fact]
public void GetAll()
{
// Arrange
var documents = CreateTestDocuments(5);
var content = GetContent();
documents.ForEach(e => e.SomeContent = content);
SUT.AddMany(documents);
// Act
var result = SUT.GetAll(x => x.SomeContent == content, PartitionKey);
// Assert
Assert.True(5 == result.Count, GetTestName());
}
[Fact]
public async Task CountAsync()
{
// Arrange
var documents = CreateTestDocuments(5);
var content = GetContent();
documents.ForEach(e => e.SomeContent = content);
SUT.AddMany(documents);
// Act
var result = await SUT.CountAsync(x => x.SomeContent == content, PartitionKey);
// Assert
Assert.True(5 == result, GetTestName());
}
[Fact]
public void Count()
{
// Arrange
var documents = CreateTestDocuments(5);
var content = GetContent();
documents.ForEach(e => e.SomeContent = content);
SUT.AddMany(documents);
// Act
var result = SUT.Count(x => x.SomeContent == content, PartitionKey);
// Assert
Assert.True(5 == result, GetTestName());
}
#endregion Read
#region Delete
[Fact]
public void DeleteOne()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = SUT.DeleteOne(document);
// Assert
Assert.True(1 == result);
Assert.False(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
}
[Fact]
public void DeleteOneLinq()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = SUT.DeleteOne(e => e.Id.Equals(document.Id), PartitionKey);
// Assert
Assert.True(1 == result);
Assert.False(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
}
[Fact]
public async Task DeleteOneAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = await SUT.DeleteOneAsync(document);
// Assert
Assert.True(1 == result);
Assert.False(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
}
[Fact]
public async Task DeleteOneAsyncLinq()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = await SUT.DeleteOneAsync(e => e.Id.Equals(document.Id), PartitionKey);
// Assert
Assert.True(1 == result);
Assert.False(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
}
[Fact]
public async Task DeleteManyAsyncLinq()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}";
var documents = CreateTestDocuments(5);
documents.ForEach(e => e.SomeContent = criteria);
SUT.AddMany(documents);
// Act
var result = await SUT.DeleteManyAsync(e => e.SomeContent == criteria, PartitionKey);
// Assert
Assert.True(5 == result);
Assert.False(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName());
}
[Fact]
public async Task DeleteManyAsync()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}";
var documents = CreateTestDocuments(5);
documents.ForEach(e => e.SomeContent = criteria);
var canPartition = !string.IsNullOrEmpty(PartitionKey) && documents.Any(e => e is IPartitionedDocument);
string secondKey = null;
if (canPartition)
{
secondKey = $"{PartitionKey}-2";
((IPartitionedDocument)documents[3]).PartitionKey = secondKey;
((IPartitionedDocument)documents[4]).PartitionKey = secondKey;
}
SUT.AddMany(documents);
// Act
var result = await SUT.DeleteManyAsync(documents);
// Assert
Assert.True(5 == result);
Assert.False(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName());
if (canPartition)
{
Assert.False(SUT.Any(e => e.SomeContent == criteria, secondKey), GetTestName());
}
}
[Fact]
public void DeleteManyLinq()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}";
var documents = CreateTestDocuments(5);
documents.ForEach(e => e.SomeContent = criteria);
SUT.AddMany(documents);
// Act
var result = SUT.DeleteMany(e => e.SomeContent == criteria, PartitionKey);
// Assert
Assert.True(5 == result);
Assert.False(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName());
}
[Fact]
public void DeleteMany()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}";
var documents = CreateTestDocuments(5);
documents.ForEach(e => e.SomeContent = criteria);
var canPartition = !string.IsNullOrEmpty(PartitionKey) && documents.Any(e => e is IPartitionedDocument);
string secondKey = null;
if (canPartition)
{
secondKey = $"{PartitionKey}-2";
((IPartitionedDocument)documents[3]).PartitionKey = secondKey;
((IPartitionedDocument)documents[4]).PartitionKey = secondKey;
}
SUT.AddMany(documents);
// Act
var result = SUT.DeleteMany(documents);
// Assert
Assert.True(5 == result);
Assert.False(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName());
if (canPartition)
{
Assert.False(SUT.Any(e => e.SomeContent == criteria, secondKey), GetTestName());
}
}
#endregion Delete
#region Project
[Fact]
public async Task ProjectOneAsync()
{
// Arrange
var someContent = GetContent();
var someDate = DateTime.UtcNow;
var document = CreateTestDocument();
document.SomeContent = someContent;
document.Nested.SomeDate = someDate;
SUT.AddOne(document);
// Act
var result = await SUT.ProjectOneAsync(
x => x.Id.Equals(document.Id),
x => new MyTestProjection
{
SomeContent = x.SomeContent,
SomeDate = x.Nested.SomeDate
},
PartitionKey);
// Assert
Assert.True(null != result, GetTestName());
Assert.True(someContent == result.SomeContent, GetTestName());
Assert.True(someDate.Minute == result.SomeDate.Minute, GetTestName());
Assert.True(someDate.Second == result.SomeDate.Second, GetTestName());
}
[Fact]
public void ProjectOne()
{
// Arrange
var someContent = GetContent();
var someDate = DateTime.UtcNow;
var document = CreateTestDocument();
document.SomeContent = someContent;
document.Nested.SomeDate = someDate;
SUT.AddOne(document);
// Act
var result = SUT.ProjectOne(
x => x.Id.Equals(document.Id),
x => new MyTestProjection
{
SomeContent = x.SomeContent,
SomeDate = x.Nested.SomeDate
},
PartitionKey);
// Assert
Assert.True(null != result, GetTestName());
Assert.True(someContent == result.SomeContent, GetTestName());
Assert.True(someDate.Minute == result.SomeDate.Minute, GetTestName());
Assert.True(someDate.Second == result.SomeDate.Second, GetTestName());
}
[Fact]
public async Task ProjectManyAsync()
{
// Arrange
var someContent = GetContent();
var someDate = DateTime.UtcNow;
var documents = CreateTestDocuments(5);
documents.ForEach(e =>
{
e.SomeContent = someContent;
e.Nested.SomeDate = someDate;
});
SUT.AddMany(documents);
// Act
var result = await SUT.ProjectManyAsync(
x => x.SomeContent == someContent,
x => new MyTestProjection
{
SomeContent = x.SomeContent,
SomeDate = x.Nested.SomeDate
},
PartitionKey);
// Assert
Assert.True(5 == result.Count, GetTestName());
Assert.True(someContent == result.First().SomeContent, GetTestName());
Assert.True(someDate.Minute == result.First().SomeDate.Minute, GetTestName());
Assert.True(someDate.Second == result.First().SomeDate.Second, GetTestName());
}
[Fact]
public void ProjectMany()
{
// Arrange
var someContent = GetContent();
var someDate = DateTime.UtcNow;
var documents = CreateTestDocuments(5);
documents.ForEach(e =>
{
e.SomeContent = someContent;
e.Nested.SomeDate = someDate;
});
SUT.AddMany(documents);
// Act
var result = SUT.ProjectMany(
x => x.SomeContent == someContent,
x => new MyTestProjection
{
SomeContent = x.SomeContent,
SomeDate = x.Nested.SomeDate
},
PartitionKey);
// Assert
Assert.True(5 == result.Count, GetTestName());
Assert.True(someContent == result.First().SomeContent, GetTestName());
Assert.True(someDate.Minute == result.First().SomeDate.Minute, GetTestName());
Assert.True(someDate.Second == result.First().SomeDate.Second, GetTestName());
}
#endregion Project
#region Max / Min Queries
[Fact]
public async Task GetByMaxAsync()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5);
var i = 1;
documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.SomeContent = criteria;
});
SUT.AddMany(documents);
var expectedMax = documents.OrderByDescending(e => e.Nested.SomeDate).First();
// Act
var result = await SUT.GetByMaxAsync(e => e.SomeContent == criteria, e => e.Nested.SomeDate, PartitionKey);
// Assert
Assert.NotNull(result);
Assert.Equal(expectedMax.Id, result.Id);
}
[Fact]
public void GetByMax()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5);
var i = 1;
documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.SomeContent = criteria;
});
SUT.AddMany(documents);
var expectedMax = documents.OrderByDescending(e => e.Nested.SomeDate).First();
// Act
var result = SUT.GetByMax(e => e.SomeContent == criteria, e => e.Nested.SomeDate, PartitionKey);
// Assert
Assert.NotNull(result);
Assert.Equal(expectedMax.Id, result.Id);
}
[Fact]
public async Task GetByMinAsync()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5);
var i = 1;
documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.SomeContent = criteria;
});
SUT.AddMany(documents);
var expectedMin = documents.OrderBy(e => e.Nested.SomeDate).First();
// Act
var result = await SUT.GetByMinAsync(e => e.SomeContent == criteria, e => e.Nested.SomeDate, PartitionKey);
// Assert
Assert.NotNull(result);
Assert.Equal(expectedMin.Id, result.Id);
}
[Fact]
public void GetByMin()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5);
var i = 1;
documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.SomeContent = criteria;
});
SUT.AddMany(documents);
var expectedMin = documents.OrderBy(e => e.Nested.SomeDate).First();
// Act
var result = SUT.GetByMin(e => e.SomeContent == criteria, e => e.Nested.SomeDate, PartitionKey);
// Assert
Assert.NotNull(result);
Assert.Equal(expectedMin.Id, result.Id);
}
[Fact]
public void GetMinValue()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5);
var i = 1;
documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.SomeContent = criteria;
});
SUT.AddMany(documents);
var expectedMin = documents.OrderBy(e => e.Nested.SomeDate).First();
// Act
var result = SUT.GetMinValue(e => e.SomeContent == criteria, e => e.Nested.SomeDate, PartitionKey);
// Assert
Assert.True(result != default(DateTime));
Assert.Equal(expectedMin.Nested.SomeDate.Date, result.Date);
}
[Fact]
public async Task GetMinValueAsync()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5);
var i = 1;
documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.SomeContent = criteria;
});
SUT.AddMany(documents);
var expectedMin = documents.OrderBy(e => e.Nested.SomeDate).First();
// Act
var result = await SUT.GetMinValueAsync(e => e.SomeContent == criteria, e => e.Nested.SomeDate, PartitionKey);
// Assert
Assert.True(result != default(DateTime));
Assert.Equal(expectedMin.Nested.SomeDate.Date, result.Date);
}
[Fact]
public void GetMaxValue()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5);
var i = 1;
documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.SomeContent = criteria;
});
SUT.AddMany(documents);
var expectedMax = documents.OrderByDescending(e => e.Nested.SomeDate).First();
// Act
var result = SUT.GetMaxValue(e => e.SomeContent == criteria, e => e.Nested.SomeDate, PartitionKey);
// Assert
Assert.True(result != default(DateTime));
Assert.Equal(expectedMax.Nested.SomeDate.Date, result.Date);
}
[Fact]
public async Task GetMaxValueAsync()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5);
var i = 1;
documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.SomeContent = criteria;
});
SUT.AddMany(documents);
var expectedMin = documents.OrderByDescending(e => e.Nested.SomeDate).First();
// Act
var result = await SUT.GetMaxValueAsync(e => e.SomeContent == criteria, e => e.Nested.SomeDate, PartitionKey);
// Assert
Assert.True(result != default(DateTime));
Assert.Equal(expectedMin.Nested.SomeDate.Date, result.Date);
}
#endregion Max / Min Queries
#region Index Management
static SemaphoreSlim textIndexSemaphore = new SemaphoreSlim(1, 1);
[Fact]
public async Task CreateTextIndexNoOptionAsync()
{
// Arrange
const string expectedIndexName = "SomeContent_text";
// Act
await textIndexSemaphore.WaitAsync();
try
{
var result = await SUT.CreateTextIndexAsync(x => x.SomeContent, null, PartitionKey);
// Assert
var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey);
Assert.Contains(expectedIndexName, listOfIndexNames);
// Cleanup
await SUT.DropIndexAsync(expectedIndexName, PartitionKey);
}
finally
{
textIndexSemaphore.Release();
}
}
[Fact]
public async Task CreateTextIndexWithOptionAsync()
{
// Arrange
string expectedIndexName = $"{Guid.NewGuid()}";
var option = new IndexCreationOptions
{
Name = expectedIndexName
};
await textIndexSemaphore.WaitAsync();
try
{
// Act
var result = await SUT.CreateTextIndexAsync(x => x.AddedAtUtc, option, PartitionKey);
// Assert
var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey);
Assert.Contains(expectedIndexName, listOfIndexNames);
// Cleanup
await SUT.DropIndexAsync(expectedIndexName, PartitionKey);
}
finally
{
textIndexSemaphore.Release();
}
}
[Fact]
public async Task CreateAscendingIndexAsync()
{
// Arrange
const string expectedIndexName = "SomeContent_1";
// Act
var result = await SUT.CreateAscendingIndexAsync(x => x.SomeContent, null, PartitionKey);
// Assert
var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey);
Assert.Contains(expectedIndexName, listOfIndexNames);
// Cleanup
await SUT.DropIndexAsync(expectedIndexName, PartitionKey);
}
[Fact]
public async Task CreateDescendingIndexAsync()
{
// Arrange
const string expectedIndexName = "SomeContent_-1";
// Act
var result = await SUT.CreateDescendingIndexAsync(x => x.SomeContent, null, PartitionKey);
// Assert
var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey);
Assert.Contains(expectedIndexName, listOfIndexNames);
// Cleanup
await SUT.DropIndexAsync(expectedIndexName, PartitionKey);
}
[Fact]
public async Task CreateHashedIndexAsync()
{
// Arrange
const string expectedIndexName = "SomeContent_hashed";
// Act
var result = await SUT.CreateHashedIndexAsync(x => x.SomeContent, null, PartitionKey);
// Assert
var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey);
Assert.Contains(expectedIndexName, listOfIndexNames);
// Cleanup
await SUT.DropIndexAsync(expectedIndexName, PartitionKey);
}
[Fact]
public async Task CreateCombinedTextIndexAsync()
{
// Arrange
const string expectedIndexName = "SomeContent2_text_SomeContent3_text";
// Act
Expression > ex = x => x.SomeContent2;
Expression > ex2 = x => x.SomeContent3;
var result = await SUT.CreateCombinedTextIndexAsync(new[] { ex, ex2 }, null, PartitionKey);
// Assert
var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey);
Assert.Contains(expectedIndexName, listOfIndexNames);
// Cleanup
await SUT.DropIndexAsync(expectedIndexName, PartitionKey);
}
#endregion Index Management
#region Math
[Fact]
public async Task SumByDecimalAsync()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5);
var i = 1;
documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.Nested.SomeAmount = 5m;
e.SomeContent = criteria;
});
SUT.AddMany(documents);
var expectedSum = documents.Sum(e => e.Nested.SomeAmount);
// Act
var result = await SUT.SumByAsync(e => e.SomeContent == criteria, e => e.Nested.SomeAmount, PartitionKey);
// Assert
Assert.Equal(expectedSum, result);
}
[Fact]
public void SumByDecimal()
{
// Arrange
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5);
var i = 1;
documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.Nested.SomeAmount = 5m;
e.SomeContent = criteria;
});
SUT.AddMany(documents);
var expectedSum = documents.Sum(e => e.Nested.SomeAmount);
// Act
var result = SUT.SumBy(e => e.SomeContent == criteria, e => e.Nested.SomeAmount, PartitionKey);
// Assert
Assert.Equal(expectedSum, result);
}
#endregion Math
#region Test Utils
[MethodImpl(MethodImplOptions.NoInlining)]
private string GetCurrentMethod()
{
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(1);
return sf.GetMethod().Name;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private string GetParentMethod()
{
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(2);
var method = sf.GetMethod().DeclaringType.Name;
return method;
}
private string GetTestName()
{
return $"{TestClassName}{PartitionKey}.{GetParentMethod()}";
}
private string GetContent()
{
return $"{TestClassName}{PartitionKey}.{Guid.NewGuid()}.{GetParentMethod()}";
}
private void Cleanup()
{
// We drop the collection at the end of each test session.
if (!string.IsNullOrEmpty(PartitionKey))
{
SUT.DropTestCollection(PartitionKey);
}
else
{
SUT.DropTestCollection();
}
}
#endregion Test Utils
}
}
================================================
FILE: CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.Update.cs
================================================
using MongoDB.Driver;
using MongoDbGenericRepository;
using MongoDbGenericRepository.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace CoreIntegrationTests.Infrastructure
{
public abstract partial class MongoDbDocumentTestBase :
IClassFixture>
where T : TestDoc, new()
{
#region Update One
[Fact]
public void UpdateOne()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
var content = GetContent();
document.SomeContent = content;
// Act
var result = SUT.UpdateOne(document);
// Assert
Assert.True(result);
var updatedDocument = SUT.GetById(document.Id, PartitionKey);
Assert.True(null != updatedDocument, GetTestName());
Assert.True(content == updatedDocument.SomeContent, GetTestName());
}
[Fact]
public async Task UpdateOneAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
var content = GetContent();
document.SomeContent = content;
// Act
var result = await SUT.UpdateOneAsync(document);
// Assert
Assert.True(result);
var updatedDocument = SUT.GetById(document.Id, PartitionKey);
Assert.True(null != updatedDocument, GetTestName());
Assert.True(content == updatedDocument.SomeContent, GetTestName());
}
[Fact]
public void UpdateOneField()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
var content = GetContent();
// Act
var result = SUT.UpdateOne(document, x => x.SomeContent, content);
// Assert
Assert.True(result, GetTestName());
var updatedDocument = SUT.GetById(document.Id, PartitionKey);
Assert.True(null != updatedDocument, GetTestName());
Assert.True(content == updatedDocument.SomeContent, GetTestName());
}
[Fact]
public async Task UpdateOneFieldAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
var content = GetContent();
// Act
var result = await SUT.UpdateOneAsync(document, x => x.SomeContent, content);
// Assert
Assert.True(result, GetTestName());
var updatedDocument = SUT.GetById(document.Id, PartitionKey);
Assert.True(null != updatedDocument, GetTestName());
Assert.True(content == updatedDocument.SomeContent, GetTestName());
}
[Fact]
public void UpdateOneFieldWithFilter()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
var content = GetContent();
// Act
var result = SUT.UpdateOne(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
// Assert
Assert.True(result, GetTestName());
var updatedDocument = SUT.GetById(document.Id, PartitionKey);
Assert.True(null != updatedDocument, GetTestName());
Assert.True(content == updatedDocument.SomeContent, GetTestName());
}
[Fact]
public async Task UpdateOneFieldWithFilterAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
var content = GetContent();
// Act
var result = await SUT.UpdateOneAsync(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
// Assert
Assert.True(result, GetTestName());
var updatedDocument = SUT.GetById(document.Id, PartitionKey);
Assert.True(null != updatedDocument, GetTestName());
Assert.True(content == updatedDocument.SomeContent, GetTestName());
}
[Fact]
public async Task UpdateOneAsyncWithUpdateDefinition()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
var childrenToAdd = new List
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var updateDef = MongoDB.Driver.Builders.Update.AddToSetEach(p => p.Children, childrenToAdd);
// Act
var result = await SUT.UpdateOneAsync(document, updateDef);
// Assert
Assert.True(result);
var updatedDocument = SUT.GetById(document.Id, PartitionKey);
Assert.True(null != updatedDocument);
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
}
[Fact]
public void UpdateOneWithUpdateDefinition()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
var childrenToAdd = new List
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var updateDef = MongoDB.Driver.Builders.Update.AddToSetEach(p => p.Children, childrenToAdd);
// Act
var result = SUT.UpdateOne(document, updateDef);
// Assert
Assert.True(result);
var updatedDocument = SUT.GetById(document.Id, PartitionKey);
Assert.True(null != updatedDocument);
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
}
#endregion Update One
#region Update Many
[Fact]
public async Task UpdateManyWithLinqFilterAsync()
{
// Arrange
var documents = CreateTestDocuments(2);
SUT.AddMany(documents);
var docIds = documents.Select(u => u.Id).ToArray();
var content = GetContent();
// Act
var result = await SUT.UpdateManyAsync(x => docIds.Contains(x.Id), x => x.SomeContent, content, PartitionKey);
// Assert
Assert.True(result == 2, GetTestName());
var updatedDocument = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey);
Assert.True(updatedDocument.Count == 2);
Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName());
}
[Fact]
public async Task UpdateManyWithFilterDefinitionAsync()
{
// Arrange
var documents = CreateTestDocuments(2);
SUT.AddMany(documents);
var docIds = documents.Select(u => u.Id).ToArray();
var filterDefinition = Builders.Filter.Where(x => docIds.Contains(x.Id));
var content = GetContent();
// Act
var result = await SUT.UpdateManyAsync(filterDefinition, x => x.SomeContent, content, PartitionKey);
// Assert
Assert.True(result == 2, GetTestName());
var updatedDocument = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey);
Assert.True(updatedDocument.Count == 2);
Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName());
}
[Fact]
public async Task UpdateManyWithLinqFilterAndUpdateDefinitionAsync()
{
// Arrange
var documents = CreateTestDocuments(2);
SUT.AddMany(documents);
var docIds = documents.Select(u => u.Id).ToArray();
var childrenToAdd = new List
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var updateDef = Builders.Update.AddToSetEach(p => p.Children, childrenToAdd);
var content = GetContent();
// Act
var result = await SUT.UpdateManyAsync(x => docIds.Contains(x.Id), updateDef, PartitionKey);
// Assert
Assert.True(result == 2, GetTestName());
var updatedDocuments = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey);
Assert.True(updatedDocuments.Count == 2);
updatedDocuments.ForEach(updatedDocument =>
{
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
});
}
[Fact]
public async Task UpdateManyWithFilterAndUpdateDefinitionsAsync()
{
// Arrange
var documents = CreateTestDocuments(2);
SUT.AddMany(documents);
var childrenToAdd = new List
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var updateDef = Builders.Update.AddToSetEach(p => p.Children, childrenToAdd);
var docIds = documents.Select(u => u.Id).ToArray();
var filterDefinition = Builders.Filter.Where(x => docIds.Contains(x.Id));
var content = GetContent();
// Act
var result = await SUT.UpdateManyAsync(filterDefinition, updateDef, PartitionKey);
// Assert
Assert.True(result == 2, GetTestName());
var updatedDocuments = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey);
Assert.True(updatedDocuments.Count == 2);
updatedDocuments.ForEach(updatedDocument =>
{
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
});
}
[Fact]
public void UpdateManyWithLinqFilter()
{
// Arrange
var documents = CreateTestDocuments(2);
SUT.AddMany(documents);
var docIds = documents.Select(u => u.Id).ToArray();
var content = GetContent();
// Act
var result = SUT.UpdateMany(x => docIds.Contains(x.Id), x => x.SomeContent, content, PartitionKey);
// Assert
Assert.True(result == 2, GetTestName());
var updatedDocument = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey);
Assert.True(updatedDocument.Count == 2);
Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName());
}
[Fact]
public void UpdateManyWithFilterDefinition()
{
// Arrange
var documents = CreateTestDocuments(2);
SUT.AddMany(documents);
var docIds = documents.Select(u => u.Id).ToArray();
var filterDefinition = Builders.Filter.Where(x => docIds.Contains(x.Id));
var content = GetContent();
// Act
var result = SUT.UpdateMany(filterDefinition, x => x.SomeContent, content, PartitionKey);
// Assert
Assert.True(result == 2, GetTestName());
var updatedDocument = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey);
Assert.True(updatedDocument.Count == 2);
Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName());
}
[Fact]
public void UpdateManyWithLinqFilterAndUpdateDefinition()
{
// Arrange
var documents = CreateTestDocuments(2);
SUT.AddMany(documents);
var docIds = documents.Select(u => u.Id).ToArray();
var childrenToAdd = new List
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var updateDef = Builders.Update.AddToSetEach(p => p.Children, childrenToAdd);
var content = GetContent();
// Act
var result = SUT.UpdateMany(x => docIds.Contains(x.Id), updateDef, PartitionKey);
// Assert
Assert.True(result == 2, GetTestName());
var updatedDocuments = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey);
Assert.True(updatedDocuments.Count == 2);
updatedDocuments.ForEach(updatedDocument =>
{
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
});
}
[Fact]
public void UpdateManyWithFilterAndUpdateDefinitions()
{
// Arrange
var documents = CreateTestDocuments(2);
SUT.AddMany(documents);
var childrenToAdd = new List
{
new Child("testType1", "testValue1"),
new Child("testType2", "testValue2")
};
var updateDef = Builders.Update.AddToSetEach(p => p.Children, childrenToAdd);
var docIds = documents.Select(u => u.Id).ToArray();
var filterDefinition = Builders.Filter.Where(x => docIds.Contains(x.Id));
var content = GetContent();
// Act
var result = SUT.UpdateMany(filterDefinition, updateDef, PartitionKey);
// Assert
Assert.True(result == 2, GetTestName());
var updatedDocuments = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey);
Assert.True(updatedDocuments.Count == 2);
updatedDocuments.ForEach(updatedDocument =>
{
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
});
}
#endregion Update Many
}
}
================================================
FILE: CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.Main.cs
================================================
using MongoDB.Driver;
using MongoDbGenericRepository.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace CoreIntegrationTests.Infrastructure
{
public abstract partial class MongoDbTKeyDocumentTestBase :
IClassFixture>
where T : TestDoc, new()
where TKey : IEquatable
{
private readonly MongoDbTestFixture _fixture;
protected MongoDbTKeyDocumentTestBase(MongoDbTestFixture fixture)
{
_fixture = fixture;
var type = CreateTestDocument();
DocumentTypeName = type.GetType().FullName;
if (type is IPartitionedDocument)
{
PartitionKey = ((IPartitionedDocument)type).PartitionKey;
}
_fixture.PartitionKey = PartitionKey;
TestClassName = GetClassName();
MongoDbConfig.EnsureConfigured();
SUT = TestRepository.Instance;
}
public abstract string GetClassName();
public T CreateTestDocument()
{
return _fixture.CreateTestDocument();
}
public List CreateTestDocuments(int numberOfDocumentsToCreate)
{
return _fixture.CreateTestDocuments(numberOfDocumentsToCreate);
}
///
/// The partition key for the collection, if any
///
protected string PartitionKey { get; set; }
///
/// the name of the test class
///
protected string TestClassName { get; set; }
///
/// The name of the document used for tests
///
protected string DocumentTypeName { get; set; }
///
/// SUT: System Under Test
///
protected static ITestRepository SUT { get; set; }
#region Add
[Fact]
public void AddOne()
{
// Arrange
var document = CreateTestDocument();
// Act
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);
Assert.True(1 == count, GetTestName());
}
[Fact]
public async Task AddOneAsync()
{
// Arrange
var document = CreateTestDocument();
// Act
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);
Assert.True(1 == count, GetTestName());
}
[Fact]
public void AddMany()
{
// Arrange
var documents = CreateTestDocuments(2);
// Act
SUT.AddMany(documents);
// Assert
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count(e => e.Id.Equals(documents[0].Id)
|| e.Id.Equals(documents[1].Id))
: SUT.Count(e => e.Id.Equals(documents[0].Id)
|| e.Id.Equals(documents[1].Id), PartitionKey);
Assert.True(2 == count, GetTestName());
}
[Fact]
public void AddManyWithDifferentPartitionKey()
{
// only run this test for tests on documents with partition key
if (!string.IsNullOrEmpty(PartitionKey))
{
// Arrange
var documents = CreateTestDocuments(4);
if (documents.Any(e => e is IPartitionedDocument))
{
var secondPartitionKey = $"{PartitionKey}-2";
((IPartitionedDocument)documents[2]).PartitionKey = secondPartitionKey;
((IPartitionedDocument)documents[3]).PartitionKey = secondPartitionKey;
// Act
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);
// Cleanup second partition
SUT.DeleteMany(e => e.Id.Equals(documents[2].Id) || e.Id.Equals(documents[3].Id), secondPartitionKey);
Assert.True(2 == count, GetTestName());
Assert.True(2 == secondPartitionCount, GetTestName());
}
}
}
[Fact]
public async Task AddManyAsync()
{
// Arrange
var documents = CreateTestDocuments(2);
// Act
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))
: SUT.Count(e => e.Id.Equals(documents[0].Id)
|| e.Id.Equals(documents[1].Id), PartitionKey);
Assert.True(2 == count, GetTestName());
}
[Fact]
public async Task AddManyAsyncWithDifferentPartitionKey()
{
// only run this test for tests on documents with partition key
if (!string.IsNullOrEmpty(PartitionKey))
{
// Arrange
var documents = CreateTestDocuments(4);
if (documents.Any(e => e is IPartitionedDocument))
{
var secondPartitionKey = $"{PartitionKey}-2";
((IPartitionedDocument)documents[2]).PartitionKey = secondPartitionKey;
((IPartitionedDocument)documents[3]).PartitionKey = secondPartitionKey;
// Act
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);
// Cleanup second partition
SUT.DeleteMany(e => e.Id.Equals(documents[2].Id) || e.Id.Equals(documents[3].Id), secondPartitionKey);
Assert.True(2 == count, GetTestName());
Assert.True(2 == secondPartitionCount, GetTestName());
}
}
}
#endregion Add
#region Read
[Fact]
public async Task GetByIdAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = await SUT.GetByIdAsync(document.Id, PartitionKey);
// Assert
Assert.True(null != result, GetTestName());
}
[Fact]
public void GetById()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = SUT.GetById(document.Id, PartitionKey);
// Assert
Assert.True(null != result, GetTestName());
}
[Fact]
public async Task GetOneByFilterDefinitionAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = await SUT.GetOneAsync(Builders.Filter.Eq(x => x.Id, document.Id), null, PartitionKey);
// Assert
Assert.True(null != result, GetTestName());
}
[Fact]
public void GetOneByFilterDefinition()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = SUT.GetOne(Builders.Filter.Eq(x => x.Id, document.Id), null, PartitionKey);
// Assert
Assert.True(null != result, GetTestName());
}
[Fact]
public async Task GetOneAsync()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = await SUT.GetOneAsync(x => x.Id.Equals(document.Id), PartitionKey);
// Assert
Assert.True(null != result, GetTestName());
}
[Fact]
public void GetOne()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = SUT.GetOne(x => x.Id.Equals(document.Id), PartitionKey);
// Assert
Assert.True(null != result, GetTestName());
}
[Fact]
public void GetCursor()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var cursor = SUT.GetCursor(x => x.Id.Equals(document.Id), PartitionKey);
var count = cursor.CountDocuments();
// Assert
Assert.True(1 == count, GetTestName());
}
[Fact]
public async Task AnyAsyncByDefinitionReturnsTrue()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = await SUT.AnyAsync(Builders.Filter.Eq(x => x.Id, document.Id), null, PartitionKey);
// Assert
Assert.True(result, GetTestName());
}
[Fact]
public async Task AnyAsyncByDefinitionReturnsFalse()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = await SUT.AnyAsync(Builders.Filter.Eq(x => x.Id, document.Init()), null, PartitionKey);
// Assert
Assert.False(result, GetTestName());
}
[Fact]
public async Task AnyAsyncReturnsTrue()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = await SUT.AnyAsync(x => x.Id.Equals(document.Id), PartitionKey);
// Assert
Assert.True(result, GetTestName());
}
[Fact]
public async Task AnyAsyncReturnsFalse()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = await SUT.AnyAsync(x => x.Id.Equals(document.Init()), PartitionKey);
// Assert
Assert.False(result, GetTestName());
}
[Fact]
public void AnyByDefinitionReturnsTrue()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = SUT.Any(Builders.Filter.Eq(x => x.Id, document.Id), null, PartitionKey);
// Assert
Assert.True(result, GetTestName());
}
[Fact]
public void AnyByDefinitionReturnsFalse()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = SUT.Any(Builders.Filter.Eq(x => x.Id, document.Init