30 using System.Collections.Generic;
32 using NUnit.Framework;
33 using NUnit.Framework.Constraints;
35 using OpenSim.Framework;
36 using OpenSim.Tests.Common;
39 using System.Data.Common;
40 using System.Reflection;
42 namespace OpenSim.Data.Tests
55 public class BasicDataServiceTest<TConn, TService>
56 where TConn : DbConnection, new()
57 where TService : class, new()
60 private TService m_service;
61 private string m_file;
76 m_connStr = !String.IsNullOrEmpty(conn) ? conn : DefaultTestConns.Get(typeof(TConn));
78 m_log = LogManager.GetLogger(this.GetType());
79 OpenSim.Tests.Common.TestLogging.LogToConsole();
96 if (typeof(TConn).Name.StartsWith(
"Sqlite"))
99 if (Directory.Exists(
"/proc/ppc64") || Directory.Exists(
"/proc/dasd"))
102 if (Util.IsWindows())
103 Util.LoadArchSpecificWindowsDll(
"sqlite3.dll");
106 if (String.IsNullOrEmpty(m_connStr))
108 m_file = Path.GetTempFileName() +
".db";
109 m_connStr =
"URI=file:" + m_file +
",version=3";
113 if (String.IsNullOrEmpty(m_connStr))
115 string msg = String.Format(
"Connection string for {0} is not defined, ignoring tests", typeof(TConn).Name);
121 using (TConn conn =
new TConn())
123 conn.ConnectionString = m_connStr;
131 string msg = String.Format(
"{0} is unable to connect to the database, ignoring tests", typeof(TConn).Name);
143 m_service =
new TService();
144 InitService(m_service);
148 m_log.Error(e.ToString());
153 [TestFixtureTearDown]
156 if (m_service != null)
158 if (m_service is IDisposable)
159 ((IDisposable)m_service).Dispose();
163 if (!String.IsNullOrEmpty(m_file) && File.Exists(m_file))
169 DbConnection cnn =
new TConn();
170 cnn.ConnectionString = m_connStr;
177 using (DbConnection dbcon = Connect())
179 using (DbCommand cmd = dbcon.CreateCommand())
181 cmd.CommandText = sql;
182 cmd.ExecuteNonQuery();
187 protected delegate
bool ProcessRow(IDataReader reader);
189 protected virtual int ExecQuery(
string sql,
bool bSingleRow, ProcessRow action)
192 using (DbConnection dbcon = Connect())
194 using (DbCommand cmd = dbcon.CreateCommand())
196 cmd.CommandText = sql;
197 CommandBehavior cb = bSingleRow ? CommandBehavior.SingleRow : CommandBehavior.Default;
198 using (DbDataReader rdr = cmd.ExecuteReader(cb))
218 foreach (
string tbl
in tables)
222 ExecuteSql(
"DROP TABLE " + tbl +
";");
235 foreach (
string store
in stores)
237 string s =
"'" + store +
"'";
244 string sCond = stores.Length > 1 ? (
"in (" + lst +
")") : (
"=" + lst);
247 ExecuteSql(
"DELETE FROM migrations where name " + sCond);
259 foreach (
string tbl
in tables)
263 ExecuteSql(
"DELETE FROM " + tbl +
";");
virtual void InitService(object service)
To be overridden in derived classes. Do whatever init with the m_service, like setting the conn strin...
BasicDataServiceTest(string conn)
virtual void ExecuteSql(string sql)
virtual DbConnection Connect()
virtual void ResetMigrations(params string[] stores)
Clear tables listed as parameters (without dropping them).
virtual void ClearTables(params string[] tables)
Clear tables listed as parameters (without dropping them).
virtual int ExecQuery(string sql, bool bSingleRow, ProcessRow action)
virtual void DropTables(params string[] tables)
Drop tables (listed as parameters). There is no "DROP IF EXISTS" syntax common for all databases...