* SubClassTest.cs: chore: 完善 Subclass 实现和子类化

* LCObject.cs:
* LeanCloud.cs:
* LCCompositionalCondition.cs:
oneRain 2020-02-26 11:16:21 +08:00
parent 05047115d1
commit cbdb4e409e
4 changed files with 83 additions and 32 deletions

View File

@ -1,49 +1,100 @@
using NUnit.Framework; using NUnit.Framework;
using LeanCloud;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections.Generic; using System.Collections.Generic;
using LeanCloud.Storage;
namespace LeanCloud.Test { namespace LeanCloud.Test {
[AVClassName("Account")] internal class Hello : LCObject {
public class Account : AVObject { internal World World => this["objectValue"] as World;
[AVFieldName("name")]
public string Name { internal Hello() : base("Hello") { }
}
internal class World : LCObject {
internal string Content {
get { get {
return GetProperty<string>("Name"); return this["content"] as string;
} } set {
set { this["content"] = value;
SetProperty(value, "Name");
} }
} }
[AVFieldName("balance")] internal World() : base("World") { }
public int Balance { }
internal class Account : LCObject {
internal int Balance {
get { get {
return GetProperty<int>("Balance"); return (int)this["balance"];
} } set {
set { this["balance"] = value;
SetProperty(value, "Balance");
} }
} }
internal Account() : base("Account") { }
} }
[TestFixture] [TestFixture]
public class SubClassTest { public class SubClassTest {
[SetUp] [SetUp]
public void SetUp() { public void SetUp() {
Utils.InitNorthChina(true); Logger.LogDelegate += Utils.Print;
LeanCloud.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
}
[TearDown]
public void TearDown() {
Logger.LogDelegate -= Utils.Print;
} }
[Test] [Test]
public async Task SubClass() { public async Task Create() {
AVObject.RegisterSubclass<Account>(); LCObject.RegisterSubclass<Account>("Account", () => new Account());
AVQuery<Account> query = new AVQuery<Account>(); Account account = new Account();
IEnumerable<Account> accounts = await query.FindAsync(); account.Balance = 1000;
foreach (Account account in accounts) { await account.Save();
Assert.NotNull(account.Name); TestContext.WriteLine(account.ObjectId);
Assert.Greater(account.Balance, 0); Assert.NotNull(account.ObjectId);
TestContext.Out.WriteLine($"{account.Name}, {account.Balance}"); }
[Test]
public async Task Query() {
LCObject.RegisterSubclass<Account>("Account", () => new Account());
LCQuery<Account> query = new LCQuery<Account>("Account");
query.WhereGreaterThan("balance", 500);
List<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");
}
} }
} }

View File

@ -171,8 +171,7 @@ namespace LeanCloud.Storage.Internal.Query {
{ "limit", Limit } { "limit", Limit }
}; };
if (conditionList != null && conditionList.Count > 0) { if (conditionList != null && conditionList.Count > 0) {
// TODO json dict["where"] = JsonConvert.SerializeObject(Encode());
dict["where"] = Encode();
} }
if (orderByList != null && orderByList.Count > 0) { if (orderByList != null && orderByList.Count > 0) {
dict["order"] = string.Join(",", orderByList); dict["order"] = string.Join(",", orderByList);

View File

@ -262,10 +262,11 @@ namespace LeanCloud.Storage {
return this; return this;
} }
public static void RegisterSubclass(string className, Type type, Func<LCObject> constructor) { public static void RegisterSubclass<T>(string className, Func<LCObject> constructor) where T : LCObject {
LCSubclassInfo subclassInfo = new LCSubclassInfo(className, type, constructor); Type classType = typeof(T);
LCSubclassInfo subclassInfo = new LCSubclassInfo(className, classType, constructor);
subclassNameDict[className] = subclassInfo; subclassNameDict[className] = subclassInfo;
subclassTypeDict[type] = subclassInfo; subclassTypeDict[classType] = subclassInfo;
} }
void ApplyOperation(string key, ILCOperation op) { void ApplyOperation(string key, ILCOperation op) {

View File

@ -29,9 +29,9 @@ namespace LeanCloud {
throw new ArgumentException(nameof(appKey)); throw new ArgumentException(nameof(appKey));
} }
// 注册 LeanCloud 内部子类化类型 // 注册 LeanCloud 内部子类化类型
LCObject.RegisterSubclass(LCUser.CLASS_NAME, typeof(LCUser), () => new LCUser()); LCObject.RegisterSubclass<LCUser>(LCUser.CLASS_NAME, () => new LCUser());
LCObject.RegisterSubclass(LCRole.CLASS_NAME, typeof(LCRole), () => new LCRole()); LCObject.RegisterSubclass<LCRole>(LCRole.CLASS_NAME, () => new LCRole());
LCObject.RegisterSubclass(LCFile.CLASS_NAME, typeof(LCFile), () => new LCFile()); LCObject.RegisterSubclass<LCFile>(LCFile.CLASS_NAME, () => new LCFile());
HttpClient = new LCHttpClient(appId, appKey, server, SDKVersion, APIVersion); HttpClient = new LCHttpClient(appId, appKey, server, SDKVersion, APIVersion);
} }