Skip to content

Commit 64dd277

Browse files
committed
Fixed Unit Tests
1 parent e31b01e commit 64dd277

File tree

3 files changed

+137
-20
lines changed

3 files changed

+137
-20
lines changed

Chronological.Tests/HttpRepositoryTests.cs

Lines changed: 101 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,126 @@ public class HttpRepositoryTests
1111
string applicationClientSecret = "testApplicationSecret";
1212
string tenant = "testTenant";
1313

14-
[Fact(Skip = "Valid Client-Id and Client-Secret requried.")]
14+
[Fact]
1515
public async void GenericFluentEventQueryTest()
1616
{
17-
var connection = new Connection(applicationClientId, applicationClientSecret, tenant);
18-
var environment = connection.GetEnvironmentsAsync().Result.FirstOrDefault();
17+
//var connection = new Connection(applicationClientId, applicationClientSecret, tenant);
18+
//var environment = connection.GetEnvironmentsAsync().Result.FirstOrDefault();
19+
20+
var environment = new Environment("TestFqdn", "TestAccessToken");
1921

2022
var from = new DateTime(2017, 12, 23, 12, 0, 0, DateTimeKind.Utc);
2123
var to = new DateTime(2018, 10, 23, 12, 0, 0, DateTimeKind.Utc);
2224

23-
var result = await new GenericFluentEventQuery<TestType1>("Test", Search.Span(from, to), Limit.CreateLimit(Limit.Take, 10), environment, new EventWebSocketRepository(new HttpRepository(environment)))
24-
.Where(x => x.Value == 10)
25+
var result = await new GenericFluentEventQuery<EmailTest>("Test", Search.Span(from, to), Limit.CreateLimit(Limit.Take, 10), environment, new EventWebSocketRepository(new MockHttpRepository(_eventsResult))) //new HttpRepository(environment)
26+
.Where(x => x.ip == "127.0.0.1")
2527
.ExecuteAsync();
2628

2729
Assert.NotNull(result);
2830
}
2931

30-
[Fact(Skip = "Valid Client-Id and Client-Secret requried.")]
32+
[Fact]
3133
public async void AggregateQueryTest()
3234
{
33-
var connection = new Connection(applicationClientId, applicationClientSecret, tenant);
34-
var environment = connection.GetEnvironmentsAsync().Result.FirstOrDefault();
35+
//var connection = new Connection(applicationClientId, applicationClientSecret, tenant);
36+
//var environment = connection.GetEnvironmentsAsync().Result.FirstOrDefault();
37+
38+
var environment = new Environment("TestFqdn", "TestAccessToken");
3539

3640
var from = new DateTime(2017, 12, 23, 12, 0, 0, DateTimeKind.Utc);
3741
var to = new DateTime(2018, 10, 23, 12, 0, 0, DateTimeKind.Utc);
3842

39-
var result = await new GenericFluentAggregateQuery<TestType1>("Test", Search.Span(from, to), environment, new AggregateWebSocketRepository(new HttpRepository(environment)))
40-
.Select(builder => builder.UniqueValues(x => x.DataType, 10,
41-
builder.DateHistogram(x => x.Date, Breaks.InDays(1),
42-
new
43-
{
44-
Maximum = builder.Maximum(x => x.Value),
45-
Minmum = builder.Minimum(x => x.Value)
46-
})))
47-
.Where(x => x.Value > 5)
43+
var result = await new GenericFluentAggregateQuery<EmailTest>("Test", Search.Span(from, to), environment, new AggregateWebSocketRepository(new MockHttpRepository(_aggregateResults))) //new HttpRepository(environment)
44+
.Select(builder => builder.UniqueValues(x => x.ip, 10, new { Count = builder.Count() }))
45+
.Where(x => x.ip == "127.0.0.1")
4846
.ExecuteAsync();
4947

50-
var uniqueValues = result.First();
51-
52-
Assert.NotNull(uniqueValues);
48+
Assert.NotNull(result);
5349
}
50+
51+
private string _eventsResult = @"{
52+
""warnings"": [],
53+
""events"": [
54+
{
55+
""schema"": {
56+
""rid"": 0,
57+
""$esn"": ""source1"",
58+
""properties"": [
59+
{
60+
""name"": ""email"",
61+
""type"": ""String""
62+
},
63+
{
64+
""name"": ""ip"",
65+
""type"": ""String""
66+
},
67+
{
68+
""name"": ""useragent"",
69+
""type"": ""String""
70+
}
71+
]
72+
},
73+
""$ts"": ""2018-10-18T11:30:36Z"",
74+
""values"": [
75+
""test1@mail.com"",
76+
""127.0.0.1"",
77+
""IE""
78+
]
79+
},
80+
{
81+
""schemaRid"": 0,
82+
""$ts"": ""2018-10-18T11:30:36Z"",
83+
""values"": [
84+
""test2@mail.com"",
85+
""127.0.0.1"",
86+
""FF""
87+
]
88+
},
89+
{
90+
""schemaRid"": 0,
91+
""$ts"": ""2018-10-18T11:30:36Z"",
92+
""values"": [
93+
""test3@mail.com"",
94+
""127.0.0.1"",
95+
""IE""
96+
]
97+
}
98+
]
99+
}";
100+
101+
private string _aggregateResults = @"{
102+
""aggregates"": [
103+
{
104+
""dimension"": [
105+
null,
106+
""EmailOpened"",
107+
""EmailSent""
108+
],
109+
""measures"": [
110+
[
111+
10.0
112+
],
113+
[
114+
20.0
115+
],
116+
[
117+
200.0
118+
]
119+
]
120+
}
121+
],
122+
""warnings"": []
123+
}";
124+
125+
}
126+
127+
public class EmailTest
128+
{
129+
[ChronologicalEventField("email")]
130+
public string email { get; set; }
131+
[ChronologicalEventField("ip")]
132+
public string ip { get; set; }
133+
[ChronologicalEventField("useragent")]
134+
public string useragent { get; set; }
54135
}
55136
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using Newtonsoft.Json.Linq;
7+
8+
namespace Chronological.Tests
9+
{
10+
internal class MockHttpRepository : IWebRequestRepository
11+
{
12+
private readonly List<string> _results;
13+
14+
public MockHttpRepository(string result) : this(new List<string> { result })
15+
{
16+
}
17+
18+
public MockHttpRepository(List<string> results)
19+
{
20+
_results = results;
21+
}
22+
23+
async Task<IReadOnlyList<JToken>> IWebRequestRepository.ExecuteRequestAsync(string query, string resourcePath, CancellationToken cancellationToken)
24+
{
25+
if ("aggregates".Equals(resourcePath, StringComparison.OrdinalIgnoreCase))
26+
{
27+
return new List<JToken>() { JToken.Parse(_results.First())["aggregates"] };
28+
}
29+
30+
return new List<JToken> { JToken.Parse(_results.First()) };
31+
}
32+
}
33+
}

Chronological/HttpRepository.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ private static async Task<JToken> GetResponseAsync(HttpWebRequest request)
8686
}
8787
catch (WebException e)
8888
{
89+
if (e.Response == null)
90+
throw e;
91+
8992
using (WebResponse response = e.Response)
9093
{
9194
HttpWebResponse httpResponse = (HttpWebResponse)response;

0 commit comments

Comments
 (0)