PageRenderTime 34ms CodeModel.GetById 25ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/mcs/class/System.Data.Linq/src/DbLinq/Test/Providers/DataContextTestBase.cs

https://github.com/ztfuqingvip/mono
C# | 158 lines | 111 code | 22 blank | 25 comment | 2 complexity | c81d01571f5c04afda81288cabc75c08 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.Data;
 29using System.Data.Common;
 30// using System.Data.Linq;
 31using System.Data.Linq.Mapping;
 32using System.Linq;
 33using System.IO;
 34
 35#if MONO_STRICT
 36using System.Data.Linq;
 37#else
 38using DbLinq.Data.Linq;
 39#endif
 40
 41using NUnit.Framework;
 42
 43using DbLinq.Null;
 44
 45namespace DbLinqTest {
 46
 47    public abstract class DataContextTestBase
 48    {
 49        DataContext context;
 50
 51        protected DataContext Context {
 52            get { return context; }
 53        }
 54
 55        [SetUp]
 56        public void SetUp()
 57        {
 58            context = CreateDataContext();
 59        }
 60
 61        protected abstract DataContext CreateDataContext();
 62
 63        [TearDown]
 64        public void TearDown()
 65        {
 66            context = null;
 67        }
 68
 69        [Test]
 70        public void ExecuteCommand()
 71        {
 72            context.Log = new StringWriter ();
 73            try 
 74            {
 75                context.ExecuteCommand("SomeCommand", 1, 2, 3);
 76            }
 77            catch (NotSupportedException)
 78            {
 79            }
 80            catch (Exception e)
 81            {
 82                Assert.Fail("# ExecuteCommand: Got exception {0}", e.ToString());
 83            }
 84            Console.WriteLine ("# ExecuteCommand: Log={0}", context.Log);
 85        }
 86
 87        [Test]
 88        public void ExecuteQuery()
 89        {
 90            context.Log = new StringWriter ();
 91            try 
 92            {
 93                context.ExecuteQuery(typeof(Person), "select * from people", 1, 2, 3);
 94            }
 95            catch (NotSupportedException)
 96            {
 97            }
 98            catch (Exception e)
 99            {
100                Assert.Fail("# ExecuteQuery: unexpected exception: {0}", e.ToString());
101            }
102            Console.WriteLine ("# ExecuteQuery: Log={0}", context.Log);
103        }
104
105        [Test]
106        public void ExecuteQueryTResult()
107        {
108            context.Log = new StringWriter ();
109            try 
110            {
111                context.ExecuteQuery<Person>("select * from people", 1, 2, 3);
112            }
113            catch (NotSupportedException)
114            {
115            }
116            catch (Exception)
117            {
118                Assert.Fail();
119            }
120            Console.WriteLine ("# ExecuteQueryTResult: Log={0}", context.Log);
121        }
122
123        [Test]
124        public void GetChangeSet()
125        {
126            // TODO
127            context.GetChangeSet();
128        }
129
130        protected abstract string People(string firstName);
131        protected abstract string People(string firstName, string lastName);
132        protected abstract string People(string firstName, string lastName, int skip, int take);
133
134        [Test]
135        public void GetCommand()
136        {
137            var foos = 
138                from p in context.GetTable<Person>()
139                where p.FirstName == "foo"
140                select p;
141            var cmd = context.GetCommand(foos);
142
143            Assert.AreEqual(People("foo"), cmd.CommandText);
144
145            foos = foos.Where(p => p.LastName == "bar");
146            var cmd2 = context.GetCommand(foos);
147
148            Assert.IsFalse(object.ReferenceEquals(cmd, cmd2));
149
150            Assert.AreEqual(People("foo", "bar"), cmd2.CommandText);
151
152            foos = foos.Skip(1).Take(2);
153            cmd = context.GetCommand(foos);
154            Assert.AreEqual(People("foo", "bar", 1, 2), cmd.CommandText);
155        }
156    }
157}
158