1 // SqliteDataAdapterUnitTests.cs - NUnit Test Cases for Mono.Data.Sqlite.SqliteDataAdapter
3 // Author(s): Thomas Zoechling <thomas.zoechling@gmx.at>
10 using Mono.Data.Sqlite;
11 using NUnit.Framework;
13 namespace MonoTests.Mono.Data.Sqlite
17 public class SqliteCommandUnitTests
19 readonly static string _uri = "SqliteTest.db";
20 readonly static string _connectionString = "URI=file://" + _uri + ", version=3";
21 static SqliteConnection _conn = new SqliteConnection (_connectionString);
22 readonly static string stringvalue = "my keyboard is better than yours : äöüß";
24 public SqliteCommandUnitTests()
36 // We want to start with a fresh db for each full run
37 // The database is created on the first open()
47 SqliteCommand createCommand = new SqliteCommand("CREATE TABLE t1(t TEXT, f FLOAT, i INTEGER, b TEXT);",_conn);
48 SqliteCommand insertCommand = new SqliteCommand("INSERT INTO t1 (t, f, i, b ) VALUES('" + stringvalue + "',123,123,'123')",_conn);
53 createCommand.ExecuteNonQuery();
54 insertCommand.ExecuteNonQuery();
58 Console.WriteLine (e);
59 throw new AssertionException("Create table failed",e);
70 SqliteCommand simpleSelect = new SqliteCommand("SELECT * FROM t1; ", _conn); // check trailing spaces
74 SqliteDataReader dr = simpleSelect.ExecuteReader();
77 string test = dr[0].ToString();
78 Assert.AreEqual(dr["T"], stringvalue); // also checks case-insensitive column
79 Assert.AreEqual(dr["F"], 123);
80 Assert.AreEqual(dr["I"], 123);
81 Assert.AreEqual(dr["B"], "123");
83 Assert.IsTrue(dr.FieldCount>0);
90 SqliteCommand insCmd = new SqliteCommand("INSERT INTO t1 VALUES ('todelete',0.1,0,'')",_conn);
91 SqliteCommand delCmd = new SqliteCommand("DELETE FROM t1 WHERE t = 'todelete'",_conn);
95 int insReturn = insCmd.ExecuteNonQuery();
96 int delReturn = delCmd.ExecuteNonQuery();
98 Assert.IsTrue(insReturn == delReturn);
105 SqliteCommand insCmd = new SqliteCommand("INSERT INTO t1 VALUES ('inserted',0.1,0,'')",_conn);
109 int insReturn = insCmd.ExecuteNonQuery();
110 Assert.IsTrue(insReturn == 1);
117 SqliteCommand insCmd = new SqliteCommand("INSERT INTO t1 VALUES ('toupdate',0.1,0,'')",_conn);
118 SqliteCommand updCmd = new SqliteCommand("UPDATE t1 SET t = 'updated' ,f = 2.0, i = 2, b = '' WHERE t = 'toupdate'",_conn);
122 insCmd.ExecuteNonQuery();
123 Assert.IsTrue(updCmd.ExecuteNonQuery() == 1);
129 public void ScalarReturn()
131 // This should return the 1 line that got inserted in CreateTable() Test
132 SqliteCommand cmd = new SqliteCommand("SELECT COUNT(*) FROM t1 WHERE t LIKE '%äöüß'",_conn);
136 Assert.AreEqual(1, Convert.ToInt32(cmd.ExecuteScalar()));
141 public void InsertWithTransaction()
144 SqliteTransaction t = _conn.BeginTransaction() as SqliteTransaction;
145 SqliteCommand c1 = new SqliteCommand("INSERT INTO t1 VALUES ('a',0.1,0,'0')",_conn,t);
146 SqliteCommand c2 = new SqliteCommand("INSERT INTO t1 VALUES ('b',1.2,0,'0')",_conn,t);
147 SqliteCommand c3 = new SqliteCommand("INSERT INTO t1 VALUES ('c',0.3,1,'0')",_conn,t);
148 SqliteCommand c4 = new SqliteCommand("INSERT INTO t1 VALUES ('d',0.4,0,'1')",_conn,t);
153 c1.ExecuteNonQuery();
154 c2.ExecuteNonQuery();
155 c3.ExecuteNonQuery();
156 c4.ExecuteNonQuery();
162 throw new AssertionException("Sqlite Commands failed", e);
169 [ExpectedException(typeof(SqliteException))]
171 [ExpectedException(typeof(SqliteSyntaxException))]
173 public void InsertWithFailingTransaction()
176 SqliteTransaction t = _conn.BeginTransaction() as SqliteTransaction;
177 SqliteCommand c1 = new SqliteCommand("INSERT INTO t1 VALUES ('1','0','0','0')",_conn,t);
178 SqliteCommand c2 = new SqliteCommand("INSERT INTO t1 VALUES ('0','1','0','0')",_conn,t);
179 SqliteCommand c3 = new SqliteCommand("INSERT INTO t1 VALUES ('x',?,'x',?,'x',?,'x')",_conn,t);
180 SqliteCommand c4 = new SqliteCommand("INSERT INTO t1 VALUES ('0','0','0','1')",_conn,t);
185 c1.ExecuteNonQuery();
186 c2.ExecuteNonQuery();
187 c3.ExecuteNonQuery();
188 c4.ExecuteNonQuery();