Merge pull request #3913 from omwok/master
[mono.git] / mcs / class / System.Data.Linq / src / DbLinq / Test / Providers / TestBase.cs
1 #region MIT license\r
2 // \r
3 // MIT license\r
4 //\r
5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne\r
6 // \r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy\r
8 // of this software and associated documentation files (the "Software"), to deal\r
9 // in the Software without restriction, including without limitation the rights\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
11 // copies of the Software, and to permit persons to whom the Software is\r
12 // furnished to do so, subject to the following conditions:\r
13 // \r
14 // The above copyright notice and this permission notice shall be included in\r
15 // all copies or substantial portions of the Software.\r
16 // \r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
23 // THE SOFTWARE.\r
24 // \r
25 #endregion\r
26 \r
27 using System;\r
28 using System.Data;\r
29 using System.IO;\r
30 using System.Xml;\r
31 using System.Reflection;\r
32 using NUnit.Framework;\r
33 \r
34 using nwind;\r
35 \r
36 #if MONO_STRICT\r
37 using System.Diagnostics;\r
38 public static class Profiler\r
39 {\r
40     private static Stopwatch timer = new Stopwatch();\r
41 \r
42     [Conditional("DEBUG")]\r
43     public static void Start()\r
44     {\r
45         timer.Reset();\r
46         timer.Start();\r
47     }\r
48     [Conditional("DEBUG")]\r
49     public static void At(string format, params object[] args)\r
50     {\r
51         timer.Stop();\r
52         Console.Write("#AT({0:D12}) ", timer.ElapsedTicks);\r
53         Console.WriteLine(format, args);\r
54         timer.Start();\r
55     }\r
56 \r
57     [Conditional("DEBUG")]\r
58     public static void Stop()\r
59     {\r
60         timer.Stop();\r
61     }\r
62 }\r
63 #else\r
64 using DbLinq.Util;\r
65 #endif\r
66 \r
67 namespace Test_NUnit\r
68 {\r
69     /// <summary>\r
70     /// base class for ReadTest and WriteTest. \r
71     /// Provides CreateDB(), Conn, and stringComparisonType.\r
72     /// </summary>\r
73     public abstract partial class TestBase\r
74     {\r
75         [SetUp]\r
76         public void BaseSetUp()\r
77         {\r
78             Profiler.Start();\r
79             Profiler.At("BaseSetUp()");\r
80         }\r
81 \r
82         [TearDown]\r
83         public void BaseTearDown()\r
84         {\r
85             Profiler.At("BaseTearDown()");\r
86             Profiler.Stop();\r
87         }\r
88 \r
89         public string DbServer\r
90         {\r
91             get\r
92             {\r
93                 return Environment.GetEnvironmentVariable("DbLinqServer") ?? "localhost";\r
94             }\r
95         }\r
96         public string connStr\r
97         {\r
98             get\r
99             {\r
100                 var xConnectionStringsDoc = new XmlDocument();\r
101                 xConnectionStringsDoc.Load("../tests/ConnectionStrings.xml");\r
102                 XmlNode currentAssemblyNode = xConnectionStringsDoc.SelectSingleNode(string.Format("//Connection[@assembly=\"{0}\"]", Assembly.GetCallingAssembly().GetName().Name));\r
103                 string stringConnection = currentAssemblyNode.FirstChild.Value.Replace(@"\\", @"\");\r
104                 if (stringConnection.Contains("{0}"))\r
105                     stringConnection = string.Format(stringConnection, DbServer);\r
106                 return stringConnection;\r
107             }\r
108         }\r
109         IDbConnection _conn;\r
110         public IDbConnection Conn\r
111         {\r
112             get\r
113             {\r
114                 if (_conn == null) { _conn = CreateConnection(connStr); _conn.Open(); }\r
115                 return _conn;\r
116             }\r
117         }\r
118 \r
119         //public Northwind CreateDB()\r
120         //{\r
121         //    return CreateDB(System.Data.ConnectionState.Closed);\r
122         //}\r
123 \r
124         static partial void CheckRecreateSqlite ();\r
125 \r
126         public Northwind CreateDB()\r
127         {\r
128             return CreateDB(System.Data.ConnectionState.Closed);\r
129         }\r
130 \r
131         public Northwind CreateDB(System.Data.ConnectionState state)\r
132         {\r
133             CheckRecreateSqlite();\r
134             var conn = CreateConnection(connStr);\r
135             if (state == System.Data.ConnectionState.Open)\r
136                 conn.Open();\r
137             var db = new Northwind(conn) { Log = Console.Out };\r
138             return db;\r
139         }\r
140 \r
141         /// <summary>\r
142         /// execute a sql statement, return an Int64.\r
143         /// </summary>\r
144         public long ExecuteScalar(string sql)\r
145         {\r
146             using (var cmd = CreateCommand(sql, Conn))\r
147             {\r
148                 object oResult = cmd.ExecuteScalar();\r
149                 Assert.IsNotNull("Expecting result, instead got null. (sql=" + sql + ")");\r
150                 Assert.IsInstanceOfType(typeof(long), oResult, "Expecting 'long' result from query " + sql + ", instead got type " + oResult.GetType());\r
151                 return (long)oResult;\r
152             }\r
153         }\r
154 \r
155         /// <summary>\r
156         /// execute a sql statement\r
157         /// </summary>\r
158         public void ExecuteNonQuery(string sql)\r
159         {\r
160             using (var cmd = CreateCommand(sql, Conn))\r
161             {\r
162                 int iResult = cmd.ExecuteNonQuery();\r
163             }\r
164         }\r
165 \r
166         public static Product NewProduct(string productName)\r
167         {\r
168             var p = new Product\r
169             {\r
170                 ProductName = productName,\r
171                 SupplierID = 1,\r
172                 CategoryID = 1,\r
173                 QuantityPerUnit = "11",\r
174 #if ORACLE || FIREBIRD\r
175                 UnitPrice = 11, //type "int?"\r
176 #else\r
177                 UnitPrice = 11m,\r
178 #endif\r
179                 UnitsInStock = 23,\r
180                 UnitsOnOrder = 0,\r
181             };\r
182             return p;\r
183         }\r
184     }\r
185 }\r