From cbdb4e409e4675512ae81ebbdeb6453f8941a133 Mon Sep 17 00:00:00 2001 From: oneRain Date: Wed, 26 Feb 2020 11:16:21 +0800 Subject: [PATCH] =?UTF-8?q?*=20SubClassTest.cs:=20chore:=20=E5=AE=8C?= =?UTF-8?q?=E5=96=84=20Subclass=20=E5=AE=9E=E7=8E=B0=E5=92=8C=E5=AD=90?= =?UTF-8?q?=E7=B1=BB=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * LCObject.cs: * LeanCloud.cs: * LCCompositionalCondition.cs: --- Storage/Storage.Test/SubClassTest.cs | 99 ++++++++++++++----- .../Query/LCCompositionalCondition.cs | 3 +- Storage/Storage/LCObject.cs | 7 +- Storage/Storage/LeanCloud.cs | 6 +- 4 files changed, 83 insertions(+), 32 deletions(-) diff --git a/Storage/Storage.Test/SubClassTest.cs b/Storage/Storage.Test/SubClassTest.cs index 3419e49..a98c05f 100644 --- a/Storage/Storage.Test/SubClassTest.cs +++ b/Storage/Storage.Test/SubClassTest.cs @@ -1,49 +1,100 @@ using NUnit.Framework; -using LeanCloud; using System.Threading.Tasks; using System.Collections.Generic; +using LeanCloud.Storage; namespace LeanCloud.Test { - [AVClassName("Account")] - public class Account : AVObject { - [AVFieldName("name")] - public string Name { + internal class Hello : LCObject { + internal World World => this["objectValue"] as World; + + internal Hello() : base("Hello") { } + } + + internal class World : LCObject { + internal string Content { get { - return GetProperty("Name"); - } - set { - SetProperty(value, "Name"); + return this["content"] as string; + } set { + this["content"] = value; } } - [AVFieldName("balance")] - public int Balance { + internal World() : base("World") { } + } + + internal class Account : LCObject { + internal int Balance { get { - return GetProperty("Balance"); - } - set { - SetProperty(value, "Balance"); + return (int)this["balance"]; + } set { + this["balance"] = value; } } + + internal Account() : base("Account") { } } [TestFixture] public class SubClassTest { [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] - public async Task SubClass() { - AVObject.RegisterSubclass(); - AVQuery query = new AVQuery(); - IEnumerable accounts = await query.FindAsync(); - foreach (Account account in accounts) { - Assert.NotNull(account.Name); - Assert.Greater(account.Balance, 0); - TestContext.Out.WriteLine($"{account.Name}, {account.Balance}"); + public async Task Create() { + LCObject.RegisterSubclass("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", () => new Account()); + LCQuery query = new LCQuery("Account"); + query.WhereGreaterThan("balance", 500); + List 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", () => new Account()); + Account account = new Account() { + Balance = 1024 + }; + await account.Save(); + await account.Delete(); + } + + [Test] + public async Task Include() { + LCObject.RegisterSubclass("Hello", () => new Hello()); + LCObject.RegisterSubclass("World", () => new World()); + + LCQuery helloQuery = new LCQuery("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"); + } } } diff --git a/Storage/Storage/Internal/Query/LCCompositionalCondition.cs b/Storage/Storage/Internal/Query/LCCompositionalCondition.cs index ad5f39c..79af22d 100644 --- a/Storage/Storage/Internal/Query/LCCompositionalCondition.cs +++ b/Storage/Storage/Internal/Query/LCCompositionalCondition.cs @@ -171,8 +171,7 @@ namespace LeanCloud.Storage.Internal.Query { { "limit", Limit } }; if (conditionList != null && conditionList.Count > 0) { - // TODO json - dict["where"] = Encode(); + dict["where"] = JsonConvert.SerializeObject(Encode()); } if (orderByList != null && orderByList.Count > 0) { dict["order"] = string.Join(",", orderByList); diff --git a/Storage/Storage/LCObject.cs b/Storage/Storage/LCObject.cs index 80593d6..d6c74c9 100644 --- a/Storage/Storage/LCObject.cs +++ b/Storage/Storage/LCObject.cs @@ -262,10 +262,11 @@ namespace LeanCloud.Storage { return this; } - public static void RegisterSubclass(string className, Type type, Func constructor) { - LCSubclassInfo subclassInfo = new LCSubclassInfo(className, type, constructor); + public static void RegisterSubclass(string className, Func constructor) where T : LCObject { + Type classType = typeof(T); + LCSubclassInfo subclassInfo = new LCSubclassInfo(className, classType, constructor); subclassNameDict[className] = subclassInfo; - subclassTypeDict[type] = subclassInfo; + subclassTypeDict[classType] = subclassInfo; } void ApplyOperation(string key, ILCOperation op) { diff --git a/Storage/Storage/LeanCloud.cs b/Storage/Storage/LeanCloud.cs index a413456..ff218b7 100644 --- a/Storage/Storage/LeanCloud.cs +++ b/Storage/Storage/LeanCloud.cs @@ -29,9 +29,9 @@ namespace LeanCloud { throw new ArgumentException(nameof(appKey)); } // 注册 LeanCloud 内部子类化类型 - LCObject.RegisterSubclass(LCUser.CLASS_NAME, typeof(LCUser), () => new LCUser()); - LCObject.RegisterSubclass(LCRole.CLASS_NAME, typeof(LCRole), () => new LCRole()); - LCObject.RegisterSubclass(LCFile.CLASS_NAME, typeof(LCFile), () => new LCFile()); + LCObject.RegisterSubclass(LCUser.CLASS_NAME, () => new LCUser()); + LCObject.RegisterSubclass(LCRole.CLASS_NAME, () => new LCRole()); + LCObject.RegisterSubclass(LCFile.CLASS_NAME, () => new LCFile()); HttpClient = new LCHttpClient(appId, appKey, server, SDKVersion, APIVersion); }