Skip to content

Commit

Permalink
Add tests and clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBenzSW committed Jul 11, 2024
1 parent c28dca8 commit cc5b495
Show file tree
Hide file tree
Showing 6 changed files with 647 additions and 442 deletions.
129 changes: 129 additions & 0 deletions ShipEngineSDK.Test/ClientUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using ShipEngineSDK;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Xunit;

namespace ShipEngineTest
{
public class ClientUtilsTests
{
public class ParameterToMultiMap
{
[Fact]
public void WithCollectionAndMultiFormat_ShouldReturnCorrectMultimap()
{
var collection = new List<string> { "value1", "value2" };
var expected = new Multimap<string, string> { { "key", "value1" }, { "key", "value2" } };

var result = ClientUtils.ParameterToMultiMap("multi", "key", collection);

Assert.Equal(expected, result);
}


[Fact]
public void WithSingleValue_ShouldReturnMultimap()
{
var result = ClientUtils.ParameterToMultiMap("", "key", "value");
Assert.Single(result);
Assert.Contains("key", result.Keys);
Assert.Contains("value", result["key"]);
}

[Fact]
public void TestDictionaryWithDefaultFormat()
{
var dictionary = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } };
var result = ClientUtils.ParameterToMultiMap("", "", dictionary);
Assert.Equal(dictionary.Count, result.Count);
foreach (var kvp in dictionary)
{
Assert.Contains(kvp.Key, result.Keys);
Assert.Contains(kvp.Value, result[kvp.Key]);
}
}

[Fact]
public void WithDeepObject_ShouldReturnMultimap()
{
var dictionary = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } };
var result = ClientUtils.ParameterToMultiMap("deepObject", "obj", dictionary);
foreach (var kvp in dictionary)
{
var expectedKey = $"obj[{kvp.Key}]";
Assert.Contains(expectedKey, result.Keys);
Assert.Contains(kvp.Value, result[expectedKey]);
}
}

[Fact]
public void WithUnsupportedCollectionFormat_ShouldReturnMultimap()
{
var collection = new List<string> { "value1", "value2" };
var result = ClientUtils.ParameterToMultiMap("csv", "key", collection);
Assert.Single(result);
Assert.Contains("key", result.Keys);
}
}

public class ParameterToString
{
public enum TestEnum
{
[EnumMember(Value = "TestValue")]
WithAttribute,
WithoutAttribute
}

[Fact]
public void WithDateTime_ShouldReturnIso8601String()
{
var dateTime = new DateTime(2020, 1, 1, 12, 0, 0, DateTimeKind.Utc);
var result = ClientUtils.ParameterToString(dateTime);
Assert.Equal("2020-01-01T12:00:00.0000000Z", result);
}

[Fact]
public void WithDateTimeOffset_ShouldReturnIso8601String()
{
var testDateTimeOffset = new DateTimeOffset(2020, 1, 1, 12, 0, 0, TimeSpan.Zero);
string result = ClientUtils.ParameterToString(testDateTimeOffset);
Assert.Equal("2020-01-01T12:00:00.0000000+00:00", result);
}

[Theory]
[InlineData(true, "true")]
[InlineData(false, "false")]
public void WithBoolean_ShouldReturnBooleanString(bool value, string expected)
{
string result = ClientUtils.ParameterToString(value);
Assert.Equal(expected, result);
}

[Fact]
public void WithCollectionOfStrings_ShouldReturnCommaSeparatedList()
{
var testList = new List<string> { "one", "two", "three" };
string result = ClientUtils.ParameterToString(testList);
Assert.Equal("one,two,three", result);
}

[Theory]
[InlineData(TestEnum.WithAttribute, "TestValue")]
[InlineData(TestEnum.WithoutAttribute, "WithoutAttribute")]
public void WithEnum_ShouldReturnAttributeOrName(TestEnum value, string expected)
{
string result = ClientUtils.ParameterToString(value);
Assert.Equal(expected, result);
}

[Fact]
public void WithNull_ShouldReturnEmptyString()
{
string result = ClientUtils.ParameterToString(null);
Assert.Empty(result);
}
}
}
}
181 changes: 181 additions & 0 deletions ShipEngineSDK.Test/MultimapTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
using ShipEngineSDK;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace ShipEngineTest
{
public class MultimapTests
{
public class AddWithKeyAndValue
{
[Fact]
public void ShouldAddNewEntry_WhenKeyDoesNotExist()
{
var map = new Multimap<string, string>();
map.Add("foo", "a");

Assert.Single(map["foo"]);
Assert.Contains("a", map["foo"]);
}

[Fact]
public void ShouldUpdateExistingEntry_WhenEntryAlreadyExists()
{
var map = new Multimap<string, string>();
map.Add("foo", "a");
map.Add("foo", "b");

Assert.Equal(2, map["foo"].Count);
Assert.Contains("a", map["foo"]);
Assert.Contains("b", map["foo"]);
}

[Fact]
public void ShouldAddNewEntry_WhenKeysAreDifferent()
{
var map = new Multimap<string, string>();
map.Add("foo", "a");
map.Add("bar", "b");

Assert.Single(map["foo"]);
Assert.Contains("a", map["foo"]);
Assert.Single(map["bar"]);
Assert.Contains("b", map["bar"]);
}

[Fact]
public void ShouldNotAddEntry_WhenValueIsNull()
{
var map = new Multimap<string, string>();
map.Add("foo", (string)null);
Assert.Empty(map);
}
}

public class AddWithKeyAndList
{
[Fact]
public void ShouldAddNewEntry_WhenKeyDoesNotExist()
{
var map = new Multimap<string, string>();
map.Add("foo", new List<string> { "a", "b" });

Assert.Equal(2, map["foo"].Count);
Assert.Contains("a", map["foo"]);
Assert.Contains("b", map["foo"]);
}

[Fact]
public void ShouldUpdateExistingEntry_WhenEntryAlreadyExists()
{
var map = new Multimap<string, string>();
map.Add("foo", new List<string> { "a" });
map.Add("foo", new List<string> { "b" });

Assert.Equal(2, map["foo"].Count);
Assert.Contains("a", map["foo"]);
Assert.Contains("b", map["foo"]);
}

[Fact]
public void ShouldAddNewEntry_WhenKeysAreDifferent()
{
var map = new Multimap<string, string>();
map.Add("foo", new List<string> { "a" });
map.Add("bar", new List<string> { "b" });

Assert.Single(map["foo"]);
Assert.Contains("a", map["foo"]);
Assert.Single(map["bar"]);
Assert.Contains("b", map["bar"]);
}

[Fact]
public void ShouldNotAddEntry_WhenValueIsEmpty()
{
var map = new Multimap<string, string>();
map.Add("foo", Enumerable.Empty<string>().ToList());
Assert.Empty(map);
}
}

public class AddWithKeyValuePair
{
[Fact]
public void ShouldAddNewEntry_WhenKeyDoesNotExist()
{
var map = new Multimap<string, string>();
map.Add(new KeyValuePair<string, IList<string>>("foo", new List<string> { "a", "b" }));

Assert.Equal(2, map["foo"].Count);
Assert.Contains("a", map["foo"]);
Assert.Contains("b", map["foo"]);
}

[Fact]
public void ShouldUpdateExistingEntry_WhenEntryAlreadyExists()
{
var map = new Multimap<string, string>();
map.Add(new KeyValuePair<string, IList<string>>("foo", new List<string> { "a" }));
map.Add(new KeyValuePair<string, IList<string>>("foo", new List<string> { "b" }));

Assert.Equal(2, map["foo"].Count);
Assert.Contains("a", map["foo"]);
Assert.Contains("b", map["foo"]);
}

[Fact]
public void ShouldAddNewEntry_WhenKeysAreDifferent()
{
var map = new Multimap<string, string>();
map.Add(new KeyValuePair<string, IList<string>>("foo", new List<string> { "a" }));
map.Add(new KeyValuePair<string, IList<string>>("bar", new List<string> { "b" }));

Assert.Single(map["foo"]);
Assert.Contains("a", map["foo"]);
Assert.Single(map["bar"]);
Assert.Contains("b", map["bar"]);
}
}

public class AddWithMultimap
{
[Fact]
public void ShouldAddNewEntry_WhenKeyDoesNotExist()
{
var map = new Multimap<string, string>();
map.Add(new Multimap<string, string> { { "foo", new List<string> { "a", "b" } } });

Assert.Equal(2, map["foo"].Count);
Assert.Contains("a", map["foo"]);
Assert.Contains("b", map["foo"]);
}

[Fact]
public void ShouldUpdateExistingEntry_WhenEntryAlreadyExists()
{
var map = new Multimap<string, string>();
map.Add(new Multimap<string, string> { { "foo", new List<string> { "a" } } });
map.Add(new Multimap<string, string> { { "foo", new List<string> { "b" } } });

Assert.Equal(2, map["foo"].Count);
Assert.Contains("a", map["foo"]);
Assert.Contains("b", map["foo"]);
}

[Fact]
public void ShouldAddNewEntry_WhenKeysAreDifferent()
{
var map = new Multimap<string, string>();
map.Add(new Multimap<string, string> { { "foo", new List<string> { "a" } } });
map.Add(new Multimap<string, string> { { "bar", new List<string> { "b" } } });

Assert.Single(map["foo"]);
Assert.Contains("a", map["foo"]);
Assert.Single(map["bar"]);
Assert.Contains("b", map["bar"]);
}
}
}
}
8 changes: 8 additions & 0 deletions ShipEngineSDK.Test/RequestOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public void FullPathReturnsPathWithParametersReplaced()
Assert.Equal("/v1/foo/se-12345", result.FullPath());
}

[Fact]
public void FullPathReturnsPathWithParametersReplacedAndEscaped()
{
var result = new RequestOptions("/v1/foo/{id}");
result.PathParameters.Add("id", "foo bar");
Assert.Equal("/v1/foo/foo%20bar", result.FullPath());
}

[Fact]
public void FullPathReturnsQueryParameters()
{
Expand Down
Loading

0 comments on commit cc5b495

Please sign in to comment.