﻿//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by AsyncGenerator.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.Collections.Generic;
using NUnit.Framework;
using NHibernate.Criterion;

namespace NHibernate.Test.NHSpecificTest.NH2546
{
	using System.Threading.Tasks;
	[TestFixture]
	public class SetCommandParameterSizesFalseFixtureAsync : BugTestCase
	{
		protected override bool AppliesTo(Dialect.Dialect dialect)
		{
			return dialect is Dialect.MsSql2008Dialect;
		}

		protected override void OnSetUp()
		{
			using (ISession session = Sfi.OpenSession())
			{
				session.Persist(new Student() { StringTypeWithLengthDefined = "Julian Maughan" });
				session.Persist(new Student() { StringTypeWithLengthDefined = "Bill Clinton" });
				session.Flush();
			}
		}
		
		protected override void OnTearDown()
		{
			using (ISession session = Sfi.OpenSession())
			{
				session.CreateQuery("delete from Student").ExecuteUpdate();
				session.Flush();
			}
			base.OnTearDown();
		}

		[Test]
		public async Task LikeExpressionWithinDefinedTypeSizeAsync()
		{
			using (ISession session = Sfi.OpenSession())
			{
				ICriteria criteria = session
					.CreateCriteria<Student>()
					.Add(Restrictions.Like("StringTypeWithLengthDefined", "Julian%"));
				
				IList<Student> list = await (criteria.ListAsync<Student>());
				
				Assert.That(list.Count, Is.EqualTo(1));
			}
		}
		
		[Test]
		public async Task LikeExpressionExceedsDefinedTypeSizeAsync()
		{
			// In this case we are forcing the usage of LikeExpression class where the length of the associated property is ignored
			using (ISession session = Sfi.OpenSession())
			{
				ICriteria criteria = session
					.CreateCriteria<Student>()
					.Add(Restrictions.Like("StringTypeWithLengthDefined", "[a-z][a-z][a-z]ian%", MatchMode.Exact, null));
				
				IList<Student> list = await (criteria.ListAsync<Student>());
				
				Assert.That(list.Count, Is.EqualTo(1));
			}
		}
	}
}
