feat: friend test
parent
0a225434c8
commit
cf87916b8c
|
@ -0,0 +1,103 @@
|
|||
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<LCUser> SignUp() {
|
||||
LCUser user = new LCUser {
|
||||
Username = Guid.NewGuid().ToString(),
|
||||
Password = "world"
|
||||
};
|
||||
return await user.SignUp();
|
||||
}
|
||||
|
||||
async Task<LCFriendshipRequest> GetRequest() {
|
||||
LCUser user = await LCUser.GetCurrent();
|
||||
LCQuery<LCFriendshipRequest> query = new LCQuery<LCFriendshipRequest>("_FriendshipRequest")
|
||||
.WhereEqualTo("friend", user)
|
||||
.WhereEqualTo("status", "pending");
|
||||
return await query.First();
|
||||
}
|
||||
|
||||
async Task<ReadOnlyCollection<LCObject>> GetFriends() {
|
||||
LCUser user = await LCUser.GetCurrent();
|
||||
LCQuery<LCObject> query = new LCQuery<LCObject>("_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<string, object> attrs = new Dictionary<string, object> {
|
||||
{ "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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using LeanCloud;
|
||||
using LeanCloud.Storage;
|
||||
|
||||
namespace Storage.Test {
|
||||
public class StatusTest {
|
||||
private LCUser user1;
|
||||
private LCUser user2;
|
||||
private LCUser user3;
|
||||
|
||||
[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 = new LCUser {
|
||||
Username = Guid.NewGuid().ToString(),
|
||||
Password = "world"
|
||||
};
|
||||
await user1.SignUp();
|
||||
|
||||
user2 = new LCUser {
|
||||
Username = Guid.NewGuid().ToString(),
|
||||
Password = "world"
|
||||
};
|
||||
await user2.SignUp();
|
||||
|
||||
user3 = new LCUser {
|
||||
Username = Guid.NewGuid().ToString(),
|
||||
Password = "world"
|
||||
};
|
||||
await user3.SignUp();
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Order(1)]
|
||||
public async Task Follow() {
|
||||
await LCUser.BecomeWithSessionToken(user2.SessionToken);
|
||||
Dictionary<string, object> attrs = new Dictionary<string, object> {
|
||||
{ "score", 100 }
|
||||
};
|
||||
await user2.Follow(user1.ObjectId, attrs);
|
||||
|
||||
await LCUser.BecomeWithSessionToken(user3.SessionToken);
|
||||
await user3.Follow(user2.ObjectId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Order(2)]
|
||||
public async Task QueryFollowersAndFollowees() {
|
||||
await LCUser.BecomeWithSessionToken(user2.SessionToken);
|
||||
|
||||
LCQuery<LCObject> query = user2.FolloweeQuery();
|
||||
ReadOnlyCollection<LCObject> results = await query.Find();
|
||||
Assert.Greater(results.Count, 0);
|
||||
foreach (LCObject item in results) {
|
||||
Assert.IsTrue(item["followee"] is LCObject);
|
||||
Assert.AreEqual(user1.ObjectId, (item["followee"] as LCObject).ObjectId);
|
||||
}
|
||||
|
||||
query = user2.FollowerQuery();
|
||||
results = await query.Find();
|
||||
Assert.Greater(results.Count, 0);
|
||||
foreach (LCObject item in results) {
|
||||
Assert.IsTrue(item["follower"] is LCObject);
|
||||
Assert.AreEqual(user3.ObjectId, (item["follower"] as LCObject).ObjectId);
|
||||
}
|
||||
|
||||
LCFollowersAndFollowees followersAndFollowees = await user2.GetFollowersAndFollowees(true, true, true);
|
||||
Assert.AreEqual(followersAndFollowees.FollowersCount, 1);
|
||||
Assert.AreEqual(followersAndFollowees.FolloweesCount, 1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Order(5)]
|
||||
public async Task Unfollow() {
|
||||
await LCUser.BecomeWithSessionToken(user2.SessionToken);
|
||||
await user2.Unfollow(user1.ObjectId);
|
||||
|
||||
await LCUser.BecomeWithSessionToken(user3.SessionToken);
|
||||
await user3.Unfollow(user1.ObjectId);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ using System.Collections.Generic;
|
|||
using System.Threading.Tasks;
|
||||
using LeanCloud;
|
||||
using LeanCloud.Storage;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Storage.Test {
|
||||
public class UserTest {
|
||||
|
@ -241,5 +242,34 @@ namespace Storage.Test {
|
|||
await LCUser.Login("hello", "world");
|
||||
await LCUser.VerifyCodeForUpdatingPhoneNumber("15101006007", "055595");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AuthData() {
|
||||
string uuid = Guid.NewGuid().ToString();
|
||||
Dictionary<string, object> authData = new Dictionary<string, object> {
|
||||
{ "expires_in", 7200 },
|
||||
{ "openid", uuid },
|
||||
{ "access_token", uuid }
|
||||
};
|
||||
LCUser currentUser = await LCUser.LoginWithAuthData(authData, "weixin");
|
||||
TestContext.WriteLine(currentUser.SessionToken);
|
||||
Assert.NotNull(currentUser.SessionToken);
|
||||
string userId = currentUser.ObjectId;
|
||||
TestContext.WriteLine($"userId: {userId}");
|
||||
TestContext.WriteLine(JsonConvert.SerializeObject(currentUser.AuthData));
|
||||
|
||||
try {
|
||||
authData = new Dictionary<string, object> {
|
||||
{ "expires_in", 7200 },
|
||||
{ "openid", uuid },
|
||||
{ "access_token", uuid }
|
||||
};
|
||||
await currentUser.AssociateAuthData(authData, "qq");
|
||||
TestContext.WriteLine(JsonConvert.SerializeObject(currentUser.AuthData));
|
||||
} catch (LCException e) {
|
||||
TestContext.WriteLine($"{e.Code} : {e.Message}");
|
||||
TestContext.WriteLine(JsonConvert.SerializeObject(currentUser.AuthData));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,6 +61,7 @@ namespace LeanCloud {
|
|||
LCObject.RegisterSubclass(LCUser.CLASS_NAME, () => new LCUser());
|
||||
LCObject.RegisterSubclass(LCRole.CLASS_NAME, () => new LCRole());
|
||||
LCObject.RegisterSubclass(LCFile.CLASS_NAME, () => new LCFile());
|
||||
LCObject.RegisterSubclass(LCFriendshipRequest.CLASS_NAME, () => new LCFriendshipRequest());
|
||||
|
||||
AppRouter = new LCAppRouter(appId, server);
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ namespace LeanCloud.Storage {
|
|||
get; internal set;
|
||||
}
|
||||
|
||||
public int FolloweeCount {
|
||||
public int FolloweesCount {
|
||||
get; internal set;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -684,7 +684,7 @@ namespace LeanCloud.Storage {
|
|||
}
|
||||
if (response.TryGetValue("followees_count", out object followeesCountObj) &&
|
||||
(followeesCountObj is int followeesCount)) {
|
||||
result.FolloweeCount = followeesCount;
|
||||
result.FolloweesCount = followeesCount;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue