using NUnit.Framework; using System.Collections.ObjectModel; using System.Collections.Generic; using System.Threading.Tasks; using LeanCloud; using LeanCloud.Storage; namespace Storage.Test { public class QueryTest { [SetUp] public void SetUp() { LCLogger.LogDelegate += Utils.Print; LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com"); } [TearDown] public void TearDown() { LCLogger.LogDelegate -= Utils.Print; } [Test] public async Task BaseQuery() { LCQuery query = new LCQuery("Hello"); query.Limit(2); ReadOnlyCollection list = await query.Find(); TestContext.WriteLine(list.Count); Assert.AreEqual(list.Count, 2); foreach (LCObject item in list) { Assert.NotNull(item.ClassName); Assert.NotNull(item.ObjectId); Assert.NotNull(item.CreatedAt); Assert.NotNull(item.UpdatedAt); TestContext.WriteLine(item.ClassName); TestContext.WriteLine(item.ObjectId); TestContext.WriteLine(item.CreatedAt); TestContext.WriteLine(item.UpdatedAt); TestContext.WriteLine(item["intValue"]); TestContext.WriteLine(item["boolValue"]); TestContext.WriteLine(item["stringValue"]); } } [Test] public async Task Count() { LCQuery query = new LCQuery("Account"); query.WhereGreaterThan("balance", 200); int count = await query.Count(); TestContext.WriteLine(count); Assert.Greater(count, 0); } [Test] public async Task OrderBy() { LCQuery query = new LCQuery("Account"); query.OrderByAscending("balance"); ReadOnlyCollection results = await query.Find(); Assert.LessOrEqual((int)results[0]["balance"], (int)results[1]["balance"]); query = new LCQuery("Account"); query.OrderByDescending("balance"); results = await query.Find(); Assert.GreaterOrEqual((int)results[0]["balance"], (int)results[1]["balance"]); } [Test] public async Task AddOrder() { LCQuery query = new LCQuery("Account"); query.AddAscendingOrder("balance"); query.AddDescendingOrder("createdAt"); ReadOnlyCollection results = await query.Find(); for (int i = 0; i + 1 < results.Count; i++) { LCObject a1 = results[i]; LCObject a2 = results[i + 1]; int b1 = (int)a1["balance"]; int b2 = (int)a2["balance"]; Assert.IsTrue(b1 < b2 || a1.CreatedAt.CompareTo(a2.CreatedAt) >= 0); } } [Test] public async Task Include() { LCQuery query = new LCQuery("Hello"); query.Include("objectValue"); LCObject hello = await query.Get("5e0d55aedd3c13006a53cd87"); LCObject world = hello["objectValue"] as LCObject; TestContext.WriteLine(world["content"]); Assert.AreEqual(world["content"], "7788"); } [Test] public async Task Get() { LCQuery query = new LCQuery("Account"); LCObject account = await query.Get("5e0d9f7fd4b56c008e5d048a"); Assert.AreEqual(account["balance"], 400); } [Test] public async Task First() { LCQuery query = new LCQuery("Account"); LCObject account = await query.First(); Assert.NotNull(account.ObjectId); } [Test] public async Task GreaterQuery() { LCQuery query = new LCQuery("Account"); query.WhereGreaterThan("balance", 200); ReadOnlyCollection list = await query.Find(); TestContext.WriteLine(list.Count); Assert.Greater(list.Count, 0); } [Test] public async Task And() { LCQuery q1 = new LCQuery("Account"); q1.WhereGreaterThan("balance", 100); LCQuery q2 = new LCQuery("Account"); q2.WhereLessThan("balance", 500); LCQuery query = LCQuery.And(new List> { q1, q2 }); ReadOnlyCollection results = await query.Find(); TestContext.WriteLine(results.Count); foreach (LCObject item in results) { int balance = (int)item["balance"]; Assert.IsTrue(balance >= 100 || balance <= 500); } } [Test] public async Task Or() { LCQuery q1 = new LCQuery("Account"); q1.WhereLessThanOrEqualTo("balance", 100); LCQuery q2 = new LCQuery("Account"); q2.WhereGreaterThanOrEqualTo("balance", 500); LCQuery query = LCQuery.Or(new List> { q1, q2 }); ReadOnlyCollection results = await query.Find(); TestContext.WriteLine(results.Count); foreach (LCObject item in results) { int balance = (int)item["balance"]; Assert.IsTrue(balance <= 100 || balance >= 500); } } [Test] public async Task WhereObjectEquals() { LCQuery worldQuery = new LCQuery("World"); LCObject world = await worldQuery.Get("5e0d55ae21460d006a1ec931"); LCQuery helloQuery = new LCQuery("Hello"); helloQuery.WhereEqualTo("objectValue", world); LCObject hello = await helloQuery.First(); TestContext.WriteLine(hello.ObjectId); Assert.AreEqual(hello.ObjectId, "5e0d55aedd3c13006a53cd87"); } [Test] public async Task Exist() { LCQuery query = new LCQuery("Account"); query.WhereExists("user"); ReadOnlyCollection results = await query.Find(); foreach (LCObject item in results) { Assert.NotNull(item["user"]); } query = new LCQuery("Account"); query.WhereDoesNotExist("user"); results = await query.Find(); foreach (LCObject item in results) { Assert.IsNull(item["user"]); } } [Test] public async Task Select() { LCQuery query = new LCQuery("Account"); query.Select("balance"); ReadOnlyCollection results = await query.Find(); foreach (LCObject item in results) { Assert.NotNull(item["balance"]); Assert.IsNull(item["user"]); } } [Test] public async Task String() { // Start LCQuery query = new LCQuery("Hello"); query.WhereStartsWith("stringValue", "hello"); ReadOnlyCollection results = await query.Find(); foreach (LCObject item in results) { string str = item["stringValue"] as string; Assert.IsTrue(str.StartsWith("hello")); } // End query = new LCQuery("Hello"); query.WhereEndsWith("stringValue", "world"); results = await query.Find(); foreach (LCObject item in results) { string str = item["stringValue"] as string; Assert.IsTrue(str.EndsWith("world")); } // Contains query = new LCQuery("Hello"); query.WhereContains("stringValue", ","); results = await query.Find(); foreach (LCObject item in results) { string str = item["stringValue"] as string; Assert.IsTrue(str.Contains(',')); } } [Test] public async Task Array() { // equal LCQuery query = new LCQuery("Book"); query.WhereEqualTo("pages", 3); ReadOnlyCollectionresults = await query.Find(); foreach (LCObject item in results) { List pages = item["pages"] as List; Assert.IsTrue(pages.Contains(3)); } // contain all List containAlls = new List { 1, 2, 3, 4, 5, 6, 7 }; query = new LCQuery("Book"); query.WhereContainsAll("pages", containAlls); results = await query.Find(); foreach (LCObject item in results) { List pages = item["pages"] as List; pages.ForEach(i => { Assert.IsTrue(pages.Contains(i)); }); } // contain in List containIns = new List { 4, 5, 6 }; query = new LCQuery("Book"); query.WhereContainedIn("pages", containIns); results = await query.Find(); foreach (LCObject item in results) { List pages = item["pages"] as List; bool f = false; containIns.ForEach(i => { f |= pages.Contains(i); }); Assert.IsTrue(f); } // not contain in List notContainIns = new List { 1, 2, 3 }; query = new LCQuery("Book"); query.WhereNotContainedIn("pages", notContainIns); results = await query.Find(); foreach (LCObject item in results) { List pages = item["pages"] as List; bool f = true; notContainIns.ForEach(i => { f &= !pages.Contains(i); }); Assert.IsTrue(f); } // size query = new LCQuery("Book"); query.WhereSizeEqualTo("pages", 7); results = await query.Find(); foreach (LCObject item in results) { List pages = item["pages"] as List; Assert.AreEqual(pages.Count, 7); } } [Test] public async Task Geo() { LCObject obj = new LCObject("Todo"); LCGeoPoint location = new LCGeoPoint(39.9, 116.4); obj["location"] = location; await obj.Save(); // near LCQuery query = new LCQuery("Todo"); LCGeoPoint point = new LCGeoPoint(39.91, 116.41); query.WhereNear("location", point); ReadOnlyCollection results = await query.Find(); Assert.Greater(results.Count, 0); // in box query = new LCQuery("Todo"); LCGeoPoint southwest = new LCGeoPoint(30, 115); LCGeoPoint northeast = new LCGeoPoint(40, 118); query.WhereWithinGeoBox("location", southwest, northeast); results = await query.Find(); Assert.Greater(results.Count, 0); } [Test] public async Task Regex() { LCQuery query = new LCQuery("Hello"); query.WhereMatches("stringValue", "^HEllo.*", modifiers: "i"); ReadOnlyCollection results = await query.Find(); Assert.Greater(results.Count, 0); foreach (LCObject item in results) { string str = item["stringValue"] as string; Assert.IsTrue(str.StartsWith("hello")); } } [Test] public async Task InQuery() { LCQuery worldQuery = new LCQuery("World"); worldQuery.WhereEqualTo("content", "7788"); LCQuery helloQuery = new LCQuery("Hello"); helloQuery.WhereMatchesQuery("objectValue", worldQuery); helloQuery.Include("objectValue"); ReadOnlyCollection hellos = await helloQuery.Find(); Assert.Greater(hellos.Count, 0); foreach (LCObject item in hellos) { LCObject world = item["objectValue"] as LCObject; Assert.AreEqual(world["content"], "7788"); } } [Test] public async Task NotInQuery() { LCQuery worldQuery = new LCQuery("World"); worldQuery.WhereEqualTo("content", "7788"); LCQuery helloQuery = new LCQuery("Hello"); helloQuery.WhereDoesNotMatchQuery("objectValue", worldQuery); helloQuery.Include("objectValue"); ReadOnlyCollection hellos = await helloQuery.Find(); Assert.Greater(hellos.Count, 0); foreach (LCObject item in hellos) { LCObject world = item["objectValue"] as LCObject; Assert.IsTrue(world == null || world["content"] != "7788"); } } } }