Skip to content
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

Merging Pull Request 204 to Master #207

Merged
merged 6 commits into from
Feb 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,5 @@ FakesAssemblies/
*.plg

# Visual Studio 6 workspace options file
*.opt
*.opt
/src/ZendeskApi_v2_Package/lib
16 changes: 8 additions & 8 deletions src/Tests/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
namespace Tests {
public class Settings
public class Settings
{

public const string Site = "https://csharpapi.zendesk.com/api/v2/";
public const string DefaultOrg = "csharpapi";
public const string Email = "eric.neifert@gmail.com";
public const string Password = "pa55word";
public const string ApiToken = "R5PXRh5hEwT9Ry3hfSzGF2WKsxPf3ScSCq0suxii";
public const string Phone = "1234567890";
public const string FormattedPhone = "(111) 222-3333";
public const string BadPhone = "0987654321";
public const string Phone = "1234567890";
public const string FormattedPhone = "(111) 222-3333";
public const string BadPhone = "0987654321";

public const string FieldKey = "test";
public const string FieldValue = "0897c9c1f80646118a8194c942aa84cf";
public const string BadFieldValue = "BAD";
public const string FieldKey = "test";
public const string FieldValue = "0897c9c1f80646118a8194c942aa84cf";
public const string BadFieldValue = "BAD";

public const long UserId = 281513402;
public const long UserId = 281513402;
public const long SampleTicketId = 1364;
public const long SampleTicketId2 = 1366;

Expand Down
55 changes: 49 additions & 6 deletions src/Tests/TicketTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using ZendeskApi_v2;
using ZendeskApi_v2.Extensions;
using ZendeskApi_v2.Models.Constants;
Expand Down Expand Up @@ -700,10 +698,55 @@ public void CanGetAuditsAndMarkAsTrusted()
}

[Test]
public void CanGetInrementalTicketExport()
public void CanGetInrementalTicketExportTestOnly()
{
var res = api.Tickets.__TestOnly__GetIncrementalTicketExport(DateTime.Now.AddDays(-1));
Assert.True(res.Results.Count > 0);
Assert.True(res.Count > 0);
}

[Test]
public void CanGetIncrementalTicketExportPaged()
{
const int maxTicketsPerPage = 1000;

var res = api.Tickets.GetIncrementalTicketExport(DateTime.Now.AddDays(-5));

Assert.AreEqual(maxTicketsPerPage, res.Tickets.Count);
Assert.IsNotNullOrEmpty(res.NextPage);
}

[Test]
public void CanGetIncrementalTicketExportWithUsersSideLoadPaged()
{
const int maxTicketsPerPage = 1000;

var res = api.Tickets.GetIncrementalTicketExport(DateTime.Now.AddDays(-5), TicketSideLoadOptionsEnum.Users);

Assert.AreEqual(maxTicketsPerPage, res.Tickets.Count);
Assert.IsTrue(res.Users.Count > 0);
Assert.IsNotNullOrEmpty(res.NextPage);

res = this.api.Tickets.GetIncrementalTicketExportNextPage(res.NextPage);

Assert.IsTrue(res.Tickets.Count > 0);
Assert.IsTrue(res.Users.Count > 0);
}

[Test]
public void CanGetIncrementalTicketExportWithGroupsSideLoadPaged()
{
const int maxTicketsPerPage = 1000;

var res = api.Tickets.GetIncrementalTicketExport(DateTime.Now.AddDays(-5), TicketSideLoadOptionsEnum.Groups);

Assert.AreEqual(maxTicketsPerPage, res.Tickets.Count);
Assert.IsTrue(res.Groups.Count > 0);
Assert.IsNotNullOrEmpty(res.NextPage);

res = this.api.Tickets.GetIncrementalTicketExportNextPage(res.NextPage);

Assert.IsTrue(res.Tickets.Count > 0);
Assert.IsTrue(res.Groups.Count > 0);
}

[Test]
Expand Down
282 changes: 282 additions & 0 deletions src/Tests/testing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
// ***********************************************************************
// Copyright (c) 2014 Charlie Poole
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using System;
using System.Threading.Tasks;
using NUnit.Framework.Constraints;


namespace NUnit.Framework
{
public delegate System.Threading.Tasks.Task AsyncTestDelegate();

public class AssertAsync
{
#region ThrowsAsync

/// <summary>
/// Verifies that an async delegate throws a particular exception when called.
/// </summary>
/// <param name="expression">A constraint to be satisfied by the exception</param>
/// <param name="code">A TestSnippet delegate</param>
/// <param name="message">The message that will be displayed on failure</param>
/// <param name="args">Arguments to be used in formatting the message</param>
public static Task<Exception> ThrowsAsync(IResolveConstraint expression, AsyncTestDelegate code, string message, params object[] args)
{
Exception caughtException;
try
{
var task = code();
return task.ContinueWith(t =>
{
// Case where code() either
// - succeeded (synchronously or asynchronously)
// - failed asynchronously
var ex = t.Exception?.InnerException;
Assert.That(ex, expression, message, args);
return ex;
});
}
// Case where code() failed synchronously
catch (Exception ex)
{
caughtException = ex;
}

Assert.That(caughtException, expression, message, args);

return TaskFromResult(caughtException);
}

/// <summary>
/// Verifies that an async delegate throws a particular exception when called.
/// </summary>
/// <param name="expression">A constraint to be satisfied by the exception</param>
/// <param name="code">A TestSnippet delegate</param>
public static Task<Exception> ThrowsAsync(IResolveConstraint expression, AsyncTestDelegate code)
{
return ThrowsAsync(expression, code, string.Empty, null);
}

/// <summary>
/// Verifies that an async delegate throws a particular exception when called.
/// </summary>
/// <param name="expectedExceptionType">The exception Type expected</param>
/// <param name="code">A TestDelegate</param>
/// <param name="message">The message that will be displayed on failure</param>
/// <param name="args">Arguments to be used in formatting the message</param>
public static Task<Exception> ThrowsAsync(Type expectedExceptionType, AsyncTestDelegate code, string message, params object[] args)
{
return ThrowsAsync(new ExceptionTypeConstraint(expectedExceptionType), code, message, args);
}

/// <summary>
/// Verifies that an async delegate throws a particular exception when called.
/// </summary>
/// <param name="expectedExceptionType">The exception Type expected</param>
/// <param name="code">A TestDelegate</param>
public static Task<Exception> ThrowsAsync(Type expectedExceptionType, AsyncTestDelegate code)
{
return ThrowsAsync(new ExceptionTypeConstraint(expectedExceptionType), code, string.Empty, null);
}

#endregion

#region ThrowsAsync<TActual>

/// <summary>
/// Verifies that an async delegate throws a particular exception when called.
/// </summary>
/// <typeparam name="TActual">Type of the expected exception</typeparam>
/// <param name="code">A TestDelegate</param>
/// <param name="message">The message that will be displayed on failure</param>
/// <param name="args">Arguments to be used in formatting the message</param>
public static Task<TActual> ThrowsAsync<TActual>(AsyncTestDelegate code, string message, params object[] args) where TActual : Exception
{
return Cast<TActual>(ThrowsAsync(typeof(TActual), code, message, args));
}

/// <summary>
/// Verifies that an async delegate throws a particular exception when called.
/// </summary>
/// <typeparam name="TActual">Type of the expected exception</typeparam>
/// <param name="code">A TestDelegate</param>
public static Task<TActual> ThrowsAsync<TActual>(AsyncTestDelegate code) where TActual : Exception
{
return ThrowsAsync<TActual>(code, string.Empty, null);
}

#endregion

#region CatchAsync

/// <summary>
/// Verifies that an async delegate throws an exception when called
/// and returns it.
/// </summary>
/// <param name="code">A TestDelegate</param>
/// <param name="message">The message that will be displayed on failure</param>
/// <param name="args">Arguments to be used in formatting the message</param>
public static Task<Exception> CatchAsync(AsyncTestDelegate code, string message, params object[] args)
{
return ThrowsAsync(new InstanceOfTypeConstraint(typeof(Exception)), code, message, args);
}

/// <summary>
/// Verifies that an async delegate throws an exception when called
/// and returns it.
/// </summary>
/// <param name="code">A TestDelegate</param>
public static Task<Exception> CatchAsync(AsyncTestDelegate code)
{
return ThrowsAsync(new InstanceOfTypeConstraint(typeof(Exception)), code);
}

/// <summary>
/// Verifies that an async delegate throws an exception of a certain Type
/// or one derived from it when called and returns it.
/// </summary>
/// <param name="expectedExceptionType">The expected Exception Type</param>
/// <param name="code">A TestDelegate</param>
/// <param name="message">The message that will be displayed on failure</param>
/// <param name="args">Arguments to be used in formatting the message</param>
public static Task<Exception> CatchAsync(Type expectedExceptionType, AsyncTestDelegate code, string message, params object[] args)
{
return ThrowsAsync(new InstanceOfTypeConstraint(expectedExceptionType), code, message, args);
}

/// <summary>
/// Verifies that an async delegate throws an exception of a certain Type
/// or one derived from it when called and returns it.
/// </summary>
/// <param name="expectedExceptionType">The expected Exception Type</param>
/// <param name="code">A TestDelegate</param>
public static Task<Exception> CatchAsync(Type expectedExceptionType, AsyncTestDelegate code)
{
return ThrowsAsync(new InstanceOfTypeConstraint(expectedExceptionType), code);
}

#endregion

#region CatchAsync<TActual>

/// <summary>
/// Verifies that an async delegate throws an exception of a certain Type
/// or one derived from it when called and returns it.
/// </summary>
/// <param name="code">A TestDelegate</param>
/// <param name="message">The message that will be displayed on failure</param>
/// <param name="args">Arguments to be used in formatting the message</param>
public static Task<TActual> CatchAsync<TActual>(AsyncTestDelegate code, string message, params object[] args) where TActual : System.Exception
{
return Cast<TActual>(ThrowsAsync(new InstanceOfTypeConstraint(typeof(TActual)), code, message, args));
}

/// <summary>
/// Verifies that an async delegate throws an exception of a certain Type
/// or one derived from it when called and returns it.
/// </summary>
/// <param name="code">A TestDelegate</param>
public static Task<TActual> CatchAsync<TActual>(AsyncTestDelegate code) where TActual : System.Exception
{
return Cast<TActual>(ThrowsAsync(new InstanceOfTypeConstraint(typeof(TActual)), code));
}

#endregion

#region DoesNotThrowAsync

/// <summary>
/// Verifies that an async delegate does not throw an exception
/// </summary>
/// <param name="code">A TestDelegate</param>
/// <param name="message">The message that will be displayed on failure</param>
/// <param name="args">Arguments to be used in formatting the message</param>
public static Task DoesNotThrowAsync(AsyncTestDelegate code, string message, params object[] args)
{
Exception caughtException;
try
{
var task = code();
return task.ContinueWith(t =>
{
// Case where code() either
// - succeeded (synchronously or asynchronously)
// - failed asynchronously
var ex = t.Exception?.InnerException;
Assert.That(ex, new ThrowsNothingConstraint(), message, args);
return ex;
});
}
// Case where code() failed synchronously
catch (Exception ex)
{
caughtException = ex;
}

Assert.That(caughtException, new ThrowsNothingConstraint(), message, args);

return TaskFromResult(caughtException);
}
/// <summary>
/// Verifies that an async delegate does not throw an exception.
/// </summary>
/// <param name="code">A TestDelegate</param>
public static Task DoesNotThrowAsync(AsyncTestDelegate code)
{
return DoesNotThrowAsync(code, string.Empty, null);
}

#endregion

#region Helpers

private static Task<T> TaskFromResult<T>(T value)
{
#if NET_4_5 || PORTABLE

return Task.FromResult(value);

#else

var tcs = new TaskCompletionSource<T>();
tcs.SetResult(value);
return tcs.Task;

#endif
}

private static Task<TActual> Cast<TActual>(Task<Exception> task) where TActual : Exception
{
return task
.ContinueWith(t =>
{
if (t.IsFaulted)
throw t.Exception.InnerException;
return (TActual)t.Result;
});
}

#endregion
}
}
Loading