PageRenderTime 113ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/Kooboo.CMS/Kooboo.CMS.Content/Kooboo.CMS.Content.Persistence.MongoDB.Tests/QueryTests.cs

http://kooboo.codeplex.com
C# | 235 lines | 201 code | 26 blank | 8 comment | 0 complexity | 4e2b47232c2840e88fe7d1dc7aa4c7d3 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. #region License
  2. //
  3. // Copyright (c) 2013, Kooboo team
  4. //
  5. // Licensed under the BSD License
  6. // See the file LICENSE.txt for details.
  7. //
  8. #endregion
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using Microsoft.VisualStudio.TestTools.UnitTesting;
  14. using Kooboo.CMS.Content.Query;
  15. using Kooboo.CMS.Content.Models;
  16. using Kooboo.CMS.Content.Persistence.MongoDB;
  17. using Kooboo.CMS.Content.Query.Expressions;
  18. namespace Kooboo.CMS.Content.Persistence.MongoDB.Tests
  19. {
  20. [TestClass]
  21. public class QueryTests
  22. {
  23. Repository repository = null;
  24. Schema newsSchema = null;
  25. Schema categorySchema = null;
  26. Schema commentSchema = null;
  27. TextFolder newsFolder = null;
  28. TextFolder categoryFolder = null;
  29. TextContent newsContent;
  30. TextContent category1;
  31. TextContent category2;
  32. TextContent commenContent;
  33. TextContent news2;
  34. TextContent news3;
  35. TextContentProvider provider = new TextContentProvider();
  36. public QueryTests()
  37. {
  38. EmptyUserKeyGenerator.DefaultGenerator = new EmptyUserKeyGenerator();
  39. Providers.DefaultProviderFactory = new MongoDB.ProviderFactory();
  40. repository = new Repository(Kooboo.UniqueIdGenerator.GetInstance().GetBase32UniqueId(10).ToString());
  41. Providers.DefaultProviderFactory.GetProvider<IRepositoryProvider>().Add(repository);
  42. categorySchema = new Schema(repository, "category") { IsDummy = false };
  43. categorySchema.AddColumn(new Column() { Name = "Title" });
  44. categoryFolder = new TextFolder(repository, "Category") { SchemaName = categorySchema.Name, IsDummy = false };
  45. Providers.DefaultProviderFactory.GetProvider<ITextFolderProvider>().Add(categoryFolder);
  46. newsSchema = new Schema(repository, "News") { IsDummy = false };
  47. newsSchema.AddColumn(new Column() { Name = "title", DataType = Common.DataType.String });
  48. newsSchema.AddColumn(new Column() { Name = "Body", DataType = Common.DataType.String });
  49. newsSchema.AddColumn(new Column() { Name = "Comments", DataType = Common.DataType.Int });
  50. Providers.DefaultProviderFactory.GetProvider<ISchemaProvider>().Add(newsSchema);
  51. newsFolder = new TextFolder(repository, "News") { SchemaName = newsSchema.Name, Categories = new List<CategoryFolder>() { new CategoryFolder() { FolderName = categoryFolder.FullName, SingleChoice = false } }, OrderSetting = new OrderSetting() { FieldName = "Sequence", Direction = OrderDirection.Descending } };
  52. Providers.DefaultProviderFactory.GetProvider<ITextFolderProvider>().Add(newsFolder);
  53. commentSchema = new Schema(repository, "Comment") { IsDummy = false };
  54. commentSchema.AddColumn(new Column() { Name = "Title" });
  55. Providers.DefaultProviderFactory.GetProvider<ISchemaProvider>().Add(commentSchema);
  56. category1 = new TextContent(repository.Name, categorySchema.Name, categoryFolder.FullName);
  57. category1["title"] = "category1";
  58. provider.Add(category1);
  59. category2 = new TextContent(repository.Name, categorySchema.Name, categoryFolder.FullName);
  60. category2["title"] = "category2";
  61. provider.Add(category2);
  62. newsContent = new TextContent(repository.Name, newsSchema.Name, newsFolder.FullName);
  63. newsContent["title"] = "news1";
  64. newsContent["body"] = "body";
  65. newsContent["comments"] = 1;
  66. provider.Add(newsContent);
  67. news2 = new TextContent(repository.Name, newsSchema.Name, newsFolder.FullName);
  68. news2["title"] = "news2";
  69. news2["body"] = "body";
  70. news2["comments"] = 0;
  71. provider.Add(news2);
  72. news3 = new TextContent(repository.Name, newsSchema.Name, newsFolder.FullName);
  73. news3["title"] = "news2";
  74. news3["body"] = "body";
  75. news3["comments"] = 5;
  76. provider.Add(news3);
  77. provider.AddCategories(newsContent, new Category() { ContentUUID = newsContent.UUID, CategoryUUID = category1.UUID, CategoryFolder = category1.FolderName });
  78. provider.AddCategories(newsContent, new Category() { ContentUUID = newsContent.UUID, CategoryUUID = category2.UUID, CategoryFolder = category2.FolderName });
  79. commenContent = new TextContent(repository.Name, commentSchema.Name, "");
  80. commenContent.ParentFolder = newsContent.FolderName;
  81. commenContent.ParentUUID = newsContent.UUID;
  82. commenContent["title"] = "comment1";
  83. provider.Add(commenContent);
  84. }
  85. [TestMethod]
  86. public void SimpleQuery()
  87. {
  88. var content = newsFolder.CreateQuery().WhereEquals("UUID", newsContent.UUID).FirstOrDefault();
  89. Assert.IsNotNull(content);
  90. Assert.AreEqual(content.UUID, newsContent.UUID);
  91. }
  92. [TestMethod]
  93. public void Query_Case_Insensitive()
  94. {
  95. var content = new TextFolder(repository, "news", null) { SchemaName = newsSchema.Name.ToLower(), IsDummy = false }.CreateQuery().WhereEquals("UUID", newsContent.UUID).FirstOrDefault();
  96. Assert.IsNotNull(content);
  97. Assert.AreEqual(content.UUID, newsContent.UUID);
  98. content = newsFolder.CreateQuery().WhereEquals("UUID", newsContent.UUID.ToUpper()).FirstOrDefault();
  99. Assert.IsNotNull(content);
  100. }
  101. [TestMethod]
  102. public void Query_By_Category()
  103. {
  104. var content = newsFolder.CreateQuery().WhereCategory(categoryFolder.CreateQuery().WhereEquals("UUID", category1.UUID)).FirstOrDefault();
  105. Assert.IsNotNull(content);
  106. }
  107. [TestMethod]
  108. public void Query_By_Category2()
  109. {
  110. var content = newsFolder.CreateQuery()
  111. .WhereCategory(categoryFolder.CreateQuery().WhereEquals("UUID", category1.UUID))
  112. .WhereCategory(categoryFolder.CreateQuery().WhereEquals("UUID", category2.UUID))
  113. .FirstOrDefault();
  114. Assert.IsNotNull(content);
  115. }
  116. [TestMethod]
  117. public void Query_Category()
  118. {
  119. var category = newsFolder.CreateQuery().WhereEquals("UUID", newsContent.UUID).Categories(categoryFolder).FirstOrDefault();
  120. Assert.IsNotNull(category);
  121. }
  122. [TestMethod]
  123. public void Query_Parent()
  124. {
  125. var parent = commentSchema.CreateQuery().WhereEquals("UUID", commenContent.UUID).Parent(newsSchema).FirstOrDefault();
  126. Assert.IsNotNull(parent);
  127. }
  128. [TestMethod]
  129. public void Query_Children()
  130. {
  131. var child = newsSchema.CreateQuery().WhereEquals("UUID", newsContent.UUID).Children(commentSchema);
  132. Assert.AreEqual(1, child.Count());
  133. }
  134. [TestMethod]
  135. public void Query_True_AndAlso_False()
  136. {
  137. var newsQuery = newsFolder.CreateQuery().Where(new FalseExpression());
  138. Assert.AreEqual(0, newsQuery.Count());
  139. newsQuery = newsFolder.CreateQuery().Where(new TrueExpression());
  140. Assert.AreEqual(3, newsQuery.Count());
  141. newsQuery = newsFolder.CreateQuery().Where(new AndAlsoExpression(new FalseExpression(), new TrueExpression()));
  142. Assert.AreEqual(0, newsQuery.Count());
  143. }
  144. [TestMethod]
  145. public void Query_WhereLessThan()
  146. {
  147. var count = newsFolder.CreateQuery().WhereLessThan("Comments", 1).Count();
  148. Assert.AreEqual(0, count);
  149. }
  150. [TestMethod]
  151. public void Query_WhereCategory_CategoriesQuery()
  152. {
  153. var categories = newsContent.Categories(categoryFolder);
  154. var newsInCategories = newsFolder.CreateQuery().WhereCategory(categories);
  155. Assert.AreEqual(1, newsInCategories.Count());
  156. }
  157. [TestMethod]
  158. public void Order_ThenOrder()
  159. {
  160. var news = newsFolder.CreateQuery().OrderBy("title").OrderByDescending("comments");
  161. Assert.AreEqual(newsContent["UUID"], news.First()["UUID"].ToString());
  162. Assert.AreEqual(news2["UUID"], news.Last()["UUID"].ToString());
  163. //All field capital will effect the order by.
  164. news = newsFolder.CreateQuery().OrderBy("comments").OrderBy("title");
  165. Assert.AreEqual(news2["UUID"], news.First()["UUID"].ToString());
  166. }
  167. [TestMethod]
  168. public void WhereIn()
  169. {
  170. var news = newsFolder.CreateQuery().WhereEquals("Published", null).WhereIn("UUID", new[] { newsContent.UUID, news2.UUID });
  171. Assert.AreEqual(2, news.Count());
  172. }
  173. [TestMethod]
  174. public void WhereNotIn()
  175. {
  176. var news = newsFolder.CreateQuery().WhereEquals("Published", null).WhereNotIn("UUID", new[] { newsContent.UUID, news2.UUID });
  177. Assert.AreEqual(1, news.Count());
  178. }
  179. [ExpectedException(typeof(NotSupportedException))]
  180. [TestMethod]
  181. public void Test_ExecuteQuery()
  182. {
  183. var contents = repository.ExecuteQuery(string.Format("db.{0}.{1}.find({{ }}).limit(50);", repository.Name, newsSchema.Name));
  184. }
  185. [TestMethod]
  186. public void Test_UpdateFields()
  187. {
  188. Kooboo.CMS.Content.Services.ServiceFactory.TextContentManager.Update(repository, newsSchema, news3.UUID, new string[] { "Published" }, new object[] { true });
  189. var news = newsFolder.CreateQuery().WhereEquals("UUID", news3.UUID).First();
  190. Assert.AreEqual(true, news.Published);
  191. }
  192. //https://jira.mongodb.org/browse/SERVER-2585
  193. [TestMethod]
  194. public void Test_NestedOr()
  195. {
  196. IWhereExpression exp = new FalseExpression();
  197. exp = new OrElseExpression(exp, (new WhereContainsExpression(null, "title", "news1")));
  198. exp = new OrElseExpression(exp, (new WhereContainsExpression(null, "title", "news2")));
  199. var news = newsFolder.CreateQuery().Where(exp);
  200. Assert.AreEqual(3, news.Count());
  201. }
  202. [TestMethod]
  203. public void OrderByTitle()
  204. {
  205. var news = newsFolder.CreateQuery().WhereEquals("Published", null).OrderBy("Title");
  206. Assert.AreEqual(newsContent.UUID, news.First().UUID);
  207. }
  208. }
  209. }