-
Notifications
You must be signed in to change notification settings - Fork 55
DOC-4345 added testable version of home page JSON example #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a91681e
DOC-4345 added testable version of home page JSON example
andy-stark-redis fbbb537
DOC-4345 added try...catch around dropIndex call
andy-stark-redis 97e9a89
Merge branch 'master' into DOC-4345-json-intro
andy-stark-redis 068db06
DOC-4345 sorted results before assert checks
andy-stark-redis 6dc26f8
Merge branch 'DOC-4345-json-intro' of github.com:andy-stark-redis/NRe…
andy-stark-redis ce4bacd
DOC-4345 removed test library import
andy-stark-redis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| // EXAMPLE: cs_home_json | ||
|
|
||
| // STEP_START import | ||
| using NRedisStack.RedisStackCommands; | ||
| using NRedisStack.Search; | ||
| using NRedisStack.Search.Aggregation; | ||
| using NRedisStack.Search.Literals.Enums; | ||
| using NRedisStack.Tests; | ||
| using StackExchange.Redis; | ||
| // STEP_END | ||
|
|
||
| // REMOVE_START | ||
| namespace Doc; | ||
| [Collection("DocsTests")] | ||
| // REMOVE_END | ||
|
|
||
| // HIDE_START | ||
| public class HomeJsonExample | ||
| { | ||
|
|
||
| [SkipIfRedis(Is.OSSCluster)] | ||
| public void run() | ||
| { | ||
| // STEP_START connect | ||
| var muxer = ConnectionMultiplexer.Connect("localhost:6379"); | ||
| var db = muxer.GetDatabase(); | ||
| // STEP_END | ||
|
|
||
| //REMOVE_START | ||
| // Clear any keys here before using them in tests. | ||
| db.KeyDelete(new RedisKey[] { "user:1", "user:2", "user:3" }); | ||
| try { db.FT().DropIndex("idx:users"); } catch { } | ||
| //REMOVE_END | ||
| // HIDE_END | ||
|
|
||
| // STEP_START create_data | ||
| var user1 = new | ||
| { | ||
| name = "Paul John", | ||
| email = "[email protected]", | ||
| age = 42, | ||
| city = "London" | ||
| }; | ||
|
|
||
| var user2 = new | ||
| { | ||
| name = "Eden Zamir", | ||
| email = "[email protected]", | ||
| age = 29, | ||
| city = "Tel Aviv" | ||
| }; | ||
|
|
||
| var user3 = new | ||
| { | ||
| name = "Paul Zamir", | ||
| email = "[email protected]", | ||
| age = 35, | ||
| city = "Tel Aviv" | ||
| }; | ||
| // STEP_END | ||
|
|
||
| // STEP_START make_index | ||
| var schema = new Schema() | ||
| .AddTextField(new FieldName("$.name", "name")) | ||
| .AddTagField(new FieldName("$.city", "city")) | ||
| .AddNumericField(new FieldName("$.age", "age")); | ||
|
|
||
| bool indexCreated = db.FT().Create( | ||
| "idx:users", | ||
| new FTCreateParams() | ||
| .On(IndexDataType.JSON) | ||
| .Prefix("user:"), | ||
| schema | ||
| ); | ||
| // STEP_END | ||
|
|
||
| // Tests for 'make_index' step. | ||
| // REMOVE_START | ||
| Assert.True(indexCreated); | ||
| // REMOVE_END | ||
|
|
||
|
|
||
| // STEP_START add_data | ||
| bool user1Set = db.JSON().Set("user:1", "$", user1); | ||
| bool user2Set = db.JSON().Set("user:2", "$", user2); | ||
| bool user3Set = db.JSON().Set("user:3", "$", user3); | ||
| // STEP_END | ||
|
|
||
| // Tests for 'add_data' step. | ||
| // REMOVE_START | ||
| Assert.True(user1Set); | ||
| Assert.True(user2Set); | ||
| Assert.True(user3Set); | ||
| // REMOVE_END | ||
|
|
||
|
|
||
| // STEP_START query1 | ||
| SearchResult findPaulResult = db.FT().Search( | ||
| "idx:users", | ||
| new Query("Paul @age:[30 40]") | ||
| ); | ||
| Console.WriteLine(string.Join( | ||
| ", ", | ||
| findPaulResult.Documents.Select(x => x["json"]) | ||
| )); | ||
| // >>> {"name":"Paul Zamir","email":"[email protected]", ... | ||
| // STEP_END | ||
|
|
||
| // Tests for 'query1' step. | ||
| // REMOVE_START | ||
| Assert.Equal( | ||
| "{\"name\":\"Paul Zamir\",\"email\":\"[email protected]\",\"age\":35,\"city\":\"Tel Aviv\"}", | ||
| string.Join(", ", findPaulResult.Documents.Select(x => x["json"])) | ||
| ); | ||
| // REMOVE_END | ||
|
|
||
|
|
||
| // STEP_START query2 | ||
| var citiesResult = db.FT().Search( | ||
| "idx:users", | ||
| new Query("Paul") | ||
| .ReturnFields(new FieldName("$.city", "city")) | ||
| ); | ||
| Console.WriteLine(string.Join( | ||
| ", ", | ||
| citiesResult.Documents.Select(x => x["city"]).OrderBy(x => x) | ||
| )); | ||
| // >>> London, Tel Aviv | ||
| // STEP_END | ||
|
|
||
| // Tests for 'query2' step. | ||
| // REMOVE_START | ||
| Assert.Equal( | ||
| "London, Tel Aviv", | ||
| string.Join(", ", citiesResult.Documents.Select(x => x["city"]).OrderBy(x => x)) | ||
| ); | ||
| // REMOVE_END | ||
|
|
||
|
|
||
| // STEP_START query3 | ||
| AggregationRequest aggRequest = new AggregationRequest("*") | ||
| .GroupBy("@city", Reducers.Count().As("count")); | ||
|
|
||
| AggregationResult aggResult = db.FT().Aggregate("idx:users", aggRequest); | ||
| IReadOnlyList<Dictionary<string, RedisValue>> resultsList = | ||
| aggResult.GetResults(); | ||
|
|
||
| for (var i = 0; i < resultsList.Count; i++) | ||
| { | ||
| Dictionary<string, RedisValue> item = resultsList.ElementAt(i); | ||
| Console.WriteLine($"{item["city"]} - {item["count"]}"); | ||
| } | ||
| // >>> London - 1 | ||
| // >>> Tel Aviv - 2 | ||
| // STEP_END | ||
|
|
||
| // Tests for 'query3' step. | ||
| // REMOVE_START | ||
| Assert.Equal(2, resultsList.Count); | ||
|
|
||
| var sortedResults = resultsList.OrderBy(x => x["city"]); | ||
| Dictionary<string, RedisValue> testItem = sortedResults.ElementAt(0); | ||
| Assert.Equal("London", testItem["city"]); | ||
| Assert.Equal(1, testItem["count"]); | ||
|
|
||
| testItem = sortedResults.ElementAt(1); | ||
| Assert.Equal("Tel Aviv", testItem["city"]); | ||
| Assert.Equal(2, testItem["count"]); | ||
| // REMOVE_END | ||
|
|
||
|
|
||
| // HIDE_START | ||
| } | ||
| } | ||
| // HIDE_END | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would move this into 'HIDE' region.
at first sight i was thinking this should go into 'REMOVE' region, but then i realized
we left some test project related stuff in 'HIDE' regions overall the docs, is it on purpose ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@atakavci Yes, I've done this on purpose for this example. It's actually for this introductory example on the NRedisStack "home" page. The existing example is written to mention everything right from the start (including imports, etc). You are right, though, that in most other examples, we try to focus on just the relevant commands, not the boilerplate.