|
10 | 10 |
|
11 | 11 | using System;
|
12 | 12 | using System.Linq;
|
| 13 | +using NHibernate.Cfg; |
13 | 14 | using NHibernate.Cfg.MappingSchema;
|
| 15 | +using NHibernate.Dialect; |
14 | 16 | using NHibernate.Mapping.ByCode;
|
15 | 17 | using NUnit.Framework;
|
| 18 | +using Environment = NHibernate.Cfg.Environment; |
16 | 19 | using NHibernate.Linq;
|
17 | 20 |
|
18 | 21 | namespace NHibernate.Test.NHSpecificTest.NH3426
|
19 | 22 | {
|
20 | 23 | using System.Threading.Tasks;
|
21 |
| - [TestFixture] |
| 24 | + /// <summary> |
| 25 | + /// Verify that we can convert a GUID column to a string in the standard GUID format inside |
| 26 | + /// the database engine. |
| 27 | + /// </summary> |
| 28 | + [TestFixture(true)] |
| 29 | + [TestFixture(false)] |
22 | 30 | public class FixtureAsync : TestCaseMappingByCode
|
23 | 31 | {
|
| 32 | + private readonly bool _useBinaryGuid; |
| 33 | + |
| 34 | + public FixtureAsync(bool useBinaryGuid) |
| 35 | + { |
| 36 | + _useBinaryGuid = useBinaryGuid; |
| 37 | + } |
| 38 | + |
| 39 | + protected override bool AppliesTo(Dialect.Dialect dialect) |
| 40 | + { |
| 41 | + // For SQLite, we run the tests for both storage modes (SQLite specific setting). |
| 42 | + if (dialect is SQLiteDialect) |
| 43 | + return true; |
| 44 | + |
| 45 | + // For all other dialects, run the tests only once since the storage mode |
| 46 | + // is not relevant. (We use the case of _useBinaryGuid==true since this is probably |
| 47 | + // what most engines do internally.) |
| 48 | + return _useBinaryGuid; |
| 49 | + } |
| 50 | + |
| 51 | + protected override void Configure(Configuration configuration) |
| 52 | + { |
| 53 | + base.Configure(configuration); |
| 54 | + |
| 55 | + if (Dialect is SQLiteDialect) |
| 56 | + { |
| 57 | + var connStr = configuration.Properties[Environment.ConnectionString]; |
| 58 | + |
| 59 | + if (_useBinaryGuid) |
| 60 | + connStr += "BinaryGuid=True;"; |
| 61 | + else |
| 62 | + connStr += "BinaryGuid=False;"; |
| 63 | + |
| 64 | + configuration.Properties[Environment.ConnectionString] = connStr; |
| 65 | + } |
| 66 | + } |
24 | 67 |
|
25 | 68 | protected override HbmMapping GetMappings()
|
26 | 69 | {
|
@@ -68,7 +111,7 @@ public async Task SelectGuidToStringAsync()
|
68 | 111 | .Select(x => new { Id = x.Id.ToString() })
|
69 | 112 | .ToListAsync());
|
70 | 113 |
|
71 |
| - Assert.AreEqual(id.ToUpper(), list[0].Id.ToUpper()); |
| 114 | + Assert.That(list[0].Id.ToUpper(), Is.EqualTo(id.ToUpper())); |
72 | 115 | }
|
73 | 116 | }
|
74 | 117 |
|
@@ -110,5 +153,53 @@ public async Task CompareStringColumnWithNullableGuidToStringAsync()
|
110 | 153 | Assert.That(list, Has.Count.EqualTo(1));
|
111 | 154 | }
|
112 | 155 | }
|
| 156 | + |
| 157 | + [Test] |
| 158 | + public async Task SelectGuidToStringImplicitAsync() |
| 159 | + { |
| 160 | + if (Dialect is SQLiteDialect && _useBinaryGuid) |
| 161 | + Assert.Ignore("Fails with BinaryGuid=True due to GH-2109. (2019-04-09)."); |
| 162 | + |
| 163 | + if (Dialect is FirebirdDialect || Dialect is MySQLDialect || Dialect is Oracle8iDialect) |
| 164 | + Assert.Ignore("Since strguid() is not applied, it fails on Firebird, MySQL and Oracle " + |
| 165 | + "because a simple cast cannot be used for GUID to string conversion on " + |
| 166 | + "these dialects. See GH-2109."); |
| 167 | + |
| 168 | + using (var session = OpenSession()) |
| 169 | + { |
| 170 | + // Verify in-db GUID to string conversion when ToString() is applied to the entity that has |
| 171 | + // a GUID id column (that is, we deliberately avoid mentioning the Id property). This |
| 172 | + // exposes bug GH-2109. |
| 173 | + var list = await (session.Query<Entity>() |
| 174 | + .Select(x => new { Id = x.ToString() }) |
| 175 | + .ToListAsync()); |
| 176 | + |
| 177 | + Assert.That(list[0].Id.ToUpper(), Is.EqualTo(id.ToUpper())); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + [Test] |
| 182 | + public async Task WhereGuidToStringImplicitAsync() |
| 183 | + { |
| 184 | + if (Dialect is SQLiteDialect && _useBinaryGuid) |
| 185 | + Assert.Ignore("Fails with BinaryGuid=True due to GH-2109. (2019-04-09)."); |
| 186 | + |
| 187 | + if (Dialect is FirebirdDialect || Dialect is MySQLDialect || Dialect is Oracle8iDialect) |
| 188 | + Assert.Ignore("Since strguid() is not applied, it fails on Firebird, MySQL and Oracle " + |
| 189 | + "because a simple cast cannot be used for GUID to string conversion on " + |
| 190 | + "these dialects. See GH-2109."); |
| 191 | + |
| 192 | + using (var session = OpenSession()) |
| 193 | + { |
| 194 | + // Verify in-db GUID to string conversion when ToString() is applied to the entity that has |
| 195 | + // a GUID id column (that is, we deliberately avoid mentioning the Id property). This |
| 196 | + // exposes bug GH-2109. |
| 197 | + var list = await (session.Query<Entity>() |
| 198 | + .Where(x => x.ToString().ToUpper() == id) |
| 199 | + .ToListAsync()); |
| 200 | + |
| 201 | + Assert.That(list, Has.Count.EqualTo(1)); |
| 202 | + } |
| 203 | + } |
113 | 204 | }
|
114 | 205 | }
|
0 commit comments