PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Test/Tests/TestSpec.cs

http://queryoverspec.codeplex.com
C# | 343 lines | 226 code | 81 blank | 36 comment | 1 complexity | 717a26a3acf76bb874a614921eae7081 MD5 | raw file
Possible License(s): LGPL-2.1
  1. namespace Naskar.QueryOverSpec.Test.Tests
  2. {
  3. using FluentAssertions;
  4. using Microsoft.Practices.Unity;
  5. using Naskar.QueryOverSpec.Impl;
  6. using Naskar.QueryOverSpec.Test.Entities;
  7. using Naskar.QueryOverSpec.Test.Repository;
  8. using Naskar.QueryOverSpec.Test.Results;
  9. using Naskar.QueryOverSpec.Test.Specs;
  10. using Naskar.QueryOverSpec.Test.Unity;
  11. using NHibernate.Criterion;
  12. using NUnit.Framework;
  13. using RestrictionExtensions = Naskar.QueryOverSpec.RestrictionExtensions;
  14. [TestFixture]
  15. public class TestSpec
  16. {
  17. private UnityContainer _container;
  18. private IRepository _repository;
  19. [TestFixtureSetUp]
  20. public void SetUp()
  21. {
  22. _container = new UnityFactory().Create("Naskar.QueryOverSpec");
  23. _repository = _container.Resolve<IRepository>();
  24. }
  25. [TearDown]
  26. public void TearDown()
  27. {
  28. _repository.RemoveAll<Vote>();
  29. _repository.RemoveAll<Instructor>();
  30. _repository.RemoveAll<Course>();
  31. }
  32. [Test]
  33. public void TestAddFindAll()
  34. {
  35. // Arrange
  36. var course1 = new Course() { Name = "Java" };
  37. var course2 = new Course() { Name = "PHP" };
  38. _repository.Add(course1);
  39. _repository.Add(course2);
  40. // Act
  41. var list = _repository.Find<Course>();
  42. // Assert
  43. list.Should().HaveCount(2);
  44. }
  45. [Test]
  46. public void TestQueryOverAsParameterAction()
  47. {
  48. // Arrange
  49. var course1 = new Course() { Name = "Java" };
  50. var course2 = new Course() { Name = "PHP" };
  51. var vote1 = new Vote() { Course = course1, Mail = "teste" };
  52. course1.Votes.Add(vote1);
  53. _repository.Add(course1);
  54. _repository.Add(course2);
  55. // Act
  56. var list =
  57. _repository.Find<Vote>(
  58. x => x.JoinQueryOver(y => y.Course).Where(y => y.Name.IsInsensitiveLike("%java%")));
  59. // Assert
  60. list.Should().HaveCount(1);
  61. }
  62. [Test]
  63. public void TestWithLambda()
  64. {
  65. // Arrange
  66. var course1 = new Course() { Name = "Java" };
  67. var course2 = new Course() { Name = "C#" };
  68. var vote1 = new Vote() { Course = course1, Mail = "teste" };
  69. course1.Votes.Add(vote1);
  70. _repository.Add(course1);
  71. _repository.Add(course2);
  72. RestrictionExtensions.RegisterQueryOverExtensions();
  73. // Act
  74. var list =
  75. _repository.Find<Course>(
  76. new LambdaSpecification<Course>(x => x.Votes.With(y => y.Mail.IsInsensitiveLike("te", MatchMode.Anywhere))));
  77. // Assert
  78. list.Should().HaveCount(1);
  79. }
  80. [Test]
  81. public void TestSpecification()
  82. {
  83. // Arrange
  84. var course1 = new Course() { Name = "Java" };
  85. var course2 = new Course() { Name = "PHP" };
  86. var vote1 = new Vote() { Course = course1, Mail = "teste" };
  87. course1.Votes.Add(vote1);
  88. var courseByExample = _container.Resolve<IByExampleSpec<Course>>();
  89. var courseWithVotes = _container.Resolve<ICourseWithVotes>();
  90. _repository.Add(course1);
  91. _repository.Add(course2);
  92. // Act
  93. var list =
  94. _repository.Find(courseWithVotes.By().And(courseByExample.By(new Course() { Name = "ava" })));
  95. // Assert
  96. list.Should().HaveCount(1);
  97. }
  98. [Test]
  99. public void TestUsingId()
  100. {
  101. // Arrange
  102. var course1 = new Course() { Name = "Java" };
  103. _repository.Add(course1);
  104. var courseByExample = _container.Resolve<IByExampleSpec<Course>>();
  105. // Act
  106. var list =
  107. _repository.Find(courseByExample.By(new Course() { Id = course1.Id }));
  108. // Assert
  109. list.Should().Contain(x => x.Id == course1.Id);
  110. }
  111. [Test]
  112. public void TestSpecificationAndCriterion()
  113. {
  114. // Arrange
  115. var course1 = new Course() { Name = "Java" };
  116. var course2 = new Course() { Name = "PHP" };
  117. var vote1 = new Vote() { Course = course1, Mail = "teste" };
  118. course1.Votes.Add(vote1);
  119. var courseWithVotes = _container.Resolve<ICourseWithVotes>();
  120. _repository.Add(course1);
  121. _repository.Add(course2);
  122. // Act
  123. var list =
  124. _repository.Find(courseWithVotes.By().And(Example.Create(new Course() { Name = "Java" })));
  125. // Assert
  126. list.Should().HaveCount(1);
  127. }
  128. [Test]
  129. public void TestQueryOverWith()
  130. {
  131. // Arrange
  132. var course1 = new Course() { Name = "Java" };
  133. var course2 = new Course() { Name = "PHP" };
  134. var vote1 = new Vote() { Course = course1, Mail = "teste" };
  135. course1.Votes.Add(vote1);
  136. var voteByExample = _container.Resolve<IByExampleSpec<Vote>>();
  137. _repository.Add(course1);
  138. _repository.Add(course2);
  139. // Act
  140. var list =
  141. _repository.Find<Vote>(
  142. voteByExample.By().With(
  143. y => y.Course, new LambdaSpecification<Course>(x => x.Name.IsInsensitiveLike("ava", MatchMode.Anywhere)))
  144. .And(x => x.Mail.IsInsensitiveLike("tes", MatchMode.Anywhere)));
  145. // Assert
  146. list.Should().HaveCount(1);
  147. }
  148. [Test]
  149. public void TestSpecificationWith()
  150. {
  151. // Arrange
  152. var instructor1 = new Instructor() { Name = "fulano", Ative = true };
  153. var instructor2 = new Instructor() { Name = "ciclano", Ative = true };
  154. var course1 = new Course() { Name = "Java", Instructor = instructor1 };
  155. instructor1.Courses.Add(course1);
  156. var course2 = new Course() { Name = "PHP" };
  157. course1.Votes.Add(new Vote() { Course = course1, Mail = "teste" });
  158. course2.Votes.Add(new Vote() { Course = course2, Mail = "teste" });
  159. var instructorByExample = _container.Resolve<IByExampleSpec<Instructor>>();
  160. var courseWithVotes = _container.Resolve<ICourseWithVotes>();
  161. _repository.Add(instructor1);
  162. _repository.Add(instructor2);
  163. _repository.Add(course1);
  164. _repository.Add(course2);
  165. var instructorExample = new Instructor() { Name = "fulano" };
  166. // Act
  167. var list =
  168. _repository.Find(
  169. courseWithVotes.By().With(x => x.Instructor, instructorByExample.By(instructorExample)));
  170. // Assert
  171. list.Should().HaveCount(1);
  172. }
  173. [Test]
  174. public void TestSpecificationWithOr()
  175. {
  176. // Arrange
  177. var instructor1 = new Instructor() { Name = "fulano", Ative = true };
  178. var instructor2 = new Instructor() { Name = "ciclano", Ative = true };
  179. var course1 = new Course() { Name = "Java", Instructor = instructor1 };
  180. instructor1.Courses.Add(course1);
  181. var course2 = new Course() { Name = "PHP", Instructor = instructor2 };
  182. instructor2.Courses.Add(course2);
  183. var course3 = new Course() { Name = "C#" };
  184. var byExamplcourseSpec = _container.Resolve<IByExampleSpec<Course>>();
  185. _repository.Add(course1);
  186. _repository.Add(course2);
  187. _repository.Add(course3);
  188. _repository.Add(instructor1);
  189. _repository.Add(instructor2);
  190. // Act
  191. var list =
  192. _repository.Find(
  193. byExamplcourseSpec.By().With(
  194. x => x.Instructor,
  195. new LambdaSpecification<Instructor>(x => x.Name.IsInsensitiveLike("fulano", MatchMode.Anywhere)).Or(
  196. new LambdaSpecification<Instructor>(x => x.Name.IsInsensitiveLike("ciclano", MatchMode.Anywhere)))));
  197. // Assert
  198. list.Should().HaveCount(2);
  199. }
  200. [Test]
  201. public void TestSpecificationWithOrUsandoCriterion()
  202. {
  203. // Arrange
  204. var instructor1 = new Instructor() { Name = "fulano", Ative = true };
  205. var instructor2 = new Instructor() { Name = "ciclano", Ative = true };
  206. var course1 = new Course() { Name = "Java", Instructor = instructor1 };
  207. instructor1.Courses.Add(course1);
  208. var course2 = new Course() { Name = "PHP", Instructor = instructor2 };
  209. instructor2.Courses.Add(course2);
  210. var course3 = new Course() { Name = "C#" };
  211. var courseByExample = _container.Resolve<IByExampleSpec<Course>>();
  212. _repository.Add(course1);
  213. _repository.Add(course2);
  214. _repository.Add(course3);
  215. _repository.Add(instructor1);
  216. _repository.Add(instructor2);
  217. // Act
  218. var list =
  219. _repository.Find(
  220. courseByExample.By()
  221. .With(x => x.Instructor, new LambdaSpecification<Instructor>(x => x.Name.IsInsensitiveLike("fulano", MatchMode.Anywhere))
  222. .Or(new CriterionSpecification<Instructor>(Property.ForName("Name").Like("ciclano", MatchMode.Anywhere)))));
  223. // Assert
  224. list.Should().HaveCount(2);
  225. }
  226. [Test]
  227. public void TestSpecificationWithTransformResult()
  228. {
  229. // Arrange
  230. var course1 = new Course() { Name = "Java" };
  231. var course2 = new Course() { Name = "PHP" };
  232. var courseByExample = _container.Resolve<IByExampleSpec<Course>>();
  233. _repository.Add(course1);
  234. _repository.Add(course2);
  235. // Act
  236. var list = _repository.Find<Course, CourseDTO>(
  237. courseByExample.By(new Course() { Name = "PH" }), x => x.Id, x => x.Name);
  238. // Assert
  239. list.Should().HaveCount(1);
  240. list[0].Name.Should().NotBeNull().Equals(course1.Name);
  241. }
  242. [Test]
  243. public void TestSpecificationWithTransformMapping()
  244. {
  245. // Arrange
  246. var course1 = new Course() { Name = "Java" };
  247. var course2 = new Course() { Name = "PHP" };
  248. var courseByExample = _container.Resolve<IByExampleSpec<Course>>();
  249. _repository.Add(course1);
  250. _repository.Add(course2);
  251. // Act
  252. var list = _repository.Find(
  253. courseByExample.By(new Course() { Name = "PH" }),
  254. new Mapping<Course, CourseDTO>()
  255. .Add(x => x.Id, y => y.Id)
  256. .Add(x => x.Name, y => y.CompleteName));
  257. // Assert
  258. list.Should().HaveCount(1);
  259. list[0].CompleteName.Should().NotBeNull().Equals(course1.Name);
  260. }
  261. }
  262. }