-
Notifications
You must be signed in to change notification settings - Fork 636
/
Copy pathAnalyticsTests.cs
215 lines (178 loc) · 7.23 KB
/
AnalyticsTests.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Autodesk.Analytics.Core;
using Autodesk.Analytics.Events;
using DesignScript.Builtin;
using Dynamo.Configuration;
using Dynamo.Logging;
using Dynamo.Models;
using Moq;
using NUnit.Framework;
namespace Dynamo.Tests
{
class TestAnalytics
{
public static void Init(IAnalyticsClient client, DynamoModel model)
{
Analytics.Start(client);
}
public static bool IsEnabled
{
get { return Analytics.IsEnabled; }
}
public static void Throw<T>() where T : Exception, new()
{
throw new T();
}
public static void TrackException<T>(bool isFatal) where T : Exception, new()
{
try
{
Throw<T>();
}
catch(Exception ex)
{
Analytics.TrackException(ex, isFatal);
}
}
}
public class AnalyticsTestsBase : DynamoModelTestBase
{
protected Mock<IAnalyticsClient> clientMoq;
public override void Setup()
{
base.Setup();
clientMoq = MockClient();
//Setup mock client and start analytics tracking.
TestAnalytics.Init(clientMoq.Object, CurrentDynamoModel);
}
protected virtual Mock<IAnalyticsClient> MockClient()
{
return new Mock<IAnalyticsClient>();
}
public override void Cleanup()
{
clientMoq.Verify(c => c.Start(), Times.Exactly(1));
base.Cleanup();
if (TestAnalytics.IsEnabled)
{
clientMoq.Verify(c => c.ShutDown());
}
}
protected virtual void VerifyEventTracking(Times times)
{
Analytics.TrackEvent(Actions.New, Categories.NodeOperations, "New Node", 5);
Thread.Sleep(100);
clientMoq.Verify(c => c.TrackEvent(Actions.New, Categories.NodeOperations, "New Node", 5), times);
Analytics.TrackScreenView("TestScreen");
Thread.Sleep(100);
clientMoq.Verify(c => c.TrackScreenView("TestScreen"), times);
Analytics.TrackActivityStatus("User");
Thread.Sleep(100);
clientMoq.Verify(c => c.TrackActivityStatus("User"), times);
TestAnalytics.TrackException<InvalidOperationException>(false);
Thread.Sleep(100);
clientMoq.Verify(c => c.TrackException(It.IsAny<InvalidOperationException>(), false), times);
var time = TimeSpan.FromMinutes(3);
var variable = "TimeVariable";
var description = "Some description";
Analytics.TrackTimedEvent(Categories.Stability, variable, time, description);
Thread.Sleep(100);
clientMoq.Verify(c => c.TrackTimedEvent(Categories.Stability, variable, time, description), times);
using (var x = Analytics.CreateTaskTimedEvent(Categories.Performance, variable, description).Result)
{
clientMoq.Verify(c => c.CreateTaskTimedEvent(Categories.Performance, variable, description, null), times);
}
var e = Analytics.TrackTaskCommandEvent("TestCommand").Result;
clientMoq.Verify(c => c.CreateTaskCommandEvent("TestCommand", "", null, null), times);
e = Analytics.TrackTaskCommandEvent("TestCommand", "TestCommand description", null, new Dictionary<string, object>() { } ).Result;
clientMoq.Verify(c => c.CreateTaskCommandEvent("TestCommand", "", null, null), times);
e = Analytics.TrackFileOperationEvent(this.TempFolder, Actions.Read, 5);
Thread.Sleep(100);
clientMoq.Verify(c => c.TrackFileOperationEvent(this.TempFolder, Actions.Read, 5, ""), times);
}
}
public class AnalyticsTests : AnalyticsTestsBase
{
[Test, Order(1)]
public void EventTrackingEnabled()
{
VerifyEventTracking(Times.Exactly(1));
}
[Test, Order(2)]
public void EventTrackingDisabled()
{
Analytics.ShutDown();
Thread.Sleep(100);
VerifyEventTracking(Times.Never());
}
}
public class DynamoAnalyticsTests : AnalyticsTestsBase
{
protected Mock<TrackerFactory> factoryMoq;
protected Mock<IEventTracker> trackerMoq;
protected const string factoryName = "DynamoAnalyticsTests";
public DynamoAnalyticsTests()
{
dynamoSettings = new PreferenceSettings();
}
protected override Mock<IAnalyticsClient> MockClient()
{
var client = new Mock<DynamoAnalyticsClient>(DynamoModel.HostAnalyticsInfo) { CallBase = true };
var session = MockAnalyticsSession();
client.Setup(c => c.Session).Returns(session);
return client.As<IAnalyticsClient>();
}
private IAnalyticsSession MockAnalyticsSession()
{
var session = new Mock<IAnalyticsSession>();
session.Setup(s => s.UserId).Returns("DynamoTestUser");
session.Setup(s => s.SessionId).Returns("UniqueSession");
session.Setup(s => s.Start()).Callback(SetupServices);
return session.Object;
}
private void SetupServices()
{
factoryMoq = new Mock<TrackerFactory>() { CallBase = true };
factoryMoq.Setup(f => f.UniqueName).Returns(factoryName);
trackerMoq = new Mock<IEventTracker>();
factoryMoq.Object.Register<AnalyticsEvent>(trackerMoq.Object);
Service.Instance.Register(factoryMoq.Object);
}
public override void Cleanup()
{
base.Cleanup();
Service.Instance.Unregister(factoryName);
}
[Test]
public void AnalyticsTrackingEnabled()
{
VerifyEventTracking(Times.Exactly(1));
//1 ApplicationLifecycle Start + 3 for exception + 6 other events
trackerMoq.Verify(t => t.Track(It.IsAny<AnalyticsEvent>(), factoryMoq.Object), Times.AtLeast(10));
}
[Test]
public void CreateDisposableEvents()
{
var variable = "TimeVariable";
var description = "Some description";
IDisposable e = Analytics.CreateTaskTimedEvent(Categories.Performance, variable, description).Result;
Assert.IsInstanceOf<TimedEvent>(e);
e.Dispose();
//1 Dispose, Timed event is not tracked for creation.
trackerMoq.Verify(t => t.Track(e as TimedEvent, factoryMoq.Object), Times.Exactly(1));
e = Analytics.TrackTaskCommandEvent("TestCommand").Result;
Assert.IsInstanceOf<CommandEvent>(e);
e.Dispose();
//1 Create + 1 Dispose
trackerMoq.Verify(t => t.Track(e as CommandEvent, factoryMoq.Object), Times.Exactly(2));
e = Analytics.TrackTaskFileOperationEvent(this.TempFolder, Actions.Save, 5).Result;
Assert.IsInstanceOf<FileOperationEvent>(e);
e.Dispose();
//1 Create + 1 Dispose
trackerMoq.Verify(t => t.Track(e as FileOperationEvent, factoryMoq.Object), Times.Exactly(2));
}
}
}