From e81ec58497012d884785312c7d13cb30dc52a33d Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Tue, 31 Aug 2010 12:13:55 -0700 Subject: [PATCH] Added unit tests for big text --HG-- branch : dev --- src/Orchard.Core.Tests/Body/BodyPartTests.cs | 100 ++++++++++++++ .../Orchard.Core.Tests.csproj | 1 + .../Builders/SessionFactoryBuilderTests.cs | 128 ++++-------------- src/Orchard.Tests/Data/ProviderUtilities.cs | 110 +++++++++++++++ src/Orchard.Tests/Data/ProvidersTests.cs | 60 ++++++++ .../Orchard.Framework.Tests.csproj | 3 + src/Orchard.Tests/Records/BigRecord.cs | 9 ++ 7 files changed, 308 insertions(+), 103 deletions(-) create mode 100644 src/Orchard.Core.Tests/Body/BodyPartTests.cs create mode 100644 src/Orchard.Tests/Data/ProviderUtilities.cs create mode 100644 src/Orchard.Tests/Data/ProvidersTests.cs create mode 100644 src/Orchard.Tests/Records/BigRecord.cs diff --git a/src/Orchard.Core.Tests/Body/BodyPartTests.cs b/src/Orchard.Core.Tests/Body/BodyPartTests.cs new file mode 100644 index 000000000..03319a0bf --- /dev/null +++ b/src/Orchard.Core.Tests/Body/BodyPartTests.cs @@ -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().As(); + builder.RegisterType().As(); + builder.RegisterInstance(new Mock().Object); + builder.RegisterInstance(new Mock().Object); + builder.RegisterInstance(new Mock().Object); + builder.RegisterInstance(new Mock().Object); + builder.RegisterType().As(); + + builder.RegisterType().As(); + + builder.RegisterType().As(); + builder.RegisterType().As(); + } + + [Test] + public void BodyCanHandleLongText() { + var contentManager = _container.Resolve(); + + contentManager.Create(ThingDriver.ContentType.Name, t => { + t.As().Record = new BodyPartRecord(); + t.Text = new String('x', 10000); + }); + + var bodies = contentManager.Query().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 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(ThingDriver.ContentType.Name)); + Filters.Add(new ActivatingFilter>(ThingDriver.ContentType.Name)); + Filters.Add(new ActivatingFilter(ThingDriver.ContentType.Name)); + Filters.Add(new ActivatingFilter(ThingDriver.ContentType.Name)); + } + } + + public class Thing : ContentPart { + public int Id { get { return ContentItem.Id; } } + + public string Text { + get { return this.As().Text; } + set { this.As().Text = value; } + } + + } + + public class ThingDriver : ContentItemDriver { + public readonly static ContentType ContentType = new ContentType { + Name = "thing", + DisplayName = "Thing" + }; + } + + } +} \ No newline at end of file diff --git a/src/Orchard.Core.Tests/Orchard.Core.Tests.csproj b/src/Orchard.Core.Tests/Orchard.Core.Tests.csproj index 1c720d5d5..41c019947 100644 --- a/src/Orchard.Core.Tests/Orchard.Core.Tests.csproj +++ b/src/Orchard.Core.Tests/Orchard.Core.Tests.csproj @@ -101,6 +101,7 @@ + diff --git a/src/Orchard.Tests/Data/Builders/SessionFactoryBuilderTests.cs b/src/Orchard.Tests/Data/Builders/SessionFactoryBuilderTests.cs index e8e0f8799..014e0c911 100644 --- a/src/Orchard.Tests/Data/Builders/SessionFactoryBuilderTests.cs +++ b/src/Orchard.Tests/Data/Builders/SessionFactoryBuilderTests.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( - (dataFolder, connectionString) => new SqlCeDataServicesProvider(dataFolder, connectionString), - new Dictionary {{"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( - (dataFolder, connectionString) => new SqlServerDataServicesProvider(dataFolder, connectionString), - new Dictionary {{"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)); + + }); } } } \ No newline at end of file diff --git a/src/Orchard.Tests/Data/ProviderUtilities.cs b/src/Orchard.Tests/Data/ProviderUtilities.cs new file mode 100644 index 000000000..cf7b76f17 --- /dev/null +++ b/src/Orchard.Tests/Data/ProviderUtilities.cs @@ -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 recordDescriptors, Action 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( + (dataFolder, connectionString) => new SqlServerDataServicesProvider(dataFolder, connectionString), + new Dictionary {{"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 recordDescriptors, Action 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( + (dataFolder, connectionString) => new SqlCeDataServicesProvider(dataFolder, connectionString), + new Dictionary {{"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) {} + } + } + } +} diff --git a/src/Orchard.Tests/Data/ProvidersTests.cs b/src/Orchard.Tests/Data/ProvidersTests.cs new file mode 100644 index 000000000..1f4dcaf1c --- /dev/null +++ b/src/Orchard.Tests/Data/ProvidersTests.cs @@ -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(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(foo.Id); + session.Close(); + + Assert.That(foo, Is.Not.Null); + Assert.That(foo.Body, Is.EqualTo(new String('x', 10000))); + + }); + } + } +} \ No newline at end of file diff --git a/src/Orchard.Tests/Orchard.Framework.Tests.csproj b/src/Orchard.Tests/Orchard.Framework.Tests.csproj index 66858c551..14dd9970c 100644 --- a/src/Orchard.Tests/Orchard.Framework.Tests.csproj +++ b/src/Orchard.Tests/Orchard.Framework.Tests.csproj @@ -188,7 +188,9 @@ + + @@ -210,6 +212,7 @@ + diff --git a/src/Orchard.Tests/Records/BigRecord.cs b/src/Orchard.Tests/Records/BigRecord.cs new file mode 100644 index 000000000..2a5e1bfd2 --- /dev/null +++ b/src/Orchard.Tests/Records/BigRecord.cs @@ -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; } + } +} \ No newline at end of file