-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c28dca8
commit cc5b495
Showing
6 changed files
with
647 additions
and
442 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.