forked from redis/NRedisStack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeo_tutorial.cs
98 lines (78 loc) · 3.09 KB
/
Geo_tutorial.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
// EXAMPLE: geo_tutorial
// HIDE_START
using NRedisStack.Tests;
using StackExchange.Redis;
// HIDE_END
// REMOVE_START
namespace Doc;
[Collection("DocsTests")]
// REMOVE_END
// HIDE_START
public class Geo_tutorial
// REMOVE_START
: AbstractNRedisStackTest, IDisposable
// REMOVE_END
{
// REMOVE_START
public Geo_tutorial(EndpointsFixture fixture) : base(fixture) { }
[SkippableFact]
// REMOVE_END
public void run()
{
//REMOVE_START
// This is needed because we're constructing ConfigurationOptions in the test before calling GetConnection
SkipIfTargetConnectionDoesNotExist(EndpointsFixture.Env.Standalone);
var _ = GetCleanDatabase(EndpointsFixture.Env.Standalone);
//REMOVE_END
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
//REMOVE_START
// Clear any keys here before using them in tests.
db.KeyDelete("bikes:rentable");
//REMOVE_END
// HIDE_END
// STEP_START geoadd
bool res1 = db.GeoAdd("bikes:rentable", -122.27652, 37.805186, "station:1");
Console.WriteLine(res1); // >>> True
bool res2 = db.GeoAdd("bikes:rentable", -122.2674626, 37.8062344, "station:2");
Console.WriteLine(res2); // >>> True
bool res3 = db.GeoAdd("bikes:rentable", -122.2469854, 37.8104049, "station:3");
Console.WriteLine(res3); // >>> True
// STEP_END
// Tests for 'geoadd' step.
// REMOVE_START
Assert.True(res1);
Assert.True(res2);
Assert.True(res3);
// REMOVE_END
// STEP_START geosearch
GeoRadiusResult[] res4 = db.GeoSearch("bikes:rentable",
-122.27652,
37.805186,
new GeoSearchCircle(5, GeoUnit.Kilometers)
);
foreach (GeoRadiusResult member in res4)
{
Console.WriteLine($"Member: '{member.Member}', distance: {member.Distance}, position: {member.Position}");
}
// >>> Member: 'station:1', distance: 0.0001, position: -122.27652043104172 37.80518485897756
// >>> Member: 'station:2', distance: 0.8047, position: -122.26745992898941 37.80623423353753
// >>> Member: 'station:3', distance: 2.6596, position: -122.24698394536972 37.81040384984464
// STEP_END
// Tests for 'geosearch' step.
// REMOVE_START
Assert.Equal(3, res4.Length);
Assert.Equal("station:1", res4[0].Member);
GeoPosition pos1 = res4[0].Position ?? new GeoPosition();
Assert.Equal("-122.27652, 37.80518", $"{pos1.Longitude:F5}, {pos1.Latitude:F5}");
Assert.Equal("station:2", res4[1].Member);
GeoPosition pos2 = res4[1].Position ?? new GeoPosition();
Assert.Equal("-122.26746, 37.80623", $"{pos2.Longitude:F5}, {pos2.Latitude:F5}");
Assert.Equal("station:3", res4[2].Member);
GeoPosition pos3 = res4[2].Position ?? new GeoPosition();
Assert.Equal("-122.24698, 37.81040", $"{pos3.Longitude:F5}, {pos3.Latitude:F5}");
// REMOVE_END
// HIDE_START
}
}
// HIDE_END