From 1a83abd25fc6aefe0ad9d48d1c53b08d17d7c77c Mon Sep 17 00:00:00 2001 From: alexandre-spieser Date: Mon, 5 Feb 2018 23:48:11 +0000 Subject: [PATCH] refactoring test classes. --- IntegrationTests/CRUDTKeyPartitionedTests.cs | 26 ++ IntegrationTests/CRUDTKeyTests.cs | 21 ++ .../CreateTKeyPartitionedTests.cs | 76 ---- IntegrationTests/CreateTKeyTests.cs | 75 ---- .../DeletePartitionedTKeyTests.cs | 137 ------- IntegrationTests/DeleteTKeyTests.cs | 135 ------- .../Infrastructure/GlobalVariables.cs | 15 + .../Infrastructure/MongoDBTestBase.cs | 335 ++++++++++++++++++ .../Infrastructure/RandomExtensions.cs | 61 ++++ IntegrationTests/IntegrationTests.csproj | 37 +- ...PartitionedCollectionNameAttributeTests.cs | 27 ++ IntegrationTests/packages.config | 1 - .../Attributes/CollectionNameAttribute.cs | 7 + 13 files changed, 508 insertions(+), 445 deletions(-) create mode 100644 IntegrationTests/CRUDTKeyPartitionedTests.cs create mode 100644 IntegrationTests/CRUDTKeyTests.cs delete mode 100644 IntegrationTests/CreateTKeyPartitionedTests.cs delete mode 100644 IntegrationTests/CreateTKeyTests.cs delete mode 100644 IntegrationTests/DeletePartitionedTKeyTests.cs delete mode 100644 IntegrationTests/DeleteTKeyTests.cs create mode 100644 IntegrationTests/Infrastructure/GlobalVariables.cs create mode 100644 IntegrationTests/Infrastructure/MongoDBTestBase.cs create mode 100644 IntegrationTests/Infrastructure/RandomExtensions.cs create mode 100644 IntegrationTests/TKeyPartitionedCollectionNameAttributeTests.cs diff --git a/IntegrationTests/CRUDTKeyPartitionedTests.cs b/IntegrationTests/CRUDTKeyPartitionedTests.cs new file mode 100644 index 0000000..7a8de0c --- /dev/null +++ b/IntegrationTests/CRUDTKeyPartitionedTests.cs @@ -0,0 +1,26 @@ +using IntegrationTests.Infrastructure; +using MongoDB.Bson.Serialization.Attributes; +using MongoDbGenericRepository.Models; +using NUnit.Framework; +using System; + +namespace IntegrationTests +{ + public class PartitionedTKeyTestDocument : TestDoc, IPartitionedDocument + { + public PartitionedTKeyTestDocument() + { + PartitionKey = "TestPartitionKey"; + } + public string PartitionKey { get; set; } + } + + [TestFixture] + public class CRUDTKeyPartitionedTests : MongoDBTestBase + { + public override string GetClassName() + { + return "CRUDTKeyPartitionedTests"; + } + } +} diff --git a/IntegrationTests/CRUDTKeyTests.cs b/IntegrationTests/CRUDTKeyTests.cs new file mode 100644 index 0000000..564d67c --- /dev/null +++ b/IntegrationTests/CRUDTKeyTests.cs @@ -0,0 +1,21 @@ +using IntegrationTests.Infrastructure; +using MongoDB.Bson.Serialization.Attributes; +using MongoDbGenericRepository.Models; +using NUnit.Framework; +using System; + +namespace IntegrationTests +{ + public class TKeyTestDocument : TestDoc + { + } + + [TestFixture] + public class CRUDTKeyTests : MongoDBTestBase + { + public override string GetClassName() + { + return "CreateTKeyTests"; + } + } +} diff --git a/IntegrationTests/CreateTKeyPartitionedTests.cs b/IntegrationTests/CreateTKeyPartitionedTests.cs deleted file mode 100644 index 4d00dd5..0000000 --- a/IntegrationTests/CreateTKeyPartitionedTests.cs +++ /dev/null @@ -1,76 +0,0 @@ -using IntegrationTests.Infrastructure; -using MongoDB.Bson.Serialization.Attributes; -using MongoDbGenericRepository.Models; -using NUnit.Framework; -using System; -using System.Collections.Generic; -using System.Threading.Tasks; - -namespace IntegrationTests -{ - public class CreateTestsPartitionedTKeyDocument : IDocument, IPartitionedDocument - { - [BsonId] - public Guid Id { get; set; } - public int Version { get; set; } - public CreateTestsPartitionedTKeyDocument() - { - Id = Guid.NewGuid(); - Version = 2; - PartitionKey = "TestPartitionKey"; - } - public string PartitionKey { get; set; } - public string SomeContent { get; set; } - } - - public class CreatePartitionedTKeyTests : BaseMongoDbRepositoryTests - { - [Test] - public void PartitionedAddOne() - { - // Arrange - var document = new CreateTestsPartitionedTKeyDocument(); - // Act - SUT.AddOne(document); - // Assert - long count = SUT.Count(e => e.Id == document.Id, PartitionKey); - Assert.AreEqual(1, count); - } - - [Test] - public async Task PartitionedAddOneAsync() - { - // Arrange - var document = new CreateTestsPartitionedTKeyDocument(); - // Act - await SUT.AddOneAsync(document); - // Assert - long count = SUT.Count(e => e.Id == document.Id, PartitionKey); - Assert.AreEqual(1, count); - } - - [Test] - public void PartitionedAddMany() - { - // Arrange - var documents = new List { new CreateTestsPartitionedTKeyDocument(), new CreateTestsPartitionedTKeyDocument() }; - // Act - SUT.AddMany(documents); - // Assert - long count = SUT.Count(e => e.Id == documents[0].Id || e.Id == documents[1].Id, PartitionKey); - Assert.AreEqual(2, count); - } - - [Test] - public async Task PartitionedAddManyAsync() - { - // Arrange - var documents = new List { new CreateTestsPartitionedTKeyDocument(), new CreateTestsPartitionedTKeyDocument() }; - // Act - await SUT.AddManyAsync(documents); - // Assert - long count = SUT.Count(e => e.Id == documents[0].Id || e.Id == documents[1].Id, PartitionKey); - Assert.AreEqual(2, count); - } - } -} diff --git a/IntegrationTests/CreateTKeyTests.cs b/IntegrationTests/CreateTKeyTests.cs deleted file mode 100644 index f709ba7..0000000 --- a/IntegrationTests/CreateTKeyTests.cs +++ /dev/null @@ -1,75 +0,0 @@ -using IntegrationTests.Infrastructure; -using MongoDB.Bson.Serialization.Attributes; -using MongoDbGenericRepository.Models; -using NUnit.Framework; -using System; -using System.Collections.Generic; -using System.Threading.Tasks; - -namespace IntegrationTests -{ - public class CreateTestsTKeyDocument : IDocument - { - [BsonId] - public Guid Id { get; set; } - public int Version { get; set; } - public CreateTestsTKeyDocument() - { - Id = Guid.NewGuid(); - Version = 2; - } - public string SomeContent { get; set; } - } - - [TestFixture] - public class CreateTKeyTests : BaseMongoDbRepositoryTests - { - [Test] - public void TKeyAddOne() - { - // Arrange - var document = new CreateTestsTKeyDocument(); - // Act - SUT.AddOne(document); - // Assert - long count = SUT.Count(e => e.Id == document.Id); - Assert.AreEqual(1, count); - } - - [Test] - public async Task TKeyAddOneAsync() - { - // Arrange - var document = new CreateTestsTKeyDocument(); - // Act - await SUT.AddOneAsync(document); - // Assert - long count = SUT.Count(e => e.Id == document.Id); - Assert.AreEqual(1, count); - } - - [Test] - public void TKeyAddMany() - { - // Arrange - var documents = new List { new CreateTestsTKeyDocument(), new CreateTestsTKeyDocument() }; - // Act - SUT.AddMany(documents); - // Assert - long count = SUT.Count(e => e.Id == documents[0].Id || e.Id == documents[1].Id); - Assert.AreEqual(2, count); - } - - [Test] - public async Task TKeyAddManyAsync() - { - // Arrange - var documents = new List { new CreateTestsTKeyDocument(), new CreateTestsTKeyDocument() }; - // Act - await SUT.AddManyAsync(documents); - // Assert - long count = SUT.Count(e => e.Id == documents[0].Id || e.Id == documents[1].Id); - Assert.AreEqual(2, count); - } - } -} diff --git a/IntegrationTests/DeletePartitionedTKeyTests.cs b/IntegrationTests/DeletePartitionedTKeyTests.cs deleted file mode 100644 index b213cb4..0000000 --- a/IntegrationTests/DeletePartitionedTKeyTests.cs +++ /dev/null @@ -1,137 +0,0 @@ -using IntegrationTests.Infrastructure; -using MongoDB.Bson.Serialization.Attributes; -using MongoDbGenericRepository.Models; -using NUnit.Framework; -using System; -using System.Threading.Tasks; - -namespace IntegrationTests -{ - public class DeleteTestsPartitionedTKeyDocument : IPartitionedDocument, IDocument - { - [BsonId] - public Guid Id { get; set; } - public int Version { get; set; } - public DeleteTestsPartitionedTKeyDocument() - { - Id = Guid.NewGuid(); - Version = 2; - PartitionKey = "TestPartitionKey"; - } - public string PartitionKey { get; set; } - public string SomeContent { get; set; } - } - - public class DeletePartitionedTKeyTests : BaseMongoDbRepositoryTests - { - [Test] - public void PartitionedDeleteOne() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = SUT.DeleteOne(document); - // Assert - Assert.AreEqual(1, result); - Assert.IsFalse(SUT.Any(e => e.Id == document.Id, PartitionKey)); - } - - [Test] - public void PartitionedDeleteOneLinq() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = SUT.DeleteOne(e => e.Id == document.Id, PartitionKey); - // Assert - Assert.AreEqual(1, result); - Assert.IsFalse(SUT.Any(e => e.Id == document.Id, PartitionKey)); - } - - [Test] - public async Task PartitionedDeleteOneAsync() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = await SUT.DeleteOneAsync(document); - // Assert - Assert.AreEqual(1, result); - Assert.IsFalse(SUT.Any(e => e.Id == document.Id, PartitionKey)); - } - - [Test] - public async Task PartitionedDeleteOneAsyncLinq() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = await SUT.DeleteOneAsync(e => e.Id == document.Id, PartitionKey); - // Assert - Assert.AreEqual(1, result); - Assert.IsFalse(SUT.Any(e => e.Id == document.Id, PartitionKey)); - } - - [Test] - public async Task PartitionedDeleteManyAsyncLinq() - { - // Arrange - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent"); - SUT.AddMany(documents); - // Act - var result = await SUT.DeleteManyAsync(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey); - // Assert - Assert.AreEqual(5, result); - Assert.IsFalse(SUT.Any(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey)); - } - - [Test] - public async Task PartitionedDeleteManyAsync() - { - // Arrange - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent"); - SUT.AddMany(documents); - // Act - var result = await SUT.DeleteManyAsync(documents); - // Assert - Assert.AreEqual(5, result); - Assert.IsFalse(SUT.Any(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey)); - } - - [Test] - public void PartitionedDeleteManyLinq() - { - // Arrange - var content = "DeleteManyLinqContent"; - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = content); - SUT.AddMany(documents); - // Act - var result = SUT.DeleteMany(e => e.SomeContent == content, PartitionKey); - // Assert - Assert.AreEqual(5, result); - Assert.IsFalse(SUT.Any(e => e.SomeContent == content, PartitionKey)); - } - - [Test] - public void PartitionedDeleteMany() - { - // Arrange - var content = "DeleteManyContent"; - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = content); - SUT.AddMany(documents); - // Act - var result = SUT.DeleteMany(documents); - // Assert - Assert.AreEqual(5, result); - Assert.IsFalse(SUT.Any(e => e.SomeContent == content, PartitionKey)); - } - } -} diff --git a/IntegrationTests/DeleteTKeyTests.cs b/IntegrationTests/DeleteTKeyTests.cs deleted file mode 100644 index dd90c09..0000000 --- a/IntegrationTests/DeleteTKeyTests.cs +++ /dev/null @@ -1,135 +0,0 @@ -using IntegrationTests.Infrastructure; -using MongoDB.Bson.Serialization.Attributes; -using MongoDbGenericRepository.Models; -using NUnit.Framework; -using System; -using System.Threading.Tasks; - -namespace IntegrationTests -{ - public class DeleteTestsTKeyDocument : IDocument - { - [BsonId] - public Guid Id { get; set; } - public int Version { get; set; } - public DeleteTestsTKeyDocument() - { - Id = Guid.NewGuid(); - Version = 2; - } - public string SomeContent { get; set; } - } - - public class DeleteTKeyTests : BaseMongoDbRepositoryTests - { - [Test] - public void DeleteOne() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = SUT.DeleteOne(document); - // Assert - Assert.AreEqual(1, result); - Assert.IsFalse(SUT.Any(e => e.Id == document.Id)); - } - - [Test] - public void DeleteOneLinq() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = SUT.DeleteOne(e => e.Id == document.Id); - // Assert - Assert.AreEqual(1, result); - Assert.IsFalse(SUT.Any(e => e.Id == document.Id)); - } - - [Test] - public async Task DeleteOneAsync() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = await SUT.DeleteOneAsync(document); - // Assert - Assert.AreEqual(1, result); - Assert.IsFalse(SUT.Any(e => e.Id == document.Id)); - } - - [Test] - public async Task DeleteOneAsyncLinq() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = await SUT.DeleteOneAsync(e => e.Id == document.Id); - // Assert - Assert.AreEqual(1, result); - Assert.IsFalse(SUT.Any(e => e.Id == document.Id)); - } - - [Test] - public async Task DeleteManyAsyncLinq() - { - // Arrange - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent"); - SUT.AddMany(documents); - // Act - var result = await SUT.DeleteManyAsync(e => e.SomeContent == "DeleteManyAsyncLinqContent"); - // Assert - Assert.AreEqual(5, result); - Assert.IsFalse(SUT.Any(e => e.SomeContent == "DeleteManyAsyncLinqContent")); - } - - [Test] - public async Task DeleteManyAsync() - { - // Arrange - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent"); - SUT.AddMany(documents); - // Act - var result = await SUT.DeleteManyAsync(documents); - // Assert - Assert.AreEqual(5, result); - Assert.IsFalse(SUT.Any(e => e.SomeContent == "DeleteManyAsyncLinqContent")); - } - - [Test] - public void DeleteManyLinq() - { - // Arrange - var content = "DeleteManyLinqContent"; - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = content); - SUT.AddMany(documents); - // Act - var result = SUT.DeleteMany(e => e.SomeContent == content); - // Assert - Assert.AreEqual(5, result); - Assert.IsFalse(SUT.Any(e => e.SomeContent == content)); - } - - [Test] - public void DeleteMany() - { - // Arrange - var content = "DeleteManyContent"; - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = content); - SUT.AddMany(documents); - // Act - var result = SUT.DeleteMany(documents); - // Assert - Assert.AreEqual(5, result); - Assert.IsFalse(SUT.Any(e => e.SomeContent == content)); - } - } -} diff --git a/IntegrationTests/Infrastructure/GlobalVariables.cs b/IntegrationTests/Infrastructure/GlobalVariables.cs new file mode 100644 index 0000000..f3ac4b3 --- /dev/null +++ b/IntegrationTests/Infrastructure/GlobalVariables.cs @@ -0,0 +1,15 @@ +using System; + +namespace IntegrationTests.Infrastructure +{ + /// + /// A class holding global variables. + /// + public static class GlobalVariables + { + /// + /// A random number generator. + /// + public static Random Random = new Random(); + } +} diff --git a/IntegrationTests/Infrastructure/MongoDBTestBase.cs b/IntegrationTests/Infrastructure/MongoDBTestBase.cs new file mode 100644 index 0000000..1bd0eaf --- /dev/null +++ b/IntegrationTests/Infrastructure/MongoDBTestBase.cs @@ -0,0 +1,335 @@ +using MongoDB.Bson.Serialization.Attributes; +using MongoDbGenericRepository.Models; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; + +namespace IntegrationTests.Infrastructure +{ + public class TestDoc : IDocument + where TKey : IEquatable + { + [BsonId] + public TKey Id { get; set; } + public int Version { get; set; } + + public TestDoc() + { + InitializeFields(); + Version = 2; + Nested = new Nested + { + SomeDate = DateTime.UtcNow + }; + } + + public string SomeContent { get; set; } + + public Nested Nested { get; set; } + + private void InitializeFields() + { + var idTypeName = typeof(TKey).Name; + switch (idTypeName) + { + case "Guid": + Id = (TKey)(object)Guid.NewGuid(); + break; + case "Int16": + Id = (TKey)(object)GlobalVariables.Random.Next(1, short.MaxValue); + break; + case "Int32": + Id = (TKey)(object)GlobalVariables.Random.Next(1, int.MaxValue); + break; + case "Int64": + Id = (TKey)(object)(GlobalVariables.Random.NextLong(1, long.MaxValue)); + break; + case "String": + Id = (TKey)(object)Guid.NewGuid().ToString(); + break; + } + + } + } + + + [TestFixture] + public abstract class MongoDBTestBase + where T: TestDoc, new() + where TKey : IEquatable + { + public T CreateTestDocument() + { + return new T(); + } + + public abstract string GetClassName(); + + public List CreateTestDocuments(int numberOfDocumentsToCreate) + { + var docs = new List(); + for (var i = 0; i < numberOfDocumentsToCreate; i++) + { + docs.Add(new T()); + } + return docs; + } + + /// + /// 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; } + + public MongoDBTestBase() + { + var type = CreateTestDocument(); + DocumentTypeName = type.GetType().FullName; + if (type is IPartitionedDocument) + { + PartitionKey = ((IPartitionedDocument)type).PartitionKey; + } + TestClassName = GetClassName(); + } + + [OneTimeSetUp] + public void Init() + { + var connectionString = ConfigurationManager.ConnectionStrings["MongoDbTests"].ConnectionString; + SUT = new TestRepository(connectionString, "MongoDbTests"); + } + + [OneTimeTearDown] + 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 Add + + [Test] + public void AddOne() + { + // Arrange + var document = new T(); + // 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.AreEqual(1, count, GetTestName()); + } + + [Test] + public async Task AddOneAsync() + { + // Arrange + var document = new T(); + // 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.AreEqual(1, count, GetTestName()); + } + + [Test] + public void AddMany() + { + // Arrange + var documents = new List { new T(), new T() }; + // 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.AreEqual(2, count, GetTestName()); + } + + [Test] + public async Task AddManyAsync() + { + // Arrange + var documents = new List { new T(), new T() }; + // 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.AreEqual(2, count, GetTestName()); + } + + + #endregion Add + + #region Delete + + [Test] + public void DeleteOne() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.DeleteOne(document); + // Assert + Assert.AreEqual(1, result); + Assert.IsFalse(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); + } + + [Test] + public void DeleteOneLinq() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.DeleteOne(e => e.Id.Equals(document.Id), PartitionKey); + // Assert + Assert.AreEqual(1, result); + Assert.IsFalse(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); + } + + [Test] + public async Task DeleteOneAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.DeleteOneAsync(document); + // Assert + Assert.AreEqual(1, result); + Assert.IsFalse(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); + } + + [Test] + 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.AreEqual(1, result); + Assert.IsFalse(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); + } + + [Test] + 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.AreEqual(5, result); + Assert.IsFalse(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName()); + } + + [Test] + public async Task DeleteManyAsync() + { + // Arrange + var criteria = $"{GetTestName()}.{DocumentTypeName}"; + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = criteria); + SUT.AddMany(documents); + // Act + var result = await SUT.DeleteManyAsync(documents); + // Assert + Assert.AreEqual(5, result); + Assert.IsFalse(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName()); + } + + [Test] + 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.AreEqual(5, result); + Assert.IsFalse(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName()); + } + + [Test] + public void DeleteMany() + { + // Arrange + var criteria = $"{GetTestName()}.{DocumentTypeName}"; + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = criteria); + SUT.AddMany(documents); + // Act + var result = SUT.DeleteMany(documents); + // Assert + Assert.AreEqual(5, result); + Assert.IsFalse(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName()); + } + + #endregion Delete + + #region Project + + + + #endregion Project + + + #region Test Utils + [MethodImpl(MethodImplOptions.NoInlining)] + private string GetCurrentMethod() + { + StackTrace st = new StackTrace(); + StackFrame sf = st.GetFrame(1); + + return sf.GetMethod().Name; + } + + private string GetTestName() + { + return $"{TestClassName}.{GetCurrentMethod()}"; + } + + #endregion Test Utils + } +} diff --git a/IntegrationTests/Infrastructure/RandomExtensions.cs b/IntegrationTests/Infrastructure/RandomExtensions.cs new file mode 100644 index 0000000..c051f06 --- /dev/null +++ b/IntegrationTests/Infrastructure/RandomExtensions.cs @@ -0,0 +1,61 @@ +using System; + +namespace IntegrationTests.Infrastructure +{ + // Thanks BlueRaja - Danny Pflughoeft https://stackoverflow.com/a/13095144/5103354 + /// + /// Extensions for the random number generator + /// + public static class RandomExtensions + { + + /// + /// Returns a random long from min (inclusive) to max (exclusive) + /// + /// The given random instance + /// The inclusive minimum bound + /// The exclusive maximum bound. Must be greater than min + public static long NextLong(this Random random, long min, long max) + { + if (max <= min) + throw new ArgumentOutOfRangeException("max", "max must be > min!"); + + //Working with ulong so that modulo works correctly with values > long.MaxValue + ulong uRange = (ulong)(max - min); + + //Prevent a modulo bias; see https://stackoverflow.com/a/10984975/238419 + //for more information. + //In the worst case, the expected number of calls is 2 (though usually it's + //much closer to 1) so this loop doesn't really hurt performance at all. + ulong ulongRand; + do + { + byte[] buf = new byte[8]; + random.NextBytes(buf); + ulongRand = (ulong)BitConverter.ToInt64(buf, 0); + } while (ulongRand > ulong.MaxValue - ((ulong.MaxValue % uRange) + 1) % uRange); + + return (long)(ulongRand % uRange) + min; + } + + /// + /// Returns a random long from 0 (inclusive) to max (exclusive) + /// + /// The given random instance + /// The exclusive maximum bound. Must be greater than 0 + public static long NextLong(this Random random, long max) + { + return random.NextLong(0, max); + } + + /// + /// Returns a random long over all possible values of long (except long.MaxValue, similar to + /// random.Next()) + /// + /// The given random instance + public static long NextLong(this Random random) + { + return random.NextLong(long.MinValue, long.MaxValue); + } + } +} diff --git a/IntegrationTests/IntegrationTests.csproj b/IntegrationTests/IntegrationTests.csproj index 8c41bb5..c5b061e 100644 --- a/IntegrationTests/IntegrationTests.csproj +++ b/IntegrationTests/IntegrationTests.csproj @@ -30,48 +30,37 @@ 4 - - ..\packages\MongoDbGenericRepository.1.3.4\lib\net45\DnsClient.dll - - ..\packages\MongoDbGenericRepository.1.3.4\lib\net45\MongoDB.Bson.dll + ..\packages\MongoDB.Bson.2.5.0\lib\net45\MongoDB.Bson.dll - ..\packages\MongoDbGenericRepository.1.3.4\lib\net45\MongoDB.Driver.dll + ..\packages\MongoDB.Driver.2.5.0\lib\net45\MongoDB.Driver.dll - ..\packages\MongoDbGenericRepository.1.3.4\lib\net45\MongoDB.Driver.Core.dll - - - ..\packages\MongoDbGenericRepository.1.3.4\lib\net45\MongoDbGenericRepository.dll + ..\packages\MongoDB.Driver.Core.2.5.0\lib\net45\MongoDB.Driver.Core.dll ..\packages\NUnit.3.9.0\lib\net45\nunit.framework.dll - - ..\packages\MongoDbGenericRepository.1.3.4\lib\net45\System.Buffers.dll - - - ..\packages\MongoDbGenericRepository.1.3.4\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - - - - + + + + + + + @@ -97,5 +86,11 @@ + + + {efc776c4-2af3-440c-be80-3fbe335817a5} + MongoDbGenericRepository + + \ No newline at end of file diff --git a/IntegrationTests/TKeyPartitionedCollectionNameAttributeTests.cs b/IntegrationTests/TKeyPartitionedCollectionNameAttributeTests.cs new file mode 100644 index 0000000..3c5ab25 --- /dev/null +++ b/IntegrationTests/TKeyPartitionedCollectionNameAttributeTests.cs @@ -0,0 +1,27 @@ +using IntegrationTests.Infrastructure; +using MongoDB.Bson.Serialization.Attributes; +using MongoDbGenericRepository.Attributes; +using MongoDbGenericRepository.Models; +using System; + +namespace IntegrationTests +{ + [CollectionName("TestingCollectionNameAttributePartitionedTKey")] + public class TKeyPartitionedCollectionNameDoc : TestDoc, IPartitionedDocument + { + public TKeyPartitionedCollectionNameDoc() + { + PartitionKey = "TestPartitionKey"; + } + + public string PartitionKey { get; set; } + } + + public class TKeyPartitionedCollectionNameAttributeTests : MongoDBTestBase + { + public override string GetClassName() + { + return "TKeyPartitionedCollectionNameAttributeTests"; + } + } +} diff --git a/IntegrationTests/packages.config b/IntegrationTests/packages.config index 45c1c48..b8fecf4 100644 --- a/IntegrationTests/packages.config +++ b/IntegrationTests/packages.config @@ -4,7 +4,6 @@ - diff --git a/MongoDbGenericRepository/Attributes/CollectionNameAttribute.cs b/MongoDbGenericRepository/Attributes/CollectionNameAttribute.cs index 233b548..128f87a 100644 --- a/MongoDbGenericRepository/Attributes/CollectionNameAttribute.cs +++ b/MongoDbGenericRepository/Attributes/CollectionNameAttribute.cs @@ -2,6 +2,13 @@ namespace MongoDbGenericRepository.Attributes { + /// + /// This attribute allows you to specify of the name of the collection. + /// Added at commit c117bf2a7fee378f1e02199dea9b2023a7089ee2 by https://github.com/Etchelon + /// who has included the CollectionName attribute into the repo to give another choice to the user on how + /// to name their collections. + /// The attribute takes precedence of course, and if not present the library will fall back to your Pluralize method. + /// [AttributeUsage(AttributeTargets.Class)] public class CollectionNameAttribute : Attribute {