using NUnit.Framework; using System; using System.Linq; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Threading.Tasks; using LeanCloud; using LeanCloud.Storage; namespace Storage.Test { public class FriendTest { async Task SignUp() { LCUser user = new LCUser { Username = Guid.NewGuid().ToString(), Password = "world" }; return await user.SignUp(); } async Task GetRequest() { LCUser user = await LCUser.GetCurrent(); LCQuery query = new LCQuery("_FriendshipRequest") .WhereEqualTo("friend", user) .WhereEqualTo("status", "pending"); return await query.First(); } async Task> GetFriends() { LCUser user = await LCUser.GetCurrent(); LCQuery query = new LCQuery("_Followee") .WhereEqualTo("user", user) .WhereEqualTo("friendStatus", true); return await query.Find(); } private LCUser user1; private LCUser user2; [SetUp] public void SetUp() { LCLogger.LogDelegate += Utils.Print; LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com"); } [TearDown] public void TearDown() { LCLogger.LogDelegate -= Utils.Print; } [Test] [Order(0)] public async Task Init() { user1 = await SignUp(); user2 = await SignUp(); Dictionary attrs = new Dictionary { { "group", "sport" } }; await LCFriendship.Request(user1.ObjectId, attrs); await SignUp(); await LCFriendship.Request(user1.ObjectId); await LCUser.BecomeWithSessionToken(user1.SessionToken); } [Test] [Order(1)] public async Task Accept() { // 查询好友请求 LCFriendshipRequest request = await GetRequest(); // 接受 await LCFriendship.AcceptRequest(request); // 查询好友 Assert.Greater((await GetFriends()).Count, 0); } [Test] [Order(2)] public async Task Decline() { // 查询好友请求 LCFriendshipRequest request = await GetRequest(); // 拒绝 await LCFriendship.DeclineRequest(request); } [Test] [Order(3)] public async Task Attributes() { LCObject followee = (await GetFriends()).First(); followee["group"] = "friend"; await followee.Save(); } [Test] [Order(4)] public async Task Delete() { await user1.Unfollow(user2.ObjectId); // 查询好友 Assert.AreEqual((await GetFriends()).Count, 0); } } }