mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 19:04:51 +08:00
Added unit tests for big text
--HG-- branch : dev
This commit is contained in:
100
src/Orchard.Core.Tests/Body/BodyPartTests.cs
Normal file
100
src/Orchard.Core.Tests/Body/BodyPartTests.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Autofac;
|
||||
using JetBrains.Annotations;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Drivers;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
using Orchard.ContentManagement.MetaData;
|
||||
using Orchard.ContentManagement.Records;
|
||||
using Orchard.Core.Common.Handlers;
|
||||
using Orchard.Core.Common.Models;
|
||||
using Orchard.Data;
|
||||
using Orchard.Environment;
|
||||
using Orchard.Security;
|
||||
using Orchard.Tests.Modules;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
using Orchard.Tests.Stubs;
|
||||
using Orchard.UI.Notify;
|
||||
|
||||
namespace Orchard.Core.Tests.Body {
|
||||
[TestFixture]
|
||||
public class BodyPartTests : DatabaseEnabledTestsBase {
|
||||
|
||||
public override void Register(ContainerBuilder builder) {
|
||||
builder.RegisterType<DefaultContentManager>().As<IContentManager>();
|
||||
builder.RegisterType<DefaultContentManagerSession>().As<IContentManagerSession>();
|
||||
builder.RegisterInstance(new Mock<IContentDefinitionManager>().Object);
|
||||
builder.RegisterInstance(new Mock<ITransactionManager>().Object);
|
||||
builder.RegisterInstance(new Mock<IAuthorizer>().Object);
|
||||
builder.RegisterInstance(new Mock<INotifier>().Object);
|
||||
builder.RegisterType<OrchardServices>().As<IOrchardServices>();
|
||||
|
||||
builder.RegisterType<ThingHandler>().As<IContentHandler>();
|
||||
|
||||
builder.RegisterType<DefaultContentQuery>().As<IContentQuery>();
|
||||
builder.RegisterType<BodyPartHandler>().As<IContentHandler>();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BodyCanHandleLongText() {
|
||||
var contentManager = _container.Resolve<IContentManager>();
|
||||
|
||||
contentManager.Create<Thing>(ThingDriver.ContentType.Name, t => {
|
||||
t.As<BodyPart>().Record = new BodyPartRecord();
|
||||
t.Text = new String('x', 10000);
|
||||
});
|
||||
|
||||
var bodies = contentManager.Query<BodyPart>().List();
|
||||
Assert.That(bodies, Is.Not.Null);
|
||||
Assert.That(bodies.Any(), Is.True);
|
||||
Assert.That(bodies.First().Text, Is.EqualTo(new String('x', 10000)));
|
||||
|
||||
}
|
||||
|
||||
protected override IEnumerable<Type> DatabaseTypes {
|
||||
get {
|
||||
return new[] {
|
||||
typeof(BodyPartRecord),
|
||||
typeof(ContentTypeRecord),
|
||||
typeof(ContentItemRecord),
|
||||
typeof(ContentItemVersionRecord),
|
||||
typeof(CommonPartRecord),
|
||||
typeof(CommonPartVersionRecord),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
[UsedImplicitly]
|
||||
public class ThingHandler : ContentHandler {
|
||||
public ThingHandler() {
|
||||
Filters.Add(new ActivatingFilter<Thing>(ThingDriver.ContentType.Name));
|
||||
Filters.Add(new ActivatingFilter<ContentPart<CommonPartVersionRecord>>(ThingDriver.ContentType.Name));
|
||||
Filters.Add(new ActivatingFilter<CommonPart>(ThingDriver.ContentType.Name));
|
||||
Filters.Add(new ActivatingFilter<BodyPart>(ThingDriver.ContentType.Name));
|
||||
}
|
||||
}
|
||||
|
||||
public class Thing : ContentPart {
|
||||
public int Id { get { return ContentItem.Id; } }
|
||||
|
||||
public string Text {
|
||||
get { return this.As<BodyPart>().Text; }
|
||||
set { this.As<BodyPart>().Text = value; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class ThingDriver : ContentItemDriver<Thing> {
|
||||
public readonly static ContentType ContentType = new ContentType {
|
||||
Name = "thing",
|
||||
DisplayName = "Thing"
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -101,6 +101,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Common\Providers\CommonPartProviderTests.cs" />
|
||||
<Compile Include="Body\BodyPartTests.cs" />
|
||||
<Compile Include="Routable\Services\RoutableServiceTests.cs" />
|
||||
<Compile Include="Feeds\Controllers\FeedControllerTests.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
@@ -14,122 +14,44 @@ using Orchard.Tests.Records;
|
||||
namespace Orchard.Tests.Data.Builders {
|
||||
[TestFixture]
|
||||
public class SessionFactoryBuilderTests {
|
||||
private string _tempDataFolder;
|
||||
|
||||
[SetUp]
|
||||
public void Init() {
|
||||
var tempFilePath = Path.GetTempFileName();
|
||||
File.Delete(tempFilePath);
|
||||
Directory.CreateDirectory(tempFilePath);
|
||||
_tempDataFolder = tempFilePath;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void Term() {
|
||||
try { Directory.Delete(_tempDataFolder, true); }
|
||||
catch (IOException) { }
|
||||
}
|
||||
|
||||
private static void CreateSqlServerDatabase(string databasePath) {
|
||||
var databaseName = Path.GetFileNameWithoutExtension(databasePath);
|
||||
using (var connection = new SqlConnection(
|
||||
"Data Source=.\\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=true;User Instance=True;")) {
|
||||
connection.Open();
|
||||
using (var command = connection.CreateCommand()) {
|
||||
command.CommandText =
|
||||
"CREATE DATABASE " + databaseName +
|
||||
" ON PRIMARY (NAME=" + databaseName +
|
||||
", FILENAME='" + databasePath.Replace("'", "''") + "')";
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
command.CommandText =
|
||||
"EXEC sp_detach_db '" + databaseName + "', 'true'";
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
[Test]
|
||||
public void SqlCeSchemaShouldBeGeneratedAndUsable() {
|
||||
|
||||
var recordDescriptors = new[] {
|
||||
new RecordBlueprint {TableName = "Hello", Type = typeof (FooRecord)}
|
||||
};
|
||||
|
||||
var parameters = new SessionFactoryParameters {
|
||||
Provider = "SqlCe",
|
||||
DataFolder = _tempDataFolder,
|
||||
RecordDescriptors = recordDescriptors
|
||||
};
|
||||
|
||||
var manager = (IDataServicesProviderFactory) new DataServicesProviderFactory(new[] {
|
||||
new Meta<CreateDataServicesProvider>(
|
||||
(dataFolder, connectionString) => new SqlCeDataServicesProvider(dataFolder, connectionString),
|
||||
new Dictionary<string, object> {{"ProviderName", "SqlCe"}})
|
||||
});
|
||||
|
||||
var configuration = manager
|
||||
.CreateProvider(parameters)
|
||||
.BuildConfiguration(parameters);
|
||||
|
||||
configuration.SetProperty("connection.release_mode", "on_close");
|
||||
|
||||
new SchemaExport(configuration).Execute(false, true, false);
|
||||
|
||||
var sessionFactory = configuration.BuildSessionFactory();
|
||||
|
||||
var session = sessionFactory.OpenSession();
|
||||
var foo = new FooRecord {Name = "hi there", Id = 1};
|
||||
session.Save(foo);
|
||||
session.Flush();
|
||||
session.Close();
|
||||
|
||||
Assert.That(foo, Is.Not.EqualTo(0));
|
||||
|
||||
sessionFactory.Close();
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SqlServerSchemaShouldBeGeneratedAndUsable() {
|
||||
var databasePath = Path.Combine(_tempDataFolder, "Orchard.mdf");
|
||||
CreateSqlServerDatabase(databasePath);
|
||||
|
||||
var recordDescriptors = new[] {
|
||||
new RecordBlueprint {TableName = "Hello", Type = typeof (FooRecord)}
|
||||
};
|
||||
|
||||
var manager = (IDataServicesProviderFactory)new DataServicesProviderFactory(new[] {
|
||||
new Meta<CreateDataServicesProvider>(
|
||||
(dataFolder, connectionString) => new SqlServerDataServicesProvider(dataFolder, connectionString),
|
||||
new Dictionary<string, object> {{"ProviderName", "SqlServer"}})
|
||||
});
|
||||
var parameters = new SessionFactoryParameters {
|
||||
Provider = "SqlServer",
|
||||
DataFolder = _tempDataFolder,
|
||||
ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFileName=" + databasePath + ";Integrated Security=True;User Instance=True;",
|
||||
RecordDescriptors = recordDescriptors,
|
||||
};
|
||||
ProviderUtilities.RunWithSqlCe(recordDescriptors,
|
||||
sessionFactory => {
|
||||
var session = sessionFactory.OpenSession();
|
||||
var foo = new FooRecord { Name = "hi there" };
|
||||
session.Save(foo);
|
||||
session.Flush();
|
||||
session.Close();
|
||||
|
||||
var configuration = manager
|
||||
.CreateProvider(parameters)
|
||||
.BuildConfiguration(parameters);
|
||||
Assert.That(foo, Is.Not.EqualTo(0));
|
||||
|
||||
new SchemaExport(configuration).Execute(false, true, false);
|
||||
});
|
||||
|
||||
var sessionFactory = configuration.BuildSessionFactory();
|
||||
}
|
||||
|
||||
var session = sessionFactory.OpenSession();
|
||||
var foo = new FooRecord { Name = "hi there" };
|
||||
session.Save(foo);
|
||||
session.Flush();
|
||||
session.Close();
|
||||
[Test]
|
||||
public void SqlServerSchemaShouldBeGeneratedAndUsable() {
|
||||
var recordDescriptors = new[] {
|
||||
new RecordBlueprint {TableName = "Hello", Type = typeof (FooRecord)}
|
||||
};
|
||||
|
||||
Assert.That(foo, Is.Not.EqualTo(0));
|
||||
ProviderUtilities.RunWithSqlServer(recordDescriptors,
|
||||
sessionFactory => {
|
||||
var session = sessionFactory.OpenSession();
|
||||
var foo = new FooRecord { Name = "hi there" };
|
||||
session.Save(foo);
|
||||
session.Flush();
|
||||
session.Close();
|
||||
|
||||
sessionFactory.Close();
|
||||
Assert.That(foo, Is.Not.EqualTo(0));
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
110
src/Orchard.Tests/Data/ProviderUtilities.cs
Normal file
110
src/Orchard.Tests/Data/ProviderUtilities.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.IO;
|
||||
using Autofac.Features.Metadata;
|
||||
using NHibernate;
|
||||
using NHibernate.Tool.hbm2ddl;
|
||||
using Orchard.Data.Providers;
|
||||
using Orchard.Environment.ShellBuilders.Models;
|
||||
|
||||
namespace Orchard.Tests.Data {
|
||||
public class ProviderUtilities {
|
||||
|
||||
public static void RunWithSqlServer(IEnumerable<RecordBlueprint> recordDescriptors, Action<ISessionFactory> action) {
|
||||
var temporaryPath = Path.GetTempFileName();
|
||||
if(File.Exists(temporaryPath))
|
||||
File.Delete(temporaryPath);
|
||||
Directory.CreateDirectory(temporaryPath);
|
||||
var databasePath = Path.Combine(temporaryPath, "Orchard.mdf");
|
||||
var databaseName = Path.GetFileNameWithoutExtension(databasePath);
|
||||
try {
|
||||
|
||||
// create database
|
||||
using ( var connection = new SqlConnection(
|
||||
"Data Source=.\\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=true;User Instance=True;") ) {
|
||||
connection.Open();
|
||||
using ( var command = connection.CreateCommand() ) {
|
||||
command.CommandText =
|
||||
"CREATE DATABASE " + databaseName +
|
||||
" ON PRIMARY (NAME=" + databaseName +
|
||||
", FILENAME='" + databasePath.Replace("'", "''") + "')";
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
command.CommandText =
|
||||
"EXEC sp_detach_db '" + databaseName + "', 'true'";
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
var manager = (IDataServicesProviderFactory)new DataServicesProviderFactory(new[] {
|
||||
new Meta<CreateDataServicesProvider>(
|
||||
(dataFolder, connectionString) => new SqlServerDataServicesProvider(dataFolder, connectionString),
|
||||
new Dictionary<string, object> {{"ProviderName", "SqlServer"}})
|
||||
});
|
||||
var parameters = new SessionFactoryParameters {
|
||||
Provider = "SqlServer",
|
||||
DataFolder = temporaryPath,
|
||||
ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFileName=" + databasePath + ";Integrated Security=True;User Instance=True;",
|
||||
RecordDescriptors = recordDescriptors,
|
||||
};
|
||||
|
||||
var configuration = manager
|
||||
.CreateProvider(parameters)
|
||||
.BuildConfiguration(parameters);
|
||||
|
||||
new SchemaExport(configuration).Execute(false, true, false);
|
||||
|
||||
using ( var sessionFactory = configuration.BuildSessionFactory() ) {
|
||||
action(sessionFactory);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
Directory.Delete(temporaryPath, true);
|
||||
}
|
||||
catch (IOException) {}
|
||||
}
|
||||
}
|
||||
|
||||
public static void RunWithSqlCe(IEnumerable<RecordBlueprint> recordDescriptors, Action<ISessionFactory> action) {
|
||||
var temporaryPath = Path.GetTempFileName();
|
||||
if ( File.Exists(temporaryPath) )
|
||||
File.Delete(temporaryPath);
|
||||
Directory.CreateDirectory(temporaryPath);
|
||||
var databasePath = Path.Combine(temporaryPath, "Orchard.mdf");
|
||||
var databaseName = Path.GetFileNameWithoutExtension(databasePath);
|
||||
var parameters = new SessionFactoryParameters {
|
||||
Provider = "SqlCe",
|
||||
DataFolder = temporaryPath,
|
||||
RecordDescriptors = recordDescriptors
|
||||
};
|
||||
try {
|
||||
var manager = (IDataServicesProviderFactory)new DataServicesProviderFactory(new[] {
|
||||
new Meta<CreateDataServicesProvider>(
|
||||
(dataFolder, connectionString) => new SqlCeDataServicesProvider(dataFolder, connectionString),
|
||||
new Dictionary<string, object> {{"ProviderName", "SqlCe"}})
|
||||
});
|
||||
|
||||
var configuration = manager
|
||||
.CreateProvider(parameters)
|
||||
.BuildConfiguration(parameters);
|
||||
|
||||
configuration.SetProperty("connection.release_mode", "on_close");
|
||||
|
||||
new SchemaExport(configuration).Execute(false, true, false);
|
||||
|
||||
using ( var sessionFactory = configuration.BuildSessionFactory() ) {
|
||||
action(sessionFactory);
|
||||
}
|
||||
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
Directory.Delete(temporaryPath, true);
|
||||
}
|
||||
catch (IOException) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
60
src/Orchard.Tests/Data/ProvidersTests.cs
Normal file
60
src/Orchard.Tests/Data/ProvidersTests.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using NUnit.Framework;
|
||||
using Orchard.Environment.ShellBuilders.Models;
|
||||
using Orchard.Tests.Records;
|
||||
|
||||
namespace Orchard.Tests.Data {
|
||||
[TestFixture]
|
||||
public class ProvidersTests {
|
||||
|
||||
[Test]
|
||||
public void SqlCeShouldHandleBigFields() {
|
||||
|
||||
var recordDescriptors = new[] {
|
||||
new RecordBlueprint {TableName = "Big", Type = typeof (BigRecord)}
|
||||
};
|
||||
|
||||
ProviderUtilities.RunWithSqlCe(recordDescriptors,
|
||||
sessionFactory => {
|
||||
var session = sessionFactory.OpenSession();
|
||||
var foo = new BigRecord { Body = new String('x', 10000) };
|
||||
session.Save(foo);
|
||||
session.Flush();
|
||||
session.Close();
|
||||
|
||||
session = sessionFactory.OpenSession();
|
||||
foo = session.Get<BigRecord>(foo.Id);
|
||||
session.Close();
|
||||
|
||||
Assert.That(foo, Is.Not.Null);
|
||||
Assert.That(foo.Body, Is.EqualTo(new String('x', 10000)));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void SqlServerShouldHandleBigFields() {
|
||||
|
||||
var recordDescriptors = new[] {
|
||||
new RecordBlueprint {TableName = "Big", Type = typeof (BigRecord)}
|
||||
};
|
||||
|
||||
ProviderUtilities.RunWithSqlServer(recordDescriptors,
|
||||
sessionFactory => {
|
||||
var session = sessionFactory.OpenSession();
|
||||
var foo = new BigRecord { Body = new String('x', 10000) };
|
||||
session.Save(foo);
|
||||
session.Flush();
|
||||
session.Close();
|
||||
|
||||
session = sessionFactory.OpenSession();
|
||||
foo = session.Get<BigRecord>(foo.Id);
|
||||
session.Close();
|
||||
|
||||
Assert.That(foo, Is.Not.Null);
|
||||
Assert.That(foo.Body, Is.EqualTo(new String('x', 10000)));
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@@ -188,7 +188,9 @@
|
||||
<Compile Include="DataMigration\Utilities\NullInterpreter.cs" />
|
||||
<Compile Include="DataUtility.cs" />
|
||||
<Compile Include="Data\Builders\SessionFactoryBuilderTests.cs" />
|
||||
<Compile Include="Data\ProviderUtilities.cs" />
|
||||
<Compile Include="Data\RepositoryTests.cs" />
|
||||
<Compile Include="Data\ProvidersTests.cs" />
|
||||
<Compile Include="Data\StubLocator.cs" />
|
||||
<Compile Include="Environment\AutofacUtil\AutofacTests.cs" />
|
||||
<Compile Include="Environment\AutofacUtil\DynamicProxy2\DynamicProxyTests.cs" />
|
||||
@@ -210,6 +212,7 @@
|
||||
<Compile Include="Mvc\Html\HtmlHelperExtensionsTests.cs" />
|
||||
<Compile Include="Mvc\Routes\ShellRouteTests.cs" />
|
||||
<Compile Include="Mvc\Routes\UrlPrefixTests.cs" />
|
||||
<Compile Include="Records\BigRecord.cs" />
|
||||
<Compile Include="Stubs\StubReportsCoordinator.cs" />
|
||||
<Compile Include="Stubs\StubVirtualPathProvider.cs" />
|
||||
<Compile Include="Stubs\StubFileSystem.cs" />
|
||||
|
9
src/Orchard.Tests/Records/BigRecord.cs
Normal file
9
src/Orchard.Tests/Records/BigRecord.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Orchard.Data.Conventions;
|
||||
|
||||
namespace Orchard.Tests.Records {
|
||||
public class BigRecord {
|
||||
public virtual int Id { get; set; }
|
||||
[StringLengthMax]
|
||||
public virtual string Body { get; set; }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user