ParametersHelperTests.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using Moq;
  2. using PetaPoco.Internal;
  3. using Shouldly;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Reflection;
  10. using Xunit;
  11. namespace PetaPoco.Tests.Unit.Utilities
  12. {
  13. public class ParametersHelperTests
  14. {
  15. [Fact]
  16. public void EnsureParamPrefix_int_ShouldWork()
  17. {
  18. int input = 34;
  19. string output = input.EnsureParamPrefix("@");
  20. output.ShouldBe("@34");
  21. }
  22. [Theory]
  23. [InlineData("@foo")]
  24. [InlineData("$foo")]
  25. [InlineData(":foo")]
  26. [InlineData("^foo")]
  27. [InlineData("foo")]
  28. public void EnsureParamPrefix_string_ShouldWork(string input)
  29. {
  30. string output = input.EnsureParamPrefix("@");
  31. output.ShouldBe("@foo");
  32. }
  33. [Theory]
  34. [InlineData(":")]
  35. [InlineData("^")]
  36. public void ReplaceParamPrefix_ShouldWork(string prefix)
  37. {
  38. var input = "This @foo is @bar";
  39. var expected = $"This {prefix}foo is {prefix}bar";
  40. var output = input.ReplaceParamPrefix(prefix);
  41. output.ShouldBe(expected);
  42. }
  43. [Fact]
  44. public void ProcessQueryParams_PositionalParams_ShouldWork()
  45. {
  46. var sql = "select * from foo where a = @0 and b = @1";
  47. var args_src = new object[] { 5, "bird" };
  48. var args_dest = new List<object>();
  49. var output = ParametersHelper.ProcessQueryParams(sql, args_src, args_dest);
  50. output.ShouldBe(sql);
  51. args_dest.ShouldBe(args_src.ToList());
  52. }
  53. private void NamedQueryParamsTestHelper(object[] args_src)
  54. {
  55. var sql = "select * from foo where a = @first and b = @second";
  56. var args_dest = new List<object>();
  57. var expected_sql = "select * from foo where a = @0 and b = @1";
  58. var expected_args = new List<object>() { 87, "Bob" };
  59. var output = ParametersHelper.ProcessQueryParams(sql, args_src, args_dest);
  60. output.ShouldBe(expected_sql);
  61. args_dest.ShouldBe(expected_args);
  62. }
  63. [Fact]
  64. public void ProcessQueryParams_ObjectWithProperties_ShouldWork()
  65. {
  66. var args_src = new[] { new { second = "Bob", first = 87 } };
  67. NamedQueryParamsTestHelper(args_src);
  68. }
  69. [Fact]
  70. public void ProcessQueryParams_Dictionary_ShouldWork()
  71. {
  72. var args_src = new[] { new Dictionary<string, object> { ["second"] = "Bob", ["first"] = 87 } };
  73. NamedQueryParamsTestHelper(args_src);
  74. }
  75. [Fact]
  76. public void ProcessQueryParams_DictionaryAndObject_ShouldWork()
  77. {
  78. var args_src = new object[] { new Dictionary<string, object> { ["second"] = "Bob" }, new { first = 87 } };
  79. NamedQueryParamsTestHelper(args_src);
  80. }
  81. [Fact]
  82. public void ProcessQueryParams_ObjectMissingParam_ShouldThrow()
  83. {
  84. var args_src = new[] { new {first = 87 } };
  85. Action act = () => NamedQueryParamsTestHelper(args_src);
  86. var ex = act.ShouldThrow<ArgumentException>();
  87. ex.Message.ShouldMatch(@"^Parameter '@second' specified");
  88. }
  89. [Fact]
  90. public void ProcessQueryParams_DictionaryMissingParam_ShouldThrow()
  91. {
  92. var args_src = new[] { new Dictionary<string, object>() { ["first"] = 87 } };
  93. Action act = () => NamedQueryParamsTestHelper(args_src);
  94. var ex = act.ShouldThrow<ArgumentException>();
  95. ex.Message.ShouldMatch(@"^Parameter '@second' specified");
  96. }
  97. [Fact]
  98. public void ProcessQueryParams_ObjectWithNull_ShouldWork()
  99. {
  100. var sql = "select * from foo where a = @first";
  101. var args_src = new[] { new { first = (string)null } };
  102. var args_dest = new List<object>();
  103. var expected_sql = "select * from foo where a = @0";
  104. var expected_args = new List<object>() { null };
  105. var output = ParametersHelper.ProcessQueryParams(sql, args_src, args_dest);
  106. output.ShouldBe(expected_sql);
  107. args_dest.ShouldBe(expected_args);
  108. }
  109. [Fact]
  110. public void ProcessQueryParams_DictionaryWithNull_ShouldWork()
  111. {
  112. var sql = "select * from foo where a = @first";
  113. var args_src = new[] { new Dictionary<string, object>() { ["first"] = null } };
  114. var args_dest = new List<object>();
  115. var expected_sql = "select * from foo where a = @0";
  116. var expected_args = new List<object>() { null };
  117. var output = ParametersHelper.ProcessQueryParams(sql, args_src, args_dest);
  118. output.ShouldBe(expected_sql);
  119. args_dest.ShouldBe(expected_args);
  120. }
  121. private void NamedProcParamsTestHelper(object[] args_src)
  122. {
  123. Action<IDbDataParameter, object, PropertyInfo> setAction = (p, o, pi) => p.Value = o;
  124. var cmd = new SqlCommand();
  125. var expected = new [] { new SqlParameter("foo", 42), new SqlParameter("bar", "Dirk Gently") };
  126. var output = ParametersHelper.ProcessStoredProcParams(cmd, args_src, setAction)
  127. .Cast<IDbDataParameter>()
  128. .ToArray();
  129. output.Count().ShouldBe(2);
  130. output[0].ParameterName.ShouldBe(expected[0].ParameterName);
  131. output[0].Value.ShouldBe(expected[0].Value);
  132. output[1].ParameterName.ShouldBe(expected[1].ParameterName);
  133. output[1].Value.ShouldBe(expected[1].Value);
  134. }
  135. [Fact]
  136. public void ProcessStoredProcParams_Parameter_ShouldWork()
  137. {
  138. var args_src = new object[] { new SqlParameter("foo", 42), new SqlParameter("bar", "Dirk Gently") };
  139. NamedProcParamsTestHelper(args_src);
  140. }
  141. [Fact]
  142. public void ProcessStoredProcParams_ObjectWithProperties_ShouldWork()
  143. {
  144. var args_src = new object[] { new { foo = 42, bar = "Dirk Gently" } };
  145. NamedProcParamsTestHelper(args_src);
  146. }
  147. [Fact]
  148. public void ProcessStoredProcParams_Dictionary_ShouldWork()
  149. {
  150. var args_src = new[] { new Dictionary<string, object>() { ["foo"] = 42, ["bar"] = "Dirk Gently" } };
  151. NamedProcParamsTestHelper(args_src);
  152. }
  153. [Fact]
  154. public void ProcessStoredProcParams_DictionaryAndObject_ShouldWork()
  155. {
  156. var args_src = new object[] { new Dictionary<string, object>() { ["foo"] = 42 }, new { bar = "Dirk Gently" } };
  157. NamedProcParamsTestHelper(args_src);
  158. }
  159. }
  160. }