From 14148f751afef137b90ec9ee535d8d849a83c0e0 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sat, 30 Apr 2022 10:36:03 +0100 Subject: [PATCH] Add tests for ToConnectionString/ToGetParameters --- X10D.Tests/src/Collections/DictionaryTests.cs | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/X10D.Tests/src/Collections/DictionaryTests.cs b/X10D.Tests/src/Collections/DictionaryTests.cs index b6b33fc..9fb8be2 100644 --- a/X10D.Tests/src/Collections/DictionaryTests.cs +++ b/X10D.Tests/src/Collections/DictionaryTests.cs @@ -87,4 +87,103 @@ public class DictionaryTests Assert.ThrowsException(() => dictionary.AddOrUpdate(1, addValueFactory!, (_, _) => "one")); Assert.ThrowsException(() => dictionary.AddOrUpdate(1, null!, (_, _, _) => "one", 0)); } + + [TestMethod] + public void ToConnectionString_ShouldReturnConnectionString() + { + var dictionary = new Dictionary + { + ["Data Source"] = "localhost", ["Initial Catalog"] = "test", ["Integrated Security"] = "True", ["Foobar"] = null + }; + + string connectionString = dictionary.ToConnectionString(); + Assert.AreEqual("Data Source=localhost;Initial Catalog=test;Integrated Security=True;Foobar=", connectionString); + } + + [TestMethod] + public void ToConnectionString_ShouldReturnTransformedValueConnectionString() + { + var dictionary = new Dictionary + { + ["Data Source"] = "localhost", ["Initial Catalog"] = "test", ["Integrated Security"] = "True", ["Foobar"] = null + }; + + string connectionString = dictionary.ToConnectionString(v => v?.ToUpperInvariant()); + Assert.AreEqual("Data Source=LOCALHOST;Initial Catalog=TEST;Integrated Security=TRUE;Foobar=", connectionString); + } + + [TestMethod] + public void ToConnectionString_ShouldReturnTransformedKeyValueConnectionString() + { + var dictionary = new Dictionary + { + ["Data Source"] = "localhost", ["Initial Catalog"] = "test", ["Integrated Security"] = "True", ["Foobar"] = null + }; + + string connectionString = dictionary.ToConnectionString(k => k.ToUpper(), v => v?.ToUpperInvariant()); + Assert.AreEqual("DATA SOURCE=LOCALHOST;INITIAL CATALOG=TEST;INTEGRATED SECURITY=TRUE;FOOBAR=", connectionString); + } + + [TestMethod] + public void ToConnectionString_ShouldThrow_GivenNullSource() + { + Dictionary? dictionary = null; + Assert.ThrowsException(() => dictionary!.ToConnectionString()); + Assert.ThrowsException(() => dictionary!.ToConnectionString(null!)); + Assert.ThrowsException(() => dictionary!.ToConnectionString(null!, null!)); + } + + [TestMethod] + public void ToConnectionString_ShouldThrow_GivenNullSelector() + { + var dictionary = new Dictionary(); + Assert.ThrowsException(() => dictionary.ToConnectionString(null!)); + Assert.ThrowsException(() => dictionary.ToConnectionString(null!, _ => _)); + Assert.ThrowsException(() => dictionary.ToConnectionString(_ => _, null!)); + } + + [TestMethod] + public void ToGetParameters_ShouldReturnParameters() + { + var dictionary = new Dictionary {["id"] = "1", ["user"] = "hello world", ["foo"] = "bar"}; + + string queryString = dictionary.ToGetParameters(); + Assert.AreEqual("id=1&user=hello+world&foo=bar", queryString); + } + + [TestMethod] + public void ToGetParameters_ShouldReturnTransformedValueParameters() + { + var dictionary = new Dictionary {["id"] = "1", ["user"] = "hello world", ["foo"] = null}; + + string queryString = dictionary.ToGetParameters(v => v?.ToUpper()); + Assert.AreEqual("id=1&user=HELLO+WORLD&foo=", queryString); + } + + [TestMethod] + public void ToGetParameters_ShouldReturnTransformedKeyValueParameters() + { + var dictionary = new Dictionary {["id"] = "1", ["user"] = "hello world", ["foo"] = null}; + + string queryString = dictionary.ToGetParameters(k => k.ToUpper(), v => v?.ToUpper()); + Assert.AreEqual("ID=1&USER=HELLO+WORLD&FOO=", queryString); + } + + [TestMethod] + public void ToGetParameters_ShouldThrow_GivenNullSource() + { + Dictionary? dictionary = null; + Assert.ThrowsException(() => dictionary!.ToGetParameters()); + Assert.ThrowsException(() => dictionary!.ToGetParameters(null!)); + Assert.ThrowsException(() => dictionary!.ToGetParameters(null!, null!)); + } + + [TestMethod] + public void ToGetParameters_ShouldThrow_GivenNullSelector() + { + var dictionary = new Dictionary(); + Assert.ThrowsException(() => dictionary.ToGetParameters(null!)); + Assert.ThrowsException(() => dictionary.ToGetParameters(null!, _ => _)); + Assert.ThrowsException(() => dictionary.ToGetParameters(_ => _, null!)); + } }