mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-04 11:24:44 +00:00
Replace ToRequestParameters implementations.
This commit is contained in:
@@ -46,7 +46,7 @@ public class IssuesClientTests
|
||||
client.GetAllForCurrent();
|
||||
|
||||
connection.Received().GetAll<Issue>(Arg.Is<Uri>(u => u.ToString() == "/issues"),
|
||||
Args.EmptyDictionary);
|
||||
Arg.Any<Dictionary<string, string>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -58,7 +58,11 @@ public class IssuesClientTests
|
||||
client.GetAllForCurrent(new IssueRequest { SortDirection = SortDirection.Ascending });
|
||||
|
||||
connection.Received().GetAll<Issue>(Arg.Is<Uri>(u => u.ToString() == "/issues"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["direction"] == "asc" && d.Count == 1));
|
||||
Arg.Is<Dictionary<string, string>>(d => d.Count == 4
|
||||
&& d["filter"] == "assigned"
|
||||
&& d["sort"] == "created"
|
||||
&& d["state"] == "open"
|
||||
&& d["direction"] == "asc"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +77,7 @@ public class IssuesClientTests
|
||||
client.GetAllForOwnedAndMemberRepositories();
|
||||
|
||||
connection.Received().GetAll<Issue>(Arg.Is<Uri>(u => u.ToString() == "/user/issues"),
|
||||
Args.EmptyDictionary);
|
||||
Arg.Any<Dictionary<string, string>>());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,7 +92,7 @@ public class IssuesClientTests
|
||||
client.GetForRepository("fake", "repo");
|
||||
|
||||
connection.Received().GetAll<Issue>(Arg.Is<Uri>(u => u.ToString() == "/repos/fake/repo/issues"),
|
||||
Args.EmptyDictionary);
|
||||
Arg.Any<Dictionary<string, string>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -103,7 +107,11 @@ public class IssuesClientTests
|
||||
});
|
||||
|
||||
connection.Received().GetAll<Issue>(Arg.Is<Uri>(u => u.ToString() == "/repos/fake/repo/issues"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["direction"] == "asc" && d.Count == 1));
|
||||
Arg.Is<Dictionary<string, string>>(d => d.Count == 4
|
||||
&& d["state"] == "open"
|
||||
&& d["direction"] == "asc"
|
||||
&& d["sort"] == "created"
|
||||
&& d["filter"] == "assigned"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using NSubstitute;
|
||||
using Octokit;
|
||||
using Octokit.Tests;
|
||||
using Octokit.Tests.Helpers;
|
||||
using Xunit;
|
||||
|
||||
@@ -46,7 +45,7 @@ public class MilestonesClientTests
|
||||
await client.GetForRepository("fake", "repo");
|
||||
|
||||
connection.Received().GetAll<Milestone>(Arg.Is<Uri>(u => u.ToString() == "/repos/fake/repo/milestones"),
|
||||
Args.EmptyDictionary);
|
||||
Arg.Any<Dictionary<string, string>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -58,7 +57,10 @@ public class MilestonesClientTests
|
||||
client.GetForRepository("fake", "repo", new MilestoneRequest { SortDirection = SortDirection.Descending });
|
||||
|
||||
connection.Received().GetAll<Milestone>(Arg.Is<Uri>(u => u.ToString() == "/repos/fake/repo/milestones"),
|
||||
Arg.Is<Dictionary<string, string>>(d => d["direction"] == "desc" && d.Count == 1));
|
||||
Arg.Is<Dictionary<string, string>>(d => d.Count == 3
|
||||
&& d["direction"] == "desc"
|
||||
&& d["state"] == "open"
|
||||
&& d["sort"] == "due_date"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,17 +7,6 @@ public class IssueRequestTests
|
||||
{
|
||||
public class TheToParametersDictionaryMethod
|
||||
{
|
||||
[Fact]
|
||||
public void OnlyContainsChangedValues()
|
||||
{
|
||||
var request = new IssueRequest { SortDirection = SortDirection.Ascending };
|
||||
|
||||
var parameters = request.ToParametersDictionary();
|
||||
|
||||
Assert.Equal(1, parameters.Count);
|
||||
Assert.Equal("asc", parameters["direction"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ContainsSetValues()
|
||||
{
|
||||
@@ -44,13 +33,16 @@ public class IssueRequestTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReturnsEmptyDictionaryForDefaultRequest()
|
||||
public void ReturnsDictionaryOfDefaultValues()
|
||||
{
|
||||
var request = new IssueRequest();
|
||||
|
||||
var parameters = request.ToParametersDictionary();
|
||||
|
||||
Assert.Empty(parameters);
|
||||
Assert.Equal("assigned", parameters["filter"]);
|
||||
Assert.Equal("open", parameters["state"]);
|
||||
Assert.Equal("created", parameters["sort"]);
|
||||
Assert.Equal("desc", parameters["direction"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,17 +5,6 @@ public class MilestoneRequestTests
|
||||
{
|
||||
public class TheToParametersDictionaryMethod
|
||||
{
|
||||
[Fact]
|
||||
public void OnlyContainsChangedValues()
|
||||
{
|
||||
var request = new MilestoneRequest { SortDirection = SortDirection.Descending };
|
||||
|
||||
var parameters = request.ToParametersDictionary();
|
||||
|
||||
Assert.Equal(1, parameters.Count);
|
||||
Assert.Equal("desc", parameters["direction"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ContainsSetValues()
|
||||
{
|
||||
@@ -34,26 +23,15 @@ public class MilestoneRequestTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DoesNotAddDefaultAscendingSort()
|
||||
{
|
||||
var request = new MilestoneRequest
|
||||
{
|
||||
SortDirection = SortDirection.Ascending,
|
||||
};
|
||||
|
||||
var parameters = request.ToParametersDictionary();
|
||||
|
||||
Assert.Empty(parameters);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReturnsEmptyDictionaryForDefaultRequest()
|
||||
public void ReturnsDefaultValuesForDefaultRequest()
|
||||
{
|
||||
var request = new MilestoneRequest();
|
||||
|
||||
var parameters = request.ToParametersDictionary();
|
||||
|
||||
Assert.Empty(parameters);
|
||||
Assert.Equal("open", parameters["state"]);
|
||||
Assert.Equal("due_date", parameters["sort"]);
|
||||
Assert.Equal("asc", parameters["direction"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using Octokit.Internal;
|
||||
|
||||
namespace Octokit
|
||||
{
|
||||
public class IssueRequest
|
||||
public class IssueRequest : RequestParameters
|
||||
{
|
||||
static readonly IssueRequest _defaultParameterValues = new IssueRequest();
|
||||
public IssueRequest()
|
||||
{
|
||||
Filter = IssueFilter.Assigned;
|
||||
@@ -21,54 +18,11 @@ namespace Octokit
|
||||
public IssueFilter Filter { get; set; }
|
||||
public ItemState State { get; set; }
|
||||
public Collection<string> Labels { get; private set; }
|
||||
[Parameter(Key = "sort")]
|
||||
public IssueSort SortProperty { get; set; }
|
||||
[Parameter(Key = "direction")]
|
||||
public SortDirection SortDirection { get; set; }
|
||||
public DateTimeOffset? Since { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a dictionary of query string parameters that represent this request. Only values that
|
||||
/// do not have default values are in the dictionary. If everything is default, this returns an
|
||||
/// empty dictionary.
|
||||
/// </summary>
|
||||
[SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase",
|
||||
Justification = "The API expects lowercase")]
|
||||
public virtual IDictionary<string, string> ToParametersDictionary()
|
||||
{
|
||||
var parameters = new Dictionary<string, string>();
|
||||
|
||||
if (Filter != _defaultParameterValues.Filter)
|
||||
{
|
||||
var filter = Enum.GetName(typeof(IssueFilter), Filter) ?? "filter";
|
||||
parameters.Add("filter", filter.ToLowerInvariant());
|
||||
}
|
||||
|
||||
if (State != _defaultParameterValues.State)
|
||||
{
|
||||
parameters.Add("state", "closed");
|
||||
}
|
||||
|
||||
if (SortProperty != _defaultParameterValues.SortProperty)
|
||||
{
|
||||
var sort = Enum.GetName(typeof(IssueSort), SortProperty) ?? "created";
|
||||
parameters.Add("sort", sort.ToLowerInvariant());
|
||||
}
|
||||
|
||||
if (SortDirection != _defaultParameterValues.SortDirection)
|
||||
{
|
||||
parameters.Add("direction", "asc");
|
||||
}
|
||||
|
||||
if (Since != null)
|
||||
{
|
||||
parameters.Add("since", Since.Value.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture));
|
||||
}
|
||||
if (Labels.Count > 0)
|
||||
{
|
||||
parameters.Add("labels", String.Join(",", Labels));
|
||||
}
|
||||
|
||||
return parameters;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -78,7 +32,7 @@ namespace Octokit
|
||||
public enum IssueFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// Issues assigned to the authenticated user.
|
||||
/// Issues assigned to the authenticated user. (Default)
|
||||
/// </summary>
|
||||
Assigned,
|
||||
|
||||
@@ -96,7 +50,7 @@ namespace Octokit
|
||||
/// Issues the authenticated user is subscribed to for updates.
|
||||
/// </summary>
|
||||
Subscribed,
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// All issues the authenticated user can see, regardless of participation or creation.
|
||||
/// </summary>
|
||||
@@ -118,14 +72,28 @@ namespace Octokit
|
||||
|
||||
public enum IssueSort
|
||||
{
|
||||
/// <summary>
|
||||
/// Sort by create date (default)
|
||||
/// </summary>
|
||||
Created,
|
||||
|
||||
/// <summary>
|
||||
/// Sort by the date of the last update
|
||||
/// </summary>
|
||||
Updated,
|
||||
|
||||
/// <summary>
|
||||
/// Sort by the number of comments
|
||||
/// </summary>
|
||||
Comments
|
||||
}
|
||||
|
||||
public enum SortDirection
|
||||
{
|
||||
[Parameter(Value = "asc")]
|
||||
Ascending,
|
||||
|
||||
[Parameter(Value = "desc")]
|
||||
Descending
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Octokit.Internal;
|
||||
|
||||
namespace Octokit
|
||||
{
|
||||
public class MilestoneRequest
|
||||
public class MilestoneRequest : RequestParameters
|
||||
{
|
||||
static readonly MilestoneRequest _defaultParameterValues = new MilestoneRequest();
|
||||
|
||||
public MilestoneRequest()
|
||||
{
|
||||
State = ItemState.Open;
|
||||
@@ -16,41 +12,17 @@ namespace Octokit
|
||||
}
|
||||
|
||||
public ItemState State { get; set; }
|
||||
|
||||
[Parameter(Key = "sort")]
|
||||
public MilestoneSort SortProperty { get; set; }
|
||||
|
||||
[Parameter(Key = "direction")]
|
||||
public SortDirection SortDirection { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a dictionary of query string parameters that represent this request. Only values that
|
||||
/// do not have default values are in the dictionary. If everything is default, this returns an
|
||||
/// empty dictionary.
|
||||
/// </summary>
|
||||
[SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase",
|
||||
Justification = "The API expects lowercase")]
|
||||
public virtual IDictionary<string, string> ToParametersDictionary()
|
||||
{
|
||||
var parameters = new Dictionary<string, string>();
|
||||
|
||||
if (State != _defaultParameterValues.State)
|
||||
{
|
||||
parameters.Add("state", "closed");
|
||||
}
|
||||
|
||||
if (SortProperty != _defaultParameterValues.SortProperty)
|
||||
{
|
||||
parameters.Add("sort", "completeness");
|
||||
}
|
||||
|
||||
if (SortDirection != _defaultParameterValues.SortDirection)
|
||||
{
|
||||
parameters.Add("direction", "desc");
|
||||
}
|
||||
|
||||
return parameters;
|
||||
}
|
||||
}
|
||||
|
||||
public enum MilestoneSort
|
||||
{
|
||||
[Parameter(Value = "due_date")]
|
||||
DueDate,
|
||||
Completeness
|
||||
}
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Octokit
|
||||
namespace Octokit
|
||||
{
|
||||
public class RepositoryIssueRequest : IssueRequest
|
||||
{
|
||||
@@ -10,22 +7,5 @@ namespace Octokit
|
||||
/// Use the milestone number for a specific milestone. Use the value "none" for issues with any milestones.
|
||||
/// </summary>
|
||||
public string Milestone { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a dictionary of query string parameters that represent this request. Only values that
|
||||
/// do not have default values are in the dictionary. If everything is default, this returns an
|
||||
/// empty dictionary.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override IDictionary<string, string> ToParametersDictionary()
|
||||
{
|
||||
var dictionary = base.ToParametersDictionary();
|
||||
Debug.Assert(dictionary != null, "Base implementation is wrong. Dictionary should never be null");
|
||||
if (!Milestone.IsBlank())
|
||||
{
|
||||
dictionary.Add("milestone", Milestone);
|
||||
}
|
||||
return dictionary;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,12 +21,11 @@ namespace Octokit
|
||||
{
|
||||
var properties = _propertiesMap.GetOrAdd(GetType(), GetPropertiesForType);
|
||||
|
||||
var dict = (from property in properties
|
||||
return (from property in properties
|
||||
let value = GetValue(property)
|
||||
let key = GetKey(property)
|
||||
where value != null
|
||||
select new { key, value }).ToDictionary(kvp => kvp.key, kvp => kvp.value);
|
||||
return dict;
|
||||
}
|
||||
|
||||
static List<PropertyInfo> GetPropertiesForType(Type type)
|
||||
|
||||
Reference in New Issue
Block a user