forked from SolrNet/SolrNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMappingManagerTests.cs
185 lines (160 loc) · 6.2 KB
/
MappingManagerTests.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
#region license
// Copyright (c) 2007-2010 Mauricio Scheffer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using System;
using System.Linq;
using MbUnit.Framework;
using SolrNet.Exceptions;
using SolrNet.Mapping;
namespace SolrNet.Tests {
[TestFixture]
public class MappingManagerTests {
[Test]
public void AddAndGet() {
var mgr = new MappingManager();
mgr.Add(typeof (Entity).GetProperty("Id"), "id");
var fields = mgr.GetFields(typeof (Entity));
Assert.AreEqual(1, fields.Count);
}
[Test]
public void No_Mapped_type_returns_empty() {
var mgr = new MappingManager();
var fields = mgr.GetFields(typeof (Entity));
Assert.AreEqual(0, fields.Count);
}
[Test]
public void Add_duplicate_property_overwrites() {
var mgr = new MappingManager();
mgr.Add(typeof (Entity).GetProperty("Id"), "id");
mgr.Add(typeof (Entity).GetProperty("Id"), "id2");
var fields = mgr.GetFields(typeof (Entity));
Assert.AreEqual(1, fields.Count);
Assert.AreEqual("id2", fields.First().Value.FieldName);
}
[Test]
public void UniqueKey_Set_and_get() {
var mgr = new MappingManager();
var property = typeof (Entity).GetProperty("Id");
mgr.Add(property, "id");
mgr.SetUniqueKey(property);
var key = mgr.GetUniqueKey(typeof (Entity));
Assert.AreEqual(property, key.Property);
Assert.AreEqual("id", key.FieldName);
}
[Test]
public void UniqueKey_Set_and_get_for_inherited_classes()
{
var mgr = new MappingManager();
var property = typeof(Entity).GetProperty("Id");
mgr.Add(property, "id");
mgr.SetUniqueKey(property);
var key = mgr.GetUniqueKey(typeof(InheritedEntity));
Assert.AreEqual(property, key.Property);
Assert.AreEqual("id", key.FieldName);
}
[Test]
[ExpectedException(typeof (ArgumentException))]
public void SetUniqueKey_without_mapping_throws() {
var mgr = new MappingManager();
var property = typeof (Entity).GetProperty("Id");
mgr.SetUniqueKey(property);
}
[Test]
public void Add_property_only() {
var mgr = new MappingManager();
var property = typeof (Entity).GetProperty("Id");
mgr.Add(property);
var fields = mgr.GetFields(typeof (Entity));
Assert.AreEqual(1, fields.Count);
Assert.AreEqual("Id", fields.First().Value.FieldName);
}
[Test]
[ExpectedException(typeof (ArgumentNullException))]
public void SetUniqueKey_doesnt_admit_null() {
var mgr = new MappingManager();
mgr.SetUniqueKey(null);
}
[Test]
[ExpectedException(typeof (ArgumentNullException))]
public void GetUniqueKey_doesnt_admit_null() {
var mgr = new MappingManager();
mgr.GetUniqueKey(null);
}
[Test]
[ExpectedException(typeof (ArgumentNullException))]
public void GetFields_doesnt_admit_null() {
var mgr = new MappingManager();
mgr.GetFields(null);
}
[Test]
[ExpectedException(typeof (ArgumentNullException))]
public void AddProperty_doesnt_admit_null() {
var mgr = new MappingManager();
mgr.Add(null);
}
[Test]
[ExpectedException(typeof (ArgumentNullException))]
public void AddProperty2_doesnt_admit_null() {
var mgr = new MappingManager();
mgr.Add(null, "");
}
[Test]
[ExpectedException(typeof (ArgumentNullException))]
public void AddProperty3_doesnt_admit_null() {
var mgr = new MappingManager();
mgr.Add(typeof (Entity).GetProperties()[0], null);
}
[Test]
public void Inherited() {
var mgr = new MappingManager();
mgr.Add(typeof(Entity).GetProperty("Id"), "id");
mgr.Add(typeof(InheritedEntity).GetProperty("Description"), "desc");
var entityFields = mgr.GetFields(typeof(Entity));
Assert.AreEqual(1, entityFields.Count);
var inheritedEntityFields = mgr.GetFields(typeof(InheritedEntity));
Assert.AreEqual(2, inheritedEntityFields.Count);
}
[Test]
public void Inherited_gets_id_property_correctly()
{
var mgr = new MappingManager();
mgr.Add(typeof(Entity).GetProperty("Id"), "id");
Assert.IsTrue(mgr.GetFields(typeof(Entity)).ContainsKey("id"), "Entity contains id field");
Assert.IsTrue(mgr.GetFields(typeof(InheritedEntity)).ContainsKey("id"), "InheritedEntity contains id field");
}
[Test]
public void Inherited_gets_id_property_correctly2()
{
var mgr = new MappingManager();
mgr.Add(typeof(InheritedEntity).GetProperty("Id"), "id");
Assert.IsTrue(mgr.GetFields(typeof(InheritedEntity)).ContainsKey("id"), "InheritedEntity contains id field");
Assert.IsTrue(mgr.GetFields(typeof(Entity)).ContainsKey("id"), "Entity contains id field");
}
[Test]
public void GetRegistered() {
var mgr = new MappingManager();
mgr.Add(typeof(Entity).GetProperty("Id"), "id");
var types = mgr.GetRegisteredTypes();
Assert.AreEqual(1, types.Count);
Assert.Contains(types, typeof(Entity));
}
}
public class Entity {
public int Id { get; set; }
}
public class InheritedEntity: Entity {
public string Description { get; set; }
}
}