added tests for partitioned collections
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class CreateTestsPartitionedDocument : PartitionedDocument
|
||||
{
|
||||
public CreateTestsPartitionedDocument() : base("TestPartitionKey")
|
||||
{
|
||||
Version = 1;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class CreatePartitionedTests : BaseMongoDbRepositoryTests<CreateTestsPartitionedDocument>
|
||||
{
|
||||
[Test]
|
||||
public void AddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new CreateTestsPartitionedDocument();
|
||||
// Act
|
||||
SUT.AddOne(document);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey);
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AddOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = new CreateTestsPartitionedDocument();
|
||||
// Act
|
||||
await SUT.AddOneAsync(document);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey);
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<CreateTestsPartitionedDocument> { new CreateTestsPartitionedDocument(), new CreateTestsPartitionedDocument() };
|
||||
// Act
|
||||
SUT.AddMany(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsPartitionedDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id, PartitionKey);
|
||||
Assert.AreEqual(2, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AddManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<CreateTestsPartitionedDocument> { new CreateTestsPartitionedDocument(), new CreateTestsPartitionedDocument() };
|
||||
// Act
|
||||
await SUT.AddManyAsync(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsPartitionedDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id, PartitionKey);
|
||||
Assert.AreEqual(2, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,41 +2,31 @@
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class CreateTests
|
||||
public class CreateTestsDocument : Document
|
||||
{
|
||||
/// <summary>
|
||||
/// SUT: System Under Test
|
||||
/// </summary>
|
||||
private static ITestRepository SUT { get; set; }
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void Init()
|
||||
public CreateTestsDocument()
|
||||
{
|
||||
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.
|
||||
SUT.DropTestCollection<InsertTestsDocument>();
|
||||
Version = 2;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class CreateTests : BaseMongoDbRepositoryTests<CreateTestsDocument>
|
||||
{
|
||||
[Test]
|
||||
public void AddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new InsertTestsDocument();
|
||||
var document = new CreateTestsDocument();
|
||||
// Act
|
||||
SUT.AddOne(document);
|
||||
// Assert
|
||||
long count = SUT.Count<InsertTestsDocument>(e => e.Id == document.Id);
|
||||
long count = SUT.Count<CreateTestsDocument>(e => e.Id == document.Id);
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
@@ -44,11 +34,11 @@ namespace IntegrationTests
|
||||
public async Task AddOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = new InsertTestsDocument();
|
||||
var document = new CreateTestsDocument();
|
||||
// Act
|
||||
await SUT.AddOneAsync(document);
|
||||
// Assert
|
||||
long count = SUT.Count<InsertTestsDocument>(e => e.Id == document.Id);
|
||||
long count = SUT.Count<CreateTestsDocument>(e => e.Id == document.Id);
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
@@ -56,11 +46,11 @@ namespace IntegrationTests
|
||||
public void AddMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<InsertTestsDocument> { new InsertTestsDocument(), new InsertTestsDocument() };
|
||||
var documents = new List<CreateTestsDocument> { new CreateTestsDocument(), new CreateTestsDocument() };
|
||||
// Act
|
||||
SUT.AddMany(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<InsertTestsDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
|
||||
long count = SUT.Count<CreateTestsDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
|
||||
Assert.AreEqual(2, count);
|
||||
}
|
||||
|
||||
@@ -68,26 +58,12 @@ namespace IntegrationTests
|
||||
public async Task AddManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<InsertTestsDocument> { new InsertTestsDocument(), new InsertTestsDocument() };
|
||||
var documents = new List<CreateTestsDocument> { new CreateTestsDocument(), new CreateTestsDocument() };
|
||||
// Act
|
||||
await SUT.AddManyAsync(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<InsertTestsDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
|
||||
long count = SUT.Count<CreateTestsDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
|
||||
Assert.AreEqual(2, count);
|
||||
}
|
||||
|
||||
|
||||
#region Utils
|
||||
|
||||
private class InsertTestsDocument : Document
|
||||
{
|
||||
public InsertTestsDocument()
|
||||
{
|
||||
Version = 2;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System.Configuration;
|
||||
|
||||
namespace IntegrationTests.Infrastructure
|
||||
{
|
||||
public class BaseMongoDbRepositoryTests<T> where T : Document, new()
|
||||
{
|
||||
public T GetDocumentInstance()
|
||||
{
|
||||
return new T();
|
||||
}
|
||||
|
||||
public BaseMongoDbRepositoryTests()
|
||||
{
|
||||
var type = GetDocumentInstance();
|
||||
if (type is IPartitionedDocument)
|
||||
{
|
||||
PartitionKey = ((IPartitionedDocument)type).PartitionKey;
|
||||
}
|
||||
}
|
||||
|
||||
public string PartitionKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// SUT: System Under Test
|
||||
/// </summary>
|
||||
protected static ITestRepository SUT { get; set; }
|
||||
|
||||
[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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,5 +5,6 @@ namespace IntegrationTests
|
||||
public interface ITestRepository : IBaseMongoRepository
|
||||
{
|
||||
void DropTestCollection<TDocument>();
|
||||
void DropTestCollection<TDocument>(string partitionKey);
|
||||
}
|
||||
}
|
||||
@@ -13,5 +13,10 @@ namespace IntegrationTests.Infrastructure
|
||||
{
|
||||
_mongoDbContext.DropCollection<TDocument>();
|
||||
}
|
||||
|
||||
public void DropTestCollection<TDocument>(string partitionKey)
|
||||
{
|
||||
_mongoDbContext.DropCollection<TDocument>(partitionKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,10 +56,13 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Infrastructure\BaseMongoDbRepositoryTests.cs" />
|
||||
<Compile Include="CreatePartitionedTests.cs" />
|
||||
<Compile Include="Infrastructure\ITestRepository.cs" />
|
||||
<Compile Include="Infrastructure\TestRepository.cs" />
|
||||
<Compile Include="CreateTests.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ReadTests.cs" />
|
||||
<Compile Include="UpdateTests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class ReadTestsDocument : Document
|
||||
{
|
||||
public ReadTestsDocument()
|
||||
{
|
||||
Version = 2;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class ReadTests
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
namespace IntegrationTests
|
||||
{
|
||||
class UpdateTests
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user