using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Moq;
using NPoco;
using NPoco.DatabaseTypes;
using NPoco.Linq;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.SqlSyntax;
namespace Umbraco.Tests.Testing
{
///
/// An implementation of for tests.
///
///
/// Supports writing to the database, and logs Sql statements.
/// Cannot support reading from the database, and throws.
/// Tries to pretend it supports transactions, connections, etc. as best as possible.
///
public class TestDatabase : IUmbracoDatabase
{
///
/// Initializes a new instance of the class.
///
///
/// When both parameters are supplied, they should of course be consistent.
///
public TestDatabase(DatabaseType databaseType = null, ISqlSyntaxProvider syntaxProvider = null)
{
DatabaseType = databaseType ?? new SqlServerCEDatabaseType();
SqlContext = new SqlContext(syntaxProvider ?? new SqlCeSyntaxProvider(), DatabaseType, Mock.Of());
}
///
/// Represents a database operation.
///
public class Operation
{
public Operation(string text)
{
Text = text;
}
public Operation(string text, string sql)
: this(text)
{
Sql = sql;
}
public Operation(string text, string sql, params object[] args)
: this(text, sql)
{
Args = args;
}
///
/// Gets the operation text.
///
public string Text { get; }
///
/// Gets the operation Sql statement.
///
public string Sql { get; }
///
/// Gets the operation Sql arguments.
///
public object[] Args { get; }
}
///
/// Gets the database operations.
///
public List Operations { get; } = new List();
#region Connection, Transaction and Stuff
public void Dispose()
{ }
public IDatabase OpenSharedConnection()
{
return this;
}
public void CloseSharedConnection()
{ }
public int OneTimeCommandTimeout { get; set; }
public MapperCollection Mappers { get; set; }
public IPocoDataFactory PocoDataFactory { get; set; }
public DatabaseType DatabaseType { get; }
public List Interceptors { get; }
public string ConnectionString { get; }
public DbConnection Connection { get; }
public DbTransaction Transaction { get; }
public IDictionary Data { get; }
public ISqlContext SqlContext { get; }
public string InstanceId { get; }
public bool InTransaction { get; }
public DbParameter CreateParameter()
{
throw new NotImplementedException();
}
public void AddParameter(DbCommand cmd, object value)
{
throw new NotImplementedException();
}
public DbCommand CreateCommand(DbConnection connection, CommandType commandType, string sql, params object[] args)
{
throw new NotImplementedException();
}
public ITransaction GetTransaction()
{
throw new NotImplementedException();
}
public ITransaction GetTransaction(IsolationLevel isolationLevel)
{
throw new NotImplementedException();
}
public void SetTransaction(DbTransaction tran)
{
throw new NotImplementedException();
}
public void BeginTransaction()
{
Operations.Add(new Operation("BEGIN"));
}
public void BeginTransaction(IsolationLevel isolationLevel)
{
Operations.Add(new Operation("BEGIN " + isolationLevel));
}
public void AbortTransaction()
{
Operations.Add(new Operation("ABORT"));
}
public void CompleteTransaction()
{
Operations.Add(new Operation("COMMIT"));
}
#endregion
#region Writes
public int Execute(string sql, params object[] args)
{
Operations.Add(new Operation("EXECUTE", sql, args));
return default;
}
public int Execute(Sql sql)
{
Operations.Add(new Operation("EXECUTE", sql.SQL, sql.Arguments));
return default;
}
public int Execute(string sql, CommandType commandType, params object[] args)
{
Operations.Add(new Operation("EXECUTE", sql, args));
return default;
}
public T ExecuteScalar(string sql, params object[] args)
{
Operations.Add(new Operation("EXECUTE SCALAR", sql, args));
return default;
}
public T ExecuteScalar(Sql sql)
{
Operations.Add(new Operation("EXECUTE SCALAR", sql.SQL, sql.Arguments));
return default;
}
public T ExecuteScalar(string sql, CommandType commandType, params object[] args)
{
Operations.Add(new Operation("EXECUTE SCALAR", sql, args));
return default;
}
public Task ExecuteScalarAsync(string sql, params object[] args)
{
throw new NotImplementedException();
}
public Task ExecuteScalarAsync(Sql sql)
{
throw new NotImplementedException();
}
public Task ExecuteAsync(string sql, params object[] args)
{
throw new NotImplementedException();
}
public Task ExecuteAsync(Sql sql)
{
throw new NotImplementedException();
}
public object Insert(string tableName, string primaryKeyName, bool autoIncrement, T poco)
{
throw new NotImplementedException();
}
public object Insert(string tableName, string primaryKeyName, T poco)
{
throw new NotImplementedException();
}
public object Insert(T poco)
{
throw new NotImplementedException();
}
public Task