// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Linq.Expressions;
using Microsoft.Extensions.Options;
using Moq;
using NPoco;
using NPoco.DatabaseTypes;
using NPoco.Linq;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Infrastructure.Migrations.Install;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax;
using Umbraco.Cms.Persistence.SqlServer.Services;
namespace Umbraco.Cms.Tests.Common.TestHelpers;
///
/// 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 SqlServerDatabaseType();
SqlContext = new SqlContext(syntaxProvider ?? new SqlServerSyntaxProvider(Options.Create(new GlobalSettings())), DatabaseType, Mock.Of());
}
///
/// Gets the database operations.
///
public List Operations { get; } = new();
public void Dispose()
{
}
public IDatabase OpenSharedConnection() => this;
public void CloseSharedConnection()
{
}
(List, List, List, List) IDatabaseQuery.FetchMultiple(Sql sql) =>
throw new NotImplementedException();
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 int CommandTimeout { get; set; }
public ISqlContext SqlContext { get; }
public string InstanceId { get; }
public bool InTransaction { get; }
public bool EnableSqlCount { get; set; }
public int SqlCount { get; }
IMapperCollection IDatabaseConfig.Mappers { get => Mappers; set => throw new NotImplementedException(); }
IDatabaseType IDatabaseConfig.DatabaseType => DatabaseType;
public int BulkInsertRecords(IEnumerable records) => throw new NotImplementedException();
public bool IsUmbracoInstalled() => true;
public DatabaseSchemaResult ValidateSchema() => throw new NotImplementedException();
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"));
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, CancellationToken token) => throw new NotImplementedException();
public Task ExecuteScalarAsync(string sql, object[] args, CancellationToken token) => throw new NotImplementedException();
public Task ExecuteScalarAsync(Sql sql, CancellationToken token) => throw new NotImplementedException();
public Task ExecuteAsync(string sql, CancellationToken token) => throw new NotImplementedException();
public Task ExecuteAsync(string sql, object[] args, CancellationToken token) => throw new NotImplementedException();
public Task ExecuteAsync(Sql sql, CancellationToken token) => throw new NotImplementedException();
public Task