using NUnit.Framework; using System.Collections.Generic; using System.Threading.Tasks; using System.Linq; using Newtonsoft.Json; namespace LeanCloud.Test { public class QueryTest { [SetUp] public void SetUp() { Utils.InitNorthChina(); } [Test] public async Task BasicQuery() { var query = new AVQuery("Account"); query.WhereGreaterThanOrEqualTo("balance", 100); query.WhereLessThanOrEqualTo("balance", 100); var results = await query.FindAsync(); foreach (var result in results) { TestContext.Out.WriteLine(result.ObjectId); } } [Test] public async Task Count() { var query = new AVQuery("Foo"); query.WhereEqualTo("content", "hello, world"); var count = await query.CountAsync(); Assert.Greater(count, 8); } [Test] public async Task Or() { AVQuery q1 = new AVQuery("Foo"); q1.WhereEqualTo("content", "hello"); AVQuery q2 = new AVQuery("Foo"); q2.WhereEqualTo("content", "world"); AVQuery query = AVQuery.Or(new List> { q1, q2 }); IEnumerable results = await query.FindAsync(); foreach (AVObject result in results) { TestContext.Out.WriteLine(result.ObjectId); } } [Test] public async Task And() { AVQuery q1 = new AVQuery("Foo"); q1.WhereContains("content", "hello"); AVQuery q2 = new AVQuery("Foo"); q2.WhereContains("content", "world"); AVQuery query = AVQuery.And(new List> { q1, q2 }); IEnumerable results = await query.FindAsync(); TestContext.Out.WriteLine($"Count: {results.Count()}"); foreach (AVObject result in results) { TestContext.Out.WriteLine(result.ObjectId); } } [Test] public async Task OrPro() { AVQuery q1 = AVQuery.Or(new List> { new AVQuery("Account").WhereEqualTo("balance", 100) }); AVQuery q2 = AVQuery.Or(new List> { new AVQuery("Account").WhereEqualTo("balance", 200) }); AVQuery query = AVQuery.Or(new List> { q1, q2 }); query.WhereEqualTo("balance", 100); IEnumerable results = await query.FindAsync(); foreach (AVObject result in results) { TestContext.Out.WriteLine(result.ObjectId); } } [Test] public async Task Related() { AVObject todo = AVObject.CreateWithoutData("Todo", "5d71f798d5de2b006c0136bc"); AVQuery query = new AVQuery("Tag"); query.WhereRelatedTo(todo, "tags"); IEnumerable results = await query.FindAsync(); foreach (AVObject tag in results) { TestContext.Out.WriteLine(tag.ObjectId); } } [Test] public void Where() { AVQuery q1 = new AVQuery(); q1.WhereEqualTo("aa", "bb"); AVQuery q2 = new AVQuery(); q2.WhereEqualTo("cc", "dd"); q2.WhereEqualTo("ee", "ff"); List> queryList = new List> { q1, q2 }; AVQuery query = AVQuery.Or(queryList); IDictionary obj = query.BuildWhere(); TestContext.Out.WriteLine(JsonConvert.SerializeObject(obj)); AVQuery q3 = new AVQuery(); q3.WhereEqualTo("xx", "yy"); IDictionary q3Obj = q3.BuildWhere(); TestContext.Out.WriteLine(JsonConvert.SerializeObject(q3Obj)); AVQuery q4 = new AVQuery(); q4.WhereEqualTo("aaa", "bbb"); q4.WhereEqualTo("ccc", "ddd"); IDictionary q4Obj = q4.BuildWhere(); TestContext.Out.WriteLine(JsonConvert.SerializeObject(q4Obj)); AVQuery q5 = new AVQuery(); q5.WhereEqualTo("aaa", "bbb"); q5.WhereEqualTo("aaa", "ccc"); IDictionary q5Obj = q5.BuildWhere(); TestContext.Out.WriteLine(JsonConvert.SerializeObject(q5Obj)); } } }