Permalink
Browse files

Initial Commit; 1 test with Mocks

  • Loading branch information...
1 parent 78e9253 commit a732b6408fcb29a6eed50fc9e835af56b069de8a @maddenpj maddenpj committed Dec 13, 2012
@@ -45,6 +45,13 @@
<Reference Include="MbUnit40, Version=3.3.0.0, Culture=neutral, PublicKeyToken=eb9cfa67ee6ab36e, processorArchitecture=MSIL">
<HintPath>..\packages\mbunit.3.3.454.0\lib\net40\MbUnit40.dll</HintPath>
</Reference>
+ <Reference Include="Moq">
+ <HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
+ </Reference>
+ <Reference Include="RestSharp, Version=104.1.0.0, Culture=neutral, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\RestSharp.104.1\lib\net4\RestSharp.dll</HintPath>
+ </Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
View
@@ -1,26 +1,29 @@
using System;
+using System.Collections.Generic;
using Client.Model;
using MbUnit.Framework;
+using Moq;
namespace Client.Tests
{
[TestFixture]
public class ReadTests
{
- private const string API_KEY = "your-api-key";
- private const string API_SECRET = "your-api-secret";
- private const string TEST_SERIES_ID = "existing-series-id";
- private const string TEST_SERIES_KEY_1 = "existing-series-key-1";
- private const string TEST_SERIES_KEY_2 = "existing-series-key-2";
+ private const string API_KEY = "fddc9934f6784a739cc82e2833521218";
+ private const string API_SECRET = "6d1f4fae625b4847968b472d3feb5ba5";
+ private const string TEST_SERIES_ID = "17b836c0635844a686249969c5b768c6";
+ private const string TEST_SERIES_KEY_1 = "asdf";
+ private const string TEST_SERIES_KEY_2 = "my_favorite_series";
- private Client GetClient()
+ private Client GetClient(RestSharp.RestClient restClient = null)
{
return new ClientBuilder()
.Host("api.tempo-db.com")
.Key(API_KEY)
.Port(443)
.Secret(API_SECRET)
.Secure(true)
+ .RestClient(restClient)
.Build();
}
@@ -49,12 +52,25 @@ public void ItShouldReadSeriesDataById()
[Test]
public void ItShouldReadRawData()
{
- var client = GetClient();
- var filter = new Filter();
- filter.AddKey(TEST_SERIES_KEY_1);
- filter.AddKey(TEST_SERIES_KEY_2);
- var results = client.ReadMultipleSeries(new DateTime(2012, 06, 23), new DateTime(2012, 06, 24), filter, IntervalParameter.Raw());
- Assert.IsNotEmpty(results);
+ List<DataSet> ret = new List<DataSet>();
+ ret.Add(new DataSet());
+
+ var res = new RestSharp.RestResponse<List<DataSet>>
+ {
+ StatusCode = System.Net.HttpStatusCode.OK,
+ ResponseStatus = RestSharp.ResponseStatus.Completed,
+ Data = ret
+ };
+
+ var restClient = new Mock<RestSharp.RestClient>();
+ restClient.Setup(cl => cl.Execute<List<DataSet>>(It.IsAny<RestSharp.RestRequest>())).Returns(res);
+
+ var client = GetClient(restClient.Object);
+ var filter = new Filter();
+ filter.AddKey(TEST_SERIES_KEY_1);
+ filter.AddKey(TEST_SERIES_KEY_2);
+ var results = client.ReadMultipleSeries(new DateTime(2012, 06, 23), new DateTime(2012, 06, 24), filter, IntervalParameter.Raw());
+ Assert.IsNotEmpty(results);
}
@@ -8,17 +8,18 @@ namespace Client.Tests
public class SeriesTests
{
- private const string API_KEY = "your-api-key";
- private const string API_SECRET = "your-api-secret";
- private const string TEST_SERIES_ID_1 = "existing-series-id-1";
- private const string TEST_SERIES_ID_2 = "existing-series-id-2";
- private const string TEST_SERIES_KEY_1 = "existing-series-key-1";
- private const string TEST_SERIES_KEY_2 = "existing-series-key-2";
- private const string TEST_SERIES_TAG = "existing-series-tag";
- private const string TEST_ATTRIBUTE_KEY = "existing-attribute-key";
- private const string TEST_ATTRIBUTE_VALUE = "existing-attribute-value";
+ private const string API_KEY = "fddc9934f6784a739cc82e2833521218";
+ private const string API_SECRET = "6d1f4fae625b4847968b472d3feb5ba5";
+ private const string TEST_SERIES_ID_1 = "17b836c0635844a686249969c5b768c6";
+ private const string TEST_SERIES_KEY_1 = "asdf";
+ private const string TEST_SERIES_KEY_2 = "my_favorite_series";
+ private const string TEST_SERIES_ID_2 = "1adba64a618745b28c6e7762f88f3722";
+ private const string TEST_SERIES_TAG = "temp";
+ private const string TEST_ATTRIBUTE_KEY = "sensor";
+ private const string TEST_ATTRIBUTE_VALUE = "1";
+
private Client GetClient()
{
return new ClientBuilder()
View
@@ -8,11 +8,11 @@ namespace Client.Tests
[TestFixture]
public class WriteTests
{
- private const string API_KEY = "your-api-key";
- private const string API_SECRET = "your-api-secret";
- private const string TEST_SERIES_ID = "existing-series-id";
- private const string TEST_SERIES_KEY_1 = "existing-series-key-1";
- private const string TEST_SERIES_KEY_2 = "existing-series-key-2";
+ private const string API_KEY = "fddc9934f6784a739cc82e2833521218";
+ private const string API_SECRET = "6d1f4fae625b4847968b472d3feb5ba5";
+ private const string TEST_SERIES_ID = "17b836c0635844a686249969c5b768c6";
+ private const string TEST_SERIES_KEY_1 = "asdf";
+ private const string TEST_SERIES_KEY_2 = "my_favorite_series";
private Client GetClient()
{
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="mbunit" version="3.3.454.0" />
+ <package id="Moq" version="4.0.10827" targetFramework="net40" />
+ <package id="RestSharp" version="104.1" targetFramework="net40" />
</packages>
View
@@ -29,21 +29,24 @@ public class Client
private readonly bool _secure;
private readonly string _version;
+ private RestClient _restClient;
+
// private readonly DateTimeFormatter iso8601 = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
/// <param name="key"> Api key </param>
/// <param name="secret"> Api secret </param>
/// <param name="host"> Hostname of the api server </param>
/// <param name="port"> Port that the api server is listening on </param>
/// <param name="secure"> Uses http if false, https if true </param>
- public Client(string key, string secret, string host, int port, string version, bool secure)
+ public Client(string key, string secret, string host, int port, string version, bool secure, RestClient restClient = null)
{
_key = key;
_secret = secret;
_host = host;
_port = port;
_version = version;
_secure = secure;
+ _restClient = restClient;
}
/// <summary>
@@ -91,18 +94,26 @@ public T Execute<T>(RestRequest request) where T : new()
private RestClient GetRestClient()
{
- string protocol = _secure ? "https://" : "http://";
- string portString = (_port == 80) ? "" : ":" + _port;
- string baseUrl = protocol + _host + portString + "/" + _version;
-
- var client = new RestClient
- {
- BaseUrl = baseUrl,
- Authenticator = new HttpBasicAuthenticator(_key, _secret)
- };
-
- client.AddHandler("*", new JsonDeserializer());
- return client;
+ if (_restClient == null)
+ {
+
+ string protocol = _secure ? "https://" : "http://";
+ string portString = (_port == 80) ? "" : ":" + _port;
+ string baseUrl = protocol + _host + portString + "/" + _version;
+
+ var client = new RestClient
+ {
+ BaseUrl = baseUrl,
+ Authenticator = new HttpBasicAuthenticator(_key, _secret)
+ };
+
+ client.AddHandler("*", new JsonDeserializer());
+ return client;
+ }
+ else
+ {
+ return _restClient;
+ }
}
View
@@ -1,3 +1,5 @@
+using RestSharp;
+
namespace Client
{
/// <summary>
@@ -27,14 +29,15 @@ public class ClientBuilder
private int _portRenamed = 443;
private bool _secureRenamed = true;
private string _versionRenamed = "v1";
+ private RestClient _restClientRenamed = null;
/// <summary>
/// Returns the built Client instance
/// </summary>
/// <returns> A new Client instance </returns>
public Client Build()
{
- return new Client(_keyRenamed, _secretRenamed, _hostRenamed, _portRenamed, _versionRenamed, _secureRenamed);
+ return new Client(_keyRenamed, _secretRenamed, _hostRenamed, _portRenamed, _versionRenamed, _secureRenamed, _restClientRenamed);
}
/// <summary>
@@ -98,5 +101,15 @@ public ClientBuilder Secure(bool secure)
_secureRenamed = secure;
return this;
}
+
+ /// <summary>
+ /// Sets the rest client
+ /// </summary>
+ /// <param name="restClient"> RestClient </param>
+ public ClientBuilder RestClient(RestClient restClient)
+ {
+ _restClientRenamed = restClient;
+ return this;
+ }
}
}
@@ -33,6 +33,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
+ <Reference Include="Microsoft.QualityTools.Testing.Fakes, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.4.5.7\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>

0 comments on commit a732b64

Please sign in to comment.