Files
Umbraco-CMS/tests/Umbraco.Tests.Integration.SqlCe/Umbraco.Infrastructure/Persistence/DatabaseBuilderTests.cs
Paul Johnson 00133e880d Move test projects from src/ to tests/ (#11357)
* Update gitignore

* Move csproj

* Update project references

* Update solutions

* Update build scripts

* Tests used to share editorconfig with projects in src

* Fix broken tests.

* Stop copying around .editorconfig

merged root one with linting

* csharp_style_expression_bodied -> suggestion

* Move StyleCop rulesets to matching directories and update shared build properties

* Remove legacy build files, update NuGet.cofig and solution files

* Restore myget source

* Clean up .gitignore

* Update .gitignore

* Move new test classes to tests after merge

* Gitignore + nuget config

* Move new test

Co-authored-by: Ronald Barendse <ronald@barend.se>
2021-10-18 08:14:04 +01:00

86 lines
3.6 KiB
C#

using System;
using System.IO;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using NPoco;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Infrastructure.Migrations.Install;
using Umbraco.Cms.Infrastructure.Persistence;
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
{
[TestFixture]
[UmbracoTest]
[Platform("Win")]
public class DatabaseBuilderTests : UmbracoIntegrationTest
{
private IDbProviderFactoryCreator DbProviderFactoryCreator => GetRequiredService<IDbProviderFactoryCreator>();
private IUmbracoDatabaseFactory UmbracoDatabaseFactory => GetRequiredService<IUmbracoDatabaseFactory>();
private IDatabaseCreator EmbeddedDatabaseCreator => GetRequiredService<IDatabaseCreator>();
public DatabaseBuilderTests()
{
TestOptionAttributeBase.ScanAssemblies.Add(typeof(DatabaseBuilderTests).Assembly);
}
[Test]
public void CreateDatabase()
{
var path = TestContext.CurrentContext.TestDirectory.Split("bin")[0];
AppDomain.CurrentDomain.SetData("DataDirectory", path);
const string dbFile = "DatabaseContextTests.sdf";
// delete database file
// NOTE: using a custom db file for this test since we're re-using the one created with BaseDatabaseFactoryTest
var filePath = string.Concat(path, dbFile);
if (File.Exists(filePath))
File.Delete(filePath);
var connectionString = $"Datasource=|DataDirectory|{dbFile};Flush Interval=1";
UmbracoDatabaseFactory.Configure(connectionString, Constants.DbProviderNames.SqlCe);
DbProviderFactoryCreator.CreateDatabase(Constants.DbProviderNames.SqlCe, connectionString);
UmbracoDatabaseFactory.CreateDatabase();
// test get database type (requires an actual database)
using (var database = UmbracoDatabaseFactory.CreateDatabase())
{
var databaseType = database.DatabaseType;
Assert.AreEqual(DatabaseType.SQLCe, databaseType);
}
// create application context
//var appCtx = new ApplicationContext(
// _databaseFactory,
// new ServiceContext(migrationEntryService: Mock.Of<IMigrationEntryService>()),
// CacheHelper.CreateDisabledCacheHelper(),
// new ProfilingLogger(Mock.Of<ILogger>(), Mock.Of<IProfiler>()));
// create the umbraco database
DatabaseSchemaCreator schemaHelper;
using (var database = UmbracoDatabaseFactory.CreateDatabase())
using (var transaction = database.GetTransaction())
{
schemaHelper = new DatabaseSchemaCreator(database, Mock.Of<ILogger<DatabaseSchemaCreator>>(), NullLoggerFactory.Instance, new UmbracoVersion(), Mock.Of<IEventAggregator>());
schemaHelper.InitializeDatabaseSchema();
transaction.Complete();
}
var umbracoNodeTable = schemaHelper.TableExists("umbracoNode");
var umbracoUserTable = schemaHelper.TableExists("umbracoUser");
var cmsTagsTable = schemaHelper.TableExists("cmsTags");
Assert.That(umbracoNodeTable, Is.True);
Assert.That(umbracoUserTable, Is.True);
Assert.That(cmsTagsTable, Is.True);
}
}
}