Skip to content

Commit

Permalink
Fixes Issue #50 by properly reading/writing timeframes from/to JSON:
Browse files Browse the repository at this point in the history
- Change the base class `QueryTimeframe` to interface `IQueryTimeframe`.
- Add a `TimeframeConverter` to help with parsing JSON to the appropriate concrete implementation of `IQueryTimeframe`.
- Use TimeframeConverter` to also help write `QueryRelativeTimeframe` to JSON.
- We are using this new converter to create a URL-encoded JSON array to stuff in the query params for a funnel query, so we no longer drop relative timeframes.
- Now we actually parse the timeframe in the `FunnelResultStep`, whether absolute or relative.
  • Loading branch information
masojus committed Apr 17, 2017
1 parent 9a259b9 commit 70fba95
Show file tree
Hide file tree
Showing 15 changed files with 341 additions and 154 deletions.
77 changes: 69 additions & 8 deletions Keen.NET.Test/FunnelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Keen.Core.Query;
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -91,7 +92,7 @@ public async Task Funnel_Simple_Success()
queryMock = new Mock<IQueries>();
queryMock.Setup(m => m.Funnel(
It.Is<IEnumerable<FunnelStep>>(f => f.Equals(funnelsteps)),
It.Is<QueryTimeframe>(t => t == timeframe),
It.Is<IQueryTimeframe>(t => t == timeframe),
It.Is<string>(t => t == "")
))
.Returns(Task.FromResult(expected));
Expand Down Expand Up @@ -152,7 +153,7 @@ public async Task Funnel_Inverted_Success()
queryMock = new Mock<IQueries>();
queryMock.Setup(m => m.Funnel(
It.Is<IEnumerable<FunnelStep>>(f => f.Equals(funnelsteps)),
It.Is<QueryTimeframe>(t => t == timeframe),
It.Is<IQueryTimeframe>(t => t == timeframe),
It.Is<string>(t => t == "")
))
.Returns(Task.FromResult(expected));
Expand Down Expand Up @@ -223,7 +224,7 @@ public async Task Funnel_Optional_Success()
queryMock = new Mock<IQueries>();
queryMock.Setup(m => m.Funnel(
It.Is<IEnumerable<FunnelStep>>(f => f.Equals(funnelsteps)),
It.Is<QueryTimeframe>(t => t == timeframe),
It.Is<IQueryTimeframe>(t => t == timeframe),
It.Is<string>(t => t == "")
))
.Returns(Task.FromResult(expected));
Expand Down Expand Up @@ -287,7 +288,7 @@ public async Task Funnel_ValidFilter_Success()
queryMock = new Mock<IQueries>();
queryMock.Setup(m => m.Funnel(
It.Is<IEnumerable<FunnelStep>>(f => f.Equals(funnelsteps)),
It.Is<QueryTimeframe>(t => t == timeframe),
It.Is<IQueryTimeframe>(t => t == timeframe),
It.Is<string>(t => t == "")
))
.Returns(Task.FromResult(expected));
Expand Down Expand Up @@ -318,13 +319,11 @@ public async Task Funnel_ValidTimeframe_Success()
{
EventCollection = FunnelColA,
ActorProperty = "id",
Timeframe = timeframe, // Issue #50 : These are ignored.
},
new FunnelStep
{
EventCollection = FunnelColB,
ActorProperty = "id",
Timeframe = timeframe,
},
};

Expand All @@ -350,7 +349,7 @@ public async Task Funnel_ValidTimeframe_Success()
queryMock = new Mock<IQueries>();
queryMock.Setup(m => m.Funnel(
It.Is<IEnumerable<FunnelStep>>(f => f.Equals(funnelsteps)),
It.Is<QueryTimeframe>(t => t == timeframe),
It.Is<IQueryTimeframe>(t => t == timeframe),
It.Is<string>(t => t == "")
))
.Returns(Task.FromResult(expected));
Expand All @@ -369,6 +368,68 @@ public async Task Funnel_ValidTimeframe_Success()
queryMock.VerifyAll();
}

[Test]
public async Task Funnel_ValidTimeframeInSteps_Success()
{
var client = new KeenClient(SettingsEnv);

IEnumerable<FunnelStep> funnelsteps = new []
{
new FunnelStep
{
EventCollection = FunnelColA,
ActorProperty = "id",
Timeframe = QueryRelativeTimeframe.ThisMonth(),
},
new FunnelStep
{
EventCollection = FunnelColB,
ActorProperty = "id",
Timeframe = new QueryAbsoluteTimeframe(DateTime.Now.AddDays(-30), DateTime.Now),
},
};

var expected = new FunnelResult
{
Steps = new[]
{
new FunnelResultStep
{
EventCollection = FunnelColA,
},
new FunnelResultStep
{
EventCollection = FunnelColB,
},
},
Result = new[] { 3, 2 }
};

Mock<IQueries> queryMock = null;
if (UseMocks)
{
queryMock = new Mock<IQueries>();
queryMock.Setup(m => m.Funnel(
It.Is<IEnumerable<FunnelStep>>(f => f.Equals(funnelsteps)),
It.Is<IQueryTimeframe>(t => null == t),
It.Is<string>(t => t == "")
))
.Returns(Task.FromResult(expected));

client.Queries = queryMock.Object;
}

var reply = (await client.QueryFunnelAsync(funnelsteps, null));
Assert.NotNull(reply);
Assert.NotNull(reply.Result);
Assert.True(reply.Result.SequenceEqual(expected.Result));
Assert.NotNull(reply.Steps);
Assert.AreEqual(reply.Steps.Count(), 2);

if (null != queryMock)
queryMock.VerifyAll();
}

[Test]
public async Task Funnel_WithActors_Success()
{
Expand Down Expand Up @@ -415,7 +476,7 @@ public async Task Funnel_WithActors_Success()
queryMock = new Mock<IQueries>();
queryMock.Setup(m => m.Funnel(
It.Is<IEnumerable<FunnelStep>>(f => f.Equals(funnelsteps)),
It.Is<QueryTimeframe>(t => t == timeframe),
It.Is<IQueryTimeframe>(t => t == timeframe),
It.Is<string>(t => t == "")
))
.Returns(Task.FromResult(expected));
Expand Down
20 changes: 10 additions & 10 deletions Keen.NET.Test/QueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public async Task Query_ValidRelativeGroup_Success()
It.Is<string>(c => c == testCol),
It.Is<string>(p => p == ""),
It.Is<string>(g => g == groupby),
It.Is<QueryTimeframe>(t => t == timeframe),
It.Is<IQueryTimeframe>(t => t == timeframe),
It.Is<IEnumerable<QueryFilter>>(f => f == null),
It.Is<string>(z => z == "")))
.Returns(Task.FromResult(reply));
Expand Down Expand Up @@ -212,7 +212,7 @@ public async Task Query_ValidRelativeGroupInterval_Success()
It.Is<string>(c => c == testCol),
It.Is<string>(p => p == ""),
It.Is<string>(g => g == groupby),
It.Is<QueryTimeframe>(t => t == timeframe),
It.Is<IQueryTimeframe>(t => t == timeframe),
It.Is<QueryInterval>(i => i == interval),
It.Is<IEnumerable<QueryFilter>>(f => f == null),
It.Is<string>(z => z == "")))
Expand Down Expand Up @@ -277,7 +277,7 @@ public async Task Query_ValidRelative_Success()
It.Is<QueryType>(q => q == QueryType.Count()),
It.Is<string>(c => c == testCol),
It.Is<string>(p => p == ""),
It.Is<QueryTimeframe>(t => t == timeframe),
It.Is<IQueryTimeframe>(t => t == timeframe),
It.Is<IEnumerable<QueryFilter>>(f => f == null),
It.Is<string>(z => z == "")))
.Returns(Task.FromResult("0"));
Expand Down Expand Up @@ -311,7 +311,7 @@ public async Task Query_ValidRelativeInterval_Success()
It.Is<QueryType>(q => q == QueryType.Count()),
It.Is<string>(c => c == testCol),
It.Is<string>(p => p == ""),
It.Is<QueryTimeframe>(t => t == timeframe),
It.Is<IQueryTimeframe>(t => t == timeframe),
It.Is<QueryInterval>(i => i == interval),
It.Is<IEnumerable<QueryFilter>>(f => f == null),
It.Is<string>(z => z == "")))
Expand Down Expand Up @@ -342,7 +342,7 @@ public async Task Query_ValidFilter_Success()
It.Is<QueryType>(q => q == QueryType.Count()),
It.Is<string>(c => c == testCol),
It.Is<string>(p => p == ""),
It.Is<QueryTimeframe>(t => t == timeframe),
It.Is<IQueryTimeframe>(t => t == timeframe),
It.Is<IEnumerable<QueryFilter>>(f => f == filters),
It.Is<string>(z => z == "")))
.Returns(Task.FromResult("1"));
Expand Down Expand Up @@ -885,7 +885,7 @@ public async Task MultiAnalysis_Valid_Success()
queryMock.Setup(m => m.MultiAnalysis(
It.Is<string>(c => c == testCol),
It.Is<IEnumerable<MultiAnalysisParam>>(p => p == param),
It.Is<QueryTimeframe>(t => t == timeframe),
It.Is<IQueryTimeframe>(t => t == timeframe),
It.Is<IEnumerable<QueryFilter>>(f => f == null),
It.Is<string>(tz => tz == "")
))
Expand Down Expand Up @@ -926,7 +926,7 @@ public async Task MultiAnalysis_ValidRelativeTimeFrame_Success()
queryMock.Setup(m => m.MultiAnalysis(
It.Is<string>(c => c == testCol),
It.Is<IEnumerable<MultiAnalysisParam>>(p => p == param),
It.Is<QueryTimeframe>(t => t == timeframe),
It.Is<IQueryTimeframe>(t => t == timeframe),
It.Is<IEnumerable<QueryFilter>>(f => f == null),
It.Is<string>(tz => tz == "")
))
Expand Down Expand Up @@ -974,7 +974,7 @@ public async Task MultiAnalysis_ValidGroupBy_Success()
queryMock.Setup(m => m.MultiAnalysis(
It.Is<string>(c => c == testCol),
It.Is<IEnumerable<MultiAnalysisParam>>(p => p == param),
It.Is<QueryTimeframe>(t => t == timeframe),
It.Is<IQueryTimeframe>(t => t == timeframe),
It.Is<IEnumerable<QueryFilter>>(f => f == null),
It.Is<string>(g => g == groupby),
It.Is<string>(tz => tz == "")
Expand Down Expand Up @@ -1038,7 +1038,7 @@ public async Task MultiAnalysis_ValidIntervalGroupBy_Success()
queryMock.Setup(m => m.MultiAnalysis(
It.Is<string>(c => c == testCol),
It.Is<IEnumerable<MultiAnalysisParam>>(p => p == param),
It.Is<QueryTimeframe>(t => t == timeframe),
It.Is<IQueryTimeframe>(t => t == timeframe),
It.Is<QueryInterval>(i=>i==interval),
It.Is<IEnumerable<QueryFilter>>(f => f == null),
It.Is<string>(g => g == groupby),
Expand Down Expand Up @@ -1086,7 +1086,7 @@ public async Task MultiAnalysis_ValidInterval_Success()
queryMock.Setup(m => m.MultiAnalysis(
It.Is<string>(c => c == testCol),
It.Is<IEnumerable<MultiAnalysisParam>>(p => p == param),
It.Is<QueryTimeframe>(t => t == timeframe),
It.Is<IQueryTimeframe>(t => t == timeframe),
It.Is<QueryInterval>(i => i == interval),
It.Is<IEnumerable<QueryFilter>>(f => f == null),
It.Is<string>(tz => tz == "")
Expand Down
4 changes: 2 additions & 2 deletions Keen/Keen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
<Compile Include="KeenHttpClientFactory.cs" />
<Compile Include="KeenHttpClientProvider.cs" />
<Compile Include="Query\FunnelResult.cs" />
<Compile Include="Query\FunnelResultActors.cs" />
<Compile Include="Query\FunnelResultStep.cs" />
<Compile Include="Query\FunnelStep.cs" />
<Compile Include="IKeenHttpClientProvider.cs" />
Expand All @@ -62,8 +61,9 @@
<Compile Include="Query\QueryIntervalValue.cs" />
<Compile Include="Query\QueryRelativeTimeframe.cs" />
<Compile Include="Query\QueryAbsoluteTimeframe.cs" />
<Compile Include="Query\QueryTimeframe.cs" />
<Compile Include="Query\IQueryTimeframe.cs" />
<Compile Include="Query\QueryType.cs" />
<Compile Include="Query\TimeframeConverter.cs" />
<None Include="app.config" />
<Compile Include="CachedEvent.cs" />
<Compile Include="DynamicPropertyValue.cs" />
Expand Down
Loading

0 comments on commit 70fba95

Please sign in to comment.