-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathMongoDBTests.cs
More file actions
165 lines (136 loc) · 4.76 KB
/
MongoDBTests.cs
File metadata and controls
165 lines (136 loc) · 4.76 KB
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
165
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Waher.Persistence.Filters;
using Waher.Runtime.Console;
using Waher.Runtime.Inventory;
using Waher.Things;
using Waher.Things.SensorData;
namespace Waher.Persistence.MongoDB.Test
{
[TestClass]
public class MongoDBTests
{
[AssemblyInitialize]
public static void AssemblyInitialize(TestContext _)
{
Types.Initialize(typeof(MongoDBProvider).Assembly,
typeof(MongoDBTests).Assembly,
typeof(Types).Assembly,
typeof(ThingReference).Assembly);
Database.Register(new MongoDBProvider("MongoDBTest", "Default"));
}
[TestMethod]
public void Test_01_Serializer()
{
((MongoDBProvider)Database.Provider).GetObjectSerializer(typeof(ThingReference));
}
[TestMethod]
public async Task Test_02_Insert()
{
ThingReference Node = new("Node1");
await Database.Insert(Node);
}
[TestMethod]
public async Task Test_03_InsertMany()
{
await Database.Insert(new ThingReference("Node2"), new ThingReference("Node3"), new ThingReference("Node4"));
}
[TestMethod]
public async Task Test_04_InsertManyDifferent()
{
ThingReference Ref = new("Node1");
DateTime TP = DateTime.Now;
await Database.Insert(
new QuantityField(Ref, TP, "Temperature", 12.3, 1, "°C", FieldType.Momentary, FieldQoS.AutomaticReadout, "TestModule", 1, 2, 3),
new BooleanField(Ref, TP, "Error", false, FieldType.Status, FieldQoS.AutomaticReadout, "TestModule", 2),
new DateTimeField(Ref, TP, "Last Battery Change", new DateTime(2016, 2, 13, 10, 50, 25), FieldType.Status, FieldQoS.AutomaticReadout, "TestModule", 3),
new Int32Field(Ref, TP, "Nr Restarts", 123, FieldType.Status, FieldQoS.AutomaticReadout, "TestModule", 4),
new TimeField(Ref, TP, "Time of day", TP.TimeOfDay, FieldType.Status, FieldQoS.AutomaticReadout, "TestModule", 5),
new StringField(Ref, TP, "Serial Number", "1234567890", FieldType.Identity, FieldQoS.AutomaticReadout, "TestModule", 6));
}
[TestMethod]
public async Task Test_05_Find()
{
IEnumerable<ThingReference> ThingReferences = await Database.Find<ThingReference>();
foreach (ThingReference ThingReference in ThingReferences)
ConsoleOut.WriteLine(ThingReference.ToString());
}
[TestMethod]
public async Task Test_06_FindFilter()
{
IEnumerable<ThingReference> ThingReferences = await Database.Find<ThingReference>(new FilterFieldEqualTo("NodeId", "Node2"));
foreach (ThingReference ThingReference in ThingReferences)
ConsoleOut.WriteLine(ThingReference.ToString());
}
[TestMethod]
public async Task Test_07_FindFilterSort()
{
IEnumerable<ThingReference> ThingReferences = await Database.Find<ThingReference>(new FilterFieldLikeRegEx("NodeId", "Node(2|3)"), "-NodeId");
foreach (ThingReference ThingReference in ThingReferences)
ConsoleOut.WriteLine(ThingReference.ToString());
}
[TestMethod]
public async Task Test_08_FindInherited()
{
IEnumerable<Field> Fields = await Database.Find<Field>();
foreach (Field Field in Fields)
ConsoleOut.WriteLine(Field.ToString());
}
[TestMethod]
public async Task Test_09_Update()
{
IEnumerable<ThingReference> ThingReferences = await Database.Find<ThingReference>();
foreach (ThingReference ThingReference in ThingReferences)
ThingReference.SourceId = "Source 1";
await Database.Update(ThingReferences);
ThingReferences = await Database.Find<ThingReference>();
foreach (ThingReference ThingReference in ThingReferences)
ConsoleOut.WriteLine(ThingReference.ToString());
}
[TestMethod]
public async Task Test_10_Delete()
{
IEnumerable<Field> Fields = await Database.Find<Field>();
await Database.Delete(Fields);
}
[TestMethod]
public async Task Test_11_Process()
{
await Database.Process(new ConsoleOutProcessor<ThingReference>());
}
private class ConsoleOutProcessor<T> : IProcessor<T>
{
public ConsoleOutProcessor()
{
}
public bool IsAsynchronous => false;
public bool Process(T Object)
{
ConsoleOut.WriteLine(Object.ToString());
return true;
}
public Task<bool> ProcessAsync(T Object) => Task.FromResult(false);
public bool Flush() => true;
public Task<bool> FlushAsync() => Task.FromResult(true);
}
[TestMethod]
public async Task Test_12_ProcessFilter()
{
await Database.Process(new ConsoleOutProcessor<ThingReference>(),
new FilterFieldEqualTo("NodeId", "Node2"));
}
[TestMethod]
public async Task Test_13_ProcessFilterSort()
{
await Database.Process(new ConsoleOutProcessor<ThingReference>(),
new FilterFieldLikeRegEx("NodeId", "Node(2|3)"), "-NodeId");
}
[TestMethod]
public async Task Test_14_ProcessInherited()
{
await Database.Process(new ConsoleOutProcessor<Field>());
}
}
}