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) { Text = text; Sql = sql; } public Operation(string text, string sql, params object[] args) { Text = text.Trim(); Sql = sql.Trim(); 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 InsertAsync(T poco) { throw new NotImplementedException(); } public Task UpdateAsync(object poco) { throw new NotImplementedException(); } public Task UpdateAsync(object poco, IEnumerable columns) { throw new NotImplementedException(); } public Task UpdateAsync(T poco, Expression> fields) { throw new NotImplementedException(); } public Task DeleteAsync(object poco) { throw new NotImplementedException(); } public void InsertBulk(IEnumerable pocos) { throw new NotImplementedException(); } public void InsertBatch(IEnumerable pocos, BatchOptions options = null) { throw new NotImplementedException(); } public int Update(string tableName, string primaryKeyName, object poco, object primaryKeyValue) { throw new NotImplementedException(); } public int Update(string tableName, string primaryKeyName, object poco) { throw new NotImplementedException(); } public int Update(string tableName, string primaryKeyName, object poco, object primaryKeyValue, IEnumerable columns) { throw new NotImplementedException(); } public int Update(string tableName, string primaryKeyName, object poco, IEnumerable columns) { throw new NotImplementedException(); } public int Update(object poco, IEnumerable columns) { throw new NotImplementedException(); } public int Update(object poco, object primaryKeyValue, IEnumerable columns) { throw new NotImplementedException(); } public int Update(object poco) { throw new NotImplementedException(); } public int Update(T poco, Expression> fields) { throw new NotImplementedException(); } public int Update(object poco, object primaryKeyValue) { throw new NotImplementedException(); } public int Update(string sql, params object[] args) { throw new NotImplementedException(); } public int Update(Sql sql) { throw new NotImplementedException(); } public IUpdateQueryProvider UpdateMany() { throw new NotImplementedException(); } public int Delete(string tableName, string primaryKeyName, object poco) { throw new NotImplementedException(); } public int Delete(string tableName, string primaryKeyName, object poco, object primaryKeyValue) { throw new NotImplementedException(); } public int Delete(object poco) { throw new NotImplementedException(); } public int Delete(string sql, params object[] args) { throw new NotImplementedException(); } public int Delete(Sql sql) { throw new NotImplementedException(); } public int Delete(object pocoOrPrimaryKey) { throw new NotImplementedException(); } public IDeleteQueryProvider DeleteMany() { throw new NotImplementedException(); } public void Save(T poco) { throw new NotImplementedException(); } public bool IsNew(T poco) { throw new NotImplementedException(); } #endregion #region Reads (not implemented) public List Fetch(Type type, string sql, params object[] args) { throw new NotImplementedException(); } public List Fetch(Type type, Sql Sql) { throw new NotImplementedException(); } public IEnumerable Query(Type type, string sql, params object[] args) { throw new NotImplementedException(); } public IEnumerable Query(Type type, Sql Sql) { throw new NotImplementedException(); } public List Fetch() { throw new NotImplementedException(); } public List Fetch(string sql, params object[] args) { throw new NotImplementedException(); } public List Fetch(Sql sql) { throw new NotImplementedException(); } public List Fetch(long page, long itemsPerPage, string sql, params object[] args) { throw new NotImplementedException(); } public List Fetch(long page, long itemsPerPage, Sql sql) { throw new NotImplementedException(); } public Page Page(long page, long itemsPerPage, string sql, params object[] args) { throw new NotImplementedException(); } public Page Page(long page, long itemsPerPage, Sql sql) { throw new NotImplementedException(); } public List SkipTake(long skip, long take, string sql, params object[] args) { throw new NotImplementedException(); } public List SkipTake(long skip, long take, Sql sql) { throw new NotImplementedException(); } public List FetchOneToMany(Expression> many, string sql, params object[] args) { throw new NotImplementedException(); } public List FetchOneToMany(Expression> many, Sql sql) { throw new NotImplementedException(); } public List FetchOneToMany(Expression> many, Func idFunc, string sql, params object[] args) { throw new NotImplementedException(); } public List FetchOneToMany(Expression> many, Func idFunc, Sql sql) { throw new NotImplementedException(); } public IEnumerable Query(string sql, params object[] args) { throw new NotImplementedException(); } public IEnumerable Query(Sql sql) { throw new NotImplementedException(); } public IQueryProviderWithIncludes Query() { throw new NotImplementedException(); } public T SingleById(object primaryKey) { throw new NotImplementedException(); } public T Single(string sql, params object[] args) { throw new NotImplementedException(); } public T SingleInto(T instance, string sql, params object[] args) { throw new NotImplementedException(); } public T SingleOrDefaultById(object primaryKey) { throw new NotImplementedException(); } public T SingleOrDefault(string sql, params object[] args) { throw new NotImplementedException(); } public T SingleOrDefaultInto(T instance, string sql, params object[] args) { throw new NotImplementedException(); } public T First(string sql, params object[] args) { throw new NotImplementedException(); } public T FirstInto(T instance, string sql, params object[] args) { throw new NotImplementedException(); } public T FirstOrDefault(string sql, params object[] args) { throw new NotImplementedException(); } public T FirstOrDefaultInto(T instance, string sql, params object[] args) { throw new NotImplementedException(); } public T Single(Sql sql) { throw new NotImplementedException(); } public T SingleInto(T instance, Sql sql) { throw new NotImplementedException(); } public T SingleOrDefault(Sql sql) { throw new NotImplementedException(); } public T SingleOrDefaultInto(T instance, Sql sql) { throw new NotImplementedException(); } public T First(Sql sql) { throw new NotImplementedException(); } public T FirstInto(T instance, Sql sql) { throw new NotImplementedException(); } public T FirstOrDefault(Sql sql) { throw new NotImplementedException(); } public T FirstOrDefaultInto(T instance, Sql sql) { throw new NotImplementedException(); } public Dictionary Dictionary(Sql Sql) { throw new NotImplementedException(); } public Dictionary Dictionary(string sql, params object[] args) { throw new NotImplementedException(); } public bool Exists(object primaryKey) { throw new NotImplementedException(); } public TRet FetchMultiple(Func, List, TRet> cb, string sql, params object[] args) { throw new NotImplementedException(); } public TRet FetchMultiple(Func, List, List, TRet> cb, string sql, params object[] args) { throw new NotImplementedException(); } public TRet FetchMultiple(Func, List, List, List, TRet> cb, string sql, params object[] args) { throw new NotImplementedException(); } public TRet FetchMultiple(Func, List, TRet> cb, Sql sql) { throw new NotImplementedException(); } public TRet FetchMultiple(Func, List, List, TRet> cb, Sql sql) { throw new NotImplementedException(); } public TRet FetchMultiple(Func, List, List, List, TRet> cb, Sql sql) { throw new NotImplementedException(); } public Tuple, List> FetchMultiple(string sql, params object[] args) { throw new NotImplementedException(); } public Tuple, List, List> FetchMultiple(string sql, params object[] args) { throw new NotImplementedException(); } public Tuple, List, List, List> FetchMultiple(string sql, params object[] args) { throw new NotImplementedException(); } public Tuple, List> FetchMultiple(Sql sql) { throw new NotImplementedException(); } public Tuple, List, List> FetchMultiple(Sql sql) { throw new NotImplementedException(); } public Tuple, List, List, List> FetchMultiple(Sql sql) { throw new NotImplementedException(); } public Task SingleByIdAsync(object primaryKey) { throw new NotImplementedException(); } public Task SingleOrDefaultByIdAsync(object primaryKey) { throw new NotImplementedException(); } public Task> QueryAsync(string sql, params object[] args) { throw new NotImplementedException(); } public Task> QueryAsync(Sql sql) { throw new NotImplementedException(); } public Task> FetchAsync(string sql, params object[] args) { throw new NotImplementedException(); } public Task> FetchAsync(Sql sql) { throw new NotImplementedException(); } public Task> FetchAsync() { throw new NotImplementedException(); } public Task> PageAsync(long page, long itemsPerPage, string sql, params object[] args) { throw new NotImplementedException(); } public Task> PageAsync(long page, long itemsPerPage, Sql sql) { throw new NotImplementedException(); } public Task> FetchAsync(long page, long itemsPerPage, string sql, params object[] args) { throw new NotImplementedException(); } public Task> FetchAsync(long page, long itemsPerPage, Sql sql) { throw new NotImplementedException(); } public Task> SkipTakeAsync(long skip, long take, string sql, params object[] args) { throw new NotImplementedException(); } public Task> SkipTakeAsync(long skip, long take, Sql sql) { throw new NotImplementedException(); } #endregion #region Stuff public void BuildPageQueries(long skip, long take, string sql, ref object[] args, out string sqlCount, out string sqlPage) { throw new NotImplementedException(); } #endregion } }