2019-08-27 16:14:35 +08:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-05-06 16:02:26 +08:00
|
|
|
|
using System.Collections.ObjectModel;
|
2020-04-28 17:04:46 +08:00
|
|
|
|
using LeanCloud;
|
2020-02-27 12:31:48 +08:00
|
|
|
|
using LeanCloud.Storage;
|
2019-08-27 16:14:35 +08:00
|
|
|
|
|
2020-04-28 17:04:46 +08:00
|
|
|
|
namespace Storage.Test {
|
2019-08-27 16:14:35 +08:00
|
|
|
|
public class RelationTest {
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void SetUp() {
|
2020-03-10 16:55:17 +08:00
|
|
|
|
LCLogger.LogDelegate += Utils.Print;
|
2020-03-10 16:26:21 +08:00
|
|
|
|
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
|
2020-02-27 12:31:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
|
public void TearDown() {
|
2020-03-10 16:55:17 +08:00
|
|
|
|
LCLogger.LogDelegate -= Utils.Print;
|
2019-08-27 16:14:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2020-02-27 12:31:48 +08:00
|
|
|
|
public async Task AddAndRemove() {
|
|
|
|
|
LCObject parent = new LCObject("Parent");
|
|
|
|
|
LCObject c1 = new LCObject("Child");
|
|
|
|
|
parent.AddRelation("children", c1);
|
|
|
|
|
LCObject c2 = new LCObject("Child");
|
|
|
|
|
parent.AddRelation("children", c2);
|
|
|
|
|
await parent.Save();
|
|
|
|
|
|
|
|
|
|
LCRelation<LCObject> relation = parent["children"] as LCRelation<LCObject>;
|
|
|
|
|
LCQuery<LCObject> query = relation.Query;
|
|
|
|
|
int count = await query.Count();
|
|
|
|
|
|
|
|
|
|
TestContext.WriteLine($"count: {count}");
|
|
|
|
|
Assert.AreEqual(count, 2);
|
|
|
|
|
|
|
|
|
|
parent.RemoveRelation("children", c2);
|
|
|
|
|
await parent.Save();
|
|
|
|
|
|
|
|
|
|
int count2 = await query.Count();
|
|
|
|
|
TestContext.WriteLine($"count: {count2}");
|
|
|
|
|
Assert.AreEqual(count2, 1);
|
2019-08-27 16:14:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2020-02-27 12:31:48 +08:00
|
|
|
|
public async Task Query() {
|
|
|
|
|
LCQuery<LCObject> query = new LCQuery<LCObject>("Parent");
|
|
|
|
|
LCObject parent = await query.Get("5e13112021b47e0070ed0922");
|
|
|
|
|
LCRelation<LCObject> relation = parent["children"] as LCRelation<LCObject>;
|
|
|
|
|
|
|
|
|
|
TestContext.WriteLine(relation.Key);
|
|
|
|
|
TestContext.WriteLine(relation.Parent);
|
|
|
|
|
TestContext.WriteLine(relation.TargetClass);
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(relation.Key);
|
|
|
|
|
Assert.NotNull(relation.Parent);
|
|
|
|
|
Assert.NotNull(relation.TargetClass);
|
|
|
|
|
|
|
|
|
|
LCQuery<LCObject> relationQuery = relation.Query;
|
2020-05-06 16:02:26 +08:00
|
|
|
|
ReadOnlyCollection<LCObject> results = await relationQuery.Find();
|
|
|
|
|
foreach (LCObject item in results) {
|
2020-02-27 12:31:48 +08:00
|
|
|
|
TestContext.WriteLine(item.ObjectId);
|
|
|
|
|
Assert.NotNull(item.ObjectId);
|
2020-05-06 16:02:26 +08:00
|
|
|
|
}
|
2019-08-27 16:14:35 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|