forked from ravendb/ravendb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIdsaTest.cs
177 lines (155 loc) · 5.83 KB
/
IdsaTest.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
// -----------------------------------------------------------------------
// <copyright file="IdsaTest.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Indexes;
using Raven.Client.Linq;
using Raven.Tests.Common;
using Xunit;
namespace Raven.Tests.MailingList
{
public class IdsaTest : RavenTest
{
[Fact]
public void CanGetEmptyCollection()
{
using (var store = NewDocumentStore())
{
new CasinosSuspensionsIndex().Execute(store);
using (var documentSession = store.OpenSession())
{
var casino = new Casino("cities/1", "address", "name")
{
Suspensions = new List<Suspension>
{
new Suspension(DateTime.UtcNow, new List<Exemption>())
}
};
documentSession.Store(casino);
documentSession.SaveChanges();
var suspensions = documentSession.Query<CasinosSuspensionsIndex.IndexResult, CasinosSuspensionsIndex>().
Customize(x => x.WaitForNonStaleResults()).
Where(x => x.CityId == "cities/1").
OrderByDescending(x => x.DateTime).
Take(10).
ProjectFromIndexFieldsInto<CasinosSuspensionsIndex.IndexResult>().
ToList();
// note that suspensions[0].Exemptions will be null, because we don't have
// any values in the array, and we don't store empty arrays
Assert.NotEmpty(suspensions);
Assert.Null(suspensions.Single().Exemptions);
}
}
}
public class CasinosSuspensionsIndex : AbstractIndexCreationTask<Casino, CasinosSuspensionsIndex.IndexResult>
{
public class IndexResult
{
public string CityId { get; set; }
public string CasinoId { get; set; }
public string CasinoAddress { get; set; }
public string Id { get; set; }
public DateTime DateTime { get; set; }
public IList<Exemption> Exemptions { get; set; }
}
public CasinosSuspensionsIndex()
{
Map = casinos => from casino in casinos
from suspension in casino.Suspensions
select new
{
CityId = casino.CityId,
CasinoId = casino.Id,
CasinoAddress = casino.Address,
Id = suspension.Id,
DateTime = suspension.DateTime,
Exemptions = (object[]) suspension.Exemptions ?? new object[0]
};
Store(x => x.CityId, FieldStorage.Yes);
Store(x => x.CasinoId, FieldStorage.Yes);
Store(x => x.CasinoAddress, FieldStorage.Yes);
Store(x => x.Id, FieldStorage.Yes);
Store(x => x.DateTime, FieldStorage.Yes);
Store(x => x.Exemptions, FieldStorage.Yes);
}
}
public class Casino
{
public string Id { get; set; }
public DateTime AdditionDate { get; set; }
public string CityId { get; set; }
public string Address { get; set; }
public string Title { get; set; }
public CasinoStatus Status { get; set; }
public IList<Comment> Comments { get; set; }
public IList<Suspension> Suspensions { get; set; }
private Casino()
{
Status = CasinoStatus.Opened;
Comments = new List<Comment>();
Suspensions = new List<Suspension>();
}
public Casino(string cityId, string address, string name)
: this()
{
AdditionDate = DateTime.UtcNow;
CityId = cityId;
Address = address;
Title = name;
}
}
public enum CasinoStatus
{
Opened = 1,
Closed = 2
}
public class Suspension
{
public string Id { get; set; }
public DateTime DateTime { get; set; }
public IList<Exemption> Exemptions { get; set; }
public Suspension(DateTime dateTime, IList<Exemption> exemptions)
{
DateTime = dateTime;
Exemptions = exemptions;
}
}
public class Exemption
{
public ExemptionItemType ItemType { get; set; }
public long Quantity { get; set; }
public Exemption(ExemptionItemType itemType, long quantity)
{
ItemType = itemType;
Quantity = quantity;
}
}
public enum ExemptionItemType
{
Unknown = 1,
Pc = 2,
SlotMachine = 3,
Table = 4,
Terminal = 5
}
public class Comment
{
public string Id { get; set; }
public DateTime DateTime { get; set; }
public string Author { get; set; }
public string Text { get; set; }
public Comment(string author, string text)
{
DateTime = DateTime.UtcNow;
Author = author;
Text = text;
}
}
}
}