forked from ravendb/ravendb
/
DynamicQueryMapping.cs
146 lines (131 loc) · 4.47 KB
/
DynamicQueryMapping.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
//-----------------------------------------------------------------------
// <copyright file="DynamicQueryMapping.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Raven.Abstractions.Data;
using Raven.Database;
using Raven.Database.Config;
using Raven.Database.Data;
using Xunit;
using Data=Raven.Database.Data;
namespace Raven.Tests.Indexes
{
public class DynamicQueryMapping
{
[Fact]
public void CanExtractTermsFromRangedQuery()
{
var mapping = Data.DynamicQueryMapping.Create(new DocumentDatabase(new RavenConfiguration{ RunInMemory = true}), "Term:[0 TO 10]",null);
Assert.Equal("Term", mapping.Items[0].From);
}
[Fact]
public void CanExtractTermsFromEqualityQuery()
{
var mapping = Data.DynamicQueryMapping.Create(new DocumentDatabase(new RavenConfiguration { RunInMemory = true }), "Term:Whatever", null);
Assert.Equal("Term", mapping.Items[0].From);
}
[Fact]
public void CanExtractMultipleTermsQuery()
{
var mapping = Data.DynamicQueryMapping.Create(new DocumentDatabase(new RavenConfiguration { RunInMemory = true }), "Term:Whatever OR Term2:[0 TO 10]", null);
Assert.Equal(2, mapping.Items.Length);
Assert.True(mapping.Items.Any(x => x.From == "Term"));
Assert.True(mapping.Items.Any(x => x.From == "Term2"));
}
[Fact]
public void CanExtractTermsFromComplexQuery()
{
var mapping = Data.DynamicQueryMapping.Create(new DocumentDatabase(new RavenConfiguration { RunInMemory = true }), "+(Term:bar Term2:baz) +Term3:foo -Term4:rob", null);
Assert.Equal(4, mapping.Items.Length);
Assert.True(mapping.Items.Any(x => x.From == "Term"));
Assert.True(mapping.Items.Any(x => x.From == "Term2"));
Assert.True(mapping.Items.Any(x => x.From == "Term3"));
Assert.True(mapping.Items.Any(x => x.From == "Term4"));
}
[Fact]
public void CanExtractMultipleNestedTermsQuery()
{
var mapping = Data.DynamicQueryMapping.Create(new DocumentDatabase(new RavenConfiguration { RunInMemory = true }), "Term:Whatever OR (Term2:Whatever AND Term3:Whatever)", null);
Assert.Equal(3, mapping.Items.Length);
Assert.True(mapping.Items.Any(x => x.From == "Term"));
Assert.True(mapping.Items.Any(x => x.From == "Term2"));
Assert.True(mapping.Items.Any(x => x.From == "Term3"));
}
[Fact]
public void CreateDefinitionSupportsSimpleProperties()
{
Data.DynamicQueryMapping mapping = new Data.DynamicQueryMapping()
{
Items = new Data.DynamicQueryMappingItem[]{
new Data.DynamicQueryMappingItem(){
From = "Name",
To = "Name"
}
}
};
var definition = mapping.CreateIndexDefinition();
Assert.Equal("from doc in docs\r\nselect new { Name = doc.Name }", definition.Map);
}
[Fact]
public void CreateDefinitionSupportsArrayProperties()
{
Data.DynamicQueryMapping mapping = new Data.DynamicQueryMapping()
{
Items = new Data.DynamicQueryMappingItem[]{
new Data.DynamicQueryMappingItem(){
From = "Tags,Name",
To = "docTagsName"
}
}
};
var definition = mapping.CreateIndexDefinition();
Assert.Equal("from doc in docs\r\nfrom docTagsItem in ((IEnumerable<dynamic>)doc.Tags).DefaultIfEmpty()\r\nselect new { docTagsName = docTagsItem.Name }", definition.Map);
}
[Fact]
public void CreateDefinitionSupportsNestedProperties()
{
Data.DynamicQueryMapping mapping = new Data.DynamicQueryMapping()
{
Items = new Data.DynamicQueryMappingItem[]{
new Data.DynamicQueryMappingItem(){
From = "User.Name",
To = "UserName"
}
}
};
var definition = mapping.CreateIndexDefinition();
Assert.Equal("from doc in docs\r\nselect new { UserName = doc.User.Name }", definition.Map);
}
[Fact]
public void CreateMapReduceIndex()
{
var mapping = new Data.DynamicQueryMapping()
{
AggregationOperation = AggregationOperation.Count,
Items = new[]{
new DynamicQueryMappingItem(){
From = "User.Name",
To = "UserName"
}
}
};
var definition = mapping.CreateIndexDefinition();
Assert.Equal(@"from doc in docs
select new { UserName = doc.User.Name, Count = 1 }", definition.Map);
Assert.Equal(@"from result in results
group result by result.UserName
into g
select new
{
UserName = g.Key,
Count = g.Sum(x=>x.Count)
}
", definition.Reduce);
}
}
}