|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using NHibernate.Cfg; |
| 4 | +using NHibernate.Cfg.MappingSchema; |
| 5 | +using NHibernate.Dialect; |
| 6 | +using NHibernate.Mapping.ByCode; |
| 7 | +using NUnit.Framework; |
| 8 | + |
| 9 | +namespace NHibernate.Test.NHSpecificTest.GH2110 |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Verify that we can convert a GUID column to a string in the standard GUID format inside SQLite |
| 13 | + /// regardless of the BinaryGuid SQLite setting. |
| 14 | + /// </summary> |
| 15 | + [TestFixture(true)] |
| 16 | + [TestFixture(false)] |
| 17 | + public class ByCodeFixture : TestCaseMappingByCode |
| 18 | + { |
| 19 | + private readonly bool _useBinaryGuid; |
| 20 | + |
| 21 | + public ByCodeFixture(bool useBinaryGuid) |
| 22 | + { |
| 23 | + _useBinaryGuid = useBinaryGuid; |
| 24 | + } |
| 25 | + |
| 26 | + protected override bool AppliesTo(Dialect.Dialect dialect) |
| 27 | + { |
| 28 | + return dialect is SQLiteDialect; |
| 29 | + } |
| 30 | + |
| 31 | + protected override void Configure(Configuration configuration) |
| 32 | + { |
| 33 | + base.Configure(configuration); |
| 34 | + |
| 35 | + var connStr = configuration.Properties["connection.connection_string"]; |
| 36 | + |
| 37 | + if (_useBinaryGuid) |
| 38 | + connStr += "BinaryGuid=True;"; |
| 39 | + else |
| 40 | + connStr += "BinaryGuid=False;"; |
| 41 | + |
| 42 | + configuration.Properties["connection.connection_string"] = connStr; |
| 43 | + } |
| 44 | + |
| 45 | + protected override HbmMapping GetMappings() |
| 46 | + { |
| 47 | + var mapper = new ModelMapper(); |
| 48 | + mapper.Class<Entity>(rc => |
| 49 | + { |
| 50 | + rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); |
| 51 | + rc.Property(x => x.Name); |
| 52 | + }); |
| 53 | + |
| 54 | + return mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 55 | + } |
| 56 | + |
| 57 | + protected override void OnSetUp() |
| 58 | + { |
| 59 | + using (var session = OpenSession()) |
| 60 | + using (var transaction = session.BeginTransaction()) |
| 61 | + { |
| 62 | + var e1 = new Entity { Name = "Bob" }; |
| 63 | + session.Save(e1); |
| 64 | + |
| 65 | + var e2 = new Entity { Name = "Sally" }; |
| 66 | + session.Save(e2); |
| 67 | + |
| 68 | + transaction.Commit(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + protected override void OnTearDown() |
| 73 | + { |
| 74 | + using (var session = OpenSession()) |
| 75 | + using (var transaction = session.BeginTransaction()) |
| 76 | + { |
| 77 | + // The HQL delete does all the job inside the database without loading the entities, but it does |
| 78 | + // not handle delete order for avoiding violating constraints if any. Use |
| 79 | + // session.Delete("from System.Object"); |
| 80 | + // instead if in need of having NHibernate ordering the deletes, but this will cause |
| 81 | + // loading the entities in the session. |
| 82 | + session.CreateQuery("delete from System.Object").ExecuteUpdate(); |
| 83 | + |
| 84 | + transaction.Commit(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + [Test] |
| 89 | + public void DbExplicitIdGuidToStringIsSameAsDotNet() |
| 90 | + { |
| 91 | + using (var session = OpenSession()) |
| 92 | + using (var transaction = session.BeginTransaction()) |
| 93 | + { |
| 94 | + List<string> guidCsharpStrings = |
| 95 | + session.Query<Entity>().Select(e => e.Id).ToList().Select(guid => guid.ToString()).ToList(); |
| 96 | + |
| 97 | + // Verify in-db GUID to string conversion when ToString() is applied directly to the GUID column. |
| 98 | + List<string> guidDbStringsExplicitId = |
| 99 | + session.Query<Entity>().Select(e => e.Id.ToString()).ToList() |
| 100 | + .Select(s => s.ToLower()).ToList(); |
| 101 | + |
| 102 | + Assert.That(guidDbStringsExplicitId, Is.EquivalentTo(guidCsharpStrings)); |
| 103 | + transaction.Commit(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + [Test] |
| 108 | + public void DbImplicitIdGuidToStringIsSameAsDotNet() |
| 109 | + { |
| 110 | + using (var session = OpenSession()) |
| 111 | + using (var transaction = session.BeginTransaction()) |
| 112 | + { |
| 113 | + List<string> guidCsharpStrings = |
| 114 | + session.Query<Entity>().Select(e => e.Id).ToList().Select(guid => guid.ToString()).ToList(); |
| 115 | + |
| 116 | + // Verify in-db GUID to string conversion when ToString() is applied to the entity that has |
| 117 | + // a GUID id column (that is, we deliberately avoid mentioning the Id property). This |
| 118 | + // exposes bug GH-2109. |
| 119 | + List<string> guidDbStringsImplicitId = |
| 120 | + session.Query<Entity>().Select(e => e.ToString()).ToList() |
| 121 | + .Select(s => s.ToLower()).ToList(); |
| 122 | + |
| 123 | + Assert.That(guidDbStringsImplicitId, Is.EquivalentTo(guidCsharpStrings)); |
| 124 | + transaction.Commit(); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments