PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Data.Linq/src/DbLinq.SqlServer/Test/MsSqlDataContextTest.cs

https://github.com/ztfuqingvip/mono
C# | 126 lines | 91 code | 11 blank | 24 comment | 0 complexity | abbbbca6b678964551f18a25facbef72 MD5 | raw file
Possible License(s): GPL-2.0, Unlicense, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0
  1. #region MIT license
  2. //
  3. // MIT license
  4. //
  5. // Copyright (c) 2009 Novell, Inc.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #endregion
  26. using System;
  27. using System.Collections;
  28. using System.Collections.Generic;
  29. using System.Collections.ObjectModel;
  30. using System.IO;
  31. using System.Linq;
  32. using System.Reflection;
  33. #if MONO_STRICT
  34. using System.Data.Linq;
  35. using System.Data.Linq.Mapping;
  36. #else
  37. using DbLinq.Data.Linq;
  38. using DbLinq.Data.Linq.Mapping;
  39. #endif
  40. using DbLinq.Null;
  41. using NUnit.Framework;
  42. namespace DbLinqTest {
  43. [TestFixture]
  44. public class MsSqlDataContextTest : DataContextTestBase
  45. {
  46. static MsSqlDataContextTest()
  47. {
  48. #if !MONO_STRICT
  49. // Make sure this assembly has a ref to DbLinq.SqlServer.dll.
  50. var dummy = new DbLinq.SqlServer.SqlServerSqlProvider();
  51. #endif
  52. }
  53. protected override DataContext CreateDataContext()
  54. {
  55. return new DataContext (new NullConnection (), new AttributeMappingSource ());
  56. }
  57. protected override string People(string firstName)
  58. {
  59. return string.Format(
  60. "SELECT [first_name], [last_name]{0}" +
  61. "FROM [people]{0}" +
  62. "WHERE ([first_name] = '" + firstName + "')",
  63. Environment.NewLine); ;
  64. }
  65. protected override string People(string firstName, string lastName)
  66. {
  67. return People(firstName) + " AND ([last_name] = '" + lastName + "')";
  68. }
  69. protected override string People(string firstName, string lastName, int skip, int take)
  70. {
  71. return string.Format("SELECT *{0}" +
  72. "FROM ({0}" +
  73. " SELECT [first_name], [last_name]{0}" +
  74. ",{0}" +
  75. " ROW_NUMBER() OVER(ORDER BY [first_name], [last_name]{0}" +
  76. ") AS [__ROW_NUMBER]{0}" +
  77. " FROM [people]{0}" +
  78. "WHERE ([first_name] = '{1}') AND ([last_name] = '{2}') ) AS [t0]{0}" +
  79. "WHERE [__ROW_NUMBER] BETWEEN {3}+1 AND {3}+{4}{0}" +
  80. "ORDER BY [__ROW_NUMBER]",
  81. Environment.NewLine, firstName, lastName, skip, take);
  82. }
  83. [Test]
  84. public void Count()
  85. {
  86. var oldLog = Context.Log;
  87. var log = new StringWriter();
  88. try
  89. {
  90. Context.Log = log;
  91. (from p in Context.GetTable<Person>()
  92. orderby p.LastName
  93. select p)
  94. .Count();
  95. }
  96. catch (NotSupportedException)
  97. {
  98. Console.WriteLine("# logfile=\n{0}", log.ToString());
  99. var expected = string.Format("SELECT COUNT(*){0}" +
  100. "FROM [people]{0}" +
  101. "--",
  102. Environment.NewLine);
  103. Assert.IsTrue(log.ToString().Contains(expected));
  104. }
  105. catch (Exception e)
  106. {
  107. Assert.Fail("# ExecuteCommand: Got exception {0}", e.ToString());
  108. }
  109. finally
  110. {
  111. Context.Log = oldLog;
  112. }
  113. }
  114. }
  115. }