csharp-sdk-upm/Storage/Storage.Test/QueryTest.cs

122 lines
4.8 KiB
C#
Raw Normal View History

using NUnit.Framework;
2019-08-07 14:58:24 +08:00
using System.Collections.Generic;
using System.Threading.Tasks;
2019-08-07 14:58:24 +08:00
using System.Linq;
using Newtonsoft.Json;
namespace LeanCloud.Test {
public class QueryTest {
[SetUp]
public void SetUp() {
Utils.InitNorthChina();
}
[Test]
2019-08-07 14:58:24 +08:00
public async Task BasicQuery() {
var query = new AVQuery<AVObject>("Account");
query.WhereGreaterThanOrEqualTo("balance", 100);
query.WhereLessThanOrEqualTo("balance", 100);
2019-08-06 15:21:39 +08:00
var results = await query.FindAsync();
foreach (var result in results) {
TestContext.Out.WriteLine(result.ObjectId);
}
}
[Test]
2019-08-07 14:58:24 +08:00
public async Task Count() {
2019-08-06 18:26:56 +08:00
var query = new AVQuery<AVObject>("Foo");
query.WhereEqualTo("content", "hello, world");
var count = await query.CountAsync();
Assert.Greater(count, 8);
}
2019-08-07 14:58:24 +08:00
[Test]
public async Task Or() {
AVQuery<AVObject> q1 = new AVQuery<AVObject>("Foo");
q1.WhereEqualTo("content", "hello");
AVQuery<AVObject> q2 = new AVQuery<AVObject>("Foo");
q2.WhereEqualTo("content", "world");
AVQuery<AVObject> query = AVQuery<AVObject>.Or(new List<AVQuery<AVObject>> { q1, q2 });
IEnumerable<AVObject> results = await query.FindAsync();
foreach (AVObject result in results) {
TestContext.Out.WriteLine(result.ObjectId);
}
}
[Test]
public async Task And() {
AVQuery<AVObject> q1 = new AVQuery<AVObject>("Foo");
q1.WhereContains("content", "hello");
AVQuery<AVObject> q2 = new AVQuery<AVObject>("Foo");
q2.WhereContains("content", "world");
AVQuery<AVObject> query = AVQuery<AVObject>.And(new List<AVQuery<AVObject>> { q1, q2 });
IEnumerable<AVObject> results = await query.FindAsync();
TestContext.Out.WriteLine($"Count: {results.Count()}");
foreach (AVObject result in results) {
TestContext.Out.WriteLine(result.ObjectId);
}
}
* csharp-sdk.sln: chore: 重构 AVFile 为 AVObject 的子类 * RTM.csproj: * AVQuery2.cs: * AVRealtime.cs: * AVIMNotice.cs: * AVIMClient.cs: * AVIMMessage.cs: * Utils.cs: * ICacheEngine.cs: * IAVIMMessage.cs: * AVIMSignature.cs: * AVIMEventArgs.cs: * IAVIMListener.cs: * AVIMException.cs: * JustTest.cs: * FileTest.cs: * AVIMEnumerator.cs: * AVFile.cs: * QueryTest.cs: * AVIMTextMessage.cs: * AVQuery.cs: * AVObject.cs: * AVIMTypedMessage.cs: * AVIMImageMessage.cs: * IAVTimer.cs: * AVIMConversation.cs: * AVClient.cs: * AVIMAudioMessage.cs: * ISignatureFactory.cs: * AVIMCorePlugins.cs: * AVIMBinaryMessage.cs: * AVIMRecalledMessage.cs: * AVIMMessageListener.cs: * AckCommand.cs: * IAVIMPlatformHooks.cs: * AVIMConversationQuery.cs: * AVIMCommand.cs: * ReadCommand.cs: * PatchCommand.cs: * GoAwayListener.cs: * AVIMProtocol.cs: * MessageCommand.cs: * SessionListener.cs: * SessionCommand.cs: * AVIMTemporaryConversation.cs: * RouterState.cs: * AVRouterController.cs: * AVIMCommandRunner.cs: * IAVIMCommandRunner.cs: * AVDecoder.cs: * IAVRouterController.cs: * IWebSocketClient.cs: * MessagePatchListener.cs: * ConversationCommand.cs: * AVIMMessageFieldNameAttribute.cs: * AVIMMessageClassNameAttribute.cs: * QueryOperation.cs: * QueryCondition.cs: * OfflineMessageListener.cs: * AVIMTypedMessageTypeIntAttribute.cs: * AVTimer.Portable.cs: * AVIMConversationListener.cs: * ConversationUnreadListener.cs: * StringEngine.cs: * AWSUploader.cs: * DateTimeEngine.cs: * QiniuUploader.cs: * QCloudUploader.cs: * DictionaryEngine.cs: * AVFileController.cs: * FreeStyleMessageClassInfo.cs: * DefaultWebSocketClient.Portable.cs: * FreeStyleMessageClassingController.cs: * IFreeStyleMessageClassingController.cs:
2019-09-12 15:07:19 +08:00
[Test]
public async Task OrPro() {
AVQuery<AVObject> q1 = AVQuery<AVObject>.Or(new List<AVQuery<AVObject>> {
new AVQuery<AVObject>("Account").WhereEqualTo("balance", 100)
});
AVQuery<AVObject> q2 = AVQuery<AVObject>.Or(new List<AVQuery<AVObject>> {
new AVQuery<AVObject>("Account").WhereEqualTo("balance", 200)
});
AVQuery<AVObject> query = AVQuery<AVObject>.Or(new List<AVQuery<AVObject>> {
q1, q2
});
query.WhereEqualTo("balance", 100);
IEnumerable<AVObject> 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<AVObject> query = new AVQuery<AVObject>("Tag");
query.WhereRelatedTo(todo, "tags");
IEnumerable<AVObject> results = await query.FindAsync();
foreach (AVObject tag in results) {
TestContext.Out.WriteLine(tag.ObjectId);
}
}
[Test]
public void Where() {
AVQuery<AVObject> q1 = new AVQuery<AVObject>();
q1.WhereEqualTo("aa", "bb");
AVQuery<AVObject> q2 = new AVQuery<AVObject>();
q2.WhereEqualTo("cc", "dd");
q2.WhereEqualTo("ee", "ff");
List<AVQuery<AVObject>> queryList = new List<AVQuery<AVObject>> {
q1, q2
};
AVQuery<AVObject> query = AVQuery<AVObject>.Or(queryList);
IDictionary<string, object> obj = query.BuildWhere();
TestContext.Out.WriteLine(JsonConvert.SerializeObject(obj));
AVQuery<AVObject> q3 = new AVQuery<AVObject>();
q3.WhereEqualTo("xx", "yy");
IDictionary<string, object> q3Obj = q3.BuildWhere();
TestContext.Out.WriteLine(JsonConvert.SerializeObject(q3Obj));
AVQuery<AVObject> q4 = new AVQuery<AVObject>();
q4.WhereEqualTo("aaa", "bbb");
q4.WhereEqualTo("ccc", "ddd");
IDictionary<string, object> q4Obj = q4.BuildWhere();
TestContext.Out.WriteLine(JsonConvert.SerializeObject(q4Obj));
AVQuery<AVObject> q5 = new AVQuery<AVObject>();
q5.WhereEqualTo("aaa", "bbb");
q5.WhereEqualTo("aaa", "ccc");
IDictionary<string, object> q5Obj = q5.BuildWhere();
TestContext.Out.WriteLine(JsonConvert.SerializeObject(q5Obj));
}
}
}