Skip to content

Commit 26f88b2

Browse files
author
rstam
committed
CSHARP-950: Added integration tests with the server.
1 parent 6a182f4 commit 26f88b2

File tree

1 file changed

+77
-10
lines changed

1 file changed

+77
-10
lines changed

MongoDB.DriverUnitTests/Builders/QueryBuilderTests.cs

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,18 @@ public void TestNear()
434434
var query = Query.Near("loc", 1.1, 2.2);
435435
var selector = "{ '$near' : [1.1, 2.2] }";
436436
Assert.AreEqual(PositiveTest("loc", selector), query.ToJson());
437-
Assert.AreEqual(NegativeTest("loc", selector), Query.Not(query).ToJson());
437+
438+
var collection = Configuration.TestCollection;
439+
collection.Drop();
440+
collection.CreateIndex(IndexKeys.GeoSpatial("loc"));
441+
collection.Insert(new BsonDocument { { "_id", 1 }, { "loc", new BsonArray { 1, 1 } } });
442+
collection.Insert(new BsonDocument { { "_id", 2 }, { "loc", new BsonArray { 2, 2 } } });
443+
444+
query = Query.Near("loc", 0.0, 0.0);
445+
var results = collection.Find(query).ToList();
446+
Assert.AreEqual(2, results.Count);
447+
Assert.AreEqual(1, results[0]["_id"].ToInt32());
448+
Assert.AreEqual(2, results[1]["_id"].ToInt32());
438449
}
439450

440451
[Test]
@@ -443,7 +454,17 @@ public void TestNearWithMaxDistance()
443454
var query = Query.Near("loc", 1.1, 2.2, 3.3);
444455
var expected = "{ 'loc' : { '$near' : [1.1, 2.2], '$maxDistance' : 3.3 } }".Replace("'", "\"");
445456
Assert.AreEqual(expected, query.ToJson());
446-
Assert.AreEqual(NegateArbitraryQuery(expected), Query.Not(query).ToJson());
457+
458+
var collection = Configuration.TestCollection;
459+
collection.Drop();
460+
collection.CreateIndex(IndexKeys.GeoSpatial("loc"));
461+
collection.Insert(new BsonDocument { { "_id", 1 }, { "loc", new BsonArray { 1, 1 } } });
462+
collection.Insert(new BsonDocument { { "_id", 2 }, { "loc", new BsonArray { 2, 2 } } });
463+
464+
query = Query.Near("loc", 0.0, 0.0, 2.0);
465+
var results = collection.Find(query).ToList();
466+
Assert.AreEqual(1, results.Count);
467+
Assert.AreEqual(1, results[0]["_id"].ToInt32());
447468
}
448469

449470
[Test]
@@ -452,7 +473,18 @@ public void TestNearWithSphericalTrue()
452473
var query = Query.Near("loc", 1.1, 2.2, 3.3, true);
453474
var expected = "{ 'loc' : { '$nearSphere' : [1.1, 2.2], '$maxDistance' : 3.3 } }".Replace("'", "\"");
454475
Assert.AreEqual(expected, query.ToJson());
455-
Assert.AreEqual(NegateArbitraryQuery(expected), Query.Not(query).ToJson());
476+
477+
var collection = Configuration.TestCollection;
478+
collection.Drop();
479+
collection.CreateIndex(IndexKeys.GeoSpatial("loc"));
480+
collection.Insert(new BsonDocument { { "_id", 1 }, { "loc", new BsonArray { 1, 1 } } });
481+
collection.Insert(new BsonDocument { { "_id", 2 }, { "loc", new BsonArray { 2, 2 } } });
482+
483+
var radiansPerDegree = 2 * Math.PI / 360.0;
484+
query = Query.Near("loc", 0.0, 0.0, 2.0 * radiansPerDegree, true);
485+
var results = collection.Find(query).ToList();
486+
Assert.AreEqual(1, results.Count);
487+
Assert.AreEqual(1, results[0]["_id"].ToInt32());
456488
}
457489

458490
[Test]
@@ -462,27 +494,62 @@ public void TestNearWithGeoJson()
462494
var query = Query.Near("loc", point);
463495
var selector = "{ '$near' : { '$geometry' : { 'type' : 'Point', 'coordinates' : [40.0, 18.0] } } }";
464496
Assert.AreEqual(PositiveTest("loc", selector), query.ToJson());
465-
Assert.AreEqual(NegativeTest("loc", selector), Query.Not(query).ToJson());
497+
498+
var collection = Configuration.TestCollection;
499+
collection.Drop();
500+
collection.CreateIndex(IndexKeys.GeoSpatial("loc"));
501+
collection.Insert(new BsonDocument { { "_id", 1 }, { "loc", new BsonArray { 1, 1 } } });
502+
collection.Insert(new BsonDocument { { "_id", 2 }, { "loc", new BsonArray { 2, 2 } } });
503+
504+
query = Query.Near("loc", 0.0, 0.0);
505+
var results = collection.Find(query).ToList();
506+
Assert.AreEqual(2, results.Count);
507+
Assert.AreEqual(1, results[0]["_id"].ToInt32());
508+
Assert.AreEqual(2, results[1]["_id"].ToInt32());
466509
}
467510

468511
[Test]
469512
public void TestNearWithGeoJsonWithMaxDistance()
470513
{
471514
var point = GeoJson.Point(GeoJson.Geographic(40, 18));
472515
var query = Query.Near("loc", point, 42);
473-
var selector = "{ '$near' : { '$geometry' : { 'type' : 'Point', 'coordinates' : [40.0, 18.0] }, '$maxDistance' : 42.0 } }".Replace("'", "\"");
474-
Assert.AreEqual(PositiveTest("loc", selector), query.ToJson());
475-
Assert.AreEqual(NegativeTest("loc", selector), Query.Not(query).ToJson());
516+
var expected = "{ 'loc' : { '$near' : { '$geometry' : { 'type' : 'Point', 'coordinates' : [40.0, 18.0] }, '$maxDistance' : 42.0 } } }".Replace("'", "\"");
517+
Assert.AreEqual(expected, query.ToJson());
518+
519+
var collection = Configuration.TestCollection;
520+
collection.Drop();
521+
collection.CreateIndex(IndexKeys.GeoSpatialSpherical("loc"));
522+
collection.Insert(new BsonDocument { { "_id", 1 }, { "loc", GeoJson.Point(GeoJson.Geographic(1, 1)).ToBsonDocument() } });
523+
collection.Insert(new BsonDocument { { "_id", 2 }, { "loc", GeoJson.Point(GeoJson.Geographic(2, 2)).ToBsonDocument() } });
524+
525+
var circumferenceOfTheEarth = 40075000; // meters at the equator, approx
526+
var metersPerDegree = circumferenceOfTheEarth / 360.0;
527+
query = Query.Near("loc", GeoJson.Point(GeoJson.Geographic(0, 0)), 2.0 * metersPerDegree);
528+
var results = collection.Find(query).ToList();
529+
Assert.AreEqual(1, results.Count);
530+
Assert.AreEqual(1, results[0]["_id"].ToInt32());
476531
}
477532

478533
[Test]
479534
public void TestNearWithGeoJsonWithSpherical()
480535
{
481536
var point = GeoJson.Point(GeoJson.Geographic(40, 18));
482537
var query = Query.Near("loc", point, 42, true);
483-
var selector = "{ '$nearSphere' : { '$geometry' : { 'type' : 'Point', 'coordinates' : [40.0, 18.0] }, '$maxDistance' : 42.0 } }".Replace("'", "\"");
484-
Assert.AreEqual(PositiveTest("loc", selector), query.ToJson());
485-
Assert.AreEqual(NegativeTest("loc", selector), Query.Not(query).ToJson());
538+
var expected = "{ 'loc' : { '$nearSphere' : { '$geometry' : { 'type' : 'Point', 'coordinates' : [40.0, 18.0] }, '$maxDistance' : 42.0 } } }".Replace("'", "\"");
539+
Assert.AreEqual(expected, query.ToJson());
540+
541+
var collection = Configuration.TestCollection;
542+
collection.Drop();
543+
collection.CreateIndex(IndexKeys.GeoSpatialSpherical("loc"));
544+
collection.Insert(new BsonDocument { { "_id", 1 }, { "loc", GeoJson.Point(GeoJson.Geographic(1, 1)).ToBsonDocument() } });
545+
collection.Insert(new BsonDocument { { "_id", 2 }, { "loc", GeoJson.Point(GeoJson.Geographic(2, 2)).ToBsonDocument() } });
546+
547+
var circumferenceOfTheEarth = 40075000; // meters at the equator, approx
548+
var metersPerDegree = circumferenceOfTheEarth / 360.0;
549+
query = Query.Near("loc", GeoJson.Point(GeoJson.Geographic(0, 0)), 2.0 * metersPerDegree, true);
550+
var results = collection.Find(query).ToList();
551+
Assert.AreEqual(1, results.Count);
552+
Assert.AreEqual(1, results[0]["_id"].ToInt32());
486553
}
487554

488555
[Test]

0 commit comments

Comments
 (0)