-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathLoadContentTests.cs
More file actions
174 lines (140 loc) · 6.42 KB
/
LoadContentTests.cs
File metadata and controls
174 lines (140 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace SenseNet.Client.Tests
{
[TestClass]
public class LoadContentTests
{
[TestMethod]
public async Task Load_NotFound()
{
// load a non-existing content: this should not throw an exception
var content = await Content.LoadAsync("/Root/ThisPathDoesNotExist");
Assert.IsNull(content);
}
[TestMethod]
public async Task LoadById()
{
var content = await Content.LoadAsync(Constants.User.AdminId);
Assert.IsNotNull(content);
Assert.AreEqual(Constants.User.AdminId, content.Id);
}
[TestMethod]
public async Task LoadReferences_Default()
{
var admins = await Content.LoadReferencesAsync(Constants.Group.AdministratorsPath, "Members");
var admin = admins.FirstOrDefault();
Assert.IsNotNull(admin);
Assert.AreEqual(Constants.User.AdminId, admin.Id);
Assert.IsNotNull(admin["DisplayName"]); // all fields should be present in the result
}
[TestMethod]
public async Task LoadReferences_WithSelect()
{
// ------------------------------------ load by path
var admins = await Content.LoadReferencesAsync(Constants.Group.AdministratorsPath, "Members", new[] { "Id", "Path" });
var admin = admins.FirstOrDefault();
Assert.IsNotNull(admin);
Assert.AreEqual(Constants.User.AdminId, admin.Id);
Assert.IsNull(admin["DisplayName"]); // because we did not select this field
// ------------------------------------ load by id
var adminsGroup = await Content.LoadAsync(Constants.Group.AdministratorsPath);
admins = await Content.LoadReferencesAsync(adminsGroup.Id, "Members", new[] { "Id", "Path" });
admin = admins.FirstOrDefault();
Assert.IsNotNull(admin);
Assert.AreEqual(Constants.User.AdminId, admin.Id);
Assert.IsNull(admin["DisplayName"]); // because we did not select this field
}
[TestMethod]
public async Task LoadReferences_DynamicProperty()
{
dynamic adminGroup = await Content.LoadAsync(new ODataRequest
{
Path = Constants.Group.AdministratorsPath,
Expand = new[] { "Members" },
Select = new[] { "Id", "Name", "Members/Id", "Members/Path", "Members/Type", "Members/CreationDate" },
SiteUrl = ServerContext.GetUrl(null)
});
IEnumerable<dynamic> members = adminGroup.Members;
Assert.IsNotNull(members);
// Use items as dynamic, without converting them to Content.
// This is OK if you do not need the Content class' functionality.
dynamic admin = members.FirstOrDefault();
Assert.IsNotNull(admin);
int id = admin.Id;
string path = admin.Path;
Assert.AreEqual(Constants.User.AdminId, id);
Assert.AreEqual(Constants.User.AdminPath, path);
}
[TestMethod]
public async Task LoadReferences_DynamicProperty_Enumerable()
{
dynamic adminGroup = await Content.LoadAsync(new ODataRequest
{
Path = Constants.Group.AdministratorsPath,
Expand = new[] { "Members" },
Select = new[] { "Id", "Name", "Members/Id", "Members/Name", "Members/Path", "Members/Type", "Members/CreationDate", "Members/Index" },
SiteUrl = ServerContext.GetUrl(null)
});
//var members = ((IEnumerable<dynamic>)adminGroup.Members).ToContentEnumerable();
//var members = adminGroup.Members.ToContentEnumerable();
var members = ContentExtensions.ToContentEnumerable(adminGroup.Members);
foreach (dynamic member in members)
{
int newIndex = member.Index + 1;
member.Index = newIndex;
// use the client Content API, this was the purpose of the ToContentEnumerable extension method
await member.SaveAsync();
// load it again from the server
dynamic tempContent = await Content.LoadAsync(member.Id);
Assert.AreEqual(newIndex, (int)tempContent.Index);
}
}
[TestMethod]
public async Task Load_PropertyAccess_DateTime()
{
var content = await Content.LoadAsync(Constants.User.AdminId);
dynamic dContent = content;
Assert.IsNotNull(content);
var date1 = ((JValue)content["CreationDate"]).Value<DateTime>();
var date2 = Convert.ToDateTime(content["CreationDate"]);
DateTime date3 = dContent.CreationDate;
var baseDate = new DateTime(2015, 1, 1);
Assert.IsTrue(date1 > baseDate);
Assert.IsTrue(date2 > baseDate);
Assert.IsTrue(date3 > baseDate);
Assert.IsTrue(date1 == date2);
Assert.IsTrue(date2 == date3);
}
[TestMethod]
public async Task Query_Simple()
{
var tasks = await Content.QueryAsync("+TypeIs:Folder",
new[] {"Id", "Name", "Path", "DisplayName", "Description"},
settings: new QuerySettings
{
EnableAutofilters = FilterStatus.Disabled,
Top = 5
});
var count = tasks.Count();
Assert.IsTrue(count > 0 && count <= 5);
}
[TestMethod]
public async Task Query_Complex()
{
var tasks = await Content.QueryAsync("+TypeIs:(File Folder) +Name:(*e* *.js) +CreationDate:>'2000-01-01'",
new[] { "Id", "Name", "Path", "DisplayName", "Description" },
settings: new QuerySettings
{
EnableAutofilters = FilterStatus.Disabled,
Top = 5
});
var count = tasks.Count();
Assert.IsTrue(count > 0 && count <= 5);
}
}
}