PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/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
  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
 27using System;
 28using System.Collections;
 29using System.Collections.Generic;
 30using System.Collections.ObjectModel;
 31using System.IO;
 32using System.Linq;
 33using System.Reflection;
 34
 35#if MONO_STRICT
 36using System.Data.Linq;
 37using System.Data.Linq.Mapping;
 38#else
 39using DbLinq.Data.Linq;
 40using DbLinq.Data.Linq.Mapping;
 41#endif
 42
 43using DbLinq.Null;
 44using NUnit.Framework;
 45
 46namespace DbLinqTest {
 47
 48    [TestFixture]
 49    public class MsSqlDataContextTest : DataContextTestBase
 50    {
 51        static MsSqlDataContextTest()
 52        {
 53#if !MONO_STRICT
 54            // Make sure this assembly has a ref to DbLinq.SqlServer.dll.
 55            var dummy = new DbLinq.SqlServer.SqlServerSqlProvider();
 56#endif
 57        }
 58
 59        protected override DataContext CreateDataContext()
 60        {
 61            return new DataContext (new NullConnection (), new AttributeMappingSource ());
 62        }
 63
 64        protected override string People(string firstName)
 65        {
 66            return string.Format(
 67                "SELECT [first_name], [last_name]{0}" + 
 68                "FROM [people]{0}" +
 69                "WHERE ([first_name] = '" + firstName + "')", 
 70                Environment.NewLine); ;
 71        }
 72
 73        protected override string People(string firstName, string lastName)
 74        {
 75            return People(firstName) + " AND ([last_name] = '" + lastName + "')";
 76        }
 77
 78        protected override string People(string firstName, string lastName, int skip, int take)
 79        {
 80            return string.Format("SELECT *{0}" +
 81                "FROM ({0}" +
 82                "    SELECT [first_name], [last_name]{0}" +
 83                ",{0}" +
 84                "    ROW_NUMBER() OVER(ORDER BY [first_name], [last_name]{0}" +
 85                ") AS [__ROW_NUMBER]{0}" +
 86                "    FROM [people]{0}" +
 87                "WHERE ([first_name] = '{1}') AND ([last_name] = '{2}')    ) AS [t0]{0}" +
 88                "WHERE [__ROW_NUMBER] BETWEEN {3}+1 AND {3}+{4}{0}" +
 89                "ORDER BY [__ROW_NUMBER]",
 90                Environment.NewLine, firstName, lastName, skip, take);
 91        }
 92
 93        [Test]
 94        public void Count()
 95        {
 96            var oldLog = Context.Log;
 97            var log = new StringWriter();
 98            try
 99            {
100                Context.Log = log;
101                (from p in Context.GetTable<Person>()
102                     orderby p.LastName
103                     select p)
104                    .Count();
105            }
106            catch (NotSupportedException)
107            {
108                Console.WriteLine("# logfile=\n{0}", log.ToString());
109                var expected = string.Format("SELECT COUNT(*){0}" +
110                    "FROM [people]{0}" +
111                    "--",
112                    Environment.NewLine);
113                Assert.IsTrue(log.ToString().Contains(expected));
114            }
115            catch (Exception e)
116            {
117                Assert.Fail("# ExecuteCommand: Got exception {0}", e.ToString());
118            }
119            finally
120            {
121                Context.Log = oldLog;
122            }
123        }
124    }
125}
126