Skip to content

Add cursor and summary calls to the client object #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions Client.Tests/Client.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<RestorePackages>false</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -52,15 +52,18 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DataPointModelTests.cs" />
<Compile Include="CursorTests.cs" />
<Compile Include="DeleteTests.cs" />
<Compile Include="IncrementTests.cs" />
<Compile Include="JsonDeserializationTests.cs" />
<Compile Include="Json\JsonDeserializationTests.cs" />
<Compile Include="Json\JsonSerializationTests.cs" />
<Compile Include="Model\DataPointTests.cs" />
<Compile Include="Model\SegmentTests.cs" />
<Compile Include="Model\SeriesTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReadTests.cs" />
<Compile Include="JsonSerializationTests.cs" />
<Compile Include="SeriesModelTests.cs" />
<Compile Include="SeriesTests.cs" />
<Compile Include="SummaryTests.cs" />
<Compile Include="TestCommon.cs" />
<Compile Include="WriteTests.cs" />
</ItemGroup>
Expand Down
160 changes: 160 additions & 0 deletions Client.Tests/CursorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
using Client;
using Client.Model;
using Moq;
using NUnit.Framework;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;


namespace Client.Tests
{
[TestFixture]
public class CursorTests
{
[Test]
public void SingleSegmentSmokeTest()
{
var response = new RestResponse {
StatusCode = System.Net.HttpStatusCode.OK,
Content = "{\"data\":[{\"t\":\"2012-03-27T00:00:00.000-05:00\",\"v\":12.34},{\"t\":\"2012-03-27T01:00:00.000-05:00\",\"v\":23.45}],\"rollup\":null}"
};
var mockclient = new Mock<RestClient>();
mockclient.Setup(cl => cl.Execute(It.IsAny<RestRequest>())).Returns(response);
var client = TestCommon.GetClient(mockclient.Object);
var cursor = client.ReadCursorByKey("key1", new DateTime(2012, 3, 27), new DateTime(2012, 3, 28));

var expected = new List<DataPoint> {
new DataPoint(new DateTime(2012, 3, 27, 0, 0, 0), 12.34),
new DataPoint(new DateTime(2012, 3, 27, 1, 0, 0), 23.45)
};
var output = new List<DataPoint>();
foreach(DataPoint dp in cursor)
{
output.Add(dp);
}
Assert.AreEqual(expected, output);
}

[Test]
public void MultipleSegmentSmokeTest()
{
var response1 = new RestResponse {
StatusCode = System.Net.HttpStatusCode.OK,
Content = "{\"data\":[{\"t\":\"2012-03-27T00:00:00.000-05:00\",\"v\":12.34},{\"t\":\"2012-03-27T01:00:00.000-05:00\",\"v\":23.45}],\"rollup\":null}"
};
response1.Headers.Add(new Parameter {
Name = "Link",
Value = "</v1/series/key/key1/data/segment/?start=2012-03-27T00:02:00.000-05:00&end=2012-03-28>; rel=\"next\""
});
var response2 = new RestResponse {
StatusCode = System.Net.HttpStatusCode.OK,
Content = "{\"data\":[{\"t\":\"2012-03-27T02:00:00.000-05:00\",\"v\":34.56}],\"rollup\":null}"
};
var calls = 0;
RestResponse[] responses = { response1, response2 };
var mockclient = new Mock<RestClient>();
mockclient.Setup(cl => cl.Execute(It.IsAny<RestRequest>())).Returns(() => responses[calls]).Callback(() => calls++);

var client = TestCommon.GetClient(mockclient.Object);
var cursor = client.ReadCursorByKey("key1", new DateTime(2012, 3, 27), new DateTime(2012, 3, 28));

var expected = new List<DataPoint> {
new DataPoint(new DateTime(2012, 3, 27, 0, 0, 0), 12.34),
new DataPoint(new DateTime(2012, 3, 27, 1, 0, 0), 23.45),
new DataPoint(new DateTime(2012, 3, 27, 2, 0, 0), 34.56)
};
var output = new List<DataPoint>();
foreach(DataPoint dp in cursor)
{
output.Add(dp);
}
Assert.AreEqual(expected, output);
}

[Test]
public void RequestMethod()
{
var response = new RestResponse {
StatusCode = System.Net.HttpStatusCode.OK,
Content = "{\"data\":[{\"t\":\"2012-03-27T00:00:00.000-05:00\",\"v\":12.34},{\"t\":\"2012-03-27T01:00:00.000-05:00\",\"v\":23.45}],\"rollup\":null}"
};
var mockclient = TestCommon.GetMockRestClient(response);
var client = TestCommon.GetClient(mockclient.Object);
client.ReadCursorByKey("key1", new DateTime(2012, 3, 27), new DateTime(2012, 3, 28));

Expression<Func<RestRequest, bool>> assertion = req => req.Method == Method.GET;
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(assertion)));
}

[Test]
public void RequestStartTime()
{
var response = new RestResponse {
StatusCode = System.Net.HttpStatusCode.OK,
Content = "{\"data\":[{\"t\":\"2012-03-27T00:00:00.000-05:00\",\"v\":12.34},{\"t\":\"2012-03-27T01:00:00.000-05:00\",\"v\":23.45}],\"rollup\":null}"
};
var mockclient = TestCommon.GetMockRestClient(response);
var client = TestCommon.GetClient(mockclient.Object);
var start = new DateTime(2012, 6, 23);
var end = new DateTime(2012, 6, 24);

client.ReadCursorByKey("testkey", start, end, IntervalParameter.Raw());

Expression<Func<RestRequest, bool>> assertion = req => TestCommon.ContainsParameter(req.Parameters, "start", "2012-06-23T00:00:00.000-05:00");
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(assertion)));
}

[Test]
public void RequestEndTime()
{
var response = new RestResponse {
StatusCode = System.Net.HttpStatusCode.OK,
Content = "{\"data\":[{\"t\":\"2012-03-27T00:00:00.000-05:00\",\"v\":12.34},{\"t\":\"2012-03-27T01:00:00.000-05:00\",\"v\":23.45}],\"rollup\":null}"
};
var mockclient = TestCommon.GetMockRestClient(response);
var client = TestCommon.GetClient(mockclient.Object);
var start = new DateTime(2012, 6, 23);
var end = new DateTime(2012, 6, 24);

client.ReadCursorByKey("testkey", start, end, IntervalParameter.Raw());

Expression<Func<RestRequest, bool>> assertion = req => TestCommon.ContainsParameter(req.Parameters, "end", "2012-06-24T00:00:00.000-05:00");
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(assertion)));
}

[Test]
public void RequestUrl()
{
var response = new RestResponse {
StatusCode = System.Net.HttpStatusCode.OK,
Content = "{\"data\":[{\"t\":\"2012-03-27T00:00:00.000-05:00\",\"v\":12.34},{\"t\":\"2012-03-27T01:00:00.000-05:00\",\"v\":23.45}],\"rollup\":null}"
};
var mockclient = TestCommon.GetMockRestClient(response);
var client = TestCommon.GetClient(mockclient.Object);

client.ReadCursorByKey("testkey", new DateTime(2012, 06, 23), new DateTime(2012, 06, 24), IntervalParameter.Raw());

mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(req => req.Resource == "/{version}/series/{property}/{value}/data/segment/")));
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(req => TestCommon.ContainsParameter(req.Parameters, "property", "key"))));
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(req => TestCommon.ContainsParameter(req.Parameters, "value", "testkey"))));
}

[Test]
public void RequestInterval()
{
var response = new RestResponse {
StatusCode = System.Net.HttpStatusCode.OK,
Content = "{\"data\":[{\"t\":\"2012-03-27T00:00:00.000-05:00\",\"v\":12.34},{\"t\":\"2012-03-27T01:00:00.000-05:00\",\"v\":23.45}],\"rollup\":null}"
};
var mockclient = TestCommon.GetMockRestClient(response);
var client = TestCommon.GetClient(mockclient.Object);

client.ReadCursorByKey("testkey", new DateTime(2012, 06, 23), new DateTime(2012, 06, 24), IntervalParameter.Raw());

Expression<Func<RestRequest, bool>> assertion = req => TestCommon.ContainsParameter(req.Parameters, "interval", "raw");
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(assertion)));
}
}
}
4 changes: 2 additions & 2 deletions Client.Tests/DeleteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void SmokeTest()
client.DeleteById("series-id", new DateTime(2012, 1, 1), new DateTime(2012, 1, 1, 1, 0, 0));

mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(req => req.Method == Method.DELETE)));
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(req => req.Resource == "/series/{property}/{value}/data")));
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(req => req.Resource == "/{version}/series/{property}/{value}/data")));
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(req => TestCommon.ContainsParameter(req.Parameters, "start", "2012-01-01T00:00:00.000-06:00"))));
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(req => TestCommon.ContainsParameter(req.Parameters, "end", "2012-01-01T01:00:00.000-06:00"))));
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(req => TestCommon.ContainsParameter(req.Parameters, "property", "id"))));
Expand All @@ -39,7 +39,7 @@ public void Key()
client.DeleteByKey("series-key", new DateTime(2012, 1, 1), new DateTime(2012, 1, 1, 1, 0, 0));

mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(req => req.Method == Method.DELETE)));
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(req => req.Resource == "/series/{property}/{value}/data")));
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(req => req.Resource == "/{version}/series/{property}/{value}/data")));
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(req => TestCommon.ContainsParameter(req.Parameters, "start", "2012-01-01T00:00:00.000-06:00"))));
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(req => TestCommon.ContainsParameter(req.Parameters, "end", "2012-01-01T01:00:00.000-06:00"))));
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(req => TestCommon.ContainsParameter(req.Parameters, "property", "key"))));
Expand Down
2 changes: 1 addition & 1 deletion Client.Tests/IncrementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Request()
data.Add(new DataPoint(DateTime.Now, valueToAdd));
client.IncrementByKey("key", data);

Expression<Func<RestRequest, bool>> assertion = req => req.Method == Method.POST && req.Resource == "/series/{property}/{value}/increment/";
Expression<Func<RestRequest, bool>> assertion = req => req.Method == Method.POST && req.Resource == "/{version}/series/{property}/{value}/increment/";
mockclient.Verify(cl => cl.Execute(It.Is<RestRequest>(assertion)));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Client.Model;
using Client.Json;
using Client.Model;
using Moq;
using NUnit.Framework;
using RestSharp;
Expand Down Expand Up @@ -172,5 +173,46 @@ public void EmptySummary()
Assert.AreEqual(0, result.Summary.Count);
}
}

[TestFixture]
class SummaryTests
{
static string jsonSummary = @"{
""mean"": 3.00,
""sum"": 12.00,
""min"": 2.00,
""max"": 4.00,
""stddev"": 0.8165,
""ss"": 2.00,
""count"": 4
}";

[Test]
public void SmokeTest()
{
var jsonResponse = new RestResponse {
Content = SummaryTests.jsonSummary
};

var result = JsonDeserializationTests.deserializer.Deserialize<Summary>(jsonResponse);
Assert.AreEqual(3.0, result["mean"]);
Assert.AreEqual(12.0, result["sum"]);
Assert.AreEqual(2.0, result["min"]);
Assert.AreEqual(4.0, result["max"]);
Assert.AreEqual(0.8165, result["stddev"]);
Assert.AreEqual(2.0, result["ss"]);
Assert.AreEqual(4, result["count"]);
}

[Test]
public void Empty()
{
var jsonResponse = new RestResponse {
Content = "{}"
};
var result = JsonDeserializationTests.deserializer.Deserialize<Summary>(jsonResponse);
Assert.AreEqual(0, result.Count);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Client.Model;
using Client.Json;
using Client.Model;
using Moq;
using NUnit.Framework;
using RestSharp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Client.Tests
{
[TestFixture]
class DataPointModelTests
class DataPointTests
{
[Test]
public void Unequality_Timestamp()
Expand Down
65 changes: 65 additions & 0 deletions Client.Tests/Model/SegmentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Client.Model;
using NUnit.Framework;
using System;
using System.Collections.Generic;


namespace Client.Tests
{
[TestFixture]
public class SegmentTests
{
[Test]
public void Constructor()
{
List<DataPoint> data = new List<DataPoint> { new DataPoint(new DateTime(2012, 3, 27), 12.34) };
Segment segment = new Segment(data, "next");
Assert.AreEqual(data, segment.Data);
Assert.AreEqual("next", segment.NextUrl);
}

[Test]
public void Deserialize()
{
string content = "{\"data\":[{\"t\":\"2012-03-27T00:00:00.000-05:00\",\"v\":12.34}],\"rollup\":null}";
RestSharp.RestResponse response = new RestSharp.RestResponse {
StatusCode = System.Net.HttpStatusCode.OK,
Content = content
};
RestSharp.Parameter link = new RestSharp.Parameter {
Name = "Link",
Value = "</v1/series/key/key1/data/segment/?start=2012-01-01&end=2012-01-02>; rel=\"next\""
};
response.Headers.Add(link);

Segment segment = Segment.FromResponse(response);
Segment expected = new Segment(new List<DataPoint>{new DataPoint(new DateTime(2012, 3, 27), 12.34)}, "/v1/series/key/key1/data/segment/?start=2012-01-01&end=2012-01-02");
Assert.AreEqual(expected.Data, segment.Data);
Assert.AreEqual(expected.NextUrl, segment.NextUrl);
}

[Test]
public void Iterator()
{
string content = "{\"data\":[{\"t\":\"2012-03-27T00:00:00.000-05:00\",\"v\":12.34},{\"t\":\"2012-03-27T01:00:00.000-05:00\",\"v\":23.45}],\"rollup\":null}";
RestSharp.RestResponse response = new RestSharp.RestResponse
{
StatusCode = System.Net.HttpStatusCode.OK,
Content = content
};

Segment segment = Segment.FromResponse(response);
List<DataPoint> expected = new List<DataPoint> {
new DataPoint(new DateTime(2012, 3, 27, 0, 0, 0), 12.34),
new DataPoint(new DateTime(2012, 3, 27, 1, 0, 0), 23.45)
};

List<DataPoint> output = new List<DataPoint>();
foreach(DataPoint dp in segment)
{
output.Add(dp);
}
Assert.AreEqual(expected, output);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Client.Tests
{
[TestFixture]
class SeriesModelTests
class SeriesTests
{
[Test]
public void Equality()
Expand Down
6 changes: 3 additions & 3 deletions Client.Tests/ReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void RequestUrl()

client.ReadByKey("testkey", new DateTime(2012, 06, 23), new DateTime(2012, 06, 24), IntervalParameter.Raw());

mockclient.Verify(cl => cl.Execute<DataSet>(It.Is<RestRequest>(req => req.Resource == "/series/{property}/{value}/data")));
mockclient.Verify(cl => cl.Execute<DataSet>(It.Is<RestRequest>(req => req.Resource == "/{version}/series/{property}/{value}/data")));
mockclient.Verify(cl => cl.Execute<DataSet>(It.Is<RestRequest>(req => TestCommon.ContainsParameter(req.Parameters, "property", "key"))));
mockclient.Verify(cl => cl.Execute<DataSet>(It.Is<RestRequest>(req => TestCommon.ContainsParameter(req.Parameters, "value", "testkey"))));
}
Expand All @@ -108,7 +108,7 @@ public void RequestMethod_Id()

client.ReadById("testid", new DateTime(2012, 06, 23), new DateTime(2012, 06, 24), IntervalParameter.Raw());

mockclient.Verify(cl => cl.Execute<DataSet>(It.Is<RestRequest>(req => req.Resource == "/series/{property}/{value}/data")));
mockclient.Verify(cl => cl.Execute<DataSet>(It.Is<RestRequest>(req => req.Resource == "/{version}/series/{property}/{value}/data")));
mockclient.Verify(cl => cl.Execute<DataSet>(It.Is<RestRequest>(req => TestCommon.ContainsParameter(req.Parameters, "property", "id"))));
mockclient.Verify(cl => cl.Execute<DataSet>(It.Is<RestRequest>(req => TestCommon.ContainsParameter(req.Parameters, "value", "testid"))));
}
Expand Down Expand Up @@ -178,7 +178,7 @@ public void RequestUrl()
filter.AddKey("series2");
client.ReadMultipleSeries(new DateTime(2012, 06, 23), new DateTime(2012, 06, 24), filter, IntervalParameter.Raw());

Expression<Func<RestRequest, bool>> assertion = req => req.Resource == "/data/";
Expression<Func<RestRequest, bool>> assertion = req => req.Resource == "/{version}/data/";
mockclient.Verify(cl => cl.Execute<List<DataSet>>(It.Is<RestRequest>(assertion)));

}
Expand Down
Loading