update Tests to reflect upcoming async changes for library

This commit is contained in:
2013-10-22 21:33:58 +02:00
parent 5cc471cf3d
commit de81634646
6 changed files with 114 additions and 92 deletions
+22 -46
View File
@@ -2,98 +2,74 @@
using System.Collections.Generic;
using UptimeSharp.Models;
using System;
using System.Threading.Tasks;
namespace UptimeSharp.Tests
{
public class AlertsTest : IDisposable
public class AlertsTest : TestsBase
{
UptimeClient client;
// this API key is associated with the test account uptimesharp@outlook.com
// please don't abuse it and create your own if you want to test the project!
string APIKey = "u97240-a24c634b3b84f1af602628e8";
public AlertsTest() : base() { }
// setup
public AlertsTest()
[Fact]
public async Task AddInvalidAlertWithTypeSms()
{
client = new UptimeClient(APIKey);
}
// teardown
public void Dispose()
{
client.GetAlerts().ForEach(alert =>
await ThrowsAsync<UptimeSharpException>(async () =>
{
try
{
client.DeleteAlert(alert);
}
catch(UptimeSharpException e){}
await client.AddAlert(Models.AlertType.SMS, "+436601289172");
});
}
[Fact]
public void AddInvalidAlertWithTypeSms()
public async Task AddInvalidAlertWithTypeTwitter()
{
Assert.Throws<UptimeSharpException>(() =>
await ThrowsAsync<UptimeSharpException>(async () =>
{
client.AddAlert(Models.AlertType.SMS, "+436601289172");
await client.AddAlert(Models.AlertType.Twitter, "artistandsocial");
});
}
[Fact]
public void AddInvalidAlertWithTypeTwitter()
{
Assert.Throws<UptimeSharpException>(() =>
{
client.AddAlert(Models.AlertType.Twitter, "artistandsocial");
});
}
[Fact]
public void AddAndRemoveAlerts()
public async Task AddAndRemoveAlerts()
{
string email = "example@ceecore.com";
Assert.True(client.AddAlert(AlertType.Email, email));
Assert.True(await client.AddAlert(AlertType.Email, email));
Alert origin = GetOriginAlert(email);
Alert origin = await GetOriginAlert(email);
Assert.Equal(email, origin.Value);
Assert.Equal(AlertType.Email, origin.Type);
client.DeleteAlert(origin);
await client.DeleteAlert(origin);
origin = GetOriginAlert(email);
origin = await GetOriginAlert(email);
Assert.Null(origin);
}
[Fact]
public void AddAndRetrieveSpecificAlerts()
public async Task AddAndRetrieveSpecificAlerts()
{
Assert.True(client.AddAlert(AlertType.Email, "example1@ceecore.com"));
Assert.True(client.AddAlert(AlertType.Boxcar, "example2@ceecore.com"));
Assert.True(await client.AddAlert(AlertType.Email, "example1@ceecore.com"));
Assert.True(await client.AddAlert(AlertType.Boxcar, "example2@ceecore.com"));
List<Alert> alerts = client.GetAlerts();
List<Alert> alerts = await client.GetAlerts();
Assert.InRange(alerts.ToArray().Length, 2, 100);
List<Alert> specificAlerts = client.GetAlerts(new string[] { alerts[0].ID, alerts[1].ID });
List<Alert> specificAlerts = await client.GetAlerts(new string[] { alerts[0].ID, alerts[1].ID });
Assert.Equal(2, specificAlerts.ToArray().Length);
}
private Alert GetOriginAlert(string value)
private async Task<Alert> GetOriginAlert(string value)
{
List<Alert> alerts = client.GetAlerts();
List<Alert> alerts = await client.GetAlerts();
Alert origin = null;
alerts.ForEach(alert =>
+2 -13
View File
@@ -4,20 +4,9 @@ using UptimeSharp.Models;
namespace UptimeSharp.Tests
{
public class ClientTest
public class ClientTest : TestsBase
{
UptimeClient client;
// this API key is associated with the test account uptimesharp@outlook.com
// please don't abuse it and create your own if you want to test the project!
string APIKey = "u97240-a24c634b3b84f1af602628e8";
// setup
public ClientTest()
{
client = new UptimeClient(APIKey);
}
public ClientTest() : base() { }
[Fact]
+22 -33
View File
@@ -2,37 +2,26 @@
using System;
using System.Collections.Generic;
using UptimeSharp.Models;
using System.Threading.Tasks;
namespace UptimeSharp.Tests
{
public class MonitorsTest : IDisposable
public class MonitorsTest : TestsBase
{
UptimeClient client;
// this API key is associated with the test account uptimesharp@outlook.com
// please don't abuse it and create your own if you want to test the project!
string APIKey = "u97240-a24c634b3b84f1af602628e8";
public MonitorsTest() : base() { }
// setup
public MonitorsTest()
public async Task Dispose()
{
client = new UptimeClient(APIKey);
}
// teardown
public void Dispose()
{
List<Monitor> monitors = client.GetMonitors();
monitors.ForEach(monitor => client.DeleteMonitor(monitor));
List<Monitor> monitors = await client.GetMonitors();
monitors.ForEach(item => monitorsToDelete.Add(item.ID));
}
[Fact]
public void AddHTTPMonitor()
public async Task AddHTTPMonitor()
{
Assert.True(client.AddMonitor(
Assert.True(await client.AddMonitor(
name: "test_1",
uri: "http://test1.com"
));
@@ -40,9 +29,9 @@ namespace UptimeSharp.Tests
[Fact]
public void AddKeywordMonitor()
public async Task AddKeywordMonitor()
{
Assert.True(client.AddMonitor(
Assert.True(await client.AddMonitor(
name: "test_2",
uri: "http://test2.com",
type: Models.Type.Keyword,
@@ -53,9 +42,9 @@ namespace UptimeSharp.Tests
[Fact]
public void AddPingMonitor()
public async Task AddPingMonitor()
{
Assert.True(client.AddMonitor(
Assert.True(await client.AddMonitor(
name: "test_3",
uri: "http://test3.com",
type: Models.Type.Ping
@@ -64,9 +53,9 @@ namespace UptimeSharp.Tests
[Fact]
public void AddPortMonitor()
public async Task AddPortMonitor()
{
Assert.True(client.AddMonitor(
Assert.True(await client.AddMonitor(
name: "test_4",
uri: "127.0.0.1",
type: Models.Type.Port,
@@ -77,16 +66,16 @@ namespace UptimeSharp.Tests
[Fact]
public void GetMonitors()
public async Task GetMonitors()
{
Assert.True(client.AddMonitor(
Assert.True(await client.AddMonitor(
name: "test_5",
uri: "255.0.0.1",
type: Models.Type.Port,
subtype: Subtype.HTTP
));
List<Monitor> items = client.GetMonitors();
List<Monitor> items = await client.GetMonitors();
Monitor monitor = null;
items.ForEach(item =>
@@ -106,14 +95,14 @@ namespace UptimeSharp.Tests
[Fact]
public void ModifyAMonitor()
public async Task ModifyAMonitor()
{
Assert.True(client.AddMonitor(
Assert.True(await client.AddMonitor(
name: "test_6",
uri: "http://test6.com"
));
List<Monitor> items = client.GetMonitors();
List<Monitor> items = await client.GetMonitors();
Monitor monitor = null;
items.ForEach(item =>
@@ -128,9 +117,9 @@ namespace UptimeSharp.Tests
monitor.Name = "updated_test_6";
Assert.True(client.ModifyMonitor(monitor));
Assert.True(await client.ModifyMonitor(monitor));
monitor = client.GetMonitor(monitor.ID);
monitor = await client.GetMonitor(monitor.ID);
Assert.Equal(monitor.Name, "updated_test_6");
}
+59
View File
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace UptimeSharp.Tests
{
public class TestsBase : IDisposable
{
protected UptimeClient client;
protected List<int> alertsToDelete = new List<int>();
protected List<int> monitorsToDelete = new List<int>();
// this API key is associated with the test account uptimesharp@outlook.com
// please don't abuse it and create your own if you want to test the project!
protected string APIKey = "u97240-a24c634b3b84f1af602628e8";
// setup
public TestsBase()
{
client = new UptimeClient(APIKey);
}
// teardown
public void Dispose()
{
alertsToDelete.ForEach(async id =>
{
await client.DeleteAlert(id);
});
monitorsToDelete.ForEach(async id =>
{
await client.DeleteMonitor(id);
});
}
// async throws
public static async Task ThrowsAsync<TException>(Func<Task> func)
{
var expected = typeof(TException);
Type actual = null;
try
{
await func();
}
catch (Exception e)
{
actual = e.GetType();
}
Assert.Equal(expected, actual);
}
}
}
@@ -54,6 +54,7 @@
<Compile Include="ClientTest.cs" />
<Compile Include="MonitorsTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestsBase.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
@@ -65,6 +66,11 @@
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.10\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.10\tools\Microsoft.Bcl.Build.targets')" />
<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.10\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
<Error Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.10\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
+3
View File
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Bcl" version="1.0.19" targetFramework="net40" />
<package id="Microsoft.Bcl.Async" version="1.0.16" targetFramework="net40" />
<package id="Microsoft.Bcl.Build" version="1.0.10" targetFramework="net40" />
<package id="RestSharp" version="104.1" targetFramework="net40" />
<package id="ServiceStack.Text" version="3.9.58" targetFramework="net40" />
<package id="xunit" version="1.9.2" targetFramework="net40" />