From fbb07475a1b8618a11bb7a63871cf1580fac4903 Mon Sep 17 00:00:00 2001 From: alexandre-spieser Date: Sun, 29 Oct 2017 19:29:43 +0000 Subject: [PATCH] Added tests for IdentityUsers --- .../CoreIntegrationTests.csproj | 8 +- CoreIntegrationTests/IdentityUserTests.cs | 79 +++++++++++++++++++ .../BaseMongoDbRepositoryTests.cs | 3 +- .../Infrastructure/MongoDbConfig.cs | 43 ++++++++++ MongoDbGenericRepository/Models/IDocument.cs | 3 +- MongoDbGenericRepository/_rels/.rels | 5 ++ 6 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 CoreIntegrationTests/IdentityUserTests.cs create mode 100644 CoreIntegrationTests/Infrastructure/MongoDbConfig.cs create mode 100644 MongoDbGenericRepository/_rels/.rels diff --git a/CoreIntegrationTests/CoreIntegrationTests.csproj b/CoreIntegrationTests/CoreIntegrationTests.csproj index 4edac06..c189edc 100644 --- a/CoreIntegrationTests/CoreIntegrationTests.csproj +++ b/CoreIntegrationTests/CoreIntegrationTests.csproj @@ -5,11 +5,13 @@ + + - - - + + + diff --git a/CoreIntegrationTests/IdentityUserTests.cs b/CoreIntegrationTests/IdentityUserTests.cs new file mode 100644 index 0000000..04771b9 --- /dev/null +++ b/CoreIntegrationTests/IdentityUserTests.cs @@ -0,0 +1,79 @@ +using IntegrationTests.Infrastructure; +using MongoDB.Bson.Serialization.Attributes; +using MongoDbGenericRepository.Models; +using System; +using System.Collections.Generic; +using System.Text; +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); + } + } +} diff --git a/CoreIntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs b/CoreIntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs index 590b6de..e8a31f9 100644 --- a/CoreIntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs +++ b/CoreIntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs @@ -5,7 +5,7 @@ using System; namespace IntegrationTests.Infrastructure { - public class BaseMongoDbRepositoryTests : IDisposable where T : Document, new() + public class BaseMongoDbRepositoryTests : IDisposable where T : new() { public T CreateTestDocument() { @@ -44,6 +44,7 @@ namespace IntegrationTests.Infrastructure public void Init() { + MongoDbConfig.EnsureConfigured(); SUT = TestRepository.Instance; } diff --git a/CoreIntegrationTests/Infrastructure/MongoDbConfig.cs b/CoreIntegrationTests/Infrastructure/MongoDbConfig.cs new file mode 100644 index 0000000..7dd3e84 --- /dev/null +++ b/CoreIntegrationTests/Infrastructure/MongoDbConfig.cs @@ -0,0 +1,43 @@ +using CoreIntegrationTests; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Conventions; +using System.Threading; + +namespace IntegrationTests.Infrastructure +{ + internal static class MongoDbConfig + { + private static bool _initialized = false; + private static object _initializationLock = new object(); + 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(), + }; + } + + } +} diff --git a/MongoDbGenericRepository/Models/IDocument.cs b/MongoDbGenericRepository/Models/IDocument.cs index 3142dfb..d034223 100644 --- a/MongoDbGenericRepository/Models/IDocument.cs +++ b/MongoDbGenericRepository/Models/IDocument.cs @@ -13,7 +13,6 @@ namespace MongoDbGenericRepository.Models /// The Primary Key, which must be decorated with the [BsonId] attribute /// if you want the MongoDb C# driver to consider it to be the document ID. /// - [BsonId] TKey Id { get; set; } /// /// A version number, to indicate the version of the schema. @@ -25,7 +24,7 @@ namespace MongoDbGenericRepository.Models /// This class represents a basic document that can be stored in MongoDb. /// Your document must implement this class in order for the MongoDbRepository to handle them. /// - public interface IDocument: IDocument + public interface IDocument : IDocument { } } \ No newline at end of file diff --git a/MongoDbGenericRepository/_rels/.rels b/MongoDbGenericRepository/_rels/.rels new file mode 100644 index 0000000..018fd40 --- /dev/null +++ b/MongoDbGenericRepository/_rels/.rels @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file