| 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 |
[TestFixture] |
| 16 |
public class SqliteDataAdapterUnitTests |
| 17 |
{ |
| 18 |
string _uri; |
| 19 |
string _connectionString; |
| 20 |
SqliteConnection _conn; |
| 21 |
|
| 22 |
[SetUp] |
| 23 |
public void SetUp () |
| 24 |
{ |
| 25 |
_uri = Path.GetTempFileName (); |
| 26 |
_connectionString = "URI=file://" + _uri + ", version=3"; |
| 27 |
_conn = new SqliteConnection (_connectionString); |
| 28 |
} |
| 29 |
|
| 30 |
[TearDown] |
| 31 |
public void TearDown () |
| 32 |
{ |
| 33 |
if (File.Exists (_uri)) |
| 34 |
File.Delete (_uri); |
| 35 |
} |
| 36 |
|
| 37 |
SqliteDataAdapter PrepareDataAdapter() |
| 38 |
{ |
| 39 |
SqliteCommand select = new SqliteCommand("SELECT t, f, i, b FROM t1",_conn); |
| 40 |
SqliteCommand update = new SqliteCommand("UPDATE t1 SET t = :textP, f = :floatP, i = :integerP, n=:blobP WHERE t = :textP "); |
| 41 |
update.Connection=_conn; |
| 42 |
SqliteCommand delete = new SqliteCommand("DELETE FROM t1 WHERE t = :textP"); |
| 43 |
delete.Connection=_conn; |
| 44 |
SqliteCommand insert = new SqliteCommand("INSERT INTO t1 (t, f, i, b ) VALUES(:textP,:floatP,:integerP,:blobP)"); |
| 45 |
insert.Connection=_conn; |
| 46 |
SqliteDataAdapter custDA = new SqliteDataAdapter(select); |
| 47 |
|
| 48 |
SqliteParameter textP = new SqliteParameter(); |
| 49 |
textP.ParameterName = "textP"; |
| 50 |
textP.SourceColumn = "t"; |
| 51 |
|
| 52 |
SqliteParameter floatP = new SqliteParameter(); |
| 53 |
floatP.ParameterName = "floatP"; |
| 54 |
floatP.SourceColumn = "f"; |
| 55 |
|
| 56 |
SqliteParameter integerP = new SqliteParameter(); |
| 57 |
integerP.ParameterName ="integerP"; |
| 58 |
integerP.SourceColumn = "i"; |
| 59 |
|
| 60 |
SqliteParameter blobP = new SqliteParameter(); |
| 61 |
blobP.ParameterName = "blobP"; |
| 62 |
blobP.SourceColumn = "b"; |
| 63 |
|
| 64 |
update.Parameters.Add(textP); |
| 65 |
update.Parameters.Add(floatP); |
| 66 |
update.Parameters.Add(integerP); |
| 67 |
update.Parameters.Add(blobP); |
| 68 |
|
| 69 |
delete.Parameters.Add(textP); |
| 70 |
|
| 71 |
insert.Parameters.Add(textP); |
| 72 |
insert.Parameters.Add(floatP); |
| 73 |
insert.Parameters.Add(integerP); |
| 74 |
insert.Parameters.Add(blobP); |
| 75 |
|
| 76 |
custDA.UpdateCommand = update; |
| 77 |
custDA.DeleteCommand = delete; |
| 78 |
custDA.InsertCommand = insert; |
| 79 |
|
| 80 |
return custDA; |
| 81 |
} |
| 82 |
|
| 83 |
[Test] |
| 84 |
[Category ("NotWorking")] |
| 85 |
public void GetSchemaTable() |
| 86 |
{ |
| 87 |
_conn.ConnectionString = _connectionString; |
| 88 |
SqliteDataReader reader = null; |
| 89 |
using (_conn) |
| 90 |
{ |
| 91 |
_conn.Open (); |
| 92 |
SqliteCommand cmd = (SqliteCommand) _conn.CreateCommand (); |
| 93 |
cmd.CommandText = "select * from t1"; |
| 94 |
reader = cmd.ExecuteReader (); |
| 95 |
try |
| 96 |
{ |
| 97 |
DataTable dt = reader.GetSchemaTable (); |
| 98 |
Assert.IsNotNull (dt, "#GS1 should return valid table"); |
| 99 |
Assert.IsTrue (dt.Rows.Count > 0, "#GS2 should return with rows ;-)"); |
| 100 |
} |
| 101 |
finally |
| 102 |
{ |
| 103 |
if (reader != null && !reader.IsClosed) |
| 104 |
reader.Close (); |
| 105 |
_conn.Close (); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
[Test] |
| 111 |
[Category ("NotWorking")] |
| 112 |
public void DataAdapterRandomValues() |
| 113 |
{ |
| 114 |
SqliteDataAdapter da = PrepareDataAdapter(); |
| 115 |
DataSet ds = new DataSet(); |
| 116 |
int i = 0; |
| 117 |
Random random = new Random(); |
| 118 |
using(_conn) |
| 119 |
{ |
| 120 |
_conn.Open(); |
| 121 |
da.Fill(ds); |
| 122 |
for(; i<300;i++) |
| 123 |
{ |
| 124 |
DataRow dr = ds.Tables[0].NewRow(); |
| 125 |
|
| 126 |
foreach(DataColumn dc in ds.Tables[0].Columns) |
| 127 |
{ |
| 128 |
switch(dc.DataType.Name) |
| 129 |
{ |
| 130 |
case "String": |
| 131 |
{ |
| 132 |
int ml=0; |
| 133 |
if(dc.MaxLength!=-1) |
| 134 |
{ |
| 135 |
ml=dc.MaxLength; |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
ml=256; |
| 140 |
} |
| 141 |
StringBuilder builder = new StringBuilder(ml); |
| 142 |
for (int k=0; k < random.Next(ml); k++) |
| 143 |
{ |
| 144 |
builder.Append((char)random.Next(65536)); |
| 145 |
|
| 146 |
} |
| 147 |
string curs = builder.ToString(); |
| 148 |
dr[dc]=curs; |
| 149 |
break; |
| 150 |
} |
| 151 |
|
| 152 |
case "Int32": |
| 153 |
{ |
| 154 |
dr[dc]=random.Next(65536); |
| 155 |
break; |
| 156 |
} |
| 157 |
|
| 158 |
case "Int64": |
| 159 |
{ |
| 160 |
dr[dc]=Convert.ToInt64(random.Next(65536)); |
| 161 |
break; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
ds.Tables[0].Rows.Add(dr); |
| 166 |
} |
| 167 |
int res = da.Update(ds); |
| 168 |
Assert.AreEqual(i,res); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |