Add code for using MongoDB
This commit is contained in:
parent
dd370e27cb
commit
313d668bfe
8 changed files with 218 additions and 1 deletions
54
src/Robware.Data.MongoDB.Tests/BlogRepositoryTests.cs
Normal file
54
src/Robware.Data.MongoDB.Tests/BlogRepositoryTests.cs
Normal file
|
@ -0,0 +1,54 @@
|
|||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using NSubstitute;
|
||||
using Robware.Blog;
|
||||
using Robware.Blog.Data.MongoDB.State;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace Robware.Data.MongoDB.Tests {
|
||||
public class BlogRepositoryTests {
|
||||
[Fact]
|
||||
public async Task SavePost_WithNewPost_GetsNewIdAndSetsTimestampAndSavesAsync() {
|
||||
var collection = Substitute.For<IMongoCollection<BlogPostState>>();
|
||||
collection.CountDocumentsAsync(Arg.Any<FilterDefinition<BlogPostState>>()).Returns(10);
|
||||
var database = Substitute.For<IMongoDatabase>();
|
||||
database.GetCollection<BlogPostState>("blog").Returns(collection);
|
||||
|
||||
var repo = new BlogRepository(database);
|
||||
var post = new BlogPost();
|
||||
await repo.SavePost(post);
|
||||
post.Id.Should().Be(11);
|
||||
post.Timestamp.Should().BeCloseTo(DateTime.Now);
|
||||
await collection.Received(1).InsertOneAsync(Arg.Any<BlogPostState>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SavePost_WithExistingPost_DoesNotSetTimestampAndSavesAsync() {
|
||||
var collection = Substitute.For<IMongoCollection<BlogPostState>>();
|
||||
var database = Substitute.For<IMongoDatabase>();
|
||||
database.GetCollection<BlogPostState>("blog").Returns(collection);
|
||||
|
||||
var repo = new BlogRepository(database);
|
||||
var post = new BlogPost() {
|
||||
Id = 1
|
||||
};
|
||||
await repo.SavePost(post);
|
||||
post.Id.Should().Be(1);
|
||||
post.Timestamp.Should().Be(new DateTime());
|
||||
await collection.Received(1).InsertOneAsync(Arg.Any<BlogPostState>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetCountAsync_ReturnsCountFromDatabase() {
|
||||
var collection = Substitute.For<IMongoCollection<BlogPostState>>();
|
||||
collection.CountDocumentsAsync(Arg.Any<FilterDefinition<BlogPostState>>()).Returns(10);
|
||||
var database = Substitute.For<IMongoDatabase>();
|
||||
database.GetCollection<BlogPostState>("blog").Returns(collection);
|
||||
|
||||
var repo = new BlogRepository(database);
|
||||
(await repo.GetCountAsync()).Should().Be(10);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue