Skip to content

Commit

Permalink
[chore] Linting + docstrings (#539)
Browse files Browse the repository at this point in the history
- Added docstrings
- Fix style issues
- Update examples submodule
- Use switch case for SmartRateAccuracy
- Re-record cassettes as needed
  • Loading branch information
nwithan8 committed Jan 2, 2024
1 parent ec528c4 commit 8ffd509
Show file tree
Hide file tree
Showing 209 changed files with 4,364 additions and 2,921 deletions.
1 change: 1 addition & 0 deletions EasyPost.Integration/Basics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Basics
[Fact, Testing.Access, Testing.Compile]
public void UserCanLocallyConstructResponseObject()
{
// ReSharper disable once UnusedVariable
var address = new Address();
var addressCollection = new AddressCollection();
var apiKey = new ApiKey();
Expand Down
1 change: 1 addition & 0 deletions EasyPost.Integration/Synchronous.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace EasyPost.Integration;

public class Synchronous
{
// ReSharper disable once RedundantArgumentDefaultValue
private Utils.VCR Vcr { get; } = new("synchronous", Utils.ApiKey.Test);

/// <summary>
Expand Down
26 changes: 15 additions & 11 deletions EasyPost.Integration/TestUtils.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ReSharper disable once RedundantUsingDirective
using System.Net.Http;
using System.Runtime.CompilerServices;
using EasyVCR;
Expand All @@ -9,8 +10,8 @@ public class Utils
{
internal const string ApiKeyFailedToPull = "couldnotpullapikey";

private static readonly List<string> BodyCensors = new()
{
private static readonly List<string> BodyCensors =
[
"api_keys",
"children",
"client_ip",
Expand All @@ -20,20 +21,20 @@ public class Utils
"keys",
"phone_number",
"phone",
"test_credentials",
};
"test_credentials"
];

private static readonly List<string> HeaderCensors = new()
{
private static readonly List<string> HeaderCensors =
[
"Authorization",
"User-Agent",
};
"User-Agent"
];

private static readonly List<string> QueryCensors = new()
{
private static readonly List<string> QueryCensors =
[
"card[number]",
"card[cvc]"
};
];

public enum ApiKey
{
Expand Down Expand Up @@ -79,6 +80,8 @@ internal static string NetVersion
{
get
{
// ReSharper disable once RedundantAssignment
// ReSharper disable once ConvertToConstant.Local
string netVersion = "net";
#if NET462
netVersion = "netstandard";
Expand All @@ -88,6 +91,7 @@ internal static string NetVersion
}
}

// ReSharper disable once InconsistentNaming
public class VCR
{
// Cassettes folder will always been in the same directory as this TestUtils.cs file
Expand Down
12 changes: 3 additions & 9 deletions EasyPost.Integration/Utilities/Attributes/TestType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,18 @@ internal static class Testing
/// Marks an integration test that is testing access levels (test that users can/cannot access a function or property as expected)
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
internal sealed class Access : BaseCustomAttribute
{
}
internal sealed class Access : BaseCustomAttribute;

/// <summary>
/// Marks an integration test that is testing compile-time behavior (test that code as written will compile)
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
internal sealed class Compile : BaseCustomAttribute
{
}
internal sealed class Compile : BaseCustomAttribute;

/// <summary>
/// Marks an integration test that is testing run-time behavior (test that code as written will run)
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
internal sealed class Run : BaseCustomAttribute
{
}
internal sealed class Run : BaseCustomAttribute;
}
}
26 changes: 14 additions & 12 deletions EasyPost.Tests/ClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using EasyPost._base;
using EasyPost.Exceptions.API;
using EasyPost.Tests._Utilities;
using EasyPost.Tests._Utilities.Attributes;
Expand Down Expand Up @@ -50,8 +51,6 @@ public void TestThreadSafety()
Client client2 = new(new ClientConfiguration(key2));
Client client3 = new(new ClientConfiguration(key3));

static void Thread(Client client, string keyToMatch) => Assert.Equal(keyToMatch, client.ApiKeyInUse);

Thread thread1 = new(() => Thread(client1, key1));
Thread thread2 = new(() => Thread(client2, key2));
Thread thread3 = new(() => Thread(client3, key3));
Expand All @@ -60,6 +59,9 @@ public void TestThreadSafety()
thread2.Start();
thread3.Start();
thread1.Start();
return;

static void Thread(EasyPostClient client, string keyToMatch) => Assert.Equal(keyToMatch, client.ApiKeyInUse);
}

#endregion
Expand Down Expand Up @@ -116,14 +118,14 @@ public async Task TestRequestHooks()

Hooks hooks = new()
{
OnRequestExecuting = (sender, args) =>
OnRequestExecuting = (_, args) =>
{
// Modifying the HttpRequestMessage in this action does not impact the HttpRequestMessage being executed (passed by value, not reference)
preRequestCallbackCallCount++;
Assert.True(args.RequestTimestamp > 0);
requestGuid = args.Id;
},
OnRequestResponseReceived = (sender, args) =>
OnRequestResponseReceived = (_, args) =>
{
postRequestCallbackCallCount++;
Assert.True(args.RequestTimestamp > 0);
Expand All @@ -133,7 +135,7 @@ public async Task TestRequestHooks()
},
};

UseVCRWithCustomClient("request_hooks", (apiKey, httpClient) =>
UseVCRWithCustomClient("request_hooks", (_, httpClient) =>
new Client(new ClientConfiguration(FakeApikey)
{
CustomHttpClient = httpClient,
Expand All @@ -160,12 +162,12 @@ public async Task TestMultipleRequestHookCallbacks()
bool postRequestCallback2Called = false;

Hooks hooks = new();
hooks.OnRequestExecuting += (sender, args) => preRequestCallback1Called = true;
hooks.OnRequestExecuting += (sender, args) => preRequestCallback2Called = true;
hooks.OnRequestResponseReceived += (sender, args) => postRequestCallback1Called = true;
hooks.OnRequestResponseReceived += (sender, args) => postRequestCallback2Called = true;
hooks.OnRequestExecuting += (_, _) => preRequestCallback1Called = true;
hooks.OnRequestExecuting += (_, _) => preRequestCallback2Called = true;
hooks.OnRequestResponseReceived += (_, _) => postRequestCallback1Called = true;
hooks.OnRequestResponseReceived += (_, _) => postRequestCallback2Called = true;

UseVCRWithCustomClient("multiple_request_hooks", (apiKey, httpClient) =>
UseVCRWithCustomClient("multiple_request_hooks", (_, httpClient) =>
new Client(new ClientConfiguration(FakeApikey)
{
CustomHttpClient = httpClient,
Expand Down Expand Up @@ -231,14 +233,14 @@ public async Task TestCancellationToken()

Hooks hooks = new()
{
OnRequestExecuting = (sender, args) =>
OnRequestExecuting = (_, _) =>
{
// Use the cancellation token to cancel the request
cancelTokenSource.Cancel();
},
};

UseVCRWithCustomClient("cancellation_token", (apiKey, httpClient) =>
UseVCRWithCustomClient("cancellation_token", (_, httpClient) =>
new Client(new ClientConfiguration(FakeApikey)
{
CustomHttpClient = httpClient,
Expand Down
11 changes: 8 additions & 3 deletions EasyPost.Tests/ExceptionsTests/ExceptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ public async Task TestApiExceptionPrettyPrint()

// Now test with some error-related JSON inside the response with sub-errors
errorMessageStringJson = "{\"error\": {\"code\": \"ERROR_CODE\", \"message\": \"ERROR_MESSAGE\", \"errors\": [{\"field\": \"SUB_ERROR_FIELD\", \"message\": \"SUB_ERROR_MESSAGE\"}]}}";
List<Error> subErrors = new()
{
List<Error> subErrors =
[
new Error
{
Field = "SUB_ERROR_FIELD",
RawMessage = "SUB_ERROR_MESSAGE"
}
};
];

// Generate a dummy HttpResponseMessage with the given status code to parse
httpStatusCode = (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), statusCode.ToString(CultureInfo.InvariantCulture));
Expand Down Expand Up @@ -365,6 +365,7 @@ public async Task TestKnownApiExceptionGeneration()

[Fact]
[Testing.Exception]
// ReSharper disable once InconsistentNaming
public async Task TestUnknownApiException1xxGeneration()
{
// library does not have a specific exception for this status code
Expand All @@ -385,6 +386,7 @@ public async Task TestUnknownApiException1xxGeneration()

[Fact]
[Testing.Exception]
// ReSharper disable once InconsistentNaming
public async Task TestUnknownApiException3xxGeneration()
{
// library does not have a specific exception for this status code
Expand All @@ -405,6 +407,7 @@ public async Task TestUnknownApiException3xxGeneration()

[Fact]
[Testing.Exception]
// ReSharper disable once InconsistentNaming
public async Task TestUnknownApiException4xxGeneration()
{
// library does not have a specific exception for this status code
Expand All @@ -425,6 +428,7 @@ public async Task TestUnknownApiException4xxGeneration()

[Fact]
[Testing.Exception]
// ReSharper disable once InconsistentNaming
public async Task TestUnknownApiException5xxGeneration()
{
// library does not have a specific exception for this status code
Expand All @@ -445,6 +449,7 @@ public async Task TestUnknownApiException5xxGeneration()

[Fact]
[Testing.Exception]
// ReSharper disable once InconsistentNaming
public async Task TestHTTPTimeoutFriendlyException()
{
// create a new client with a very short timeout
Expand Down
4 changes: 2 additions & 2 deletions EasyPost.Tests/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static byte[] EventBody
{
Dictionary<string, object> fixture = GetFixtureStructure().Pickups.Basic;

const string pickupDate = "2023-12-01";
const string pickupDate = "2023-12-25";

fixture!.AddOrUpdate("min_datetime", pickupDate);
fixture!.AddOrUpdate("max_datetime", pickupDate);
Expand Down Expand Up @@ -89,7 +89,7 @@ public static byte[] EventBody

internal static string PickupService => GetFixtureStructure().ServiceNames.Usps.PickupService;

internal static string PlannedShipDate => "2023-12-01";
internal static string PlannedShipDate => "2023-12-25";

internal static Dictionary<string, object> ReferralCustomer => GetFixtureStructure().Users.Referral;

Expand Down
6 changes: 1 addition & 5 deletions EasyPost.Tests/ModelsTests/TrackerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@

namespace EasyPost.Tests.ModelsTests
{
public class TrackerTests : UnitTest
public class TrackerTests() : UnitTest("tracker")
{
public TrackerTests() : base("tracker")
{
}

[Fact]
[Testing.Properties]
#pragma warning disable CS1998
Expand Down
8 changes: 8 additions & 0 deletions EasyPost.Tests/ParametersTests/ParametersTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using EasyPost._base;
using EasyPost.Models.API;
Expand Down Expand Up @@ -227,27 +228,33 @@ public void TestRequiredAndOptionalParameterValidation()
private sealed class ParameterSetWithRequiredAndOptionalParameters : Parameters.BaseParameters<EasyPostObject>
{
[TopLevelRequestParameter(Necessity.Required, "test", "required")]
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public string? RequiredParameter { get; set; }

[TopLevelRequestParameter(Necessity.Optional, "test", "optional")]
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public string? OptionalParameter { get; set; }
}

private sealed class ParameterSetWithCompetingParameters : Parameters.BaseParameters<EasyPostObject>
{
[TopLevelRequestParameter(Necessity.Optional, "location")]
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public string? AParam { get; set; }

[TopLevelRequestParameter(Necessity.Optional, "location")]
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public string? BParam { get; set; }
}

private sealed class ParameterSetWithCompetingParametersNonAlphabetic : Parameters.BaseParameters<EasyPostObject>
{
[TopLevelRequestParameter(Necessity.Optional, "location")]
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public string? BParam { get; set; }

[TopLevelRequestParameter(Necessity.Optional, "location")]
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public string? AParam { get; set; }
}

Expand Down Expand Up @@ -420,6 +427,7 @@ public void TestParameterMatchOverrideFunction()
}

#pragma warning disable CA1852 // Can be sealed
[SuppressMessage("ReSharper", "UnusedMember.Local")]
internal class ExampleDecoratorParameters : Parameters.BaseParameters<EasyPostObject>
{
// Default values set to guarantee any property won't be skipped for serialization due to a null value
Expand Down

0 comments on commit 8ffd509

Please sign in to comment.