chore: unit test.

oneRain 2020-12-24 10:58:16 +08:00
parent 675afb4276
commit 9ee503ed9d
6 changed files with 82 additions and 27 deletions

View File

@ -10,15 +10,22 @@ using static NUnit.Framework.TestContext;
namespace Realtime.Test { namespace Realtime.Test {
public class Client { public class Client {
private const string USERNAME1 = "username1";
private const string PASSWORD1 = "password1";
private const string USERNAME2 = "username2";
private const string PASSWORD2 = "password2";
[SetUp] [SetUp]
public void SetUp() { public async Task SetUp() {
LCLogger.LogDelegate += Utils.Print; Utils.SetUp();
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com"); await NewUser(USERNAME1, PASSWORD1);
await NewUser(USERNAME2, PASSWORD2);
} }
[TearDown] [TearDown]
public void TearDown() { public void TearDown() {
LCLogger.LogDelegate -= Utils.Print; Utils.TearDown();
} }
[Test] [Test]
@ -34,12 +41,12 @@ namespace Realtime.Test {
[Test] [Test]
public async Task OpenAndCloseByLCUser() { public async Task OpenAndCloseByLCUser() {
LCUser user = await LCUser.Login("hello", "world"); LCUser user = await LCUser.Login(USERNAME1, PASSWORD1);
LCIMClient client = new LCIMClient(user); LCIMClient client = new LCIMClient(user);
await client.Open(); await client.Open();
LCUser game = await LCUser.Login("game", "play"); LCUser game = await LCUser.Login(USERNAME2, PASSWORD2);
LCIMClient client2 = new LCIMClient(game); LCIMClient client2 = new LCIMClient(game);
await client2.Open(); await client2.Open();
@ -134,5 +141,21 @@ namespace Realtime.Test {
await tcs.Task; await tcs.Task;
} }
private async Task NewUser(string username, string password) {
try {
await LCUser.Login(username, password);
} catch (LCException e) {
if (e.Code == 211) {
LCUser user1 = new LCUser {
Username = username,
Password = password
};
await user1.SignUp();
} else {
throw e;
}
}
}
} }
} }

View File

@ -16,8 +16,7 @@ namespace Realtime.Test {
[SetUp] [SetUp]
public async Task SetUp() { public async Task SetUp() {
LCLogger.LogDelegate += Utils.Print; Utils.SetUp();
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
c1 = new LCIMClient(Guid.NewGuid().ToString()); c1 = new LCIMClient(Guid.NewGuid().ToString());
await c1.Open(); await c1.Open();
c2 = new LCIMClient(Guid.NewGuid().ToString()); c2 = new LCIMClient(Guid.NewGuid().ToString());
@ -32,7 +31,7 @@ namespace Realtime.Test {
await c1.Close(); await c1.Close();
await c2.Close(); await c2.Close();
await lean.Close(); await lean.Close();
LCLogger.LogDelegate -= Utils.Print; Utils.TearDown();
} }
[Test] [Test]

View File

@ -1,27 +1,24 @@
using NUnit.Framework; using NUnit.Framework;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Threading.Tasks; using System.Threading.Tasks;
using LeanCloud;
using LeanCloud.Common;
using LeanCloud.Realtime; using LeanCloud.Realtime;
namespace Realtime.Test { namespace Realtime.Test {
public class ConversationQuery { public class ConversationQuery {
private string clientId = "hello123"; private readonly string clientId = "m1";
private LCIMClient client; private LCIMClient client;
[SetUp] [SetUp]
public async Task SetUp() { public async Task SetUp() {
LCLogger.LogDelegate += Utils.Print; Utils.SetUp();
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
client = new LCIMClient(clientId); client = new LCIMClient(clientId);
await client.Open(); await client.Open();
} }
[TearDown] [TearDown]
public async Task TearDown() { public async Task TearDown() {
LCLogger.LogDelegate -= Utils.Print;
await client.Close(); await client.Close();
Utils.TearDown();
} }
[Test] [Test]
@ -36,7 +33,7 @@ namespace Realtime.Test {
[Test] [Test]
public async Task QueryMemberConversation() { public async Task QueryMemberConversation() {
string memberId = "cc1"; string memberId = "m1";
LCIMConversationQuery query = new LCIMConversationQuery(client); LCIMConversationQuery query = new LCIMConversationQuery(client);
query.WhereEqualTo("m", memberId); query.WhereEqualTo("m", memberId);
ReadOnlyCollection<LCIMConversation> conversations = await query.Find(); ReadOnlyCollection<LCIMConversation> conversations = await query.Find();
@ -45,5 +42,34 @@ namespace Realtime.Test {
Assert.True(conversation.MemberIds.Contains(memberId)); Assert.True(conversation.MemberIds.Contains(memberId));
} }
} }
[Test]
public async Task QueryCompact() {
string memberId = "m1";
LCIMConversationQuery query = new LCIMConversationQuery(client)
.WhereEqualTo("m", memberId);
query.Compact = true;
ReadOnlyCollection<LCIMConversation> conversations = await query.Find();
foreach (LCIMConversation conversation in conversations) {
Assert.True(conversation.MemberIds.Count == 0);
await conversation.Fetch();
Assert.True(conversation.MemberIds.Count > 0);
}
}
[Test]
public async Task QueryWithLastMessage() {
string memberId = "m1";
LCIMConversationQuery query = new LCIMConversationQuery(client)
.WhereEqualTo("m", memberId);
query.WithLastMessageRefreshed = true;
ReadOnlyCollection<LCIMConversation> conversations = await query.Find();
foreach (LCIMConversation conversation in conversations) {
Assert.True(!string.IsNullOrEmpty(conversation.LastMessage.Id));
if (conversation.LastMessage is LCIMBinaryMessage binaryMessage) {
TestContext.WriteLine(System.Text.Encoding.UTF8.GetString(binaryMessage.Data));
}
}
}
} }
} }

View File

@ -32,8 +32,7 @@ namespace Realtime.Test {
[SetUp] [SetUp]
public async Task SetUp() { public async Task SetUp() {
LCLogger.LogDelegate += Utils.Print; Utils.SetUp();
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
m1 = new LCIMClient("m1"); m1 = new LCIMClient("m1");
m2 = new LCIMClient("m2"); m2 = new LCIMClient("m2");
await m1.Open(); await m1.Open();
@ -45,7 +44,7 @@ namespace Realtime.Test {
public async Task TearDown() { public async Task TearDown() {
await m1.Close(); await m1.Close();
await m2.Close(); await m2.Close();
LCLogger.LogDelegate -= Utils.Print; Utils.TearDown();
} }
[Test] [Test]
@ -76,17 +75,19 @@ namespace Realtime.Test {
Assert.NotNull(textMessage.Id); Assert.NotNull(textMessage.Id);
LCFile image = new LCFile("hello", "../../../../../assets/hello.png"); LCFile image = new LCFile("hello", "../../../../../assets/hello.png");
await image.Save();
LCIMImageMessage imageMessage = new LCIMImageMessage(image); LCIMImageMessage imageMessage = new LCIMImageMessage(image);
await conversation.Send(imageMessage); await conversation.Send(imageMessage);
Assert.NotNull(imageMessage.Id); Assert.NotNull(imageMessage.Id);
LCFile file = new LCFile("apk", "../../../../../assets/test.apk"); LCFile file = new LCFile("apk", "../../../../../assets/test.apk");
await file.Save();
LCIMFileMessage fileMessage = new LCIMFileMessage(file); LCIMFileMessage fileMessage = new LCIMFileMessage(file);
await conversation.Send(fileMessage); await conversation.Send(fileMessage);
Assert.NotNull(fileMessage.Id); Assert.NotNull(fileMessage.Id);
LCIMBinaryMessage binaryMessage = new LCIMBinaryMessage(System.Text.Encoding.UTF8.GetBytes("LeanCloud"));
await conversation.Send(binaryMessage);
Assert.NotNull(binaryMessage.Id);
await tcs.Task; await tcs.Task;
} }
@ -152,7 +153,7 @@ namespace Realtime.Test {
[Test] [Test]
[Order(4)] [Order(4)]
public async Task Query() { public async Task Query() {
ReadOnlyCollection<LCIMMessage> messages = await conversation.QueryMessages(); ReadOnlyCollection<LCIMMessage> messages = await conversation.QueryMessages(messageType: -6);
Assert.Greater(messages.Count, 0); Assert.Greater(messages.Count, 0);
foreach (LCIMMessage message in messages) { foreach (LCIMMessage message in messages) {
Assert.AreEqual(message.ConversationId, conversation.Id); Assert.AreEqual(message.ConversationId, conversation.Id);

View File

@ -1,7 +1,6 @@
using NUnit.Framework; using NUnit.Framework;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using LeanCloud;
using LeanCloud.Realtime; using LeanCloud.Realtime;
using LeanCloud.Realtime.Internal.Protocol; using LeanCloud.Realtime.Internal.Protocol;
@ -14,8 +13,7 @@ namespace Realtime.Test {
[SetUp] [SetUp]
public async Task SetUp() { public async Task SetUp() {
LCLogger.LogDelegate += Utils.Print; Utils.SetUp();
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
c1 = new LCIMClient(Guid.NewGuid().ToString()); c1 = new LCIMClient(Guid.NewGuid().ToString());
c2 = new LCIMClient(Guid.NewGuid().ToString()); c2 = new LCIMClient(Guid.NewGuid().ToString());
await c1.Open(); await c1.Open();
@ -28,7 +26,7 @@ namespace Realtime.Test {
public async Task TearDown() { public async Task TearDown() {
await c1.Close(); await c1.Close();
await c2.Close(); await c2.Close();
LCLogger.LogDelegate -= Utils.Print; Utils.TearDown();
} }
[Test] [Test]

View File

@ -1,10 +1,18 @@
using System; using System;
using LeanCloud; using LeanCloud;
using LeanCloud.Common;
using NUnit.Framework; using NUnit.Framework;
namespace Realtime.Test { namespace Realtime.Test {
public static class Utils { public static class Utils {
internal static void SetUp() {
LCLogger.LogDelegate += Print;
LCApplication.Initialize("3zWMOXuO9iSdnjXM942i6DdI-gzGzoHsz", "bkwiNq4Tj417eUaHlTWS5sPm", "https://3zwmoxuo.lc-cn-n1-shared.com");
}
internal static void TearDown() {
LCLogger.LogDelegate -= Print;
}
internal static void Print(LCLogLevel level, string info) { internal static void Print(LCLogLevel level, string info) {
switch (level) { switch (level) {
case LCLogLevel.Debug: case LCLogLevel.Debug: