PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

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