Skip to content

Commit

Permalink
Improve settings splitting (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
romankr authored Nov 15, 2024
1 parent ab87d1a commit dd01266
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,104 @@ namespace OddsCollector.Functions.Tests.Tests.OddsApi.Configuration;

internal class OddsApiOptions
{
private static readonly IEnumerable<(string LeagueString, HashSet<string> ExpectedLeagues)>
TestCases = new List<(string LeagueString, HashSet<string> ExpectedLeagues)>
{
("league", ["league"]),
("league1;league2", ["league1", "league2"]),
("league1;;league2", ["league1", "league2"]),
("league1;league1", ["league1"])
};

[TestCaseSource(nameof(TestCases))]
public void SetLeagues_WithValidLeagueInputString_ReturnsCorrectLeagues(
(string LeagueString, HashSet<string> ExpectedLeagues) testCase)
[Test]
public void SetLeagues_WithOneLeague_ReturnsCorrectLeagues()
{
var options = new OddsApiClientOptions();

options.AddLeagues("league");

options.Leagues.Should().NotBeNull().And.BeEquivalentTo(["league"]);
}

[Test]
public void SetLeagues_WithMultipleLeagues_ReturnsCorrectLeagues()
{
var options = new OddsApiClientOptions();

options.AddLeagues("league1;league2");

options.Leagues.Should().NotBeNull().And.BeEquivalentTo(["league1", "league2"]);
}

[Test]
public void SetLeagues_WithDuplicateLeagues_ReturnsOneLeague()
{
var options = new OddsApiClientOptions();

options.AddLeagues("league1;league1");

options.Leagues.Should().NotBeNull().And.BeEquivalentTo(["league1"]);
}

[Test]
public void SetLeagues_WithEmptyLeague_ReturnsCorrectLeagues()
{
var options = new OddsApiClientOptions();

options.AddLeagues("league1;;league2");

options.Leagues.Should().NotBeNull().And.BeEquivalentTo(["league1", "league2"]);
}

[Test]
public void SetLeagues_WithLeadingAndTrailingCharacters_ReturnsCorrectLeagues()
{
// Arrange
var options = new OddsApiClientOptions();

// Act
options.AddLeagues(testCase.LeagueString);
options.AddLeagues("league1\n; league2\r");

// Assert
options.Leagues.Should().NotBeNull().And.BeEquivalentTo(testCase.ExpectedLeagues);
options.Leagues.Should().NotBeNull().And.BeEquivalentTo(["league1", "league2"]);
}

[Test]
public void SetLeagues_WithNullOrEmptyLeagues_ThrowsException()
{
// Arrange
var options = new OddsApiClientOptions();

// Act
var action = () => options.AddLeagues(string.Empty);

// Assert
action.Should().ThrowExactly<ArgumentException>().WithParameterName("leaguesString");
action.Should().ThrowExactly<ArgumentException>().WithParameterName("leagues");
}

[Test]
public void SetLeagues_WithNullLeagues_ThrowsException()
{
// Arrange
var options = new OddsApiClientOptions();

// Act
var action = () => options.AddLeagues(null);

// Assert
action.Should().ThrowExactly<ArgumentNullException>().WithParameterName("leaguesString");
action.Should().ThrowExactly<ArgumentNullException>().WithParameterName("leagues");
}

[Test]
public void SetApiKey_WithValidKey_ReturnsThisKey()
{
// Arrange
const string key = nameof(key);
var options = new OddsApiClientOptions();

// Act
options.SetApiKey(key);

// Assert
options.ApiKey.Should().NotBeNull().And.Be(key);
}

[Test]
public void SetApiKey_WithNullApiKey_ThrowsException()
{
// Arrange
var options = new OddsApiClientOptions();

// Act
var action = () => options.SetApiKey(null);

// Assert
action.Should().ThrowExactly<ArgumentNullException>().WithParameterName("apiKey");
}

[Test]
public void SetApiKey_WithEmptyApiKey_ThrowsException()
{
// Arrange
var options = new OddsApiClientOptions();

// Act
var action = () => options.SetApiKey(string.Empty);

// Assert
action.Should().ThrowExactly<ArgumentException>().WithParameterName("apiKey");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ internal class OddsApiClientOptions

public string ApiKey { get; set; } = string.Empty;

public void AddLeagues(string? leaguesString)
public void AddLeagues(string? leagues)
{
ArgumentException.ThrowIfNullOrEmpty(leaguesString);
ArgumentException.ThrowIfNullOrEmpty(leagues);

foreach (var league in leaguesString.Split(";"))
{
if (!string.IsNullOrEmpty(league))
{
Leagues.Add(league);
}
}
var deserialized =
leagues.Split(";", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

Leagues.UnionWith(deserialized);
}

public void SetApiKey(string? apiKey)
Expand Down

0 comments on commit dd01266

Please sign in to comment.