Skip to content

Commit f85dc86

Browse files
committed
CSHARP-683: restricted within to be for only polygon types.
1 parent 35b72f8 commit f85dc86

File tree

3 files changed

+151
-4
lines changed

3 files changed

+151
-4
lines changed

MongoDB.Driver/Builders/QueryBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ public static IMongoQuery Where(BsonJavaScript javascript)
696696
/// <param name="name">The name of the element to test.</param>
697697
/// <param name="geometry">The geometry.</param>
698698
/// <returns>An IMongoQuery.</returns>
699-
public static IMongoQuery Within<TCoordinates>(string name, GeoJsonGeometry<TCoordinates> geometry)
699+
public static IMongoQuery Within<TCoordinates>(string name, GeoJsonPolygon<TCoordinates> geometry)
700700
where TCoordinates : GeoJsonCoordinates
701701
{
702702
if (name == null)
@@ -1319,7 +1319,7 @@ public static IMongoQuery Where(Expression<Func<TDocument, bool>> expression)
13191319
/// <param name="memberExpression">The member expression.</param>
13201320
/// <param name="geometry">The geometry.</param>
13211321
/// <returns>An IMongoQuery.</returns>
1322-
public static IMongoQuery Within<TMember, TCoordinates>(Expression<Func<TDocument, TMember>> memberExpression, GeoJsonGeometry<TCoordinates> geometry)
1322+
public static IMongoQuery Within<TMember, TCoordinates>(Expression<Func<TDocument, TMember>> memberExpression, GeoJsonPolygon<TCoordinates> geometry)
13231323
where TCoordinates : GeoJsonCoordinates
13241324
{
13251325
return new QueryBuilder<TDocument>().Within(memberExpression, geometry);
@@ -1550,7 +1550,7 @@ public IMongoQuery GeoIntersects<TMember, TCoordinates>(Expression<Func<TDocumen
15501550
{
15511551
if (memberExpression == null)
15521552
{
1553-
throw new ArgumentNullException("name");
1553+
throw new ArgumentNullException("memberExpression");
15541554
}
15551555
if (geometry == null)
15561556
{
@@ -2184,7 +2184,7 @@ public IMongoQuery Where(Expression<Func<TDocument, bool>> expression)
21842184
/// <param name="memberExpression">The member expression.</param>
21852185
/// <param name="geometry">The geometry.</param>
21862186
/// <returns>An IMongoQuery.</returns>
2187-
public IMongoQuery Within<TMember, TCoordinates>(Expression<Func<TDocument, TMember>> memberExpression, GeoJsonGeometry<TCoordinates> geometry)
2187+
public IMongoQuery Within<TMember, TCoordinates>(Expression<Func<TDocument, TMember>> memberExpression, GeoJsonPolygon<TCoordinates> geometry)
21882188
where TCoordinates : GeoJsonCoordinates
21892189
{
21902190
if (memberExpression == null)
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/* Copyright 2010-2013 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using System.Text;
20+
using MongoDB.Bson;
21+
using MongoDB.Bson.Serialization.Attributes;
22+
using MongoDB.Driver;
23+
using MongoDB.Driver.Builders;
24+
using MongoDB.Driver.GeoJsonObjectModel;
25+
using NUnit.Framework;
26+
27+
namespace MongoDB.DriverUnitTests.Builders
28+
{
29+
public class QueryBuilderGeoJsonIntegrationTests
30+
{
31+
private MongoCollection<GeoClass> _collection;
32+
33+
private class GeoClass
34+
{
35+
public int Id { get; set; }
36+
37+
[BsonElement("loc")]
38+
public GeoJsonPoint<GeoJson2DGeographicCoordinates> Location { get; set; }
39+
40+
[BsonElement("sur")]
41+
public GeoJsonPolygon<GeoJson2DGeographicCoordinates> Surrounding { get; set; }
42+
}
43+
44+
[SetUp]
45+
public void SetUp()
46+
{
47+
var db = Configuration.TestDatabase;
48+
_collection = db.GetCollection<GeoClass>("geo");
49+
50+
_collection.Drop();
51+
_collection.EnsureIndex(IndexKeys<GeoClass>.GeoSpatialSpherical(x => x.Location));
52+
_collection.EnsureIndex(IndexKeys<GeoClass>.GeoSpatialSpherical(x => x.Surrounding));
53+
54+
var doc = new GeoClass
55+
{
56+
Id = 1,
57+
Location = GeoJson.Point(GeoJson.Geographic(40.5, 18.5)),
58+
Surrounding = GeoJson.Polygon(
59+
GeoJson.Geographic(40, 18),
60+
GeoJson.Geographic(40, 19),
61+
GeoJson.Geographic(41, 19),
62+
GeoJson.Geographic(40, 18))
63+
};
64+
65+
_collection.Save(doc);
66+
}
67+
68+
[Test]
69+
public void TestGeoIntersects()
70+
{
71+
var server = Configuration.TestServer;
72+
server.Connect();
73+
if (server.BuildInfo.Version >= new Version(2, 3, 2))
74+
{
75+
var point = GeoJson.Point(GeoJson.Geographic(40, 18));
76+
77+
var query = Query<GeoClass>.GeoIntersects(x => x.Surrounding, point);
78+
79+
var results = _collection.Count(query);
80+
81+
Assert.AreEqual(1, results);
82+
}
83+
}
84+
85+
[Test]
86+
public void TestNear()
87+
{
88+
var server = Configuration.TestServer;
89+
server.Connect();
90+
if (server.BuildInfo.Version >= new Version(2, 3, 2))
91+
{
92+
var point = GeoJson.Point(GeoJson.Geographic(40, 18));
93+
94+
var query = Query<GeoClass>.Near(x => x.Location, point);
95+
96+
var results = _collection.Count(query);
97+
98+
Assert.AreEqual(1, results);
99+
}
100+
}
101+
102+
[Test]
103+
public void TestWithin()
104+
{
105+
var server = Configuration.TestServer;
106+
server.Connect();
107+
if (server.BuildInfo.Version >= new Version(2, 3, 2))
108+
{
109+
var polygon = GeoJson.Polygon(
110+
GeoJson.Geographic(40, 18),
111+
GeoJson.Geographic(40, 19),
112+
GeoJson.Geographic(41, 19),
113+
GeoJson.Geographic(41, 18),
114+
GeoJson.Geographic(40, 18));
115+
116+
var query = Query<GeoClass>.Within(x => x.Location, polygon);
117+
118+
var results = _collection.Count(query);
119+
120+
Assert.AreEqual(1, results);
121+
}
122+
}
123+
124+
[Test]
125+
public void TestWithinNotFound()
126+
{
127+
var server = Configuration.TestServer;
128+
server.Connect();
129+
if (server.BuildInfo.Version >= new Version(2, 3, 2))
130+
{
131+
var polygon = GeoJson.Polygon(
132+
GeoJson.Geographic(41, 19),
133+
GeoJson.Geographic(41, 20),
134+
GeoJson.Geographic(42, 20),
135+
GeoJson.Geographic(42, 19),
136+
GeoJson.Geographic(41, 19));
137+
138+
var query = Query<GeoClass>.Within(x => x.Location, polygon);
139+
140+
var results = _collection.Count(query);
141+
142+
Assert.AreEqual(0, results);
143+
}
144+
}
145+
}
146+
}

MongoDB.DriverUnitTests/MongoDB.DriverUnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<Compile Include="Builders\IndexOptionsBuilderTests.cs" />
9999
<Compile Include="Builders\MapReduceOptionsBuilderTests.cs" />
100100
<Compile Include="Builders\DeprecatedQueryBuilderTests.cs" />
101+
<Compile Include="Builders\QueryBuilderGeoJsonIntegrationTests.cs" />
101102
<Compile Include="Builders\QueryBuilderTests.cs" />
102103
<Compile Include="Builders\SortByBuilderTests.cs" />
103104
<Compile Include="Builders\QueryBuilderTypedTests.cs" />

0 commit comments

Comments
 (0)