2019-07-30 16:47:10 +08:00
|
|
|
using NUnit.Framework;
|
2019-09-23 16:16:04 +08:00
|
|
|
using System;
|
2019-07-30 16:47:10 +08:00
|
|
|
using System.Threading.Tasks;
|
2019-09-23 16:16:04 +08:00
|
|
|
using System.Collections.Generic;
|
2021-04-29 15:58:22 +08:00
|
|
|
using System.Linq;
|
2020-04-28 17:04:46 +08:00
|
|
|
using LeanCloud;
|
2020-02-25 20:39:49 +08:00
|
|
|
using LeanCloud.Storage;
|
2019-07-30 16:47:10 +08:00
|
|
|
|
2020-05-06 12:06:35 +08:00
|
|
|
using static NUnit.Framework.TestContext;
|
|
|
|
|
2020-04-28 17:04:46 +08:00
|
|
|
namespace Storage.Test {
|
2021-04-29 15:58:22 +08:00
|
|
|
public class ObjectTest : BaseTest {
|
2019-07-30 16:47:10 +08:00
|
|
|
[Test]
|
2020-02-25 20:39:49 +08:00
|
|
|
public async Task CreateObject() {
|
2021-04-29 15:58:22 +08:00
|
|
|
Hello @object = new Hello();
|
2020-02-25 20:39:49 +08:00
|
|
|
@object["intValue"] = 123;
|
|
|
|
@object["boolValue"] = true;
|
|
|
|
@object["stringValue"] = "hello, world";
|
|
|
|
@object["time"] = DateTime.Now;
|
|
|
|
@object["intList"] = new List<int> { 1, 1, 2, 3, 5, 8 };
|
|
|
|
@object["stringMap"] = new Dictionary<string, object> {
|
|
|
|
{ "k1", 111 },
|
|
|
|
{ "k2", true },
|
|
|
|
{ "k3", "haha" }
|
2019-09-23 16:16:04 +08:00
|
|
|
};
|
2020-02-25 20:39:49 +08:00
|
|
|
LCObject nestedObj = new LCObject("World");
|
|
|
|
nestedObj["content"] = "7788";
|
|
|
|
@object["objectValue"] = nestedObj;
|
|
|
|
@object["pointerList"] = new List<object> { new LCObject("World"), nestedObj };
|
|
|
|
await @object.Save();
|
|
|
|
|
2021-04-29 15:58:22 +08:00
|
|
|
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"]);
|
2020-02-25 20:39:49 +08:00
|
|
|
|
|
|
|
Assert.AreEqual(nestedObj, @object["objectValue"]);
|
2021-04-29 15:58:22 +08:00
|
|
|
WriteLine(nestedObj.ClassName);
|
|
|
|
WriteLine(nestedObj.ObjectId);
|
2020-02-25 20:39:49 +08:00
|
|
|
|
|
|
|
Assert.NotNull(@object.ObjectId);
|
|
|
|
Assert.NotNull(@object.ClassName);
|
|
|
|
Assert.NotNull(@object.CreatedAt);
|
|
|
|
Assert.NotNull(@object.UpdatedAt);
|
|
|
|
Assert.AreEqual(@object["intValue"], 123);
|
|
|
|
Assert.AreEqual(@object["boolValue"], true);
|
|
|
|
Assert.AreEqual(@object["stringValue"], "hello, world");
|
|
|
|
|
|
|
|
Assert.NotNull(nestedObj);
|
|
|
|
Assert.NotNull(nestedObj.ClassName);
|
|
|
|
Assert.NotNull(nestedObj.ObjectId);
|
|
|
|
Assert.NotNull(nestedObj.CreatedAt);
|
|
|
|
Assert.NotNull(nestedObj.UpdatedAt);
|
|
|
|
|
|
|
|
List<object> pointerList = @object["pointerList"] as List<object>;
|
|
|
|
foreach (object pointerObj in pointerList) {
|
|
|
|
LCObject pointer = pointerObj as LCObject;
|
|
|
|
Assert.NotNull(pointer.ObjectId);
|
|
|
|
}
|
2019-09-23 16:16:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-02-25 20:39:49 +08:00
|
|
|
public async Task SaveAll() {
|
|
|
|
List<LCObject> list = new List<LCObject>();
|
2019-09-23 16:16:04 +08:00
|
|
|
for (int i = 0; i < 5; i++) {
|
2021-04-29 15:58:22 +08:00
|
|
|
World world = new World {
|
|
|
|
Content = $"word_{i}"
|
|
|
|
};
|
2020-02-25 20:39:49 +08:00
|
|
|
list.Add(world);
|
2019-09-23 16:16:04 +08:00
|
|
|
}
|
2020-02-25 20:39:49 +08:00
|
|
|
await LCObject.SaveAll(list);
|
|
|
|
foreach (LCObject obj in list) {
|
|
|
|
Assert.NotNull(obj.ObjectId);
|
2019-09-23 16:16:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-02-25 20:39:49 +08:00
|
|
|
public async Task Delete() {
|
2021-04-29 15:58:22 +08:00
|
|
|
World world = new World();
|
2020-02-25 20:39:49 +08:00
|
|
|
await world.Save();
|
|
|
|
await world.Delete();
|
2019-09-23 16:16:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-02-25 20:39:49 +08:00
|
|
|
public async Task DeleteAll() {
|
2021-04-29 15:58:22 +08:00
|
|
|
List<World> list = new List<World> {
|
|
|
|
new World(),
|
|
|
|
new World(),
|
|
|
|
new World(),
|
|
|
|
new World(),
|
2019-09-23 16:16:04 +08:00
|
|
|
};
|
2020-02-25 20:39:49 +08:00
|
|
|
await LCObject.SaveAll(list);
|
|
|
|
await LCObject.DeleteAll(list);
|
2019-09-23 16:16:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-02-25 20:39:49 +08:00
|
|
|
public async Task Fetch() {
|
2021-04-29 15:58:22 +08:00
|
|
|
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");
|
2019-09-23 16:16:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-02-25 20:39:49 +08:00
|
|
|
public async Task SaveWithOption() {
|
2021-04-29 15:58:22 +08:00
|
|
|
Account account = new Account {
|
|
|
|
Balance = 10
|
|
|
|
};
|
2020-02-25 20:39:49 +08:00
|
|
|
await account.Save();
|
2019-09-23 16:16:04 +08:00
|
|
|
|
2020-02-25 20:39:49 +08:00
|
|
|
LCQuery<LCObject> q = new LCQuery<LCObject>("Account");
|
|
|
|
q.WhereGreaterThan("balance", 100);
|
2019-09-23 16:16:04 +08:00
|
|
|
try {
|
2020-02-25 20:39:49 +08:00
|
|
|
await account.Save(fetchWhenSave: true, query: q);
|
|
|
|
} catch(LCException e) {
|
2021-04-29 15:58:22 +08:00
|
|
|
WriteLine($"{e.Code} : {e.Message}");
|
2020-02-25 20:39:49 +08:00
|
|
|
Assert.AreEqual(e.Code, 305);
|
2019-09-23 16:16:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-02-25 20:39:49 +08:00
|
|
|
public async Task Unset() {
|
2021-04-29 15:58:22 +08:00
|
|
|
Hello hello = new Hello {
|
|
|
|
World = new World()
|
|
|
|
};
|
2020-02-25 20:39:49 +08:00
|
|
|
await hello.Save();
|
2021-04-29 15:58:22 +08:00
|
|
|
Assert.NotNull(hello.World);
|
2019-12-05 16:06:54 +08:00
|
|
|
|
2021-04-29 15:58:22 +08:00
|
|
|
hello.Unset("objectValue");
|
2020-02-25 20:39:49 +08:00
|
|
|
await hello.Save();
|
2021-04-29 15:58:22 +08:00
|
|
|
Assert.IsNull(hello.World);
|
2019-12-06 17:02:10 +08:00
|
|
|
}
|
2020-05-06 12:06:35 +08:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public async Task OperateNullProperty() {
|
2021-04-29 15:58:22 +08:00
|
|
|
Hello obj = new Hello();
|
2020-05-06 12:06:35 +08:00
|
|
|
obj.Increment("intValue", 123);
|
|
|
|
obj.Increment("intValue", 321);
|
|
|
|
obj.Add("intList", 1);
|
|
|
|
obj.Add("intList", 2);
|
|
|
|
obj.Add("intList", 3);
|
|
|
|
await obj.Save();
|
|
|
|
|
|
|
|
WriteLine(obj["intValue"]);
|
|
|
|
Assert.AreEqual(obj["intValue"], 444);
|
|
|
|
List<object> intList = obj["intList"] as List<object>;
|
|
|
|
WriteLine(intList.Count);
|
|
|
|
Assert.AreEqual(intList.Count, 3);
|
|
|
|
Assert.AreEqual(intList[0], 1);
|
|
|
|
Assert.AreEqual(intList[1], 2);
|
|
|
|
Assert.AreEqual(intList[2], 3);
|
|
|
|
}
|
2020-05-06 12:38:44 +08:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public async Task FetchAll() {
|
2021-04-29 15:58:22 +08:00
|
|
|
LCQuery<LCObject> query = new LCQuery<LCObject>("Hello");
|
|
|
|
IEnumerable<string> ids = (await query.Find()).Select(obj => obj.ObjectId);
|
|
|
|
IEnumerable<LCObject> list = ids.Select(id => LCObject.CreateWithoutData("Hello", id));
|
2020-05-06 12:38:44 +08:00
|
|
|
await LCObject.FetchAll(list);
|
2021-04-29 15:58:22 +08:00
|
|
|
Assert.Greater(list.Count(), 0);
|
2020-05-06 12:38:44 +08:00
|
|
|
foreach (LCObject obj in list) {
|
2021-04-29 15:58:22 +08:00
|
|
|
Assert.NotNull(obj.CreatedAt);
|
2020-05-06 12:38:44 +08:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 14:07:35 +08:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public async Task Serialization() {
|
2021-04-29 15:58:22 +08:00
|
|
|
Hello obj = new Hello();
|
2020-05-06 14:07:35 +08:00
|
|
|
obj["intValue"] = 123;
|
|
|
|
obj["boolValue"] = true;
|
|
|
|
obj["stringValue"] = "hello, world";
|
|
|
|
obj["time"] = DateTime.Now;
|
|
|
|
obj["intList"] = new List<int> { 1, 1, 2, 3, 5, 8 };
|
|
|
|
obj["stringMap"] = new Dictionary<string, object> {
|
|
|
|
{ "k1", 111 },
|
|
|
|
{ "k2", true },
|
|
|
|
{ "k3", "haha" }
|
|
|
|
};
|
|
|
|
LCObject nestedObj = new LCObject("World");
|
|
|
|
nestedObj["content"] = "7788";
|
|
|
|
obj["objectValue"] = nestedObj;
|
|
|
|
obj["pointerList"] = new List<object> {
|
|
|
|
new LCObject("World"),
|
|
|
|
nestedObj
|
|
|
|
};
|
|
|
|
await obj.Save();
|
|
|
|
|
|
|
|
string json = obj.ToString();
|
|
|
|
WriteLine(json);
|
|
|
|
LCObject newObj = LCObject.ParseObject(json);
|
|
|
|
Assert.NotNull(newObj.ObjectId);
|
|
|
|
Assert.NotNull(newObj.ClassName);
|
|
|
|
Assert.NotNull(newObj.CreatedAt);
|
|
|
|
Assert.NotNull(newObj.UpdatedAt);
|
|
|
|
Assert.AreEqual(newObj["intValue"], 123);
|
|
|
|
Assert.AreEqual(newObj["boolValue"], true);
|
|
|
|
Assert.AreEqual(newObj["stringValue"], "hello, world");
|
|
|
|
}
|
2019-07-30 16:47:10 +08:00
|
|
|
}
|
2019-12-06 17:02:10 +08:00
|
|
|
}
|