Skip to content

Commit

Permalink
Added write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maddenpj committed Dec 14, 2012
1 parent b5d10cf commit dea1a59
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 79 deletions.
1 change: 1 addition & 0 deletions Client.Tests/Client.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReadTests.cs" />
<Compile Include="SeriesTests.cs" />
<Compile Include="TestCommon.cs" />
<Compile Include="WriteTests.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
86 changes: 25 additions & 61 deletions Client.Tests/ReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void ItShouldReadSeriesDataByKey()
Series = series
};

var client = GetClient(GetMockRestClient<DataSet>(ret).Object);
var client = TestCommon.GetClient(TestCommon.GetMockRestClient<DataSet>(ret).Object);
var results = client.ReadByKey("testkey", new DateTime(2012, 06, 23), new DateTime(2012, 06, 24), IntervalParameter.Raw());
Assert.IsNotNull(results);
Assert.IsNotNull(results.Series);
Expand All @@ -35,56 +35,56 @@ public void ItShouldReadSeriesDataByKey_RequestMethod()
{
Expression<Func<RestRequest, bool>> assertion = req => req.Method == Method.GET;

var client = GetClient(GetMockRestClient<DataSet>(new DataSet(),assertion).Object);
var client = TestCommon.GetClient(TestCommon.GetMockRestClient<DataSet>(new DataSet(),assertion).Object);
var results = client.ReadByKey("testkey", new DateTime(2012, 06, 23), new DateTime(2012, 06, 24), IntervalParameter.Raw());
}

[Test]
public void ItShouldReadSeriesDataByKey_RequestStartTime()
{
Expression<Func<RestRequest, bool>> assertion = req => ContainsParameter(req.Parameters, "start", "2012-06-23T00:00:00.000-07:00");
Expression<Func<RestRequest, bool>> assertion = req => TestCommon.ContainsParameter(req.Parameters, "start", "2012-06-23T00:00:00.000-07:00");

var client = GetClient(GetMockRestClient<DataSet>(new DataSet(), assertion).Object);
var client = TestCommon.GetClient(TestCommon.GetMockRestClient<DataSet>(new DataSet(), assertion).Object);
var results = client.ReadByKey("testkey", new DateTime(2012, 06, 23), new DateTime(2012, 06, 24), IntervalParameter.Raw());
}

[Test]
public void ItShouldReadSeriesDataByKey_RequestEndTime()
{
Expression<Func<RestRequest, bool>> assertion = req => ContainsParameter(req.Parameters, "end", "2012-06-24T00:00:00.000-07:00");
Expression<Func<RestRequest, bool>> assertion = req => TestCommon.ContainsParameter(req.Parameters, "end", "2012-06-24T00:00:00.000-07:00");

var client = GetClient(GetMockRestClient<DataSet>(new DataSet(), assertion).Object);
var client = TestCommon.GetClient(TestCommon.GetMockRestClient<DataSet>(new DataSet(), assertion).Object);
var results = client.ReadByKey("testkey", new DateTime(2012, 06, 23), new DateTime(2012, 06, 24), IntervalParameter.Raw());
}

[Test]
public void ItShouldReadSeriesDataByKey_RequestUrl()
{
Expression<Func<RestRequest, bool>> assertion = req => req.Resource == "/series/{property}/{value}/data" &&
ContainsParameter(req.Parameters, "property", "key") &&
ContainsParameter(req.Parameters, "value", "testkey");
TestCommon.ContainsParameter(req.Parameters, "property", "key") &&
TestCommon.ContainsParameter(req.Parameters, "value", "testkey");

var client = GetClient(GetMockRestClient<DataSet>(new DataSet(), assertion).Object);
var client = TestCommon.GetClient(TestCommon.GetMockRestClient<DataSet>(new DataSet(), assertion).Object);
var results = client.ReadByKey("testkey", new DateTime(2012, 06, 23), new DateTime(2012, 06, 24), IntervalParameter.Raw());
}

[Test]
public void ItShouldReadSeriesDataByKey_RequestInterval()
{
Expression<Func<RestRequest, bool>> assertion = req => ContainsParameter(req.Parameters, "interval", "raw");
Expression<Func<RestRequest, bool>> assertion = req => TestCommon.ContainsParameter(req.Parameters, "interval", "raw");

var client = GetClient(GetMockRestClient<DataSet>(new DataSet(), assertion).Object);
var client = TestCommon.GetClient(TestCommon.GetMockRestClient<DataSet>(new DataSet(), assertion).Object);
var results = client.ReadByKey("testkey", new DateTime(2012, 06, 23), new DateTime(2012, 06, 24), IntervalParameter.Raw());
}

[Test]
public void ItShouldReadSeriesDataById_RequestUrl()
{
Expression<Func<RestRequest, bool>> assertion = req => req.Resource == "/series/{property}/{value}/data" &&
ContainsParameter(req.Parameters, "property", "id") &&
ContainsParameter(req.Parameters, "value", "testid");
TestCommon.ContainsParameter(req.Parameters, "property", "id") &&
TestCommon.ContainsParameter(req.Parameters, "value", "testid");

var client = GetClient(GetMockRestClient<DataSet>(new DataSet(), assertion).Object);
var client = TestCommon.GetClient(TestCommon.GetMockRestClient<DataSet>(new DataSet(), assertion).Object);
var results = client.ReadById("testid", new DateTime(2012, 06, 23), new DateTime(2012, 06, 24), IntervalParameter.Raw());
}

Expand All @@ -101,8 +101,8 @@ public void ItShouldReadMultipleSeries_Response()
Series = new Series { Key = "series2" }
}};

var restClient = GetMockRestClient<List<DataSet>>(ret).Object;
var client = GetClient(restClient);
var restClient = TestCommon.GetMockRestClient<List<DataSet>>(ret).Object;
var client = TestCommon.GetClient(restClient);
var filter = new Filter();
filter.AddKey("series1");
filter.AddKey("series2");
Expand All @@ -117,7 +117,7 @@ public void ItShouldReadMultipleSeries_RequestUrl()
{
Expression<Func<RestRequest, bool>> assertion = req => req.Resource == "/data/";

var client = GetClient(GetMockRestClient<List<DataSet>>(new List<DataSet>(), assertion).Object);
var client = TestCommon.GetClient(TestCommon.GetMockRestClient<List<DataSet>>(new List<DataSet>(), assertion).Object);
var filter = new Filter();
filter.AddKey("series1");
filter.AddKey("series2");
Expand All @@ -127,10 +127,10 @@ public void ItShouldReadMultipleSeries_RequestUrl()
[Test]
public void ItShouldReadMultipleSeries_RequestFilter()
{
Expression<Func<RestRequest, bool>> assertion = req => ContainsParameter(req.Parameters, "key", "series1") &&
ContainsParameter(req.Parameters, "key", "series2");
Expression<Func<RestRequest, bool>> assertion = req => TestCommon.ContainsParameter(req.Parameters, "key", "series1") &&
TestCommon.ContainsParameter(req.Parameters, "key", "series2");

var client = GetClient(GetMockRestClient<List<DataSet>>(new List<DataSet>(), assertion).Object);
var client = TestCommon.GetClient(TestCommon.GetMockRestClient<List<DataSet>>(new List<DataSet>(), assertion).Object);
var filter = new Filter();
filter.AddKey("series1");
filter.AddKey("series2");
Expand All @@ -140,57 +140,21 @@ public void ItShouldReadMultipleSeries_RequestFilter()
[Test]
public void ItShouldReadSeriesDataByKey_RequestInterval1Hour()
{
Expression<Func<RestRequest, bool>> assertion = req => ContainsParameter(req.Parameters, "interval", "1hour");
Expression<Func<RestRequest, bool>> assertion = req => TestCommon.ContainsParameter(req.Parameters, "interval", "1hour");

var client = GetClient(GetMockRestClient<DataSet>(new DataSet(), assertion).Object);
var client = TestCommon.GetClient(TestCommon.GetMockRestClient<DataSet>(new DataSet(), assertion).Object);
var results = client.ReadByKey("testkey", new DateTime(2012, 06, 23), new DateTime(2012, 06, 24), IntervalParameter.Hours(1));
}

[Test]
public void ItShouldReadSeriesDataByKey_RequestFunction()
{
Expression<Func<RestRequest, bool>> assertion = req => ContainsParameter(req.Parameters, "function", "sum");
Expression<Func<RestRequest, bool>> assertion = req => TestCommon.ContainsParameter(req.Parameters, "function", "sum");

var client = GetClient(GetMockRestClient<DataSet>(new DataSet(), assertion).Object);
var client = TestCommon.GetClient(TestCommon.GetMockRestClient<DataSet>(new DataSet(), assertion).Object);
var results = client.ReadByKey("testkey", new DateTime(2012, 06, 23), new DateTime(2012, 06, 24), IntervalParameter.Hours(1), FoldingFunction.Sum);
}

private Client GetClient(RestClient restClient = null)
{
return new ClientBuilder()
.Host("api.tempo-db.com")
.Key("api-key")
.Port(443)
.Secret("api-secret")
.Secure(true)
.RestClient(restClient)
.Build();
}

private Mock<RestClient> GetMockRestClient<T>(T response, Expression<Func<RestRequest, bool>> requestValidator = null) where T : new()
{
if (requestValidator == null)
requestValidator = req => true;

var res = new RestSharp.RestResponse<T>
{
StatusCode = System.Net.HttpStatusCode.OK,
ResponseStatus = RestSharp.ResponseStatus.Completed,
Data = response
};

var restClient = new Mock<RestSharp.RestClient>();
restClient.Setup(cl => cl.Execute<T>(It.Is<RestRequest>(requestValidator))).Returns(res);
return restClient;
}

public static bool ContainsParameter(List<Parameter> parameters, string name, string value)
{
foreach (var parameter in parameters)
{
if (parameter.Name.ToString() == name && parameter.Value.ToString() == value) return true;
}
return false;
}

}
}
65 changes: 65 additions & 0 deletions Client.Tests/TestCommon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using Moq;
using RestSharp;

namespace Client.Tests
{
class TestCommon
{
public static Client GetClient(RestClient restClient = null)
{
return new ClientBuilder()
.Host("api.tempo-db.com")
.Key("api-key")
.Port(443)
.Secret("api-secret")
.Secure(true)
.RestClient(restClient)
.Build();
}

public static Mock<RestClient> GetMockRestClient<T>(T response, Expression<Func<RestRequest, bool>> requestValidator = null) where T : new()
{
if (requestValidator == null)
requestValidator = req => true;

var res = new RestSharp.RestResponse<T>
{
StatusCode = System.Net.HttpStatusCode.OK,
ResponseStatus = RestSharp.ResponseStatus.Completed,
Data = response
};

var restClient = new Mock<RestClient>();
restClient.Setup(cl => cl.Execute<T>(It.Is<RestRequest>(requestValidator))).Returns(res);
return restClient;
}

public static Mock<RestClient> GetMockRestClient(Expression<Func<RestRequest, bool>> requestValidator = null)
{
if (requestValidator == null)
requestValidator = req => true;

var res = new RestSharp.RestResponse
{
StatusCode = System.Net.HttpStatusCode.OK
};

var restClient = new Mock<RestClient>();
restClient.Setup(cl => cl.Execute(It.Is<RestRequest>(requestValidator))).Returns(res);
return restClient;
}

public static bool ContainsParameter(List<Parameter> parameters, string name, string value)
{
foreach (var parameter in parameters)
{
if (parameter.Name.ToString() == name && parameter.Value.ToString() == value) return true;
}
return false;
}
}
}
45 changes: 28 additions & 17 deletions Client.Tests/WriteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Collections.Generic;
using Client.Model;
using MbUnit.Framework;
using RestSharp;
using System.Linq.Expressions;
using Moq;

namespace Client.Tests
{
Expand All @@ -26,43 +29,51 @@ private Client GetClient()
}

[Test]
public void ItShouldAddDataPointToSeriesByKey()
public void ItShouldAddDataPointToSeriesByKey_RequestMethod()
{
var client = GetClient();
Expression<Func<RestRequest, bool>> assertion = req => req.Method == Method.POST;

var client = TestCommon.GetClient(TestCommon.GetMockRestClient(assertion).Object);
var data = new List<DataPoint>();
double valueToAdd = new Random().NextDouble()*1000D;
data.Add(new DataPoint(DateTime.Now, valueToAdd));
client.WriteByKey(TEST_SERIES_KEY_1, data);

client.WriteByKey("testkey", data);
}

[Test]
public void ItShouldAddDataPointToSeriesById()
[Test]
public void ItShouldAddDataPointToSeriesByKey_JsonObject()
{
var client = GetClient();
var data = new List<DataPoint>();
double valueToAdd = new Random().NextDouble() * 1000D;
data.Add(new DataPoint(DateTime.Now, valueToAdd));
client.WriteById(TEST_SERIES_ID, data);
Expression<Func<RestRequest, bool>> assertion = req => TestCommon.ContainsParameter(req.Parameters, "application/json", "[{\"t\":\"2012-12-12T00:00:00.000-08:00\",\"v\":12.34}]");

var client = TestCommon.GetClient(TestCommon.GetMockRestClient(assertion).Object);
var data = new List<DataPoint>();
data.Add(new DataPoint(new DateTime(2012,12,12), 12.34));
client.WriteByKey("testkey", data);
}



[Test]
public void ItShouldAddBulkDataToMultipleSeries()
public void ItShouldAddBulkDataToMultipleSeries_RequestCount()
{
var baseDateTime = new DateTime(2012,06,23);
var client = GetClient();
for (int i = 0; i < 100; i++)
var numPoints = 100;

var mockClient = TestCommon.GetMockRestClient();
var client = TestCommon.GetClient(mockClient.Object);

var baseDateTime = new DateTime(2012, 06, 23);
for (int i = 0; i < numPoints; i++)
{
var points = new List<BulkPoint>
{
new BulkKeyPoint(TEST_SERIES_KEY_1, 12.555D * new Random().NextDouble()),
new BulkKeyPoint(TEST_SERIES_KEY_2, 555D * new Random().NextDouble())
new BulkKeyPoint("testkey1", 12.555D * new Random().NextDouble()),
new BulkKeyPoint("testkey2", 555D * new Random().NextDouble())
};

var dataSet = new BulkDataSet(baseDateTime.AddMinutes(5*i), points);
client.WriteBulkData(dataSet);
}
mockClient.Verify(cl => cl.Execute(It.IsAny<RestRequest>()), Times.Exactly(100));
}
}
}
3 changes: 2 additions & 1 deletion Client/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public void Execute(RestRequest request)
public T Execute<T>(RestRequest request) where T : new()
{
RestClient client = GetRestClient();



IRestResponse<T> response = client.Execute<T>(request);

if (response.StatusCode != HttpStatusCode.OK)
Expand Down

0 comments on commit dea1a59

Please sign in to comment.