forked from ravendb/ravendb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIndexTest.cs
165 lines (147 loc) · 5.15 KB
/
IndexTest.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
using System.Linq;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
using Raven.Tests.Common;
using Xunit;
namespace Raven.Tests.MailingList
{
public class IndexTest : RavenTest
{
[Fact]
public void FloatArrayIndexTest()
{
using (var store = NewDocumentStore())
{
store.RunInMemory = true;
store.Initialize();
store.ExecuteIndex(new RatingByCategoryIndex());
using (var session = store.OpenSession())
{
session.Store(new Book
{
Category = Categories.Fiction,
Name = "Book 1",
Ratings = new[]
{
new Rating
{
User = "User 1",
Rate = 1.5F,
},
new Rating
{
User = "User 2",
Rate = 3.5F,
}
}
});
session.Store(new Book
{
Category = Categories.Fiction,
Name = "Book 2",
Ratings = new[]
{
new Rating
{
User = "User 3",
Rate = 2.5F,
},
new Rating
{
User = "User 4",
Rate = 4.5F,
}
}
});
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var categories = session
.Query<IndexResult, RatingByCategoryIndex>()
.Customize(x => x.WaitForNonStaleResults())
.ToList();
Assert.NotNull(categories);
Assert.Equal(1, categories.Count);
var books = categories[0].Books;
Assert.NotNull(books);
Assert.Equal(2, books.Length);
foreach (var book in books)
{
Assert.True(book.MinRating > 1F);
Assert.True(book.MaxRating > book.MinRating);
Assert.False(string.IsNullOrEmpty(book.Name));
}
}
}
}
}
public class IndexResult
{
public IndexBookRating[] Books { get; set; }
public Categories Category { get; set; }
}
public class IndexBookRating
{
public float MaxRating { get; set; }
public float MinRating { get; set; }
public string Name { get; set; }
}
public class Book
{
public Categories Category { get; set; }
public string Id { get; set; }
public string Name { get; set; }
public Rating[] Ratings { get; set; }
}
public enum Categories
{
Fiction,
Reference,
}
public class Rating
{
public double Rate { get; set; }
public string User { get; set; }
}
public class RatingByCategoryIndex : AbstractMultiMapIndexCreationTask<RatingByCategoryIndex.IndexData>
{
public RatingByCategoryIndex()
{
AddMap<Book>(books => books
.Select(p => new
{
p.Name,
p.Category,
Ratings = p.Ratings.Select(x => x.Rate),
})
.Select(p => new IndexData
{
Category = p.Category,
Books = new dynamic[]
{
new
{
p.Name,
MinRating = p.Ratings.Min(),
MaxRating = p.Ratings.Max(),
}
},
}));
Reduce = results => results
.GroupBy(x => x.Category)
.Select(g => new
{
Category = g.Key,
Books = g
.SelectMany<IndexData, object>(x => x.Books)
.ToArray<object>(),
});
}
public class IndexData
{
public dynamic[] Books { get; set; }
public Categories Category { get; set; }
}
}
}