|
| 1 | +// Copyright (C) 2018 Xtensive LLC. |
| 2 | +// All rights reserved. |
| 3 | +// For conditions of distribution and use, see license. |
| 4 | +// Created by: Denis Kudelin |
| 5 | +// Created: 2018.12.28 |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Linq; |
| 10 | +using System.Text; |
| 11 | +using NUnit.Framework; |
| 12 | +using Xtensive.Orm.Configuration; |
| 13 | +using Xtensive.Orm.Tests.Issues.IssueJira0739_StructureFieldsRemapOnCastBugModel; |
| 14 | + |
| 15 | +namespace Xtensive.Orm.Tests.Issues |
| 16 | +{ |
| 17 | + [TestFixture] |
| 18 | + public class IssueJira0739_StructureFieldsRemapOnCastBug : AutoBuildTest |
| 19 | + { |
| 20 | + protected override DomainConfiguration BuildConfiguration() |
| 21 | + { |
| 22 | + var configuration = base.BuildConfiguration(); |
| 23 | + configuration.Types.Register(typeof(Product).Assembly, typeof(Product).Namespace); |
| 24 | + configuration.UpgradeMode = DomainUpgradeMode.Recreate; |
| 25 | + return configuration; |
| 26 | + } |
| 27 | + |
| 28 | + protected override void PopulateData() |
| 29 | + { |
| 30 | + using (var session = Domain.OpenSession()) |
| 31 | + using (var transactionScope = session.OpenTransaction()) { |
| 32 | + var rpu = new RecipeProductUsage(); |
| 33 | + rpu.Sequence = 1; |
| 34 | + rpu.Quantity.MeasureType = "A"; |
| 35 | + rpu.Quantity.SomeStructureField.SomeStructureField1 = "ABC"; |
| 36 | + rpu.Quantity.SomeStructureField.SomeStructureField2 = "DEF"; |
| 37 | + rpu.Quantity.SomeStructureField.S2.SomeAnotherStructureField1 = "GHI"; |
| 38 | + rpu.Quantity.SomeStructureField.S2.SomeAnotherStructureField2 = "JKL"; |
| 39 | + |
| 40 | + rpu = new RecipeProductUsage(); |
| 41 | + rpu.Sequence = 2; |
| 42 | + rpu.Quantity.MeasureType = "B"; |
| 43 | + rpu.Quantity.SomeStructureField.SomeStructureField1 = "ABCC"; |
| 44 | + rpu.Quantity.SomeStructureField.SomeStructureField2 = "DEFF"; |
| 45 | + rpu.Quantity.SomeStructureField.S2.SomeAnotherStructureField1 = "GHII"; |
| 46 | + rpu.Quantity.SomeStructureField.S2.SomeAnotherStructureField2 = "JKLL"; |
| 47 | + |
| 48 | + transactionScope.Complete(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + [Test] |
| 53 | + public void CastToBaseClassTest() |
| 54 | + { |
| 55 | + using (var session = Domain.OpenSession()) |
| 56 | + using (var transaction = session.OpenTransaction()) { |
| 57 | + var result = session.Query.All<RecipeProductUsage>() |
| 58 | + .Cast<ProductUsage>() |
| 59 | + .Where(pu => pu.Quantity.SomeStructureField.SomeStructureField1.StartsWith("ABC")) |
| 60 | + .ToArray(); |
| 61 | + |
| 62 | + Assert.That(result.Length, Is.EqualTo(2)); |
| 63 | + |
| 64 | + var a = result.First(u => u.Sequence == 1); |
| 65 | + Assert.That(a.Quantity.MeasureType, Is.EqualTo("A")); |
| 66 | + Assert.That(a.Quantity.SomeStructureField.SomeStructureField1, Is.EqualTo("ABC")); |
| 67 | + Assert.That(a.Quantity.SomeStructureField.SomeStructureField2, Is.EqualTo("DEF")); |
| 68 | + Assert.That(a.Quantity.SomeStructureField.S2.SomeAnotherStructureField1, Is.EqualTo("GHI")); |
| 69 | + Assert.That(a.Quantity.SomeStructureField.S2.SomeAnotherStructureField2, Is.EqualTo("JKL")); |
| 70 | + |
| 71 | + var b = result.First(u => u.Sequence == 2); |
| 72 | + Assert.That(b.Quantity.MeasureType, Is.EqualTo("B")); |
| 73 | + Assert.That(b.Quantity.SomeStructureField.SomeStructureField1, Is.EqualTo("ABCC")); |
| 74 | + Assert.That(b.Quantity.SomeStructureField.SomeStructureField2, Is.EqualTo("DEFF")); |
| 75 | + Assert.That(b.Quantity.SomeStructureField.S2.SomeAnotherStructureField1, Is.EqualTo("GHII")); |
| 76 | + Assert.That(b.Quantity.SomeStructureField.S2.SomeAnotherStructureField2, Is.EqualTo("JKLL")); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + [Test] |
| 81 | + public void CastToInterfaceTest() |
| 82 | + { |
| 83 | + using (var session = Domain.OpenSession()) |
| 84 | + using (var transaction = session.OpenTransaction()) { |
| 85 | + var result = session.Query.All<RecipeProductUsage>() |
| 86 | + .Cast<IProductUsage>() |
| 87 | + .Where(pu => pu.Quantity.SomeStructureField.SomeStructureField1.StartsWith("ABC")) |
| 88 | + .ToArray(); |
| 89 | + |
| 90 | + Assert.That(result.Length, Is.EqualTo(2)); |
| 91 | + |
| 92 | + var a = result.First(u => u.Sequence == 1); |
| 93 | + Assert.That(a.Quantity.MeasureType, Is.EqualTo("A")); |
| 94 | + Assert.That(a.Quantity.SomeStructureField.SomeStructureField1, Is.EqualTo("ABC")); |
| 95 | + Assert.That(a.Quantity.SomeStructureField.SomeStructureField2, Is.EqualTo("DEF")); |
| 96 | + Assert.That(a.Quantity.SomeStructureField.S2.SomeAnotherStructureField1, Is.EqualTo("GHI")); |
| 97 | + Assert.That(a.Quantity.SomeStructureField.S2.SomeAnotherStructureField2, Is.EqualTo("JKL")); |
| 98 | + |
| 99 | + var b = result.First(u => u.Sequence == 2); |
| 100 | + Assert.That(b.Quantity.MeasureType, Is.EqualTo("B")); |
| 101 | + Assert.That(b.Quantity.SomeStructureField.SomeStructureField1, Is.EqualTo("ABCC")); |
| 102 | + Assert.That(b.Quantity.SomeStructureField.SomeStructureField2, Is.EqualTo("DEFF")); |
| 103 | + Assert.That(b.Quantity.SomeStructureField.S2.SomeAnotherStructureField1, Is.EqualTo("GHII")); |
| 104 | + Assert.That(b.Quantity.SomeStructureField.S2.SomeAnotherStructureField2, Is.EqualTo("JKLL")); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +namespace Xtensive.Orm.Tests.Issues.IssueJira0739_StructureFieldsRemapOnCastBugModel |
| 111 | +{ |
| 112 | + public interface IProductUsage : IEntity |
| 113 | + { |
| 114 | + [Field] |
| 115 | + float LossFactor { get; set; } |
| 116 | + |
| 117 | + [Field] |
| 118 | + int Sequence { get; set; } |
| 119 | + |
| 120 | + [Field] |
| 121 | + DimensionalField Quantity { get; set; } |
| 122 | + |
| 123 | + [Field] |
| 124 | + Product Product { get; } |
| 125 | + } |
| 126 | + |
| 127 | + public abstract class MesObject : Entity |
| 128 | + { |
| 129 | + [Key, Field] |
| 130 | + public long ID { get; set; } |
| 131 | + } |
| 132 | + |
| 133 | + [HierarchyRoot] |
| 134 | + public class Product : MesObject |
| 135 | + { |
| 136 | + } |
| 137 | + |
| 138 | + [HierarchyRoot] |
| 139 | + public abstract class ProductUsage : MesObject, IProductUsage |
| 140 | + { |
| 141 | + [Field] |
| 142 | + public Product Product { get; private set; } |
| 143 | + |
| 144 | + [Field] |
| 145 | + public DimensionalField Quantity { get; set; } |
| 146 | + |
| 147 | + [Field] |
| 148 | + public float LossFactor { get; set; } |
| 149 | + |
| 150 | + [Field] |
| 151 | + public int Sequence { get; set; } |
| 152 | + } |
| 153 | + |
| 154 | + public class RecipeProductUsage : ProductUsage |
| 155 | + { |
| 156 | + [Field] |
| 157 | + public string EngineeringReferenceCode { get; set; } |
| 158 | + } |
| 159 | + |
| 160 | + public class DimensionalField : Structure |
| 161 | + { |
| 162 | + [Field(DefaultValue = "", Length = 20, Nullable = false)] |
| 163 | + public string MeasureType { get; set; } |
| 164 | + |
| 165 | + [Field] |
| 166 | + public decimal NormalizedValue { get; set; } |
| 167 | + |
| 168 | + [Field(DefaultValue = 1.0)] |
| 169 | + public double LastUsedScale { get; set; } |
| 170 | + |
| 171 | + [Field] |
| 172 | + public SomeStructure SomeStructureField { get; set; } |
| 173 | + } |
| 174 | + |
| 175 | + public class SomeStructure : Structure |
| 176 | + { |
| 177 | + [Field] |
| 178 | + public SomeAnotherStructure S2 { get; set; } |
| 179 | + |
| 180 | + [Field] |
| 181 | + public string SomeStructureField1 { get; set; } |
| 182 | + |
| 183 | + [Field] |
| 184 | + public string SomeStructureField2 { get; set; } |
| 185 | + } |
| 186 | + |
| 187 | + public class SomeAnotherStructure : Structure |
| 188 | + { |
| 189 | + [Field] |
| 190 | + public string SomeAnotherStructureField1 { get; set; } |
| 191 | + |
| 192 | + [Field] |
| 193 | + public string SomeAnotherStructureField2 { get; set; } |
| 194 | + } |
| 195 | +} |
0 commit comments