Skip to content

Commit 2395be0

Browse files
committed
Assertion-based exception checks in tests
1 parent d086405 commit 2395be0

5 files changed

Lines changed: 73 additions & 132 deletions

File tree

Orm/Xtensive.Orm.Tests/Interfaces/KeyStructureConflictTest.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,17 @@ public class Child2 : Root2, IChild
4545

4646
namespace Xtensive.Orm.Tests.Interfaces
4747
{
48-
public class KeyStructureConflictTest : AutoBuildTest
48+
public class KeyStructureConflictTest
4949
{
50-
protected override DomainConfiguration BuildConfiguration()
50+
[Test]
51+
public void MainTest()
5152
{
52-
var config = base.BuildConfiguration();
53-
config.Types.Register(typeof (Root1).Assembly, typeof (Root1).Namespace);
54-
return config;
55-
}
56-
57-
protected override Domain BuildDomain(DomainConfiguration configuration)
58-
{
59-
try {
60-
base.BuildDomain(configuration);
61-
Assert.Fail();
62-
}
63-
catch (DomainBuilderException e) {
64-
Console.WriteLine(e);
65-
}
66-
return null;
53+
var config = DomainConfigurationFactory.Create();
54+
config.Types.Register(typeof(Root1).Assembly, typeof(Root1).Namespace);
55+
var ex = Assert.Throws<DomainBuilderException>(() => Domain.Build(config));
56+
var message = ex.Message;
57+
Assert.That(message.Contains("IChild") && message.Contains("different key structure") && message.Contains("Root1 & Root2"),
58+
Is.True);
6759
}
6860
}
6961
}

Orm/Xtensive.Orm.Tests/Interfaces/TypeIdModeConflictTest.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,17 @@ public class Root2 : Entity, IRoot
3434

3535
namespace Xtensive.Orm.Tests.Interfaces
3636
{
37-
public class TypeIdModeConflictTest : AutoBuildTest
37+
public class TypeIdModeConflictTest
3838
{
39-
protected override DomainConfiguration BuildConfiguration()
39+
[Test]
40+
public void MainTest()
4041
{
41-
var config = base.BuildConfiguration();
42-
config.Types.Register(typeof (Root1).Assembly, typeof (Root1).Namespace);
43-
return config;
44-
}
45-
46-
protected override Domain BuildDomain(DomainConfiguration configuration)
47-
{
48-
try {
49-
base.BuildDomain(configuration);
50-
Assert.Fail();
51-
}
52-
catch (DomainBuilderException e) {
53-
Console.WriteLine(e);
54-
}
55-
return null;
42+
var config = DomainConfigurationFactory.Create();
43+
config.Types.Register(typeof(Root1).Assembly, typeof(Root1).Namespace);
44+
var ex = Assert.Throws<DomainBuilderException>(() => Domain.Build(config));
45+
var message = ex.Message;
46+
Assert.That(message.Contains("IRoot") && message.Contains("one of which includes TypeId, but another doesn't") && message.Contains("Root1 & Root2"),
47+
Is.True);
5648
}
5749
}
5850
}

Orm/Xtensive.Orm.Tests/Interfaces/UnusedTypeRemovalTest.cs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,18 @@ public class First : Entity, IFirst
5151

5252
namespace Xtensive.Orm.Tests.Interfaces
5353
{
54-
public class UnusedTypeRemovalTest : AutoBuildTest
54+
public class UnusedTypeRemovalTest
5555
{
5656
[Test]
5757
public void MainTest()
5858
{
59-
Assert.IsNull(Domain);
60-
}
59+
var config = DomainConfigurationFactory.Create();
60+
config.Types.Register(typeof(IFirst).Assembly, typeof(IFirst).Namespace);
6161

62-
protected override DomainConfiguration BuildConfiguration()
63-
{
64-
var config = base.BuildConfiguration();
65-
config.Types.Register(typeof (IFirst).Assembly, typeof (IFirst).Namespace);
66-
return config;
67-
}
68-
69-
protected override Domain BuildDomain(DomainConfiguration configuration)
70-
{
71-
try {
72-
base.BuildDomain(configuration);
73-
Assert.Fail();
74-
}
75-
catch (DomainBuilderException e) {
76-
Console.WriteLine(e);
77-
}
78-
return null;
62+
var ex = Assert.Throws<DomainBuilderException>(() => Domain.Build(config));
63+
var message = ex.Message;
64+
Assert.That(message.Contains("ISecond") && message.Contains("don't belong") && message.Contains("hierarchy"),
65+
Is.True);
7966
}
8067
}
8168
}

Orm/Xtensive.Orm.Tests/Issues/Issue0262_StructureAssignment.cs

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -64,53 +64,40 @@ protected override DomainConfiguration BuildConfiguration()
6464
[Test]
6565
public void SetTest()
6666
{
67-
using (var session = Domain.OpenSession()) {
68-
using (var t = session.OpenTransaction()) {
69-
70-
var container = new Container();
71-
try {
72-
container.Value = new Triple();
73-
Assert.Fail();
74-
} catch (InvalidOperationException) {
75-
76-
}
77-
78-
// Rollback
79-
}
67+
using (var session = Domain.OpenSession())
68+
using (var t = session.OpenTransaction()) {
69+
70+
var container = new Container();
71+
_ = Assert.Throws<InvalidOperationException>(() => { container.Value = new Triple(); });
72+
73+
// Rollback
8074
}
8175
}
8276

8377
[Test]
8478
public void CastTest()
8579
{
86-
using (var session = Domain.OpenSession()) {
87-
using (var t = session.OpenTransaction()) {
88-
89-
var container = new Container();
90-
try {
91-
container.Value = (Pair)new Triple();
92-
Assert.Fail();
93-
} catch (InvalidOperationException) {
94-
95-
}
96-
97-
// Rollback
98-
}
80+
using (var session = Domain.OpenSession())
81+
using (var t = session.OpenTransaction()) {
82+
83+
var container = new Container();
84+
_ = Assert.Throws<InvalidOperationException>(() => { container.Value = (Pair) new Triple(); });
85+
86+
// Rollback
9987
}
10088
}
10189

10290
[Test]
10391
public void ValidTest()
10492
{
105-
using (var session = Domain.OpenSession()) {
106-
using (var t = session.OpenTransaction()) {
107-
108-
var container = new Container();
109-
var triple = new Triple();
110-
container.Value = new Pair(triple.One, triple.Two);
111-
112-
// Rollback
113-
}
93+
using (var session = Domain.OpenSession())
94+
using (var t = session.OpenTransaction()) {
95+
96+
var container = new Container();
97+
var triple = new Triple();
98+
container.Value = new Pair(triple.One, triple.Two);
99+
100+
// Rollback
114101
}
115102
}
116103
}

Orm/Xtensive.Orm.Tests/Issues/IssueJira0197_ExtendedErrorInformation.cs

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2011 Xtensive LLC.
1+
// Copyright (C) 2011 Xtensive LLC.
22
// All rights reserved.
33
// For conditions of distribution and use, see license.
44
// Created by: Denis Krjuchkov
@@ -43,68 +43,51 @@ protected override void CheckRequirements()
4343
protected override DomainConfiguration BuildConfiguration()
4444
{
4545
var config = base.BuildConfiguration();
46-
config.Types.Register(typeof (ErrorProvider).Assembly, typeof (ErrorProvider).Namespace);
46+
config.Types.Register(typeof(ErrorProvider));
4747
return config;
4848
}
4949

5050
[Test]
5151
public void InsertNullTest()
5252
{
53-
using (var session = Domain.OpenSession()) {
54-
using (var t = session.OpenTransaction()) {
55-
try {
56-
new ErrorProvider(1);
57-
session.SaveChanges();
58-
}
59-
catch (CheckConstraintViolationException exception) {
60-
var expected = Domain.Model.Types[typeof (ErrorProvider)];
61-
Assert.AreEqual(expected, exception.Info.Type);
62-
Assert.AreEqual(expected.Fields["NotNull"], exception.Info.Field);
63-
}
64-
}
53+
using (var session = Domain.OpenSession())
54+
using (var t = session.OpenTransaction()) {
55+
_ = new ErrorProvider(1);
56+
var ex = Assert.Throws<CheckConstraintViolationException>(() => session.SaveChanges());
57+
var expected = Domain.Model.Types[typeof(ErrorProvider)];
58+
Assert.That(ex.Info.Type, Is.EqualTo(expected));
59+
Assert.That(ex.Info.Field, Is.EqualTo(expected.Fields[nameof(ErrorProvider.NotNull)]));
6560
}
6661
}
6762

6863
[Test]
6964
public void DuplicateUniqueIndexTest()
7065
{
71-
using (var session = Domain.OpenSession()) {
72-
using (var t = session.OpenTransaction()) {
73-
try {
74-
new ErrorProvider(2) {NotNull = string.Empty, Unique = 2};
75-
session.SaveChanges();
76-
new ErrorProvider(3) {NotNull = string.Empty, Unique = 2};
77-
session.SaveChanges();
78-
Assert.Fail();
79-
}
80-
catch (UniqueConstraintViolationException exception) {
81-
Assert.AreEqual(Domain.Model.Types[typeof (ErrorProvider)], exception.Info.Type);
82-
}
83-
}
66+
using (var session = Domain.OpenSession())
67+
using (var t = session.OpenTransaction()) {
68+
_ = new ErrorProvider(2) { NotNull = string.Empty, Unique = 2 };
69+
session.SaveChanges();
70+
_ = new ErrorProvider(3) { NotNull = string.Empty, Unique = 2 };
71+
var ex = Assert.Throws<UniqueConstraintViolationException>(() => session.SaveChanges());
72+
Assert.That(ex.Info.Type, Is.EqualTo(Domain.Model.Types[typeof(ErrorProvider)]));
8473
}
8574
}
8675

8776
[Test]
8877
public void DuplicatePrimaryKeyTest()
8978
{
90-
using (var session = Domain.OpenSession()) {
91-
using (var t = session.OpenTransaction()) {
92-
new ErrorProvider(3) {NotNull = string.Empty, Unique = 31};
93-
t.Complete();
94-
}
79+
using (var session = Domain.OpenSession())
80+
using (var t = session.OpenTransaction()) {
81+
_ = new ErrorProvider(3) { NotNull = string.Empty, Unique = 31 };
82+
t.Complete();
9583
}
9684

97-
using (var session = Domain.OpenSession()) {
98-
using (var t = session.OpenTransaction()) {
99-
try {
100-
new ErrorProvider(3) {NotNull = string.Empty, Unique = 32};
101-
session.SaveChanges();
102-
Assert.Fail();
103-
}
104-
catch (UniqueConstraintViolationException exception) {
105-
Assert.AreEqual(Domain.Model.Types[typeof (ErrorProvider)], exception.Info.Type);
106-
}
107-
}
85+
using (var session = Domain.OpenSession())
86+
using (var t = session.OpenTransaction()) {
87+
_ = new ErrorProvider(3) { NotNull = string.Empty, Unique = 32 };
88+
89+
var ex = Assert.Throws<UniqueConstraintViolationException>(() => session.SaveChanges());
90+
Assert.That(ex.Info.Type, Is.EqualTo(Domain.Model.Types[typeof(ErrorProvider)]));
10891
}
10992
}
11093
}

0 commit comments

Comments
 (0)