Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
2009-01-19 Zoltan Varga <vargaz@gmail.com>
* SqliteCommandUnitTests.cs: Fix test ordering issues with the new nunit
version by marking Create () as [SetUp] instead of [Test].
2008-08-04 Zoltan Varga <vargaz@gmail.com>
* SqliteParameterUnitTests.cs: Disable the InsertRandomValuesWithParameter
test as it fails randomly.
2008-07-15 Marek Habersack <mhabersack@novell.com>
* SqliteParameterUnitTests.cs: adjust the blob value comparison
test for the 2.0 profile.
* SqliteCommandUnitTests.cs, SqliteExceptionUnitTests.cs: change
the expected exception type for the 2.0 profile.
* SqliteConnectionTest.cs: add tests for the 2.0 profile, disable
tests which are incorrect for this profile.
2007-01-16 Nagappan A <anagappan@novell.com>
* SqliteDataAdapterUnitTests.cs (GetSchemaTable): Type casted to
fix compiler error.
* SqliteDataReaderTest.cs (GetSchemaTableTest)
(TypeOfNullInResultTest): Type casted to fix compiler error.
2006-03-11 Joshua Tauberer <tauberer@for.net>
* SqliteTest.cs: Test ANSI characters.
2006-03-07 Kamil Skalski <nazgul@nemerle.org>
* SqliteDataReaderTest.cs: Add test for getting field type of null
value
2006-01-29 Joshua Tauberer <tauberer@for.net>
* Added tests from Thomas Zoechling <thomas.zoechling@gmx.at>.
2006-01-02 Joshua Tauberer <tauberer@for.net>
* SqliteTest.cs: Cleaned up and added new stuff.
2005-05-20 Sureshkumar T <tsureshkumar@novell.com>
* test.sql: script to create a test db.
* SqliteConnectionTest.cs: Added. Tests for SqliteConnection.
* SqliteDataReaderTest.cs: Added. Tests for SqliteDataReader.

View File

@@ -0,0 +1,202 @@
// SqliteDataAdapterUnitTests.cs - NUnit Test Cases for Mono.Data.Sqlite.SqliteDataAdapter
//
// Author(s): Thomas Zoechling <thomas.zoechling@gmx.at>
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 SqliteCommandUnitTests
{
readonly static string _uri = Path.Combine (Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SqliteTest.db");
readonly static string _connectionString = "URI=file://" + _uri + ", version=3";
static SqliteConnection _conn = new SqliteConnection (_connectionString);
readonly static string stringvalue = "my keyboard is better than yours : äöüß";
public SqliteCommandUnitTests()
{
}
[SetUp]
public void Create()
{
try
{
if(File.Exists(_uri))
{
_conn.Dispose();
// We want to start with a fresh db for each full run
// The database is created on the first open()
File.Delete(_uri);
}
}
catch(Exception e)
{
throw e;
}
try
{
using (SqliteCommand createCommand = new SqliteCommand("CREATE TABLE t1(t TEXT, f FLOAT, i INTEGER, b TEXT);", _conn))
using (SqliteCommand insertCommand = new SqliteCommand("INSERT INTO t1 (t, f, i, b ) VALUES('" + stringvalue + "',123,123,'123')", _conn))
{
_conn.Open();
createCommand.ExecuteNonQuery();
insertCommand.ExecuteNonQuery();
}
}
catch(Exception e)
{
Console.WriteLine (e);
throw new AssertionException("Create table failed",e);
}
finally
{
_conn.Close();
}
}
[Test]
public void Select()
{
using (_conn)
using (SqliteCommand simpleSelect = new SqliteCommand("SELECT * FROM t1; ", _conn)) // check trailing spaces
{
_conn.Open();
using (SqliteDataReader dr = simpleSelect.ExecuteReader())
{
while (dr.Read())
{
string test = dr[0].ToString();
Assert.AreEqual(dr["T"], stringvalue); // also checks case-insensitive column
Assert.AreEqual(dr["F"], 123);
Assert.AreEqual(dr["I"], 123);
Assert.AreEqual(dr["B"], "123");
}
Assert.IsTrue(dr.FieldCount>0);
}
}
}
[Test]
public void Delete()
{
using (_conn)
using (SqliteCommand insCmd = new SqliteCommand("INSERT INTO t1 VALUES ('todelete',0.1,0,'')", _conn))
using (SqliteCommand delCmd = new SqliteCommand("DELETE FROM t1 WHERE t = 'todelete'", _conn))
{
_conn.Open();
int insReturn = insCmd.ExecuteNonQuery();
int delReturn = delCmd.ExecuteNonQuery();
Assert.IsTrue(insReturn == delReturn);
}
}
[Test]
public void Insert()
{
using (_conn)
using (SqliteCommand insCmd = new SqliteCommand("INSERT INTO t1 VALUES ('inserted',0.1,0,'')", _conn))
{
_conn.Open();
int insReturn = insCmd.ExecuteNonQuery();
Assert.IsTrue(insReturn == 1);
}
}
[Test]
public void Update()
{
using (_conn)
using (SqliteCommand insCmd = new SqliteCommand("INSERT INTO t1 VALUES ('toupdate',0.1,0,'')", _conn))
using (SqliteCommand updCmd = new SqliteCommand("UPDATE t1 SET t = 'updated' ,f = 2.0, i = 2, b = '' WHERE t = 'toupdate'", _conn))
{
_conn.Open();
insCmd.ExecuteNonQuery();
Assert.IsTrue(updCmd.ExecuteNonQuery() == 1);
}
}
[Test]
public void ScalarReturn()
{
// This should return the 1 line that got inserted in CreateTable() Test
using (_conn)
using (SqliteCommand cmd = new SqliteCommand("SELECT COUNT(*) FROM t1 WHERE t LIKE '%äöüß'", _conn))
{
_conn.Open();
Assert.AreEqual(1, Convert.ToInt32(cmd.ExecuteScalar()));
}
}
[Test]
public void InsertWithTransaction()
{
_conn.Open();
using (_conn)
using (SqliteTransaction t = _conn.BeginTransaction() as SqliteTransaction)
using (SqliteCommand c1 = new SqliteCommand("INSERT INTO t1 VALUES ('a',0.1,0,'0')", _conn, t))
using (SqliteCommand c2 = new SqliteCommand("INSERT INTO t1 VALUES ('b',1.2,0,'0')", _conn, t))
using (SqliteCommand c3 = new SqliteCommand("INSERT INTO t1 VALUES ('c',0.3,1,'0')", _conn, t))
using (SqliteCommand c4 = new SqliteCommand("INSERT INTO t1 VALUES ('d',0.4,0,'1')", _conn, t))
{
try
{
c1.ExecuteNonQuery();
c2.ExecuteNonQuery();
c3.ExecuteNonQuery();
c4.ExecuteNonQuery();
t.Commit();
}
catch(Exception e)
{
t.Rollback();
throw new AssertionException("Sqlite Commands failed", e);
}
}
}
[Test]
#if NET_2_0
[ExpectedException(typeof(SqliteException))]
#else
[ExpectedException(typeof(SqliteSyntaxException))]
#endif
public void InsertWithFailingTransaction()
{
_conn.Open();
using (_conn)
using (SqliteTransaction t = _conn.BeginTransaction() as SqliteTransaction)
using (SqliteCommand c1 = new SqliteCommand("INSERT INTO t1 VALUES ('1','0','0','0')", _conn, t))
using (SqliteCommand c2 = new SqliteCommand("INSERT INTO t1 VALUES ('0','1','0','0')", _conn, t))
using (SqliteCommand c3 = new SqliteCommand("INSERT INTO t1 VALUES ('x',?,'x',?,'x',?,'x')", _conn, t))
using (SqliteCommand c4 = new SqliteCommand("INSERT INTO t1 VALUES ('0','0','0','1')", _conn, t))
{
try
{
c1.ExecuteNonQuery();
c2.ExecuteNonQuery();
c3.ExecuteNonQuery();
c4.ExecuteNonQuery();
t.Commit();
}
catch(Exception e)
{
t.Rollback();
throw e;
}
}
}
}
}

View File

@@ -0,0 +1,116 @@
// SqliteConnectionTest.cs - NUnit Test Cases for SqliteConnection
//
// Authors:
// Sureshkumar T <tsureshkumar@novell.com>
//
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Data;
using System.IO;
using Mono.Data.Sqlite;
using NUnit.Framework;
namespace MonoTests.Mono.Data.Sqlite
{
[TestFixture]
public class SqliteConnectionTest
{
readonly static string _uri = Path.Combine (Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "test.db");
readonly static string _connectionString = "URI=file://" + _uri + ", version=3";
SqliteConnection _conn = new SqliteConnection ();
#if NET_2_0
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConnectionStringTest_Null ()
{
_conn.ConnectionString = null;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ConnectionStringTest_MustBeClosed ()
{
_conn.ConnectionString = _connectionString;
try {
_conn.Open ();
_conn.ConnectionString = _connectionString;
} finally {
_conn.Close ();
}
}
#else
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ConnectionStringTest_Empty ()
{
_conn.ConnectionString = "";
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ConnectionStringTest_NoURI ()
{
_conn.ConnectionString = "version=3";
}
// In 2.0 _conn.Database always returns "main"
[Test]
public void ConnectionStringTest_IgnoreSpacesAndTrim ()
{
_conn.ConnectionString = "URI=file://xyz , ,,, ,, version=3";
Assert.AreEqual ("xyz", _conn.Database, "#1 file path is wrong");
}
#endif
// behavior has changed, I guess
//[Test]
[Ignore ("opening a connection should not create db! though, leave for now")]
public void OpenTest ()
{
try {
_conn.ConnectionString = _connectionString;
_conn.Open ();
Assert.AreEqual (ConnectionState.Open, _conn.State, "#1 not opened");
_conn.Close ();
// negative test: try opening a non-existent file
_conn.ConnectionString = "URI=file://abcdefgh.db, version=3";
try {
_conn.Open ();
Assert.Fail ("#1 should have failed on opening a non-existent db");
} catch (ArgumentException e) {Console.WriteLine (e);}
} finally {
if (_conn != null && _conn.State != ConnectionState.Closed)
_conn.Close ();
}
}
}
}

View File

@@ -0,0 +1,157 @@
// SqliteDataAdapterUnitTests.cs - NUnit Test Cases for Mono.Data.Sqlite.SqliteDataAdapter
//
// Author(s): Thomas Zoechling <thomas.zoechling@gmx.at>
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 SqliteDataAdapterUnitTests
{
readonly static string _uri = "SqliteTest.db";
readonly static string _connectionString = "URI=file://" + _uri + ", version=3";
static SqliteConnection _conn = new SqliteConnection (_connectionString);
static SqliteDataAdapter PrepareDataAdapter()
{
SqliteCommand select = new SqliteCommand("SELECT t, f, i, b FROM t1",_conn);
SqliteCommand update = new SqliteCommand("UPDATE t1 SET t = :textP, f = :floatP, i = :integerP, n=:blobP WHERE t = :textP ");
update.Connection=_conn;
SqliteCommand delete = new SqliteCommand("DELETE FROM t1 WHERE t = :textP");
delete.Connection=_conn;
SqliteCommand insert = new SqliteCommand("INSERT INTO t1 (t, f, i, b ) VALUES(:textP,:floatP,:integerP,:blobP)");
insert.Connection=_conn;
SqliteDataAdapter custDA = new SqliteDataAdapter(select);
SqliteParameter textP = new SqliteParameter();
textP.ParameterName = "textP";
textP.SourceColumn = "t";
SqliteParameter floatP = new SqliteParameter();
floatP.ParameterName = "floatP";
floatP.SourceColumn = "f";
SqliteParameter integerP = new SqliteParameter();
integerP.ParameterName ="integerP";
integerP.SourceColumn = "i";
SqliteParameter blobP = new SqliteParameter();
blobP.ParameterName = "blobP";
blobP.SourceColumn = "b";
update.Parameters.Add(textP);
update.Parameters.Add(floatP);
update.Parameters.Add(integerP);
update.Parameters.Add(blobP);
delete.Parameters.Add(textP);
insert.Parameters.Add(textP);
insert.Parameters.Add(floatP);
insert.Parameters.Add(integerP);
insert.Parameters.Add(blobP);
custDA.UpdateCommand = update;
custDA.DeleteCommand = delete;
custDA.InsertCommand = insert;
return custDA;
}
[Test]
[Category ("NotWorking")]
public void GetSchemaTable()
{
_conn.ConnectionString = _connectionString;
SqliteDataReader reader = null;
using (_conn)
{
_conn.Open ();
SqliteCommand cmd = (SqliteCommand) _conn.CreateCommand ();
cmd.CommandText = "select * from t1";
reader = cmd.ExecuteReader ();
try
{
DataTable dt = reader.GetSchemaTable ();
Assert.IsNotNull (dt, "#GS1 should return valid table");
Assert.IsTrue (dt.Rows.Count > 0, "#GS2 should return with rows ;-)");
}
finally
{
if (reader != null && !reader.IsClosed)
reader.Close ();
_conn.Close ();
}
}
}
[Test]
[Category ("NotWorking")]
public void DataAdapterRandomValues()
{
SqliteDataAdapter da = PrepareDataAdapter();
DataSet ds = new DataSet();
int i = 0;
Random random = new Random();
using(_conn)
{
_conn.Open();
da.Fill(ds);
for(; i<300;i++)
{
DataRow dr = ds.Tables[0].NewRow();
foreach(DataColumn dc in ds.Tables[0].Columns)
{
switch(dc.DataType.Name)
{
case "String":
{
int ml=0;
if(dc.MaxLength!=-1)
{
ml=dc.MaxLength;
}
else
{
ml=256;
}
StringBuilder builder = new StringBuilder(ml);
for (int k=0; k < random.Next(ml); k++)
{
builder.Append((char)random.Next(65536));
}
string curs = builder.ToString();
dr[dc]=curs;
break;
}
case "Int32":
{
dr[dc]=random.Next(65536);
break;
}
case "Int64":
{
dr[dc]=Convert.ToInt64(random.Next(65536));
break;
}
}
}
ds.Tables[0].Rows.Add(dr);
}
int res = da.Update(ds);
Assert.AreEqual(i,res);
}
}
}
}

View File

@@ -0,0 +1,303 @@
// SqliteDataReaderTest.cs - NUnit Test Cases for SqliteDataReader
//
// Authors:
// Sureshkumar T <tsureshkumar@novell.com>
//
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using Mono.Data.Sqlite;
using NUnit.Framework;
namespace MonoTests.Mono.Data.Sqlite
{
[TestFixture]
public class SqliteDataReaderTest
{
readonly static string _uri = "./test.db";
readonly static string _connectionString = "URI=file://" + _uri + ", version=3";
SqliteConnection _conn = new SqliteConnection ();
[TestFixtureSetUp]
public void FixtureSetUp ()
{
if (! File.Exists (_uri) || new FileInfo (_uri).Length == 0) {
// ignore all tests
Assert.Ignore ("#000 ignoring all fixtures. No database present");
}
}
[Test]
[Category ("NotWorking")]
public void GetSchemaTableTest ()
{
_conn.ConnectionString = _connectionString;
SqliteDataReader reader = null;
using (_conn) {
_conn.Open ();
SqliteCommand cmd = (SqliteCommand) _conn.CreateCommand ();
cmd.CommandText = "select * from test";
reader = cmd.ExecuteReader ();
try {
DataTable dt = reader.GetSchemaTable ();
Assert.IsNotNull (dt, "#GS1 should return valid table");
Assert.IsTrue (dt.Rows.Count > 0, "#GS2 should return with rows ;-)");
} finally {
if (reader != null && !reader.IsClosed)
reader.Close ();
_conn.Close ();
}
}
}
[Test]
public void TypeOfNullInResultTest ()
{
_conn.ConnectionString = _connectionString;
SqliteDataReader reader = null;
using (_conn) {
_conn.Open ();
SqliteCommand cmd = (SqliteCommand) _conn.CreateCommand ();
cmd.CommandText = "select null from test";
reader = cmd.ExecuteReader ();
try {
Assert.IsTrue (reader.Read());
Assert.IsNotNull (reader.GetFieldType (0));
} finally {
if (reader != null && !reader.IsClosed)
reader.Close ();
_conn.Close ();
}
}
}
[Test]
public void TimestampTest ()
{
_conn.ConnectionString = _connectionString;
using (_conn) {
_conn.Open ();
var cmd = (SqliteCommand) _conn.CreateCommand ();
cmd.CommandText = @"CREATE TABLE IF NOT EXISTS TestNullableDateTime (nullable TIMESTAMP NULL, dummy int); INSERT INTO TestNullableDateTime (nullable, dummy) VALUES (124123, 2);";
cmd.ExecuteNonQuery ();
var query = "SELECT * FROM TestNullableDateTime;";
cmd = (SqliteCommand) _conn.CreateCommand ();
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
using (var reader = cmd.ExecuteReader ()) {
try {
var dt = reader ["nullable"];
Assert.Fail ("Expected: FormatException");
} catch (FormatException ex) {
// expected this one
}
}
}
_conn.ConnectionString = _connectionString + ",DateTimeFormat=UnixEpoch";
using (_conn) {
_conn.Open ();
var cmd = (SqliteCommand) _conn.CreateCommand ();
cmd.CommandText = @"CREATE TABLE IF NOT EXISTS TestNullableDateTime (nullable TIMESTAMP NULL, dummy int); INSERT INTO TestNullableDateTime (nullable, dummy) VALUES (124123, 2);";
cmd.ExecuteNonQuery ();
var query = "SELECT * FROM TestNullableDateTime;";
cmd = (SqliteCommand) _conn.CreateCommand ();
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
using (var reader = cmd.ExecuteReader ()) {
// this should succeed now
var dt = reader ["nullable"];
}
}
}
[Test]
public void CloseConnectionTest ()
{
// When this test fails it may confuse nunit a bit, causing it to show strange
// exceptions, since it leaks file handles (and nunit tries to open files,
// which it doesn't expect to fail).
// For the same reason a lot of other tests will fail when this one fails.
_conn.ConnectionString = _connectionString;
using (_conn) {
_conn.Open ();
using (var cmd = (SqliteCommand) _conn.CreateCommand ()) {
cmd.CommandText = @"CREATE TABLE IF NOT EXISTS TestNullableDateTime (nullable TIMESTAMP NULL, dummy int); INSERT INTO TestNullableDateTime (nullable, dummy) VALUES (124123, 2);";
cmd.ExecuteNonQuery ();
}
}
for (int i = 0; i < 1000; i++) {
_conn.ConnectionString = _connectionString;
using (_conn) {
_conn.Open ();
using (var cmd = (SqliteCommand) _conn.CreateCommand ()) {
cmd.CommandText = "SELECT * FROM TestNullableDateTime;";
cmd.CommandType = CommandType.Text;
using (var reader = cmd.ExecuteReader (CommandBehavior.CloseConnection)) {
reader.Read ();
}
}
}
}
}
void AddParameter (System.Data.Common.DbCommand cm, string name, object value)
{
var param = cm.CreateParameter ();
param.ParameterName = ":" + name;
param.Value = value;
cm.Parameters.Add (param);
}
class D
{
public string Sql;
public Type Expected;
public object Value;
}
[Test]
public void TestDataTypes ()
{
SqliteParameter param;
var data = new List<D> ()
{
new D () { Sql = "DATETIME", Expected = typeof (DateTime), Value = DateTime.Now },
new D () { Sql = "GUIDBLOB NOT NULL", Expected = typeof (Guid), Value = new byte [] { 3, 14, 15 } },
new D () { Sql = "BOOLEAN", Expected = typeof (bool), Value = true },
new D () { Sql = "INT32", Expected = typeof (long), Value = 1 },
new D () { Sql = "INT32 NOT NULL", Expected = typeof (long), Value = 2 },
new D () { Sql = "UINT1", Expected = typeof (long), Value = 3 },
// these map to the INTEGER affinity
new D () { Sql = "INT", Expected = typeof (int), Value = 4 },
new D () { Sql = "INTEGER", Expected = typeof (long), Value = 5 },
new D () { Sql = "TINYINT", Expected = typeof (byte), Value = 6 },
new D () { Sql = "SMALLINT", Expected = typeof (short), Value = 7 },
new D () { Sql = "MEDIUMINT", Expected = typeof (long), Value = 8 },
new D () { Sql = "BIGINT", Expected = typeof (long), Value = 9 },
new D () { Sql = "UNSIGNED BIG INT", Expected = typeof (long), Value = 0 },
new D () { Sql = "INT2", Expected = typeof (long), Value = 1 },
new D () { Sql = "INT4", Expected = typeof (long), Value = 2 },
// these map to the TEXT affinity
new D () { Sql = "CHARACTER(20)", Expected = typeof (string), Value = "a" },
new D () { Sql = "VARCHAR(255)", Expected = typeof (string), Value = "b" },
new D () { Sql = "VARYING CHARACTER(255)", Expected = typeof (string), Value = "c" },
new D () { Sql = "NCHAR(55)", Expected = typeof (string), Value = "d" },
new D () { Sql = "NATIVE CHARACTER(70)", Expected = typeof (string), Value = "e" },
new D () { Sql = "NVARCHAR(100)", Expected = typeof (string), Value = "f" },
new D () { Sql = "TEXT", Expected = typeof (string), Value = "g" },
new D () { Sql = "CLOB", Expected = typeof (string), Value = "h" },
// these map to the NONE affinity
new D () { Sql = "BLOB", Expected = typeof (byte[]), Value = new byte [] { 3, 14, 15 } },
new D () { Sql = "", Expected = typeof (object), Value = null },
// these map to the REAL affinity
new D () { Sql = "REAL", Expected = typeof (float), Value = 3.2 },
new D () { Sql = "DOUBLE", Expected = typeof (double), Value = 4.2 },
new D () { Sql = "DOUBLE PRECISION", Expected = typeof (double), Value = 5.2 },
new D () { Sql = "FLOAT", Expected = typeof (double), Value = 6.2 },
// these map to the NUMERIC affinity
new D () { Sql = "NUMERIC", Expected = typeof (decimal), Value = 3.2 },
new D () { Sql = "DECIMAL(10,5)", Expected = typeof (decimal), Value = null },
new D () { Sql = "BOOLEAN", Expected = typeof (bool), Value = true },
new D () { Sql = "DATE", Expected = typeof (DateTime), Value = DateTime.Now },
new D () { Sql = "DATETIME", Expected = typeof (DateTime), Value = DateTime.Now },
};
_conn.ConnectionString = _connectionString;
using (_conn) {
_conn.Open();
using (var cm = _conn.CreateCommand ()) {
var sql = new StringBuilder ();
var args = new StringBuilder ();
var vals = new StringBuilder ();
sql.AppendLine ("DROP TABLE TEST;");
sql.Append ("CREATE TABLE TEST (");
bool comma = false;
for (int i = 0; i < data.Count; i++) {
if (comma) {
sql.Append (", ");
args.Append (", ");
vals.Append (", ");
}
comma = true;
sql.AppendFormat ("F{0} {1}", i, data [i].Sql);
args.AppendFormat ("F{0}", i);
vals.AppendFormat (":F{0}", i);
AddParameter (cm, "F" + i.ToString (), data [i].Value);
}
sql.AppendLine (");");
sql.Append ("INSERT INTO TEST (");
sql.Append (args.ToString ());
sql.Append (") VALUES (");
sql.Append (vals.ToString ());
sql.Append (");");
cm.CommandText = sql.ToString ();
cm.ExecuteNonQuery ();
}
using (var cm = _conn.CreateCommand ()) {
cm.CommandText = "SELECT * FROM TEST";
using (var dr = cm.ExecuteReader ()) {
dr.Read ();
for (int i = 0; i < data.Count; i++) {
string tn = data [i].Sql.Replace (" NOT NULL", "");
int index = dr.GetOrdinal ("F" + i.ToString ());
Assert.AreEqual (tn, dr.GetDataTypeName (index), "F" + i.ToString () + " (" + data [i].Sql + ")");
Assert.AreEqual (data [i].Expected.FullName, dr.GetFieldType (index).ToString (), "F" + i.ToString () + " (" + data [i].Sql + ")");
}
}
}
}
}
}
}

View File

@@ -0,0 +1,43 @@
// SqliteExceptionUnitTests.cs - NUnit Test Cases for Mono.Data.Sqlite.SqliteExceptions
//
// Author(s): Thomas Zoechling <thomas.zoechling@gmx.at>
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 SqliteExceptionUnitTests
{
readonly static string _uri = "SqliteTest.db";
readonly static string _connectionString = "URI=file://" + _uri + ", version=3";
static SqliteConnection _conn = new SqliteConnection (_connectionString);
public SqliteExceptionUnitTests()
{
}
[Test]
#if NET_2_0
[ExpectedException(typeof(SqliteException))]
#else
[ExpectedException(typeof(SqliteSyntaxException))]
#endif
public void WrongSyntax()
{
SqliteCommand insertCmd = new SqliteCommand("INSERT INTO t1 VALUES (,')",_conn);
using(_conn)
{
_conn.Open();
int res = insertCmd.ExecuteNonQuery();
Assert.AreEqual(res,1);
}
}
}
}

View File

@@ -0,0 +1,47 @@
// SqliteFunctionTests.cs - NUnit Test Cases for SqliteFunction
//
// Authors:
// Rolf Bjarne Kvinge <rolf@xamarin.com>
//
//
// Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
//
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 SqliteFunctionTest
{
readonly static string uri = Path.Combine (Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "test.db");
[Test]
public void CollationTest()
{
var builder = new SqliteConnectionStringBuilder();
builder.DataSource = uri;
var connectionString = builder.ToString();
using (var connection = new SqliteConnection (connectionString)) {
connection.Open ();
connection.Close ();
}
}
[SqliteFunction(Name = "TestCollation", FuncType = FunctionType.Collation)]
public class TestCollation : SqliteFunction
{
public override int Compare (string param1, string param2)
{
return string.Compare (param1, param2);
}
}
}
}

View File

@@ -0,0 +1,93 @@
// SqliteParameterUnitTests.cs - NUnit Test Cases for Mono.Data.Sqlite.SqliteParameter
//
// Author(s): Thomas Zoechling <thomas.zoechling@gmx.at>
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 SqliteParameterUnitTests
{
readonly static string _uri = "SqliteTest.db";
readonly static string _connectionString = "URI=file://" + _uri + ", version=3";
static SqliteConnection _conn = new SqliteConnection (_connectionString);
public SqliteParameterUnitTests()
{
}
[Test]
[Category ("NotWorking")]
// fails randomly :)
public void InsertRandomValuesWithParameter()
{
SqliteParameter textP = new SqliteParameter();
textP.ParameterName = "textP";
textP.SourceColumn = "t";
SqliteParameter floatP = new SqliteParameter();
floatP.ParameterName = "floatP";
floatP.SourceColumn = "nu";
SqliteParameter integerP = new SqliteParameter();
integerP.ParameterName ="integerP";
integerP.SourceColumn = "i";
SqliteParameter blobP = new SqliteParameter();
blobP.ParameterName = "blobP";
blobP.SourceColumn = "b";
Random random = new Random();
StringBuilder builder = new StringBuilder();
for (int k=0; k < random.Next(0,100); k++)
{
builder.Append((char)random.Next(65536));
}
SqliteCommand insertCmd = new SqliteCommand("DELETE FROM t1; INSERT INTO t1 (t, f, i, b ) VALUES(:textP,:floatP,:integerP,:blobP)",_conn);
insertCmd.Parameters.Add(textP);
insertCmd.Parameters.Add(floatP);
insertCmd.Parameters.Add(blobP);
insertCmd.Parameters.Add(integerP);
textP.Value=builder.ToString();
floatP.Value=Convert.ToInt64(random.Next(999));
integerP.Value=random.Next(999);
blobP.Value=System.Text.Encoding.UTF8.GetBytes("\u05D0\u05D1\u05D2" + builder.ToString());
SqliteCommand selectCmd = new SqliteCommand("SELECT * from t1", _conn);
using(_conn)
{
_conn.Open();
int res = insertCmd.ExecuteNonQuery();
Assert.AreEqual(res,1);
using (IDataReader reader = selectCmd.ExecuteReader()) {
Assert.AreEqual(reader.Read(), true);
Assert.AreEqual(reader["t"], textP.Value);
Assert.AreEqual(reader["f"], floatP.Value);
Assert.AreEqual(reader["i"], integerP.Value);
object compareValue;
#if NET_2_0
if (blobP.Value is byte[])
compareValue = System.Text.Encoding.UTF8.GetString ((byte[])blobP.Value);
else
#endif
compareValue = blobP.Value;
Assert.AreEqual(reader["b"], compareValue);
Assert.AreEqual(reader.Read(), false);
}
}
}
}
}

View File

@@ -0,0 +1,176 @@
//
// SqliteTest.cs - Test for the Sqlite ADO.NET Provider in Mono.Data.Sqlite
// This provider works on Linux and Windows and uses the native
// sqlite.dll or sqlite.so library.
//
// Modify or add to this test as needed...
//
// SQL Lite can be downloaded from
// http://www.hwaci.com/sw/sqlite/download.html
//
// There are binaries for Windows and Linux.
//
// To compile:
// mcs SqliteTest.cs -r System.Data.dll -r Mono.Data.Sqlite.dll
//
// Author:
// Daniel Morgan <danmorg@sc.rr.com>
//
using System;
using System.Data;
using Mono.Data.Sqlite;
namespace Test.Mono.Data.Sqlite
{
class SqliteTest
{
[STAThread]
static void Main(string[] args)
{
Test(false, null);
Console.WriteLine();
Test(false, "ISO-8859-1");
Console.WriteLine();
Test(true, null);
}
static void Test(bool v3, string encoding) {
if (!v3)
Console.WriteLine("Testing Version 2" + (encoding != null ? " with " + encoding + " encoding" : ""));
else
Console.WriteLine("Testing Version 3");
System.IO.File.Delete("SqliteTest.db");
SqliteConnection dbcon = new SqliteConnection();
// the connection string is a URL that points
// to a file. If the file does not exist, a
// file is created.
// "URI=file:some/path"
string connectionString =
"URI=file:SqliteTest.db";
if (v3)
connectionString += ",Version=3";
if (encoding != null)
connectionString += ",encoding=" + encoding;
dbcon.ConnectionString = connectionString;
dbcon.Open();
SqliteCommand dbcmd = new SqliteCommand();
dbcmd.Connection = dbcon;
dbcmd.CommandText =
"CREATE TABLE MONO_TEST ( " +
"NID INT, " +
"NDESC TEXT, " +
"NTIME DATETIME); " +
"INSERT INTO MONO_TEST " +
"(NID, NDESC, NTIME) " +
"VALUES(1,'One (unicode test: \u05D0)', '2006-01-01')";
Console.WriteLine("Create & insert modified rows = 1: " + dbcmd.ExecuteNonQuery());
dbcmd.CommandText =
"INSERT INTO MONO_TEST " +
"(NID, NDESC, NTIME) " +
"VALUES(:NID,:NDESC,:NTIME)";
dbcmd.Parameters.Add( new SqliteParameter("NID", 2) );
dbcmd.Parameters.Add( new SqliteParameter(":NDESC", "Two (unicode test: \u05D1)") );
dbcmd.Parameters.Add( new SqliteParameter(":NTIME", DateTime.Now) );
Console.WriteLine("Insert modified rows with parameters = 1, 2: " + dbcmd.ExecuteNonQuery() + " , " + dbcmd.LastInsertRowID());
dbcmd.CommandText =
"INSERT INTO MONO_TEST " +
"(NID, NDESC, NTIME) " +
"VALUES(3,'Three, quoted parameter test, and next is null; :NTIME', NULL)";
Console.WriteLine("Insert with null modified rows and ID = 1, 3: " + dbcmd.ExecuteNonQuery() + " , " + dbcmd.LastInsertRowID());
dbcmd.CommandText =
"INSERT INTO MONO_TEST " +
"(NID, NDESC, NTIME) " +
"VALUES(4,'Four with ANSI char: ü', NULL)";
Console.WriteLine("Insert with ANSI char ü = 1, 4: " + dbcmd.ExecuteNonQuery() + " , " + dbcmd.LastInsertRowID());
dbcmd.CommandText =
"INSERT INTO MONO_TEST " +
"(NID, NDESC, NTIME) " +
"VALUES(?,?,?)";
dbcmd.Parameters.Clear();
IDbDataParameter param1 = dbcmd.CreateParameter();
param1.DbType = DbType.DateTime;
param1.Value = 5;
dbcmd.Parameters.Add(param1);
IDbDataParameter param2 = dbcmd.CreateParameter();
param2.Value = "Using unnamed parameters";
dbcmd.Parameters.Add(param2);
IDbDataParameter param3 = dbcmd.CreateParameter();
param3.DbType = DbType.DateTime;
param3.Value = DateTime.Parse("2006-05-11 11:45:00");
dbcmd.Parameters.Add(param3);
Console.WriteLine("Insert with unnamed parameters = 1, 5: " + dbcmd.ExecuteNonQuery() + " , " + dbcmd.LastInsertRowID());
dbcmd.CommandText =
"SELECT * FROM MONO_TEST";
SqliteDataReader reader;
reader = dbcmd.ExecuteReader();
Console.WriteLine("read and display data...");
while(reader.Read())
for (int i = 0; i < reader.FieldCount; i++)
Console.WriteLine(" Col {0}: {1} (type: {2}, data type: {3})",
i, reader[i] == null ? "(null)" : reader[i].ToString(), reader[i] == null ? "(null)" : reader[i].GetType().FullName, reader.GetDataTypeName(i));
dbcmd.CommandText = "SELECT NDESC FROM MONO_TEST WHERE NID=2";
Console.WriteLine("read and display a scalar = 'Two': " + dbcmd.ExecuteScalar());
dbcmd.CommandText = "SELECT count(*) FROM MONO_TEST";
Console.WriteLine("read and display a non-column scalar = 3: " + dbcmd.ExecuteScalar());
Console.WriteLine("read and display data using DataAdapter/DataSet...");
SqliteDataAdapter adapter = new SqliteDataAdapter("SELECT * FROM MONO_TEST", connectionString);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
foreach(DataTable myTable in dataset.Tables){
foreach(DataRow myRow in myTable.Rows){
foreach (DataColumn myColumn in myTable.Columns){
Console.WriteLine(" " + myRow[myColumn]);
}
}
}
/*Console.WriteLine("read and display data using DataAdapter/DataTable...");
DataTable dt = new DataTable();
adapter.Fill(dt);
DataView dv = new DataView(dt);
foreach (DataRowView myRow in dv) {
foreach (DataColumn myColumn in myRow.Row.Table.Columns) {
Console.WriteLine(" " + myRow[myColumn.ColumnName]);
}
}*/
try {
dbcmd.CommandText = "SELECT NDESC INVALID SYNTAX FROM MONO_TEST WHERE NID=2";
dbcmd.ExecuteNonQuery();
Console.WriteLine("Should not reach here.");
} catch (Exception e) {
Console.WriteLine("Testing a syntax error: " + e.GetType().Name + ": " + e.Message);
}
/*try {
dbcmd.CommandText = "SELECT 0/0 FROM MONO_TEST WHERE NID=2";
Console.WriteLine("Should not reach here: " + dbcmd.ExecuteScalar());
} catch (Exception e) {
Console.WriteLine("Testing an execution error: " + e.GetType().Name + ": " + e.Message);
}*/
dataset.Dispose();
adapter.Dispose();
reader.Close();
dbcmd.Dispose();
dbcon.Close();
}
}
}

View File

@@ -0,0 +1,8 @@
create table test (
id int NOT NULL PRIMARY KEY,
name varchar (20)
);
insert into test values (1, "mono test 1");
insert into test values (2, "mono test 2");
insert into test values (3, "mono test 3");