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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
using System;
using System.Collections.Generic;
using developwithpassion.bdd.contexts;
using Gorilla.Commons.Testing;
using Gorilla.Commons.Utility.Core;
namespace MoMoney.DataAccess.Transactions
{
public class SessionSpecs
{
public class behaves_like_session : concerns_for<ISession, Session>
{
context c = () =>
{
transaction = the_dependency<ITransaction>();
database = the_dependency<IDatabase>();
};
static protected ITransaction transaction;
static protected IDatabase database;
}
[Concern(typeof (Session))]
public class when_saving_a_transient_item_to_a_session : behaves_like_session
{
it should_add_the_entity_to_the_identity_map = () => map.was_told_to(x => x.add(guid, entity));
context c = () =>
{
guid = Guid.NewGuid();
entity = an<ITestEntity>();
map = an<IIdentityMap<Guid, ITestEntity>>();
when_the(entity).is_told_to(x => x.id).it_will_return(guid);
when_the(transaction).is_told_to(x => x.create_for<ITestEntity>()).it_will_return(map);
};
because b = () => sut.save(entity);
static ITestEntity entity;
static IIdentityMap<Guid, ITestEntity> map;
static Id<Guid> guid;
}
[Concern(typeof (Session))]
public class when_commiting_the_changes_made_in_a_session : behaves_like_session
{
it should_commit_all_the_changes_from_the_running_transaction =
() => transaction.was_told_to(x => x.commit_changes());
it should_not_rollback_any_changes_from_the_running_transaction =
() => transaction.was_not_told_to(x => x.rollback_changes());
because b = () =>
{
sut.flush();
sut.Dispose();
};
}
[Concern(typeof (Session))]
public class when_closing_a_session_before_flushing_the_changes : behaves_like_session
{
it should_rollback_any_changes_made_in_the_current_transaction =
() => transaction.was_told_to(x => x.rollback_changes());
because b = () => sut.Dispose();
}
[Concern(typeof (Session))]
public class when_loading_all_instances_of_a_certain_type_and_some_have_already_been_loaded : behaves_like_session
{
it should_return_the_items_from_the_cache = () => results.should_contain(cached_item);
it should_exclude_duplicates_from_the_database = () => results.should_not_contain(database_item);
it should_add_items_from_the_database_to_the_identity_map =
() => identity_map.was_told_to(x => x.add(id_of_the_uncached_item, uncached_item));
context c = () =>
{
id = Guid.NewGuid();
id_of_the_uncached_item = Guid.NewGuid();
identity_map = an<IIdentityMap<Guid, ITestEntity>>();
cached_item = an<ITestEntity>();
database_item = an<ITestEntity>();
uncached_item = an<ITestEntity>();
when_the(cached_item).is_told_to(x => x.id).it_will_return(id);
when_the(database_item).is_told_to(x => x.id).it_will_return(id);
when_the(uncached_item).is_told_to(x => x.id).it_will_return(id_of_the_uncached_item);
when_the(transaction).is_told_to(x => x.create_for<ITestEntity>()).it_will_return(identity_map);
when_the(identity_map).is_told_to(x => x.contains_an_item_for(id)).it_will_return(true);
when_the(identity_map).is_told_to(x => x.all()).it_will_return(cached_item);
when_the(database).is_told_to(x => x.fetch_all<ITestEntity>())
.it_will_return(database_item, uncached_item);
};
because b = () =>
{
sut.find<ITestEntity>(id);
results = sut.all<ITestEntity>();
};
static IEnumerable<ITestEntity> results;
static Id<Guid> id;
static Id<Guid> id_of_the_uncached_item;
static ITestEntity cached_item;
static ITestEntity database_item;
static IIdentityMap<Guid, ITestEntity> identity_map;
static ITestEntity uncached_item;
}
[Concern(typeof (Session))]
public class when_looking_up_a_specific_entity_by_its_id_and_it_has_not_been_loaded_into_the_cache :
behaves_like_session
{
it should_return_that_item = () =>
{
result.should_be_equal_to(correct_item);
};
it should_add_that_item_to_the_identity_map = () => map.was_told_to(x => x.add(id, correct_item));
context c = () =>
{
id = Guid.NewGuid();
wrong_item = an<ITestEntity>();
correct_item = an<ITestEntity>();
map = an<IIdentityMap<Guid, ITestEntity>>();
when_the(wrong_item).is_told_to(x => x.id).it_will_return<Id<Guid>>(Guid.NewGuid());
when_the(correct_item).is_told_to(x => x.id).it_will_return(id);
when_the(database)
.is_told_to(x => x.fetch_all<ITestEntity>())
.it_will_return(wrong_item, correct_item);
when_the(transaction).is_told_to(x => x.create_for<ITestEntity>())
.it_will_return(map);
};
because b = () =>
{
result = sut.find<ITestEntity>(id);
};
static Id<Guid> id;
static IIdentifiable<Guid> result;
static ITestEntity correct_item;
static ITestEntity wrong_item;
static IIdentityMap<Guid, ITestEntity> map;
}
[Concern(typeof (Session))]
public class when_deleting_an_item_from_the_database : behaves_like_session
{
it should_remove_that_item_from_the_cache = () => map.was_told_to(x => x.evict(id));
context c = () =>
{
id = Guid.NewGuid();
entity = an<ITestEntity>();
map = an<IIdentityMap<Guid, ITestEntity>>();
when_the(entity).is_told_to(x => x.id).it_will_return(id);
when_the(transaction).is_told_to(x => x.create_for<ITestEntity>()).it_will_return(map);
when_the(database).is_told_to(x => x.fetch_all<ITestEntity>()).it_will_return(entity);
};
because b = () =>
{
sut.find<ITestEntity>(id);
sut.delete(entity);
};
static Id<Guid> id;
static IIdentityMap<Guid, ITestEntity> map;
static ITestEntity entity;
}
public interface ITestEntity : IIdentifiable<Guid> {}
}
}
|