|
| 1 | +// Copyright (C) 2020-2021 Xtensive LLC. |
| 2 | +// This code is distributed under MIT license terms. |
| 3 | +// See the License.txt file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Text; |
| 8 | +using NUnit.Framework; |
| 9 | +using Xtensive.Orm.Configuration; |
| 10 | +using Xtensive.Orm.Model; |
| 11 | +using Xtensive.Orm.Services; |
| 12 | +using Xtensive.Orm.Tests.Issues.IssueGitHub0132_IncorrectInsertForSingleTableHierarchyModel; |
| 13 | + |
| 14 | +namespace Xtensive.Orm.Tests.Issues.IssueGitHub0132_IncorrectInsertForSingleTableHierarchyModel |
| 15 | +{ |
| 16 | + [HierarchyRoot(InheritanceSchema.SingleTable)] |
| 17 | + public class A : Entity |
| 18 | + { |
| 19 | + [Field, Key] |
| 20 | + public int Id { get; private set; } |
| 21 | + |
| 22 | + [Field] |
| 23 | + public string Name { get; set; } |
| 24 | + |
| 25 | + public A(Session session) |
| 26 | + : base(session) |
| 27 | + { |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public class B1 : A |
| 32 | + { |
| 33 | + [Field] |
| 34 | + public EntitySet<C> ConflictingField { get; set; } |
| 35 | + |
| 36 | + public B1(Session session) |
| 37 | + : base(session) |
| 38 | + { |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public class B2 : A |
| 43 | + { |
| 44 | + [Field] |
| 45 | + public double ConflictingField { get; set; } |
| 46 | + |
| 47 | + public B2(Session session) |
| 48 | + : base(session) |
| 49 | + { |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + [HierarchyRoot] |
| 54 | + public class C : Entity |
| 55 | + { |
| 56 | + [Field, Key] |
| 57 | + public int Id { get; private set; } |
| 58 | + |
| 59 | + [Field] |
| 60 | + [Association(PairTo = nameof(B1.ConflictingField))] |
| 61 | + public B1 Ref { get; set; } |
| 62 | + |
| 63 | + public C(Session session) |
| 64 | + : base(session) |
| 65 | + { |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +namespace Xtensive.Orm.Tests.Issues |
| 71 | +{ |
| 72 | + public class IssueGitHub0132_IncorrectInsertForSingleTableHierarchy : AutoBuildTest |
| 73 | + { |
| 74 | + protected override DomainConfiguration BuildConfiguration() |
| 75 | + { |
| 76 | + var config = base.BuildConfiguration(); |
| 77 | + config.Types.Register(typeof(A).Assembly, typeof(A).Namespace); |
| 78 | + return config; |
| 79 | + } |
| 80 | + |
| 81 | + [Test] |
| 82 | + public void WriteToWrongColumnTest() |
| 83 | + { |
| 84 | + using (var session = Domain.OpenSession()) |
| 85 | + using (var tx = session.OpenTransaction()) { |
| 86 | + var aId = new A(session) { Name = "AName" }.Id; |
| 87 | + var b = new B1(session) { Name = "B1Name" }; |
| 88 | + _ = b.ConflictingField.Add(new C(session)); |
| 89 | + var b1Id = b.Id; |
| 90 | + var b2Id = new B2(session) { Name = "B2Name", ConflictingField = 5.0 }.Id; |
| 91 | + Assert.DoesNotThrow(session.SaveChanges); |
| 92 | + var accessor = session.Services.Get<DirectSqlAccessor>(); |
| 93 | + using (var command = accessor.CreateCommand()) { |
| 94 | + command.CommandText = GetCommandText(session); |
| 95 | + using (var reader = command.ExecuteReader()) { |
| 96 | + while (reader.Read()) { |
| 97 | + var id = reader.GetInt32(0); |
| 98 | + var conflictingField = reader.GetDouble(1); |
| 99 | + if (id != b2Id) { |
| 100 | + Assert.That(conflictingField, Is.EqualTo(0.0)); |
| 101 | + } |
| 102 | + else { |
| 103 | + Assert.That(conflictingField, Is.EqualTo(5.0)); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private string GetCommandText(Session session) |
| 112 | + { |
| 113 | + var typeInfo = Domain.Model.Types[typeof(A)]; |
| 114 | + var table = session.StorageNode.Mapping[typeInfo]; |
| 115 | + |
| 116 | + var tableRef = Sql.SqlDml.TableRef(table); |
| 117 | + var select = Sql.SqlDml.Select(tableRef); |
| 118 | + select.Columns.Add(tableRef[nameof(A.Id)]); |
| 119 | + select.Columns.Add(tableRef[nameof(B2.ConflictingField)]); |
| 120 | + |
| 121 | + var queryBuilder = session.Services.Get<QueryBuilder>(); |
| 122 | + |
| 123 | + var compileResult = queryBuilder.CompileQuery(select); |
| 124 | + return compileResult.GetCommandText(); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments