-
Notifications
You must be signed in to change notification settings - Fork 15
/
MongoPersistenceFixture.cs
106 lines (92 loc) · 3.54 KB
/
MongoPersistenceFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Reflection;
using System.Threading;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver;
using NStore.Core.Persistence;
using NStore.Persistence.Mongo;
using Xunit;
#if MAP_DOMAIN
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Options;
using MongoDB.Bson.Serialization.Serializers;
#endif
[assembly: CollectionBehavior(DisableTestParallelization = true)]
namespace NStore.Persistence.Tests
{
public partial class BasePersistenceTest
{
protected string _mongoConnectionString;
protected IMongoPersistence _mongoPersistence;
private MongoPersistenceOptions _options;
private const string TestSuitePrefix = "Mongo";
static BasePersistenceTest()
{
// https://github.com/mongodb/mongo-csharp-driver/releases/tag/v2.19.0
var objectSerializer = new ObjectSerializer(type => ObjectSerializer.AllAllowedTypes(type));
BsonSerializer.RegisterSerializer(objectSerializer);
}
#if MAP_DOMAIN
static BasePersistenceTest()
{
// enable support for dots in key names
BsonClassMap.RegisterClassMap<Changeset>(map =>
{
map.AutoMap();
map.MapProperty(x => x.Headers).SetSerializer(
new DictionaryInterfaceImplementerSerializer<
Dictionary<String, Object>
>(DictionaryRepresentation.ArrayOfArrays)
);
});
}
#endif
protected internal IPersistence Create(bool dropOnInit)
{
_mongoConnectionString = GetPartitionsConnectionString();
_options = GetMongoPersistenceOptions();
if (dropOnInit)
{
_options.DropOnInit = true;
}
_mongoPersistence = CreatePersistence(_options);
_mongoPersistence.InitAsync(CancellationToken.None).Wait();
return _mongoPersistence;
}
protected virtual internal MongoPersistenceOptions GetMongoPersistenceOptions()
{
return new MongoPersistenceOptions
{
PartitionsConnectionString = _mongoConnectionString,
UseLocalSequence = true,
PartitionsCollectionName = "partitions_" + GetType().Name + "_" + _testRunId,
SequenceCollectionName = "seq_" + _testRunId
};
}
private static string GetPartitionsConnectionString()
{
var mongo = Environment.GetEnvironmentVariable("NSTORE_MONGODB");
if (string.IsNullOrWhiteSpace(mongo))
{
throw new TestMisconfiguredException("NSTORE_MONGODB environment variable not set");
}
return mongo;
}
protected IMongoCollection<TChunk> GetCollection<TChunk>()
{
var fieldInfo = _mongoPersistence.GetType()
.GetField("_chunks", BindingFlags.NonPublic | BindingFlags.Instance);
var collection = (IMongoCollection<TChunk>)fieldInfo.GetValue(_mongoPersistence);
return collection;
}
protected virtual IMongoPersistence CreatePersistence(MongoPersistenceOptions options)
{
return new MongoPersistence(options);
}
protected void Clear(IPersistence persistence, bool drop)
{
// nothing to do
}
}
}