Debian Codegraph @

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

raw symbols SCIP
1 //
2 // Unit test for bug https://bugzilla.xamarin.com/show_bug.cgi?id=27864
3 //
4 // Authors:
5 // Thomas Zoechling <[email protected]>
6 // Alex Soto <[email protected]>
7 //
8 //
9 // Copyright 2015 Xamarin Inc.
10 //
11
12 using System;
13 using System.Data;
14 using System.IO;
15 using System.Text;
16 using Mono.Data.Sqlite;
17 using NUnit.Framework;
18
19 namespace MonoTests.Mono.Data.Sqlite {
20
21 [TestFixture]
22 public class SqliteiOS82BugTests {
23 string dbPath;
24 string connectionString;
25 SqliteConnection cnn;
26
27 [SetUp]
28 public void Create()
29 {
30 dbPath = Path.GetTempFileName ();
31
32 // We want to start with a fresh db for each full run
33 // The database is created on the first open()
34 // but TempFile does create a file
35 if (File.Exists (dbPath))
36 File.Delete (dbPath);
37
38 connectionString = "Data Source=" + dbPath;
39 cnn = new SqliteConnection (connectionString);
40
41 try {
42 if(File.Exists(dbPath)) {
43 cnn.Dispose();
44 // We want to start with a fresh db for each full run
45 // The database is created on the first open()
46 File.Delete(dbPath);
47 }
48 }
49 catch(Exception e) {
50 throw e;
51 }
52
53 try {
54 using (var createCommand = new SqliteCommand ("CREATE TABLE Company (RecordId int, Name text);", cnn))
55 using (var insertCommand = new SqliteCommand ("INSERT INTO Company VALUES (1, 'Test CO')", cnn)) {
56 cnn.Open();
57 createCommand.ExecuteNonQuery();
58 insertCommand.ExecuteNonQuery();
59 }
60 }
61 catch(Exception e) {
62 Console.WriteLine (e);
63 throw new AssertionException ("Create table failed", e);
64 }
65 finally {
66 cnn.Close();
67 }
68 }
69
70 [TearDown]
71 public void TearDown ()
72 {
73 if (File.Exists (dbPath))
74 File.Delete (dbPath);
75 }
76
77 // Ref: https://bugzilla.xamarin.com/show_bug.cgi?id=27864
78 // 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
79 // The pruppose of this test is to know when apple fixes this. Expected test behaivour is as follows.
80 // If iOS 8.2+ Test will pass as long as apple does not fix the bug
81 // If iOS < 8.2 Test will pass as it does work as expected
82 // 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
83 [Test]
84 public void SqliteSelectTestBug27864()
85 {
86 try {
87 var cmdText = "SELECT " +
88 "2 AS SortOrder, " +
89 "com.Name AS 'Company.Name' " +
90 "FROM " +
91 "company com " +
92 "UNION " +
93 "SELECT " +
94 "0 AS SortOrder, " +
95 "com.Name AS 'Company.Name' " +
96 "FROM " +
97 "Company com " +
98 "ORDER BY " +
99 "com.Name, " +
100 "SortOrder COLLATE NOCASE";
101
102 using (cnn)
103 using (var cmd = new SqliteCommand (cmdText, cnn)) {
104 cnn.Open();
105 using (SqliteDataReader dr = cmd.ExecuteReader()) {
106 var i = 0;
107 while (dr.Read()) {
108 Assert.AreEqual(dr["SortOrder"], i);
109 Assert.AreEqual(dr["Company.Name"], "Test CO");
110 i += 2;
111 }
112 Assert.IsTrue(dr.FieldCount>0, i.ToString ());
113 }
114 }
115 } catch (SqliteException ex) {
116 #if MONOTOUCH
117 // Expected Exception from iOS 8.2 (broken) to 9.0 (fixed)
118 if (BCL.Tests.TestRuntime.CheckSystemVersion (8,2) && !BCL.Tests.TestRuntime.CheckSystemVersion (9,0))
119 Assert.That (ex.Message.Contains ("no such column: com.Name"));
120 else
121 throw;
122 #endif
123 }
124 }
125 }
126 }