Skip to content

Commit 047b005

Browse files
committed
Getting closer to actually tackling this silliness. Will likely represent the Mongo Id as a string for the integration tests. If we want to share objects for all repo integration tests, we'll need to update the tests accordingly.
1 parent 15333d6 commit 047b005

3 files changed

Lines changed: 58 additions & 89 deletions

File tree

SharpRepository.MongoDbRepository/MongoDbRepositoryBase.cs

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,19 @@ protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = nul
4646

4747
protected override T GetQuery(TKey key)
4848
{
49-
return BaseQuery().FirstOrDefault();
49+
return _database.GetCollection<T>(TypeName).FindOne(Query.EQ("_id", new ObjectId(key.ToString())));
5050
}
5151

5252
protected override void AddItem(T entity)
5353
{
5454
_database.GetCollection<T>(TypeName).Insert(entity);
5555
}
56-
56+
5757
protected override void DeleteItem(T entity)
5858
{
59-
// Yikes.
60-
//IMongoQuery mq = new QueryDocument(entity.ToBsonDocument());
6159
TKey pkValue;
6260
GetPrimaryKey(entity, out pkValue);
6361
_database.GetCollection<T>(TypeName).Remove(Query.EQ("_id", new ObjectId(pkValue.ToString())));
64-
//_database.GetCollection<T>(TypeName).Remove(mq);
6562
}
6663

6764
protected override void UpdateItem(T entity)
@@ -71,41 +68,11 @@ protected override void UpdateItem(T entity)
7168

7269
protected override void SaveChanges()
7370
{
74-
//_session.Save();
7571
}
7672

7773
public override void Dispose()
7874
{
7975

8076
}
81-
82-
private TKey GeneratePrimaryKey()
83-
{
84-
if (typeof(TKey) == typeof(Guid))
85-
{
86-
return (TKey)Convert.ChangeType(Guid.NewGuid(), typeof(TKey));
87-
}
88-
89-
//if (typeof(TKey) == typeof(Int32))
90-
//{
91-
// var nextInt = Convert.ToInt32(_database.GetCollection<T>(TypeName).GenerateId());
92-
// return (TKey)Convert.ChangeType(nextInt, typeof(TKey));
93-
//}
94-
95-
//if (typeof(TKey) == typeof(Int64))
96-
//{
97-
// var nextLong = _database.GetCollection<T>().GenerateId();
98-
// return (TKey)Convert.ChangeType(nextLong, typeof(TKey));
99-
//}
100-
101-
throw new InvalidOperationException("Primary key could not be generated. This only works for GUID, Int32 and Int64.");
102-
}
103-
104-
105-
private bool MatchOnPrimaryKey(T item, TKey keyValue)
106-
{
107-
TKey value;
108-
return GetPrimaryKey(item, out value) && keyValue.Equals(value);
109-
}
11077
}
11178
}

SharpRepository.Tests.Integration/Data/RepositoryTestCaseDataFactory.cs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,47 @@ public class RepositoryTestCaseDataFactory
1919
{
2020
public static IEnumerable<TestCaseData> Build(RepositoryTypes[] includeTypes)
2121
{
22-
//if (includeTypes.Contains(RepositoryTypes.All) || includeTypes.Contains(RepositoryTypes.InMemory))
23-
//{
24-
// yield return new TestCaseData(new InMemoryRepository<Contact, int>()).SetName("InMemoryRepository Test");
25-
//}
26-
27-
//if (includeTypes.Contains(RepositoryTypes.All) || includeTypes.Contains(RepositoryTypes.Xml))
28-
//{
29-
// var xmlDataDirectoryPath = XmlDataDirectoryFactory.Build("Contact");
30-
// yield return
31-
// new TestCaseData(new XmlRepository<Contact, int>(xmlDataDirectoryPath)).SetName("XmlRepository Test");
32-
//}
22+
if (includeTypes.Contains(RepositoryTypes.All) || includeTypes.Contains(RepositoryTypes.InMemory))
23+
{
24+
yield return new TestCaseData(new InMemoryRepository<Contact, int>()).SetName("InMemoryRepository Test");
25+
}
3326

34-
//if (includeTypes.Contains(RepositoryTypes.All) || includeTypes.Contains(RepositoryTypes.Ef))
35-
//{
36-
// var dbPath = EfDataDirectoryFactory.Build();
37-
// Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
38-
// yield return
39-
// new TestCaseData(new EfRepository<Contact, int>(new TestObjectEntities("Data Source=" + dbPath))).SetName("EfRepository Test");
40-
//}
27+
if (includeTypes.Contains(RepositoryTypes.All) || includeTypes.Contains(RepositoryTypes.Xml))
28+
{
29+
var xmlDataDirectoryPath = XmlDataDirectoryFactory.Build("Contact");
30+
yield return
31+
new TestCaseData(new XmlRepository<Contact, int>(xmlDataDirectoryPath)).SetName("XmlRepository Test");
32+
}
4133

42-
//if (includeTypes.Contains(RepositoryTypes.All) || includeTypes.Contains(RepositoryTypes.Dbo4))
43-
//{
44-
// var dbPath = Db4oDataDirectoryFactory.Build("Contact");
45-
// yield return new TestCaseData(new Db4oRepository<Contact, int>(dbPath)).SetName("Db4oRepository Test");
46-
//}
34+
if (includeTypes.Contains(RepositoryTypes.All) || includeTypes.Contains(RepositoryTypes.Ef))
35+
{
36+
var dbPath = EfDataDirectoryFactory.Build();
37+
Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
38+
yield return
39+
new TestCaseData(new EfRepository<Contact, int>(new TestObjectEntities("Data Source=" + dbPath))).SetName("EfRepository Test");
40+
}
4741

48-
if (includeTypes.Contains(RepositoryTypes.All) || includeTypes.Contains(RepositoryTypes.MongoDb))
42+
if (includeTypes.Contains(RepositoryTypes.All) || includeTypes.Contains(RepositoryTypes.Dbo4))
4943
{
50-
string connectionString = MongoDbDataDirectoryFactory.Build("Contact");
51-
yield return new TestCaseData(new MongoDbRepository<Contact, int>(connectionString)).SetName("MongoDb Test");
44+
var dbPath = Db4oDataDirectoryFactory.Build("Contact");
45+
yield return new TestCaseData(new Db4oRepository<Contact, int>(dbPath)).SetName("Db4oRepository Test");
5246
}
5347

54-
//if (includeTypes.Contains(RepositoryTypes.All) || includeTypes.Contains(RepositoryTypes.RavenDb))
48+
//if (includeTypes.Contains(RepositoryTypes.All) || includeTypes.Contains(RepositoryTypes.MongoDb))
5549
//{
56-
// var documentStore = new EmbeddableDocumentStore
57-
// {
58-
// RunInMemory = true,
59-
// Conventions = { DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites }
60-
// };
61-
// yield return new TestCaseData(new RavenDbRepository<Contact, int>(documentStore)).SetName("RavenDbRepository Test");
50+
// string connectionString = MongoDbDataDirectoryFactory.Build("Contact");
51+
// yield return new TestCaseData(new MongoDbRepository<Contact, int>(connectionString)).SetName("MongoDb Test");
6252
//}
53+
54+
if (includeTypes.Contains(RepositoryTypes.All) || includeTypes.Contains(RepositoryTypes.RavenDb))
55+
{
56+
var documentStore = new EmbeddableDocumentStore
57+
{
58+
RunInMemory = true,
59+
Conventions = { DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites }
60+
};
61+
yield return new TestCaseData(new RavenDbRepository<Contact, int>(documentStore)).SetName("RavenDbRepository Test");
62+
}
6363
}
6464
}
6565
}

SharpRepository.Tests.Integration/MongoRepositoryTests.cs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
43
using MongoDB.Bson;
4+
using MongoDB.Bson.Serialization.Attributes;
5+
using MongoDB.Bson.Serialization.IdGenerators;
56
using MongoDB.Driver;
67
using MongoDB.Driver.Builders;
78
using MongoDB.Driver.Linq;
89
using NUnit.Framework;
9-
using SharpRepository.Repository;
10-
using SharpRepository.Repository.Specifications;
11-
using SharpRepository.Tests.Integration.TestAttributes;
12-
using SharpRepository.Tests.Integration.TestObjects;
13-
using Should;
1410
using SharpRepository.MongoDbRepository;
11+
using Should;
1512

1613
namespace SharpRepository.Tests.Integration
1714
{
15+
// Getting MongoDb installed on Windows
16+
// http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/
17+
// When defining the log location include a file name like log.txt when the command fails.
1818
public class Order
1919
{
20-
public ObjectId Id { get; set; }
20+
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
21+
[BsonRepresentation(BsonType.ObjectId)]
22+
public string OrderId { get; set; }
2123
public string Name { get; set; }
2224
}
2325

@@ -39,12 +41,12 @@ public void Mongo()
3941

4042
foreach (var order in database.GetCollection<Order>("Order").AsQueryable())
4143
{
42-
Console.WriteLine(order.Name + ", " + order.Id);
44+
Console.WriteLine(order.Name + ", " + order.OrderId);
4345
}
4446

4547
Console.WriteLine("* READ *");
4648

47-
var read = orders.AsQueryable().FirstOrDefault(e => e.Id == create.Id);
49+
var read = orders.AsQueryable().FirstOrDefault(e => e.OrderId == create.OrderId);
4850
read.Name.ShouldEqual(create.Name);
4951

5052
Console.WriteLine("* UPDATE *");
@@ -54,22 +56,22 @@ public void Mongo()
5456

5557
foreach (var order in database.GetCollection<Order>("Order").AsQueryable())
5658
{
57-
Console.WriteLine(order.Name + ", " + order.Id);
59+
Console.WriteLine(order.Name + ", " + order.OrderId);
5860
}
5961

60-
var update = database.GetCollection<Order>("Order").AsQueryable().FirstOrDefault(e => e.Id == read.Id);
61-
update.Id.ShouldEqual(read.Id);
62+
var update = database.GetCollection<Order>("Order").AsQueryable().FirstOrDefault(e => e.OrderId == read.OrderId);
63+
update.OrderId.ShouldEqual(read.OrderId);
6264
update.Name.ShouldEqual(read.Name);
6365

6466
Console.WriteLine("* DELETE *");
6567

66-
var delete = database.GetCollection<Order>("Order").AsQueryable().FirstOrDefault(e => e.Id == update.Id);
68+
var delete = database.GetCollection<Order>("Order").AsQueryable().FirstOrDefault(e => e.OrderId == update.OrderId);
6769

68-
database.GetCollection<Order>("Order").Remove(Query.EQ("_id", delete.Id));
70+
database.GetCollection<Order>("Order").Remove(Query.EQ("OrderId", delete.OrderId));
6971

7072
foreach (var order in database.GetCollection<Order>("Order").AsQueryable())
7173
{
72-
Console.WriteLine(order.Name + ", " + order.Id);
74+
Console.WriteLine(order.Name + ", " + order.OrderId);
7375
}
7476

7577
database.GetCollection<Order>("Order").RemoveAll();
@@ -78,7 +80,7 @@ public void Mongo()
7880

7981
foreach (var order in database.GetCollection<Order>("Order").AsQueryable())
8082
{
81-
Console.WriteLine(order.Name + ", " + order.Id);
83+
Console.WriteLine(order.Name + ", " + order.OrderId);
8284
}
8385

8486
server.DropDatabase("Order");
@@ -88,27 +90,27 @@ public void Mongo()
8890
public void MongoRepository_Supports_Basic_Crud_Operations()
8991
{
9092
const string connectionString = "mongodb://127.0.0.1";
91-
var repo = new MongoDbRepository<Order, ObjectId>(connectionString);
93+
var repo = new MongoDbRepository<Order, string>(connectionString);
9294

9395
// Create
9496
var create = new Order { Name = "Big sale" };
9597
repo.Add(create);
9698

9799
// Read
98-
var read = repo.Get(create.Id);
100+
var read = repo.Get(create.OrderId);
99101
read.Name.ShouldEqual(create.Name);
100102

101103
// Update
102104
read.Name = "Really big sale";
103105
repo.Update(read);
104106

105-
var update = repo.Get(read.Id);
106-
update.Id.ShouldEqual(read.Id);
107+
var update = repo.Get(read.OrderId);
108+
update.OrderId.ShouldEqual(read.OrderId);
107109
update.Name.ShouldEqual(read.Name);
108110

109111
// Delete
110112
repo.Delete(update);
111-
var delete = repo.Get(read.Id);
113+
var delete = repo.Get(read.OrderId);
112114
delete.ShouldBeNull();
113115
}
114116
}

0 commit comments

Comments
 (0)