MssqlCeQueryTests.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using Shouldly;
  3. using Xunit;
  4. namespace PetaPoco.Tests.Integration.Databases.MSSQLCe
  5. {
  6. [Collection("MssqlCe")]
  7. public class MssqlCeQueryTests : BaseQueryTests
  8. {
  9. public MssqlCeQueryTests()
  10. : base(new MssqlCeDBTestProvider())
  11. {
  12. }
  13. [Fact]
  14. public virtual void Query_ForEnumWithUnderlyingType_ShouldConvertValues()
  15. {
  16. foreach (var sqltype in new[] { "TINYINT", "SMALLINT", "INTEGER", "BIGINT" })
  17. {
  18. var t1 = DB.Single<T1>($"SELECT CONVERT({sqltype}, 1) AS [C]");
  19. t1.C.ShouldBe((E1) 1);
  20. var t2 = DB.Single<T2>($"SELECT CONVERT({sqltype}, 1) AS [C]");
  21. t2.C.ShouldBe((E2) 1);
  22. var t3 = DB.Single<T3>($"SELECT CONVERT({sqltype}, 1) AS [C]");
  23. t3.C.ShouldBe((E3) 1);
  24. var t4 = DB.Single<T4>($"SELECT CONVERT({sqltype}, 1) AS [C]");
  25. t4.C.ShouldBe((E4) 1);
  26. }
  27. }
  28. [Fact]
  29. public virtual void Query_ForNullableTypes_ShouldConvertValues()
  30. {
  31. // DateTime => DateTime?
  32. var t1 = DB.Single<TestClass>("SELECT GETDATE() AS [DateTimeValue]");
  33. t1.DateTimeValue.HasValue.ShouldBeTrue();
  34. // INTEGER => enum?
  35. var t2 = DB.Single<TestClass>("SELECT 1 AS [EnumValue]");
  36. t2.EnumValue.HasValue.ShouldBeTrue();
  37. t2.EnumValue.Value.ShouldBe((E3) 1);
  38. // TEXT => enum?
  39. var t3 = DB.Single<TestClass>("SELECT 'Hello' AS [EnumValue]");
  40. t3.EnumValue.HasValue.ShouldBeTrue();
  41. t3.EnumValue.Value.ShouldBe(E3.Hello);
  42. // TEXT => Guid?
  43. var t4 = DB.Single<TestClass>("SELECT 'ffb68770-dfcc-49b2-b800-d499477af784' AS [GuidValue]");
  44. t4.GuidValue.HasValue.ShouldBeTrue();
  45. t4.GuidValue.Value.ShouldBe(Guid.Parse("ffb68770-dfcc-49b2-b800-d499477af784"));
  46. // INTEGER => int?
  47. var t5 = DB.Single<TestClass>("SELECT 1 AS [IntValue]");
  48. t5.IntValue.HasValue.ShouldBeTrue();
  49. t5.IntValue.Value.ShouldBe(1);
  50. // SMALLINT => int?
  51. var t6 = DB.Single<TestClass>("SELECT CONVERT(SMALLINT, 1) AS [IntValue]");
  52. t6.IntValue.HasValue.ShouldBeTrue();
  53. t6.IntValue.Value.ShouldBe(1);
  54. // BIGINT => int?
  55. var t7 = DB.Single<TestClass>("SELECT CONVERT(BIGINT, 1) AS [IntValue]");
  56. t7.IntValue.HasValue.ShouldBeTrue();
  57. t7.IntValue.Value.ShouldBe(1);
  58. }
  59. }
  60. internal enum E1 : byte
  61. {
  62. }
  63. internal class T1
  64. {
  65. public E1 C { get; set; }
  66. }
  67. internal enum E2 : short
  68. {
  69. }
  70. internal class T2
  71. {
  72. public E2 C { get; set; }
  73. }
  74. internal enum E3 : int
  75. {
  76. Hello = 1,
  77. }
  78. internal class T3
  79. {
  80. public E3 C { get; set; }
  81. }
  82. internal enum E4 : long
  83. {
  84. }
  85. internal class T4
  86. {
  87. public E4 C { get; set; }
  88. }
  89. internal class TestClass
  90. {
  91. public DateTime? DateTimeValue { get; set; }
  92. public E3? EnumValue { get; set; }
  93. public Guid? GuidValue { get; set; }
  94. public int? IntValue { get; set; }
  95. }
  96. }