|
| 1 | +// Copyright (C) 2020 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 | +// Created by: Alexey Kulakov |
| 5 | +// Created: 2020.11.16 |
| 6 | + |
| 7 | +using NUnit.Framework; |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Linq; |
| 11 | +using Xtensive.Orm.Configuration; |
| 12 | +using Xtensive.Orm.Tests.Issues.IssueJira0801_ReduntandColumnRemoverRemovesPartOfTempTableColumnsModel; |
| 13 | + |
| 14 | +namespace Xtensive.Orm.Tests.Issues.IssueJira0801_ReduntandColumnRemoverRemovesPartOfTempTableColumnsModel |
| 15 | +{ |
| 16 | + [HierarchyRoot] |
| 17 | + public class EquipmentStateRecord : Entity |
| 18 | + { |
| 19 | + [Field, Key] |
| 20 | + public long Id { get; private set; } |
| 21 | + |
| 22 | + [Field] |
| 23 | + public DateTime StartDate { get; set; } |
| 24 | + |
| 25 | + [Field] |
| 26 | + public DateTime? EndDate { get; set; } |
| 27 | + |
| 28 | + [Field] |
| 29 | + public Equipment Equipment { get; set; } |
| 30 | + |
| 31 | + [Field] |
| 32 | + public MachineStateType Type { get; set; } |
| 33 | + } |
| 34 | + |
| 35 | + [HierarchyRoot] |
| 36 | + public class Equipment : Entity |
| 37 | + { |
| 38 | + [Field, Key] |
| 39 | + public long Id { get; private set; } |
| 40 | + } |
| 41 | + |
| 42 | + public enum MachineStateType |
| 43 | + { |
| 44 | + Undefined = 0, |
| 45 | + Production = 1, |
| 46 | + EmergencyStop = 2, |
| 47 | + Adjustment = 3, |
| 48 | + SwitchedOn = 4, |
| 49 | + SwitchedOff = 5 |
| 50 | + } |
| 51 | + |
| 52 | + public class Segment |
| 53 | + { |
| 54 | + public long EquipmentId { get; set; } |
| 55 | + |
| 56 | + public DateTime StartDate { get; set; } |
| 57 | + |
| 58 | + public DateTime EndDate { get; set; } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +namespace Xtensive.Orm.Tests.Issues |
| 63 | +{ |
| 64 | + public class IssueJira0801_ReduntandColumnRemoverRemovesPartOfTempTableColumns : AutoBuildTest |
| 65 | + { |
| 66 | + private long equipmentId; |
| 67 | + |
| 68 | + protected override void CheckRequirements() => |
| 69 | + Require.AllFeaturesSupported(Providers.ProviderFeatures.TemporaryTables); |
| 70 | + |
| 71 | + protected override DomainConfiguration BuildConfiguration() |
| 72 | + { |
| 73 | + var config = base.BuildConfiguration(); |
| 74 | + config.Types.Register(typeof(EquipmentStateRecord).Assembly, typeof(EquipmentStateRecord).Namespace); |
| 75 | + return config; |
| 76 | + } |
| 77 | + |
| 78 | + protected override void PopulateData() |
| 79 | + { |
| 80 | + using (var session = Domain.OpenSession()) |
| 81 | + using (var tx = session.OpenTransaction()) { |
| 82 | + var equipment = new Equipment(); |
| 83 | + equipmentId = equipment.Id; |
| 84 | + |
| 85 | + foreach (var item in Enum.GetValues(typeof(MachineStateType)).Cast<MachineStateType>()) { |
| 86 | + _ = new EquipmentStateRecord() { |
| 87 | + Equipment = equipment, |
| 88 | + Type = item, |
| 89 | + StartDate = DateTime.Now.AddDays(-1), |
| 90 | + EndDate = DateTime.Now.AddHours(-1) |
| 91 | + }; |
| 92 | + |
| 93 | + _ = new EquipmentStateRecord() { |
| 94 | + Equipment = equipment, |
| 95 | + Type = item, |
| 96 | + StartDate = DateTime.Now.AddDays(-2), |
| 97 | + EndDate = DateTime.Now.AddHours(-2) |
| 98 | + }; |
| 99 | + |
| 100 | + _ = new EquipmentStateRecord() { |
| 101 | + Equipment = equipment, |
| 102 | + Type = item, |
| 103 | + StartDate = DateTime.Now.AddDays(-3), |
| 104 | + EndDate = DateTime.Now.AddHours(-3) |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + tx.Complete(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + [Test] |
| 113 | + public void MainTest() |
| 114 | + { |
| 115 | + using (var session = Domain.OpenSession()) |
| 116 | + using (var tx = session.OpenTransaction()) { |
| 117 | + var segments = GetSegments().Select(x => new { x.EquipmentId, x.StartDate, x.EndDate }).ToArray(); |
| 118 | + var remoteSegments = session.Query.Store(segments); |
| 119 | + |
| 120 | + var testQuery = session.Query.All<EquipmentStateRecord>() |
| 121 | + .Where(x => x.EndDate.HasValue) |
| 122 | + .Join(remoteSegments, |
| 123 | + record => record.Equipment.Id, |
| 124 | + segment => segment.EquipmentId, |
| 125 | + (record, segment) => new { |
| 126 | + EquipmentId = record.Equipment.Id, |
| 127 | + Type = record.Type, |
| 128 | + StateStartDate = record.StartDate, |
| 129 | + StateEndDate = record.EndDate.Value, |
| 130 | + SegmentStartDate = segment.StartDate, |
| 131 | + SegmentEndDate = segment.EndDate, |
| 132 | + StartTime = record.StartDate < segment.StartDate ? segment.StartDate : record.StartDate, |
| 133 | + EndTime = record.EndDate.Value > segment.EndDate ? segment.EndDate : record.EndDate.Value |
| 134 | + }) |
| 135 | + .Where(x => x.StateStartDate < x.SegmentEndDate) |
| 136 | + .GroupBy(x => new { x.EquipmentId, x.Type }, x => x.EndTime - x.StartTime) |
| 137 | + .ToArray(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private IEnumerable<Segment> GetSegments() |
| 142 | + { |
| 143 | + yield return new Segment { EquipmentId = equipmentId, StartDate = DateTime.Now.AddDays(-1), EndDate = DateTime.Now }; |
| 144 | + yield return new Segment { EquipmentId = equipmentId, StartDate = DateTime.Now.AddDays(-2), EndDate = DateTime.Now }; |
| 145 | + yield return new Segment { EquipmentId = equipmentId, StartDate = DateTime.Now.AddDays(-3), EndDate = DateTime.Now }; |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments