| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
using System; |
| 7 |
using System.Data; |
| 8 |
using System.IO; |
| 9 |
using System.Text; |
| 10 |
using Mono.Data.Sqlite; |
| 11 |
using NUnit.Framework; |
| 12 |
|
| 13 |
namespace MonoTests.Mono.Data.Sqlite |
| 14 |
{ |
| 15 |
|
| 16 |
[TestFixture] |
| 17 |
public class SqliteCommandUnitTests |
| 18 |
{ |
| 19 |
string _uri; |
| 20 |
string _connectionString; |
| 21 |
SqliteConnection _conn; |
| 22 |
readonly static string stringvalue = "my keyboard is better than yours : äöüß"; |
| 23 |
|
| 24 |
public SqliteCommandUnitTests() |
| 25 |
{ |
| 26 |
} |
| 27 |
|
| 28 |
[SetUp] |
| 29 |
public void Create() |
| 30 |
{ |
| 31 |
_uri = Path.GetTempFileName (); |
| 32 |
_connectionString = "URI=file://" + _uri + ", version=3"; |
| 33 |
_conn = new SqliteConnection (_connectionString); |
| 34 |
|
| 35 |
try |
| 36 |
{ |
| 37 |
if(File.Exists(_uri)) |
| 38 |
{ |
| 39 |
_conn.Dispose(); |
| 40 |
|
| 41 |
|
| 42 |
File.Delete(_uri); |
| 43 |
|
| 44 |
} |
| 45 |
} |
| 46 |
catch(Exception e) |
| 47 |
{ |
| 48 |
throw e; |
| 49 |
} |
| 50 |
|
| 51 |
try |
| 52 |
{ |
| 53 |
using (SqliteCommand createCommand = new SqliteCommand("CREATE TABLE t1(t TEXT, f FLOAT, i INTEGER, b TEXT);", _conn)) |
| 54 |
using (SqliteCommand insertCommand = new SqliteCommand("INSERT INTO t1 (t, f, i, b ) VALUES('" + stringvalue + "',123,123,'123')", _conn)) |
| 55 |
{ |
| 56 |
_conn.Open(); |
| 57 |
createCommand.ExecuteNonQuery(); |
| 58 |
insertCommand.ExecuteNonQuery(); |
| 59 |
} |
| 60 |
} |
| 61 |
catch(Exception e) |
| 62 |
{ |
| 63 |
Console.WriteLine (e); |
| 64 |
throw new AssertionException("Create table failed",e); |
| 65 |
} |
| 66 |
finally |
| 67 |
{ |
| 68 |
_conn.Close(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
[TearDown] |
| 73 |
public void TearDown () |
| 74 |
{ |
| 75 |
if (File.Exists (_uri)) |
| 76 |
File.Delete (_uri); |
| 77 |
} |
| 78 |
|
| 79 |
[Test] |
| 80 |
public void Select() |
| 81 |
{ |
| 82 |
using (_conn) |
| 83 |
using (SqliteCommand simpleSelect = new SqliteCommand("SELECT * FROM t1; ", _conn)) |
| 84 |
{ |
| 85 |
_conn.Open(); |
| 86 |
using (SqliteDataReader dr = simpleSelect.ExecuteReader()) |
| 87 |
{ |
| 88 |
while (dr.Read()) |
| 89 |
{ |
| 90 |
string test = dr[0].ToString(); |
| 91 |
Assert.AreEqual(dr["T"], stringvalue); |
| 92 |
Assert.AreEqual(dr["F"], 123); |
| 93 |
Assert.AreEqual(dr["I"], 123); |
| 94 |
Assert.AreEqual(dr["B"], "123"); |
| 95 |
} |
| 96 |
Assert.IsTrue(dr.FieldCount>0); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
[Test] |
| 102 |
public void Delete() |
| 103 |
{ |
| 104 |
using (_conn) |
| 105 |
using (SqliteCommand insCmd = new SqliteCommand("INSERT INTO t1 VALUES ('todelete',0.1,0,'')", _conn)) |
| 106 |
using (SqliteCommand delCmd = new SqliteCommand("DELETE FROM t1 WHERE t = 'todelete'", _conn)) |
| 107 |
{ |
| 108 |
_conn.Open(); |
| 109 |
int insReturn = insCmd.ExecuteNonQuery(); |
| 110 |
int delReturn = delCmd.ExecuteNonQuery(); |
| 111 |
|
| 112 |
Assert.IsTrue(insReturn == delReturn); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
[Test] |
| 117 |
public void Insert() |
| 118 |
{ |
| 119 |
using (_conn) |
| 120 |
using (SqliteCommand insCmd = new SqliteCommand("INSERT INTO t1 VALUES ('inserted',0.1,0,'')", _conn)) |
| 121 |
{ |
| 122 |
_conn.Open(); |
| 123 |
int insReturn = insCmd.ExecuteNonQuery(); |
| 124 |
Assert.IsTrue(insReturn == 1); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
[Test] |
| 129 |
public void Update() |
| 130 |
{ |
| 131 |
using (_conn) |
| 132 |
using (SqliteCommand insCmd = new SqliteCommand("INSERT INTO t1 VALUES ('toupdate',0.1,0,'')", _conn)) |
| 133 |
using (SqliteCommand updCmd = new SqliteCommand("UPDATE t1 SET t = 'updated' ,f = 2.0, i = 2, b = '' WHERE t = 'toupdate'", _conn)) |
| 134 |
{ |
| 135 |
_conn.Open(); |
| 136 |
insCmd.ExecuteNonQuery(); |
| 137 |
Assert.IsTrue(updCmd.ExecuteNonQuery() == 1); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
[Test] |
| 143 |
public void ScalarReturn() |
| 144 |
{ |
| 145 |
|
| 146 |
using (_conn) |
| 147 |
using (SqliteCommand cmd = new SqliteCommand("SELECT COUNT(*) FROM t1 WHERE t LIKE '%äöüß'", _conn)) |
| 148 |
{ |
| 149 |
_conn.Open(); |
| 150 |
Assert.AreEqual(1, Convert.ToInt32(cmd.ExecuteScalar())); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
[Test] |
| 155 |
public void InsertWithTransaction() |
| 156 |
{ |
| 157 |
_conn.Open(); |
| 158 |
using (_conn) |
| 159 |
using (SqliteTransaction t = _conn.BeginTransaction() as SqliteTransaction) |
| 160 |
using (SqliteCommand c1 = new SqliteCommand("INSERT INTO t1 VALUES ('a',0.1,0,'0')", _conn, t)) |
| 161 |
using (SqliteCommand c2 = new SqliteCommand("INSERT INTO t1 VALUES ('b',1.2,0,'0')", _conn, t)) |
| 162 |
using (SqliteCommand c3 = new SqliteCommand("INSERT INTO t1 VALUES ('c',0.3,1,'0')", _conn, t)) |
| 163 |
using (SqliteCommand c4 = new SqliteCommand("INSERT INTO t1 VALUES ('d',0.4,0,'1')", _conn, t)) |
| 164 |
{ |
| 165 |
try |
| 166 |
{ |
| 167 |
c1.ExecuteNonQuery(); |
| 168 |
c2.ExecuteNonQuery(); |
| 169 |
c3.ExecuteNonQuery(); |
| 170 |
c4.ExecuteNonQuery(); |
| 171 |
t.Commit(); |
| 172 |
} |
| 173 |
catch(Exception e) |
| 174 |
{ |
| 175 |
t.Rollback(); |
| 176 |
throw new AssertionException("Sqlite Commands failed", e); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
[Test] |
| 182 |
[ExpectedException(typeof(SqliteException))] |
| 183 |
public void InsertWithFailingTransaction() |
| 184 |
{ |
| 185 |
_conn.Open(); |
| 186 |
using (_conn) |
| 187 |
using (SqliteTransaction t = _conn.BeginTransaction() as SqliteTransaction) |
| 188 |
using (SqliteCommand c1 = new SqliteCommand("INSERT INTO t1 VALUES ('1','0','0','0')", _conn, t)) |
| 189 |
using (SqliteCommand c2 = new SqliteCommand("INSERT INTO t1 VALUES ('0','1','0','0')", _conn, t)) |
| 190 |
using (SqliteCommand c3 = new SqliteCommand("INSERT INTO t1 VALUES ('x',?,'x',?,'x',?,'x')", _conn, t)) |
| 191 |
using (SqliteCommand c4 = new SqliteCommand("INSERT INTO t1 VALUES ('0','0','0','1')", _conn, t)) |
| 192 |
{ |
| 193 |
try |
| 194 |
{ |
| 195 |
c1.ExecuteNonQuery(); |
| 196 |
c2.ExecuteNonQuery(); |
| 197 |
c3.ExecuteNonQuery(); |
| 198 |
c4.ExecuteNonQuery(); |
| 199 |
t.Commit(); |
| 200 |
} |
| 201 |
catch(Exception e) |
| 202 |
{ |
| 203 |
t.Rollback(); |
| 204 |
throw e; |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
} |