Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3fa344f

Browse files
authored
Adding and updating some unit tests for coverage (#78)
* Added and fixed several unit tests based on code coverage * Removing some unnecessary imports and adding a little wiggle room on test timing that was causing intermittent failures locally
1 parent 8b4a42d commit 3fa344f

File tree

5 files changed

+168
-7
lines changed

5 files changed

+168
-7
lines changed

Tests/AnalyticsTest.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class AnalyticsTest
1616
{
1717
private readonly Analytics _analytics;
1818

19+
private Mock<IStorage> _storage;
20+
1921
private Settings? _settings;
2022

2123
public AnalyticsTest()
@@ -28,9 +30,13 @@ public AnalyticsTest()
2830
.Setup(httpClient => httpClient.Settings())
2931
.ReturnsAsync(_settings);
3032

33+
_storage = new Mock<IStorage>();
34+
_storage.Setup(Storage => Storage.RemoveFile("")).Returns(true);
35+
_storage.Setup(Storage => Storage.Read(StorageConstants.Events)).Returns("test,foo");
36+
3137
var config = new Configuration(
3238
writeKey: "123",
33-
storageProvider: new DefaultStorageProvider("tests"),
39+
storageProvider: new MockStorageProvider(_storage),
3440
autoAddSegmentDestination: false,
3541
useSynchronizeDispatcher: true,
3642
httpClientProvider: new MockHttpClientProvider(mockHttpClient)
@@ -173,5 +179,16 @@ public void TestDisable()
173179
Assert.True(actual.UserId != null || actual.AnonymousId != null);
174180
Assert.NotNull(actual.Integrations);
175181
}
182+
183+
[Fact]
184+
public void TestPurge()
185+
{
186+
_analytics.PurgeStorage();
187+
_storage.Verify(o => o.RemoveFile("test"), Times.Exactly(1));
188+
_storage.Verify(o => o.RemoveFile("foo"), Times.Exactly(1));
189+
190+
_analytics.PurgeStorage("bar");
191+
_storage.Verify(o => o.RemoveFile("bar"), Times.Exactly(1));
192+
}
176193
}
177194
}

Tests/EventsTest.cs

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ public void TestTrackT()
9292
Assert.Equal(expectedEvent, actual[0].Event);
9393
}
9494

95+
[Fact]
96+
public void TestTrackTNullProperties()
97+
{
98+
string expectedEvent = "foo";
99+
var actual = new List<TrackEvent>();
100+
_plugin.Setup(o => o.Track(Capture.In(actual)));
101+
102+
_analytics.Add(_plugin.Object);
103+
_analytics.Track(expectedEvent, (FooBar) null);
104+
105+
Assert.NotEmpty(actual);
106+
Assert.True(actual[0].Properties.Count == 0);
107+
Assert.Equal(expectedEvent, actual[0].Event);
108+
}
109+
95110
[Fact]
96111
public void TestTrackTNoProperties()
97112
{
@@ -166,6 +181,23 @@ public void TestIdentifyNoUserId()
166181
Assert.Equal(expectedUserId, actualUserId);
167182
}
168183

184+
[Fact]
185+
public void TestIdentifyNoUserIdNullTraits()
186+
{
187+
var actual = new List<IdentifyEvent>();
188+
_plugin.Setup(o => o.Identify(Capture.In(actual)));
189+
string expectedUserId = _analytics.UserId();
190+
191+
_analytics.Add(_plugin.Object);
192+
_analytics.Identify((JsonObject) null);
193+
194+
string actualUserId = _analytics.UserId();
195+
196+
Assert.NotEmpty(actual);
197+
Assert.True(actual[0].Traits.Count == 0);
198+
Assert.Equal(expectedUserId, actualUserId);
199+
}
200+
169201
[Fact]
170202
public void TestIdentifyT()
171203
{
@@ -184,6 +216,23 @@ public void TestIdentifyT()
184216
Assert.Equal(expectedUserId, actualUserId);
185217
}
186218

219+
[Fact]
220+
public void TestIdentifyTNullTraits()
221+
{
222+
string expectedUserId = "newUserId";
223+
var actual = new List<IdentifyEvent>();
224+
_plugin.Setup(o => o.Identify(Capture.In(actual)));
225+
226+
_analytics.Add(_plugin.Object);
227+
_analytics.Identify(expectedUserId, (FooBar) null);
228+
229+
string actualUserId = _analytics.UserId();
230+
231+
Assert.NotEmpty(actual);
232+
Assert.True(actual[0].Traits.Count == 0);
233+
Assert.Equal(expectedUserId, actualUserId);
234+
}
235+
187236
[Fact]
188237
public void TestIdentifyTNoTraits()
189238
{
@@ -192,7 +241,7 @@ public void TestIdentifyTNoTraits()
192241
_plugin.Setup(o => o.Identify(Capture.In(actual)));
193242

194243
_analytics.Add(_plugin.Object);
195-
_analytics.Identify(expectedUserId);
244+
_analytics.Identify(expectedUserId, (FooBar) null);
196245
string actualUserId = _analytics.UserId();
197246

198247
Assert.NotEmpty(actual);
@@ -217,6 +266,21 @@ public void TestIdentifyTNoUserId()
217266
Assert.Equal(expectedUserId, actualUserId);
218267
}
219268

269+
[Fact]
270+
public void TestIdentifyTNoUserIdNullTraits()
271+
{
272+
var actual = new List<IdentifyEvent>();
273+
_plugin.Setup(o => o.Identify(Capture.In(actual)));
274+
string expectedUserId = _analytics.UserId();
275+
276+
_analytics.Add(_plugin.Object);
277+
_analytics.Identify((FooBar) null);
278+
string actualUserId = _analytics.UserId();
279+
280+
Assert.NotEmpty(actual);
281+
Assert.True(actual[0].Traits.Count == 0);
282+
Assert.Equal(expectedUserId, actualUserId);
283+
}
220284

221285
[Fact]
222286
public void TestIdentifyReload()
@@ -297,7 +361,7 @@ public void TestScreenTWithNulls()
297361
_plugin.Setup(o => o.Screen(Capture.In(actual)));
298362

299363
_analytics.Add(_plugin.Object);
300-
_analytics.Screen(null, null, null);
364+
_analytics.Screen(null, (FooBar) null, null);
301365

302366
Assert.NotEmpty(actual);
303367
Assert.True(actual[0].Properties.Count == 0);
@@ -369,7 +433,7 @@ public void TestPageTWithNulls()
369433
_plugin.Setup(o => o.Page(Capture.In(actual)));
370434

371435
_analytics.Add(_plugin.Object);
372-
_analytics.Page(null, null, null);
436+
_analytics.Page(null, (FooBar) null, null);
373437

374438
Assert.NotEmpty(actual);
375439
Assert.True(actual[0].Properties.Count == 0);
@@ -429,14 +493,14 @@ public void TestGroupT()
429493
}
430494

431495
[Fact]
432-
public void TestGroupTNoProperties()
496+
public void TestGroupTNullProperties()
433497
{
434498
string expectedGroupId = "foo";
435499
var actual = new List<GroupEvent>();
436500
_plugin.Setup(o => o.Group(Capture.In(actual)));
437501

438502
_analytics.Add(_plugin.Object);
439-
_analytics.Group(expectedGroupId);
503+
_analytics.Group(expectedGroupId, (FooBar) null);
440504

441505
Assert.NotEmpty(actual);
442506
Assert.True(actual[0].Traits.Count == 0);

Tests/PluginsTest.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using System.Linq;
23
using Moq;
34
using Segment.Analytics;
@@ -105,5 +106,57 @@ public void FindByDestinationKey()
105106

106107
Assert.Equal(expected, actual);
107108
}
109+
110+
[Fact]
111+
public void TestDestinationPluginProcess()
112+
{
113+
var plugin = new Mock<DestinationPlugin>
114+
{
115+
// need this setting to prevent faking internal methods
116+
CallBase = true
117+
};
118+
plugin.Setup(o => o.Key).Returns("mock");
119+
120+
var alias = new List<AliasEvent>();
121+
plugin.Setup(o => o.Alias(Moq.Capture.In(alias))).Returns((AliasEvent a) => {return a;}).Verifiable();
122+
var group = new List<GroupEvent>();
123+
plugin.Setup(o => o.Group(Moq.Capture.In(group))).Returns((GroupEvent a) => {return a;}).Verifiable();
124+
var identify = new List<IdentifyEvent>();
125+
plugin.Setup(o => o.Identify(Moq.Capture.In(identify))).Returns((IdentifyEvent a) => {return a;}).Verifiable();
126+
var page = new List<PageEvent>();
127+
plugin.Setup(o => o.Page(Moq.Capture.In(page))).Returns((PageEvent a) => {return a;}).Verifiable();
128+
var screen = new List<ScreenEvent>();
129+
plugin.Setup(o => o.Screen(Moq.Capture.In(screen))).Returns((ScreenEvent a) => {return a;}).Verifiable();
130+
var track = new List<TrackEvent>();
131+
plugin.Setup(o => o.Track(Moq.Capture.In(track))).Returns((TrackEvent a) => {return a;}).Verifiable();
132+
133+
_analytics.Add(plugin.Object);
134+
_analytics.ManuallyEnableDestination(plugin.Object);
135+
136+
_analytics.Alias("testalias");
137+
plugin.Verify(o => o.Alias(It.IsAny<AliasEvent>()), Times.Exactly(1));
138+
Assert.Equal("testalias", alias[0].UserId);
139+
140+
_analytics.Group("testgroup");
141+
plugin.Verify(o => o.Group(It.IsAny<GroupEvent>()), Times.Exactly(1));
142+
Assert.Equal("testgroup", group[0].GroupId);
143+
144+
_analytics.Identify("testidentify");
145+
plugin.Verify(o => o.Identify(It.IsAny<IdentifyEvent>()), Times.Exactly(1));
146+
Assert.Equal("testidentify", identify[0].UserId);
147+
148+
_analytics.Page("testpage");
149+
plugin.Verify(o => o.Page(It.IsAny<PageEvent>()), Times.Exactly(1));
150+
Assert.Equal("testpage", page[0].Name);
151+
152+
_analytics.Screen("testscreen");
153+
plugin.Verify(o => o.Screen(It.IsAny<ScreenEvent>()), Times.Exactly(1));
154+
Assert.Equal("testscreen", screen[0].Name);
155+
156+
_analytics.Track("testtrack");
157+
plugin.Verify(o => o.Track(It.IsAny<TrackEvent>()), Times.Exactly(1));
158+
Assert.Equal("testtrack", track[0].Event);
159+
160+
}
108161
}
109162
}

Tests/Utilities/EventPipelineTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public void TestStart()
104104
[Fact]
105105
public async void TestStop()
106106
{
107+
_eventPipeline.Start();
108+
Assert.True(_eventPipeline.Running);
107109
_eventPipeline.Stop();
108110
Assert.False(_eventPipeline.Running);
109111

@@ -165,7 +167,7 @@ public async Task TestPeriodicalFlush()
165167
_eventPipeline.Start();
166168
_eventPipeline.Put(new ScreenEvent("test"));
167169

168-
await Task.Delay(2000);
170+
await Task.Delay(2050);
169171

170172
_storage.Verify(o => o.Rollover(), Times.Exactly(2));
171173
_storage.Verify(o => o.Read(StorageConstants.Events), Times.Exactly(2));

Tests/Utilities/UserPrefsTest.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,31 @@ public void EditorTest()
131131
Assert.Equal(expectedInt, _prefs.GetInt("int"));
132132
Assert.Equal(expectedFloat, _prefs.GetFloat("float"));
133133
Assert.Equal(expectedString, _prefs.GetString("string"));
134+
135+
136+
}
137+
138+
[Fact]
139+
public void EditorClearTest()
140+
{
141+
int expectedInt = 100;
142+
float expectedFloat = 100f;
143+
var userPrefs = (UserPrefs)_prefs;
144+
Editor editor = userPrefs.Edit()
145+
.PutInt("int", expectedInt)
146+
.Clear()
147+
.PutFloat("float", expectedFloat);
148+
149+
Assert.Equal(1, _prefs.GetInt("int"));
150+
Assert.Equal(0.1f, _prefs.GetFloat("float"));
151+
Assert.Equal("string", _prefs.GetString("string"));
152+
153+
// This should remove the string and int and use the new float value
154+
editor.Apply();
155+
156+
Assert.Equal(-1, _prefs.GetInt("int")); // default value
157+
Assert.Equal(expectedFloat, _prefs.GetFloat("float"));
158+
Assert.Null(_prefs.GetString("string")); // default value
134159
}
135160
}
136161
}

0 commit comments

Comments
 (0)