2019-08-01 17:36:34 +08:00
|
|
|
|
using NUnit.Framework;
|
2020-05-06 16:02:26 +08:00
|
|
|
|
using System.Collections.ObjectModel;
|
2019-08-07 14:58:24 +08:00
|
|
|
|
using System.Collections.Generic;
|
2019-08-01 17:36:34 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2020-02-26 13:01:22 +08:00
|
|
|
|
using LeanCloud.Storage;
|
2019-08-01 17:36:34 +08:00
|
|
|
|
|
2020-04-28 17:04:46 +08:00
|
|
|
|
namespace Storage.Test {
|
2021-04-29 15:58:22 +08:00
|
|
|
|
public class QueryTest : BaseTest {
|
2019-08-01 17:36:34 +08:00
|
|
|
|
[Test]
|
2020-02-26 13:01:22 +08:00
|
|
|
|
public async Task BaseQuery() {
|
|
|
|
|
LCQuery<LCObject> query = new LCQuery<LCObject>("Hello");
|
|
|
|
|
query.Limit(2);
|
2020-05-06 16:02:26 +08:00
|
|
|
|
ReadOnlyCollection<LCObject> list = await query.Find();
|
2020-02-26 13:01:22 +08:00
|
|
|
|
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"]);
|
2019-08-06 15:21:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2019-08-07 14:58:24 +08:00
|
|
|
|
public async Task Count() {
|
2020-02-26 13:01:22 +08:00
|
|
|
|
LCQuery<LCObject> query = new LCQuery<LCObject>("Account");
|
|
|
|
|
query.WhereGreaterThan("balance", 200);
|
|
|
|
|
int count = await query.Count();
|
|
|
|
|
TestContext.WriteLine(count);
|
|
|
|
|
Assert.Greater(count, 0);
|
2019-08-01 17:36:34 +08:00
|
|
|
|
}
|
2019-08-07 14:58:24 +08:00
|
|
|
|
|
|
|
|
|
[Test]
|
2020-02-26 13:01:22 +08:00
|
|
|
|
public async Task OrderBy() {
|
|
|
|
|
LCQuery<LCObject> query = new LCQuery<LCObject>("Account");
|
2020-05-06 16:02:26 +08:00
|
|
|
|
query.OrderByAscending("balance");
|
|
|
|
|
ReadOnlyCollection<LCObject> results = await query.Find();
|
2020-02-26 13:01:22 +08:00
|
|
|
|
Assert.LessOrEqual((int)results[0]["balance"], (int)results[1]["balance"]);
|
|
|
|
|
|
|
|
|
|
query = new LCQuery<LCObject>("Account");
|
|
|
|
|
query.OrderByDescending("balance");
|
|
|
|
|
results = await query.Find();
|
|
|
|
|
Assert.GreaterOrEqual((int)results[0]["balance"], (int)results[1]["balance"]);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 16:02:26 +08:00
|
|
|
|
[Test]
|
|
|
|
|
public async Task AddOrder() {
|
|
|
|
|
LCQuery<LCObject> query = new LCQuery<LCObject>("Account");
|
|
|
|
|
query.AddAscendingOrder("balance");
|
|
|
|
|
query.AddDescendingOrder("createdAt");
|
|
|
|
|
ReadOnlyCollection<LCObject> 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-26 13:01:22 +08:00
|
|
|
|
[Test]
|
|
|
|
|
public async Task Include() {
|
2021-04-29 15:58:22 +08:00
|
|
|
|
Hello hello = new Hello {
|
|
|
|
|
World = new World {
|
|
|
|
|
Content = "7788"
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
await hello.Save();
|
|
|
|
|
|
2020-02-26 13:01:22 +08:00
|
|
|
|
LCQuery<LCObject> query = new LCQuery<LCObject>("Hello");
|
|
|
|
|
query.Include("objectValue");
|
2021-04-29 15:58:22 +08:00
|
|
|
|
Hello queryHello = (await query.Get(hello.ObjectId)) as Hello;
|
|
|
|
|
World world = queryHello.World;
|
|
|
|
|
TestContext.WriteLine(world.Content);
|
|
|
|
|
Assert.AreEqual(world.Content, "7788");
|
2020-02-26 13:01:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Get() {
|
2021-04-29 15:58:22 +08:00
|
|
|
|
Account account = new Account {
|
|
|
|
|
Balance = 1024
|
|
|
|
|
};
|
|
|
|
|
await account.Save();
|
2020-02-26 13:01:22 +08:00
|
|
|
|
LCQuery<LCObject> query = new LCQuery<LCObject>("Account");
|
2021-04-29 15:58:22 +08:00
|
|
|
|
Account newAccount = (await query.Get(account.ObjectId)) as Account;
|
|
|
|
|
Assert.AreEqual(newAccount.Balance, 1024);
|
2020-02-26 13:01:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task First() {
|
|
|
|
|
LCQuery<LCObject> query = new LCQuery<LCObject>("Account");
|
|
|
|
|
LCObject account = await query.First();
|
|
|
|
|
Assert.NotNull(account.ObjectId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task GreaterQuery() {
|
|
|
|
|
LCQuery<LCObject> query = new LCQuery<LCObject>("Account");
|
|
|
|
|
query.WhereGreaterThan("balance", 200);
|
2020-05-06 16:02:26 +08:00
|
|
|
|
ReadOnlyCollection<LCObject> list = await query.Find();
|
2020-02-26 13:01:22 +08:00
|
|
|
|
TestContext.WriteLine(list.Count);
|
|
|
|
|
Assert.Greater(list.Count, 0);
|
2019-08-07 14:58:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task And() {
|
2020-02-26 13:01:22 +08:00
|
|
|
|
LCQuery<LCObject> q1 = new LCQuery<LCObject>("Account");
|
|
|
|
|
q1.WhereGreaterThan("balance", 100);
|
|
|
|
|
LCQuery<LCObject> q2 = new LCQuery<LCObject>("Account");
|
|
|
|
|
q2.WhereLessThan("balance", 500);
|
|
|
|
|
LCQuery<LCObject> query = LCQuery<LCObject>.And(new List<LCQuery<LCObject>> { q1, q2 });
|
2020-05-06 16:02:26 +08:00
|
|
|
|
ReadOnlyCollection<LCObject> results = await query.Find();
|
2020-02-26 13:01:22 +08:00
|
|
|
|
TestContext.WriteLine(results.Count);
|
2020-05-06 16:02:26 +08:00
|
|
|
|
foreach (LCObject item in results) {
|
2020-02-26 13:01:22 +08:00
|
|
|
|
int balance = (int)item["balance"];
|
|
|
|
|
Assert.IsTrue(balance >= 100 || balance <= 500);
|
2020-05-06 16:02:26 +08:00
|
|
|
|
}
|
2020-02-26 13:01:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Or() {
|
|
|
|
|
LCQuery<LCObject> q1 = new LCQuery<LCObject>("Account");
|
|
|
|
|
q1.WhereLessThanOrEqualTo("balance", 100);
|
|
|
|
|
LCQuery<LCObject> q2 = new LCQuery<LCObject>("Account");
|
|
|
|
|
q2.WhereGreaterThanOrEqualTo("balance", 500);
|
|
|
|
|
LCQuery<LCObject> query = LCQuery<LCObject>.Or(new List<LCQuery<LCObject>> { q1, q2 });
|
2020-05-06 16:02:26 +08:00
|
|
|
|
ReadOnlyCollection<LCObject> results = await query.Find();
|
2020-02-26 13:01:22 +08:00
|
|
|
|
TestContext.WriteLine(results.Count);
|
2020-05-06 16:02:26 +08:00
|
|
|
|
foreach (LCObject item in results) {
|
2020-02-26 13:01:22 +08:00
|
|
|
|
int balance = (int)item["balance"];
|
|
|
|
|
Assert.IsTrue(balance <= 100 || balance >= 500);
|
2020-05-06 16:02:26 +08:00
|
|
|
|
}
|
2019-08-07 14:58:24 +08:00
|
|
|
|
}
|
2019-09-12 15:07:19 +08:00
|
|
|
|
|
|
|
|
|
[Test]
|
2020-02-26 13:01:22 +08:00
|
|
|
|
public async Task WhereObjectEquals() {
|
2021-04-29 15:58:22 +08:00
|
|
|
|
World world = new World();
|
|
|
|
|
Hello hello = new Hello {
|
|
|
|
|
World = world
|
|
|
|
|
};
|
|
|
|
|
await hello.Save();
|
|
|
|
|
|
2020-02-26 13:01:22 +08:00
|
|
|
|
LCQuery<LCObject> worldQuery = new LCQuery<LCObject>("World");
|
2021-04-29 15:58:22 +08:00
|
|
|
|
LCObject queryWorld = await worldQuery.Get(world.ObjectId);
|
2020-02-26 13:01:22 +08:00
|
|
|
|
LCQuery<LCObject> helloQuery = new LCQuery<LCObject>("Hello");
|
2021-04-29 15:58:22 +08:00
|
|
|
|
helloQuery.WhereEqualTo("objectValue", queryWorld);
|
|
|
|
|
LCObject queryHello = await helloQuery.First();
|
|
|
|
|
TestContext.WriteLine(queryHello.ObjectId);
|
|
|
|
|
Assert.AreEqual(queryHello.ObjectId, hello.ObjectId);
|
2020-02-26 13:01:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Exist() {
|
|
|
|
|
LCQuery<LCObject> query = new LCQuery<LCObject>("Account");
|
|
|
|
|
query.WhereExists("user");
|
2020-05-06 16:02:26 +08:00
|
|
|
|
ReadOnlyCollection<LCObject> results = await query.Find();
|
|
|
|
|
foreach (LCObject item in results) {
|
2020-02-26 13:01:22 +08:00
|
|
|
|
Assert.NotNull(item["user"]);
|
2020-05-06 16:02:26 +08:00
|
|
|
|
}
|
2020-02-26 13:01:22 +08:00
|
|
|
|
|
|
|
|
|
query = new LCQuery<LCObject>("Account");
|
|
|
|
|
query.WhereDoesNotExist("user");
|
|
|
|
|
results = await query.Find();
|
2020-05-06 16:02:26 +08:00
|
|
|
|
foreach (LCObject item in results) {
|
2020-02-26 13:01:22 +08:00
|
|
|
|
Assert.IsNull(item["user"]);
|
2020-05-06 16:02:26 +08:00
|
|
|
|
}
|
2020-02-26 13:01:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Select() {
|
|
|
|
|
LCQuery<LCObject> query = new LCQuery<LCObject>("Account");
|
|
|
|
|
query.Select("balance");
|
2020-05-06 16:02:26 +08:00
|
|
|
|
ReadOnlyCollection<LCObject> results = await query.Find();
|
|
|
|
|
foreach (LCObject item in results) {
|
2020-02-26 13:01:22 +08:00
|
|
|
|
Assert.NotNull(item["balance"]);
|
|
|
|
|
Assert.IsNull(item["user"]);
|
2020-05-06 16:02:26 +08:00
|
|
|
|
}
|
2019-09-12 15:07:19 +08:00
|
|
|
|
}
|
2019-09-16 16:46:12 +08:00
|
|
|
|
|
|
|
|
|
[Test]
|
2020-02-26 13:01:22 +08:00
|
|
|
|
public async Task String() {
|
|
|
|
|
// Start
|
|
|
|
|
LCQuery<LCObject> query = new LCQuery<LCObject>("Hello");
|
|
|
|
|
query.WhereStartsWith("stringValue", "hello");
|
2020-05-06 16:02:26 +08:00
|
|
|
|
ReadOnlyCollection<LCObject> results = await query.Find();
|
|
|
|
|
foreach (LCObject item in results) {
|
2020-02-26 13:01:22 +08:00
|
|
|
|
string str = item["stringValue"] as string;
|
|
|
|
|
Assert.IsTrue(str.StartsWith("hello"));
|
2020-05-06 16:02:26 +08:00
|
|
|
|
}
|
2020-02-26 13:01:22 +08:00
|
|
|
|
|
|
|
|
|
// End
|
|
|
|
|
query = new LCQuery<LCObject>("Hello");
|
|
|
|
|
query.WhereEndsWith("stringValue", "world");
|
|
|
|
|
results = await query.Find();
|
2020-05-06 16:02:26 +08:00
|
|
|
|
foreach (LCObject item in results) {
|
2020-02-26 13:01:22 +08:00
|
|
|
|
string str = item["stringValue"] as string;
|
|
|
|
|
Assert.IsTrue(str.EndsWith("world"));
|
2020-05-06 16:02:26 +08:00
|
|
|
|
}
|
2020-02-26 13:01:22 +08:00
|
|
|
|
|
|
|
|
|
// Contains
|
|
|
|
|
query = new LCQuery<LCObject>("Hello");
|
|
|
|
|
query.WhereContains("stringValue", ",");
|
|
|
|
|
results = await query.Find();
|
2020-05-06 16:02:26 +08:00
|
|
|
|
foreach (LCObject item in results) {
|
2020-02-26 13:01:22 +08:00
|
|
|
|
string str = item["stringValue"] as string;
|
|
|
|
|
Assert.IsTrue(str.Contains(','));
|
2020-05-06 16:02:26 +08:00
|
|
|
|
}
|
2019-09-16 16:46:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2020-02-26 13:01:22 +08:00
|
|
|
|
public async Task Array() {
|
|
|
|
|
// equal
|
|
|
|
|
LCQuery<LCObject> query = new LCQuery<LCObject>("Book");
|
|
|
|
|
query.WhereEqualTo("pages", 3);
|
2020-05-06 16:02:26 +08:00
|
|
|
|
ReadOnlyCollection<LCObject>results = await query.Find();
|
|
|
|
|
foreach (LCObject item in results) {
|
2020-02-26 13:01:22 +08:00
|
|
|
|
List<object> pages = item["pages"] as List<object>;
|
|
|
|
|
Assert.IsTrue(pages.Contains(3));
|
2020-05-06 16:02:26 +08:00
|
|
|
|
}
|
2020-02-26 13:01:22 +08:00
|
|
|
|
|
|
|
|
|
// contain all
|
|
|
|
|
List<int> containAlls = new List<int> { 1, 2, 3, 4, 5, 6, 7 };
|
|
|
|
|
query = new LCQuery<LCObject>("Book");
|
|
|
|
|
query.WhereContainsAll("pages", containAlls);
|
|
|
|
|
results = await query.Find();
|
2020-05-06 16:02:26 +08:00
|
|
|
|
foreach (LCObject item in results) {
|
2020-02-26 13:01:22 +08:00
|
|
|
|
List<object> pages = item["pages"] as List<object>;
|
|
|
|
|
pages.ForEach(i => {
|
|
|
|
|
Assert.IsTrue(pages.Contains(i));
|
|
|
|
|
});
|
2020-05-06 16:02:26 +08:00
|
|
|
|
}
|
2020-02-26 13:01:22 +08:00
|
|
|
|
|
|
|
|
|
// contain in
|
|
|
|
|
List<int> containIns = new List<int> { 4, 5, 6 };
|
|
|
|
|
query = new LCQuery<LCObject>("Book");
|
|
|
|
|
query.WhereContainedIn("pages", containIns);
|
|
|
|
|
results = await query.Find();
|
2020-05-06 16:02:26 +08:00
|
|
|
|
foreach (LCObject item in results) {
|
2020-02-26 13:01:22 +08:00
|
|
|
|
List<object> pages = item["pages"] as List<object>;
|
|
|
|
|
bool f = false;
|
|
|
|
|
containIns.ForEach(i => {
|
|
|
|
|
f |= pages.Contains(i);
|
|
|
|
|
});
|
|
|
|
|
Assert.IsTrue(f);
|
2020-05-06 16:02:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// not contain in
|
|
|
|
|
List<int> notContainIns = new List<int> { 1, 2, 3 };
|
|
|
|
|
query = new LCQuery<LCObject>("Book");
|
|
|
|
|
query.WhereNotContainedIn("pages", notContainIns);
|
|
|
|
|
results = await query.Find();
|
|
|
|
|
foreach (LCObject item in results) {
|
|
|
|
|
List<object> pages = item["pages"] as List<object>;
|
|
|
|
|
bool f = true;
|
|
|
|
|
notContainIns.ForEach(i => {
|
|
|
|
|
f &= !pages.Contains(i);
|
|
|
|
|
});
|
|
|
|
|
Assert.IsTrue(f);
|
|
|
|
|
}
|
2020-02-26 13:01:22 +08:00
|
|
|
|
|
|
|
|
|
// size
|
|
|
|
|
query = new LCQuery<LCObject>("Book");
|
|
|
|
|
query.WhereSizeEqualTo("pages", 7);
|
|
|
|
|
results = await query.Find();
|
2020-05-06 16:02:26 +08:00
|
|
|
|
foreach (LCObject item in results) {
|
2020-02-26 13:01:22 +08:00
|
|
|
|
List<object> pages = item["pages"] as List<object>;
|
|
|
|
|
Assert.AreEqual(pages.Count, 7);
|
2020-05-06 16:02:26 +08:00
|
|
|
|
}
|
2020-02-26 13:01:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[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<LCObject> query = new LCQuery<LCObject>("Todo");
|
|
|
|
|
LCGeoPoint point = new LCGeoPoint(39.91, 116.41);
|
|
|
|
|
query.WhereNear("location", point);
|
2020-05-06 16:02:26 +08:00
|
|
|
|
ReadOnlyCollection<LCObject> results = await query.Find();
|
2020-02-26 13:01:22 +08:00
|
|
|
|
Assert.Greater(results.Count, 0);
|
|
|
|
|
|
|
|
|
|
// in box
|
|
|
|
|
query = new LCQuery<LCObject>("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);
|
2019-09-16 16:46:12 +08:00
|
|
|
|
}
|
2020-05-06 16:02:26 +08:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task Regex() {
|
|
|
|
|
LCQuery<LCObject> query = new LCQuery<LCObject>("Hello");
|
|
|
|
|
query.WhereMatches("stringValue", "^HEllo.*", modifiers: "i");
|
|
|
|
|
ReadOnlyCollection<LCObject> 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<LCObject> worldQuery = new LCQuery<LCObject>("World");
|
|
|
|
|
worldQuery.WhereEqualTo("content", "7788");
|
|
|
|
|
LCQuery<LCObject> helloQuery = new LCQuery<LCObject>("Hello");
|
|
|
|
|
helloQuery.WhereMatchesQuery("objectValue", worldQuery);
|
|
|
|
|
helloQuery.Include("objectValue");
|
|
|
|
|
ReadOnlyCollection<LCObject> 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<LCObject> worldQuery = new LCQuery<LCObject>("World");
|
|
|
|
|
worldQuery.WhereEqualTo("content", "7788");
|
|
|
|
|
LCQuery<LCObject> helloQuery = new LCQuery<LCObject>("Hello");
|
|
|
|
|
helloQuery.WhereDoesNotMatchQuery("objectValue", worldQuery);
|
|
|
|
|
helloQuery.Include("objectValue");
|
|
|
|
|
ReadOnlyCollection<LCObject> hellos = await helloQuery.Find();
|
|
|
|
|
Assert.Greater(hellos.Count, 0);
|
|
|
|
|
foreach (LCObject item in hellos) {
|
|
|
|
|
LCObject world = item["objectValue"] as LCObject;
|
2020-05-07 10:56:45 +08:00
|
|
|
|
Assert.IsTrue(world == null ||
|
|
|
|
|
world["content"] == null ||
|
|
|
|
|
world["content"] as string != "7788");
|
2020-05-06 16:02:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-01 17:36:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|