Debian Codegraph @

mcs/class/Mono.Data.Sqlite/Test/SqliteDataReaderTest.cs

raw symbols SCIP
1 // SqliteDataReaderTest.cs - NUnit Test Cases for SqliteDataReader
2 //
3 // Authors:
4 // Sureshkumar T <[email protected]>
5 //
6
7 //
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30
31 using System;
32 using System.Collections.Generic;
33 using System.Data;
34 using System.IO;
35 using System.Text;
36 using Mono.Data.Sqlite;
37
38 using NUnit.Framework;
39
40 using MonoTests.Helpers;
41
42 namespace MonoTests.Mono.Data.Sqlite
43 {
44 [TestFixture]
45 public class SqliteDataReaderTest
46 {
47 readonly static string _uri = TestResourceHelper.GetFullPathOfResource("Test/test.db");
48 readonly static string _connectionString = "URI=file://" + _uri + ", version=3";
49 SqliteConnection _conn = new SqliteConnection ();
50
51 [TestFixtureSetUp]
52 public void FixtureSetUp ()
53 {
54 if (! File.Exists (_uri) || new FileInfo (_uri).Length == 0) {
55 // ignore all tests
56 Assert.Ignore ("#000 ignoring all fixtures. No database present");
57 }
58 }
59
60 [Test]
61 public void EmptyStatementThrowsAgrumentException ()
62 {
63 _conn.ConnectionString = _connectionString;
64 SqliteDataReader reader = null;
65 using (_conn) {
66 _conn.Open ();
67 SqliteCommand cmd = (SqliteCommand) _conn.CreateCommand ();
68 cmd.CommandText = string.Empty;
69 try {
70 reader = cmd.ExecuteReader ();
71 Assert.Fail ();
72 } catch (ArgumentException ex){
73 // expected this one
74 } catch (Exception ex) {
75 Assert.Fail ();
76 }
77 }
78 }
79
80 [Test]
81 public void NullStatementThrowsAgrumentException ()
82 {
83 _conn.ConnectionString = _connectionString;
84 SqliteDataReader reader = null;
85 using (_conn) {
86 _conn.Open ();
87 SqliteCommand cmd = (SqliteCommand) _conn.CreateCommand ();
88 cmd.CommandText = null;
89 try {
90 reader = cmd.ExecuteReader ();
91 Assert.Fail ();
92 } catch (ArgumentException ex){
93 // expected this one
94 } catch (Exception ex) {
95 Assert.Fail ();
96 }
97 }
98 }
99
100 [Test]
101 public void WhitespaceOnlyStatementThrowsAgrumentException ()
102 {
103 _conn.ConnectionString = _connectionString;
104 SqliteDataReader reader = null;
105 using (_conn) {
106 _conn.Open ();
107 SqliteCommand cmd = (SqliteCommand) _conn.CreateCommand ();
108 cmd.CommandText = " ";
109 try {
110 reader = cmd.ExecuteReader ();
111 Assert.Fail ();
112 } catch (ArgumentException ex){
113 // expected this one
114 } catch (Exception ex) {
115 Assert.Fail ();
116 }
117 }
118 }
119
120 [Test]
121 [Category ("NotWorking")]
122 public void GetSchemaTableTest ()
123 {
124 _conn.ConnectionString = _connectionString;
125 SqliteDataReader reader = null;
126 using (_conn) {
127 _conn.Open ();
128 SqliteCommand cmd = (SqliteCommand) _conn.CreateCommand ();
129 cmd.CommandText = "select * from test";
130 reader = cmd.ExecuteReader ();
131 try {
132 DataTable dt = reader.GetSchemaTable ();
133 Assert.IsNotNull (dt, "#GS1 should return valid table");
134 Assert.IsTrue (dt.Rows.Count > 0, "#GS2 should return with rows ;-)");
135 } finally {
136 if (reader != null && !reader.IsClosed)
137 reader.Close ();
138 _conn.Close ();
139 }
140 }
141 }
142
143 [Test]
144 public void TypeOfNullInResultTest ()
145 {
146 _conn.ConnectionString = _connectionString;
147 SqliteDataReader reader = null;
148 using (_conn) {
149 _conn.Open ();
150 SqliteCommand cmd = (SqliteCommand) _conn.CreateCommand ();
151 cmd.CommandText = "select null from test";
152 reader = cmd.ExecuteReader ();
153 try {
154 Assert.IsTrue (reader.Read());
155 Assert.IsNotNull (reader.GetFieldType (0));
156 } finally {
157 if (reader != null && !reader.IsClosed)
158 reader.Close ();
159 _conn.Close ();
160 }
161 }
162 }
163
164 [Test]
165 public void TimestampTest ()
166 {
167 _conn.ConnectionString = _connectionString;
168 using (_conn) {
169 _conn.Open ();
170 var cmd = (SqliteCommand) _conn.CreateCommand ();
171 cmd.CommandText = @"CREATE TABLE IF NOT EXISTS TestNullableDateTime (nullable TIMESTAMP NULL, dummy int); INSERT INTO TestNullableDateTime (nullable, dummy) VALUES (124123, 2);";
172 cmd.ExecuteNonQuery ();
173
174 var query = "SELECT * FROM TestNullableDateTime;";
175 cmd = (SqliteCommand) _conn.CreateCommand ();
176 cmd.CommandText = query;
177 cmd.CommandType = CommandType.Text;
178
179 using (var reader = cmd.ExecuteReader ()) {
180 try {
181 var dt = reader ["nullable"];
182 Assert.Fail ("Expected: FormatException");
183 } catch (FormatException ex) {
184 // expected this one
185 }
186 }
187 }
188
189 _conn.ConnectionString = _connectionString + ",DateTimeFormat=UnixEpoch";
190 using (_conn) {
191 _conn.Open ();
192 var cmd = (SqliteCommand) _conn.CreateCommand ();
193 cmd.CommandText = @"CREATE TABLE IF NOT EXISTS TestNullableDateTime (nullable TIMESTAMP NULL, dummy int); INSERT INTO TestNullableDateTime (nullable, dummy) VALUES (124123, 2);";
194 cmd.ExecuteNonQuery ();
195
196 var query = "SELECT * FROM TestNullableDateTime;";
197 cmd = (SqliteCommand) _conn.CreateCommand ();
198 cmd.CommandText = query;
199 cmd.CommandType = CommandType.Text;
200
201 using (var reader = cmd.ExecuteReader ()) {
202 // this should succeed now
203 var dt = reader ["nullable"];
204 }
205 }
206 }
207
208 [Test]
209 public void CloseConnectionTest ()
210 {
211 // When this test fails it may confuse nunit a bit, causing it to show strange
212 // exceptions, since it leaks file handles (and nunit tries to open files,
213 // which it doesn't expect to fail).
214
215 // For the same reason a lot of other tests will fail when this one fails.
216
217 _conn.ConnectionString = _connectionString;
218 using (_conn) {
219 _conn.Open ();
220 using (var cmd = (SqliteCommand) _conn.CreateCommand ()) {
221 cmd.CommandText = @"CREATE TABLE IF NOT EXISTS TestNullableDateTime (nullable TIMESTAMP NULL, dummy int); INSERT INTO TestNullableDateTime (nullable, dummy) VALUES (124123, 2);";
222 cmd.ExecuteNonQuery ();
223 }
224 }
225
226 for (int i = 0; i < 1000; i++) {
227 _conn.ConnectionString = _connectionString;
228 using (_conn) {
229 _conn.Open ();
230 using (var cmd = (SqliteCommand) _conn.CreateCommand ()) {
231 cmd.CommandText = "SELECT * FROM TestNullableDateTime;";
232 cmd.CommandType = CommandType.Text;
233
234 using (var reader = cmd.ExecuteReader (CommandBehavior.CloseConnection)) {
235 reader.Read ();
236 }
237 }
238 }
239 }
240 }
241
242 void AddParameter (global::System.Data.Common.DbCommand cm, string name, object value)
243 {
244 var param = cm.CreateParameter ();
245 param.ParameterName = ":" + name;
246 param.Value = value;
247 cm.Parameters.Add (param);
248 }
249
250 class D
251 {
252 public string Sql;
253 public Type Expected;
254 public object Value;
255 }
256
257 [Test]
258 public void TestDataTypes ()
259 {
260 SqliteParameter param;
261
262 var data = new List<D> ()
263 {
264 new D () { Sql = "DATETIME", Expected = typeof (DateTime), Value = DateTime.Now },
265 new D () { Sql = "GUIDBLOB NOT NULL", Expected = typeof (Guid), Value = new byte [] { 3, 14, 15 } },
266 new D () { Sql = "BOOLEAN", Expected = typeof (bool), Value = true },
267 new D () { Sql = "INT32", Expected = typeof (long), Value = 1 },
268 new D () { Sql = "INT32 NOT NULL", Expected = typeof (long), Value = 2 },
269 new D () { Sql = "UINT1", Expected = typeof (long), Value = 3 },
270
271 // these map to the INTEGER affinity
272 new D () { Sql = "INT", Expected = typeof (int), Value = 4 },
273 new D () { Sql = "INTEGER", Expected = typeof (long), Value = 5 },
274 new D () { Sql = "TINYINT", Expected = typeof (byte), Value = 6 },
275 new D () { Sql = "SMALLINT", Expected = typeof (short), Value = 7 },
276 new D () { Sql = "MEDIUMINT", Expected = typeof (long), Value = 8 },
277 new D () { Sql = "BIGINT", Expected = typeof (long), Value = 9 },
278 new D () { Sql = "UNSIGNED BIG INT", Expected = typeof (long), Value = 0 },
279 new D () { Sql = "INT2", Expected = typeof (long), Value = 1 },
280 new D () { Sql = "INT4", Expected = typeof (long), Value = 2 },
281
282 // these map to the TEXT affinity
283 new D () { Sql = "CHARACTER(20)", Expected = typeof (string), Value = "a" },
284 new D () { Sql = "VARCHAR(255)", Expected = typeof (string), Value = "b" },
285 new D () { Sql = "VARYING CHARACTER(255)", Expected = typeof (string), Value = "c" },
286 new D () { Sql = "NCHAR(55)", Expected = typeof (string), Value = "d" },
287 new D () { Sql = "NATIVE CHARACTER(70)", Expected = typeof (string), Value = "e" },
288 new D () { Sql = "NVARCHAR(100)", Expected = typeof (string), Value = "f" },
289 new D () { Sql = "TEXT", Expected = typeof (string), Value = "g" },
290 new D () { Sql = "CLOB", Expected = typeof (string), Value = "h" },
291
292 // these map to the NONE affinity
293 new D () { Sql = "BLOB", Expected = typeof (byte[]), Value = new byte [] { 3, 14, 15 } },
294 new D () { Sql = "", Expected = typeof (object), Value = null },
295
296 // these map to the REAL affinity
297 new D () { Sql = "REAL", Expected = typeof (float), Value = 3.2 },
298 new D () { Sql = "DOUBLE", Expected = typeof (double), Value = 4.2 },
299 new D () { Sql = "DOUBLE PRECISION", Expected = typeof (double), Value = 5.2 },
300 new D () { Sql = "FLOAT", Expected = typeof (double), Value = 6.2 },
301
302 // these map to the NUMERIC affinity
303 new D () { Sql = "NUMERIC", Expected = typeof (decimal), Value = 3.2 },
304 new D () { Sql = "DECIMAL(10,5)", Expected = typeof (decimal), Value = null },
305 new D () { Sql = "BOOLEAN", Expected = typeof (bool), Value = true },
306 new D () { Sql = "DATE", Expected = typeof (DateTime), Value = DateTime.Now },
307 new D () { Sql = "DATETIME", Expected = typeof (DateTime), Value = DateTime.Now },
308
309
310 };
311
312 _conn.ConnectionString = _connectionString;
313 using (_conn) {
314 _conn.Open();
315
316 using (var cm = _conn.CreateCommand ()) {
317 var sql = new StringBuilder ();
318 var args = new StringBuilder ();
319 var vals = new StringBuilder ();
320 sql.AppendLine ("DROP TABLE TEST;");
321 sql.Append ("CREATE TABLE TEST (");
322
323 bool comma = false;
324 for (int i = 0; i < data.Count; i++) {
325 if (comma) {
326 sql.Append (", ");
327 args.Append (", ");
328 vals.Append (", ");
329 }
330 comma = true;
331 sql.AppendFormat ("F{0} {1}", i, data [i].Sql);
332 args.AppendFormat ("F{0}", i);
333 vals.AppendFormat (":F{0}", i);
334 AddParameter (cm, "F" + i.ToString (), data [i].Value);
335 }
336 sql.AppendLine (");");
337 sql.Append ("INSERT INTO TEST (");
338 sql.Append (args.ToString ());
339 sql.Append (") VALUES (");
340 sql.Append (vals.ToString ());
341 sql.Append (");");
342
343 cm.CommandText = sql.ToString ();
344
345 cm.ExecuteNonQuery ();
346 }
347
348 using (var cm = _conn.CreateCommand ()) {
349 cm.CommandText = "SELECT * FROM TEST";
350 using (var dr = cm.ExecuteReader ()) {
351 dr.Read ();
352
353 for (int i = 0; i < data.Count; i++) {
354 string tn = data [i].Sql.Replace (" NOT NULL", "");
355 int index = dr.GetOrdinal ("F" + i.ToString ());
356 Assert.AreEqual (tn, dr.GetDataTypeName (index), "F" + i.ToString () + " (" + data [i].Sql + ")");
357 Assert.AreEqual (data [i].Expected.FullName, dr.GetFieldType (index).ToString (), "F" + i.ToString () + " (" + data [i].Sql + ")");
358 }
359 }
360 }
361 }
362 }
363 }
364 }