|
1 | 1 | using System;
|
2 | 2 | using System.Linq;
|
| 3 | +using NHibernate.Cfg; |
3 | 4 | using NHibernate.Cfg.MappingSchema;
|
| 5 | +using NHibernate.Dialect; |
4 | 6 | using NHibernate.Mapping.ByCode;
|
5 | 7 | using NUnit.Framework;
|
| 8 | +using Environment = NHibernate.Cfg.Environment; |
6 | 9 |
|
7 | 10 | namespace NHibernate.Test.NHSpecificTest.NH3426
|
8 | 11 | {
|
9 |
| - [TestFixture] |
| 12 | + /// <summary> |
| 13 | + /// Verify that we can convert a GUID column to a string in the standard GUID format inside |
| 14 | + /// the database engine. |
| 15 | + /// </summary> |
| 16 | + [TestFixture(true)] |
| 17 | + [TestFixture(false)] |
10 | 18 | public class Fixture : TestCaseMappingByCode
|
11 | 19 | {
|
| 20 | + private readonly bool _useBinaryGuid; |
| 21 | + |
| 22 | + public Fixture(bool useBinaryGuid) |
| 23 | + { |
| 24 | + _useBinaryGuid = useBinaryGuid; |
| 25 | + } |
| 26 | + |
| 27 | + protected override bool AppliesTo(Dialect.Dialect dialect) |
| 28 | + { |
| 29 | + // For SQLite, we run the tests for both storage modes (SQLite specific setting). |
| 30 | + if (dialect is SQLiteDialect) |
| 31 | + return true; |
| 32 | + |
| 33 | + // For all other dialects, run the tests only once since the storage mode |
| 34 | + // is not relevant. (We use the case of _useBinaryGuid==true since this is probably |
| 35 | + // what most engines do internally.) |
| 36 | + return _useBinaryGuid; |
| 37 | + } |
| 38 | + |
| 39 | + protected override void Configure(Configuration configuration) |
| 40 | + { |
| 41 | + base.Configure(configuration); |
| 42 | + |
| 43 | + if (Dialect is SQLiteDialect) |
| 44 | + { |
| 45 | + var connStr = configuration.Properties[Environment.ConnectionString]; |
| 46 | + |
| 47 | + if (_useBinaryGuid) |
| 48 | + connStr += "BinaryGuid=True;"; |
| 49 | + else |
| 50 | + connStr += "BinaryGuid=False;"; |
| 51 | + |
| 52 | + configuration.Properties[Environment.ConnectionString] = connStr; |
| 53 | + } |
| 54 | + } |
12 | 55 |
|
13 | 56 | protected override HbmMapping GetMappings()
|
14 | 57 | {
|
@@ -56,7 +99,7 @@ public void SelectGuidToString()
|
56 | 99 | .Select(x => new { Id = x.Id.ToString() })
|
57 | 100 | .ToList();
|
58 | 101 |
|
59 |
| - Assert.AreEqual(id.ToUpper(), list[0].Id.ToUpper()); |
| 102 | + Assert.That(list[0].Id.ToUpper(), Is.EqualTo(id.ToUpper())); |
60 | 103 | }
|
61 | 104 | }
|
62 | 105 |
|
@@ -98,5 +141,53 @@ public void CompareStringColumnWithNullableGuidToString()
|
98 | 141 | Assert.That(list, Has.Count.EqualTo(1));
|
99 | 142 | }
|
100 | 143 | }
|
| 144 | + |
| 145 | + [Test] |
| 146 | + public void SelectGuidToStringImplicit() |
| 147 | + { |
| 148 | + if (Dialect is SQLiteDialect && _useBinaryGuid) |
| 149 | + Assert.Ignore("Fails with BinaryGuid=True due to GH-2109. (2019-04-09)."); |
| 150 | + |
| 151 | + if (Dialect is FirebirdDialect || Dialect is MySQLDialect || Dialect is Oracle8iDialect) |
| 152 | + Assert.Ignore("Since strguid() is not applied, it fails on Firebird, MySQL and Oracle " + |
| 153 | + "because a simple cast cannot be used for GUID to string conversion on " + |
| 154 | + "these dialects. See GH-2109."); |
| 155 | + |
| 156 | + using (var session = OpenSession()) |
| 157 | + { |
| 158 | + // Verify in-db GUID to string conversion when ToString() is applied to the entity that has |
| 159 | + // a GUID id column (that is, we deliberately avoid mentioning the Id property). This |
| 160 | + // exposes bug GH-2109. |
| 161 | + var list = session.Query<Entity>() |
| 162 | + .Select(x => new { Id = x.ToString() }) |
| 163 | + .ToList(); |
| 164 | + |
| 165 | + Assert.That(list[0].Id.ToUpper(), Is.EqualTo(id.ToUpper())); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + [Test] |
| 170 | + public void WhereGuidToStringImplicit() |
| 171 | + { |
| 172 | + if (Dialect is SQLiteDialect && _useBinaryGuid) |
| 173 | + Assert.Ignore("Fails with BinaryGuid=True due to GH-2109. (2019-04-09)."); |
| 174 | + |
| 175 | + if (Dialect is FirebirdDialect || Dialect is MySQLDialect || Dialect is Oracle8iDialect) |
| 176 | + Assert.Ignore("Since strguid() is not applied, it fails on Firebird, MySQL and Oracle " + |
| 177 | + "because a simple cast cannot be used for GUID to string conversion on " + |
| 178 | + "these dialects. See GH-2109."); |
| 179 | + |
| 180 | + using (var session = OpenSession()) |
| 181 | + { |
| 182 | + // Verify in-db GUID to string conversion when ToString() is applied to the entity that has |
| 183 | + // a GUID id column (that is, we deliberately avoid mentioning the Id property). This |
| 184 | + // exposes bug GH-2109. |
| 185 | + var list = session.Query<Entity>() |
| 186 | + .Where(x => x.ToString().ToUpper() == id) |
| 187 | + .ToList(); |
| 188 | + |
| 189 | + Assert.That(list, Has.Count.EqualTo(1)); |
| 190 | + } |
| 191 | + } |
101 | 192 | }
|
102 | 193 | }
|
0 commit comments