Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/SyntaxProvider/SqlServerSyntaxProviderTests.cs
Andy Butland 825f791d01 Remove the non-controversial, straightforward obsoleted constructs for Umbraco 16 (#18661)
* Removed obsoletes from IConfigManipulator.

* Removed obsolete models builder extensions.

* Removed the obsolete ContentDashboardSettings.

* Removed the obsolete InstallMissingDatabase setting on GlobalSettings.

* Removed obsolete NuCache settings.

* Removed obsolete RuntimeMinificationSettings.

* Removed obsolete health check constant.

* Removed obsolete icon constant.

* Removed obsolete telemetry constant.

* Removed obsolete property and constructor on UmbracoBuilder.

* Removed obsolete constructor on AuditNotificationsHandler.

* Removed obsolete constructor on HTTP header health checks.

* Removed obsolete constructor on MediaFileManager.

* Removed obsolete GetDefaultFileContent on ViewHelper.

* Remove obsoleted methods on embed providers.

* Fix tests.

* Removed obsolete constructors on BlockEditorDataConverter.

* Removed obsolete SeedCacheDuration property on CacheSettings.

* Removed obsolete PublishCulture on ContentRepositoryExtensions.

* Removed obsolete MonitorLock.

* Removed obsolete synchronous HasSavedValues from IDataTypeUsageService and IDataTypeUsageRepository.

* Removed obsolete HasSavedPropertyValues from IPropertyTypeUsageService and IPropertyTypeUsageRepository.

* Removed obsolete methods in ITrackedReferencesService and ITrackedReferencesRepository.

* Removed obsolete DateValueEditor constructors.

* Removed obsolete GetAutomaticRelationTypesAliases.

* Removed obsolete constructor on TextOnlyValueEditor.

* Removed obsolete constructors on RegexValidator and RequiredValidator.

* Removed obsolete constructs on SliderValueConverter and TagsValueConverter.

* Removed obsolete GetContentType methods from IPublishedCache.

* Removed ContentFinderByIdPath.

* Removed obsolete constructor on DefaultMediaUrlProvider.

* Removed obsolete constructor on Domain.

* Removed obsolete constructor on PublishedRequest.

* Removed obsolete methods on CheckPermissions.

* Removed obsolete GetUserId from IBackOfficeSecurity.

* Removed obsolete methods on LegacyPasswordSecurity.

* Removed obsolete constructors on AuditService.

* Removed obsolete methods on IContentEditingService.

* Remove obsolete constructors and methods on ContentService/IContentService.

* Removed obsolete constructor in ContentTypeEditingService.

* Removed obsolete constructor in MediaTypeEditingService.

* Removed obsolete constructor in MemberTypeEditingService.

* Removed obsolete constructor in ContentTypeService.

* Removed obsolete constructors in ContentTypeServiceBase.

* Removed obsolete constructors and methods in ContentVersionService.

* Removed obsolete constructor in DataTypeUsageService.

* Removed obsolete constructor in DomainService.

* Removed obsolete constructor in FileService.

* Removes obsolete AttemptMove from IContentService.

* Removes obsolete SetPreventCleanup from IContentVersionService.

* Removes obsolete GetReferences from IDataTypeService.

* Removed obsolete SetConsentLevel from IMetricsConsentService.

* Removed obsolete methods from IPackageDataInstallation.

* Removed obsolete methods from IPackagingService.

* Removed obsolete methods on ITwoFactorLoginService.
Removed obsolete ITemporaryMediaService.

* Removed obsolete constructor from MediaService, MemberTypeService and MediaTypeService.

* More obsolete constructors.

* Removed obsoleted overloads on IPropertyValidationService.

* Fixed build for tests.

* Removed obsolete constructor for PublicAccessService, UserService and RelationService.

* Removed GetDefaultMemberType.

* Removed obsolete user group functionality from IUserService.

* Removed obsolete extension methods on IUserService.

* Removed obsolete method from ITelemetryService.

* Removed obsolete UdiParserServiceConnectors.

* Removed obsolete method on ICookieManager.

* Removed obsolete DynamicContext.

* Removed obsolete XmlHelper.

* Fixed failing integration tests.

* Removed obsoletes in Umbraco.Cms.Api.Common

* Removed obsoletes in Umbraco.Cms.Api.Delivery

* Removed obsoletes in Umbraco.Cms.Api.Management

* Removed obsoletes in Umbraco.Examine.Lucene

* Removed obsoletes in Umbraco.Infrastructure

* Fix failing delivery API contract integration test.

* Made integration tests internal.

* Removed obsoletes from web projects.

* Fix build.

* Removed Twitter OEmbed provider

* Removed obsolete constructor on PublishedDataType.

* Removed obsolete constructors on PublishedCacheBase.

* Removed the obsolete PropertyEditorTagsExtensions.

* Removed obsoletion properties on configuration response  models (#18697)

* Removed obsolete methods from server-side models.

* Update client-side types and sdk.

* Update client-side files.

* Removed obsoletion of Utf8ToAsciiConverter.ToAsciiString overload. (#18694)

* Removed obsolete method in UserService. (#18710)

* Removed obsoleted group alias keys from being publicly available. (#18682)

* Removed unneceessary ApiVersion attribute.

* Clean-up obsoletions on MemberService (#18703)

* Removed obsoleted method on MemberService, added future obsoletion to interface and updated all callers.

* Removed obsoletion on member service method that's not obsolete on the interface.
2025-03-21 17:02:31 +00:00

190 lines
7.2 KiB
C#

using System.Diagnostics;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NPoco;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Infrastructure.Migrations;
using Umbraco.Cms.Infrastructure.Migrations.Expressions.Common.Expressions;
using Umbraco.Cms.Infrastructure.Migrations.Expressions.Create.Index;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Infrastructure.Persistence.DatabaseAnnotations;
using Umbraco.Cms.Infrastructure.Persistence.DatabaseModelDefinitions;
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax;
using Umbraco.Cms.Persistence.SqlServer.Services;
using Umbraco.Cms.Tests.Common.TestHelpers;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.SyntaxProvider;
[TestFixture]
internal sealed class SqlServerSyntaxProviderTests : UmbracoIntegrationTest
{
private ISqlContext SqlContext => GetRequiredService<IUmbracoDatabaseFactory>().SqlContext;
private SqlServerSyntaxProvider GetSqlSyntax() => new(Options.Create(new GlobalSettings()));
private class TestPlan : MigrationPlan
{
public TestPlan() : base("Test")
{
}
}
private MigrationContext GetMigrationContext(out TestDatabase db)
{
var logger = Mock.Of<ILogger<MigrationContext>>();
var sqlSyntax = GetSqlSyntax();
db = new TestDatabase(DatabaseType.SqlServer2005, sqlSyntax);
return new MigrationContext(new TestPlan(), db, logger);
}
[UmbracoTest(Database = UmbracoTestOptions.Database.NewEmptyPerTest)]
[Test]
public void Can_Generate_Delete_SubQuery_Statement()
{
var mediaObjectType = Constants.ObjectTypes.Media;
var subQuery = SqlContext.Sql()
.Select("DISTINCT cmsContentNu.nodeId")
.From<ContentNuDto>()
.InnerJoin<NodeDto>()
.On<ContentNuDto, NodeDto>(left => left.NodeId, right => right.NodeId)
.Where<NodeDto>(dto => dto.NodeObjectType == mediaObjectType);
var sqlOutput = SqlContext.SqlSyntax.GetDeleteSubquery("cmsContentNu", "nodeId", subQuery);
string t(string x)
{
return SqlContext.SqlSyntax.GetQuotedTableName(x);
}
string c(string x)
{
return SqlContext.SqlSyntax.GetQuotedColumnName(x);
}
Assert.AreEqual(
@$"DELETE FROM {t("cmsContentNu")} WHERE {c("nodeId")} IN (SELECT {c("nodeId")} FROM (SELECT DISTINCT cmsContentNu.nodeId FROM {t("cmsContentNu")} INNER JOIN {t("umbracoNode")} ON {t("cmsContentNu")}.{c("nodeId")} = {t("umbracoNode")}.{c("id")} WHERE (({t("umbracoNode")}.{c("nodeObjectType")} = @0))) x)".Replace(Environment.NewLine, " ")
.Replace("\n", " ").Replace("\r", " "),
sqlOutput.SQL.Replace(Environment.NewLine, " ").Replace("\n", " ").Replace("\r", " "));
Assert.AreEqual(1, sqlOutput.Arguments.Length);
Assert.AreEqual(mediaObjectType, sqlOutput.Arguments[0]);
}
[NUnit.Framework.Ignore("This doesn't actually test anything")]
[Test]
public void Can_Generate_Create_Table_Statement()
{
var type = typeof(NodeDto);
var definition = DefinitionFactory.GetTableDefinition(type, SqlContext.SqlSyntax);
var create = SqlContext.SqlSyntax.Format(definition);
var primaryKey = SqlContext.SqlSyntax.FormatPrimaryKey(definition);
var indexes = SqlContext.SqlSyntax.Format(definition.Indexes);
var keys = SqlContext.SqlSyntax.Format(definition.ForeignKeys);
Debug.Print(create);
Debug.Print(primaryKey);
foreach (var sql in keys)
{
Debug.Print(sql);
}
foreach (var sql in indexes)
{
Debug.Print(sql);
}
}
[Test]
public void Format_SqlServer_NonClusteredIndexDefinition_AddsNonClusteredDirective()
{
var sqlSyntax = GetSqlSyntax();
var indexDefinition = CreateIndexDefinition();
indexDefinition.IndexType = IndexTypes.NonClustered;
var actual = sqlSyntax.Format(indexDefinition);
Assert.AreEqual("CREATE NONCLUSTERED INDEX [IX_A] ON [TheTable] ([A])", actual);
}
[Test]
public void Format_SqlServer_NonClusteredIndexDefinition_UsingIsClusteredFalse_AddsClusteredDirective()
{
var sqlSyntax = GetSqlSyntax();
var indexDefinition = CreateIndexDefinition();
indexDefinition.IndexType = IndexTypes.Clustered;
var actual = sqlSyntax.Format(indexDefinition);
Assert.AreEqual("CREATE CLUSTERED INDEX [IX_A] ON [TheTable] ([A])", actual);
}
[Test]
public void CreateIndexBuilder_SqlServer_NonClustered_CreatesNonClusteredIndex()
{
var context = GetMigrationContext(out var db);
var createExpression = new CreateIndexExpression(context) { Index = { Name = "IX_A" } };
new CreateIndexBuilder(createExpression)
.OnTable("TheTable").OnColumn("A").Ascending().WithOptions().NonClustered()
.Do();
Assert.AreEqual(1, db.Operations.Count);
Assert.AreEqual("CREATE NONCLUSTERED INDEX [IX_A] ON [TheTable] ([A])", db.Operations[0].Sql);
}
[Test]
public void CreateIndexBuilder_SqlServer_Unique_CreatesUniqueNonClusteredIndex()
{
var context = GetMigrationContext(out var db);
var createExpression = new CreateIndexExpression(context) { Index = { Name = "IX_A" } };
new CreateIndexBuilder(createExpression)
.OnTable("TheTable").OnColumn("A").Ascending().WithOptions().Unique()
.Do();
Assert.AreEqual(1, db.Operations.Count);
Assert.AreEqual("CREATE UNIQUE NONCLUSTERED INDEX [IX_A] ON [TheTable] ([A])", db.Operations[0].Sql);
}
[Test]
public void CreateIndexBuilder_SqlServer_Unique_CreatesUniqueNonClusteredIndex_Multi_Columnn()
{
var context = GetMigrationContext(out var db);
var createExpression = new CreateIndexExpression(context) { Index = { Name = "IX_AB" } };
new CreateIndexBuilder(createExpression)
.OnTable("TheTable").OnColumn("A").Ascending().OnColumn("B").Ascending().WithOptions().Unique()
.Do();
Assert.AreEqual(1, db.Operations.Count);
Assert.AreEqual("CREATE UNIQUE NONCLUSTERED INDEX [IX_AB] ON [TheTable] ([A],[B])", db.Operations[0].Sql);
}
[Test]
public void CreateIndexBuilder_SqlServer_Clustered_CreatesClusteredIndex()
{
var context = GetMigrationContext(out var db);
var createExpression = new CreateIndexExpression(context) { Index = { Name = "IX_A" } };
new CreateIndexBuilder(createExpression)
.OnTable("TheTable").OnColumn("A").Ascending().WithOptions().Clustered()
.Do();
Assert.AreEqual(1, db.Operations.Count);
Assert.AreEqual("CREATE CLUSTERED INDEX [IX_A] ON [TheTable] ([A])", db.Operations[0].Sql);
}
private static IndexDefinition CreateIndexDefinition() =>
new IndexDefinition { ColumnName = "A", Name = "IX_A", TableName = "TheTable", SchemaName = "dbo" };
}