forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRefreshFixture.cs
117 lines (101 loc) · 2.72 KB
/
RefreshFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Data;
using NUnit.Framework;
namespace NHibernate.Test.Cascade
{
[TestFixture]
public class RefreshFixture : TestCase
{
protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}
protected override string[] Mappings
{
get { return new[] { "Cascade.Job.hbm.xml", "Cascade.JobBatch.hbm.xml" }; }
}
[Test]
public void RefreshCascade()
{
using(ISession session = OpenSession())
{
using (ITransaction txn = session.BeginTransaction())
{
JobBatch batch = new JobBatch(DateTime.Now);
batch.CreateJob().ProcessingInstructions = "Just do it!";
batch.CreateJob().ProcessingInstructions = "I know you can do it!";
// write the stuff to the database; at this stage all job.status values are zero
session.Persist(batch);
session.Flush();
// behind the session's back, let's modify the statuses
UpdateStatuses(session);
// Now lets refresh the persistent batch, and see if the refresh cascaded to the jobs collection elements
session.Refresh(batch);
foreach (Job job in batch.Jobs)
{
Assert.That(job.Status, Is.EqualTo(1), "Jobs not refreshed!");
}
txn.Rollback();
}
}
}
private void UpdateStatuses(ISession session)
{
var conn = session.Connection;
var cmd = conn.CreateCommand();
cmd.CommandText = "UPDATE T_JOB SET JOB_STATUS = 1";
cmd.CommandType = CommandType.Text;
session.GetSessionImplementation().ConnectionManager.EnlistInTransaction(cmd);
cmd.ExecuteNonQuery();
}
[Test]
public void RefreshIgnoringTransient()
{
// No exception expected
using (ISession session = OpenSession())
{
using (ITransaction txn = session.BeginTransaction())
{
var batch = new JobBatch(DateTime.Now);
session.Refresh(batch);
txn.Rollback();
}
}
}
[Test]
public void RefreshIgnoringTransientInCollection()
{
using (ISession session = OpenSession())
{
using (ITransaction txn = session.BeginTransaction())
{
var batch = new JobBatch(DateTime.Now);
batch.CreateJob().ProcessingInstructions = "Just do it!";
session.Persist(batch);
session.Flush();
batch.CreateJob().ProcessingInstructions = "I know you can do it!";
session.Refresh(batch);
Assert.That(batch.Jobs.Count == 1);
txn.Rollback();
}
}
}
[Test]
public void RefreshNotIgnoringTransientByUnsavedValue()
{
ISession session = OpenSession();
ITransaction txn = session.BeginTransaction();
var batch = new JobBatch { BatchDate = DateTime.Now, Id = 1 };
try
{
session.Refresh(batch);
}
catch (UnresolvableObjectException)
{
// as expected
txn.Rollback();
session.Close();
}
}
}
}