PageRenderTime 67ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FluentNHibernate.Testing/Testing/PersistenceSpecificationExtensionsSpecs.cs

https://github.com/dotnetchris/fluent-nhibernate
C# | 566 lines | 472 code | 94 blank | 0 comment | 0 complexity | 243bf84b3298bc570bdaade934d784d0 MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using FluentNHibernate.Testing.Testing.Values;
  6. using FluentNHibernate.Testing.Values;
  7. using NHibernate;
  8. using NUnit.Framework;
  9. using Rhino.Mocks;
  10. namespace FluentNHibernate.Testing.Testing
  11. {
  12. public class InspectablePersistenceSpecification<T> : PersistenceSpecification<T>
  13. {
  14. public InspectablePersistenceSpecification(ISessionSource source) : base(source)
  15. {}
  16. public InspectablePersistenceSpecification(ISessionSource source, IEqualityComparer entityEqualityComparer) : base(source, entityEqualityComparer)
  17. {}
  18. public InspectablePersistenceSpecification(ISession session) : base(session)
  19. {}
  20. public InspectablePersistenceSpecification(ISession session, IEqualityComparer entityEqualityComparer) : base(session, entityEqualityComparer)
  21. {}
  22. public List<Property<T>> AllProperties
  23. {
  24. get { return allProperties; }
  25. }
  26. }
  27. public abstract class With_persistence_specification<T> : Specification
  28. {
  29. private ISession session;
  30. protected InspectablePersistenceSpecification<T> sut;
  31. protected IEqualityComparer comparer;
  32. public override void establish_context()
  33. {
  34. session = MockRepository.GenerateStub<ISession>();
  35. session.Stub(x => x.BeginTransaction()).Return(MockRepository.GenerateStub<ITransaction>());
  36. comparer = MockRepository.GenerateStub<IEqualityComparer>();
  37. sut = new InspectablePersistenceSpecification<T>(session, comparer);
  38. }
  39. }
  40. public class When_a_checked_property_is_added : With_persistence_specification<PropertyEntity>
  41. {
  42. public override void because()
  43. {
  44. sut.CheckProperty(x => x.GetterAndSetter, "expected");
  45. }
  46. [Test]
  47. public void should_add_a_property_check()
  48. {
  49. sut.AllProperties.First().ShouldBeOfType(typeof(Property<PropertyEntity, object>));
  50. }
  51. [Test]
  52. public void should_add_one_check_to_the_specification()
  53. {
  54. sut.AllProperties.ShouldHaveCount(1);
  55. }
  56. [Test]
  57. public void should_set_the_custom_equality_comparer()
  58. {
  59. sut.AllProperties.First().EntityEqualityComparer.ShouldEqual(comparer);
  60. }
  61. }
  62. public class When_a_checked_property_of_an_array_type_is_added : With_persistence_specification<ListEntity>
  63. {
  64. public override void because()
  65. {
  66. sut.CheckProperty(x => x.Array, new[] {"foo", "bar", "baz"});
  67. }
  68. [Test]
  69. public void should_add_a_list_check()
  70. {
  71. sut.AllProperties.First().ShouldBeOfType(typeof(List<ListEntity, string>));
  72. }
  73. [Test]
  74. public void should_add_one_check_to_the_specification()
  75. {
  76. sut.AllProperties.ShouldHaveCount(1);
  77. }
  78. [Test]
  79. public void should_set_the_custom_equality_comparer()
  80. {
  81. sut.AllProperties.First().EntityEqualityComparer.ShouldEqual(comparer);
  82. }
  83. }
  84. public class When_a_checked_property_with_a_custom_setter_is_added : With_persistence_specification<PropertyEntity>
  85. {
  86. protected Action<PropertyEntity, string> propertySetter;
  87. public override void establish_context()
  88. {
  89. base.establish_context();
  90. propertySetter = MockRepository.GenerateStub<Action<PropertyEntity, string>>();
  91. }
  92. public override void because()
  93. {
  94. sut.CheckProperty(x => x.GetterAndSetter, "expected", propertySetter);
  95. }
  96. [Test]
  97. public void should_add_a_property_check()
  98. {
  99. sut.AllProperties.First().ShouldBeOfType(typeof(Property<PropertyEntity, string>));
  100. }
  101. [Test]
  102. public void should_add_one_check_to_the_specification()
  103. {
  104. sut.AllProperties.ShouldHaveCount(1);
  105. }
  106. [Test]
  107. public void should_set_the_custom_equality_comparer()
  108. {
  109. sut.AllProperties.First().EntityEqualityComparer.ShouldEqual(comparer);
  110. }
  111. }
  112. public class When_the_value_setter_of_a_checked_property_is_invoked : When_a_checked_property_with_a_custom_setter_is_added
  113. {
  114. private PropertyEntity entity;
  115. public override void establish_context()
  116. {
  117. base.establish_context();
  118. entity = new PropertyEntity();
  119. }
  120. public override void because()
  121. {
  122. base.because();
  123. ((Property<PropertyEntity, string>)sut.AllProperties.First()).ValueSetter.Invoke(entity, null, "expected");
  124. }
  125. [Test]
  126. public void should_invoke_the_custom_setter()
  127. {
  128. propertySetter.AssertWasCalled(x => x.Invoke(entity, "expected"));
  129. }
  130. }
  131. public class When_a_checked_reference_is_added : With_persistence_specification<ReferenceEntity>
  132. {
  133. public override void because()
  134. {
  135. sut.CheckReference(x => x.Reference, new OtherEntity());
  136. }
  137. [Test]
  138. public void should_add_a_reference_property_check()
  139. {
  140. sut.AllProperties.First().ShouldBeOfType(typeof(ReferenceProperty<ReferenceEntity, object>));
  141. }
  142. [Test]
  143. public void should_add_one_check_to_the_specification()
  144. {
  145. sut.AllProperties.ShouldHaveCount(1);
  146. }
  147. [Test]
  148. public void should_set_the_custom_equality_comparer()
  149. {
  150. sut.AllProperties.First().EntityEqualityComparer.ShouldEqual(comparer);
  151. }
  152. }
  153. public class When_a_checked_reference_with_a_custom_setter_is_added : With_persistence_specification<ReferenceEntity>
  154. {
  155. protected Action<ReferenceEntity, OtherEntity> propertySetter;
  156. public override void establish_context()
  157. {
  158. base.establish_context();
  159. propertySetter = MockRepository.GenerateStub<Action<ReferenceEntity, OtherEntity>>();
  160. }
  161. public override void because()
  162. {
  163. sut.CheckReference(x => x.Reference, new OtherEntity(), propertySetter);
  164. }
  165. [Test]
  166. public void should_add_a_reference_property_check()
  167. {
  168. sut.AllProperties.First().ShouldBeOfType(typeof(ReferenceProperty<ReferenceEntity, OtherEntity>));
  169. }
  170. [Test]
  171. public void should_add_one_check_to_the_specification()
  172. {
  173. sut.AllProperties.ShouldHaveCount(1);
  174. }
  175. [Test]
  176. public void should_set_the_custom_equality_comparer()
  177. {
  178. sut.AllProperties.First().EntityEqualityComparer.ShouldEqual(comparer);
  179. }
  180. }
  181. public class When_the_value_setter_of_a_checked_reference_is_invoked : When_a_checked_reference_with_a_custom_setter_is_added
  182. {
  183. private ReferenceEntity entity;
  184. private OtherEntity referenced;
  185. public override void establish_context()
  186. {
  187. base.establish_context();
  188. entity = new ReferenceEntity();
  189. referenced = new OtherEntity();
  190. }
  191. public override void because()
  192. {
  193. base.because();
  194. ((Property<ReferenceEntity, OtherEntity>)sut.AllProperties.First()).ValueSetter.Invoke(entity, null, referenced);
  195. }
  196. [Test]
  197. public void should_invoke_the_custom_setter()
  198. {
  199. propertySetter.AssertWasCalled(x => x.Invoke(entity, referenced));
  200. }
  201. }
  202. public class When_a_checked_list_is_added : With_persistence_specification<ReferenceEntity>
  203. {
  204. public override void because()
  205. {
  206. sut.CheckList(x => x.ReferenceList, new[] {new OtherEntity(), new OtherEntity()});
  207. }
  208. [Test]
  209. public void should_add_a_reference_list_check()
  210. {
  211. sut.AllProperties.First().ShouldBeOfType(typeof(ReferenceList<ReferenceEntity, OtherEntity>));
  212. }
  213. [Test]
  214. public void should_add_one_check_to_the_specification()
  215. {
  216. sut.AllProperties.ShouldHaveCount(1);
  217. }
  218. [Test]
  219. public void should_set_the_custom_equality_comparer()
  220. {
  221. sut.AllProperties.First().EntityEqualityComparer.ShouldEqual(comparer);
  222. }
  223. }
  224. public class When_a_checked_enumerable_with_a_custom_item_setter_is_added : With_persistence_specification<ReferenceEntity>
  225. {
  226. protected Action<ReferenceEntity, OtherEntity> listSetter;
  227. public override void establish_context()
  228. {
  229. base.establish_context();
  230. listSetter = MockRepository.GenerateStub<Action<ReferenceEntity, OtherEntity>>();
  231. }
  232. public override void because()
  233. {
  234. sut.CheckEnumerable(x => x.ReferenceList, listSetter, new[] {new OtherEntity(), new OtherEntity()});
  235. }
  236. [Test]
  237. public void should_add_a_reference_list_check()
  238. {
  239. sut.AllProperties.First().ShouldBeOfType(typeof(ReferenceList<ReferenceEntity, OtherEntity>));
  240. }
  241. [Test]
  242. public void should_add_one_check_to_the_specification()
  243. {
  244. sut.AllProperties.ShouldHaveCount(1);
  245. }
  246. [Test]
  247. public void should_set_the_custom_equality_comparer()
  248. {
  249. sut.AllProperties.First().EntityEqualityComparer.ShouldEqual(comparer);
  250. }
  251. }
  252. public class When_a_checked_list_with_a_custom_list_setter_is_added : With_persistence_specification<ReferenceEntity>
  253. {
  254. protected Action<ReferenceEntity, IEnumerable<OtherEntity>> listSetter;
  255. public override void establish_context()
  256. {
  257. base.establish_context();
  258. listSetter = MockRepository.GenerateStub<Action<ReferenceEntity, IEnumerable<OtherEntity>>>();
  259. }
  260. public override void because()
  261. {
  262. sut.CheckList(x => x.ReferenceList, new[] {new OtherEntity(), new OtherEntity()}, listSetter);
  263. }
  264. [Test]
  265. public void should_add_a_reference_list_check()
  266. {
  267. sut.AllProperties.First().ShouldBeOfType(typeof(ReferenceList<ReferenceEntity, OtherEntity>));
  268. }
  269. [Test]
  270. public void should_add_one_check_to_the_specification()
  271. {
  272. sut.AllProperties.ShouldHaveCount(1);
  273. }
  274. [Test]
  275. public void should_set_the_custom_equality_comparer()
  276. {
  277. sut.AllProperties.First().EntityEqualityComparer.ShouldEqual(comparer);
  278. }
  279. }
  280. public class When_the_list_setter_of_a_checked_list_is_invoked : When_a_checked_list_with_a_custom_list_setter_is_added
  281. {
  282. private ReferenceEntity entity;
  283. private OtherEntity[] referenced;
  284. public override void establish_context()
  285. {
  286. base.establish_context();
  287. entity = new ReferenceEntity();
  288. referenced = new[] {new OtherEntity(), new OtherEntity()};
  289. }
  290. public override void because()
  291. {
  292. base.because();
  293. ((ReferenceList<ReferenceEntity, OtherEntity>)sut.AllProperties.First()).ValueSetter.Invoke(entity, null, referenced);
  294. }
  295. [Test]
  296. public void should_invoke_the_custom_setter()
  297. {
  298. listSetter.AssertWasCalled(x => x.Invoke(entity, referenced));
  299. }
  300. }
  301. public class When_a_checked_list_with_a_custom_list_item_setter_is_added : With_persistence_specification<ReferenceEntity>
  302. {
  303. protected Action<ReferenceEntity, OtherEntity> listItemSetter;
  304. public override void establish_context()
  305. {
  306. base.establish_context();
  307. listItemSetter = MockRepository.GenerateStub<Action<ReferenceEntity, OtherEntity>>();
  308. }
  309. public override void because()
  310. {
  311. sut.CheckList(x => x.ReferenceList, new[] {new OtherEntity(), new OtherEntity()}, listItemSetter);
  312. }
  313. [Test]
  314. public void should_add_a_reference_list_check()
  315. {
  316. sut.AllProperties.First().ShouldBeOfType(typeof(ReferenceList<ReferenceEntity, OtherEntity>));
  317. }
  318. [Test]
  319. public void should_add_one_check_to_the_specification()
  320. {
  321. sut.AllProperties.ShouldHaveCount(1);
  322. }
  323. [Test]
  324. public void should_set_the_custom_equality_comparer()
  325. {
  326. sut.AllProperties.First().EntityEqualityComparer.ShouldEqual(comparer);
  327. }
  328. }
  329. public class When_the_list_item_setter_of_a_checked_list_is_invoked : When_a_checked_list_with_a_custom_list_item_setter_is_added
  330. {
  331. private ReferenceEntity entity;
  332. private OtherEntity[] referenced;
  333. public override void establish_context()
  334. {
  335. base.establish_context();
  336. entity = new ReferenceEntity();
  337. referenced = new[] {new OtherEntity(), new OtherEntity()};
  338. }
  339. public override void because()
  340. {
  341. base.because();
  342. ((ReferenceList<ReferenceEntity, OtherEntity>)sut.AllProperties.First()).ValueSetter.Invoke(entity, null, referenced);
  343. }
  344. [Test]
  345. public void should_invoke_the_custom_setter_for_each_item()
  346. {
  347. listItemSetter.AssertWasCalled(x => x.Invoke(entity, referenced[0]));
  348. listItemSetter.AssertWasCalled(x => x.Invoke(entity, referenced[1]));
  349. }
  350. }
  351. public class When_a_checked_component_list_is_added : With_persistence_specification<ReferenceEntity>
  352. {
  353. public override void because()
  354. {
  355. sut.CheckComponentList(x => x.ReferenceList, new[] {new OtherEntity(), new OtherEntity()});
  356. }
  357. [Test]
  358. public void should_add_a_list_check()
  359. {
  360. sut.AllProperties.First().ShouldBeOfType(typeof(List<ReferenceEntity, OtherEntity>));
  361. }
  362. [Test]
  363. public void should_add_one_check_to_the_specification()
  364. {
  365. sut.AllProperties.ShouldHaveCount(1);
  366. }
  367. [Test]
  368. public void should_set_the_custom_equality_comparer()
  369. {
  370. sut.AllProperties.First().EntityEqualityComparer.ShouldEqual(comparer);
  371. }
  372. }
  373. public class When_a_checked_component_list_with_a_custom_list_setter_is_added : With_persistence_specification<ReferenceEntity>
  374. {
  375. protected Action<ReferenceEntity, IEnumerable<OtherEntity>> listSetter;
  376. public override void establish_context()
  377. {
  378. base.establish_context();
  379. listSetter = MockRepository.GenerateStub<Action<ReferenceEntity, IEnumerable<OtherEntity>>>();
  380. }
  381. public override void because()
  382. {
  383. sut.CheckComponentList(x => x.ReferenceList, new[] {new OtherEntity(), new OtherEntity()}, listSetter);
  384. }
  385. [Test]
  386. public void should_add_a_reference_property_check()
  387. {
  388. sut.AllProperties.First().ShouldBeOfType(typeof(List<ReferenceEntity, OtherEntity>));
  389. }
  390. [Test]
  391. public void should_add_one_check_to_the_specification()
  392. {
  393. sut.AllProperties.ShouldHaveCount(1);
  394. }
  395. [Test]
  396. public void should_set_the_custom_equality_comparer()
  397. {
  398. sut.AllProperties.First().EntityEqualityComparer.ShouldEqual(comparer);
  399. }
  400. }
  401. public class When_the_list_setter_of_a_checked_component_list_is_invoked : When_a_checked_component_list_with_a_custom_list_setter_is_added
  402. {
  403. private ReferenceEntity entity;
  404. private OtherEntity[] referenced;
  405. public override void establish_context()
  406. {
  407. base.establish_context();
  408. entity = new ReferenceEntity();
  409. referenced = new[] {new OtherEntity(), new OtherEntity()};
  410. }
  411. public override void because()
  412. {
  413. base.because();
  414. ((List<ReferenceEntity, OtherEntity>)sut.AllProperties.First()).ValueSetter.Invoke(entity, null, referenced);
  415. }
  416. [Test]
  417. public void should_invoke_the_custom_setter()
  418. {
  419. listSetter.AssertWasCalled(x => x.Invoke(entity, referenced));
  420. }
  421. }
  422. public class When_a_checked_component_list_with_a_custom_list_item_setter_is_added : With_persistence_specification<ReferenceEntity>
  423. {
  424. protected Action<ReferenceEntity, OtherEntity> listItemSetter;
  425. public override void establish_context()
  426. {
  427. base.establish_context();
  428. listItemSetter = MockRepository.GenerateStub<Action<ReferenceEntity, OtherEntity>>();
  429. }
  430. public override void because()
  431. {
  432. sut.CheckComponentList(x => x.ReferenceList, new[] {new OtherEntity(), new OtherEntity()}, listItemSetter);
  433. }
  434. [Test]
  435. public void should_add_a_reference_property_check()
  436. {
  437. sut.AllProperties.First().ShouldBeOfType(typeof(List<ReferenceEntity, OtherEntity>));
  438. }
  439. [Test]
  440. public void should_add_one_check_to_the_specification()
  441. {
  442. sut.AllProperties.ShouldHaveCount(1);
  443. }
  444. [Test]
  445. public void should_set_the_custom_equality_comparer()
  446. {
  447. sut.AllProperties.First().EntityEqualityComparer.ShouldEqual(comparer);
  448. }
  449. }
  450. public class When_the_list_item_setter_of_a_checked_component_list_is_invoked : When_a_checked_component_list_with_a_custom_list_item_setter_is_added
  451. {
  452. private ReferenceEntity entity;
  453. private OtherEntity[] referenced;
  454. public override void establish_context()
  455. {
  456. base.establish_context();
  457. entity = new ReferenceEntity();
  458. referenced = new[] {new OtherEntity(), new OtherEntity()};
  459. }
  460. public override void because()
  461. {
  462. base.because();
  463. ((List<ReferenceEntity, OtherEntity>)sut.AllProperties.First()).ValueSetter.Invoke(entity, null, referenced);
  464. }
  465. [Test]
  466. public void should_invoke_the_custom_setter_for_each_item()
  467. {
  468. listItemSetter.AssertWasCalled(x => x.Invoke(entity, referenced[0]));
  469. listItemSetter.AssertWasCalled(x => x.Invoke(entity, referenced[1]));
  470. }
  471. }
  472. }