forked from ravendb/ravendb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTransformWithLoad.cs
211 lines (188 loc) · 6.53 KB
/
TransformWithLoad.cs
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Collections.Generic;
using System.Linq;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
using Raven.Tests.Common;
using Raven.Tests.Helpers;
using Xunit;
namespace Raven.Tests.MailingList
{
public class TransformWithLoad : RavenTestBase
{
private IDocumentStore store;
public TransformWithLoad()
{
store = NewDocumentStore();
new ContactTransformer().Execute(store);
new Contact_ByName().Execute(store);
using (var session = store.OpenSession())
{
var detail1 = new Detail
{
Name = "Detail 1",
};
var detail2 = new Detail
{
Name = "Detail 2"
};
session.Store(detail1);
session.Store(detail2);
session.SaveChanges();
var contact = new Contact
{
Id = "contacts/1",
Name = "Contact 1",
DetailIds = new List<string>
{
detail1.Id,
detail2.Id
}
};
session.Store(contact);
session.Advanced.GetMetadataFor(contact)["Val"] = "hello";
session.SaveChanges();
}
}
[Fact]
public void Should_get_id_when_transformer_loads_document()
{
using (var session = store.OpenSession())
{
// Act
var contactListViewModel = session.Query<Contact, Contact_ByName>().TransformWith<ContactTransformer, ContactDto>().ToList();
// Assert
foreach (var detail in contactListViewModel.SelectMany(c => c.ContactDetails))
{
Assert.NotNull(detail.Id);
}
var contactViewModel = session.Load<ContactTransformer, ContactDto>("contacts/1");
foreach (var detail in contactViewModel.ContactDetails)
{
Assert.NotNull(detail.Id);
}
}
}
[Fact]
public void LazyLoadById()
{
using (var session = store.OpenSession())
{
var contactViewModel = session.Advanced.Lazily.Load<ContactTransformer, ContactDto>("contacts/1");
var contactDto = contactViewModel.Value;
foreach (var detail in contactDto.ContactDetails)
{
Assert.NotNull(detail.Id);
}
}
}
[Fact]
public void EagerLoadById()
{
using (var session = store.OpenSession())
{
var contactDto = session.Load<ContactTransformer, ContactDto>("contacts/1");
foreach (var detail in contactDto.ContactDetails)
{
Assert.NotNull(detail.Id);
}
}
}
[Fact]
public void WithMetadata()
{
using (var session = store.OpenSession())
{
var c = session.Load<ContactTransformer, ContactDto>("contacts/1");
Assert.Equal("hello", c.MetaVal);
}
}
[Fact]
public void LazyLoadByIds()
{
using (var session = store.OpenSession())
{
var result = session.Advanced.Lazily.Load<ContactTransformer, ContactDto>(new [] { "contacts/1", "contacts/2" }).Value;
Assert.Equal(2, result.Length);
Assert.NotNull(result[0]);
Assert.Null(result[1]);
foreach (var detail in result[0].ContactDetails)
{
Assert.NotNull(detail.Id);
}
}
}
[Fact]
public void LoadByIds()
{
using (var session = store.OpenSession())
{
var result = session.Load<ContactTransformer, ContactDto>(new [] { "contacts/1", "contacts/2" });
Assert.Equal(2, result.Length);
Assert.NotNull(result[0]);
Assert.Null(result[1]);
foreach (var detail in result[0].ContactDetails)
{
Assert.NotNull(detail.Id);
}
}
}
[Fact]
public void PlainLoadByIds()
{
using (var session = store.OpenSession())
{
var result = session.Load<Contact>(new [] { "contacts/1", "contacts/2" });
Assert.Equal(2, result.Length);
Assert.NotNull(result[0]);
Assert.Null(result[1]);
}
}
public class Contact
{
public Contact()
{
DetailIds = new List<string>();
}
public string Id { get; set; }
public string Name { get; set; }
public List<string> DetailIds { get; set; }
}
public class ContactDto
{
public string ContactId { get; set; }
public string ContactName { get; set; }
public List<Detail> ContactDetails { get; set; }
public string MetaVal { get; set; }
}
public class ContactTransformer : AbstractTransformerCreationTask<Contact>
{
public ContactTransformer()
{
TransformResults = contacts => from c in contacts
select new
{
ContactId = c.Id,
ContactName = c.Name,
ContactDetails = LoadDocument<Detail>(c.DetailIds),
MetaVal = MetadataFor(c).Value<string>("Val")
};
}
}
public class Contact_ByName : AbstractIndexCreationTask<Contact>
{
public Contact_ByName()
{
Map = contacts => from c in contacts select new { c.Name };
Index(x => x.Name, FieldIndexing.Analyzed);
}
}
public class Detail
{
public string Id { get; set; }
public string Name { get; set; }
}
}
}