Permalink
Browse files

Implemented MongoRepository with passing RepositoryTests.

  • Loading branch information...
1 parent f47cfb4 commit ccb8cac7f4ca14c108b75cd3bf8336c1a5c0db58 @ChrisFulstow committed Apr 26, 2011
View
Binary file not shown.
View
Oops, something went wrong.
View
Oops, something went wrong.
Binary file not shown.
@@ -61,6 +61,12 @@
<Content Include="Ionic.Zip.dll" />
<Content Include="MarkdownSharp.dll" />
<Content Include="MarkdownSharp.pdb" />
+ <Content Include="MongoDB.Bson.dll" />
+ <Content Include="MongoDB.Bson.pdb" />
+ <Content Include="MongoDB.Bson.xml" />
+ <Content Include="MongoDB.Driver.dll" />
+ <Content Include="MongoDB.Driver.pdb" />
+ <Content Include="MongoDB.Driver.XML" />
<Content Include="Newtonsoft.Json.dll" />
<Content Include="Newtonsoft.Json.pdb" />
<Content Include="Newtonsoft.Json.xml" />
@@ -34,6 +34,7 @@
<ItemGroup>
<Reference Include="Gallio" />
<Reference Include="MbUnit" />
+ <Reference Include="MongoDB.Driver, Version=1.0.0.4098, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL" />
<Reference Include="NSubstitute">
<HintPath>..\NBlog.Lib\NSubstitute.dll</HintPath>
</Reference>
@@ -55,8 +56,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="EntryControllerTests.cs" />
- <Compile Include="JsonRepositoryTests.cs" />
- <Compile Include="Repository\RepositoryTests.cs" />
+ <Compile Include="RepositoryTests.cs" />
<Compile Include="SlugTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
@@ -4,10 +4,12 @@
using System.IO;
using System.Linq;
using MbUnit.Framework;
+using MongoDB.Driver;
using NBlog.Web.Application;
using NBlog.Web.Application.Service.Entity;
using NBlog.Web.Application.Storage;
using NBlog.Web.Application.Storage.Json;
+using NBlog.Web.Application.Storage.Mongo;
using NBlog.Web.Application.Storage.Sql;
namespace NBlog.Tests
@@ -18,6 +20,8 @@ public class RepositoryTests
private static readonly string JsonWorkingFolder;
private static readonly string SqlConnectionString;
private static readonly string SqlDatabaseName;
+ private static readonly string MongoConnectionString;
+ private static readonly string MongoDatabaseName;
static RepositoryTests()
{
@@ -27,8 +31,12 @@ static RepositoryTests()
Keys.Add<User>(u => u.Username);
JsonWorkingFolder = Path.Combine(Path.GetTempPath(), "NBlogIntegrationTests");
+
SqlConnectionString = "Server=.;Trusted_Connection=True;";
SqlDatabaseName = "NBlogIntegrationTests";
+
+ MongoDatabaseName = "nblog-integration-tests";
+ MongoConnectionString = "mongodb://localhost";
}
@@ -54,6 +62,11 @@ public void TestCleanup()
cmd.ExecuteNonQuery();
}
}
+ else if (repositoryType == typeof(MongoRepository))
+ {
+ var server = MongoServer.Create(MongoConnectionString);
+ server.DropDatabase(MongoDatabaseName);
+ }
}
@@ -63,7 +76,7 @@ public void FixtureTearDown()
if (Instance.GetType() == typeof(SqlRepository))
{
const string dropSql = @"
- ALTER DATABASE [{0}] SET OFFLINE WITH ROLLBACK IMMEDIATE
+ ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DROP DATABASE [{0}]";
using (var cnn = new SqlConnection(SqlConnectionString))
@@ -80,6 +93,7 @@ public static IEnumerable<IRepository> GetInstances()
{
yield return BuildJsonRepository();
yield return BuildSqlRepository();
+ yield return BuildMongoRepository();
}
@@ -99,6 +113,12 @@ private static SqlRepository BuildSqlRepository()
}
+ private static MongoRepository BuildMongoRepository()
+ {
+ return new MongoRepository(Keys, MongoConnectionString, MongoDatabaseName);
+ }
+
+
[Test]
public void Single_Should_Return_Correct_Entity_By_Key()
{
@@ -119,6 +139,18 @@ public void Single_Should_Return_Correct_Entity_By_Key()
}
+ [Test, ExpectedException(typeof(Exception))]
+ //[Test]
+ public void Single_Should_Throw_When_Entity_Does_Not_Exist()
+ {
+ // arrange
+ var repository = Instance;
+
+ // act
+ var entry = repository.Single<Entry>("this-entry-does-not-exist");
+ }
+
+
[Test]
public void List_Should_Return_All_Entities()
{
@@ -1,10 +1,12 @@
using System;
+using MongoDB.Bson.Serialization.Attributes;
using PetaPoco;
namespace NBlog.Web.Application.Service.Entity
{
public class Entry
{
+ [BsonId]
public string Slug { get; set; }
public string Title { get; set; }
public string Author { get; set; }
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
+using MongoDB.Bson;
+using MongoDB.Driver;
namespace NBlog.Web.Application.Storage.Mongo
{
@@ -9,41 +11,68 @@ public class MongoRepository : IRepository
private readonly string _connectionString;
private readonly string _databaseName;
+ private readonly MongoServer _server;
+ private readonly MongoDatabase _db;
public MongoRepository(RepositoryKeys keys, string connectionString, string databaseName)
{
_keys = keys;
_connectionString = connectionString;
_databaseName = databaseName;
+
+ _server = MongoServer.Create(_connectionString);
+ _db = _server.GetDatabase(databaseName);
}
public TEntity Single<TEntity>(object key) where TEntity : class, new()
{
- throw new NotImplementedException();
+ var collection = GetCollection<TEntity>();
+ var query = new QueryDocument("_id", BsonValue.Create(key));
+ var entity = collection.FindOneAs<TEntity>(query);
+
+ if (entity == null)
+ throw new NullReferenceException("Document with key '" + key + "' not found.");
+
+ return entity;
}
public IEnumerable<TEntity> All<TEntity>() where TEntity : class, new()
{
- throw new NotImplementedException();
+ var collection = GetCollection<TEntity>();
+ var entity = collection.FindAllAs<TEntity>();
+ return entity;
}
public bool Exists<TEntity>(object key) where TEntity : class, new()
{
- throw new NotImplementedException();
+ var collection = GetCollection<TEntity>();
+ var query = new QueryDocument("_id", BsonValue.Create(key));
+ var entity = collection.FindOneAs<TEntity>(query);
+ return (entity != null);
}
public void Save<TEntity>(TEntity item) where TEntity : class, new()
{
- throw new NotImplementedException();
+ var collection = GetCollection<TEntity>();
+ collection.Save(item);
}
+
public void Delete<TEntity>(object key) where TEntity : class, new()
{
- throw new NotImplementedException();
+ var collection = GetCollection<TEntity>();
+ var query = new QueryDocument("_id", BsonValue.Create(key));
+ collection.Remove(query);
+ }
+
+
+ private MongoCollection GetCollection<TEntity>()
+ {
+ return _db.GetCollection(typeof(TEntity).Name);
}
}
}
@@ -113,6 +113,12 @@
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
</Reference>
+ <Reference Include="MongoDB.Bson">
+ <HintPath>..\NBlog.Lib\MongoDB.Bson.dll</HintPath>
+ </Reference>
+ <Reference Include="MongoDB.Driver">
+ <HintPath>..\NBlog.Lib\MongoDB.Driver.dll</HintPath>
+ </Reference>
<Reference Include="Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\NBlog.Lib\Newtonsoft.Json.dll</HintPath>
@@ -69,7 +69,7 @@
</div>
<footer class="text-links">
- Powered by <a href="http://nblog.codeplex.com/">NBlog</a>
+ Powered by <a href="http://chrisfulstow.com/nblog">NBlog</a>
</footer>
</div>
@@ -176,7 +176,7 @@
<div class="center_wrapper">
<div class="left">
- &copy; 2011 Website.com - Powered by <a href="http://nblog.codeplex.com/" target="_blank">NBlog</a>
+ &copy; 2011 Website.com - Powered by <a href="http://chrisfulstow.com/nblog" target="_blank">NBlog</a>
</div>
<div class="right">
<a href="http://templates.arcsin.se/">Website template</a> by <a href="http://arcsin.se/">Arcsin</a>

0 comments on commit ccb8cac

Please sign in to comment.