refactoring test classes.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
|
||||
namespace IntegrationTests.Infrastructure
|
||||
{
|
||||
/// <summary>
|
||||
/// A class holding global variables.
|
||||
/// </summary>
|
||||
public static class GlobalVariables
|
||||
{
|
||||
/// <summary>
|
||||
/// A random number generator.
|
||||
/// </summary>
|
||||
public static Random Random = new Random();
|
||||
}
|
||||
}
|
||||
@@ -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<TKey> : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
[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<T, TKey>
|
||||
where T: TestDoc<TKey>, new()
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
public T CreateTestDocument()
|
||||
{
|
||||
return new T();
|
||||
}
|
||||
|
||||
public abstract string GetClassName();
|
||||
|
||||
public List<T> CreateTestDocuments(int numberOfDocumentsToCreate)
|
||||
{
|
||||
var docs = new List<T>();
|
||||
for (var i = 0; i < numberOfDocumentsToCreate; i++)
|
||||
{
|
||||
docs.Add(new T());
|
||||
}
|
||||
return docs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The partition key for the collection, if any
|
||||
/// </summary>
|
||||
protected string PartitionKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// the name of the test class
|
||||
/// </summary>
|
||||
protected string TestClassName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The name of the document used for tests
|
||||
/// </summary>
|
||||
protected string DocumentTypeName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// SUT: System Under Test
|
||||
/// </summary>
|
||||
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<T>(PartitionKey);
|
||||
}
|
||||
else
|
||||
{
|
||||
SUT.DropTestCollection<T>();
|
||||
}
|
||||
}
|
||||
|
||||
#region Add
|
||||
|
||||
[Test]
|
||||
public void AddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new T();
|
||||
// Act
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T, TKey>(e => e.Id.Equals(document.Id))
|
||||
: SUT.Count<T, TKey>(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<T, TKey>(document);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T, TKey>(e => e.Id.Equals(document.Id))
|
||||
: SUT.Count<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
Assert.AreEqual(1, count, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<T> { new T(), new T() };
|
||||
// Act
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T, TKey>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id))
|
||||
: SUT.Count<T, TKey>(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<T> { new T(), new T() };
|
||||
// Act
|
||||
await SUT.AddManyAsync<T, TKey>(documents);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T, TKey>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id))
|
||||
: SUT.Count<T, TKey>(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<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<T, TKey>(document);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteOneLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<T, TKey>(document);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteOneAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<T, TKey>(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<T, TKey>(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<T, TKey>(e => e.SomeContent == criteria, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<T, TKey>(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<T, TKey>(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<T, TKey>(documents);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<T, TKey>(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<T, TKey>(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<T, TKey>(e => e.SomeContent == criteria, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<T, TKey>(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<T, TKey>(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<T, TKey>(documents);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<T, TKey>(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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
|
||||
namespace IntegrationTests.Infrastructure
|
||||
{
|
||||
// Thanks BlueRaja - Danny Pflughoeft https://stackoverflow.com/a/13095144/5103354
|
||||
/// <summary>
|
||||
/// Extensions for the random number generator <see cref="Random"/>
|
||||
/// </summary>
|
||||
public static class RandomExtensions
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random long from min (inclusive) to max (exclusive)
|
||||
/// </summary>
|
||||
/// <param name="random">The given random instance</param>
|
||||
/// <param name="min">The inclusive minimum bound</param>
|
||||
/// <param name="max">The exclusive maximum bound. Must be greater than min</param>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random long from 0 (inclusive) to max (exclusive)
|
||||
/// </summary>
|
||||
/// <param name="random">The given random instance</param>
|
||||
/// <param name="max">The exclusive maximum bound. Must be greater than 0</param>
|
||||
public static long NextLong(this Random random, long max)
|
||||
{
|
||||
return random.NextLong(0, max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random long over all possible values of long (except long.MaxValue, similar to
|
||||
/// random.Next())
|
||||
/// </summary>
|
||||
/// <param name="random">The given random instance</param>
|
||||
public static long NextLong(this Random random)
|
||||
{
|
||||
return random.NextLong(long.MinValue, long.MaxValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user