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

[chore] Add unit test verifying how competing/overriding parameters work #518

Merged
merged 1 commit into from
Sep 13, 2023
Merged
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
69 changes: 69 additions & 0 deletions EasyPost.Tests/ParametersTests/ParametersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,57 @@ public void TestReusingParameterSets()
Assert.False(parametersDictionary.ContainsKey("before_id"));
}

[Fact]
[Testing.Logic]
public void TestCompetingParameters()
{
var parametersWithCompetingParameters = new ParameterSetWithCompetingParameters
{
AParam = "location1",
BParam = "location2",
};

// Both values are serializing to the same location ("location") in the dictionary, so which wins?
var dictionary = parametersWithCompetingParameters.ToDictionary();

// It seems that BParam here wins.
Assert.Equal("location2", dictionary["location"]);

// Is it because the properties are serialized in alphabetical order, or because BParam is the last property in the code structure?
// Let's test by reversing the order of the properties.
var parametersWithCompetingParametersNonAlphabetic = new ParameterSetWithCompetingParametersNonAlphabetic
{
// The order the properties are set in the constructor shouldn't matter.
// In the source code, BParam physically comes before AParam.
// We'll replicate that order here just for readability sake
BParam = "location1",
AParam = "location2",
};

var dictionaryNonAlphabetic = parametersWithCompetingParametersNonAlphabetic.ToDictionary();
Assert.Equal("location2", dictionaryNonAlphabetic["location"]);

// Just one last confirmation, let's keep the flipped alphabetical order, but rule out that the constructor order matters.
var parametersWithCompetingParametersNonAlphabetic2 = new ParameterSetWithCompetingParametersNonAlphabetic
{
// Again, AParam is physically located after BParam in the code structure, but it's set first in the constructor here.
AParam = "location2",
BParam = "location1",
};

var dictionaryNonAlphabetic2 = parametersWithCompetingParametersNonAlphabetic2.ToDictionary();
Assert.Equal("location2", dictionaryNonAlphabetic2["location"]);

// The constructor order doesn't seem to matter (which is a good thing because we can't control in what order end-users will set the properties).

// It seems the properties are in fact serialized not in alphabetical order, but in the order they are defined in the code structure.
// If you need to add an override to a parameter, physically place it after the parameter it overrides in the code structure, to ensure it is serialized last.

// The downside to this is linting. Our linter rules like to order properties alphabetically in the code structure.
// Meaning, to ensure that the override parameter is physically below the parameter it overrides, we either have to disable the linter rule for that file,
// or the override parameter has to be alphabetically after the parameter it overrides, which limits us on naming choices.
}

[Fact]
[Testing.Exception]
public void TestRequiredAndOptionalParameterValidation()
Expand Down Expand Up @@ -183,6 +234,24 @@ private sealed class ParameterSetWithRequiredAndOptionalParameters : Parameters.
public string? OptionalParameter { get; set; }
}

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

[TopLevelRequestParameter(Necessity.Optional, "location")]
public string? BParam { get; set; }
}

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

[TopLevelRequestParameter(Necessity.Optional, "location")]
public string? AParam { get; set; }
}

/// <summary>
/// This test proves that we can reuse the Addresses.Create parameter object,
/// with its serialization logic adapting to whether it is a top-level parameter object
Expand Down
Loading