Skip to content

Commit c3097bc

Browse files
committed
Add doctest for Search Quickstart
1 parent 7d2b006 commit c3097bc

File tree

3 files changed

+229
-26
lines changed

3 files changed

+229
-26
lines changed

.github/workflows/doctests.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Documentation Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
workflow_dispatch:
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
doctests:
14+
runs-on: ubuntu-latest
15+
services:
16+
redis-stack:
17+
image: redis/redis-stack-server:latest
18+
options: >-
19+
--health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5
20+
ports:
21+
- 6379:6379
22+
steps:
23+
- uses: actions/checkout@v3
24+
- name: .NET Core 6
25+
uses: actions/setup-dotnet@v2
26+
with:
27+
dotnet-version: '6.0.x'
28+
- name: .NET Core 7
29+
uses: actions/setup-dotnet@v2
30+
with:
31+
dotnet-version: '7.0.x'
32+
- name: run tests
33+
working-directory: tests/Doc
34+
run: |
35+
dotnet test

tests/Doc/Doc.csproj

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<IsWindows>$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::get_Windows())))</IsWindows>
5-
<TargetFrameworks Condition=" '$(IsWindows)' == 'true'">net6.0;net7.0;net481</TargetFrameworks>
6-
<TargetFrameworks Condition=" '$(IsWindows)' != 'true'">net6.0;net7.0</TargetFrameworks>
7-
<ImplicitUsings>enable</ImplicitUsings>
8-
<Nullable>enable</Nullable>
9-
<LangVersion>latest</LangVersion>
10-
11-
12-
<IsPackable>false</IsPackable>
13-
<OutputType>Module</OutputType>
14-
</PropertyGroup>
15-
16-
<ItemGroup>
17-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
18-
<PackageReference Include="xunit" Version="2.4.2" />
19-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
20-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21-
<PrivateAssets>all</PrivateAssets>
22-
</PackageReference>
23-
<PackageReference Include="StackExchange.Redis" Version="2.6.104" />
24-
</ItemGroup>
25-
26-
</Project>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<IsWindows>$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::get_Windows())))</IsWindows>
4+
<TargetFrameworks Condition=" '$(IsWindows)' == 'true'">net6.0;net7.0;net481</TargetFrameworks>
5+
<TargetFrameworks Condition=" '$(IsWindows)' != 'true'">net6.0;net7.0</TargetFrameworks>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<LangVersion>latest</LangVersion>
9+
<IsPackable>false</IsPackable>
10+
<OutputType>Module</OutputType>
11+
</PropertyGroup>
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
14+
<PackageReference Include="xunit" Version="2.4.2" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
19+
<PackageReference Include="StackExchange.Redis" Version="2.6.104" />
20+
</ItemGroup>
21+
<ItemGroup>
22+
<ProjectReference Include="..\..\src\NRedisStack\NRedisStack.csproj" />
23+
</ItemGroup>
24+
</Project>

tests/Doc/SearchQuickstartExample.cs

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
// EXAMPLE: search_quickstart
2+
using System;
3+
using NRedisStack;
4+
using NRedisStack.RedisStackCommands;
5+
using NRedisStack.Search;
6+
using NRedisStack.Search.Aggregation;
7+
using NRedisStack.Search.Literals.Enums;
8+
using StackExchange.Redis;
9+
10+
namespace NRedisStack.Doc;
11+
12+
public class SearchQuickstartExample
13+
{
14+
[Fact]
15+
public void run()
16+
{
17+
// STEP_START connect
18+
var redis = ConnectionMultiplexer.Connect("localhost:6379");
19+
var db = redis.GetDatabase();
20+
var ft = db.FT();
21+
var json = db.JSON();
22+
// STEP_END
23+
24+
// REMOVE_START
25+
try { ft.DropIndex("idx:bicycle"); } catch { };
26+
// REMOVE_END
27+
28+
// STEP_START data_sample
29+
var bike1 = new {
30+
Brand = "Diaz Ltd",
31+
Model = "Dealer Sl",
32+
Price = 7315.58M,
33+
Description="The Diaz Ltd Dealer Sl is a reliable choice" +
34+
" for urban cycling. The Diaz Ltd Dealer Sl " +
35+
"is a comfortable choice for urban cycling.",
36+
Condition="used"
37+
};
38+
// STEP_END
39+
40+
var bicycles = new[] {
41+
bike1,
42+
new {
43+
Brand = "Bridges Group",
44+
Model = "Project Pro",
45+
Price = 3610.82M,
46+
Description = "This mountain bike is perfect for mountain biking. The Bridges Group Project Pro is a responsive choice for mountain biking.",
47+
Condition = "used"
48+
},
49+
new {
50+
Brand = "Vega, Cole and Miller",
51+
Model = "Group Advanced",
52+
Price = 8961.42M,
53+
Description = "The Vega, Cole and Miller Group Advanced provides an excellent ride. With its fast carbon frame and 24 gears, this bicycle is perfect for any terrain.",
54+
Condition = "used"
55+
},
56+
new {
57+
Brand = "Powell-Montgomery",
58+
Model = "Angle Race",
59+
Price = 4050.27M,
60+
Description = "The Powell-Montgomery Angle Race is a smooth choice for road cycling. The Powell-Montgomery Angle Race provides a durable ride.",
61+
Condition = "used"
62+
},
63+
new {
64+
Brand = "Gill-Lewis",
65+
Model = "Action Evo",
66+
Price = 283.68M,
67+
Description = "The Gill-Lewis Action Evo provides a smooth ride. The Gill-Lewis Action Evo provides an excellent ride.",
68+
Condition = "used"
69+
},
70+
new {
71+
Brand = "Rodriguez-Guerrero",
72+
Model = "Drama Comp",
73+
Price = 4462.55M,
74+
Description = "This kids bike is perfect for young riders. With its excellent aluminum frame and 12 gears, this bicycle is perfect for any terrain.",
75+
Condition = "new"
76+
},
77+
new {
78+
Brand = "Moore PLC",
79+
Model = "Award Race",
80+
Price = 3790.76M,
81+
Description = "This olive folding bike features a carbon frame and 27.5 inch wheels. This folding bike is perfect for compact storage and transportation.",
82+
Condition = "new"
83+
},
84+
new {
85+
Brand = "Hall, Haley and Hayes",
86+
Model = "Weekend Plus",
87+
Price = 2008.4M,
88+
Description = "The Hall, Haley and Hayes Weekend Plus provides a comfortable ride. This blue kids bike features a steel frame and 29.0 inch wheels.",
89+
Condition = "new"
90+
},
91+
new {
92+
Brand = "Peck-Carson",
93+
Model = "Sun Hybrid",
94+
Price = 9874.95M,
95+
Description = "With its comfortable aluminum frame and 25 gears, this bicycle is perfect for any terrain. The Peck-Carson Sun Hybrid provides a comfortable ride.",
96+
Condition = "new"
97+
},
98+
new {
99+
Brand = "Fowler Ltd",
100+
Model = "Weekend Trail",
101+
Price = 3833.71M,
102+
Description = "The Fowler Ltd Letter Trail is a comfortable choice for transporting cargo. This cargo bike is perfect for transporting cargo.",
103+
Condition = "refurbished"
104+
}
105+
};
106+
107+
// STEP_START define_index
108+
var schema = new Schema()
109+
.AddTextField(new FieldName("$.Brand", "Brand"))
110+
.AddTextField(new FieldName("$.Model", "Model"))
111+
.AddTextField(new FieldName("$.Description", "Description"))
112+
.AddNumericField(new FieldName("$.Price", "Price"))
113+
.AddTagField(new FieldName("$.Condition", "Condition"));
114+
// STEP_END
115+
116+
// STEP_START create_index
117+
ft.Create(
118+
"idx:bicycle",
119+
new FTCreateParams().On(IndexDataType.JSON).Prefix("bicycle:"),
120+
schema);
121+
// STEP_END
122+
123+
// STEP_START add_documents
124+
for (int i=0; i < bicycles.Length; i++)
125+
{
126+
json.Set($"bicycle:{i}", "$", bicycles[i]);
127+
}
128+
// STEP_END
129+
130+
// STEP_START query_single_term_and_num_range
131+
var query = new Query("folding @Price:[1000 4000]");
132+
var res = ft.Search("idx:bicycle", query).Documents;
133+
Console.WriteLine(string.Join("\n", res.Select(x => x["json"])));
134+
// Prints: {"Brand":"Moore PLC","Model":"Award Race","Price":3790.76,"Description":"This olive folding bike features a carbon frame and 27.5 inch wheels. This folding bike is perfect for compact storage and transportation.","Condition":"new"}
135+
// STEP_END
136+
// REMOVE_START
137+
Assert.Single(res);
138+
Assert.Equal("bicycle:6", res[0].Id);
139+
// REMOVE_END
140+
141+
// STEP_START query_single_term_limit_fields
142+
var cargoQuery = new Query("cargo").ReturnFields("Price");
143+
var cargoRes = ft.Search("idx:bicycle", cargoQuery).Documents;
144+
Console.WriteLine(cargoRes.First()["Price"]);
145+
// Prints: 3833.71
146+
// STEP_END
147+
// REMOVE_START
148+
Assert.Single(cargoRes);
149+
Assert.Equal("bicycle:9", cargoRes[0].Id);
150+
// REMOVE_END
151+
152+
// STEP_START simple_aggregation
153+
var request = new AggregationRequest("*").GroupBy("@Condition", Reducers.Count().As("Count"));
154+
var result = ft.Aggregate("idx:bicycle", request);
155+
156+
for (var i=0; i<result.TotalResults; i++)
157+
{
158+
var row = result.GetRow(i);
159+
Console.WriteLine($"{row["Condition"]} - {row["Count"]}");
160+
}
161+
// Prints:
162+
// refurbished - 1
163+
// used - 5
164+
// new - 4
165+
// STEP_END
166+
// REMOVE_START
167+
Assert.Equal(3, result.TotalResults);
168+
// REMOVE_END
169+
}
170+
}

0 commit comments

Comments
 (0)