|
| 1 | +using NRedisStack.Core.DataTypes; |
| 2 | +using NRedisStack.RedisStackCommands; |
| 3 | +using NRedisStack.Search.Literals.Enums; |
| 4 | +using NRedisStack.Search; |
| 5 | +using NRedisStack; |
| 6 | +using StackExchange.Redis; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Threading; |
| 9 | +using System; |
| 10 | +using static NRedisStack.Search.Schema; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// The goal of this code is to verify that the binary package is working correctly. |
| 14 | +/// It should be orchestrated in such a way that the binary package is retrieved from |
| 15 | +/// a local NuGet source and then the code is executed. |
| 16 | +/// </summary> |
| 17 | +public class PackageVerification |
| 18 | +{ |
| 19 | + public static void Main() |
| 20 | + { |
| 21 | + ConfigurationOptions configurationOptions = new ConfigurationOptions(); |
| 22 | + configurationOptions.SyncTimeout = 20000; |
| 23 | + configurationOptions.EndPoints.Add("localhost:6379"); |
| 24 | + ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(configurationOptions); |
| 25 | + |
| 26 | + IDatabase db = redis.GetDatabase(); |
| 27 | + db.Execute("FLUSHALL"); |
| 28 | + |
| 29 | + IJsonCommands json = db.JSON(); |
| 30 | + |
| 31 | + json.Set("product:15970", "$", new |
| 32 | + { |
| 33 | + id = 15970, |
| 34 | + description = "Turtle Navy Blue Shirt", |
| 35 | + price = 34.95, |
| 36 | + }); |
| 37 | + |
| 38 | + json.Set("product:59263", "$", new |
| 39 | + { |
| 40 | + id = 59263, |
| 41 | + description = "Titan Silver Watch", |
| 42 | + price = 129.99, |
| 43 | + }); |
| 44 | + |
| 45 | + ISearchCommands ft = db.FT(); |
| 46 | + |
| 47 | + try |
| 48 | + { |
| 49 | + ft.DropIndex("idx1"); |
| 50 | + } |
| 51 | + catch |
| 52 | + { |
| 53 | + } |
| 54 | + |
| 55 | + ft.Create("idx1", new FTCreateParams().On(IndexDataType.JSON) |
| 56 | + .Prefix("product:"), |
| 57 | + new Schema().AddNumericField(new FieldName("$.id", "id")) |
| 58 | + .AddTextField(new FieldName("$.description", "description")) |
| 59 | + .AddNumericField(new FieldName("$.price", "price"))); |
| 60 | + |
| 61 | + // wait for index to be created |
| 62 | + Thread.Sleep(2000); |
| 63 | + |
| 64 | + List<string> results = ft.Search("idx1", new Query("@description:Blue")).ToJson(); |
| 65 | + |
| 66 | + Assert(1 == results.Count); |
| 67 | + |
| 68 | + var expected = "{\"id\":15970,\"description\":\"Turtle Navy Blue Shirt\",\"price\":34.95}"; |
| 69 | + Assert(expected == results[0]); |
| 70 | + |
| 71 | + Console.WriteLine("All good."); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Poor Man's assert, since we don't want to depend on NUnit. |
| 76 | + /// </summary> |
| 77 | + private static void Assert(bool condition) |
| 78 | + { |
| 79 | + if (!condition) |
| 80 | + { |
| 81 | + throw new System.Exception(); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments