-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.cs
164 lines (127 loc) · 5.02 KB
/
Database.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#region SearchAThing.UnitTests, Copyright(C) 2015-2016 Lorenzo Delana, License under MIT
/*
* The MIT License(MIT)
* Copyright(c) 2016 Lorenzo Delana, https://searchathing.com
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion
using SearchAThing.Core;
using SearchAThing.Graph;
using SearchAThing.MongoDB;
using SearchAThing.UnitTests.MongoConcurrencyTypes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Xunit;
#if UNIT_TEST_DATABASE
using MongoDB.Driver.Linq;
using MongoDB.Driver;
#endif
namespace SearchAThing.UnitTests
{
namespace MongoConcurrencyTypes
{
public class I
{
public I()
{
}
public string iprop1 { get; set; }
public string iprop2 { get; set; }
public List<I> Set { get; set; }
}
public class A : MongoEntity
{
public string prop1 { get; set; }
public string prop2 { get; set; }
public List<I> Set { get; set; }
}
}
public class Database
{
#if UNIT_TEST_DATABASE
[Trait("Category", "Database")]
[Fact(DisplayName = "MongoConcurrency")]
public void MongoConcurrency()
{
const string connectionString = @"mongodb://localhost/searchathing_unittest_mongoconcurrency";
{
// populate sample
{
var ctx = new MongoContext(connectionString);
var repoA = ctx.GetRepository<A>();
// set data
repoA.Collection.AsQueryable().Foreach(w => ctx.Delete(w));
ctx.Save();
var iz = new I() { iprop1 = "z1", iprop2 = "z2" };
var iy = new I() { iprop1 = "y1", iprop2 = "y2" }; //, Set = new List<I>() { iz } };
var a = ctx.New<A>();
a.prop1 = "x1";
a.prop2 = "x2";
a.Set = new List<I>() { iy };
ctx.Save();
// set coll prop
iy.Set = new List<I>() { iz };
ctx.Save();
}
// retrieve two istance of the same document
var ctx1 = new MongoContext(connectionString);
var repoA1 = ctx1.GetRepository<A>();
// Note the .Attach(ctx) extension to bring objects under control of the context
var doc1 = repoA1.Collection.AsQueryable().Attach(ctx1).First();
var ctx2 = new MongoContext(connectionString);
var repoA2 = ctx2.GetRepository<A>();
// Note the .Attach(ctx) extension to bring objects under control of the context
var doc2 = repoA2.Collection.AsQueryable().Attach(ctx2).First();
Assert.False(object.ReferenceEquals(doc1, doc2));
// modify (1)
{
doc1.prop1 = "_x1_"; // [1]
var itemy = doc1.Set.First();
var sety1 = itemy.Set;
var toremove = sety1.First();
sety1.Remove(toremove); // [1.1]
var iyy1 = new I() { iprop1 = "yy1", iprop2 = "yy2" };
doc1.Set.Add(iyy1); // [1.2]
}
// modify (2)
{
doc2.prop2 = "_x2_"; // [2]
}
// save changes
ctx1.Save();
ctx2.Save();
}
{
// reload
var ctx = new MongoContext(connectionString);
var repo = ctx.GetRepository<A>();
// retrieve two istance of the same document
var doc = repo.Collection.AsQueryable().First();
Assert.True(doc.prop1 == "_x1_"); // [1]
Assert.True(doc.prop2 == "_x2_"); // [2]
Assert.True(doc.Set.First().Set.Count == 0); // [1.1]
}
}
#endif
}
}