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

101 lines
3.0 KiB
C#
Raw Normal View History

using NUnit.Framework;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
2020-04-28 17:04:46 +08:00
using LeanCloud;
using LeanCloud.Storage;
2020-04-28 17:04:46 +08:00
namespace Storage.Test {
internal class Hello : LCObject {
internal World World => this["objectValue"] as World;
internal Hello() : base("Hello") { }
}
internal class World : LCObject {
internal string Content {
get {
return this["content"] as string;
} set {
this["content"] = value;
}
}
internal World() : base("World") { }
}
internal class Account : LCObject {
internal int Balance {
get {
return (int)this["balance"];
} set {
this["balance"] = value;
}
}
internal Account() : base("Account") { }
}
[TestFixture]
public class SubClassTest {
[SetUp]
public void SetUp() {
2021-04-07 18:08:17 +08:00
Utils.SetUp();
}
[TearDown]
public void TearDown() {
2021-04-07 18:08:17 +08:00
Utils.TearDown();
}
[Test]
public async Task Create() {
LCObject.RegisterSubclass<Account>("Account", () => new Account());
Account account = new Account();
account.Balance = 1000;
await account.Save();
TestContext.WriteLine(account.ObjectId);
Assert.NotNull(account.ObjectId);
}
[Test]
public async Task Query() {
LCObject.RegisterSubclass<Account>("Account", () => new Account());
LCQuery<Account> query = new LCQuery<Account>("Account");
query.WhereGreaterThan("balance", 500);
ReadOnlyCollection<Account> list = await query.Find();
TestContext.WriteLine(list.Count);
Assert.Greater(list.Count, 0);
foreach (Account account in list) {
Assert.NotNull(account.ObjectId);
}
}
[Test]
public async Task Delete() {
LCObject.RegisterSubclass<Account>("Account", () => new Account());
Account account = new Account() {
Balance = 1024
};
await account.Save();
await account.Delete();
}
[Test]
public async Task Include() {
LCObject.RegisterSubclass<Hello>("Hello", () => new Hello());
LCObject.RegisterSubclass<World>("World", () => new World());
LCQuery<Hello> helloQuery = new LCQuery<Hello>("Hello");
helloQuery.Include("objectValue");
Hello hello = await helloQuery.Get("5e0d55aedd3c13006a53cd87");
World world = hello.World;
TestContext.WriteLine(hello.ObjectId);
Assert.AreEqual(hello.ObjectId, "5e0d55aedd3c13006a53cd87");
TestContext.WriteLine(world.ObjectId);
Assert.AreEqual(world.ObjectId, "5e0d55ae21460d006a1ec931");
Assert.AreEqual(world.Content, "7788");
}
}
}