|
| 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.02.18 |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Text; |
| 10 | +using Xtensive.Orm.Configuration; |
| 11 | +using Xtensive.Orm.Validation; |
| 12 | +using Xtensive.Orm.Tests.Issues.IssueJira0793_FieldValidationTriggersLazyLoadFieldsFetchModel; |
| 13 | +using NUnit.Framework; |
| 14 | +using Xtensive.Core; |
| 15 | + |
| 16 | +namespace Xtensive.Orm.Tests.Issues.IssueJira0793_FieldValidationTriggersLazyLoadFieldsFetchModel |
| 17 | +{ |
| 18 | + [HierarchyRoot] |
| 19 | + public class Book : Entity |
| 20 | + { |
| 21 | + [Field, Key] |
| 22 | + public int Id { get; set; } |
| 23 | + |
| 24 | + [Field(Length = 50), NotNullOrEmptyConstraint] |
| 25 | + public string Title { get; set; } |
| 26 | + |
| 27 | + [Field(Length = int.MaxValue, LazyLoad = true)] |
| 28 | + public string Description { get; set; } |
| 29 | + |
| 30 | + [Field(LazyLoad = true)] |
| 31 | + public byte[] BookFile { get; set; } |
| 32 | + |
| 33 | + [Field(Length = 5)] |
| 34 | + public string FileExtension { get; set; } |
| 35 | + |
| 36 | + [Field] |
| 37 | + public Author Author { get; set; } |
| 38 | + } |
| 39 | + |
| 40 | + [HierarchyRoot] |
| 41 | + public class Author : Entity |
| 42 | + { |
| 43 | + [Field, Key] |
| 44 | + public int Id { get; set; } |
| 45 | + |
| 46 | + [Field()] |
| 47 | + public string FirstName { get; set; } |
| 48 | + |
| 49 | + [Field] |
| 50 | + public string LastName { get; set; } |
| 51 | + |
| 52 | + [Field(Length = int.MaxValue, LazyLoad = true)] |
| 53 | + [NotNullConstraint]// should always be triggered |
| 54 | + public string Biography { get; set; } |
| 55 | + } |
| 56 | + |
| 57 | + [HierarchyRoot] |
| 58 | + public class Chapter : Entity |
| 59 | + { |
| 60 | + [Field, Key] |
| 61 | + public int Id { get; set; } |
| 62 | + |
| 63 | + [Field] |
| 64 | + public Book Owner { get; set; } |
| 65 | + |
| 66 | + [Field] |
| 67 | + public string Title { get; set; } |
| 68 | + |
| 69 | + [Field(Length = int.MaxValue, LazyLoad = true)] |
| 70 | + [NotNullOrEmptyConstraint(ValidateOnlyIfModified = true)]// should validate only if changed |
| 71 | + public string Description { get; set; } |
| 72 | + } |
| 73 | + |
| 74 | + public class QueryCounter |
| 75 | + { |
| 76 | + public int Count { get; private set; } |
| 77 | + |
| 78 | + public Disposable Attach(Session session) |
| 79 | + { |
| 80 | + session.Events.DbCommandExecuted += Encount; |
| 81 | + return new Disposable((disposing) => session.Events.DbCommandExecuted -= Encount); |
| 82 | + } |
| 83 | + |
| 84 | + public void Reset() => Count = 0; |
| 85 | + |
| 86 | + private void Encount(object sender, DbCommandEventArgs eventArgs) => Count++; |
| 87 | + |
| 88 | + public QueryCounter() |
| 89 | + { |
| 90 | + Count = 0; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +namespace Xtensive.Orm.Tests.Issues |
| 96 | +{ |
| 97 | + public class IssueJira0793_FieldValidationTriggersLazyLoadFieldsFetch : AutoBuildTest |
| 98 | + { |
| 99 | + private Key oblomovKey; |
| 100 | + private Key goncharovKey; |
| 101 | + |
| 102 | + protected override DomainConfiguration BuildConfiguration() |
| 103 | + { |
| 104 | + var configuration = base.BuildConfiguration(); |
| 105 | + configuration.Types.Register(typeof(Book).Assembly, typeof(Book).Namespace); |
| 106 | + configuration.UpgradeMode = DomainUpgradeMode.Recreate; |
| 107 | + return configuration; |
| 108 | + } |
| 109 | + |
| 110 | + protected override void PopulateData() |
| 111 | + { |
| 112 | + using (var session = Domain.OpenSession()) |
| 113 | + using (var transaction = session.OpenTransaction()) { |
| 114 | + var author = new Author() { |
| 115 | + FirstName = "Ivan", |
| 116 | + LastName = "Goncharov", |
| 117 | + Biography = "Some biography of Ivan Alexandrovich" }; |
| 118 | + var book = new Book() { |
| 119 | + Title = "Oblomov", |
| 120 | + Description = "A drama about how human's lazyness and absence of strenght may affect his life.", |
| 121 | + BookFile = new byte[] { 3, 3, 3, 3, 3, 3, 3 }, |
| 122 | + Author = author |
| 123 | + }; |
| 124 | + |
| 125 | + new Chapter() { Title = "Chapter #1", Description = "Detailed description of Chapter #1", Owner = book }; |
| 126 | + new Chapter() { Title = "Chapter #2", Description = "Detailed description of Chapter #2", Owner = book }; |
| 127 | + new Chapter() { Title = "Chapter #3", Description = "Detailed description of Chapter #3", Owner = book }; |
| 128 | + |
| 129 | + oblomovKey = book.Key; |
| 130 | + goncharovKey = author.Key; |
| 131 | + |
| 132 | + transaction.Complete(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + [Test] |
| 137 | + public void LazyFieldHasNoConstraintTest() |
| 138 | + { |
| 139 | + var counter = new QueryCounter(); |
| 140 | + using (var session = Domain.OpenSession()) { |
| 141 | + using (counter.Attach(session)) |
| 142 | + using (var transaction = session.OpenTransaction()) { |
| 143 | + var oblomov = session.Query.Single<Book>(oblomovKey); |
| 144 | + oblomov.Title = "Oblomov by Goncharov"; |
| 145 | + transaction.Complete(); |
| 146 | + } |
| 147 | + Assert.That(counter.Count, Is.EqualTo(2)); |
| 148 | + counter.Reset(); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + [Test] |
| 153 | + public void LazyFieldHasCheckAlwaysConstraintTest() |
| 154 | + { |
| 155 | + var counter = new QueryCounter(); |
| 156 | + using (var session = Domain.OpenSession()) { |
| 157 | + using (counter.Attach(session)) |
| 158 | + using (var transaction = session.OpenTransaction()) { |
| 159 | + var goncharov = session.Query.Single<Author>(goncharovKey); |
| 160 | + goncharov.FirstName = goncharov.FirstName + "modified"; // should prefetch lazy load field |
| 161 | + transaction.Complete(); |
| 162 | + } |
| 163 | + Assert.That(counter.Count, Is.EqualTo(3)); |
| 164 | + counter.Reset(); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + [Test] |
| 169 | + public void LazyFieldHasCheckIfModifiedConstraintTest() |
| 170 | + { |
| 171 | + var counter = new QueryCounter(); |
| 172 | + using (var session = Domain.OpenSession()) { |
| 173 | + using (counter.Attach(session)) |
| 174 | + using (var transaction = session.OpenTransaction()) { |
| 175 | + foreach (var chapter in session.Query.All<Chapter>()) { |
| 176 | + chapter.Title = chapter.Title + " modified"; |
| 177 | + } |
| 178 | + transaction.Complete(); |
| 179 | + } |
| 180 | + Assert.That(counter.Count, Is.EqualTo(2)); |
| 181 | + counter.Reset(); |
| 182 | + } |
| 183 | + |
| 184 | + using (var session = Domain.OpenSession()) { |
| 185 | + int updatedItems = 0; |
| 186 | + using (counter.Attach(session)) |
| 187 | + using (var transaction = session.OpenTransaction()) { |
| 188 | + foreach(var chapter in session.Query.All<Chapter>()) { |
| 189 | + chapter.Description = chapter.Description + " modified"; |
| 190 | + updatedItems++; |
| 191 | + } |
| 192 | + transaction.Complete(); |
| 193 | + } |
| 194 | + Assert.That(counter.Count, Is.EqualTo(5)); // query + fetches + update |
| 195 | + counter.Reset(); |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments