PageRenderTime 16ms CodeModel.GetById 8ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/mono/mono
C# | 127 lines | 93 code | 10 blank | 24 comment | 0 complexity | c88b7c8795fa72756fa93422202130f5 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 [t0].[first_name] AS [FirstName], [t0].[last_name] AS [LastName]{0}" + 
 68                "FROM [people] AS [t0]{0}" +
 69                "WHERE [t0].[first_name] = @p0",
 70                Environment.NewLine);
 71        }
 72
 73        protected override string People(string firstName, string lastName)
 74        {
 75            return string.Format(
 76                "SELECT [t0].[first_name] AS [FirstName], [t0].[last_name] AS [LastName]{0}" + 
 77                "FROM [people] AS [t0]{0}" +
 78                "WHERE ([t0].[last_name] = @p0) AND ([t0].[first_name] = @p1)",
 79                Environment.NewLine);
 80        }
 81
 82        protected override string People(string firstName, string lastName, int skip, int take)
 83        {
 84            return string.Format("SELECT [t1].[first_name] AS [FirstName], [t1].[last_name] AS [LastName]{0}" +
 85                "FROM ({0}" +
 86                "    SELECT ROW_NUMBER() OVER (ORDER BY [t0].[first_name], [t0].[last_name]) AS [ROW_NUMBER], [t0].[first_name], [t0].[last_name]{0}" +
 87                "    FROM [people] AS [t0]{0}" +
 88                "    WHERE ([t0].[last_name] = @p0) AND ([t0].[first_name] = @p1){0}" +
 89                "    ) AS [t1]{0}" +
 90                "WHERE [t1].[ROW_NUMBER] BETWEEN @p2 + 1 AND @p2 + @p3{0}" +
 91                "ORDER BY [t1].[ROW_NUMBER]",
 92                Environment.NewLine, firstName, lastName, skip, take);
 93        }
 94
 95        [Test]
 96        public void Count()
 97        {
 98            var oldLog = Context.Log;
 99            var log = new StringWriter();
100            try
101            {
102                Context.Log = log;
103                (from p in Context.GetTable<Person>()
104                     orderby p.LastName
105                     select p)
106                    .Count();
107            }
108            catch (NotSupportedException)
109            {
110                Console.WriteLine("# logfile=\n{0}", log.ToString());
111                var expected = string.Format("SELECT COUNT(*) AS [value]{0}" +
112                    "FROM [people] AS [t0]{0}" +
113                    "--",
114                    Environment.NewLine);
115                StringAssert.Contains (expected, log.ToString());
116            }
117            catch (Exception e)
118            {
119                Assert.Fail("# ExecuteCommand: Got exception {0}", e.ToString());
120            }
121            finally
122            {
123                Context.Log = oldLog;
124            }
125        }
126    }
127}