diff --git a/Storage/Storage.Test/ACLTest.cs b/Storage/Storage.Test/ACLTest.cs index 22be01f..c510d2c 100644 --- a/Storage/Storage.Test/ACLTest.cs +++ b/Storage/Storage.Test/ACLTest.cs @@ -1,46 +1,41 @@ using NUnit.Framework; using System.Threading.Tasks; using System.Collections.ObjectModel; +using System; using LeanCloud; using LeanCloud.Storage; namespace Storage.Test { - public class ACLTest { - [SetUp] - public void SetUp() { - Utils.SetUp(); - } - - [TearDown] - public void TearDown() { - Utils.TearDown(); - } + public class ACLTest : BaseTest { + private Account account; [Test] + [Order(0)] public async Task PrivateReadAndWrite() { - LCObject account = new LCObject("Account"); - LCACL acl = new LCACL(); - acl.PublicReadAccess = false; - acl.PublicWriteAccess = false; - account.ACL = acl; + Account account = new Account(); + account.ACL = new LCACL { + PublicReadAccess = false, + PublicWriteAccess = false + }; + account.Balance = 1024; account["balance"] = 1024; await account.Save(); - Assert.IsFalse(acl.PublicReadAccess); - Assert.IsFalse(acl.PublicWriteAccess); + Assert.IsFalse(account.ACL.PublicReadAccess); + Assert.IsFalse(account.ACL.PublicWriteAccess); } [Test] + [Order(1)] public async Task UserReadAndWrite() { - await LCUser.Login("hello", "world"); - LCObject account = new LCObject("Account"); + await LCUser.Login(TestPhone, TestPhone); + account = new Account(); LCUser currentUser = await LCUser.GetCurrent(); - LCACL acl = LCACL.CreateWithOwner(currentUser); - account.ACL = acl; - account["balance"] = 512; + account.ACL = LCACL.CreateWithOwner(currentUser); + account.Balance = 512; await account.Save(); - Assert.IsTrue(acl.GetUserReadAccess(currentUser)); - Assert.IsTrue(acl.GetUserWriteAccess(currentUser)); + Assert.IsTrue(account.ACL.GetUserReadAccess(currentUser)); + Assert.IsTrue(account.ACL.GetUserWriteAccess(currentUser)); LCQuery query = new LCQuery("Account"); LCObject result = await query.Get(account.ObjectId); @@ -57,29 +52,39 @@ namespace Storage.Test { } [Test] + [Order(2)] public async Task RoleReadAndWrite() { - LCQuery query = LCRole.GetQuery(); - LCRole owner = await query.Get("5e1440cbfc36ed006add1b8d"); - LCObject account = new LCObject("Account"); + LCUser currentUser = await LCUser.Login(TestPhone, TestPhone); + string name = $"role_{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}"; + LCACL roleACL = new LCACL(); + roleACL.SetUserReadAccess(currentUser, true); + roleACL.SetUserWriteAccess(currentUser, true); + LCRole role = LCRole.Create(name, roleACL); + role.AddRelation("users", currentUser); + await role.Save(); + + account = new Account(); LCACL acl = new LCACL(); - acl.SetRoleReadAccess(owner, true); - acl.SetRoleWriteAccess(owner, true); + acl.SetRoleReadAccess(role, true); + acl.SetRoleWriteAccess(role, true); account.ACL = acl; await account.Save(); - Assert.IsTrue(acl.GetRoleReadAccess(owner)); - Assert.IsTrue(acl.GetRoleWriteAccess(owner)); + Assert.IsTrue(acl.GetRoleReadAccess(role)); + Assert.IsTrue(acl.GetRoleWriteAccess(role)); } [Test] + [Order(3)] public async Task Query() { - await LCUser.Login("game", "play"); + await LCUser.Login(TestPhone, TestPhone); LCQuery query = new LCQuery("Account"); - LCObject account = await query.Get("5e144525dd3c13006a8f8de2"); - TestContext.WriteLine(account.ObjectId); - Assert.NotNull(account.ObjectId); + Account queryAccount = (await query.Get(account.ObjectId)) as Account; + TestContext.WriteLine(queryAccount.ObjectId); + Assert.NotNull(queryAccount.ObjectId); } [Test] + [Order(4)] public async Task Serialization() { await LCUser.Login("hello", "world"); LCQuery query = new LCQuery("Account") { diff --git a/Storage/Storage.Test/BaseTest.cs b/Storage/Storage.Test/BaseTest.cs new file mode 100644 index 0000000..3295d68 --- /dev/null +++ b/Storage/Storage.Test/BaseTest.cs @@ -0,0 +1,86 @@ +using NUnit.Framework; +using LeanCloud; +using LeanCloud.Storage; + +namespace Storage.Test { + internal class Hello : LCObject { + internal World World { + get => this["objectValue"] as World; + set { + this["objectValue"] = value; + } + } + + internal Hello() : base("Hello") { } + } + + internal class World : LCObject { + internal string Content { + get => this["content"] as string; + set { + this["content"] = value; + } + } + + internal World() : base("World") { } + } + + internal class Account : LCObject { + internal int Balance { + get => (int)this["balance"]; + set { + this["balance"] = value; + } + } + + internal LCUser User { + get => this["user"] as LCUser; + set { + this["user"] = value; + } + } + + internal Account() : base("Account") { } + } + + public class BaseTest { + internal const string AppId = "ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz"; + internal const string AppKey = "NUKmuRbdAhg1vrb2wexYo1jo"; + internal const string MasterKey = "pyvbNSh5jXsuFQ3C8EgnIdhw"; + internal const string AppServer = "https://ikggdre2.lc-cn-n1-shared.com"; + + internal const string TestPhone = "18888888888"; + internal const string TestSMSCode = "235750"; + + [SetUp] + public virtual void SetUp() { + LCLogger.LogDelegate += Print; + LCApplication.Initialize(AppId, AppKey, AppServer); + LCObject.RegisterSubclass("Account", () => new Account()); + LCObject.RegisterSubclass("Hello", () => new Hello()); + LCObject.RegisterSubclass("World", () => new World()); + } + + [TearDown] + public virtual void TearDown() { + LCLogger.LogDelegate -= Print; + } + + internal static void Print(LCLogLevel level, string info) { + switch (level) { + case LCLogLevel.Debug: + TestContext.Out.WriteLine($"[DEBUG] {info}"); + break; + case LCLogLevel.Warn: + TestContext.Out.WriteLine($"[WARNING] {info}"); + break; + case LCLogLevel.Error: + TestContext.Out.WriteLine($"[ERROR] {info}"); + break; + default: + TestContext.Out.WriteLine(info); + break; + } + } + } +} diff --git a/Storage/Storage.Test/CaptchaTest.cs b/Storage/Storage.Test/CaptchaTest.cs index 524f72d..3afa500 100644 --- a/Storage/Storage.Test/CaptchaTest.cs +++ b/Storage/Storage.Test/CaptchaTest.cs @@ -1,22 +1,11 @@ using NUnit.Framework; using System.Threading.Tasks; -using LeanCloud; using LeanCloud.Storage; using static NUnit.Framework.TestContext; namespace Storage.Test { - public class CaptchaTest { - [SetUp] - public void SetUp() { - Utils.SetUp(); - } - - [TearDown] - public void TearDown() { - Utils.TearDown(); - } - + public class CaptchaTest : BaseTest { //[Test] public async Task Request() { LCCapture captcha = await LCCaptchaClient.RequestCaptcha(); diff --git a/Storage/Storage.Test/CloudTest.cs b/Storage/Storage.Test/CloudTest.cs index 53bbaed..37bcfef 100644 --- a/Storage/Storage.Test/CloudTest.cs +++ b/Storage/Storage.Test/CloudTest.cs @@ -2,83 +2,60 @@ using System.Collections.Generic; using System.Threading.Tasks; using System.Linq; -using LeanCloud; using LeanCloud.Storage; namespace Storage.Test { - public class CloudTest { - [SetUp] - public void SetUp() { - LCLogger.LogDelegate += Utils.Print; - //LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com"); - LCApplication.Initialize("8ijVI3gBAnPGynW0rVfh5gHP-gzGzoHsz", "265r8JSHhNYpV0qIJBvUWrQY", "https://8ijvi3gb.lc-cn-n1-shared.com"); - } - - [TearDown] - public void TearDown() { - LCLogger.LogDelegate -= Utils.Print; + public class CloudTest : BaseTest { + [Test] + public async Task Ping() { + Dictionary response = await LCCloud.Run("ping"); + TestContext.WriteLine(response["result"]); + Assert.AreEqual(response["result"], "pong"); } [Test] - public async Task Call() { - Dictionary response = await LCCloud.Run("hello", parameters: new Dictionary { + public async Task Hello() { + string result = await LCCloud.Run("hello", new Dictionary { { "name", "world" } }); - TestContext.WriteLine(response["result"]); - Assert.AreEqual(response["result"], "Hello, world!"); + TestContext.WriteLine(result); + Assert.AreEqual(result, "hello, world"); } [Test] - public async Task RunAverageScore() { - float score = await LCCloud.Run("averageStars", new Dictionary { - { "movie", "夏洛特烦恼" } + public async Task GetObject() { + LCObject hello = new LCObject("Hello"); + await hello.Save(); + object reponse = await LCCloud.RPC("getObject", new Dictionary { + { "className", "Hello" }, + { "id", hello.ObjectId } }); - TestContext.WriteLine($"score: {score}"); - Assert.True(score.Equals(3.8f)); + LCObject obj = reponse as LCObject; + Assert.AreEqual(obj.ObjectId, hello.ObjectId); } [Test] - public async Task CallWithoutParams() { - await LCCloud.Run("hello"); - } - - [Test] - public async Task RPC() { - List result = await LCCloud.RPC("getTycoonList") as List; - IEnumerable tycoonList = result.Cast(); - foreach (LCObject item in tycoonList) { - TestContext.WriteLine(item.ObjectId); - Assert.NotNull(item.ObjectId); + public async Task GetObjects() { + object response = await LCCloud.RPC("getObjects"); + List list = response as List; + IEnumerable objects = list.Cast(); + TestContext.WriteLine(objects.Count()); + Assert.Greater(objects.Count(), 0); + foreach (LCObject obj in objects) { + int balance = (int)obj["balance"]; + Assert.Greater(balance, 100); } } [Test] - public async Task RPCObject() { - LCQuery query = new LCQuery("Todo"); - LCObject todo = await query.Get("6052cd87b725a143ea83dbf8"); - object result = await LCCloud.RPC("getTodo", todo); - LCObject obj = result as LCObject; - TestContext.WriteLine(obj.ToString()); - } - - [Test] - public async Task RPCObjects() { - Dictionary parameters = new Dictionary { - { "limit", 20 } - }; - List result = await LCCloud.RPC("getTodos", parameters) as List; - IEnumerable todos = result.Cast(); - foreach (LCObject todo in todos) { - TestContext.WriteLine(todo.ObjectId); - } - } - - [Test] - public async Task RPCObjectMap() { - Dictionary result = await LCCloud.RPC("getTodoMap") as Dictionary; - foreach (KeyValuePair kv in result) { - LCObject todo = kv.Value as LCObject; - TestContext.WriteLine(todo.ObjectId); + public async Task GetObjectMap() { + object response = await LCCloud.RPC("getObjectMap"); + Dictionary dict = response as Dictionary; + TestContext.WriteLine(dict.Count); + Assert.Greater(dict.Count, 0); + foreach (KeyValuePair kv in dict) { + LCObject obj = kv.Value as LCObject; + Assert.AreEqual(kv.Key, obj.ObjectId); } } } diff --git a/Storage/Storage.Test/ExceptionTest.cs b/Storage/Storage.Test/ExceptionTest.cs index bcd80c0..293fa07 100644 --- a/Storage/Storage.Test/ExceptionTest.cs +++ b/Storage/Storage.Test/ExceptionTest.cs @@ -2,7 +2,7 @@ using LeanCloud; namespace Storage.Test { - public class ExceptionTest { + public class ExceptionTest : BaseTest { [Test] public void LeanCloudException() { try { diff --git a/Storage/Storage.Test/FileTest.cs b/Storage/Storage.Test/FileTest.cs index 8e7c9de..3ddce19 100644 --- a/Storage/Storage.Test/FileTest.cs +++ b/Storage/Storage.Test/FileTest.cs @@ -6,39 +6,33 @@ using LeanCloud; using LeanCloud.Storage; namespace Storage.Test { - public class FileTest { + public class FileTest : BaseTest { static readonly string AvatarFilePath = "../../../../../assets/hello.png"; static readonly string APKFilePath = "../../../../../assets/test.apk"; - [SetUp] - public void SetUp() { - Utils.SetUp(); - } + private LCFile avatar; - [TearDown] - public void TearDown() { - Utils.TearDown(); + [Test] + [Order(0)] + public async Task SaveFromPath() { + avatar = new LCFile("avatar", AvatarFilePath); + await avatar.Save((count, total) => { + TestContext.WriteLine($"progress: {count}/{total}"); + }); + TestContext.WriteLine(avatar.ObjectId); + Assert.NotNull(avatar.ObjectId); } [Test] + [Order(1)] public async Task QueryFile() { LCQuery query = LCFile.GetQuery(); - LCFile file = await query.Get("5e0dbfa0562071008e21c142"); + LCFile file = await query.Get(avatar.ObjectId); Assert.NotNull(file.Url); TestContext.WriteLine(file.Url); TestContext.WriteLine(file.GetThumbnailUrl(32, 32)); } - [Test] - public async Task SaveFromPath() { - LCFile file = new LCFile("avatar", AvatarFilePath); - await file.Save((count, total) => { - TestContext.WriteLine($"progress: {count}/{total}"); - }); - TestContext.WriteLine(file.ObjectId); - Assert.NotNull(file.ObjectId); - } - [Test] public async Task SaveFromMemory() { string text = "hello, world"; @@ -69,17 +63,6 @@ namespace Storage.Test { Assert.NotNull(file.ObjectId); } - [Test] - public async Task AWS() { - LCApplication.Initialize("UlCpyvLm8aMzQsW6KnP6W3Wt-MdYXbMMI", "PyCTYoNoxCVoKKg394PBeS4r"); - LCFile file = new LCFile("avatar", AvatarFilePath); - await file.Save((count, total) => { - TestContext.WriteLine($"progress: {count}/{total}"); - }); - TestContext.WriteLine(file.ObjectId); - Assert.NotNull(file.ObjectId); - } - [Test] public async Task FileACL() { LCUser user = await LCUser.LoginAnonymously(); @@ -101,5 +84,16 @@ namespace Storage.Test { Assert.AreEqual(e.Code, 403); } } + + //[Test] + //public async Task AWS() { + // LCApplication.Initialize("UlCpyvLm8aMzQsW6KnP6W3Wt-MdYXbMMI", "PyCTYoNoxCVoKKg394PBeS4r"); + // LCFile file = new LCFile("avatar", AvatarFilePath); + // await file.Save((count, total) => { + // TestContext.WriteLine($"progress: {count}/{total}"); + // }); + // TestContext.WriteLine(file.ObjectId); + // Assert.NotNull(file.ObjectId); + //} } } diff --git a/Storage/Storage.Test/FriendTest.cs b/Storage/Storage.Test/FriendTest.cs index dc38669..94b97f5 100644 --- a/Storage/Storage.Test/FriendTest.cs +++ b/Storage/Storage.Test/FriendTest.cs @@ -8,7 +8,7 @@ using LeanCloud; using LeanCloud.Storage; namespace Storage.Test { - public class FriendTest { + public class FriendTest : BaseTest { async Task SignUp() { LCUser user = new LCUser { Username = Guid.NewGuid().ToString(), @@ -36,16 +36,6 @@ namespace Storage.Test { private LCUser user1; private LCUser user2; - [SetUp] - public void SetUp() { - Utils.SetUp(); - } - - [TearDown] - public void TearDown() { - Utils.TearDown(); - } - [Test] [Order(0)] public async Task Init() { diff --git a/Storage/Storage.Test/GeoTest.cs b/Storage/Storage.Test/GeoTest.cs index c2e413f..5efe217 100644 --- a/Storage/Storage.Test/GeoTest.cs +++ b/Storage/Storage.Test/GeoTest.cs @@ -2,7 +2,7 @@ using LeanCloud.Storage; namespace Storage.Test { - public class GeoTest { + public class GeoTest : BaseTest { [Test] public void Calculate() { LCGeoPoint p1 = new LCGeoPoint(20.0059, 110.3665); diff --git a/Storage/Storage.Test/LeaderboardTest.cs b/Storage/Storage.Test/LeaderboardTest.cs index 8d258a6..09ced33 100644 --- a/Storage/Storage.Test/LeaderboardTest.cs +++ b/Storage/Storage.Test/LeaderboardTest.cs @@ -9,21 +9,21 @@ using LeanCloud.Storage; using static NUnit.Framework.TestContext; namespace Storage.Test { - public class LeaderboardTest { + public class LeaderboardTest : BaseTest { private string leaderboardName; [SetUp] - public void SetUp() { - LCLogger.LogDelegate += Utils.Print; - LCApplication.Initialize(Utils.AppId, Utils.AppKey, Utils.AppServer, Utils.MasterKey); + public override void SetUp() { + base.SetUp(); + LCApplication.Initialize(AppId, AppKey, AppServer, MasterKey); LCApplication.UseMasterKey = true; leaderboardName = $"Leaderboard_{DateTimeOffset.Now.DayOfYear}"; } [TearDown] - public void TearDown() { + public override void TearDown() { + base.TearDown(); LCApplication.UseMasterKey = false; - Utils.TearDown(); } [Test] diff --git a/Storage/Storage.Test/ObjectTest.cs b/Storage/Storage.Test/ObjectTest.cs index 773def9..471857f 100644 --- a/Storage/Storage.Test/ObjectTest.cs +++ b/Storage/Storage.Test/ObjectTest.cs @@ -2,26 +2,17 @@ using NUnit.Framework; using System; using System.Threading.Tasks; using System.Collections.Generic; +using System.Linq; using LeanCloud; using LeanCloud.Storage; using static NUnit.Framework.TestContext; namespace Storage.Test { - public class ObjectTest { - [SetUp] - public void SetUp() { - Utils.SetUp(); - } - - [TearDown] - public void TearDown() { - Utils.TearDown(); - } - + public class ObjectTest : BaseTest { [Test] public async Task CreateObject() { - LCObject @object = new LCObject("Hello"); + Hello @object = new Hello(); @object["intValue"] = 123; @object["boolValue"] = true; @object["stringValue"] = "hello, world"; @@ -38,19 +29,19 @@ namespace Storage.Test { @object["pointerList"] = new List { new LCObject("World"), nestedObj }; await @object.Save(); - TestContext.WriteLine(@object.ClassName); - TestContext.WriteLine(@object.ObjectId); - TestContext.WriteLine(@object.CreatedAt); - TestContext.WriteLine(@object.UpdatedAt); - TestContext.WriteLine(@object["intValue"]); - TestContext.WriteLine(@object["boolValue"]); - TestContext.WriteLine(@object["stringValue"]); - TestContext.WriteLine(@object["objectValue"]); - TestContext.WriteLine(@object["time"]); + WriteLine(@object.ClassName); + WriteLine(@object.ObjectId); + WriteLine(@object.CreatedAt); + WriteLine(@object.UpdatedAt); + WriteLine(@object["intValue"]); + WriteLine(@object["boolValue"]); + WriteLine(@object["stringValue"]); + WriteLine(@object["objectValue"]); + WriteLine(@object["time"]); Assert.AreEqual(nestedObj, @object["objectValue"]); - TestContext.WriteLine(nestedObj.ClassName); - TestContext.WriteLine(nestedObj.ObjectId); + WriteLine(nestedObj.ClassName); + WriteLine(nestedObj.ObjectId); Assert.NotNull(@object.ObjectId); Assert.NotNull(@object.ClassName); @@ -77,8 +68,9 @@ namespace Storage.Test { public async Task SaveAll() { List list = new List(); for (int i = 0; i < 5; i++) { - LCObject world = new LCObject("World"); - world["content"] = $"word_{i}"; + World world = new World { + Content = $"word_{i}" + }; list.Add(world); } await LCObject.SaveAll(list); @@ -89,18 +81,18 @@ namespace Storage.Test { [Test] public async Task Delete() { - LCObject world = new LCObject("World"); + World world = new World(); await world.Save(); await world.Delete(); } [Test] public async Task DeleteAll() { - List list = new List { - new LCObject("World"), - new LCObject("World"), - new LCObject("World"), - new LCObject("World") + List list = new List { + new World(), + new World(), + new World(), + new World(), }; await LCObject.SaveAll(list); await LCObject.DeleteAll(list); @@ -108,47 +100,53 @@ namespace Storage.Test { [Test] public async Task Fetch() { - LCObject hello = LCObject.CreateWithoutData("Hello", "5e14392743c257006fb769d5"); - await hello.Fetch(includes: new List { "objectValue" }); - LCObject world = hello["objectValue"] as LCObject; - TestContext.WriteLine(world["content"]); - Assert.AreEqual(world["content"], "7788"); + Hello hello = new Hello { + World = new World { + Content = "7788" + } + }; + await hello.Save(); + + hello = LCObject.CreateWithoutData("Hello", hello.ObjectId) as Hello; + await hello.Fetch(includes: new string[] { "objectValue" }); + World world = hello.World; + WriteLine(world.Content); + Assert.AreEqual(world.Content, "7788"); } [Test] public async Task SaveWithOption() { - LCObject account = new LCObject("Account"); - account["balance"] = 10; + Account account = new Account { + Balance = 10 + }; await account.Save(); - account["balance"] = 1000; LCQuery q = new LCQuery("Account"); q.WhereGreaterThan("balance", 100); try { await account.Save(fetchWhenSave: true, query: q); } catch(LCException e) { - TestContext.WriteLine($"{e.Code} : {e.Message}"); + WriteLine($"{e.Code} : {e.Message}"); Assert.AreEqual(e.Code, 305); } } [Test] public async Task Unset() { - LCObject hello = new LCObject("Hello"); - hello["content"] = "hello, world"; + Hello hello = new Hello { + World = new World() + }; await hello.Save(); - TestContext.WriteLine(hello["content"]); - Assert.AreEqual(hello["content"], "hello, world"); + Assert.NotNull(hello.World); - hello.Unset("content"); + hello.Unset("objectValue"); await hello.Save(); - TestContext.WriteLine(hello["content"]); - Assert.IsNull(hello["content"]); + Assert.IsNull(hello.World); } [Test] public async Task OperateNullProperty() { - LCObject obj = new LCObject("Hello"); + Hello obj = new Hello(); obj.Increment("intValue", 123); obj.Increment("intValue", 321); obj.Add("intList", 1); @@ -168,21 +166,19 @@ namespace Storage.Test { [Test] public async Task FetchAll() { - List list = new List { - LCObject.CreateWithoutData("Hello", "5e8fe86938ed12000870ae82"), - LCObject.CreateWithoutData("Hello", "5e8fe867158a7a0006be0feb"), - LCObject.CreateWithoutData("Hello", "5e8fe84e5c385800081a1d64"), - }; + LCQuery query = new LCQuery("Hello"); + IEnumerable ids = (await query.Find()).Select(obj => obj.ObjectId); + IEnumerable list = ids.Select(id => LCObject.CreateWithoutData("Hello", id)); await LCObject.FetchAll(list); - Assert.Greater(list.Count, 0); + Assert.Greater(list.Count(), 0); foreach (LCObject obj in list) { - Assert.NotNull(obj["intList"]); + Assert.NotNull(obj.CreatedAt); } } [Test] public async Task Serialization() { - LCObject obj = new LCObject("Hello"); + Hello obj = new Hello(); obj["intValue"] = 123; obj["boolValue"] = true; obj["stringValue"] = "hello, world"; diff --git a/Storage/Storage.Test/OperationTest.cs b/Storage/Storage.Test/OperationTest.cs index 7e0fde4..e34fcf9 100644 --- a/Storage/Storage.Test/OperationTest.cs +++ b/Storage/Storage.Test/OperationTest.cs @@ -4,39 +4,31 @@ using System.Threading.Tasks; using LeanCloud.Storage; namespace Storage.Test { - public class OperationTest { - [SetUp] - public void SetUp() { - Utils.SetUp(); - } - - [TearDown] - public void TearDown() { - Utils.TearDown(); - } - + public class OperationTest : BaseTest { [Test] public async Task Increment() { - LCQuery query = new LCQuery("Account"); - LCObject account = await query.Get("5e154a5143c257006fbff63f"); - TestContext.WriteLine(account["balance"]); - int balance = (int)account["balance"]; + Account account = new Account { + Balance = 10 + }; + await account.Save(); + TestContext.WriteLine(account.Balance); account.Increment("balance", 100); await account.Save(); - TestContext.WriteLine(account["balance"]); - Assert.AreEqual((int)account["balance"], balance + 100); + TestContext.WriteLine(account.Balance); + Assert.AreEqual(account.Balance, 110); } [Test] public async Task Decrement() { - LCQuery query = new LCQuery("Account"); - LCObject account = await query.Get("5e154a5143c257006fbff63f"); - TestContext.WriteLine(account["balance"]); - int balance = (int)account["balance"]; + Account account = new Account { + Balance = 100 + }; + await account.Save(); + TestContext.WriteLine(account.Balance); account.Increment("balance", -10); await account.Save(); - TestContext.WriteLine(account["balance"]); - Assert.AreEqual((int)account["balance"], balance - 10); + TestContext.WriteLine(account.Balance); + Assert.AreEqual(account.Balance, 90); } [Test] diff --git a/Storage/Storage.Test/QueryTest.cs b/Storage/Storage.Test/QueryTest.cs index cadef58..f87b321 100644 --- a/Storage/Storage.Test/QueryTest.cs +++ b/Storage/Storage.Test/QueryTest.cs @@ -5,17 +5,7 @@ using System.Threading.Tasks; using LeanCloud.Storage; namespace Storage.Test { - public class QueryTest { - [SetUp] - public void SetUp() { - Utils.SetUp(); - } - - [TearDown] - public void TearDown() { - Utils.TearDown(); - } - + public class QueryTest : BaseTest { [Test] public async Task BaseQuery() { LCQuery query = new LCQuery("Hello"); @@ -80,19 +70,30 @@ namespace Storage.Test { [Test] public async Task Include() { + Hello hello = new Hello { + World = new World { + Content = "7788" + } + }; + await hello.Save(); + LCQuery query = new LCQuery("Hello"); query.Include("objectValue"); - LCObject hello = await query.Get("5e0d55aedd3c13006a53cd87"); - LCObject world = hello["objectValue"] as LCObject; - TestContext.WriteLine(world["content"]); - Assert.AreEqual(world["content"], "7788"); + Hello queryHello = (await query.Get(hello.ObjectId)) as Hello; + World world = queryHello.World; + TestContext.WriteLine(world.Content); + Assert.AreEqual(world.Content, "7788"); } [Test] public async Task Get() { + Account account = new Account { + Balance = 1024 + }; + await account.Save(); LCQuery query = new LCQuery("Account"); - LCObject account = await query.Get("5e0d9f7fd4b56c008e5d048a"); - Assert.AreEqual(account["balance"], 400); + Account newAccount = (await query.Get(account.ObjectId)) as Account; + Assert.AreEqual(newAccount.Balance, 1024); } [Test] @@ -143,13 +144,19 @@ namespace Storage.Test { [Test] public async Task WhereObjectEquals() { + World world = new World(); + Hello hello = new Hello { + World = world + }; + await hello.Save(); + LCQuery worldQuery = new LCQuery("World"); - LCObject world = await worldQuery.Get("5e0d55ae21460d006a1ec931"); + LCObject queryWorld = await worldQuery.Get(world.ObjectId); LCQuery helloQuery = new LCQuery("Hello"); - helloQuery.WhereEqualTo("objectValue", world); - LCObject hello = await helloQuery.First(); - TestContext.WriteLine(hello.ObjectId); - Assert.AreEqual(hello.ObjectId, "5e0d55aedd3c13006a53cd87"); + helloQuery.WhereEqualTo("objectValue", queryWorld); + LCObject queryHello = await helloQuery.First(); + TestContext.WriteLine(queryHello.ObjectId); + Assert.AreEqual(queryHello.ObjectId, hello.ObjectId); } [Test] diff --git a/Storage/Storage.Test/RelationTest.cs b/Storage/Storage.Test/RelationTest.cs index 5f80c51..0e76a10 100644 --- a/Storage/Storage.Test/RelationTest.cs +++ b/Storage/Storage.Test/RelationTest.cs @@ -4,23 +4,18 @@ using System.Collections.ObjectModel; using LeanCloud.Storage; namespace Storage.Test { - public class RelationTest { - [SetUp] - public void SetUp() { - Utils.SetUp(); - } - - [TearDown] - public void TearDown() { - Utils.TearDown(); - } + public class RelationTest : BaseTest { + private LCObject parent; + private LCObject c1; + private LCObject c2; [Test] + [Order(0)] public async Task AddAndRemove() { - LCObject parent = new LCObject("Parent"); - LCObject c1 = new LCObject("Child"); + parent = new LCObject("Parent"); + c1 = new LCObject("Child"); parent.AddRelation("children", c1); - LCObject c2 = new LCObject("Child"); + c2 = new LCObject("Child"); parent.AddRelation("children", c2); await parent.Save(); @@ -40,10 +35,11 @@ namespace Storage.Test { } [Test] + [Order(1)] public async Task Query() { LCQuery query = new LCQuery("Parent"); - LCObject parent = await query.Get("5e13112021b47e0070ed0922"); - LCRelation relation = parent["children"] as LCRelation; + LCObject queryParent = await query.Get(parent.ObjectId); + LCRelation relation = queryParent["children"] as LCRelation; TestContext.WriteLine(relation.Key); TestContext.WriteLine(relation.Parent); diff --git a/Storage/Storage.Test/RoleTest.cs b/Storage/Storage.Test/RoleTest.cs index 9dec68e..03f34a8 100644 --- a/Storage/Storage.Test/RoleTest.cs +++ b/Storage/Storage.Test/RoleTest.cs @@ -5,23 +5,13 @@ using System.Threading.Tasks; using LeanCloud.Storage; namespace Storage.Test { - [TestFixture] - public class RoleTest { - [SetUp] - public void SetUp() { - Utils.SetUp(); - } - - [TearDown] - public void TearDown() { - Utils.TearDown(); - } - + public class RoleTest : BaseTest { [Test] public async Task NewRole() { - LCUser currentUser = await LCUser.Login("game", "play"); - LCACL acl = new LCACL(); - acl.PublicReadAccess = true; + LCUser currentUser = await LCUser.Login(TestPhone, TestPhone); + LCACL acl = new LCACL { + PublicReadAccess = true + }; acl.SetUserWriteAccess(currentUser, true); string name = $"role_{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}"; LCRole role = LCRole.Create(name, acl); diff --git a/Storage/Storage.Test/SMSTest.cs b/Storage/Storage.Test/SMSTest.cs index 1ce6528..a864583 100644 --- a/Storage/Storage.Test/SMSTest.cs +++ b/Storage/Storage.Test/SMSTest.cs @@ -4,20 +4,10 @@ using System.Threading.Tasks; using LeanCloud.Storage; namespace Storage.Test { - public class SMSTest { - [SetUp] - public void SetUp() { - Utils.SetUp(); - } - - [TearDown] - public void TearDown() { - Utils.TearDown(); - } - + public class SMSTest : BaseTest { //[Test] public async Task RequestSMS() { - await LCSMSClient.RequestSMSCode("15101006007", + await LCSMSClient.RequestSMSCode(TestPhone, template: "test_template", signature: "flutter-test", variables: new Dictionary { @@ -27,12 +17,12 @@ namespace Storage.Test { //[Test] public async Task RequestVoice() { - await LCSMSClient.RequestVoiceCode("+8615101006007"); + await LCSMSClient.RequestVoiceCode(TestPhone); } - //[Test] + [Test] public async Task Verify() { - await LCSMSClient.VerifyMobilePhone("15101006007", ""); + await LCSMSClient.VerifyMobilePhone(TestPhone, TestSMSCode); } } } diff --git a/Storage/Storage.Test/StatusTest.cs b/Storage/Storage.Test/StatusTest.cs index 8870ede..c348357 100644 --- a/Storage/Storage.Test/StatusTest.cs +++ b/Storage/Storage.Test/StatusTest.cs @@ -6,21 +6,11 @@ using System.Collections.ObjectModel; using LeanCloud.Storage; namespace Storage.Test { - public class StatusTest { + public class StatusTest : BaseTest { private LCUser user1; private LCUser user2; private LCUser user3; - [SetUp] - public void SetUp() { - Utils.SetUp(); - } - - [TearDown] - public void TearDown() { - Utils.TearDown(); - } - [Test] [Order(0)] public async Task Init() { diff --git a/Storage/Storage.Test/SubClassTest.cs b/Storage/Storage.Test/SubClassTest.cs index 8c79235..6a48082 100644 --- a/Storage/Storage.Test/SubClassTest.cs +++ b/Storage/Storage.Test/SubClassTest.cs @@ -1,55 +1,12 @@ using NUnit.Framework; using System.Threading.Tasks; using System.Collections.ObjectModel; -using LeanCloud; using LeanCloud.Storage; 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() { - Utils.SetUp(); - } - - [TearDown] - public void TearDown() { - Utils.TearDown(); - } - + public class SubClassTest : BaseTest { [Test] public async Task Create() { - LCObject.RegisterSubclass("Account", () => new Account()); Account account = new Account(); account.Balance = 1000; await account.Save(); @@ -59,7 +16,6 @@ namespace Storage.Test { [Test] public async Task Query() { - LCObject.RegisterSubclass("Account", () => new Account()); LCQuery query = new LCQuery("Account"); query.WhereGreaterThan("balance", 500); ReadOnlyCollection list = await query.Find(); @@ -72,29 +28,11 @@ namespace Storage.Test { [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.Test/UserTest.cs b/Storage/Storage.Test/UserTest.cs index 31fc05b..effa9e4 100644 --- a/Storage/Storage.Test/UserTest.cs +++ b/Storage/Storage.Test/UserTest.cs @@ -7,18 +7,9 @@ using LeanCloud.Storage; using LC.Newtonsoft.Json; namespace Storage.Test { - public class UserTest { - [SetUp] - public void SetUp() { - Utils.SetUp(); - } - - [TearDown] - public void TearDown() { - Utils.TearDown(); - } - + public class UserTest : BaseTest { [Test] + [Order(0)] public async Task SignUp() { LCUser user = new LCUser(); long unixTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); @@ -26,11 +17,11 @@ namespace Storage.Test { user.Password = "world"; string email = $"{unixTime}@qq.com"; user.Email = email; - Random random = new Random(); - string mobile = $"151{random.Next(10000000, 99999999)}"; - user.Mobile = mobile; + //Random random = new Random(); + //string mobile = $"151{random.Next(10000000, 99999999)}"; + //user.Mobile = mobile; await user.SignUp(); - + TestContext.WriteLine(user.Username); TestContext.WriteLine(user.Password); @@ -42,40 +33,63 @@ namespace Storage.Test { } [Test] + [Order(1)] public async Task Login() { - await LCUser.Login("hello", "world"); + try { + await LCUser.Login(TestPhone, TestPhone); + } catch (LCException e) { + if (e.Code == 211) { + LCUser user = new LCUser { + Username = TestPhone, + Password = TestPhone, + Mobile = TestPhone, + Email = GetTestEmail() + }; + await user.SignUp(); + } else { + throw e; + } + } + + await LCUser.Login(TestPhone, TestPhone); LCUser current = await LCUser.GetCurrent(); Assert.NotNull(current.ObjectId); Assert.IsFalse(current.EmailVerified); - Assert.IsFalse(!current.MobileVerified); - Assert.AreEqual(current.Mobile, "15101006007"); + Assert.IsTrue(current.MobileVerified); + Assert.AreEqual(current.Mobile, TestPhone); } [Test] + [Order(2)] public async Task LoginByEmail() { - await LCUser.LoginByEmail("171253484@qq.com", "world"); + await LCUser.LoginByEmail(GetTestEmail(), TestPhone); LCUser current = await LCUser.GetCurrent(); Assert.NotNull(current.ObjectId); } [Test] + [Order(3)] public async Task LoginBySessionToken() { + LCUser user = await LCUser.Login(TestPhone, TestPhone); + string sessionToken = user.SessionToken; await LCUser.Logout(); - string sessionToken = "luo2fpl4qij2050e7enqfz173"; + await LCUser.BecomeWithSessionToken(sessionToken); LCUser current = await LCUser.GetCurrent(); Assert.NotNull(current.ObjectId); } [Test] + [Order(4)] public async Task RelateObject() { - LCUser user = await LCUser.LoginByMobilePhoneNumber("15101006007", "world"); - LCObject account = new LCObject("Account"); - account["user"] = user; + LCUser user = await LCUser.LoginByMobilePhoneNumber(TestPhone, TestPhone); + Account account = new Account(); + account.User = user; await account.Save(); } [Test] + [Order(5)] public async Task LoginAnonymous() { LCUser user = await LCUser.LoginAnonymously(); Assert.NotNull(user.ObjectId); @@ -83,6 +97,7 @@ namespace Storage.Test { } [Test] + [Order(6)] public async Task LoginWithAuthData() { string uuid = Guid.NewGuid().ToString(); Dictionary authData = new Dictionary { @@ -109,6 +124,7 @@ namespace Storage.Test { } [Test] + [Order(7)] public async Task AssociateAuthData() { string uuid = Guid.NewGuid().ToString(); LCUser currentUser = await LCUser.Login("hello", "world"); @@ -123,12 +139,14 @@ namespace Storage.Test { } [Test] + [Order(8)] public async Task DisassociateAuthData() { LCUser currentUser = await LCUser.Login("hello", "world"); await currentUser.DisassociateWithAuthData("weixin"); } [Test] + [Order(9)] public async Task IsAuthenticated() { LCUser currentUser = await LCUser.Login("hello", "world"); bool isAuthenticated = await currentUser.IsAuthenticated(); @@ -137,6 +155,7 @@ namespace Storage.Test { } [Test] + [Order(10)] public async Task UpdatePassword() { LCUser currentUser = await LCUser.Login("hello", "world"); await currentUser.UpdatePassword("world", "newWorld"); @@ -144,6 +163,7 @@ namespace Storage.Test { } [Test] + [Order(11)] public async Task LoginWithAuthDataWithUnionId() { string uuid = Guid.NewGuid().ToString(); Dictionary authData = new Dictionary { @@ -174,8 +194,9 @@ namespace Storage.Test { } [Test] + [Order(12)] public async Task AssociateAuthDataWithUnionId() { - LCUser currentUser = await LCUser.Login("hello", "world"); + LCUser currentUser = await LCUser.Login(TestPhone, TestPhone); string uuid = Guid.NewGuid().ToString(); Dictionary authData = new Dictionary { { "expires_in", 7200 }, @@ -188,22 +209,24 @@ namespace Storage.Test { // 手动测试 - //[Test] - //public async Task LoginByMobile() { - // LCUser user = await LCUser.LoginByMobilePhoneNumber("15101006007", "112358"); - // Assert.NotNull(user.ObjectId); - //} + [Test] + [Order(13)] + public async Task LoginByMobile() { + LCUser user = await LCUser.LoginByMobilePhoneNumber(TestPhone, TestPhone); + Assert.NotNull(user.ObjectId); + } //[Test] //public async Task RequestLoginSMSCode() { // await LCUser.RequestLoginSMSCode("15101006007"); //} - //[Test] - //public async Task LoginBySMSCode() { - // LCUser user = await LCUser.LoginBySMSCode("15101006007", "194998"); - // Assert.NotNull(user.ObjectId); - //} + [Test] + [Order(14)] + public async Task LoginBySMSCode() { + LCUser user = await LCUser.LoginBySMSCode(TestPhone, TestSMSCode); + Assert.NotNull(user.ObjectId); + } //[Test] //public async Task RequestEmailVerify() { @@ -215,10 +238,11 @@ namespace Storage.Test { // await LCUser.RequestMobilePhoneVerify("15101006007"); //} - //[Test] - //public async Task VerifyMobile() { - // await LCUser.VerifyMobilePhone("15101006007", "506993"); - //} + [Test] + [Order(15)] + public async Task VerifyMobile() { + await LCUser.VerifyMobilePhone(TestPhone, TestSMSCode); + } //[Test] //public async Task RequestResetPasswordBySMSCode() { @@ -230,19 +254,21 @@ namespace Storage.Test { // await LCUser.ResetPasswordBySmsCode("15101006007", "732552", "112358"); //} - [Test] - public async Task RequestSMSCodeForUpdatingPhoneNumber() { - await LCUser.Login("hello", "world"); - await LCUser.RequestSMSCodeForUpdatingPhoneNumber("15101006007"); - } + //[Test] + //public async Task RequestSMSCodeForUpdatingPhoneNumber() { + // await LCUser.Login(TestPhone, TestPhone); + // await LCUser.RequestSMSCodeForUpdatingPhoneNumber(TestPhone); + //} [Test] + [Order(16)] public async Task VerifyCodeForUpdatingPhoneNumber() { - await LCUser.Login("hello", "world"); - await LCUser.VerifyCodeForUpdatingPhoneNumber("15101006007", "969327"); + await LCUser.Login(TestPhone, TestPhone); + await LCUser.VerifyCodeForUpdatingPhoneNumber(TestPhone, TestSMSCode); } [Test] + [Order(17)] public async Task AuthData() { string uuid = Guid.NewGuid().ToString(); Dictionary authData = new Dictionary { @@ -270,5 +296,9 @@ namespace Storage.Test { TestContext.WriteLine(JsonConvert.SerializeObject(currentUser.AuthData)); } } + + private string GetTestEmail() { + return $"{TestPhone}@leancloud.rocks"; + } } } diff --git a/Storage/Storage.Test/Utils.cs b/Storage/Storage.Test/Utils.cs deleted file mode 100644 index 8558996..0000000 --- a/Storage/Storage.Test/Utils.cs +++ /dev/null @@ -1,37 +0,0 @@ -using NUnit.Framework; -using LeanCloud; - -namespace Storage.Test { - public static class Utils { - internal const string AppId = "ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz"; - internal const string AppKey = "NUKmuRbdAhg1vrb2wexYo1jo"; - internal const string MasterKey = "pyvbNSh5jXsuFQ3C8EgnIdhw"; - internal const string AppServer = "https://ikggdre2.lc-cn-n1-shared.com"; - - internal static void Print(LCLogLevel level, string info) { - switch (level) { - case LCLogLevel.Debug: - TestContext.Out.WriteLine($"[DEBUG] {info}"); - break; - case LCLogLevel.Warn: - TestContext.Out.WriteLine($"[WARNING] {info}"); - break; - case LCLogLevel.Error: - TestContext.Out.WriteLine($"[ERROR] {info}"); - break; - default: - TestContext.Out.WriteLine(info); - break; - } - } - - internal static void SetUp() { - LCLogger.LogDelegate += Print; - LCApplication.Initialize(AppId, AppKey, AppServer); - } - - internal static void TearDown() { - LCLogger.LogDelegate -= Print; - } - } -}