|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// <auto-generated> |
| 3 | +// This code was generated by AsyncGenerator. |
| 4 | +// |
| 5 | +// Changes to this file may cause incorrect behavior and will be lost if |
| 6 | +// the code is regenerated. |
| 7 | +// </auto-generated> |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | + |
| 10 | + |
| 11 | +using System.Data; |
| 12 | +using System.Data.SqlClient; |
| 13 | +using System.Linq; |
| 14 | +using NHibernate.Cfg; |
| 15 | +using NHibernate.Criterion; |
| 16 | +using NHibernate.Dialect; |
| 17 | +using NHibernate.Driver; |
| 18 | +using NHibernate.Engine; |
| 19 | +using NHibernate.Exceptions; |
| 20 | +using NHibernate.Linq; |
| 21 | +using NUnit.Framework; |
| 22 | + |
| 23 | +namespace NHibernate.Test.NHSpecificTest.NH3403 |
| 24 | +{ |
| 25 | + using System.Threading.Tasks; |
| 26 | + using System.Threading; |
| 27 | + [TestFixture] |
| 28 | + public class FixtureAsync : BugTestCase |
| 29 | + { |
| 30 | + protected override bool AppliesTo(ISessionFactoryImplementor factory) |
| 31 | + { |
| 32 | + return factory.ConnectionProvider.Driver is SqlClientDriver; |
| 33 | + } |
| 34 | + |
| 35 | + protected override void Configure(Configuration configuration) |
| 36 | + { |
| 37 | + cfg.SetProperty(Environment.ConnectionDriver, typeof(TestSqlClientDriver).AssemblyQualifiedName); |
| 38 | + } |
| 39 | + |
| 40 | + protected override void OnTearDown() |
| 41 | + { |
| 42 | + using (ISession session = OpenSession()) |
| 43 | + using (ITransaction transaction = session.BeginTransaction()) |
| 44 | + { |
| 45 | + session.Delete("from System.Object"); |
| 46 | + |
| 47 | + session.Flush(); |
| 48 | + transaction.Commit(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + protected override void OnSetUp() |
| 53 | + { |
| 54 | + base.OnSetUp(); |
| 55 | + using (ISession session = OpenSession()) |
| 56 | + using (ITransaction transaction = session.BeginTransaction()) |
| 57 | + { |
| 58 | + var e1 = new Entity { Name = "Bob" }; |
| 59 | + session.Save(e1); |
| 60 | + transaction.Commit(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + [Test] |
| 65 | + public async Task InsertShouldUseMappedSizeAsync() |
| 66 | + { |
| 67 | + Driver.ClearCommands(); |
| 68 | + |
| 69 | + using (ISession session = OpenSession()) |
| 70 | + using (ITransaction transaction = session.BeginTransaction()) |
| 71 | + { |
| 72 | + var e1 = new Entity { Name = "Al", AnsiName = "Al" }; |
| 73 | + await (session.SaveAsync(e1)); |
| 74 | + await (transaction.CommitAsync()); |
| 75 | + Assert.AreEqual(SqlDbType.NVarChar, Driver.LastCommandParameters.First().SqlDbType); |
| 76 | + Assert.AreEqual(3, Driver.LastCommandParameters.First().Size); |
| 77 | + Assert.AreEqual(SqlDbType.VarChar, Driver.LastCommandParameters.Last().SqlDbType); |
| 78 | + Assert.AreEqual(3, Driver.LastCommandParameters.Last().Size); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + [Test] |
| 83 | + public void InsertWithTooLongValuesShouldThrowAsync() |
| 84 | + { |
| 85 | + Driver.ClearCommands(); |
| 86 | + |
| 87 | + using (ISession session = OpenSession()) |
| 88 | + using (ITransaction transaction = session.BeginTransaction()) |
| 89 | + { |
| 90 | + var e1 = new Entity { Name = "Alal", AnsiName = "Alal" }; |
| 91 | + |
| 92 | + var ex = Assert.ThrowsAsync<GenericADOException>( |
| 93 | + async () => |
| 94 | + { |
| 95 | + await (session.SaveAsync(e1)); |
| 96 | + await (transaction.CommitAsync()); |
| 97 | + }); |
| 98 | + |
| 99 | + var sqlEx = ex.InnerException as SqlException; |
| 100 | + Assert.IsNotNull(sqlEx); |
| 101 | + Assert.That(sqlEx.Number, Is.EqualTo(8152)); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + [TestCase("Name", SqlDbType.NVarChar)] |
| 106 | + [TestCase("AnsiName", SqlDbType.VarChar)] |
| 107 | + public async Task LinqEqualsShouldUseMappedSizeAsync(string property, SqlDbType expectedDbType, CancellationToken cancellationToken = default(CancellationToken)) |
| 108 | + { |
| 109 | + Driver.ClearCommands(); |
| 110 | + |
| 111 | + using (ISession session = OpenSession()) |
| 112 | + using (ITransaction transaction = session.BeginTransaction()) |
| 113 | + { |
| 114 | + if (property == "Name") |
| 115 | + { |
| 116 | + await (session.Query<Entity>().Where(x => x.Name == "Bob").ToListAsync(cancellationToken)); |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + await (session.Query<Entity>().Where(x => x.AnsiName == "Bob").ToListAsync(cancellationToken)); |
| 121 | + } |
| 122 | + Assert.AreEqual(3, Driver.LastCommandParameters.First().Size); |
| 123 | + Assert.AreEqual(expectedDbType, Driver.LastCommandParameters.First().SqlDbType); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + [TestCase("Name", SqlDbType.NVarChar)] |
| 128 | + [TestCase("AnsiName", SqlDbType.VarChar)] |
| 129 | + public async Task HqlLikeShouldUseLargerSizeAsync(string property, SqlDbType expectedDbType, CancellationToken cancellationToken = default(CancellationToken)) |
| 130 | + { |
| 131 | + Driver.ClearCommands(); |
| 132 | + |
| 133 | + using (ISession session = OpenSession()) |
| 134 | + using (ITransaction transaction = session.BeginTransaction()) |
| 135 | + { |
| 136 | + await (session.CreateQuery("from Entity where " + property + " like :name").SetParameter("name", "%Bob%").ListAsync<Entity>(cancellationToken)); |
| 137 | + |
| 138 | + Assert.GreaterOrEqual(Driver.LastCommandParameters.First().Size, 5); |
| 139 | + Assert.AreEqual(expectedDbType, Driver.LastCommandParameters.First().SqlDbType); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + [TestCase("Name", SqlDbType.NVarChar)] |
| 144 | + [TestCase("AnsiName", SqlDbType.VarChar)] |
| 145 | + public async Task CriteriaEqualsShouldUseMappedSizeAsync(string property, SqlDbType expectedDbType, CancellationToken cancellationToken = default(CancellationToken)) |
| 146 | + { |
| 147 | + Driver.ClearCommands(); |
| 148 | + |
| 149 | + using (ISession session = OpenSession()) |
| 150 | + using (ITransaction transaction = session.BeginTransaction()) |
| 151 | + { |
| 152 | + Driver.ClearCommands(); |
| 153 | + |
| 154 | + await (session.CreateCriteria<Entity>().Add(Restrictions.Eq(property, "Bob")) |
| 155 | + .ListAsync<Entity>(cancellationToken)); |
| 156 | + |
| 157 | + Assert.GreaterOrEqual(Driver.LastCommandParameters.First().Size, 3); |
| 158 | + Assert.AreEqual(expectedDbType, Driver.LastCommandParameters.First().SqlDbType); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + [TestCase("Name", SqlDbType.NVarChar)] |
| 163 | + [TestCase("AnsiName", SqlDbType.VarChar)] |
| 164 | + public async Task CriteriaLikeShouldUseLargerSizeAsync(string property, SqlDbType expectedDbType, CancellationToken cancellationToken = default(CancellationToken)) |
| 165 | + { |
| 166 | + Driver.ClearCommands(); |
| 167 | + |
| 168 | + using (ISession session = OpenSession()) |
| 169 | + using (ITransaction transaction = session.BeginTransaction()) |
| 170 | + { |
| 171 | + await (session.CreateCriteria<Entity>().Add(Restrictions.Like(property, "%Bob%")) |
| 172 | + .ListAsync<Entity>(cancellationToken)); |
| 173 | + |
| 174 | + Assert.GreaterOrEqual(Driver.LastCommandParameters.First().Size, 5); |
| 175 | + Assert.AreEqual(expectedDbType, Driver.LastCommandParameters.First().SqlDbType); |
| 176 | + } |
| 177 | + } |
| 178 | + private TestSqlClientDriver Driver |
| 179 | + { |
| 180 | + get { return Sfi.ConnectionProvider.Driver as TestSqlClientDriver; } |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments