File: Bug27864.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (126 lines) | stat: -rw-r--r-- 3,357 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// Unit test for bug https://bugzilla.xamarin.com/show_bug.cgi?id=27864
//
// Authors:
//	Thomas Zoechling <thomas.zoechling@gmx.at>
//	Alex Soto <alex.soto@xamarin.com>
//	
//
// Copyright 2015 Xamarin Inc.
//

using System;
using System.Data;
using System.IO;
using System.Text;
using Mono.Data.Sqlite;
using NUnit.Framework;

namespace MonoTests.Mono.Data.Sqlite {
	
	[TestFixture]
	public class SqliteiOS82BugTests {
		string dbPath;
		string connectionString;
		SqliteConnection cnn;
		
		[SetUp]
		public void Create()
		{
			dbPath = Path.GetTempFileName ();

			// We want to start with a fresh db for each full run
			// The database is created on the first open()
			// but TempFile does create a file
			if (File.Exists (dbPath))
				File.Delete (dbPath);

			connectionString = "Data Source=" + dbPath;
			cnn = new SqliteConnection (connectionString);

			try {
				if(File.Exists(dbPath)) {
					cnn.Dispose();
					// We want to start with a fresh db for each full run
					// The database is created on the first open()
					File.Delete(dbPath);
				}
			}
			catch(Exception e) {
				throw e;
			}

			try {
				using (var createCommand = new SqliteCommand ("CREATE TABLE Company (RecordId int, Name text);", cnn))
				using (var insertCommand = new SqliteCommand ("INSERT INTO Company VALUES (1, 'Test CO')", cnn)) {
					cnn.Open();
					createCommand.ExecuteNonQuery();
					insertCommand.ExecuteNonQuery();
				}
			}
			catch(Exception e) {
				Console.WriteLine (e);
				throw new AssertionException ("Create table failed", e);
			}
			finally {
				cnn.Close();  
			}
		}

		[TearDown]
		public void TearDown ()
		{
			if (File.Exists (dbPath))
				File.Delete (dbPath);
		}

		// Ref: https://bugzilla.xamarin.com/show_bug.cgi?id=27864
		// As of iOS 8.2 Apple updated Sqlite and broke queries that used to work pre iOS 8.2 like the select command in this test
		// The pruppose of this test is to know when apple fixes this. Expected test behaivour is as follows.
		// If iOS 8.2+ Test will pass as long as apple does not fix the bug
		// If iOS < 8.2 Test will pass as it does work as expected
		// If iOS 8.2+ and apple fixed bug 27864 test will fail and we will need to remove the fail check on lines 105 and 106
		[Test]	
		public void SqliteSelectTestBug27864()
		{
			try {
				var cmdText = "SELECT " +
					"2 AS SortOrder, " +
					"com.Name AS 'Company.Name' " +
					"FROM " +
					"company com " +
					"UNION " +
					"SELECT " +
					"0 AS SortOrder, " +
					"com.Name AS 'Company.Name' " +
					"FROM " +
					"Company com " +
					"ORDER BY " +
					"com.Name, " +
					"SortOrder COLLATE NOCASE";

				using (cnn)
				using (var cmd = new SqliteCommand (cmdText, cnn)) {
					cnn.Open();
					using (SqliteDataReader dr = cmd.ExecuteReader()) {
						var i = 0;
						while (dr.Read()) {
							Assert.AreEqual(dr["SortOrder"], i);
							Assert.AreEqual(dr["Company.Name"], "Test CO");
							i += 2;
						}
						Assert.IsTrue(dr.FieldCount>0, i.ToString ());
					}
				}
			} catch (SqliteException ex) {
#if MONOTOUCH
				// Expected Exception from iOS 8.2 (broken) to 9.0 (fixed)
				if (BCL.Tests.TestRuntime.CheckSystemVersion (8,2) && !BCL.Tests.TestRuntime.CheckSystemVersion (9,0)) 
					Assert.That (ex.Message.Contains ("no such column: com.Name"));
				else
					throw;
#endif
			}
		}
	}
}